LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/desktop/unx/source - start.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 260 406 64.0 %
Date: 2013-07-09 Functions: 17 22 77.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : #include <signal.h>
      10             : #include <unistd.h>
      11             : #include <limits.h>
      12             : #include <stdlib.h>
      13             : #include <sys/types.h>
      14             : #include <sys/stat.h>
      15             : #include <sys/socket.h>
      16             : #include <arpa/inet.h>
      17             : #include <sys/un.h>
      18             : #include <sys/poll.h>
      19             : #include <fcntl.h>
      20             : #include <stdio.h>
      21             : #include <libgen.h>
      22             : #include <string.h>
      23             : #include <errno.h>
      24             : 
      25             : #include <osl/process.h>
      26             : #include <osl/thread.h>
      27             : #include <rtl/bootstrap.h>
      28             : #include <rtl/digest.h>
      29             : #include <rtl/process.h>
      30             : #include <rtl/ustrbuf.h>
      31             : #include <sal/main.h>
      32             : 
      33             : #include "args.h"
      34             : #include "../../source/inc/exithelper.h"
      35             : #include "splashx.h"
      36             : 
      37             : #define PIPEDEFAULTPATH      "/tmp"
      38             : #define PIPEALTERNATEPATH    "/var/tmp"
      39             : 
      40             : /* Easier conversions: rtl_uString to rtl_String */
      41             : static rtl_String *
      42         102 : ustr_to_str( rtl_uString *pStr )
      43             : {
      44         102 :     rtl_String *pOut = NULL;
      45             : 
      46         102 :     rtl_uString2String( &pOut, rtl_uString_getStr( pStr ),
      47         102 :             rtl_uString_getLength( pStr ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
      48             : 
      49         102 :     return pOut;
      50             : }
      51             : 
      52             : /* Easier conversions: char * to rtl_uString */
      53             : static rtl_uString *
      54          51 : charp_to_ustr( const char *pStr )
      55             : {
      56          51 :     rtl_uString *pOut = NULL;
      57             : 
      58          51 :     rtl_string2UString( &pOut, pStr, strlen( pStr ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
      59             : 
      60          51 :     return pOut;
      61             : }
      62             : 
      63             : /* Easier debugging of rtl_uString values. */
      64             : #if OSL_DEBUG_LEVEL > 1
      65             : static void
      66             : ustr_debug( const char *pMessage, rtl_uString *pStr )
      67             : {
      68             :     rtl_String *pOut = ustr_to_str( pStr );
      69             : 
      70             :     fprintf( stderr, "%s: %s\n", pMessage, rtl_string_getStr( pOut ) );
      71             : 
      72             :     rtl_string_release( pOut );
      73             :     return;
      74             : }
      75             : #else
      76             : #define ustr_debug( a, b ) {}
      77             : #endif
      78             : 
      79             : typedef struct {
      80             :     int        status_fd;
      81             :     oslProcess child;
      82             : } ChildInfo;
      83             : 
      84             : static int
      85           0 : child_info_get_status_fd (ChildInfo *info)
      86             : {
      87           0 :     return info->status_fd;
      88             : }
      89             : 
      90             : static void
      91          83 : child_info_destroy (ChildInfo *info)
      92             : {
      93          83 :     close (info->status_fd);
      94          83 :     osl_freeProcessHandle (info->child);
      95          83 :     free (info);
      96          83 : }
      97             : 
      98             : static ChildInfo *
      99          83 : child_spawn ( Args *args, sal_Bool bAllArgs, sal_Bool bWithStatus )
     100             : {
     101          83 :     rtl_uString *pApp = NULL, *pTmp = NULL;
     102             :     rtl_uString **ppArgs;
     103             :     sal_uInt32 nArgs, i;
     104             :     char buffer[64];
     105             :     ChildInfo *info;
     106             :     int status_pipe[2];
     107             :     oslProcessError nError;
     108             : 
     109          83 :     info = calloc (1, sizeof (ChildInfo));
     110             : 
     111             :     /* create pipe */
     112          83 :     if ( pipe( status_pipe ) < 0 )
     113             :     {
     114           0 :         fprintf( stderr, "ERROR: no file handles\n");
     115           0 :         exit( 1 );
     116             :     }
     117          83 :     info->status_fd = status_pipe[0];
     118             : 
     119             :     /* application name */
     120          83 :     rtl_uString_newFromAscii( &pApp, "file://" );
     121          83 :     rtl_uString_newConcat( &pApp, pApp, args->pAppPath );
     122          83 :     rtl_uString_newFromAscii( &pTmp, "soffice.bin" );
     123          83 :     rtl_uString_newConcat( &pApp, pApp, pTmp );
     124          83 :     rtl_uString_release( pTmp );
     125          83 :     pTmp = NULL;
     126             : 
     127             :     /* copy args */
     128          83 :     nArgs = bAllArgs ? args->nArgsTotal : args->nArgsEnv;
     129          83 :     ppArgs = (rtl_uString **)calloc( nArgs + 1, sizeof( rtl_uString* ) );
     130         749 :     for ( i = 0; i < nArgs; ++i )
     131         666 :         ppArgs[i] = args->ppArgs[i];
     132             : 
     133          83 :     if( bWithStatus )
     134             :     {
     135             :         /* add the pipe arg */
     136           0 :         snprintf (buffer, 63, "--splash-pipe=%d", status_pipe[1]);
     137           0 :         rtl_uString_newFromAscii( &pTmp, buffer );
     138           0 :         ppArgs[nArgs] = pTmp;
     139           0 :         ++nArgs;
     140             :     }
     141             : 
     142             :     /* start the main process */
     143          83 :     nError = osl_executeProcess( pApp, ppArgs, nArgs,
     144             :                                  osl_Process_NORMAL,
     145             :                                  NULL,
     146             :                                  NULL,
     147             :                                  NULL, 0,
     148             :                                  &info->child );
     149             : 
     150          83 :     if (pTmp)
     151           0 :         rtl_uString_release( pTmp );
     152          83 :     free (ppArgs);
     153             : 
     154          83 :     if ( nError != osl_Process_E_None )
     155             :     {
     156           0 :         fprintf( stderr, "ERROR %d forking process", nError );
     157             :         ustr_debug( "", pApp );
     158           0 :         rtl_uString_release( pApp );
     159           0 :         _exit (1);
     160             :     }
     161             : 
     162          83 :     rtl_uString_release( pApp );
     163          83 :     close( status_pipe[1] );
     164             : 
     165          83 :     return info;
     166             : }
     167             : 
     168             : static sal_Bool
     169          83 : child_exited_wait (ChildInfo *info, sal_Bool bShortWait)
     170             : {
     171          83 :     TimeValue t = { 0, 250 /* ms */ * 1000 * 1000 };
     172          83 :     if (!bShortWait)
     173          83 :         t.Seconds = 1024;
     174          83 :     return osl_joinProcessWithTimeout (info->child, &t) != osl_Process_E_TimedOut;
     175             : }
     176             : 
     177             : static int
     178          83 : child_get_exit_code (ChildInfo *info)
     179             : {
     180             :     oslProcessInfo inf;
     181             : 
     182          83 :     inf.Code = -1;
     183          83 :     inf.Size = sizeof (inf);
     184          83 :     if (osl_getProcessInfo (info->child, osl_Process_EXITCODE, &inf) != osl_Process_E_None)
     185             :     {
     186           0 :         fprintf (stderr, "Warning: failed to fetch libreoffice exit status\n");
     187           0 :         return -1;
     188             :     }
     189          83 :     return inf.Code;
     190             : }
     191             : 
     192             : typedef enum { ProgressContinue, ProgressRestart, ProgressExit } ProgressStatus;
     193             : 
     194             : /* Path of the application, with trailing slash. */
     195             : static rtl_uString *
     196          51 : get_app_path( const char *pAppExec )
     197             : {
     198             :     char pRealPath[PATH_MAX];
     199             :     rtl_uString *pResult;
     200             :     sal_Int32 len;
     201             :     char* dummy;
     202             : 
     203          51 :     char *pOrigPath = strdup( pAppExec );
     204          51 :     char *pPath = dirname( pOrigPath );
     205             : 
     206          51 :     dummy = realpath( pPath, pRealPath );
     207             :     (void)dummy;
     208          51 :     pResult = charp_to_ustr( pRealPath );
     209          51 :     free( pOrigPath );
     210             : 
     211          51 :     len = rtl_uString_getLength(pResult);
     212          51 :     if (len > 0 && rtl_uString_getStr(pResult)[len - 1] != '/')
     213             :     {
     214          51 :         rtl_uString *pSlash = NULL;
     215          51 :         rtl_uString_newFromAscii(&pSlash, "/");
     216          51 :         rtl_uString_newConcat(&pResult, pResult, pSlash);
     217          51 :         rtl_uString_release(pSlash);
     218             :     }
     219             : 
     220          51 :     return pResult;
     221             : }
     222             : 
     223             : /* Compute the OOo md5 hash from 'pText' */
     224             : static rtl_uString *
     225          51 : get_md5hash( rtl_uString *pText )
     226             : {
     227          51 :     rtl_uString *pResult = NULL;
     228          51 :     sal_Int32 nCapacity = 100;
     229          51 :     unsigned char *pData = NULL;
     230          51 :     sal_uInt32 nSize = 0;
     231             :     rtlDigest digest;
     232          51 :     sal_uInt32 md5_key_len = 0;
     233          51 :     sal_uInt8* md5_buf = NULL;
     234          51 :     sal_uInt32 i = 0;
     235             : #if OSL_DEBUG_LEVEL > 1
     236             :     rtl_String *pOut;
     237             : #endif
     238             : 
     239          51 :     if ( !pText )
     240           0 :         return NULL;
     241             : 
     242             : #if OSL_DEBUG_LEVEL > 1
     243             :     pOut = ustr_to_str( pText );
     244             :     fprintf (stderr, "Generate pipe md5 for '%s'\n", pOut->buffer);
     245             :     rtl_string_release( pOut );
     246             : #endif
     247             : 
     248          51 :     pData = (unsigned char *)rtl_uString_getStr( pText );
     249          51 :     nSize = rtl_uString_getLength( pText ) * sizeof( sal_Unicode );
     250          51 :     if ( !pData )
     251           0 :         return NULL;
     252             : 
     253          51 :     digest = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
     254          51 :     if ( digest == 0 )
     255           0 :         return NULL;
     256             : 
     257          51 :     md5_key_len = rtl_digest_queryLength( digest );
     258          51 :     md5_buf = (sal_uInt8 *)calloc( md5_key_len, sizeof( sal_uInt8 ) );
     259             : 
     260          51 :     rtl_digest_init( digest, pData , nSize );
     261          51 :     rtl_digest_update( digest, pData, nSize );
     262          51 :     rtl_digest_get( digest, md5_buf, md5_key_len );
     263          51 :     rtl_digest_destroy( digest );
     264             : 
     265             :     /* create hex-value string from the MD5 value to keep
     266             :        the string size minimal */
     267          51 :     rtl_uString_new_WithLength( &pResult, nCapacity );
     268         867 :     for ( ; i < md5_key_len; ++i )
     269             :     {
     270             :         char val[3];
     271         816 :         snprintf( val, 3, "%x", md5_buf[i] ); /* sic! we ignore some of the 0's */
     272             : 
     273         816 :         rtl_uStringbuffer_insert_ascii( &pResult, &nCapacity, rtl_uString_getLength( pResult ),
     274         816 :                 val, strlen( val ) );
     275             :     }
     276             : 
     277             :     /* cleanup */
     278          51 :     free( md5_buf );
     279             : 
     280          51 :     return pResult;
     281             : }
     282             : 
     283             : /* Construct the pipe name */
     284             : static rtl_uString *
     285          51 : get_pipe_path( rtl_uString *pAppPath )
     286             : {
     287          51 :     rtl_uString *pPath = NULL, *pTmp = NULL, *pUserInstallation = NULL;
     288          51 :     rtl_uString *pResult = NULL, *pBasePath = NULL, *pAbsUserInstallation = NULL;
     289             :     rtlBootstrapHandle handle;
     290          51 :     rtl_uString *pMd5hash = NULL;
     291             :     sal_Unicode pUnicode[RTL_USTR_MAX_VALUEOFINT32];
     292             : 
     293             :     /* setup bootstrap filename */
     294          51 :     rtl_uString_newFromAscii( &pPath, "file://" );
     295          51 :     rtl_uString_newConcat( &pPath, pPath, pAppPath );
     296          51 :     rtl_uString_newConcat( &pPath, pPath, pTmp );
     297          51 :     rtl_uString_newFromAscii( &pTmp, SAL_CONFIGFILE( "bootstrap" ) );
     298          51 :     rtl_uString_newConcat( &pPath, pPath, pTmp );
     299             : 
     300             :     ustr_debug( "bootstap", pPath );
     301             : 
     302             :     /* read userinstallation value */
     303          51 :     handle = rtl_bootstrap_args_open( pPath );
     304             : 
     305          51 :     rtl_uString_newFromAscii( &pTmp, "UserInstallation" );
     306          51 :     rtl_bootstrap_get_from_handle( handle, pTmp, &pUserInstallation, NULL );
     307             : 
     308          51 :     rtl_bootstrap_args_close( handle );
     309             : 
     310             :     /* turn it into an absolute path - unwinding symlinks etc. */
     311         102 :     if ( osl_getProcessWorkingDir (&pBasePath) ||
     312          51 :          osl_getAbsoluteFileURL( pBasePath, pUserInstallation, &pAbsUserInstallation ) )
     313           0 :         rtl_uString_newFromString (&pAbsUserInstallation, pUserInstallation);
     314             : 
     315             :     /* create the pipe name */
     316             :     ustr_debug( "user installation", pAbsUserInstallation );
     317          51 :     pMd5hash = get_md5hash( pAbsUserInstallation );
     318          51 :     if ( !pMd5hash )
     319           0 :         rtl_uString_new( &pMd5hash );
     320             : 
     321          51 :     if ( access( PIPEDEFAULTPATH, R_OK|W_OK ) == 0 )
     322          51 :         rtl_uString_newFromAscii( &pResult, PIPEDEFAULTPATH );
     323             :     else
     324           0 :         rtl_uString_newFromAscii( &pResult, PIPEALTERNATEPATH );
     325             : 
     326          51 :     rtl_uString_newFromAscii( &pTmp, "/OSL_PIPE_" );
     327          51 :     rtl_uString_newConcat( &pResult, pResult, pTmp );
     328             : 
     329          51 :     rtl_ustr_valueOfInt32( pUnicode, (int)getuid(), 10 );
     330          51 :     rtl_uString_newFromStr( &pTmp, pUnicode );
     331          51 :     rtl_uString_newConcat( &pResult, pResult, pTmp );
     332             : 
     333          51 :     rtl_uString_newFromAscii( &pTmp, "_SingleOfficeIPC_" );
     334          51 :     rtl_uString_newConcat( &pResult, pResult, pTmp );
     335             : 
     336          51 :     rtl_uString_newConcat( &pResult, pResult, pMd5hash );
     337             : 
     338             :     ustr_debug( "result", pResult );
     339             : 
     340             :     /* cleanup */
     341          51 :     rtl_uString_release( pMd5hash );
     342          51 :     rtl_uString_release( pPath );
     343          51 :     rtl_uString_release( pTmp );
     344          51 :     if ( pBasePath )
     345             :     {
     346          51 :         rtl_uString_release( pBasePath );
     347             :     }
     348          51 :     rtl_uString_release( pUserInstallation );
     349          51 :     rtl_uString_release( pAbsUserInstallation );
     350             : 
     351          51 :     return pResult;
     352             : }
     353             : 
     354             : /* Get fd of the pipe of the already running OOo. */
     355             : static int
     356          51 : connect_pipe( rtl_uString *pPipePath )
     357             : {
     358             :     int fd;
     359             :     size_t len;
     360             :     struct sockaddr_un addr;
     361             : 
     362          51 :     rtl_String *pPipeStr = ustr_to_str( pPipePath );
     363             : 
     364          51 :     memset( &addr, 0, sizeof( addr ) );
     365             : 
     366          51 :     if ( ( fd = socket( AF_UNIX, SOCK_STREAM, 0 ) ) < 0 )
     367           0 :         return fd;
     368             : 
     369          51 :     fcntl( fd, F_SETFD, FD_CLOEXEC );
     370             : 
     371          51 :     addr.sun_family = AF_UNIX;
     372          51 :     strncpy( addr.sun_path, rtl_string_getStr( pPipeStr ), sizeof( addr.sun_path ) - 1 );
     373          51 :     rtl_string_release( pPipeStr );
     374             : 
     375             : /* cut / paste from osl's pipe.c */
     376             : #if defined(FREEBSD)
     377             :     len = SUN_LEN( &addr );
     378             : #else
     379          51 :     len = sizeof( addr );
     380             : #endif
     381             : 
     382          51 :     if ( connect( fd, (struct sockaddr *)&addr, len ) < 0 )
     383             :     {
     384          51 :         close(fd);
     385          51 :         fd = -1;
     386             :     }
     387          51 :     return fd;
     388             : }
     389             : 
     390             : /* Escape: "," -> "\\,", "\0" -> "\\0", "\\" -> "\\\\" */
     391             : static rtl_uString *
     392           0 : escape_path( rtl_uString *pToEscape )
     393             : {
     394           0 :     rtl_uString *pBuffer = NULL;
     395           0 :     sal_Int32 nCapacity = 1000;
     396           0 :     sal_Int32 i = 0;
     397           0 :     sal_Int32 nEscapeLength = rtl_uString_getLength( pToEscape );
     398             : 
     399           0 :     rtl_uString_new_WithLength( &pBuffer, nCapacity );
     400             : 
     401           0 :     for ( ; i < nEscapeLength; ++i )
     402             :     {
     403           0 :         sal_Unicode c = pToEscape->buffer[i];
     404           0 :         switch ( c )
     405             :         {
     406             :             case (sal_Unicode)'\0':
     407           0 :                 rtl_uStringbuffer_insert_ascii( &pBuffer, &nCapacity,
     408             :                         rtl_uString_getLength( pBuffer ),
     409             :                         RTL_CONSTASCII_STRINGPARAM( "\\0" ) );
     410           0 :                 break;
     411             :             case (sal_Unicode)',':
     412           0 :                 rtl_uStringbuffer_insert_ascii( &pBuffer, &nCapacity,
     413             :                         rtl_uString_getLength( pBuffer ),
     414             :                         RTL_CONSTASCII_STRINGPARAM( "\\," ) );
     415           0 :                 break;
     416             :             case (sal_Unicode)'\\':
     417           0 :                 rtl_uStringbuffer_insert_ascii( &pBuffer, &nCapacity,
     418             :                         rtl_uString_getLength( pBuffer ),
     419             :                         RTL_CONSTASCII_STRINGPARAM( "\\\\" ) );
     420           0 :                 break;
     421             :             default:
     422           0 :                 rtl_uStringbuffer_insert( &pBuffer, &nCapacity,
     423             :                         rtl_uString_getLength( pBuffer ),
     424             :                         &c, 1 );
     425             :         }
     426             :     }
     427             : 
     428           0 :     return pBuffer;
     429             : }
     430             : 
     431             : /* Send args to the OOo instance (using the 'fd' file descriptor) */
     432             : static sal_Bool
     433           0 : send_args( int fd, rtl_uString *pCwdPath )
     434             : {
     435           0 :     rtl_uString *pBuffer = NULL, *pTmp = NULL;
     436           0 :     sal_Int32 nCapacity = 1000;
     437           0 :     rtl_String *pOut = NULL;
     438             :     sal_Bool bResult;
     439             :     size_t nLen;
     440           0 :     rtl_uString *pEscapedCwdPath = escape_path( pCwdPath );
     441           0 :     sal_uInt32 nArg = 0;
     442           0 :     sal_uInt32 nArgCount = rtl_getAppCommandArgCount();
     443             : 
     444           0 :     rtl_uString_new_WithLength( &pBuffer, nCapacity );
     445           0 :     rtl_uString_new( &pTmp );
     446             : 
     447           0 :     rtl_uStringbuffer_insert_ascii( &pBuffer, &nCapacity,
     448             :             rtl_uString_getLength( pBuffer ),
     449             :             RTL_CONSTASCII_STRINGPARAM( "InternalIPC::Arguments" ) );
     450             : 
     451           0 :     if ( rtl_uString_getLength( pEscapedCwdPath ) )
     452             :     {
     453           0 :         rtl_uStringbuffer_insert_ascii( &pBuffer, &nCapacity,
     454             :             rtl_uString_getLength( pBuffer ),
     455             :             RTL_CONSTASCII_STRINGPARAM( "1" ) );
     456           0 :         rtl_uStringbuffer_insert( &pBuffer, &nCapacity,
     457             :                     rtl_uString_getLength( pBuffer ),
     458           0 :                     rtl_uString_getStr( pEscapedCwdPath ),
     459             :                     rtl_uString_getLength( pEscapedCwdPath ) );
     460             :     }
     461             :     else
     462             :     {
     463           0 :         rtl_uStringbuffer_insert_ascii( &pBuffer, &nCapacity,
     464             :             rtl_uString_getLength( pBuffer ),
     465             :             RTL_CONSTASCII_STRINGPARAM( "0" ) );
     466             :     }
     467             : 
     468           0 :     for ( nArg = 0; nArg < nArgCount; ++nArg )
     469             :     {
     470           0 :         rtl_uString *pEscapedTmp = NULL;
     471           0 :         rtl_uStringbuffer_insert_ascii( &pBuffer, &nCapacity,
     472             :                 rtl_uString_getLength( pBuffer ),
     473             :                 ",", 1 );
     474             : 
     475           0 :         rtl_getAppCommandArg( nArg, &pTmp );
     476             : 
     477           0 :         pEscapedTmp = escape_path( pTmp );
     478             : 
     479           0 :         rtl_uStringbuffer_insert( &pBuffer, &nCapacity,
     480             :                 rtl_uString_getLength( pBuffer ),
     481           0 :                 rtl_uString_getStr( pEscapedTmp ),
     482             :                 rtl_uString_getLength( pEscapedTmp ) );
     483             : 
     484           0 :         rtl_uString_release( pEscapedTmp );
     485             :     }
     486             : 
     487             :     ustr_debug( "Pass args", pBuffer );
     488             : 
     489           0 :     if ( !rtl_convertUStringToString(
     490           0 :              &pOut, rtl_uString_getStr( pBuffer ),
     491             :              rtl_uString_getLength( pBuffer ), RTL_TEXTENCODING_UTF8,
     492             :              ( RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
     493             :                | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR ) ) )
     494             :     {
     495           0 :         fprintf( stderr, "ERROR: cannot convert arguments to UTF-8" );
     496           0 :         exit( 1 );
     497             :     }
     498             : 
     499           0 :     nLen = rtl_string_getLength( pOut ) + 1;
     500           0 :     bResult = ( write( fd, rtl_string_getStr( pOut ), nLen ) == (ssize_t) nLen );
     501             : 
     502           0 :     if ( bResult )
     503             :     {
     504           0 :         char resp[ strlen( "InternalIPC::ProcessingDone" ) ];
     505           0 :         ssize_t n = read( fd, resp, SAL_N_ELEMENTS( resp ) );
     506           0 :         bResult = n == (ssize_t) SAL_N_ELEMENTS( resp )
     507           0 :             && (memcmp(
     508           0 :                     resp, "InternalIPC::ProcessingDone",
     509             :                     SAL_N_ELEMENTS( resp ) )
     510             :                 == 0);
     511             :     }
     512             : 
     513             :     /* cleanup */
     514           0 :     rtl_uString_release( pEscapedCwdPath );
     515           0 :     rtl_uString_release( pBuffer );
     516           0 :     rtl_uString_release( pTmp );
     517           0 :     rtl_string_release( pOut );
     518             : 
     519           0 :     return bResult;
     520             : }
     521             : 
     522             : 
     523             : #define BUFFER_LEN 255
     524             : 
     525             : /* Read the percent to show in splash. */
     526             : static ProgressStatus
     527           0 : read_percent( ChildInfo *info, int *pPercent )
     528             : {
     529             :     static char pBuffer[BUFFER_LEN + 1];
     530             :     static char *pNext = pBuffer;
     531             :     static ssize_t nRead = 0;
     532             : 
     533             :     char *pBegin;
     534             :     char *pIter;
     535             :     char c;
     536             : 
     537             :     /* from the last call */
     538           0 :     int nNotProcessed = nRead - ( pNext - pBuffer );
     539           0 :     if ( nNotProcessed >= BUFFER_LEN )
     540           0 :         return sal_False;
     541             : 
     542           0 :     memmove( pBuffer, pNext, nNotProcessed );
     543             : 
     544             :     /* read data */
     545           0 :     nRead = read( child_info_get_status_fd (info),
     546           0 :                   pBuffer + nNotProcessed, BUFFER_LEN - nNotProcessed );
     547           0 :     if ( nRead < 0 ) {
     548           0 :         if (errno == EINTR)
     549           0 :             return ProgressContinue;
     550           0 :         return ProgressExit;
     551             :     }
     552             : 
     553           0 :     nRead += nNotProcessed;
     554           0 :     pBuffer[nRead] = '\0';
     555             : 
     556             :     /* skip old data */
     557           0 :     pBegin = pBuffer;
     558           0 :     pNext = pBuffer;
     559           0 :     for ( pIter = pBuffer; *pIter; ++pIter )
     560             :     {
     561           0 :         if ( *pIter == '\n' )
     562             :         {
     563           0 :             pBegin = pNext;
     564           0 :             pNext = pIter + 1;
     565             :         }
     566             :     }
     567             : 
     568             : #if OSL_DEBUG_LEVEL > 1
     569             :     fprintf( stderr, "Got status: %s\n", pBegin );
     570             : #endif
     571           0 :     if ( !strncasecmp( pBegin, "end", 3 ) )
     572           0 :         return ProgressExit;
     573           0 :     else if ( !strncasecmp( pBegin, "restart", 7 ) )
     574           0 :         return ProgressRestart;
     575           0 :     else if ( sscanf( pBegin, "%d%c", pPercent, &c ) == 2 && c == '%' )
     576           0 :         return ProgressContinue;
     577             : 
     578             :     /* unexpected - let's exit the splash to be safe */
     579           0 :     return ProgressExit;
     580             : }
     581             : 
     582             : /* Simple system check. */
     583             : static void
     584          51 : system_checks( void )
     585             : {
     586             : #ifdef LINUX
     587             :     struct stat buf;
     588             : 
     589             :     /* check proc is mounted - lots of things fail otherwise */
     590          51 :     if ( stat( "/proc/version", &buf ) != 0 )
     591             :     {
     592           0 :         fprintf( stderr, "ERROR: /proc not mounted - LibreOffice is unlikely to work well if at all" );
     593           0 :         exit( 1 );
     594             :     }
     595             : #endif
     596          51 : }
     597             : 
     598             : /* re-use the pagein code */
     599             : extern int pagein_execute (int argc, char **argv);
     600             : 
     601          51 : static char *build_pagein_path (Args *args, const char *pagein_name)
     602             : {
     603             :     char *path;
     604             :     rtl_String *app_path;
     605             : 
     606          51 :     app_path = ustr_to_str (args->pAppPath);
     607          51 :     path = malloc (
     608          51 :         RTL_CONSTASCII_LENGTH("@") + app_path->length + strlen (pagein_name) +
     609             :         1);
     610          51 :     strcpy (path, "@");
     611          51 :     strcpy (path + 1, rtl_string_getStr (app_path));
     612          51 :     strcat (path, pagein_name);
     613             : 
     614          51 :     rtl_string_release( app_path );
     615             : 
     616          51 :     return path;
     617             : }
     618             : 
     619             : void
     620          51 : exec_pagein (Args *args)
     621             : {
     622             :     char *argv[3];
     623             : 
     624             :     /* don't use -L - since that does a chdir that breaks relative paths */
     625          51 :     argv[0] = "dummy-pagein";
     626          51 :     argv[1] = build_pagein_path (args, "pagein-common");
     627          51 :     if (args->pPageinType) {
     628           0 :         argv[2] = build_pagein_path (args, args->pPageinType);
     629             :     } else
     630          51 :         argv[2] = NULL;
     631             : 
     632          51 :     pagein_execute (args->pPageinType ? 3 : 2, argv);
     633             : 
     634          51 :     if (argv[2])
     635           0 :         free (argv[2]);
     636          51 :     free (argv[1]);
     637          51 : }
     638             : 
     639             : #if defined SOLAR_JAVA
     640             : 
     641          51 : static void extend_library_path (const char *new_element)
     642             : {
     643          51 :     rtl_uString *pEnvName=NULL, *pOrigEnvVar=NULL, *pNewEnvVar=NULL;
     644             :     const char *pathname;
     645             : #ifdef AIX
     646             :     pathname = "LIBPATH";
     647             : #else
     648          51 :     pathname = "LD_LIBRARY_PATH";
     649             : #endif
     650             : 
     651          51 :     rtl_uString_newFromAscii( &pEnvName, pathname );
     652          51 :     rtl_uString_newFromAscii( &pNewEnvVar, new_element );
     653             : 
     654          51 :     osl_getEnvironment( pEnvName, &pOrigEnvVar );
     655          51 :     if (pOrigEnvVar && pOrigEnvVar->length)
     656             :     {
     657          51 :         rtl_uString *pDelim = NULL;
     658          51 :         rtl_uString_newFromAscii( &pDelim, ":" );
     659          51 :         rtl_uString_newConcat( &pNewEnvVar, pNewEnvVar, pDelim );
     660          51 :         rtl_uString_newConcat( &pNewEnvVar, pNewEnvVar, pOrigEnvVar );
     661          51 :         rtl_uString_release( pDelim );
     662             :     }
     663             : 
     664          51 :     osl_setEnvironment( pEnvName, pNewEnvVar );
     665             : 
     666          51 :     if (pOrigEnvVar)
     667          51 :         rtl_uString_release( pOrigEnvVar );
     668          51 :     rtl_uString_release( pNewEnvVar );
     669          51 :     rtl_uString_release( pEnvName );
     670          51 : }
     671             : 
     672             : static void
     673          51 : exec_javaldx (Args *args)
     674             : {
     675             :     char newpath[4096];
     676             :     sal_uInt32 nArgs;
     677             :     rtl_uString *pApp;
     678             :     rtl_uString **ppArgs;
     679             :     rtl_uString *pTmp, *pTmp2;
     680             : 
     681          51 :     oslProcess javaldx = NULL;
     682          51 :     oslFileHandle fileOut= 0;
     683             :     oslProcessError err;
     684             : 
     685          51 :     ppArgs = (rtl_uString **)calloc( args->nArgsEnv + 2, sizeof( rtl_uString* ) );
     686             : 
     687         154 :     for ( nArgs = 0; nArgs < args->nArgsEnv; ++nArgs )
     688         103 :         ppArgs[nArgs] = args->ppArgs[nArgs];
     689             : 
     690             :     /* Use absolute path to redirectrc */
     691          51 :     pTmp = NULL;
     692          51 :     rtl_uString_newFromAscii( &pTmp, "-env:INIFILENAME=vnd.sun.star.pathname:" );
     693          51 :     rtl_uString_newConcat( &pTmp, pTmp, args->pAppPath );
     694          51 :     pTmp2 = NULL;
     695          51 :     rtl_uString_newFromAscii( &pTmp2, "redirectrc" );
     696          51 :     rtl_uString_newConcat( &pTmp, pTmp, pTmp2 );
     697          51 :     ppArgs[nArgs] = pTmp;
     698          51 :     rtl_uString_release (pTmp2);
     699          51 :     nArgs++;
     700             : 
     701             :     /* And also to javaldx */
     702          51 :     pApp = NULL;
     703          51 :     rtl_uString_newFromAscii( &pApp, "file://" );
     704          51 :     rtl_uString_newConcat( &pApp, pApp, args->pAppPath );
     705          51 :     pTmp = NULL;
     706          51 :     rtl_uString_newFromAscii( &pTmp, "../ure-link/bin/javaldx" );
     707          51 :     rtl_uString_newConcat( &pApp, pApp, pTmp );
     708          51 :     rtl_uString_release( pTmp );
     709             : 
     710          51 :     err = osl_executeProcess_WithRedirectedIO( pApp, ppArgs, nArgs,
     711             :                                                osl_Process_NORMAL,
     712             :                                                NULL, // security
     713             :                                                NULL, // work dir
     714             :                                                NULL, 0,
     715             :                                                &javaldx, // process handle
     716             :                                                NULL,
     717             :                                                &fileOut,
     718             :                                                NULL);
     719             : 
     720          51 :     rtl_uString_release( ppArgs[nArgs-1] );
     721          51 :     rtl_uString_release( pApp );
     722          51 :     free( ppArgs );
     723             : 
     724          51 :     if( err != osl_Process_E_None)
     725             :     {
     726           0 :         fprintf (stderr, "Warning: failed to launch javaldx - java may not function correctly\n");
     727           0 :         if (javaldx)
     728           0 :             osl_freeProcessHandle(javaldx);
     729           0 :         if (fileOut)
     730           0 :             osl_closeFile(fileOut);
     731           0 :         return;
     732             :     } else {
     733             :         char *chomp;
     734             :         sal_uInt64 bytes_read;
     735             : 
     736             :         /* Magically osl_readLine doesn't work with pipes with E_SPIPE - so be this lame instead: */
     737          51 :         while (osl_readFile (fileOut, newpath, SAL_N_ELEMENTS (newpath), &bytes_read) == osl_File_E_INTR);
     738             : 
     739          51 :         if (bytes_read <= 0) {
     740           0 :             fprintf (stderr, "Warning: failed to read path from javaldx\n");
     741           0 :             if (javaldx)
     742           0 :                 osl_freeProcessHandle(javaldx);
     743           0 :             if (fileOut)
     744           0 :                 osl_closeFile(fileOut);
     745           0 :             return;
     746             :         }
     747          51 :         newpath[bytes_read] = '\0';
     748             : 
     749          51 :         if ((chomp = strstr (newpath, "\n")))
     750          51 :             *chomp = '\0';
     751             :     }
     752             : 
     753             : #if OSL_DEBUG_LEVEL > 1
     754             :     fprintf (stderr, "Adding javaldx path of '%s'\n", newpath);
     755             : #endif
     756          51 :     extend_library_path (newpath);
     757             : 
     758          51 :     if (javaldx)
     759          51 :         osl_freeProcessHandle(javaldx);
     760          51 :     if (fileOut)
     761          51 :         osl_closeFile(fileOut);
     762             : }
     763             : 
     764             : #endif
     765             : 
     766             : // has to be a global :(
     767             : oslProcess * volatile g_pProcess = 0;
     768             : 
     769           0 : void sigterm_handler(int ignored)
     770             : {
     771             :     (void) ignored;
     772           0 :     if (g_pProcess)
     773             :     {
     774             :         // forward signal to soffice.bin
     775           0 :         osl_terminateProcess(g_pProcess);
     776             :     }
     777           0 :     _exit(255);
     778             : }
     779             : 
     780             : 
     781         102 : SAL_IMPLEMENT_MAIN_WITH_ARGS( argc, argv )
     782             : {
     783          51 :     int fd = 0;
     784          51 :     sal_Bool bSentArgs = sal_False;
     785             :     const char* pUsePlugin;
     786          51 :     rtl_uString *pPipePath = NULL;
     787             :     Args *args;
     788          51 :     int status = 0;
     789          51 :     struct splash* splash = NULL;
     790             :     struct sigaction sigpipe_action;
     791             :     struct sigaction sigterm_action;
     792             : 
     793             :     /* turn SIGPIPE into an error */
     794          51 :     memset(&sigpipe_action, 0, sizeof(struct sigaction));
     795          51 :     sigpipe_action.sa_handler = SIG_IGN;
     796          51 :     sigemptyset(&sigpipe_action.sa_mask);
     797          51 :     sigaction(SIGPIPE, &sigpipe_action, 0);
     798          51 :     memset(&sigterm_action, 0, sizeof(struct sigaction));
     799          51 :     sigterm_action.sa_handler = &sigterm_handler;
     800          51 :     sigemptyset(&sigterm_action.sa_mask);
     801          51 :     sigaction(SIGTERM, &sigterm_action, 0);
     802             : 
     803          51 :     args = args_parse ();
     804          51 :     args->pAppPath = get_app_path( argv[0] );
     805          51 :     if ( !args->pAppPath )
     806             :     {
     807           0 :         fprintf( stderr, "ERROR: Can't read app link\n" );
     808           0 :         exit( 1 );
     809             :     }
     810             :     ustr_debug( "App path", args->pAppPath );
     811             : 
     812             : #ifndef ENABLE_QUICKSTART_LIBPNG
     813             :     /* we can't load and render it anyway */
     814             :     args->bInhibitSplash = sal_True;
     815             : #endif
     816             : 
     817          51 :     pUsePlugin = getenv( "SAL_USE_VCLPLUGIN" );
     818          51 :     if ( pUsePlugin && !strcmp(pUsePlugin, "svp") )
     819           0 :         args->bInhibitSplash = sal_True;
     820             : 
     821          51 :     if ( !args->bInhibitPipe )
     822             :     {
     823          51 :         pPipePath = get_pipe_path( args->pAppPath );
     824             : 
     825          51 :         if ( ( fd = connect_pipe( pPipePath ) ) >= 0 )
     826             :         {
     827             :             // Wait for answer
     828           0 :             char resp[ strlen( "InternalIPC::SendArguments" ) + 1];
     829           0 :             ssize_t n = read( fd, resp, SAL_N_ELEMENTS( resp ) );
     830           0 :             if (n == (ssize_t) SAL_N_ELEMENTS( resp )
     831           0 :                 && (memcmp(
     832           0 :                 resp, "InternalIPC::SendArguments",
     833             :                 SAL_N_ELEMENTS( resp ) - 1) == 0)) {
     834           0 :                 rtl_uString *pCwdPath = NULL;
     835           0 :                 osl_getProcessWorkingDir( &pCwdPath );
     836             : 
     837             :                 // Then send args
     838           0 :                 bSentArgs = send_args( fd, pCwdPath );
     839             :            }
     840             : 
     841           0 :             close( fd );
     842             :         }
     843             : #if OSL_DEBUG_LEVEL > 1
     844             :         else
     845             :             ustr_debug( "Failed to connect to pipe", pPipePath );
     846             : #endif
     847             :     }
     848             : 
     849          51 :     if ( !bSentArgs )
     850             :     {
     851             :         /* we have to prepare for, and exec the binary */
     852          51 :         int nPercent = 0;
     853             :         ChildInfo *info;
     854          51 :         sal_Bool bAllArgs = sal_True;
     855             :         sal_Bool bShortWait, bRestart;
     856             : 
     857             :         /* sanity check pieces */
     858          51 :         system_checks();
     859             : 
     860             :         /* load splash image and create window */
     861          51 :         if ( !args->bInhibitSplash )
     862             :         {
     863           0 :             splash = splash_create(args->pAppPath, argc, argv);
     864             :         }
     865             : 
     866             :         /* pagein */
     867          51 :         if (!args->bInhibitPagein)
     868          51 :             exec_pagein (args);
     869             : 
     870             :         /* javaldx */
     871             : #ifdef SOLAR_JAVA
     872          51 :         if (!args->bInhibitJavaLdx)
     873          51 :             exec_javaldx (args);
     874             : #endif
     875             : 
     876             :         do
     877             :         {
     878          83 :             bRestart = sal_False;
     879             : 
     880             :             /* fast updates if we have somewhere to update it to */
     881          83 :             bShortWait = splash ? sal_True : sal_False;
     882             : 
     883             :             /* Periodically update the splash & the percent according
     884             :                to what status_fd says, poll quickly only while starting */
     885          83 :             info = child_spawn (args, bAllArgs, bShortWait);
     886          83 :             g_pProcess = info->child;
     887         166 :             while (!child_exited_wait (info, bShortWait))
     888             :             {
     889             :                 ProgressStatus eResult;
     890             : 
     891           0 :                 splash_draw_progress( splash, nPercent );
     892           0 :                 eResult = read_percent( info, &nPercent );
     893           0 :                 if (eResult != ProgressContinue)
     894             :                 {
     895           0 :                     splash_destroy(splash);
     896           0 :                     splash = NULL;
     897           0 :                     bShortWait = sal_False;
     898             :                 }
     899             : 
     900             : #if OSL_DEBUG_LEVEL > 1
     901             :                 fprintf( stderr, "Polling, result is %s\n",
     902             :                          ( eResult == ProgressContinue )? "continue" :
     903             :                          ( ( eResult == ProgressRestart )? "restart" : "exit" ) );
     904             : #endif
     905             :             }
     906             : 
     907             : #if OSL_DEBUG_LEVEL > 0
     908             :             fprintf (stderr, "Exited with code '%d'\n", child_get_exit_code (info));
     909             : #endif
     910             : 
     911          83 :             status = child_get_exit_code(info);
     912          83 :             g_pProcess = 0; // reset
     913          83 :             switch (status) {
     914             :             case EXITHELPER_CRASH_WITH_RESTART: // re-start with just -env: parameters
     915             : #if OSL_DEBUG_LEVEL > 0
     916             :                 fprintf (stderr, "oosplash: re-start with just -env: params !\n");
     917             : #endif
     918           0 :                 bRestart = sal_True;
     919           0 :                 bAllArgs = sal_False;
     920           0 :                 break;
     921             :             case EXITHELPER_NORMAL_RESTART: // re-start with all arguments
     922             : #if OSL_DEBUG_LEVEL > 0
     923             :                 fprintf (stderr, "oosplash: re-start with all params !\n");
     924             : #endif
     925          32 :                 bRestart = sal_True;
     926          32 :                 bAllArgs = sal_True;
     927          32 :                 break;
     928             :             default:
     929          51 :                 break;
     930             :             }
     931             : 
     932          83 :             child_info_destroy (info);
     933          83 :         } while (bRestart);
     934             :     }
     935             : 
     936             :     /* cleanup */
     937          51 :     if ( pPipePath )
     938          51 :         rtl_uString_release( pPipePath );
     939          51 :     args_free (args);
     940             : 
     941          51 :     return status;
     942             : }
     943             : 
     944             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10