Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "osl/process.h"
30 : :
31 : : #include <limits.h>
32 : : #include <pthread.h>
33 : : #include <stdlib.h>
34 : : #include <string.h>
35 : :
36 : : #include "osl/diagnose.h"
37 : : #include "osl/file.h"
38 : : #include "osl/module.h"
39 : : #include "osl/thread.h"
40 : : #include "rtl/ustring.hxx"
41 : : #include "rtl/strbuf.h"
42 : :
43 : : #include "file_path_helper.h"
44 : :
45 : : #include "uunxapi.h"
46 : :
47 : : #ifdef ANDROID
48 : : #include <osl/detail/android-bootstrap.h>
49 : : #endif
50 : :
51 : : /***************************************
52 : : osl_bootstrap_getExecutableFile_Impl().
53 : :
54 : : @internal
55 : : @see rtl_bootstrap
56 : : @see #i37371#
57 : :
58 : : **************************************/
59 : :
60 : : extern "C" oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
61 : : rtl_uString ** ppFileURL
62 : : ) SAL_THROW_EXTERN_C();
63 : :
64 : :
65 : : #if defined(MACOSX) || defined(IOS)
66 : : #include <mach-o/dyld.h>
67 : :
68 : : oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
69 : : rtl_uString ** ppFileURL
70 : : ) SAL_THROW_EXTERN_C()
71 : : {
72 : : oslProcessError result = osl_Process_E_NotFound;
73 : :
74 : : char buffer[PATH_MAX];
75 : : size_t buflen = sizeof(buffer);
76 : :
77 : : if (_NSGetExecutablePath (buffer, (uint32_t*)&buflen) == 0)
78 : : {
79 : : /* Determine absolute path. */
80 : : char abspath[PATH_MAX];
81 : : if (realpath (buffer, abspath) != 0)
82 : : {
83 : : /* Convert from utf8 to unicode. */
84 : : rtl_uString * pAbsPath = 0;
85 : : rtl_string2UString (
86 : : &(pAbsPath),
87 : : abspath, rtl_str_getLength (abspath),
88 : : RTL_TEXTENCODING_UTF8,
89 : : OSTRING_TO_OUSTRING_CVTFLAGS);
90 : :
91 : : if (pAbsPath)
92 : : {
93 : : /* Convert from path to url. */
94 : : if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
95 : : {
96 : : /* Success. */
97 : : result = osl_Process_E_None;
98 : : }
99 : : rtl_uString_release (pAbsPath);
100 : : }
101 : : }
102 : : }
103 : :
104 : : return (result);
105 : : }
106 : :
107 : : #else
108 : : #include <dlfcn.h>
109 : :
110 : 839 : oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
111 : : rtl_uString ** ppFileURL
112 : : ) SAL_THROW_EXTERN_C()
113 : : {
114 : 839 : oslProcessError result = osl_Process_E_NotFound;
115 : :
116 : : #ifdef ANDROID
117 : : /* On Android we in theory want the address of the "lo_main()"
118 : : * function, as that is what corresponds to "main()" in
119 : : * LibreOffice programs on normal desktop OSes.
120 : : *
121 : : * But that is true only for apps with a "native activity", using
122 : : * <sal/main.h> and the org.libreoffice.android.Bootstrap
123 : : * mechanism. For more normal (?) Android apps that just use
124 : : * LibreOffice libraries (components) where the main program is in
125 : : * Java, that just use LibreOffice libraries, there is no
126 : : * lo_main(). (Note that we don't know for sure yet how
127 : : * complicated it might be to write such Android apps...)
128 : : *
129 : : * Maybe best to just pick some function in liblo-bootstrap.so
130 : : * which also such Java apps *must* load as the very first
131 : : * LibreOffice native library. We store all LibreOffice native
132 : : * shared libraries an app uses in the same folder anyway, so it
133 : : * doesn't really matter.
134 : : */
135 : : void * addr = (void *) &lo_dlopen;
136 : : #else
137 : : /* Determine address of "main()" function. */
138 : 839 : void * addr = dlsym (RTLD_DEFAULT, "main");
139 : : #endif
140 [ - + ]: 839 : if (addr != 0)
141 : : {
142 : : /* Determine module URL. */
143 [ # # ]: 0 : if (osl_getModuleURLFromAddress (addr, ppFileURL))
144 : : {
145 : : /* Success. */
146 : 0 : result = osl_Process_E_None;
147 : : }
148 : : }
149 : :
150 : : /* Fallback to ordinary osl_getExecutableFile(). */
151 [ + - ]: 839 : if (result == osl_Process_E_NotFound)
152 : 839 : result = osl_getExecutableFile (ppFileURL);
153 : :
154 : 839 : return (result);
155 : : }
156 : :
157 : : #endif
158 : :
159 : : /***************************************
160 : : CommandArgs_Impl.
161 : : **************************************/
162 : : struct CommandArgs_Impl
163 : : {
164 : : pthread_mutex_t m_mutex;
165 : : sal_uInt32 m_nCount;
166 : : rtl_uString ** m_ppArgs;
167 : : };
168 : :
169 : : static struct CommandArgs_Impl g_command_args =
170 : : {
171 : : PTHREAD_MUTEX_INITIALIZER,
172 : : 0,
173 : : 0
174 : : };
175 : :
176 : : /***************************************
177 : : osl_getExecutableFile().
178 : : **************************************/
179 : 7720 : oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
180 : : {
181 : 7720 : oslProcessError result = osl_Process_E_NotFound;
182 : :
183 : 7720 : pthread_mutex_lock (&(g_command_args.m_mutex));
184 : : OSL_ASSERT(g_command_args.m_nCount > 0);
185 [ + - ]: 7720 : if (g_command_args.m_nCount > 0)
186 : : {
187 : : /* CommandArgs set. Obtain argv[0]. */
188 : 7720 : rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
189 : 7720 : result = osl_Process_E_None;
190 : : }
191 : 7720 : pthread_mutex_unlock (&(g_command_args.m_mutex));
192 : :
193 : 7720 : return (result);
194 : : }
195 : :
196 : : /***************************************
197 : : osl_getCommandArgCount().
198 : : **************************************/
199 : 2116 : sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
200 : : {
201 : 2116 : sal_uInt32 result = 0;
202 : :
203 : 2116 : pthread_mutex_lock (&(g_command_args.m_mutex));
204 : 2116 : if (g_command_args.m_nCount == 0) {
205 : : OSL_TRACE(
206 : : OSL_LOG_PREFIX
207 : : "osl_getCommandArgCount w/o prior call to osl_setCommandArgs");
208 : : }
209 [ + - ]: 2116 : if (g_command_args.m_nCount > 0)
210 : 2116 : result = g_command_args.m_nCount - 1;
211 : 2116 : pthread_mutex_unlock (&(g_command_args.m_mutex));
212 : :
213 : 2116 : return (result);
214 : : }
215 : :
216 : : /***************************************
217 : : osl_getCommandArg().
218 : : **************************************/
219 : 15382 : oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
220 : : {
221 : 15382 : oslProcessError result = osl_Process_E_NotFound;
222 : :
223 : 15382 : pthread_mutex_lock (&(g_command_args.m_mutex));
224 : : OSL_ASSERT(g_command_args.m_nCount > 0);
225 [ + - ]: 15382 : if (g_command_args.m_nCount > (nArg + 1))
226 : : {
227 : 15382 : rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
228 : 15382 : result = osl_Process_E_None;
229 : : }
230 : 15382 : pthread_mutex_unlock (&(g_command_args.m_mutex));
231 : :
232 : 15382 : return (result);
233 : : }
234 : :
235 : : /***************************************
236 : : osl_setCommandArgs().
237 : : **************************************/
238 : 2864 : void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
239 : : {
240 : : OSL_ASSERT(argc > 0);
241 : 2864 : pthread_mutex_lock (&(g_command_args.m_mutex));
242 : : OSL_ENSURE (g_command_args.m_nCount == 0, "osl_setCommandArgs(): CommandArgs already set.");
243 [ + - ]: 2864 : if (g_command_args.m_nCount == 0)
244 : : {
245 : 2864 : rtl_uString** ppArgs = (rtl_uString**)rtl_allocateZeroMemory (argc * sizeof(rtl_uString*));
246 [ + - ]: 2864 : if (ppArgs != 0)
247 : : {
248 : 2864 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
249 [ + + ]: 16178 : for (int i = 0; i < argc; i++)
250 : : {
251 : : rtl_string2UString (
252 : : &(ppArgs[i]),
253 : 26628 : argv[i], rtl_str_getLength (argv[i]), encoding,
254 : 13314 : OSTRING_TO_OUSTRING_CVTFLAGS);
255 : : }
256 [ + - ]: 2864 : if (ppArgs[0] != 0)
257 : : {
258 : : #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
259 : : /* see @ osl_getExecutableFile(). */
260 [ - + ]: 2864 : if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), sal_Unicode('/')) == -1)
261 : : {
262 [ # # ]: 0 : const rtl::OUString PATH (RTL_CONSTASCII_USTRINGPARAM("PATH"));
263 : :
264 : 0 : rtl_uString * pSearchPath = 0;
265 [ # # ]: 0 : osl_getEnvironment (PATH.pData, &pSearchPath);
266 [ # # ]: 0 : if (pSearchPath)
267 : : {
268 : 0 : rtl_uString * pSearchResult = 0;
269 [ # # ]: 0 : osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
270 [ # # ]: 0 : if (pSearchResult)
271 : : {
272 : 0 : rtl_uString_assign (&(ppArgs[0]), pSearchResult);
273 : 0 : rtl_uString_release (pSearchResult);
274 : : }
275 : 0 : rtl_uString_release (pSearchPath);
276 : 0 : }
277 : : }
278 : : #endif
279 : 2864 : rtl_uString * pArg0 = 0;
280 [ + - ][ + - ]: 2864 : if (realpath_u (ppArgs[0], &pArg0))
281 : : {
282 [ + - ]: 2864 : osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
283 : 2864 : rtl_uString_release (pArg0);
284 : : }
285 : : }
286 : 2864 : g_command_args.m_nCount = argc;
287 : 2864 : g_command_args.m_ppArgs = ppArgs;
288 : : }
289 : : }
290 : 2864 : pthread_mutex_unlock (&(g_command_args.m_mutex));
291 : 2864 : }
292 : :
293 : : /***************************************
294 : : osl_getEnvironment().
295 : : **************************************/
296 : 204016 : oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
297 : : {
298 : 204016 : oslProcessError result = osl_Process_E_NotFound;
299 [ + - ]: 204016 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
300 : 204016 : rtl_String* pstr_env_var = 0;
301 : :
302 : : OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
303 : : OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
304 : :
305 : : rtl_uString2String(
306 : : &pstr_env_var,
307 : 204016 : rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
308 : 408032 : OUSTRING_TO_OSTRING_CVTFLAGS);
309 [ + - ]: 204016 : if (pstr_env_var != 0)
310 : : {
311 : 204016 : const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
312 [ + + ]: 204016 : if (p_env_var != 0)
313 : : {
314 : : rtl_string2UString(
315 : : ppustrValue,
316 : 162 : p_env_var, strlen(p_env_var), encoding,
317 : 162 : OSTRING_TO_OUSTRING_CVTFLAGS);
318 : : OSL_ASSERT(*ppustrValue != NULL);
319 : :
320 : 162 : result = osl_Process_E_None;
321 : : }
322 : 204016 : rtl_string_release(pstr_env_var);
323 : : }
324 : :
325 : 204016 : return (result);
326 : : }
327 : :
328 : : /***************************************
329 : : osl_setEnvironment().
330 : : **************************************/
331 : 270 : oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
332 : : {
333 : 270 : oslProcessError result = osl_Process_E_Unknown;
334 [ + - ]: 270 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
335 : 270 : rtl_String* pstr_env_var = 0;
336 : 270 : rtl_String* pstr_val = 0;
337 : :
338 : : OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
339 : : OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
340 : :
341 : : rtl_uString2String(
342 : : &pstr_env_var,
343 : 270 : rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
344 : 540 : OUSTRING_TO_OSTRING_CVTFLAGS);
345 : :
346 : : rtl_uString2String(
347 : : &pstr_val,
348 : 270 : rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
349 : 540 : OUSTRING_TO_OSTRING_CVTFLAGS);
350 : :
351 [ + - ][ + - ]: 270 : if (pstr_env_var != 0 && pstr_val != 0)
352 : : {
353 : : #if defined (SOLARIS)
354 : : rtl_String * pBuffer = NULL;
355 : :
356 : : sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
357 : : rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
358 : : pstr_env_var );
359 : : rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
360 : : rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
361 : : rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
362 : :
363 : : rtl_string_acquire(pBuffer); // argument to putenv must leak on success
364 : :
365 : : if (putenv(rtl_string_getStr(pBuffer)) == 0)
366 : : result = osl_Process_E_None;
367 : : else
368 : : rtl_string_release(pBuffer);
369 : : #else
370 [ + - ]: 270 : if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
371 : 270 : result = osl_Process_E_None;
372 : : #endif
373 : : }
374 : :
375 [ + - ]: 270 : if (pstr_val)
376 : 270 : rtl_string_release(pstr_val);
377 : :
378 [ + - ]: 270 : if (pstr_env_var != 0)
379 : 270 : rtl_string_release(pstr_env_var);
380 : :
381 : 270 : return (result);
382 : : }
383 : :
384 : : /***************************************
385 : : osl_clearEnvironment().
386 : : **************************************/
387 : 0 : oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
388 : : {
389 : 0 : oslProcessError result = osl_Process_E_Unknown;
390 [ # # ]: 0 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
391 : 0 : rtl_String* pstr_env_var = 0;
392 : :
393 : : OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
394 : :
395 : : rtl_uString2String(
396 : : &pstr_env_var,
397 : 0 : rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
398 : 0 : OUSTRING_TO_OSTRING_CVTFLAGS);
399 : :
400 [ # # ]: 0 : if (pstr_env_var)
401 : : {
402 : : #if defined (SOLARIS)
403 : : rtl_String * pBuffer = NULL;
404 : :
405 : : sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
406 : : rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
407 : : rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
408 : :
409 : : rtl_string_acquire(pBuffer); // argument to putenv must leak on success
410 : :
411 : : if (putenv(rtl_string_getStr(pBuffer)) == 0)
412 : : result = osl_Process_E_None;
413 : : else
414 : : rtl_string_release(pBuffer);
415 : : #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
416 : : //MacOSX baseline is 10.4, which has an old-school void return
417 : : //for unsetenv.
418 : : //See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
419 : : unsetenv(rtl_string_getStr(pstr_env_var));
420 : : result = osl_Process_E_None;
421 : : #else
422 [ # # ]: 0 : if (unsetenv(rtl_string_getStr(pstr_env_var)) == 0)
423 : 0 : result = osl_Process_E_None;
424 : : #endif
425 : 0 : rtl_string_release(pstr_env_var);
426 : : }
427 : :
428 : 0 : return (result);
429 : : }
430 : :
431 : :
432 : : /***************************************
433 : : osl_getProcessWorkingDir().
434 : : **************************************/
435 : 69754 : oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
436 : : {
437 : 69754 : oslProcessError result = osl_Process_E_Unknown;
438 : : char buffer[PATH_MAX];
439 : :
440 : : OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
441 : :
442 [ + - ]: 69754 : if (getcwd (buffer, sizeof(buffer)) != 0)
443 : : {
444 : 69754 : rtl_uString* ustrTmp = 0;
445 : :
446 : : rtl_string2UString(
447 : : &ustrTmp,
448 [ + - ]: 69754 : buffer, strlen(buffer), osl_getThreadTextEncoding(),
449 : 69754 : OSTRING_TO_OUSTRING_CVTFLAGS);
450 [ + - ]: 69754 : if (ustrTmp != 0)
451 : : {
452 [ + - ][ + - ]: 69754 : if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
453 : 69754 : result = osl_Process_E_None;
454 : 69754 : rtl_uString_release (ustrTmp);
455 : : }
456 : : }
457 : :
458 : 69754 : return (result);
459 : : }
460 : :
461 : : /******************************************************************************
462 : : *
463 : : * new functions to set/return the current process locale
464 : : *
465 : : *****************************************************************************/
466 : :
467 : : struct ProcessLocale_Impl
468 : : {
469 : : pthread_mutex_t m_mutex;
470 : : rtl_Locale * m_pLocale;
471 : : };
472 : :
473 : : static struct ProcessLocale_Impl g_process_locale =
474 : : {
475 : : PTHREAD_MUTEX_INITIALIZER,
476 : : 0
477 : : };
478 : :
479 : : extern "C" void _imp_getProcessLocale( rtl_Locale ** );
480 : : extern "C" int _imp_setProcessLocale( rtl_Locale * );
481 : :
482 : : /**********************************************
483 : : osl_getProcessLocale().
484 : : *********************************************/
485 : 63337 : oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
486 : : {
487 : 63337 : oslProcessError result = osl_Process_E_Unknown;
488 : : OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
489 [ + - ]: 63337 : if (ppLocale)
490 : : {
491 : 63337 : pthread_mutex_lock(&(g_process_locale.m_mutex));
492 : :
493 [ + + ]: 63337 : if (g_process_locale.m_pLocale == 0)
494 : 2975 : _imp_getProcessLocale (&(g_process_locale.m_pLocale));
495 : 63337 : *ppLocale = g_process_locale.m_pLocale;
496 : 63337 : result = osl_Process_E_None;
497 : :
498 : 63337 : pthread_mutex_unlock (&(g_process_locale.m_mutex));
499 : : }
500 : 63337 : return (result);
501 : : }
502 : :
503 : : /**********************************************
504 : : osl_setProcessLocale().
505 : : *********************************************/
506 : 0 : oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
507 : : {
508 : 0 : oslProcessError result = osl_Process_E_Unknown;
509 : :
510 : : OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
511 : :
512 : 0 : pthread_mutex_lock(&(g_process_locale.m_mutex));
513 [ # # ]: 0 : if (_imp_setProcessLocale (pLocale) == 0)
514 : : {
515 : 0 : g_process_locale.m_pLocale = pLocale;
516 : 0 : result = osl_Process_E_None;
517 : : }
518 : 0 : pthread_mutex_unlock (&(g_process_locale.m_mutex));
519 : :
520 : 0 : return (result);
521 : : }
522 : :
523 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|