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