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: 102 134 76.1 %
Date: 2012-12-17 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             : 
      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         294 : oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
     102             :     rtl_uString ** ppFileURL
     103             : ) SAL_THROW_EXTERN_C()
     104             : {
     105         294 :     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         294 :     void * addr = dlsym (RTLD_DEFAULT, "main");
     114             : #endif
     115         294 :     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         294 :     if (result == osl_Process_E_NotFound)
     127         294 :         result = osl_getExecutableFile (ppFileURL);
     128             : 
     129         294 :     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         612 : oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
     155             : {
     156         612 :     oslProcessError result = osl_Process_E_NotFound;
     157             : 
     158         612 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     159             :     OSL_ASSERT(g_command_args.m_nCount > 0);
     160         612 :     if (g_command_args.m_nCount > 0)
     161             :     {
     162             :         /* CommandArgs set. Obtain argv[0]. */
     163         612 :         rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
     164         612 :         result = osl_Process_E_None;
     165             :     }
     166         612 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     167             : 
     168         612 :     return (result);
     169             : }
     170             : 
     171             : /***************************************
     172             :  osl_getCommandArgCount().
     173             :  **************************************/
     174         794 : sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
     175             : {
     176         794 :     sal_uInt32 result = 0;
     177             : 
     178         794 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     179         794 :     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         794 :     if (g_command_args.m_nCount > 0)
     185         794 :         result = g_command_args.m_nCount - 1;
     186         794 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     187             : 
     188         794 :     return (result);
     189             : }
     190             : 
     191             : /***************************************
     192             :  osl_getCommandArg().
     193             :  **************************************/
     194        5870 : oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
     195             : {
     196        5870 :     oslProcessError result = osl_Process_E_NotFound;
     197             : 
     198        5870 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     199             :     OSL_ASSERT(g_command_args.m_nCount > 0);
     200        5870 :     if (g_command_args.m_nCount > (nArg + 1))
     201             :     {
     202        5870 :         rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
     203        5870 :         result = osl_Process_E_None;
     204             :     }
     205        5870 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     206             : 
     207        5870 :     return (result);
     208             : }
     209             : 
     210             : /***************************************
     211             :  osl_setCommandArgs().
     212             :  **************************************/
     213         492 : void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
     214             : {
     215             :     OSL_ASSERT(argc > 0);
     216         492 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     217             :     OSL_ENSURE (g_command_args.m_nCount == 0, "osl_setCommandArgs(): CommandArgs already set.");
     218         492 :     if (g_command_args.m_nCount == 0)
     219             :     {
     220         492 :         rtl_uString** ppArgs = (rtl_uString**)rtl_allocateZeroMemory (argc * sizeof(rtl_uString*));
     221         492 :         if (ppArgs != 0)
     222             :         {
     223         492 :             rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     224        3754 :             for (int i = 0; i < argc; i++)
     225             :             {
     226             :                 rtl_string2UString (
     227             :                     &(ppArgs[i]),
     228        6524 :                     argv[i], rtl_str_getLength (argv[i]), encoding,
     229        9786 :                     OSTRING_TO_OUSTRING_CVTFLAGS);
     230             :             }
     231         492 :             if (ppArgs[0] != 0)
     232             :             {
     233             : #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
     234             :                 /* see @ osl_getExecutableFile(). */
     235         492 :                 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         492 :                 rtl_uString * pArg0 = 0;
     255         492 :                 if (realpath_u (ppArgs[0], &pArg0))
     256             :                 {
     257         492 :                     osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
     258         492 :                     rtl_uString_release (pArg0);
     259             :                 }
     260             :             }
     261         492 :             g_command_args.m_nCount = argc;
     262         492 :             g_command_args.m_ppArgs = ppArgs;
     263             :         }
     264             :     }
     265         492 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     266         492 : }
     267             : 
     268             : /***************************************
     269             :  osl_getEnvironment().
     270             :  **************************************/
     271        9609 : oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
     272             : {
     273        9609 :     oslProcessError  result   = osl_Process_E_NotFound;
     274        9609 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     275        9609 :     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        9609 :         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
     283       19218 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     284        9609 :     if (pstr_env_var != 0)
     285             :     {
     286        9609 :         const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
     287        9609 :         if (p_env_var != 0)
     288             :         {
     289             :             rtl_string2UString(
     290             :                 ppustrValue,
     291          86 :                 p_env_var, strlen(p_env_var), encoding,
     292         172 :                 OSTRING_TO_OUSTRING_CVTFLAGS);
     293             :             OSL_ASSERT(*ppustrValue != NULL);
     294             : 
     295          86 :             result = osl_Process_E_None;
     296             :         }
     297        9609 :         rtl_string_release(pstr_env_var);
     298             :     }
     299             : 
     300        9609 :     return (result);
     301             : }
     302             : 
     303             : /***************************************
     304             :  osl_setEnvironment().
     305             :  **************************************/
     306          87 : oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
     307             : {
     308          87 :     oslProcessError  result   = osl_Process_E_Unknown;
     309          87 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     310          87 :     rtl_String* pstr_env_var  = 0;
     311          87 :     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          87 :         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
     319         174 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     320             : 
     321             :     rtl_uString2String(
     322             :         &pstr_val,
     323          87 :         rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
     324         174 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     325             : 
     326          87 :     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          87 :         if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
     346          87 :             result = osl_Process_E_None;
     347             : #endif
     348             :     }
     349             : 
     350          87 :     if (pstr_val)
     351          87 :         rtl_string_release(pstr_val);
     352             : 
     353          87 :     if (pstr_env_var != 0)
     354          87 :         rtl_string_release(pstr_env_var);
     355             : 
     356          87 :     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       15569 : oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
     411             : {
     412       15569 :     oslProcessError result = osl_Process_E_Unknown;
     413             :     char buffer[PATH_MAX];
     414             : 
     415             :     OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
     416             : 
     417       15569 :     if (getcwd (buffer, sizeof(buffer)) != 0)
     418             :     {
     419       15569 :         rtl_uString* ustrTmp = 0;
     420             : 
     421             :         rtl_string2UString(
     422             :             &ustrTmp,
     423       31138 :             buffer, strlen(buffer), osl_getThreadTextEncoding(),
     424       31138 :             OSTRING_TO_OUSTRING_CVTFLAGS);
     425       15569 :         if (ustrTmp != 0)
     426             :         {
     427       15569 :             if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
     428       15569 :                 result = osl_Process_E_None;
     429       15569 :             rtl_uString_release (ustrTmp);
     430             :         }
     431             :     }
     432             : 
     433       15569 :     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       13350 : oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
     461             : {
     462       13350 :     oslProcessError result = osl_Process_E_Unknown;
     463             :     OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
     464       13350 :     if (ppLocale)
     465             :     {
     466       13350 :         pthread_mutex_lock(&(g_process_locale.m_mutex));
     467             : 
     468       13350 :         if (g_process_locale.m_pLocale == 0)
     469         554 :             _imp_getProcessLocale (&(g_process_locale.m_pLocale));
     470       13350 :         *ppLocale = g_process_locale.m_pLocale;
     471       13350 :         result = osl_Process_E_None;
     472             : 
     473       13350 :         pthread_mutex_unlock (&(g_process_locale.m_mutex));
     474             :     }
     475       13350 :     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