LCOV - code coverage report
Current view: top level - sal/osl/unx - process_impl.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 103 139 74.1 %
Date: 2014-04-14 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       42506 : oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
      90             :     rtl_uString ** ppFileURL
      91             : ) SAL_THROW_EXTERN_C()
      92             : {
      93       42506 :     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       42506 :     void * addr = dlsym (RTLD_DEFAULT, "main");
     102             : #endif
     103       42506 :     if (addr != 0)
     104             :     {
     105             :         /* Determine module URL. */
     106           0 :         if (osl_getModuleURLFromAddress (addr, ppFileURL))
     107             :         {
     108             :             /* Success. */
     109           0 :             result = osl_Process_E_None;
     110             :         }
     111             :     }
     112             : 
     113             :     /* Fallback to ordinary osl_getExecutableFile(). */
     114       42506 :     if (result == osl_Process_E_NotFound)
     115       42506 :         result = osl_getExecutableFile (ppFileURL);
     116             : 
     117       42506 :     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       42509 : oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
     143             : {
     144       42509 :     oslProcessError result = osl_Process_E_NotFound;
     145             : 
     146       42509 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     147             :     OSL_ASSERT(g_command_args.m_nCount > 0);
     148       42509 :     if (g_command_args.m_nCount > 0)
     149             :     {
     150             :         /* CommandArgs set. Obtain argv[0]. */
     151       42509 :         rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
     152       42509 :         result = osl_Process_E_None;
     153             :     }
     154       42509 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     155             : 
     156       42509 :     return (result);
     157             : }
     158             : 
     159             : /***************************************
     160             :  osl_getCommandArgCount().
     161             :  **************************************/
     162      127517 : sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
     163             : {
     164      127517 :     sal_uInt32 result = 0;
     165             : 
     166      127517 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     167             :     assert (g_command_args.m_nCount != 0);
     168      127517 :     if (g_command_args.m_nCount > 0)
     169      127517 :         result = g_command_args.m_nCount - 1;
     170      127517 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     171             : 
     172      127517 :     return (result);
     173             : }
     174             : 
     175             : /***************************************
     176             :  osl_getCommandArg().
     177             :  **************************************/
     178      425147 : oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
     179             : {
     180      425147 :     oslProcessError result = osl_Process_E_NotFound;
     181             : 
     182      425147 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     183             :     OSL_ASSERT(g_command_args.m_nCount > 0);
     184      425147 :     if (g_command_args.m_nCount > (nArg + 1))
     185             :     {
     186      425147 :         rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
     187      425147 :         result = osl_Process_E_None;
     188             :     }
     189      425147 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     190             : 
     191      425147 :     return (result);
     192             : }
     193             : 
     194             : /***************************************
     195             :  osl_setCommandArgs().
     196             :  **************************************/
     197       85011 : void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
     198             : {
     199             :     // special case for argc == 0: set up fake command line
     200       85011 :     int nArgs(argc ? argc : 1);
     201       85011 :     pthread_mutex_lock (&(g_command_args.m_mutex));
     202             :     assert (g_command_args.m_nCount == 0);
     203       85011 :     if (g_command_args.m_nCount == 0)
     204             :     {
     205             :         rtl_uString** ppArgs =
     206       85011 :             (rtl_uString**)rtl_allocateZeroMemory(nArgs * sizeof(rtl_uString*));
     207       85011 :         if (ppArgs != 0 && argc == 0)
     208             :         {
     209             :             // special case: set up fake command line
     210             :             char const*const arg =
     211           0 :                 "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           0 :                 OSTRING_TO_OUSTRING_CVTFLAGS);
     215           0 :             g_command_args.m_nCount = nArgs;
     216           0 :             g_command_args.m_ppArgs = ppArgs;
     217             :         }
     218       85011 :         else if (ppArgs != 0)
     219             :         {
     220       85011 :             rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     221      340080 :             for (int i = 0; i < argc; i++)
     222             :             {
     223             :                 rtl_string2UString (
     224             :                     &(ppArgs[i]),
     225      510138 :                     argv[i], rtl_str_getLength (argv[i]), encoding,
     226      765207 :                     OSTRING_TO_OUSTRING_CVTFLAGS);
     227             :             }
     228       85011 :             if (ppArgs[0] != 0)
     229             :             {
     230             : #if !defined(ANDROID) && !defined(IOS) // No use searching PATH on Android or iOS
     231             :                 /* see @ osl_getExecutableFile(). */
     232       85011 :                 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       85011 :                 rtl_uString * pArg0 = 0;
     252       85011 :                 if (realpath_u (ppArgs[0], &pArg0))
     253             :                 {
     254       85011 :                     osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
     255       85011 :                     rtl_uString_release (pArg0);
     256             :                 }
     257             :             }
     258       85011 :             g_command_args.m_nCount = argc;
     259       85011 :             g_command_args.m_ppArgs = ppArgs;
     260             :         }
     261             :     }
     262       85011 :     pthread_mutex_unlock (&(g_command_args.m_mutex));
     263       85011 : }
     264             : 
     265             : /***************************************
     266             :  osl_getEnvironment().
     267             :  **************************************/
     268     1997763 : oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
     269             : {
     270     1997763 :     oslProcessError  result   = osl_Process_E_NotFound;
     271     1997763 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     272     1997763 :     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     1997763 :         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
     280     3995526 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     281     1997763 :     if (pstr_env_var != 0)
     282             :     {
     283     1997763 :         const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
     284     1997763 :         if (p_env_var != 0)
     285             :         {
     286             :             rtl_string2UString(
     287             :                 ppustrValue,
     288       42507 :                 p_env_var, strlen(p_env_var), encoding,
     289       85014 :                 OSTRING_TO_OUSTRING_CVTFLAGS);
     290             :             OSL_ASSERT(*ppustrValue != NULL);
     291             : 
     292       42507 :             result = osl_Process_E_None;
     293             :         }
     294     1997763 :         rtl_string_release(pstr_env_var);
     295             :     }
     296             : 
     297     1997763 :     return (result);
     298             : }
     299             : 
     300             : /***************************************
     301             :  osl_setEnvironment().
     302             :  **************************************/
     303       42507 : oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
     304             : {
     305       42507 :     oslProcessError  result   = osl_Process_E_Unknown;
     306       42507 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     307       42507 :     rtl_String* pstr_env_var  = 0;
     308       42507 :     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       42507 :         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
     316       85014 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     317             : 
     318             :     rtl_uString2String(
     319             :         &pstr_val,
     320       42507 :         rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
     321       85014 :         OUSTRING_TO_OSTRING_CVTFLAGS);
     322             : 
     323       42507 :     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       42507 :         if (setenv(rtl_string_getStr(pstr_env_var), rtl_string_getStr(pstr_val), 1) == 0)
     343       42507 :             result = osl_Process_E_None;
     344             : #endif
     345             :     }
     346             : 
     347       42507 :     if (pstr_val)
     348       42507 :         rtl_string_release(pstr_val);
     349             : 
     350       42507 :     if (pstr_env_var != 0)
     351       42507 :         rtl_string_release(pstr_env_var);
     352             : 
     353       42507 :     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      127528 : oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
     407             : {
     408      127528 :     oslProcessError result = osl_Process_E_Unknown;
     409             :     char buffer[PATH_MAX];
     410             : 
     411             :     OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
     412             : 
     413      127528 :     if (getcwd (buffer, sizeof(buffer)) != 0)
     414             :     {
     415      127528 :         rtl_uString* ustrTmp = 0;
     416             : 
     417             :         rtl_string2UString(
     418             :             &ustrTmp,
     419      255056 :             buffer, strlen(buffer), osl_getThreadTextEncoding(),
     420      255056 :             OSTRING_TO_OUSTRING_CVTFLAGS);
     421      127528 :         if (ustrTmp != 0)
     422             :         {
     423      127528 :             if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
     424      127528 :                 result = osl_Process_E_None;
     425      127528 :             rtl_uString_release (ustrTmp);
     426             :         }
     427             :     }
     428             : 
     429      127528 :     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       85011 : oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
     454             : {
     455       85011 :     oslProcessError result = osl_Process_E_Unknown;
     456             :     OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
     457       85011 :     if (ppLocale)
     458             :     {
     459       85011 :         pthread_mutex_lock(&(g_process_locale.m_mutex));
     460             : 
     461       85011 :         if (g_process_locale.m_pLocale == 0)
     462       85011 :             _imp_getProcessLocale (&(g_process_locale.m_pLocale));
     463       85011 :         *ppLocale = g_process_locale.m_pLocale;
     464       85011 :         result = osl_Process_E_None;
     465             : 
     466       85011 :         pthread_mutex_unlock (&(g_process_locale.m_mutex));
     467             :     }
     468       85011 :     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