xref: /trunk/main/sal/osl/unx/process_impl.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 #include "osl/process.h"
32 
33 #ifndef INCLUDED_LIMITS_H
34 #include <limits.h>
35 #define INCLUDED_LIMITS_H
36 #endif
37 
38 #ifndef INCLUDED_PTHREAD_H
39 #include <pthread.h>
40 #define INCLUDED_PTHREAD_H
41 #endif
42 
43 #ifndef INCLUDED_STDLIB_H
44 #include <stdlib.h>
45 #define INCLUDED_STDLIB_H
46 #endif
47 
48 #ifndef INCLUDED_STRING_H
49 #include <string.h>
50 #define INCLUDED_STRING_H
51 #endif
52 #include "osl/diagnose.h"
53 #include "osl/file.h"
54 #include "osl/module.h"
55 #include "osl/thread.h"
56 #include "rtl/ustring.hxx"
57 #include "rtl/strbuf.h"
58 
59 #ifndef _OSL_FILE_PATH_HELPER_H_
60 #include "file_path_helper.h"
61 #endif
62 
63 #ifndef _OSL_UUNXAPI_H_
64 #include "uunxapi.h"
65 #endif
66 
67 /***************************************
68   osl_bootstrap_getExecutableFile_Impl().
69 
70   @internal
71   @see rtl_bootstrap
72   @see #i37371#
73 
74  **************************************/
75 
76 extern "C" oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
77     rtl_uString ** ppFileURL
78 ) SAL_THROW_EXTERN_C();
79 
80 
81 #if defined(MACOSX)
82 #include <mach-o/dyld.h>
83 
84 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
85     rtl_uString ** ppFileURL
86 ) SAL_THROW_EXTERN_C()
87 {
88     oslProcessError result = osl_Process_E_NotFound;
89 
90     char   buffer[PATH_MAX];
91     size_t buflen = sizeof(buffer);
92 
93 #if __GNUC__ >= 4 && defined(MACOSX)
94     if (_NSGetExecutablePath (buffer, (uint32_t*)&buflen) == 0)
95 #else
96     if (_NSGetExecutablePath (buffer, &buflen) == 0)
97 #endif
98     {
99         /* Determine absolute path. */
100         char abspath[PATH_MAX];
101         if (realpath (buffer, abspath) != 0)
102         {
103             /* Convert from utf8 to unicode. */
104             rtl_uString * pAbsPath = 0;
105             rtl_string2UString (
106                 &(pAbsPath),
107                 abspath, rtl_str_getLength (abspath),
108                 RTL_TEXTENCODING_UTF8,
109                 OSTRING_TO_OUSTRING_CVTFLAGS);
110 
111             if (pAbsPath)
112             {
113                 /* Convert from path to url. */
114                 if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
115                 {
116                     /* Success. */
117                     result = osl_Process_E_None;
118                 }
119                 rtl_uString_release (pAbsPath);
120             }
121         }
122     }
123 
124     return (result);
125 }
126 
127 #elif !defined(NO_DL_FUNCTIONS)
128 #include <dlfcn.h>
129 
130 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
131     rtl_uString ** ppFileURL
132 ) SAL_THROW_EXTERN_C()
133 {
134     oslProcessError result = osl_Process_E_NotFound;
135 
136     /* Determine address of "main()" function. */
137     void * addr = dlsym (RTLD_DEFAULT, "main");
138     if (addr != 0)
139     {
140         /* Determine module URL. */
141         if (osl_getModuleURLFromAddress (addr, ppFileURL))
142         {
143             /* Success. */
144             result = osl_Process_E_None;
145         }
146     }
147 
148     return (result);
149 }
150 
151 #else  /* NO_DL_FUNCTIONS */
152 
153 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
154     rtl_uString ** ppFileURL
155 ) SAL_THROW_EXTERN_C()
156 {
157     /* Fallback to ordinary osl_getExecutableFile(). */
158     return osl_getExecutableFile (ppFileURL);
159 }
160 
161 #endif /* NO_DL_FUNCTIONS */
162 
163 /***************************************
164  CommandArgs_Impl.
165  **************************************/
166 struct CommandArgs_Impl
167 {
168     pthread_mutex_t m_mutex;
169     sal_uInt32      m_nCount;
170     rtl_uString **  m_ppArgs;
171 };
172 
173 static struct CommandArgs_Impl g_command_args =
174 {
175     PTHREAD_MUTEX_INITIALIZER,
176     0,
177     0
178 };
179 
180 /***************************************
181   osl_getExecutableFile().
182  **************************************/
183 oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
184 {
185     oslProcessError result = osl_Process_E_NotFound;
186 
187     pthread_mutex_lock (&(g_command_args.m_mutex));
188     OSL_ASSERT(g_command_args.m_nCount > 0);
189     if (g_command_args.m_nCount > 0)
190     {
191         /* CommandArgs set. Obtain argv[0]. */
192         rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
193         result = osl_Process_E_None;
194     }
195     pthread_mutex_unlock (&(g_command_args.m_mutex));
196 
197     return (result);
198 }
199 
200 /***************************************
201  osl_getCommandArgCount().
202  **************************************/
203 sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
204 {
205     sal_uInt32 result = 0;
206 
207     pthread_mutex_lock (&(g_command_args.m_mutex));
208     OSL_ASSERT(g_command_args.m_nCount > 0);
209     if (g_command_args.m_nCount > 0)
210         result = g_command_args.m_nCount - 1;
211     pthread_mutex_unlock (&(g_command_args.m_mutex));
212 
213     return (result);
214 }
215 
216 /***************************************
217  osl_getCommandArg().
218  **************************************/
219 oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
220 {
221     oslProcessError result = osl_Process_E_NotFound;
222 
223     pthread_mutex_lock (&(g_command_args.m_mutex));
224     OSL_ASSERT(g_command_args.m_nCount > 0);
225     if (g_command_args.m_nCount > (nArg + 1))
226     {
227         rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
228         result = osl_Process_E_None;
229     }
230     pthread_mutex_unlock (&(g_command_args.m_mutex));
231 
232     return (result);
233 }
234 
235 /***************************************
236  osl_setCommandArgs().
237  **************************************/
238 void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
239 {
240     OSL_ASSERT(argc > 0);
241     pthread_mutex_lock (&(g_command_args.m_mutex));
242     OSL_ENSURE (g_command_args.m_nCount == 0, "osl_setCommandArgs(): CommandArgs already set.");
243     if (g_command_args.m_nCount == 0)
244     {
245         rtl_uString** ppArgs = (rtl_uString**)rtl_allocateZeroMemory (argc * sizeof(rtl_uString*));
246         if (ppArgs != 0)
247         {
248             rtl_TextEncoding encoding = osl_getThreadTextEncoding();
249             for (int i = 0; i < argc; i++)
250             {
251                 rtl_string2UString (
252                     &(ppArgs[i]),
253                     argv[i], rtl_str_getLength (argv[i]), encoding,
254                     OSTRING_TO_OUSTRING_CVTFLAGS);
255             }
256             if (ppArgs[0] != 0)
257             {
258                 /* see @ osl_getExecutableFile(). */
259                 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), sal_Unicode('/')) == -1)
260                 {
261                     const rtl::OUString PATH (RTL_CONSTASCII_USTRINGPARAM("PATH"));
262 
263                     rtl_uString * pSearchPath = 0;
264                     osl_getEnvironment (PATH.pData, &pSearchPath);
265                     if (pSearchPath)
266                     {
267                         rtl_uString * pSearchResult = 0;
268                         osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
269                         if (pSearchResult)
270                         {
271                             rtl_uString_assign (&(ppArgs[0]), pSearchResult);
272                             rtl_uString_release (pSearchResult);
273                         }
274                         rtl_uString_release (pSearchPath);
275                     }
276                 }
277 
278                 rtl_uString * pArg0 = 0;
279                 if (realpath_u (ppArgs[0], &pArg0))
280                 {
281                     osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
282                     rtl_uString_release (pArg0);
283                 }
284             }
285             g_command_args.m_nCount = argc;
286             g_command_args.m_ppArgs = ppArgs;
287         }
288     }
289     pthread_mutex_unlock (&(g_command_args.m_mutex));
290 }
291 
292 /***************************************
293  osl_getEnvironment().
294  **************************************/
295 oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
296 {
297     oslProcessError  result   = osl_Process_E_NotFound;
298     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
299     rtl_String* pstr_env_var  = 0;
300 
301     OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
302     OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
303 
304     rtl_uString2String(
305         &pstr_env_var,
306         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
307         OUSTRING_TO_OSTRING_CVTFLAGS);
308     if (pstr_env_var != 0)
309     {
310         const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
311         if (p_env_var != 0)
312         {
313             rtl_string2UString(
314                 ppustrValue,
315                 p_env_var, strlen(p_env_var), encoding,
316                 OSTRING_TO_OUSTRING_CVTFLAGS);
317             OSL_ASSERT(*ppustrValue != NULL);
318 
319             result = osl_Process_E_None;
320         }
321         rtl_string_release(pstr_env_var);
322     }
323 
324     return (result);
325 }
326 
327 /***************************************
328  osl_setEnvironment().
329  **************************************/
330 oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
331 {
332     oslProcessError  result   = osl_Process_E_Unknown;
333     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
334     rtl_String* pstr_env_var  = 0;
335     rtl_String* pstr_val  = 0;
336 
337     OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
338     OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
339 
340     rtl_uString2String(
341         &pstr_env_var,
342         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
343         OUSTRING_TO_OSTRING_CVTFLAGS);
344 
345     rtl_uString2String(
346         &pstr_val,
347         rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
348         OUSTRING_TO_OSTRING_CVTFLAGS);
349 
350     if (pstr_env_var != 0 && pstr_val != 0)
351     {
352 #if defined (SOLARIS)
353         rtl_String * pBuffer = NULL;
354 
355         sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
356             rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
357             pstr_env_var );
358         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
359         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
360             rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
361 
362         rtl_string_acquire(pBuffer); // argument to putenv must leak on success
363 
364         if (putenv(rtl_string_getStr(pBuffer)) == 0)
365             result = osl_Process_E_None;
366         else
367             rtl_string_release(pBuffer);
368 #else
369         if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
370             result = osl_Process_E_None;
371 #endif
372     }
373 
374     if (pstr_val)
375         rtl_string_release(pstr_val);
376 
377     if (pstr_env_var != 0)
378         rtl_string_release(pstr_env_var);
379 
380     return (result);
381 }
382 
383 /***************************************
384  osl_clearEnvironment().
385  **************************************/
386 oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
387 {
388     oslProcessError  result   = osl_Process_E_Unknown;
389     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
390     rtl_String* pstr_env_var  = 0;
391 
392     OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
393 
394     rtl_uString2String(
395         &pstr_env_var,
396         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
397         OUSTRING_TO_OSTRING_CVTFLAGS);
398 
399     if (pstr_env_var)
400     {
401 #if defined (SOLARIS)
402         rtl_String * pBuffer = NULL;
403 
404         sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
405             rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
406         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
407 
408         rtl_string_acquire(pBuffer); // argument to putenv must leak on success
409 
410         if (putenv(rtl_string_getStr(pBuffer)) == 0)
411             result = osl_Process_E_None;
412         else
413             rtl_string_release(pBuffer);
414 #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
415         //MacOSX baseline is 10.4, which has an old-school void return
416         //for unsetenv.
417                 //See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
418         unsetenv(rtl_string_getStr(pstr_env_var));
419         result = osl_Process_E_None;
420 #else
421         if (unsetenv(rtl_string_getStr(pstr_env_var)) == 0)
422             result = osl_Process_E_None;
423 #endif
424         rtl_string_release(pstr_env_var);
425     }
426 
427     return (result);
428 }
429 
430 
431 /***************************************
432  osl_getProcessWorkingDir().
433  **************************************/
434 oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
435 {
436     oslProcessError result = osl_Process_E_Unknown;
437     char buffer[PATH_MAX];
438 
439     OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
440 
441     if (getcwd (buffer, sizeof(buffer)) != 0)
442     {
443         rtl_uString* ustrTmp = 0;
444 
445         rtl_string2UString(
446             &ustrTmp,
447             buffer, strlen(buffer), osl_getThreadTextEncoding(),
448             OSTRING_TO_OUSTRING_CVTFLAGS);
449         if (ustrTmp != 0)
450         {
451             if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
452                 result = osl_Process_E_None;
453             rtl_uString_release (ustrTmp);
454         }
455     }
456 
457     return (result);
458 }
459 
460 /******************************************************************************
461  *
462  *              new functions to set/return the current process locale
463  *
464  *****************************************************************************/
465 
466 struct ProcessLocale_Impl
467 {
468     pthread_mutex_t m_mutex;
469     rtl_Locale *    m_pLocale;
470 };
471 
472 static struct ProcessLocale_Impl g_process_locale =
473 {
474     PTHREAD_MUTEX_INITIALIZER,
475     0
476 };
477 
478 extern "C" void _imp_getProcessLocale( rtl_Locale ** );
479 extern "C" int  _imp_setProcessLocale( rtl_Locale * );
480 
481 /**********************************************
482  osl_getProcessLocale().
483  *********************************************/
484 oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
485 {
486     oslProcessError result = osl_Process_E_Unknown;
487     OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
488     if (ppLocale)
489     {
490         pthread_mutex_lock(&(g_process_locale.m_mutex));
491 
492         if (g_process_locale.m_pLocale == 0)
493             _imp_getProcessLocale (&(g_process_locale.m_pLocale));
494         *ppLocale = g_process_locale.m_pLocale;
495         result = osl_Process_E_None;
496 
497         pthread_mutex_unlock (&(g_process_locale.m_mutex));
498     }
499     return (result);
500 }
501 
502 /**********************************************
503  osl_setProcessLocale().
504  *********************************************/
505 oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
506 {
507     oslProcessError result = osl_Process_E_Unknown;
508 
509     OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
510 
511     pthread_mutex_lock(&(g_process_locale.m_mutex));
512     if (_imp_setProcessLocale (pLocale) == 0)
513     {
514         g_process_locale.m_pLocale = pLocale;
515         result = osl_Process_E_None;
516     }
517     pthread_mutex_unlock (&(g_process_locale.m_mutex));
518 
519     return (result);
520 }
521