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 <config_features.h>
22 #include <osl/process.h>
29 #include <osl/diagnose.h>
30 #include <osl/file.hxx>
31 #include <osl/module.h>
32 #include <osl/thread.h>
33 #include <rtl/alloc.h>
34 #include <rtl/ustring.hxx>
35 #include <rtl/strbuf.h>
36 #include <sal/log.hxx>
38 #include "file_path_helper.hxx"
40 #include "uunxapi.hxx"
41 #include "nlsupport.hxx"
44 #include <osl/detail/android-bootstrap.h>
47 #if defined(MACOSX) || defined(IOS)
48 #include <mach-o/dyld.h>
52 oslProcessError
bootstrap_getExecutableFile(rtl_uString
** ppFileURL
)
54 oslProcessError result
= osl_Process_E_NotFound
;
56 char buffer
[PATH_MAX
];
57 uint32_t buflen
= sizeof(buffer
);
59 if (_NSGetExecutablePath (buffer
, &buflen
) == 0)
61 /* Determine absolute path. */
62 char abspath
[PATH_MAX
];
63 if (realpath (buffer
, abspath
) != nullptr)
65 /* Convert from utf8 to unicode. */
66 rtl_uString
* pAbsPath
= nullptr;
69 abspath
, rtl_str_getLength (abspath
),
70 RTL_TEXTENCODING_UTF8
,
71 OSTRING_TO_OUSTRING_CVTFLAGS
);
75 /* Convert from path to url. */
76 if (osl_getFileURLFromSystemPath (pAbsPath
, ppFileURL
) == osl_File_E_None
)
79 result
= osl_Process_E_None
;
81 rtl_uString_release (pAbsPath
);
96 oslProcessError
bootstrap_getExecutableFile(rtl_uString
** ppFileURL
)
98 oslProcessError result
= osl_Process_E_NotFound
;
101 // Just return some dummy file: URL for now to see what happens
102 OUString fileURL
= "vnd.sun.star.pathname:/instdir/program/soffice";
103 rtl_uString_acquire(fileURL
.pData
);
104 *ppFileURL
= fileURL
.pData
;
105 return osl_Process_E_None
;
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");
113 // The below code looking for "main" with dlsym() will typically
114 // fail, as there is little reason for "main" to be exported, in
115 // the dlsym() sense, from an executable. But Linux has
116 // /proc/self/exe, try using that.
118 int rc
= readlink("/proc/self/exe", buf
, sizeof(buf
));
119 if (rc
> 0 && rc
< PATH_MAX
)
122 OUString path
= OUString::fromUtf8(buf
);
124 if (osl::File::getFileURLFromSystemPath(path
, fileURL
) == osl::File::E_None
)
126 rtl_uString_acquire(fileURL
.pData
);
127 *ppFileURL
= fileURL
.pData
;
128 return osl_Process_E_None
;
132 /* Determine address of "main()" function. */
133 void * addr
= dlsym (RTLD_DEFAULT
, "main");
137 /* Determine module URL. */
138 if (osl_getModuleURLFromAddress (addr
, ppFileURL
))
141 result
= osl_Process_E_None
;
155 struct CommandArgs_Impl
157 pthread_mutex_t m_mutex
;
159 rtl_uString
** m_ppArgs
;
164 static struct CommandArgs_Impl g_command_args
=
166 PTHREAD_MUTEX_INITIALIZER
,
171 oslProcessError SAL_CALL
osl_getExecutableFile (rtl_uString
** ppustrFile
)
173 pthread_mutex_lock (&(g_command_args
.m_mutex
));
174 if (g_command_args
.m_nCount
== 0)
176 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
177 return bootstrap_getExecutableFile(ppustrFile
);
180 /* CommandArgs set. Obtain argv[0]. */
181 rtl_uString_assign (ppustrFile
, g_command_args
.m_ppArgs
[0]);
182 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
183 return osl_Process_E_None
;
186 sal_uInt32 SAL_CALL
osl_getCommandArgCount()
188 sal_uInt32 result
= 0;
190 pthread_mutex_lock (&(g_command_args
.m_mutex
));
192 g_command_args
.m_nCount
== 0, "sal.osl",
193 "osl_getCommandArgCount w/o prior call to osl_setCommandArgs");
194 if (g_command_args
.m_nCount
> 0)
195 result
= g_command_args
.m_nCount
- 1;
196 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
201 oslProcessError SAL_CALL
osl_getCommandArg (sal_uInt32 nArg
, rtl_uString
** strCommandArg
)
203 oslProcessError result
= osl_Process_E_NotFound
;
205 pthread_mutex_lock (&(g_command_args
.m_mutex
));
206 assert(g_command_args
.m_nCount
> 0);
207 if (g_command_args
.m_nCount
> (nArg
+ 1))
209 rtl_uString_assign (strCommandArg
, g_command_args
.m_ppArgs
[nArg
+ 1]);
210 result
= osl_Process_E_None
;
212 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
217 void SAL_CALL
osl_setCommandArgs (int argc
, char ** argv
)
220 pthread_mutex_lock (&(g_command_args
.m_mutex
));
221 SAL_WARN_IF(g_command_args
.m_nCount
!= 0, "sal.osl", "args already set");
222 if (g_command_args
.m_nCount
== 0)
224 rtl_uString
** ppArgs
= static_cast<rtl_uString
**>(rtl_allocateZeroMemory (argc
* sizeof(rtl_uString
*)));
225 if (ppArgs
!= nullptr)
227 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
228 for (int i
= 0; i
< argc
; i
++)
232 argv
[i
], rtl_str_getLength (argv
[i
]), encoding
,
233 OSTRING_TO_OUSTRING_CVTFLAGS
);
235 if (ppArgs
[0] != nullptr)
237 #if HAVE_FEATURE_MACOSX_SANDBOX
238 // If we are called with a relative path in argv[0] in a sandboxed process
239 // osl::realpath() fails. So just use bootstrap_getExecutableFile() instead.
240 // Somewhat silly to use argv[0] and tediously figure out the absolute path from it
242 bootstrap_getExecutableFile(&ppArgs
[0]);
243 OUString
pArg0(ppArgs
[0]);
244 osl_getFileURLFromSystemPath (pArg0
.pData
, &(ppArgs
[0]));
246 #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
247 /* see @ osl_getExecutableFile(). */
248 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs
[0]), '/') == -1)
250 const OUString
PATH ("PATH");
252 rtl_uString
* pSearchPath
= nullptr;
253 osl_getEnvironment (PATH
.pData
, &pSearchPath
);
256 rtl_uString
* pSearchResult
= nullptr;
257 osl_searchPath (ppArgs
[0], pSearchPath
, &pSearchResult
);
260 rtl_uString_assign (&(ppArgs
[0]), pSearchResult
);
261 rtl_uString_release (pSearchResult
);
263 rtl_uString_release (pSearchPath
);
268 if (osl::realpath (OUString::unacquired(&ppArgs
[0]), pArg0
))
270 osl_getFileURLFromSystemPath (pArg0
.pData
, &(ppArgs
[0]));
272 #endif // !HAVE_FEATURE_MACOSX_SANDBOX
274 g_command_args
.m_nCount
= argc
;
275 g_command_args
.m_ppArgs
= ppArgs
;
278 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
281 oslProcessError SAL_CALL
osl_getEnvironment(rtl_uString
* pustrEnvVar
, rtl_uString
** ppustrValue
)
283 oslProcessError result
= osl_Process_E_NotFound
;
284 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
285 rtl_String
* pstr_env_var
= nullptr;
287 OSL_PRECOND(pustrEnvVar
, "osl_getEnvironment(): Invalid parameter");
288 OSL_PRECOND(ppustrValue
, "osl_getEnvironment(): Invalid parameter");
292 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
293 OUSTRING_TO_OSTRING_CVTFLAGS
);
294 if (pstr_env_var
!= nullptr)
296 const char* p_env_var
= getenv (rtl_string_getStr (pstr_env_var
));
297 if (p_env_var
!= nullptr)
301 p_env_var
, strlen(p_env_var
), encoding
,
302 OSTRING_TO_OUSTRING_CVTFLAGS
);
303 OSL_ASSERT(*ppustrValue
!= nullptr);
305 result
= osl_Process_E_None
;
307 rtl_string_release(pstr_env_var
);
313 oslProcessError SAL_CALL
osl_setEnvironment(rtl_uString
* pustrEnvVar
, rtl_uString
* pustrValue
)
315 oslProcessError result
= osl_Process_E_Unknown
;
316 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
317 rtl_String
* pstr_env_var
= nullptr;
318 rtl_String
* pstr_val
= nullptr;
320 OSL_PRECOND(pustrEnvVar
, "osl_setEnvironment(): Invalid parameter");
321 OSL_PRECOND(pustrValue
, "osl_setEnvironment(): Invalid parameter");
325 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
326 OUSTRING_TO_OSTRING_CVTFLAGS
);
330 rtl_uString_getStr(pustrValue
), rtl_uString_getLength(pustrValue
), encoding
,
331 OUSTRING_TO_OSTRING_CVTFLAGS
);
333 if (pstr_env_var
!= nullptr && pstr_val
!= nullptr)
336 rtl_String
* pBuffer
= NULL
;
338 sal_Int32 nCapacity
= rtl_stringbuffer_newFromStringBuffer( &pBuffer
,
339 rtl_string_getLength(pstr_env_var
) + rtl_string_getLength(pstr_val
) + 1,
341 rtl_stringbuffer_insert( &pBuffer
, &nCapacity
, pBuffer
->length
, "=", 1);
342 rtl_stringbuffer_insert( &pBuffer
, &nCapacity
, pBuffer
->length
,
343 rtl_string_getStr(pstr_val
), rtl_string_getLength(pstr_val
) );
345 rtl_string_acquire(pBuffer
); // argument to putenv must leak on success
347 if (putenv(rtl_string_getStr(pBuffer
)) == 0)
348 result
= osl_Process_E_None
;
350 rtl_string_release(pBuffer
);
352 if (setenv(rtl_string_getStr(pstr_env_var
), rtl_string_getStr(pstr_val
), 1) == 0)
353 result
= osl_Process_E_None
;
358 rtl_string_release(pstr_val
);
360 if (pstr_env_var
!= nullptr)
361 rtl_string_release(pstr_env_var
);
366 oslProcessError SAL_CALL
osl_clearEnvironment(rtl_uString
* pustrEnvVar
)
368 oslProcessError result
= osl_Process_E_Unknown
;
369 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
370 rtl_String
* pstr_env_var
= nullptr;
372 OSL_PRECOND(pustrEnvVar
, "osl_setEnvironment(): Invalid parameter");
376 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
377 OUSTRING_TO_OSTRING_CVTFLAGS
);
382 rtl_String
* pBuffer
= NULL
;
384 sal_Int32 nCapacity
= rtl_stringbuffer_newFromStringBuffer( &pBuffer
,
385 rtl_string_getLength(pstr_env_var
) + 1, pstr_env_var
);
386 rtl_stringbuffer_insert( &pBuffer
, &nCapacity
, pBuffer
->length
, "=", 1);
388 rtl_string_acquire(pBuffer
); // argument to putenv must leak on success
390 if (putenv(rtl_string_getStr(pBuffer
)) == 0)
391 result
= osl_Process_E_None
;
393 rtl_string_release(pBuffer
);
394 #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
395 // MacOSX baseline is 10.4, which has an old-school void return
397 // See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
398 unsetenv(rtl_string_getStr(pstr_env_var
));
399 result
= osl_Process_E_None
;
401 if (unsetenv(rtl_string_getStr(pstr_env_var
)) == 0)
402 result
= osl_Process_E_None
;
404 rtl_string_release(pstr_env_var
);
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
)) != nullptr)
419 rtl_uString
* ustrTmp
= nullptr;
423 buffer
, strlen(buffer
), osl_getThreadTextEncoding(),
424 OSTRING_TO_OUSTRING_CVTFLAGS
);
425 if (ustrTmp
!= nullptr)
427 if (osl_getFileURLFromSystemPath (ustrTmp
, ppustrWorkingDir
) == osl_File_E_None
)
428 result
= osl_Process_E_None
;
429 rtl_uString_release (ustrTmp
);
438 struct ProcessLocale_Impl
440 pthread_mutex_t m_mutex
;
441 rtl_Locale
* m_pLocale
;
446 static struct ProcessLocale_Impl g_process_locale
=
448 PTHREAD_MUTEX_INITIALIZER
,
452 oslProcessError SAL_CALL
osl_getProcessLocale( rtl_Locale
** ppLocale
)
454 oslProcessError result
= osl_Process_E_Unknown
;
455 OSL_PRECOND(ppLocale
, "osl_getProcessLocale(): Invalid parameter.");
458 pthread_mutex_lock(&(g_process_locale
.m_mutex
));
460 if (g_process_locale
.m_pLocale
== nullptr)
461 imp_getProcessLocale (&(g_process_locale
.m_pLocale
));
462 *ppLocale
= g_process_locale
.m_pLocale
;
463 result
= osl_Process_E_None
;
465 pthread_mutex_unlock (&(g_process_locale
.m_mutex
));
470 oslProcessError SAL_CALL
osl_setProcessLocale( rtl_Locale
* pLocale
)
472 OSL_PRECOND(pLocale
, "osl_setProcessLocale(): Invalid parameter.");
474 pthread_mutex_lock(&(g_process_locale
.m_mutex
));
475 g_process_locale
.m_pLocale
= pLocale
;
476 pthread_mutex_unlock (&(g_process_locale
.m_mutex
));
478 return osl_Process_E_None
;
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */