tdf#154546 skip dispatch when presenter controller is not set
[LibreOffice.git] / sal / osl / unx / process_impl.cxx
bloba61df87c2cff7a597bb0d59d5ae9232ed5668f15
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <osl/process.h>
24 #include <limits.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <osl/diagnose.h>
30 #include <osl/file.hxx>
31 #include <osl/module.h>
32 #include <osl/thread.h>
33 #include <rtl/alloc.h>
34 #include <rtl/ustring.hxx>
35 #include <rtl/strbuf.h>
36 #include <sal/log.hxx>
38 #include "file_path_helper.hxx"
40 #include "uunxapi.hxx"
41 #include "nlsupport.hxx"
43 #ifdef ANDROID
44 #include <osl/detail/android-bootstrap.h>
45 #endif
47 #if defined(MACOSX) || defined(IOS)
48 #include <mach-o/dyld.h>
50 namespace {
52 oslProcessError bootstrap_getExecutableFile(rtl_uString ** ppFileURL)
54 oslProcessError result = osl_Process_E_NotFound;
56 char buffer[PATH_MAX];
57 uint32_t buflen = sizeof(buffer);
59 if (_NSGetExecutablePath (buffer, &buflen) == 0)
61 /* Determine absolute path. */
62 char abspath[PATH_MAX];
63 if (realpath (buffer, abspath) != nullptr)
65 /* Convert from utf8 to unicode. */
66 rtl_uString * pAbsPath = nullptr;
67 rtl_string2UString (
68 &pAbsPath,
69 abspath, rtl_str_getLength (abspath),
70 RTL_TEXTENCODING_UTF8,
71 OSTRING_TO_OUSTRING_CVTFLAGS);
73 if (pAbsPath)
75 /* Convert from path to url. */
76 if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
78 /* Success. */
79 result = osl_Process_E_None;
81 rtl_uString_release (pAbsPath);
86 return result;
91 #else
92 #include <dlfcn.h>
94 namespace {
96 oslProcessError bootstrap_getExecutableFile(rtl_uString ** ppFileURL)
98 oslProcessError result = osl_Process_E_NotFound;
100 #ifdef EMSCRIPTEN
101 // Just return some dummy file: URL for now to see what happens
102 OUString fileURL = "vnd.sun.star.pathname:/instdir/program/soffice";
103 rtl_uString_acquire(fileURL.pData);
104 *ppFileURL = fileURL.pData;
105 return osl_Process_E_None;
106 #else
107 #ifdef ANDROID
108 /* Now with just a single DSO, this one from lo-bootstrap.c is as good as
109 * any */
110 void * addr = dlsym (RTLD_DEFAULT, "JNI_OnLoad");
111 #else
112 #if defined __linux
113 // The below code looking for "main" with dlsym() will typically
114 // fail, as there is little reason for "main" to be exported, in
115 // the dlsym() sense, from an executable. But Linux has
116 // /proc/self/exe, try using that.
117 char buf[PATH_MAX];
118 int rc = readlink("/proc/self/exe", buf, sizeof(buf));
119 if (rc > 0 && rc < PATH_MAX)
121 buf[rc] = '\0';
122 OUString path = OUString::fromUtf8(buf);
123 OUString fileURL;
124 if (osl::File::getFileURLFromSystemPath(path, fileURL) == osl::File::E_None)
126 rtl_uString_acquire(fileURL.pData);
127 *ppFileURL = fileURL.pData;
128 return osl_Process_E_None;
131 #endif
132 /* Determine address of "main()" function. */
133 void * addr = dlsym (RTLD_DEFAULT, "main");
134 #endif
135 if (addr != nullptr)
137 /* Determine module URL. */
138 if (osl_getModuleURLFromAddress (addr, ppFileURL))
140 /* Success. */
141 result = osl_Process_E_None;
145 return result;
146 #endif
151 #endif
153 namespace {
155 struct CommandArgs_Impl
157 pthread_mutex_t m_mutex;
158 sal_uInt32 m_nCount;
159 rtl_uString ** m_ppArgs;
164 static struct CommandArgs_Impl g_command_args =
166 PTHREAD_MUTEX_INITIALIZER,
168 nullptr
171 oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
173 pthread_mutex_lock (&(g_command_args.m_mutex));
174 if (g_command_args.m_nCount == 0)
176 pthread_mutex_unlock (&(g_command_args.m_mutex));
177 return bootstrap_getExecutableFile(ppustrFile);
180 /* CommandArgs set. Obtain argv[0]. */
181 rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
182 pthread_mutex_unlock (&(g_command_args.m_mutex));
183 return osl_Process_E_None;
186 sal_uInt32 SAL_CALL osl_getCommandArgCount()
188 sal_uInt32 result = 0;
190 pthread_mutex_lock (&(g_command_args.m_mutex));
191 SAL_INFO_IF(
192 g_command_args.m_nCount == 0, "sal.osl",
193 "osl_getCommandArgCount w/o prior call to osl_setCommandArgs");
194 if (g_command_args.m_nCount > 0)
195 result = g_command_args.m_nCount - 1;
196 pthread_mutex_unlock (&(g_command_args.m_mutex));
198 return result;
201 oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
203 oslProcessError result = osl_Process_E_NotFound;
205 pthread_mutex_lock (&(g_command_args.m_mutex));
206 assert(g_command_args.m_nCount > 0);
207 if (g_command_args.m_nCount > (nArg + 1))
209 rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
210 result = osl_Process_E_None;
212 pthread_mutex_unlock (&(g_command_args.m_mutex));
214 return result;
217 void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
219 assert(argc > 0);
220 pthread_mutex_lock (&(g_command_args.m_mutex));
221 SAL_WARN_IF(g_command_args.m_nCount != 0, "sal.osl", "args already set");
222 if (g_command_args.m_nCount == 0)
224 rtl_uString** ppArgs = static_cast<rtl_uString**>(rtl_allocateZeroMemory (argc * sizeof(rtl_uString*)));
225 if (ppArgs != nullptr)
227 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
228 for (int i = 0; i < argc; i++)
230 rtl_string2UString (
231 &(ppArgs[i]),
232 argv[i], rtl_str_getLength (argv[i]), encoding,
233 OSTRING_TO_OUSTRING_CVTFLAGS);
235 if (ppArgs[0] != nullptr)
237 #if HAVE_FEATURE_MACOSX_SANDBOX
238 // If we are called with a relative path in argv[0] in a sandboxed process
239 // osl::realpath() fails. So just use bootstrap_getExecutableFile() instead.
240 // Somewhat silly to use argv[0] and tediously figure out the absolute path from it
241 // anyway.
242 bootstrap_getExecutableFile(&ppArgs[0]);
243 OUString pArg0(ppArgs[0]);
244 osl_getFileURLFromSystemPath (pArg0.pData, &(ppArgs[0]));
245 #else
246 #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
247 /* see @ osl_getExecutableFile(). */
248 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), '/') == -1)
250 const OUString PATH ("PATH");
252 rtl_uString * pSearchPath = nullptr;
253 osl_getEnvironment (PATH.pData, &pSearchPath);
254 if (pSearchPath)
256 rtl_uString * pSearchResult = nullptr;
257 osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
258 if (pSearchResult)
260 rtl_uString_assign (&(ppArgs[0]), pSearchResult);
261 rtl_uString_release (pSearchResult);
263 rtl_uString_release (pSearchPath);
266 #endif
267 OUString pArg0;
268 if (osl::realpath (OUString::unacquired(&ppArgs[0]), pArg0))
270 osl_getFileURLFromSystemPath (pArg0.pData, &(ppArgs[0]));
272 #endif // !HAVE_FEATURE_MACOSX_SANDBOX
274 g_command_args.m_nCount = argc;
275 g_command_args.m_ppArgs = ppArgs;
278 pthread_mutex_unlock (&(g_command_args.m_mutex));
281 oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
283 oslProcessError result = osl_Process_E_NotFound;
284 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
285 rtl_String* pstr_env_var = nullptr;
287 OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
288 OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
290 rtl_uString2String(
291 &pstr_env_var,
292 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
293 OUSTRING_TO_OSTRING_CVTFLAGS);
294 if (pstr_env_var != nullptr)
296 const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
297 if (p_env_var != nullptr)
299 rtl_string2UString(
300 ppustrValue,
301 p_env_var, strlen(p_env_var), encoding,
302 OSTRING_TO_OUSTRING_CVTFLAGS);
303 OSL_ASSERT(*ppustrValue != nullptr);
305 result = osl_Process_E_None;
307 rtl_string_release(pstr_env_var);
310 return result;
313 oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
315 oslProcessError result = osl_Process_E_Unknown;
316 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
317 rtl_String* pstr_env_var = nullptr;
318 rtl_String* pstr_val = nullptr;
320 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
321 OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
323 rtl_uString2String(
324 &pstr_env_var,
325 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
326 OUSTRING_TO_OSTRING_CVTFLAGS);
328 rtl_uString2String(
329 &pstr_val,
330 rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
331 OUSTRING_TO_OSTRING_CVTFLAGS);
333 if (pstr_env_var != nullptr && pstr_val != nullptr)
335 #if defined (__sun)
336 rtl_String * pBuffer = NULL;
338 sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
339 rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
340 pstr_env_var );
341 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
342 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
343 rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
345 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
347 if (putenv(rtl_string_getStr(pBuffer)) == 0)
348 result = osl_Process_E_None;
349 else
350 rtl_string_release(pBuffer);
351 #else
352 if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
353 result = osl_Process_E_None;
354 #endif
357 if (pstr_val)
358 rtl_string_release(pstr_val);
360 if (pstr_env_var != nullptr)
361 rtl_string_release(pstr_env_var);
363 return result;
366 oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
368 oslProcessError result = osl_Process_E_Unknown;
369 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
370 rtl_String* pstr_env_var = nullptr;
372 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
374 rtl_uString2String(
375 &pstr_env_var,
376 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
377 OUSTRING_TO_OSTRING_CVTFLAGS);
379 if (pstr_env_var)
381 #if defined (__sun)
382 rtl_String * pBuffer = NULL;
384 sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
385 rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
386 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
388 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
390 if (putenv(rtl_string_getStr(pBuffer)) == 0)
391 result = osl_Process_E_None;
392 else
393 rtl_string_release(pBuffer);
394 #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
395 // MacOSX baseline is 10.4, which has an old-school void return
396 // for unsetenv.
397 // See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
398 unsetenv(rtl_string_getStr(pstr_env_var));
399 result = osl_Process_E_None;
400 #else
401 if (unsetenv(rtl_string_getStr(pstr_env_var)) == 0)
402 result = osl_Process_E_None;
403 #endif
404 rtl_string_release(pstr_env_var);
407 return result;
410 oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
412 oslProcessError result = osl_Process_E_Unknown;
413 char buffer[PATH_MAX];
415 OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
417 if (getcwd (buffer, sizeof(buffer)) != nullptr)
419 rtl_uString* ustrTmp = nullptr;
421 rtl_string2UString(
422 &ustrTmp,
423 buffer, strlen(buffer), osl_getThreadTextEncoding(),
424 OSTRING_TO_OUSTRING_CVTFLAGS);
425 if (ustrTmp != nullptr)
427 if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
428 result = osl_Process_E_None;
429 rtl_uString_release (ustrTmp);
433 return result;
436 namespace {
438 struct ProcessLocale_Impl
440 pthread_mutex_t m_mutex;
441 rtl_Locale * m_pLocale;
446 static struct ProcessLocale_Impl g_process_locale =
448 PTHREAD_MUTEX_INITIALIZER,
449 nullptr
452 oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
454 oslProcessError result = osl_Process_E_Unknown;
455 OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
456 if (ppLocale)
458 pthread_mutex_lock(&(g_process_locale.m_mutex));
460 if (g_process_locale.m_pLocale == nullptr)
461 imp_getProcessLocale (&(g_process_locale.m_pLocale));
462 *ppLocale = g_process_locale.m_pLocale;
463 result = osl_Process_E_None;
465 pthread_mutex_unlock (&(g_process_locale.m_mutex));
467 return result;
470 oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
472 OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
474 pthread_mutex_lock(&(g_process_locale.m_mutex));
475 g_process_locale.m_pLocale = pLocale;
476 pthread_mutex_unlock (&(g_process_locale.m_mutex));
478 return osl_Process_E_None;
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */