Bump for 3.6-28
[LibreOffice.git] / sal / osl / unx / process_impl.cxx
blobf2b90b8744f5f6894a10299172b41b777cf9a392
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "osl/process.h"
32 #ifndef INCLUDED_LIMITS_H
33 #include <limits.h>
34 #define INCLUDED_LIMITS_H
35 #endif
37 #ifndef INCLUDED_PTHREAD_H
38 #include <pthread.h>
39 #define INCLUDED_PTHREAD_H
40 #endif
42 #ifndef INCLUDED_STDLIB_H
43 #include <stdlib.h>
44 #define INCLUDED_STDLIB_H
45 #endif
47 #ifndef INCLUDED_STRING_H
48 #include <string.h>
49 #define INCLUDED_STRING_H
50 #endif
51 #include "osl/diagnose.h"
52 #include "osl/file.h"
53 #include "osl/module.h"
54 #include "osl/thread.h"
55 #include "rtl/ustring.hxx"
56 #include "rtl/strbuf.h"
58 #include "file_path_helper.h"
60 #include "uunxapi.h"
62 #ifdef ANDROID
63 #include <osl/detail/android-bootstrap.h>
64 #endif
66 /***************************************
67 osl_bootstrap_getExecutableFile_Impl().
69 @internal
70 @see rtl_bootstrap
71 @see #i37371#
73 **************************************/
75 extern "C" oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
76 rtl_uString ** ppFileURL
77 ) SAL_THROW_EXTERN_C();
80 #if defined(MACOSX) || defined(IOS)
81 #include <mach-o/dyld.h>
83 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
84 rtl_uString ** ppFileURL
85 ) SAL_THROW_EXTERN_C()
87 oslProcessError result = osl_Process_E_NotFound;
89 char buffer[PATH_MAX];
90 size_t buflen = sizeof(buffer);
92 if (_NSGetExecutablePath (buffer, (uint32_t*)&buflen) == 0)
94 /* Determine absolute path. */
95 char abspath[PATH_MAX];
96 if (realpath (buffer, abspath) != 0)
98 /* Convert from utf8 to unicode. */
99 rtl_uString * pAbsPath = 0;
100 rtl_string2UString (
101 &(pAbsPath),
102 abspath, rtl_str_getLength (abspath),
103 RTL_TEXTENCODING_UTF8,
104 OSTRING_TO_OUSTRING_CVTFLAGS);
106 if (pAbsPath)
108 /* Convert from path to url. */
109 if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
111 /* Success. */
112 result = osl_Process_E_None;
114 rtl_uString_release (pAbsPath);
119 return (result);
122 #else
123 #include <dlfcn.h>
125 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
126 rtl_uString ** ppFileURL
127 ) SAL_THROW_EXTERN_C()
129 oslProcessError result = osl_Process_E_NotFound;
131 #ifdef ANDROID
132 /* On Android we in theory want the address of the "lo_main()"
133 * function, as that is what corresponds to "main()" in
134 * LibreOffice programs on normal desktop OSes.
136 * But that is true only for apps with a "native activity", using
137 * <sal/main.h> and the org.libreoffice.android.Bootstrap
138 * mechanism. For more normal (?) Android apps that just use
139 * LibreOffice libraries (components) where the main program is in
140 * Java, that just use LibreOffice libraries, there is no
141 * lo_main(). (Note that we don't know for sure yet how
142 * complicated it might be to write such Android apps...)
144 * Maybe best to just pick some function in liblo-bootstrap.so
145 * which also such Java apps *must* load as the very first
146 * LibreOffice native library. We store all LibreOffice native
147 * shared libraries an app uses in the same folder anyway, so it
148 * doesn't really matter.
150 void * addr = (void *) &lo_dlopen;
151 #else
152 /* Determine address of "main()" function. */
153 void * addr = dlsym (RTLD_DEFAULT, "main");
154 #endif
155 if (addr != 0)
157 /* Determine module URL. */
158 if (osl_getModuleURLFromAddress (addr, ppFileURL))
160 /* Success. */
161 result = osl_Process_E_None;
165 /* Fallback to ordinary osl_getExecutableFile(). */
166 if (result == osl_Process_E_NotFound)
167 result = osl_getExecutableFile (ppFileURL);
169 return (result);
172 #endif
174 /***************************************
175 CommandArgs_Impl.
176 **************************************/
177 struct CommandArgs_Impl
179 pthread_mutex_t m_mutex;
180 sal_uInt32 m_nCount;
181 rtl_uString ** m_ppArgs;
184 static struct CommandArgs_Impl g_command_args =
186 PTHREAD_MUTEX_INITIALIZER,
191 /***************************************
192 osl_getExecutableFile().
193 **************************************/
194 oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
196 oslProcessError result = osl_Process_E_NotFound;
198 pthread_mutex_lock (&(g_command_args.m_mutex));
199 OSL_ASSERT(g_command_args.m_nCount > 0);
200 if (g_command_args.m_nCount > 0)
202 /* CommandArgs set. Obtain argv[0]. */
203 rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
204 result = osl_Process_E_None;
206 pthread_mutex_unlock (&(g_command_args.m_mutex));
208 return (result);
211 /***************************************
212 osl_getCommandArgCount().
213 **************************************/
214 sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
216 sal_uInt32 result = 0;
218 pthread_mutex_lock (&(g_command_args.m_mutex));
219 if (g_command_args.m_nCount == 0) {
220 OSL_TRACE(
221 OSL_LOG_PREFIX
222 "osl_getCommandArgCount w/o prior call to osl_setCommandArgs");
224 if (g_command_args.m_nCount > 0)
225 result = g_command_args.m_nCount - 1;
226 pthread_mutex_unlock (&(g_command_args.m_mutex));
228 return (result);
231 /***************************************
232 osl_getCommandArg().
233 **************************************/
234 oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
236 oslProcessError result = osl_Process_E_NotFound;
238 pthread_mutex_lock (&(g_command_args.m_mutex));
239 OSL_ASSERT(g_command_args.m_nCount > 0);
240 if (g_command_args.m_nCount > (nArg + 1))
242 rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
243 result = osl_Process_E_None;
245 pthread_mutex_unlock (&(g_command_args.m_mutex));
247 return (result);
250 /***************************************
251 osl_setCommandArgs().
252 **************************************/
253 void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
255 OSL_ASSERT(argc > 0);
256 pthread_mutex_lock (&(g_command_args.m_mutex));
257 OSL_ENSURE (g_command_args.m_nCount == 0, "osl_setCommandArgs(): CommandArgs already set.");
258 if (g_command_args.m_nCount == 0)
260 rtl_uString** ppArgs = (rtl_uString**)rtl_allocateZeroMemory (argc * sizeof(rtl_uString*));
261 if (ppArgs != 0)
263 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
264 for (int i = 0; i < argc; i++)
266 rtl_string2UString (
267 &(ppArgs[i]),
268 argv[i], rtl_str_getLength (argv[i]), encoding,
269 OSTRING_TO_OUSTRING_CVTFLAGS);
271 if (ppArgs[0] != 0)
273 #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
274 /* see @ osl_getExecutableFile(). */
275 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), sal_Unicode('/')) == -1)
277 const rtl::OUString PATH (RTL_CONSTASCII_USTRINGPARAM("PATH"));
279 rtl_uString * pSearchPath = 0;
280 osl_getEnvironment (PATH.pData, &pSearchPath);
281 if (pSearchPath)
283 rtl_uString * pSearchResult = 0;
284 osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
285 if (pSearchResult)
287 rtl_uString_assign (&(ppArgs[0]), pSearchResult);
288 rtl_uString_release (pSearchResult);
290 rtl_uString_release (pSearchPath);
293 #endif
294 rtl_uString * pArg0 = 0;
295 if (realpath_u (ppArgs[0], &pArg0))
297 osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
298 rtl_uString_release (pArg0);
301 g_command_args.m_nCount = argc;
302 g_command_args.m_ppArgs = ppArgs;
305 pthread_mutex_unlock (&(g_command_args.m_mutex));
308 /***************************************
309 osl_getEnvironment().
310 **************************************/
311 oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
313 oslProcessError result = osl_Process_E_NotFound;
314 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
315 rtl_String* pstr_env_var = 0;
317 OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
318 OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
320 rtl_uString2String(
321 &pstr_env_var,
322 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
323 OUSTRING_TO_OSTRING_CVTFLAGS);
324 if (pstr_env_var != 0)
326 const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
327 if (p_env_var != 0)
329 rtl_string2UString(
330 ppustrValue,
331 p_env_var, strlen(p_env_var), encoding,
332 OSTRING_TO_OUSTRING_CVTFLAGS);
333 OSL_ASSERT(*ppustrValue != NULL);
335 result = osl_Process_E_None;
337 rtl_string_release(pstr_env_var);
340 return (result);
343 /***************************************
344 osl_setEnvironment().
345 **************************************/
346 oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
348 oslProcessError result = osl_Process_E_Unknown;
349 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
350 rtl_String* pstr_env_var = 0;
351 rtl_String* pstr_val = 0;
353 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
354 OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
356 rtl_uString2String(
357 &pstr_env_var,
358 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
359 OUSTRING_TO_OSTRING_CVTFLAGS);
361 rtl_uString2String(
362 &pstr_val,
363 rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
364 OUSTRING_TO_OSTRING_CVTFLAGS);
366 if (pstr_env_var != 0 && pstr_val != 0)
368 #if defined (SOLARIS)
369 rtl_String * pBuffer = NULL;
371 sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
372 rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
373 pstr_env_var );
374 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
375 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
376 rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
378 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
380 if (putenv(rtl_string_getStr(pBuffer)) == 0)
381 result = osl_Process_E_None;
382 else
383 rtl_string_release(pBuffer);
384 #else
385 if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
386 result = osl_Process_E_None;
387 #endif
390 if (pstr_val)
391 rtl_string_release(pstr_val);
393 if (pstr_env_var != 0)
394 rtl_string_release(pstr_env_var);
396 return (result);
399 /***************************************
400 osl_clearEnvironment().
401 **************************************/
402 oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
404 oslProcessError result = osl_Process_E_Unknown;
405 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
406 rtl_String* pstr_env_var = 0;
408 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
410 rtl_uString2String(
411 &pstr_env_var,
412 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
413 OUSTRING_TO_OSTRING_CVTFLAGS);
415 if (pstr_env_var)
417 #if defined (SOLARIS)
418 rtl_String * pBuffer = NULL;
420 sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
421 rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
422 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
424 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
426 if (putenv(rtl_string_getStr(pBuffer)) == 0)
427 result = osl_Process_E_None;
428 else
429 rtl_string_release(pBuffer);
430 #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
431 //MacOSX baseline is 10.4, which has an old-school void return
432 //for unsetenv.
433 //See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
434 unsetenv(rtl_string_getStr(pstr_env_var));
435 result = osl_Process_E_None;
436 #else
437 if (unsetenv(rtl_string_getStr(pstr_env_var)) == 0)
438 result = osl_Process_E_None;
439 #endif
440 rtl_string_release(pstr_env_var);
443 return (result);
447 /***************************************
448 osl_getProcessWorkingDir().
449 **************************************/
450 oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
452 oslProcessError result = osl_Process_E_Unknown;
453 char buffer[PATH_MAX];
455 OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
457 if (getcwd (buffer, sizeof(buffer)) != 0)
459 rtl_uString* ustrTmp = 0;
461 rtl_string2UString(
462 &ustrTmp,
463 buffer, strlen(buffer), osl_getThreadTextEncoding(),
464 OSTRING_TO_OUSTRING_CVTFLAGS);
465 if (ustrTmp != 0)
467 if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
468 result = osl_Process_E_None;
469 rtl_uString_release (ustrTmp);
473 return (result);
476 /******************************************************************************
478 * new functions to set/return the current process locale
480 *****************************************************************************/
482 struct ProcessLocale_Impl
484 pthread_mutex_t m_mutex;
485 rtl_Locale * m_pLocale;
488 static struct ProcessLocale_Impl g_process_locale =
490 PTHREAD_MUTEX_INITIALIZER,
494 extern "C" void _imp_getProcessLocale( rtl_Locale ** );
495 extern "C" int _imp_setProcessLocale( rtl_Locale * );
497 /**********************************************
498 osl_getProcessLocale().
499 *********************************************/
500 oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
502 oslProcessError result = osl_Process_E_Unknown;
503 OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
504 if (ppLocale)
506 pthread_mutex_lock(&(g_process_locale.m_mutex));
508 if (g_process_locale.m_pLocale == 0)
509 _imp_getProcessLocale (&(g_process_locale.m_pLocale));
510 *ppLocale = g_process_locale.m_pLocale;
511 result = osl_Process_E_None;
513 pthread_mutex_unlock (&(g_process_locale.m_mutex));
515 return (result);
518 /**********************************************
519 osl_setProcessLocale().
520 *********************************************/
521 oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
523 oslProcessError result = osl_Process_E_Unknown;
525 OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
527 pthread_mutex_lock(&(g_process_locale.m_mutex));
528 if (_imp_setProcessLocale (pLocale) == 0)
530 g_process_locale.m_pLocale = pLocale;
531 result = osl_Process_E_None;
533 pthread_mutex_unlock (&(g_process_locale.m_mutex));
535 return (result);
538 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */