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

Generated by: LCOV version 1.10