Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "osl/process.h"
21 :
22 : #include <limits.h>
23 : #include <pthread.h>
24 : #include <stdlib.h>
25 : #include <string.h>
26 :
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"
33 : #include "sal/log.hxx"
34 :
35 : #include "file_path_helper.hxx"
36 :
37 : #include "uunxapi.hxx"
38 : #include "nlsupport.hxx"
39 :
40 : #ifdef ANDROID
41 : #include <osl/detail/android-bootstrap.h>
42 : #endif
43 :
44 : #if defined(MACOSX) || defined(IOS)
45 : #include <mach-o/dyld.h>
46 :
47 : namespace {
48 :
49 : oslProcessError SAL_CALL bootstrap_getExecutableFile(rtl_uString ** ppFileURL)
50 : {
51 : oslProcessError result = osl_Process_E_NotFound;
52 :
53 : char buffer[PATH_MAX];
54 : uint32_t buflen = sizeof(buffer);
55 :
56 : if (_NSGetExecutablePath (buffer, &buflen) == 0)
57 : {
58 : /* Determine absolute path. */
59 : char abspath[PATH_MAX];
60 : if (realpath (buffer, abspath) != 0)
61 : {
62 : /* Convert from utf8 to unicode. */
63 : rtl_uString * pAbsPath = 0;
64 : rtl_string2UString (
65 : &(pAbsPath),
66 : abspath, rtl_str_getLength (abspath),
67 : RTL_TEXTENCODING_UTF8,
68 : OSTRING_TO_OUSTRING_CVTFLAGS);
69 :
70 : if (pAbsPath)
71 : {
72 : /* Convert from path to url. */
73 : if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
74 : {
75 : /* Success. */
76 : result = osl_Process_E_None;
77 : }
78 : rtl_uString_release (pAbsPath);
79 : }
80 : }
81 : }
82 :
83 : return result;
84 : }
85 :
86 : }
87 :
88 : #else
89 : #include <dlfcn.h>
90 :
91 : namespace {
92 :
93 10 : oslProcessError SAL_CALL bootstrap_getExecutableFile(rtl_uString ** ppFileURL)
94 : {
95 10 : oslProcessError result = osl_Process_E_NotFound;
96 :
97 : #ifdef ANDROID
98 : /* Now with just a single DSO, this one from lo-bootstrap.c is as good as
99 : * any */
100 : void * addr = dlsym (RTLD_DEFAULT, "JNI_OnLoad");
101 : #else
102 : /* Determine address of "main()" function. */
103 10 : void * addr = dlsym (RTLD_DEFAULT, "main");
104 : #endif
105 10 : if (addr != 0)
106 : {
107 : /* Determine module URL. */
108 10 : if (osl_getModuleURLFromAddress (addr, ppFileURL))
109 : {
110 : /* Success. */
111 10 : result = osl_Process_E_None;
112 : }
113 : }
114 :
115 10 : return result;
116 : }
117 :
118 : }
119 :
120 : #endif
121 :
122 : /***************************************
123 : CommandArgs_Impl.
124 : **************************************/
125 : struct CommandArgs_Impl
126 : {
127 : pthread_mutex_t m_mutex;
128 : sal_uInt32 m_nCount;
129 : rtl_uString ** m_ppArgs;
130 : };
131 :
132 : static struct CommandArgs_Impl g_command_args =
133 : {
134 : PTHREAD_MUTEX_INITIALIZER,
135 : 0,
136 : 0
137 : };
138 :
139 : /***************************************
140 : osl_getExecutableFile().
141 : **************************************/
142 1534 : oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
143 : {
144 1534 : pthread_mutex_lock (&(g_command_args.m_mutex));
145 1534 : if (g_command_args.m_nCount == 0)
146 : {
147 10 : pthread_mutex_unlock (&(g_command_args.m_mutex));
148 10 : return bootstrap_getExecutableFile(ppustrFile);
149 : }
150 :
151 : /* CommandArgs set. Obtain argv[0]. */
152 1524 : rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
153 1524 : pthread_mutex_unlock (&(g_command_args.m_mutex));
154 1524 : return osl_Process_E_None;
155 : }
156 :
157 : /***************************************
158 : osl_getCommandArgCount().
159 : **************************************/
160 1513 : sal_uInt32 SAL_CALL osl_getCommandArgCount()
161 : {
162 1513 : sal_uInt32 result = 0;
163 :
164 1513 : pthread_mutex_lock (&(g_command_args.m_mutex));
165 : SAL_INFO_IF(
166 : g_command_args.m_nCount == 0, "sal.osl",
167 : "osl_getCommandArgCount w/o prior call to osl_setCommandArgs");
168 1513 : if (g_command_args.m_nCount > 0)
169 1494 : result = g_command_args.m_nCount - 1;
170 1513 : pthread_mutex_unlock (&(g_command_args.m_mutex));
171 :
172 1513 : return result;
173 : }
174 :
175 : /***************************************
176 : osl_getCommandArg().
177 : **************************************/
178 11286 : oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
179 : {
180 11286 : oslProcessError result = osl_Process_E_NotFound;
181 :
182 11286 : pthread_mutex_lock (&(g_command_args.m_mutex));
183 : assert(g_command_args.m_nCount > 0);
184 11286 : if (g_command_args.m_nCount > (nArg + 1))
185 : {
186 11286 : rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
187 11286 : result = osl_Process_E_None;
188 : }
189 11286 : pthread_mutex_unlock (&(g_command_args.m_mutex));
190 :
191 11286 : return result;
192 : }
193 :
194 : /***************************************
195 : osl_setCommandArgs().
196 : **************************************/
197 1847 : void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
198 : {
199 : assert(argc > 0);
200 1847 : pthread_mutex_lock (&(g_command_args.m_mutex));
201 : SAL_WARN_IF(g_command_args.m_nCount != 0, "sal.osl", "args already set");
202 1847 : if (g_command_args.m_nCount == 0)
203 : {
204 1847 : rtl_uString** ppArgs = static_cast<rtl_uString**>(rtl_allocateZeroMemory (argc * sizeof(rtl_uString*)));
205 1847 : if (ppArgs != 0)
206 : {
207 1847 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
208 12476 : for (int i = 0; i < argc; i++)
209 : {
210 : rtl_string2UString (
211 : &(ppArgs[i]),
212 21258 : argv[i], rtl_str_getLength (argv[i]), encoding,
213 31887 : OSTRING_TO_OUSTRING_CVTFLAGS);
214 : }
215 1847 : if (ppArgs[0] != 0)
216 : {
217 : #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
218 : /* see @ osl_getExecutableFile(). */
219 1847 : if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), '/') == -1)
220 : {
221 0 : const rtl::OUString PATH ("PATH");
222 :
223 0 : rtl_uString * pSearchPath = 0;
224 0 : osl_getEnvironment (PATH.pData, &pSearchPath);
225 0 : if (pSearchPath)
226 : {
227 0 : rtl_uString * pSearchResult = 0;
228 0 : osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
229 0 : if (pSearchResult)
230 : {
231 0 : rtl_uString_assign (&(ppArgs[0]), pSearchResult);
232 0 : rtl_uString_release (pSearchResult);
233 : }
234 0 : rtl_uString_release (pSearchPath);
235 0 : }
236 : }
237 : #endif
238 1847 : rtl_uString * pArg0 = 0;
239 1847 : if (realpath_u (ppArgs[0], &pArg0))
240 : {
241 1847 : osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
242 1847 : rtl_uString_release (pArg0);
243 : }
244 : }
245 1847 : g_command_args.m_nCount = argc;
246 1847 : g_command_args.m_ppArgs = ppArgs;
247 : }
248 : }
249 1847 : pthread_mutex_unlock (&(g_command_args.m_mutex));
250 1847 : }
251 :
252 : /***************************************
253 : osl_getEnvironment().
254 : **************************************/
255 89236 : oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
256 : {
257 89236 : oslProcessError result = osl_Process_E_NotFound;
258 89236 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
259 89236 : rtl_String* pstr_env_var = 0;
260 :
261 : OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
262 : OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
263 :
264 : rtl_uString2String(
265 : &pstr_env_var,
266 89236 : rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
267 178472 : OUSTRING_TO_OSTRING_CVTFLAGS);
268 89236 : if (pstr_env_var != 0)
269 : {
270 89236 : const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
271 89236 : if (p_env_var != 0)
272 : {
273 : rtl_string2UString(
274 : ppustrValue,
275 206 : p_env_var, strlen(p_env_var), encoding,
276 412 : OSTRING_TO_OUSTRING_CVTFLAGS);
277 : OSL_ASSERT(*ppustrValue != NULL);
278 :
279 206 : result = osl_Process_E_None;
280 : }
281 89236 : rtl_string_release(pstr_env_var);
282 : }
283 :
284 89236 : return result;
285 : }
286 :
287 : /***************************************
288 : osl_setEnvironment().
289 : **************************************/
290 460 : oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
291 : {
292 460 : oslProcessError result = osl_Process_E_Unknown;
293 460 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
294 460 : rtl_String* pstr_env_var = 0;
295 460 : rtl_String* pstr_val = 0;
296 :
297 : OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
298 : OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
299 :
300 : rtl_uString2String(
301 : &pstr_env_var,
302 460 : rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
303 920 : OUSTRING_TO_OSTRING_CVTFLAGS);
304 :
305 : rtl_uString2String(
306 : &pstr_val,
307 460 : rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
308 920 : OUSTRING_TO_OSTRING_CVTFLAGS);
309 :
310 460 : if (pstr_env_var != 0 && pstr_val != 0)
311 : {
312 : #if defined (SOLARIS)
313 : rtl_String * pBuffer = NULL;
314 :
315 : sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
316 : rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
317 : pstr_env_var );
318 : rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
319 : rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
320 : rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
321 :
322 : rtl_string_acquire(pBuffer); // argument to putenv must leak on success
323 :
324 : if (putenv(rtl_string_getStr(pBuffer)) == 0)
325 : result = osl_Process_E_None;
326 : else
327 : rtl_string_release(pBuffer);
328 : #else
329 460 : if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
330 460 : result = osl_Process_E_None;
331 : #endif
332 : }
333 :
334 460 : if (pstr_val)
335 460 : rtl_string_release(pstr_val);
336 :
337 460 : if (pstr_env_var != 0)
338 460 : rtl_string_release(pstr_env_var);
339 :
340 460 : return result;
341 : }
342 :
343 : /***************************************
344 : osl_clearEnvironment().
345 : **************************************/
346 0 : oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
347 : {
348 0 : oslProcessError result = osl_Process_E_Unknown;
349 0 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
350 0 : rtl_String* pstr_env_var = 0;
351 :
352 : OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
353 :
354 : rtl_uString2String(
355 : &pstr_env_var,
356 0 : rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
357 0 : OUSTRING_TO_OSTRING_CVTFLAGS);
358 :
359 0 : if (pstr_env_var)
360 : {
361 : #if defined (SOLARIS)
362 : rtl_String * pBuffer = NULL;
363 :
364 : sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
365 : rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
366 : rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
367 :
368 : rtl_string_acquire(pBuffer); // argument to putenv must leak on success
369 :
370 : if (putenv(rtl_string_getStr(pBuffer)) == 0)
371 : result = osl_Process_E_None;
372 : else
373 : rtl_string_release(pBuffer);
374 : #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
375 : //MacOSX baseline is 10.4, which has an old-school void return
376 : //for unsetenv.
377 : //See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
378 : unsetenv(rtl_string_getStr(pstr_env_var));
379 : result = osl_Process_E_None;
380 : #else
381 0 : if (unsetenv(rtl_string_getStr(pstr_env_var)) == 0)
382 0 : result = osl_Process_E_None;
383 : #endif
384 0 : rtl_string_release(pstr_env_var);
385 : }
386 :
387 0 : return result;
388 : }
389 :
390 : /***************************************
391 : osl_getProcessWorkingDir().
392 : **************************************/
393 49429 : oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
394 : {
395 49429 : oslProcessError result = osl_Process_E_Unknown;
396 : char buffer[PATH_MAX];
397 :
398 : OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
399 :
400 49429 : if (getcwd (buffer, sizeof(buffer)) != 0)
401 : {
402 49429 : rtl_uString* ustrTmp = 0;
403 :
404 : rtl_string2UString(
405 : &ustrTmp,
406 98858 : buffer, strlen(buffer), osl_getThreadTextEncoding(),
407 98858 : OSTRING_TO_OUSTRING_CVTFLAGS);
408 49429 : if (ustrTmp != 0)
409 : {
410 49429 : if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
411 49429 : result = osl_Process_E_None;
412 49429 : rtl_uString_release (ustrTmp);
413 : }
414 : }
415 :
416 49429 : return result;
417 : }
418 :
419 : /******************************************************************************
420 : *
421 : * new functions to set/return the current process locale
422 : *
423 : *****************************************************************************/
424 :
425 : struct ProcessLocale_Impl
426 : {
427 : pthread_mutex_t m_mutex;
428 : rtl_Locale * m_pLocale;
429 : };
430 :
431 : static struct ProcessLocale_Impl g_process_locale =
432 : {
433 : PTHREAD_MUTEX_INITIALIZER,
434 : 0
435 : };
436 :
437 : /**********************************************
438 : osl_getProcessLocale().
439 : *********************************************/
440 2121 : oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
441 : {
442 2121 : oslProcessError result = osl_Process_E_Unknown;
443 : OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
444 2121 : if (ppLocale)
445 : {
446 2121 : pthread_mutex_lock(&(g_process_locale.m_mutex));
447 :
448 2121 : if (g_process_locale.m_pLocale == 0)
449 1909 : _imp_getProcessLocale (&(g_process_locale.m_pLocale));
450 2121 : *ppLocale = g_process_locale.m_pLocale;
451 2121 : result = osl_Process_E_None;
452 :
453 2121 : pthread_mutex_unlock (&(g_process_locale.m_mutex));
454 : }
455 2121 : return result;
456 : }
457 :
458 : /**********************************************
459 : osl_setProcessLocale().
460 : *********************************************/
461 0 : oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
462 : {
463 0 : oslProcessError result = osl_Process_E_Unknown;
464 :
465 : OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
466 :
467 0 : pthread_mutex_lock(&(g_process_locale.m_mutex));
468 0 : if (_imp_setProcessLocale (pLocale) == 0)
469 : {
470 0 : g_process_locale.m_pLocale = pLocale;
471 0 : result = osl_Process_E_None;
472 : }
473 0 : pthread_mutex_unlock (&(g_process_locale.m_mutex));
474 :
475 0 : return result;
476 : }
477 :
478 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|