bump product version to 4.1.6.2
[LibreOffice.git] / sal / osl / unx / process_impl.cxx
blobbd1e5a2f97263d628a0b6db7ff4d86384ecd3c89
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 "osl/process.h"
22 #include <limits.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "osl/diagnose.h"
28 #include "osl/file.h"
29 #include "osl/module.h"
30 #include "osl/thread.h"
31 #include "rtl/ustring.hxx"
32 #include "rtl/strbuf.h"
34 #include "file_path_helper.h"
36 #include "uunxapi.h"
38 #ifdef ANDROID
39 #include <osl/detail/android-bootstrap.h>
40 #endif
42 /***************************************
43 osl_bootstrap_getExecutableFile_Impl().
45 @internal
46 @see rtl_bootstrap
47 @see #i37371#
49 **************************************/
51 extern "C" oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
52 rtl_uString ** ppFileURL
53 ) SAL_THROW_EXTERN_C();
56 #if defined(MACOSX) || defined(IOS)
57 #include <mach-o/dyld.h>
59 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
60 rtl_uString ** ppFileURL
61 ) SAL_THROW_EXTERN_C()
63 oslProcessError result = osl_Process_E_NotFound;
65 char buffer[PATH_MAX];
66 size_t buflen = sizeof(buffer);
68 if (_NSGetExecutablePath (buffer, (uint32_t*)&buflen) == 0)
70 /* Determine absolute path. */
71 char abspath[PATH_MAX];
72 if (realpath (buffer, abspath) != 0)
74 /* Convert from utf8 to unicode. */
75 rtl_uString * pAbsPath = 0;
76 rtl_string2UString (
77 &(pAbsPath),
78 abspath, rtl_str_getLength (abspath),
79 RTL_TEXTENCODING_UTF8,
80 OSTRING_TO_OUSTRING_CVTFLAGS);
82 if (pAbsPath)
84 /* Convert from path to url. */
85 if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
87 /* Success. */
88 result = osl_Process_E_None;
90 rtl_uString_release (pAbsPath);
95 return (result);
98 #else
99 #include <dlfcn.h>
101 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
102 rtl_uString ** ppFileURL
103 ) SAL_THROW_EXTERN_C()
105 oslProcessError result = osl_Process_E_NotFound;
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 /* Determine address of "main()" function. */
113 void * addr = dlsym (RTLD_DEFAULT, "main");
114 #endif
115 if (addr != 0)
117 /* Determine module URL. */
118 if (osl_getModuleURLFromAddress (addr, ppFileURL))
120 /* Success. */
121 result = osl_Process_E_None;
125 /* Fallback to ordinary osl_getExecutableFile(). */
126 if (result == osl_Process_E_NotFound)
127 result = osl_getExecutableFile (ppFileURL);
129 return (result);
132 #endif
134 /***************************************
135 CommandArgs_Impl.
136 **************************************/
137 struct CommandArgs_Impl
139 pthread_mutex_t m_mutex;
140 sal_uInt32 m_nCount;
141 rtl_uString ** m_ppArgs;
144 static struct CommandArgs_Impl g_command_args =
146 PTHREAD_MUTEX_INITIALIZER,
151 /***************************************
152 osl_getExecutableFile().
153 **************************************/
154 oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
156 oslProcessError result = osl_Process_E_NotFound;
158 pthread_mutex_lock (&(g_command_args.m_mutex));
159 OSL_ASSERT(g_command_args.m_nCount > 0);
160 if (g_command_args.m_nCount > 0)
162 /* CommandArgs set. Obtain argv[0]. */
163 rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
164 result = osl_Process_E_None;
166 pthread_mutex_unlock (&(g_command_args.m_mutex));
168 return (result);
171 /***************************************
172 osl_getCommandArgCount().
173 **************************************/
174 sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
176 sal_uInt32 result = 0;
178 pthread_mutex_lock (&(g_command_args.m_mutex));
179 if (g_command_args.m_nCount == 0) {
180 OSL_TRACE(
181 OSL_LOG_PREFIX
182 "osl_getCommandArgCount w/o prior call to osl_setCommandArgs");
184 if (g_command_args.m_nCount > 0)
185 result = g_command_args.m_nCount - 1;
186 pthread_mutex_unlock (&(g_command_args.m_mutex));
188 return (result);
191 /***************************************
192 osl_getCommandArg().
193 **************************************/
194 oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
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 > (nArg + 1))
202 rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
203 result = osl_Process_E_None;
205 pthread_mutex_unlock (&(g_command_args.m_mutex));
207 return (result);
210 /***************************************
211 osl_setCommandArgs().
212 **************************************/
213 void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
215 OSL_ASSERT(argc > 0);
216 pthread_mutex_lock (&(g_command_args.m_mutex));
217 OSL_ENSURE (g_command_args.m_nCount == 0, "osl_setCommandArgs(): CommandArgs already set.");
218 if (g_command_args.m_nCount == 0)
220 rtl_uString** ppArgs = (rtl_uString**)rtl_allocateZeroMemory (argc * sizeof(rtl_uString*));
221 if (ppArgs != 0)
223 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
224 for (int i = 0; i < argc; i++)
226 rtl_string2UString (
227 &(ppArgs[i]),
228 argv[i], rtl_str_getLength (argv[i]), encoding,
229 OSTRING_TO_OUSTRING_CVTFLAGS);
231 if (ppArgs[0] != 0)
233 #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
234 /* see @ osl_getExecutableFile(). */
235 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), sal_Unicode('/')) == -1)
237 const rtl::OUString PATH ("PATH");
239 rtl_uString * pSearchPath = 0;
240 osl_getEnvironment (PATH.pData, &pSearchPath);
241 if (pSearchPath)
243 rtl_uString * pSearchResult = 0;
244 osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
245 if (pSearchResult)
247 rtl_uString_assign (&(ppArgs[0]), pSearchResult);
248 rtl_uString_release (pSearchResult);
250 rtl_uString_release (pSearchPath);
253 #endif
254 rtl_uString * pArg0 = 0;
255 if (realpath_u (ppArgs[0], &pArg0))
257 osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
258 rtl_uString_release (pArg0);
261 g_command_args.m_nCount = argc;
262 g_command_args.m_ppArgs = ppArgs;
265 pthread_mutex_unlock (&(g_command_args.m_mutex));
268 /***************************************
269 osl_getEnvironment().
270 **************************************/
271 oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
273 oslProcessError result = osl_Process_E_NotFound;
274 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
275 rtl_String* pstr_env_var = 0;
277 OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
278 OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
280 rtl_uString2String(
281 &pstr_env_var,
282 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
283 OUSTRING_TO_OSTRING_CVTFLAGS);
284 if (pstr_env_var != 0)
286 const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
287 if (p_env_var != 0)
289 rtl_string2UString(
290 ppustrValue,
291 p_env_var, strlen(p_env_var), encoding,
292 OSTRING_TO_OUSTRING_CVTFLAGS);
293 OSL_ASSERT(*ppustrValue != NULL);
295 result = osl_Process_E_None;
297 rtl_string_release(pstr_env_var);
300 return (result);
303 /***************************************
304 osl_setEnvironment().
305 **************************************/
306 oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
308 oslProcessError result = osl_Process_E_Unknown;
309 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
310 rtl_String* pstr_env_var = 0;
311 rtl_String* pstr_val = 0;
313 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
314 OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
316 rtl_uString2String(
317 &pstr_env_var,
318 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
319 OUSTRING_TO_OSTRING_CVTFLAGS);
321 rtl_uString2String(
322 &pstr_val,
323 rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
324 OUSTRING_TO_OSTRING_CVTFLAGS);
326 if (pstr_env_var != 0 && pstr_val != 0)
328 #if defined (SOLARIS)
329 rtl_String * pBuffer = NULL;
331 sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
332 rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
333 pstr_env_var );
334 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
335 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
336 rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
338 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
340 if (putenv(rtl_string_getStr(pBuffer)) == 0)
341 result = osl_Process_E_None;
342 else
343 rtl_string_release(pBuffer);
344 #else
345 if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
346 result = osl_Process_E_None;
347 #endif
350 if (pstr_val)
351 rtl_string_release(pstr_val);
353 if (pstr_env_var != 0)
354 rtl_string_release(pstr_env_var);
356 return (result);
359 /***************************************
360 osl_clearEnvironment().
361 **************************************/
362 oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
364 oslProcessError result = osl_Process_E_Unknown;
365 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
366 rtl_String* pstr_env_var = 0;
368 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
370 rtl_uString2String(
371 &pstr_env_var,
372 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
373 OUSTRING_TO_OSTRING_CVTFLAGS);
375 if (pstr_env_var)
377 #if defined (SOLARIS)
378 rtl_String * pBuffer = NULL;
380 sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
381 rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
382 rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
384 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
386 if (putenv(rtl_string_getStr(pBuffer)) == 0)
387 result = osl_Process_E_None;
388 else
389 rtl_string_release(pBuffer);
390 #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
391 //MacOSX baseline is 10.4, which has an old-school void return
392 //for unsetenv.
393 //See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
394 unsetenv(rtl_string_getStr(pstr_env_var));
395 result = osl_Process_E_None;
396 #else
397 if (unsetenv(rtl_string_getStr(pstr_env_var)) == 0)
398 result = osl_Process_E_None;
399 #endif
400 rtl_string_release(pstr_env_var);
403 return (result);
407 /***************************************
408 osl_getProcessWorkingDir().
409 **************************************/
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)) != 0)
419 rtl_uString* ustrTmp = 0;
421 rtl_string2UString(
422 &ustrTmp,
423 buffer, strlen(buffer), osl_getThreadTextEncoding(),
424 OSTRING_TO_OUSTRING_CVTFLAGS);
425 if (ustrTmp != 0)
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 /******************************************************************************
438 * new functions to set/return the current process locale
440 *****************************************************************************/
442 struct ProcessLocale_Impl
444 pthread_mutex_t m_mutex;
445 rtl_Locale * m_pLocale;
448 static struct ProcessLocale_Impl g_process_locale =
450 PTHREAD_MUTEX_INITIALIZER,
454 extern "C" void _imp_getProcessLocale( rtl_Locale ** );
455 extern "C" int _imp_setProcessLocale( rtl_Locale * );
457 /**********************************************
458 osl_getProcessLocale().
459 *********************************************/
460 oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
462 oslProcessError result = osl_Process_E_Unknown;
463 OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
464 if (ppLocale)
466 pthread_mutex_lock(&(g_process_locale.m_mutex));
468 if (g_process_locale.m_pLocale == 0)
469 _imp_getProcessLocale (&(g_process_locale.m_pLocale));
470 *ppLocale = g_process_locale.m_pLocale;
471 result = osl_Process_E_None;
473 pthread_mutex_unlock (&(g_process_locale.m_mutex));
475 return (result);
478 /**********************************************
479 osl_setProcessLocale().
480 *********************************************/
481 oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
483 oslProcessError result = osl_Process_E_Unknown;
485 OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
487 pthread_mutex_lock(&(g_process_locale.m_mutex));
488 if (_imp_setProcessLocale (pLocale) == 0)
490 g_process_locale.m_pLocale = pLocale;
491 result = osl_Process_E_None;
493 pthread_mutex_unlock (&(g_process_locale.m_mutex));
495 return (result);
498 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */