masterfix DEV300: #i10000# build fix
[LibreOffice.git] / extensions / source / plugin / unx / sysplug.cxx
blobe78ddf2322cf04a87e27120a13a33152d6c52aef
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include <cstdarg>
32 #include <sys/types.h>
33 #include <signal.h>
34 #include <sys/wait.h>
35 #include <osl/file.hxx>
36 #include <osl/thread.h>
37 #include <rtl/bootstrap.hxx>
39 #include <plugin/impl.hxx>
41 int UnxPluginComm::nConnCounter = 0;
43 UnxPluginComm::UnxPluginComm(
44 const String& /*mimetype*/,
45 const String& library,
46 XLIB_Window aParent,
47 int nDescriptor1,
48 int nDescriptor2
49 ) :
50 PluginComm( ::rtl::OUStringToOString( library, osl_getThreadTextEncoding() ), false ),
51 PluginConnector( nDescriptor2 )
53 char pDesc[32];
54 char pWindow[32];
55 sprintf( pWindow, "%d", (int)aParent );
56 sprintf( pDesc, "%d", nDescriptor1 );
57 ByteString aLib( library, osl_getThreadTextEncoding() );
58 rtl::OString path;
59 if (!getPluginappPath(&path)) {
60 fprintf( stderr, "cannot construct path to pluginapp.bin\n" );
61 m_nCommPID = -1;
62 return;
65 char const* pArgs[5];
66 pArgs[0] = path.getStr();
67 pArgs[1] = pDesc;
68 pArgs[2] = aLib.GetBuffer();
69 pArgs[3] = pWindow;
70 pArgs[4] = NULL;
72 #if OSL_DEBUG_LEVEL > 1
73 m_nCommPID = 10;
74 fprintf( stderr, "Try to launch: %s %s %s %s, descriptors are %d, %d\n", pArgs[0], pArgs[1], pArgs[2], pArgs[3], nDescriptor1, nDescriptor2 );
75 #endif
77 if( ! ( m_nCommPID = fork() ) )
79 execvp( pArgs[0], const_cast< char ** >(pArgs) );
80 fprintf( stderr, "Error: could not exec %s\n", pArgs[0] );
81 _exit(255);
84 if( m_nCommPID != -1 )
86 // wait for pluginapp.bin to start up
87 if( ! WaitForMessage( 5000 ) )
89 fprintf( stderr, "Timeout on command: %s %s %s %s\n", pArgs[0], pArgs[1], pArgs[2], pArgs[3] );
90 invalidate();
92 else
94 MediatorMessage* pMessage = GetNextMessage( sal_True );
95 Respond( pMessage->m_nID,
96 const_cast<char*>("init ack"),8,
97 NULL );
98 delete pMessage;
99 NPP_Initialize();
104 UnxPluginComm::~UnxPluginComm()
106 NPP_Shutdown();
107 if( m_nCommPID != -1 && m_nCommPID != 0 )
109 int status = 16777216;
110 pid_t nExit = waitpid( m_nCommPID, &status, WUNTRACED );
111 #if OSL_DEBUG_LEVEL > 1
112 fprintf( stderr, "child %d (plugin app child %d) exited with status %d\n", (int)nExit, (int)m_nCommPID, (int)WEXITSTATUS(status) );
113 #else
114 (void)nExit;
115 #endif
119 bool UnxPluginComm::getPluginappPath(rtl::OString * path) {
120 OSL_ASSERT(path != NULL);
121 rtl::OUString p(
122 RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/program/pluginapp.bin"));
123 rtl::Bootstrap::expandMacros(p);
124 return
125 (osl::FileBase::getSystemPathFromFileURL(p, p) ==
126 osl::FileBase::E_None) &&
127 p.convertToString(
128 path, osl_getThreadTextEncoding(),
129 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
130 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR));