LCOV - code coverage report
Current view: top level - sal/osl/unx - process_impl.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 120 144 83.3 %
Date: 2014-11-03 Functions: 10 12 83.3 %
Legend: Lines: hit not hit

          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             : 
      34             : #include "file_path_helper.h"
      35             : 
      36             : #include "uunxapi.h"
      37             : #include "getexecutablefile.hxx"
      38             : #include "nlsupport.h"
      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             : oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
      48             :     rtl_uString ** ppFileURL
      49             : ) SAL_THROW_EXTERN_C()
      50             : {
      51             :     oslProcessError result = osl_Process_E_NotFound;
      52             : 
      53             :     char   buffer[PATH_MAX];
      54             :     size_t buflen = sizeof(buffer);
      55             : 
      56             :     if (_NSGetExecutablePath (buffer, (uint32_t*)&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             : #else
      87             : #include <dlfcn.h>
      88             : 
      89         742 : oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
      90             :     rtl_uString ** ppFileURL
      91             : ) SAL_THROW_EXTERN_C()
      92             : {
      93         742 :     oslProcessError result = osl_Process_E_NotFound;
      94             : 
      95             : #ifdef ANDROID
      96             :     /* Now with just a single DSO, this one from lo-bootstrap.c is as good as
      97             :      * any */
      98             :     void * addr = dlsym (RTLD_DEFAULT, "JNI_OnLoad");
      99             : #else
     100             :     /* Determine address of "main()" function. */
     101         742 :     void * addr = dlsym (RTLD_DEFAULT, "main");
     102             : #endif
     103         742 :     if (addr != 0)
     104             :     {
     105             :         /* Determine module URL. */
     106           4 :         if (osl_getModuleURLFromAddress (addr, ppFileURL))
     107             :         {
     108             :             /* Success. */
     109           4 :             result = osl_Process_E_None;
     110             :         }
     111             :     }
     112             : 
     113             :     /* Fallback to ordinary osl_getExecutableFile(). */
     114         742 :     if (result == osl_Process_E_NotFound)
     115         738 :         result = osl_getExecutableFile (ppFileURL);
     116             : 
     117         742 :     return (result);
     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        2324 : oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
     143             : {
     144        2324 :     oslProcessError result = osl_Process_E_NotFound;
     145             : 
     146        2324 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     147             :     OSL_ASSERT(g_command_args.m_nCount > 0);
     148        2324 :     if (g_command_args.m_nCount > 0)
     149             :     {
     150             :         /* CommandArgs set. Obtain argv[0]. */
     151        2324 :         rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
     152        2324 :         result = osl_Process_E_None;
     153             :     }
     154        2324 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     155             : 
     156        2324 :     return (result);
     157             : }
     158             : 
     159             : /***************************************
     160             :  osl_getCommandArgCount().
     161             :  **************************************/
     162        2299 : sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
     163             : {
     164        2299 :     sal_uInt32 result = 0;
     165             : 
     166        2299 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     167             :     assert (g_command_args.m_nCount != 0);
     168        2299 :     if (g_command_args.m_nCount > 0)
     169        2299 :         result = g_command_args.m_nCount - 1;
     170        2299 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     171             : 
     172        2299 :     return (result);
     173             : }
     174             : 
     175             : /***************************************
     176             :  osl_getCommandArg().
     177             :  **************************************/
     178       18348 : oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
     179             : {
     180       18348 :     oslProcessError result = osl_Process_E_NotFound;
     181             : 
     182       18348 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     183             :     OSL_ASSERT(g_command_args.m_nCount > 0);
     184       18348 :     if (g_command_args.m_nCount > (nArg + 1))
     185             :     {
     186       18348 :         rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
     187       18348 :         result = osl_Process_E_None;
     188             :     }
     189       18348 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     190             : 
     191       18348 :     return (result);
     192             : }
     193             : 
     194           0 : int SAL_CALL osl_areCommandArgsSet (void)
     195             : {
     196           0 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     197           0 :     int nRet = (int) (g_command_args.m_nCount > 0);
     198           0 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     199           0 :     return nRet;
     200             : }
     201             : 
     202             : /***************************************
     203             :  osl_setCommandArgs().
     204             :  **************************************/
     205        2767 : void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
     206             : {
     207             :     // special case for argc == 0: set up fake command line
     208        2767 :     int nArgs(argc ? argc : 1);
     209        2767 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     210             :     assert (g_command_args.m_nCount == 0);
     211        2767 :     if (g_command_args.m_nCount == 0)
     212             :     {
     213             :         rtl_uString** ppArgs =
     214        2767 :             (rtl_uString**)rtl_allocateZeroMemory(nArgs * sizeof(rtl_uString*));
     215        2767 :         if (ppArgs != 0 && argc == 0)
     216             :         {
     217             :             // special case: set up fake command line
     218             :             char const*const arg =
     219           4 :                 "this is just a fake and cheap imitation of a command line";
     220             :             rtl_string2UString(&ppArgs[0],
     221             :                 arg, rtl_str_getLength(arg), RTL_TEXTENCODING_ASCII_US,
     222           4 :                 OSTRING_TO_OUSTRING_CVTFLAGS);
     223           4 :             g_command_args.m_nCount = nArgs;
     224           4 :             g_command_args.m_ppArgs = ppArgs;
     225             :         }
     226        2763 :         else if (ppArgs != 0)
     227             :         {
     228        2763 :             rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     229       19278 :             for (int i = 0; i < argc; i++)
     230             :             {
     231             :                 rtl_string2UString (
     232             :                     &(ppArgs[i]),
     233       33030 :                     argv[i], rtl_str_getLength (argv[i]), encoding,
     234       49545 :                     OSTRING_TO_OUSTRING_CVTFLAGS);
     235             :             }
     236        2763 :             if (ppArgs[0] != 0)
     237             :             {
     238             : #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
     239             :                 /* see @ osl_getExecutableFile(). */
     240        2763 :                 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), '/') == -1)
     241             :                 {
     242           0 :                     const rtl::OUString PATH ("PATH");
     243             : 
     244           0 :                     rtl_uString * pSearchPath = 0;
     245           0 :                     osl_getEnvironment (PATH.pData, &pSearchPath);
     246           0 :                     if (pSearchPath)
     247             :                     {
     248           0 :                         rtl_uString * pSearchResult = 0;
     249           0 :                         osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
     250           0 :                         if (pSearchResult)
     251             :                         {
     252           0 :                             rtl_uString_assign (&(ppArgs[0]), pSearchResult);
     253           0 :                             rtl_uString_release (pSearchResult);
     254             :                         }
     255           0 :                         rtl_uString_release (pSearchPath);
     256           0 :                     }
     257             :                 }
     258             : #endif
     259        2763 :                 rtl_uString * pArg0 = 0;
     260        2763 :                 if (realpath_u (ppArgs[0], &pArg0))
     261             :                 {
     262        2763 :                     osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
     263        2763 :                     rtl_uString_release (pArg0);
     264             :                 }
     265             :             }
     266        2763 :             g_command_args.m_nCount = argc;
     267        2763 :             g_command_args.m_ppArgs = ppArgs;
     268             :         }
     269             :     }
     270        2767 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     271        2767 : }
     272             : 
     273             : /***************************************
     274             :  osl_getEnvironment().
     275             :  **************************************/
     276      132560 : oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
     277             : {
     278      132560 :     oslProcessError  result   = osl_Process_E_NotFound;
     279      132560 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     280      132560 :     rtl_String* pstr_env_var  = 0;
     281             : 
     282             :     OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
     283             :     OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
     284             : 
     285             :     rtl_uString2String(
     286             :         &pstr_env_var,
     287      132560 :         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
     288      265120 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     289      132560 :     if (pstr_env_var != 0)
     290             :     {
     291      132560 :         const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
     292      132560 :         if (p_env_var != 0)
     293             :         {
     294             :             rtl_string2UString(
     295             :                 ppustrValue,
     296         322 :                 p_env_var, strlen(p_env_var), encoding,
     297         644 :                 OSTRING_TO_OUSTRING_CVTFLAGS);
     298             :             OSL_ASSERT(*ppustrValue != NULL);
     299             : 
     300         322 :             result = osl_Process_E_None;
     301             :         }
     302      132560 :         rtl_string_release(pstr_env_var);
     303             :     }
     304             : 
     305      132560 :     return (result);
     306             : }
     307             : 
     308             : /***************************************
     309             :  osl_setEnvironment().
     310             :  **************************************/
     311         677 : oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
     312             : {
     313         677 :     oslProcessError  result   = osl_Process_E_Unknown;
     314         677 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     315         677 :     rtl_String* pstr_env_var  = 0;
     316         677 :     rtl_String* pstr_val  = 0;
     317             : 
     318             :     OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
     319             :     OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
     320             : 
     321             :     rtl_uString2String(
     322             :         &pstr_env_var,
     323         677 :         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
     324        1354 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     325             : 
     326             :     rtl_uString2String(
     327             :         &pstr_val,
     328         677 :         rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
     329        1354 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     330             : 
     331         677 :     if (pstr_env_var != 0 && pstr_val != 0)
     332             :     {
     333             : #if defined (SOLARIS)
     334             :         rtl_String * pBuffer = NULL;
     335             : 
     336             :         sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
     337             :             rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
     338             :             pstr_env_var );
     339             :         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
     340             :         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
     341             :             rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
     342             : 
     343             :         rtl_string_acquire(pBuffer); // argument to putenv must leak on success
     344             : 
     345             :         if (putenv(rtl_string_getStr(pBuffer)) == 0)
     346             :             result = osl_Process_E_None;
     347             :         else
     348             :             rtl_string_release(pBuffer);
     349             : #else
     350         677 :         if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
     351         677 :             result = osl_Process_E_None;
     352             : #endif
     353             :     }
     354             : 
     355         677 :     if (pstr_val)
     356         677 :         rtl_string_release(pstr_val);
     357             : 
     358         677 :     if (pstr_env_var != 0)
     359         677 :         rtl_string_release(pstr_env_var);
     360             : 
     361         677 :     return (result);
     362             : }
     363             : 
     364             : /***************************************
     365             :  osl_clearEnvironment().
     366             :  **************************************/
     367           6 : oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
     368             : {
     369           6 :     oslProcessError  result   = osl_Process_E_Unknown;
     370           6 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     371           6 :     rtl_String* pstr_env_var  = 0;
     372             : 
     373             :     OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
     374             : 
     375             :     rtl_uString2String(
     376             :         &pstr_env_var,
     377           6 :         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
     378          12 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     379             : 
     380           6 :     if (pstr_env_var)
     381             :     {
     382             : #if defined (SOLARIS)
     383             :         rtl_String * pBuffer = NULL;
     384             : 
     385             :         sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
     386             :             rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
     387             :         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
     388             : 
     389             :         rtl_string_acquire(pBuffer); // argument to putenv must leak on success
     390             : 
     391             :         if (putenv(rtl_string_getStr(pBuffer)) == 0)
     392             :             result = osl_Process_E_None;
     393             :         else
     394             :             rtl_string_release(pBuffer);
     395             : #elif (defined(MACOSX) || defined(NETBSD) || defined(FREEBSD))
     396             :         //MacOSX baseline is 10.4, which has an old-school void return
     397             :         //for unsetenv.
     398             :                 //See: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/10.4/man3/unsetenv.3.html?useVersion=10.4
     399             :         unsetenv(rtl_string_getStr(pstr_env_var));
     400             :         result = osl_Process_E_None;
     401             : #else
     402           6 :         if (unsetenv(rtl_string_getStr(pstr_env_var)) == 0)
     403           6 :             result = osl_Process_E_None;
     404             : #endif
     405           6 :         rtl_string_release(pstr_env_var);
     406             :     }
     407             : 
     408           6 :     return (result);
     409             : }
     410             : 
     411             : /***************************************
     412             :  osl_getProcessWorkingDir().
     413             :  **************************************/
     414       80208 : oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
     415             : {
     416       80208 :     oslProcessError result = osl_Process_E_Unknown;
     417             :     char buffer[PATH_MAX];
     418             : 
     419             :     OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
     420             : 
     421       80208 :     if (getcwd (buffer, sizeof(buffer)) != 0)
     422             :     {
     423       80208 :         rtl_uString* ustrTmp = 0;
     424             : 
     425             :         rtl_string2UString(
     426             :             &ustrTmp,
     427      160416 :             buffer, strlen(buffer), osl_getThreadTextEncoding(),
     428      160416 :             OSTRING_TO_OUSTRING_CVTFLAGS);
     429       80208 :         if (ustrTmp != 0)
     430             :         {
     431       80208 :             if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
     432       80208 :                 result = osl_Process_E_None;
     433       80208 :             rtl_uString_release (ustrTmp);
     434             :         }
     435             :     }
     436             : 
     437       80208 :     return (result);
     438             : }
     439             : 
     440             : /******************************************************************************
     441             :  *
     442             :  *              new functions to set/return the current process locale
     443             :  *
     444             :  *****************************************************************************/
     445             : 
     446             : struct ProcessLocale_Impl
     447             : {
     448             :     pthread_mutex_t m_mutex;
     449             :     rtl_Locale *    m_pLocale;
     450             : };
     451             : 
     452             : static struct ProcessLocale_Impl g_process_locale =
     453             : {
     454             :     PTHREAD_MUTEX_INITIALIZER,
     455             :     0
     456             : };
     457             : 
     458             : /**********************************************
     459             :  osl_getProcessLocale().
     460             :  *********************************************/
     461        3143 : oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
     462             : {
     463        3143 :     oslProcessError result = osl_Process_E_Unknown;
     464             :     OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
     465        3143 :     if (ppLocale)
     466             :     {
     467        3143 :         pthread_mutex_lock(&(g_process_locale.m_mutex));
     468             : 
     469        3143 :         if (g_process_locale.m_pLocale == 0)
     470        2837 :             _imp_getProcessLocale (&(g_process_locale.m_pLocale));
     471        3143 :         *ppLocale = g_process_locale.m_pLocale;
     472        3143 :         result = osl_Process_E_None;
     473             : 
     474        3143 :         pthread_mutex_unlock (&(g_process_locale.m_mutex));
     475             :     }
     476        3143 :     return (result);
     477             : }
     478             : 
     479             : /**********************************************
     480             :  osl_setProcessLocale().
     481             :  *********************************************/
     482           0 : oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
     483             : {
     484           0 :     oslProcessError result = osl_Process_E_Unknown;
     485             : 
     486             :     OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
     487             : 
     488           0 :     pthread_mutex_lock(&(g_process_locale.m_mutex));
     489           0 :     if (_imp_setProcessLocale (pLocale) == 0)
     490             :     {
     491           0 :         g_process_locale.m_pLocale = pLocale;
     492           0 :         result = osl_Process_E_None;
     493             :     }
     494           0 :     pthread_mutex_unlock (&(g_process_locale.m_mutex));
     495             : 
     496           0 :     return (result);
     497             : }
     498             : 
     499             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10