1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
27 #include "osl/diagnose.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"
39 #include <osl/detail/android-bootstrap.h>
42 /***************************************
43 osl_bootstrap_getExecutableFile_Impl().
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;
78 abspath
, rtl_str_getLength (abspath
),
79 RTL_TEXTENCODING_UTF8
,
80 OSTRING_TO_OUSTRING_CVTFLAGS
);
84 /* Convert from path to url. */
85 if (osl_getFileURLFromSystemPath (pAbsPath
, ppFileURL
) == osl_File_E_None
)
88 result
= osl_Process_E_None
;
90 rtl_uString_release (pAbsPath
);
101 oslProcessError SAL_CALL
osl_bootstrap_getExecutableFile_Impl (
102 rtl_uString
** ppFileURL
103 ) SAL_THROW_EXTERN_C()
105 oslProcessError result
= osl_Process_E_NotFound
;
108 /* Now with just a single DSO, this one from lo-bootstrap.c is as good as
110 void * addr
= dlsym (RTLD_DEFAULT
, "JNI_OnLoad");
112 /* Determine address of "main()" function. */
113 void * addr
= dlsym (RTLD_DEFAULT
, "main");
117 /* Determine module URL. */
118 if (osl_getModuleURLFromAddress (addr
, ppFileURL
))
121 result
= osl_Process_E_None
;
125 /* Fallback to ordinary osl_getExecutableFile(). */
126 if (result
== osl_Process_E_NotFound
)
127 result
= osl_getExecutableFile (ppFileURL
);
134 /***************************************
136 **************************************/
137 struct CommandArgs_Impl
139 pthread_mutex_t m_mutex
;
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
));
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) {
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
));
191 /***************************************
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
));
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
*));
223 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
224 for (int i
= 0; i
< argc
; i
++)
228 argv
[i
], rtl_str_getLength (argv
[i
]), encoding
,
229 OSTRING_TO_OUSTRING_CVTFLAGS
);
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
);
243 rtl_uString
* pSearchResult
= 0;
244 osl_searchPath (ppArgs
[0], pSearchPath
, &pSearchResult
);
247 rtl_uString_assign (&(ppArgs
[0]), pSearchResult
);
248 rtl_uString_release (pSearchResult
);
250 rtl_uString_release (pSearchPath
);
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");
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
));
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
);
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");
318 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
319 OUSTRING_TO_OSTRING_CVTFLAGS
);
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,
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
;
343 rtl_string_release(pBuffer
);
345 if (setenv(rtl_string_getStr(pstr_env_var
), rtl_string_getStr(pstr_val
), 1) == 0)
346 result
= osl_Process_E_None
;
351 rtl_string_release(pstr_val
);
353 if (pstr_env_var
!= 0)
354 rtl_string_release(pstr_env_var
);
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");
372 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
373 OUSTRING_TO_OSTRING_CVTFLAGS
);
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
;
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
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
;
397 if (unsetenv(rtl_string_getStr(pstr_env_var
)) == 0)
398 result
= osl_Process_E_None
;
400 rtl_string_release(pstr_env_var
);
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;
423 buffer
, strlen(buffer
), osl_getThreadTextEncoding(),
424 OSTRING_TO_OUSTRING_CVTFLAGS
);
427 if (osl_getFileURLFromSystemPath (ustrTmp
, ppustrWorkingDir
) == osl_File_E_None
)
428 result
= osl_Process_E_None
;
429 rtl_uString_release (ustrTmp
);
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.");
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
));
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
));
498 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */