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

Generated by: LCOV version 1.10