xref: /AOO41X/main/sal/osl/os2/process.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "system.h"
29*cdf0e10cSrcweir #include <osl/thread.h>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir //#include <osl/socket.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #ifndef _OSL_FILE_PATH_HELPER_H_
35*cdf0e10cSrcweir #include "file_path_helper.h"
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "procimpl.h"
39*cdf0e10cSrcweir //#include "sockimpl.h"
40*cdf0e10cSrcweir //#include "secimpl.h"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <ctype.h>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //#ifndef _RTL_USTRING_HXX_
45*cdf0e10cSrcweir #include <rtl/ustring.hxx>
46*cdf0e10cSrcweir //#endif
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir // for exception logging
49*cdf0e10cSrcweir #include <stdio.h>
50*cdf0e10cSrcweir #include <setjmp.h>
51*cdf0e10cSrcweir #include "helpers/except.h"
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #define MAX_ARGS 255
55*cdf0e10cSrcweir #define PIPENAMEMASK  "\\PIPE\\OSL_PIPE_%u"
56*cdf0e10cSrcweir #define SEMNAMEMASK   "\\SEM32\\OSL_SEM_%u"
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir typedef enum {
59*cdf0e10cSrcweir 	MSG_DATA,
60*cdf0e10cSrcweir 	MSG_END,
61*cdf0e10cSrcweir 	MSG_ACK,
62*cdf0e10cSrcweir 	MSG_REL,
63*cdf0e10cSrcweir 	MSG_UNKNOWN
64*cdf0e10cSrcweir } MessageType;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir typedef struct {
67*cdf0e10cSrcweir 	MessageType		  m_Type;
68*cdf0e10cSrcweir 	oslDescriptorFlag m_Flags;
69*cdf0e10cSrcweir 	oslDescriptorType m_Data;
70*cdf0e10cSrcweir 	HANDLE			  m_Value;
71*cdf0e10cSrcweir } Message;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir typedef struct {
74*cdf0e10cSrcweir 	HPIPE	m_hPipe;
75*cdf0e10cSrcweir } Pipe;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir typedef struct _oslSocketCallbackArg {
78*cdf0e10cSrcweir 	HANDLE	m_socket;
79*cdf0e10cSrcweir 	Pipe*	m_pipe;
80*cdf0e10cSrcweir } oslSocketCallbackArg;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir /* process termination queue */
83*cdf0e10cSrcweir static sal_Bool            bInitSessionTerm = sal_False;
84*cdf0e10cSrcweir static const sal_Char * const SessionTermQueueName = "\\QUEUES\\SESSIONS.QUE";
85*cdf0e10cSrcweir static HQUEUE             SessionTermQueue;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir /******************************************************************************
88*cdf0e10cSrcweir  *
89*cdf0e10cSrcweir  *                  Function Declarations
90*cdf0e10cSrcweir  *
91*cdf0e10cSrcweir  *****************************************************************************/
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir oslProcessError SAL_CALL osl_psz_executeProcess(sal_Char *pszImageName,
94*cdf0e10cSrcweir                                                 sal_Char *pszArguments[],
95*cdf0e10cSrcweir                                                 oslProcessOption Options,
96*cdf0e10cSrcweir                                                 oslSecurity Security,
97*cdf0e10cSrcweir                                                 sal_Char *pszDirectory,
98*cdf0e10cSrcweir                                                 sal_Char *pszEnvironments[],
99*cdf0e10cSrcweir                                                 oslProcess *pProcess,
100*cdf0e10cSrcweir 												oslFileHandle *pInputWrite,
101*cdf0e10cSrcweir 												oslFileHandle *pOutputRead,
102*cdf0e10cSrcweir 												oslFileHandle *pErrorRead );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir /* implemented in file.c */
105*cdf0e10cSrcweir extern oslFileError FileURLToPath( char *, size_t, rtl_uString* );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir static sal_Bool InitSessionTerm( void )
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir 	DosCreateQueue( &SessionTermQueue, QUE_FIFO, (PCSZ) SessionTermQueueName );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	return sal_True;
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir /******************************************************************************
115*cdf0e10cSrcweir  *
116*cdf0e10cSrcweir  *                  Functions for starting a process
117*cdf0e10cSrcweir  *
118*cdf0e10cSrcweir  *****************************************************************************/
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir /**********************************************
121*cdf0e10cSrcweir  osl_executeProcess_WithRedirectedIO
122*cdf0e10cSrcweir  *********************************************/
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir oslProcessError SAL_CALL osl_executeProcess_WithRedirectedIO(
125*cdf0e10cSrcweir 											rtl_uString *ustrImageName,
126*cdf0e10cSrcweir 											rtl_uString *ustrArguments[],
127*cdf0e10cSrcweir                                             sal_uInt32   nArguments,
128*cdf0e10cSrcweir 											oslProcessOption Options,
129*cdf0e10cSrcweir 											oslSecurity Security,
130*cdf0e10cSrcweir 											rtl_uString *ustrWorkDir,
131*cdf0e10cSrcweir 											rtl_uString *ustrEnvironment[],
132*cdf0e10cSrcweir                                             sal_uInt32   nEnvironmentVars,
133*cdf0e10cSrcweir 											oslProcess *pProcess,
134*cdf0e10cSrcweir 											oslFileHandle	*pInputWrite,
135*cdf0e10cSrcweir 											oslFileHandle	*pOutputRead,
136*cdf0e10cSrcweir 											oslFileHandle	*pErrorRead
137*cdf0e10cSrcweir 											)
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     oslProcessError Error;
141*cdf0e10cSrcweir     sal_Char* pszWorkDir=0;
142*cdf0e10cSrcweir     sal_Char** pArguments=0;
143*cdf0e10cSrcweir     sal_Char** pEnvironment=0;
144*cdf0e10cSrcweir     unsigned int index;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     char szImagePath[PATH_MAX] = "";
147*cdf0e10cSrcweir     char szWorkDir[PATH_MAX] = "";
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir #if 0
150*cdf0e10cSrcweir 	if (Options & osl_Process_SEARCHPATH)
151*cdf0e10cSrcweir 	{
152*cdf0e10cSrcweir 		const rtl::OUString PATH1;
153*cdf0e10cSrcweir 		OUString PATH (RTL_CONSTASCII_USTRINGPARAM("PATH"));
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 		rtl_uString * pSearchPath = 0;
156*cdf0e10cSrcweir 		osl_getEnvironment (PATH.pData, &pSearchPath);
157*cdf0e10cSrcweir 		if (pSearchPath)
158*cdf0e10cSrcweir 		{
159*cdf0e10cSrcweir 			rtl_uString * pSearchResult = 0;
160*cdf0e10cSrcweir 			osl_searchPath (ustrImageName, pSearchPath, &pSearchResult);
161*cdf0e10cSrcweir 			if (pSearchResult)
162*cdf0e10cSrcweir 			{
163*cdf0e10cSrcweir 				rtl_uString_assign (ustrImageName, pSearchResult);
164*cdf0e10cSrcweir 				rtl_uString_release (pSearchResult);
165*cdf0e10cSrcweir 			}
166*cdf0e10cSrcweir 			rtl_uString_release (pSearchPath);
167*cdf0e10cSrcweir 		}
168*cdf0e10cSrcweir 	}
169*cdf0e10cSrcweir #endif
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     if ( ustrImageName && ustrImageName->length )
172*cdf0e10cSrcweir     {
173*cdf0e10cSrcweir         FileURLToPath( szImagePath, PATH_MAX, ustrImageName );
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     if ( ustrWorkDir != 0 && ustrWorkDir->length )
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir         FileURLToPath( szWorkDir, PATH_MAX, ustrWorkDir );
179*cdf0e10cSrcweir         pszWorkDir = szWorkDir;
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     if ( pArguments == 0 && nArguments > 0 )
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         pArguments = (sal_Char**) malloc( ( nArguments + 2 ) * sizeof(sal_Char*) );
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     for ( index = 0 ; index < nArguments ; ++index )
189*cdf0e10cSrcweir     {
190*cdf0e10cSrcweir         rtl_String* strArg =0;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir         rtl_uString2String( &strArg,
194*cdf0e10cSrcweir                             rtl_uString_getStr(ustrArguments[index]),
195*cdf0e10cSrcweir                             rtl_uString_getLength(ustrArguments[index]),
196*cdf0e10cSrcweir                             osl_getThreadTextEncoding(),
197*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         pArguments[index]=strdup(rtl_string_getStr(strArg));
200*cdf0e10cSrcweir         rtl_string_release(strArg);
201*cdf0e10cSrcweir 		pArguments[index+1]=0;
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     for ( index = 0 ; index < nEnvironmentVars ; ++index )
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         rtl_String* strEnv=0;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         if ( pEnvironment == 0 )
209*cdf0e10cSrcweir         {
210*cdf0e10cSrcweir             pEnvironment = (sal_Char**) malloc( ( nEnvironmentVars + 2 ) * sizeof(sal_Char*) );
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         rtl_uString2String( &strEnv,
214*cdf0e10cSrcweir                             rtl_uString_getStr(ustrEnvironment[index]),
215*cdf0e10cSrcweir                             rtl_uString_getLength(ustrEnvironment[index]),
216*cdf0e10cSrcweir                             osl_getThreadTextEncoding(),
217*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir         pEnvironment[index]=strdup(rtl_string_getStr(strEnv));
220*cdf0e10cSrcweir         rtl_string_release(strEnv);
221*cdf0e10cSrcweir         pEnvironment[index+1]=0;
222*cdf0e10cSrcweir     }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 	int		rc, pid;
225*cdf0e10cSrcweir 	int		saveOutput = -1, saveInput = -1, saveError = -1;
226*cdf0e10cSrcweir 	int		stdOutput[2] = { -1, -1 }, stdInput[2] = { -1, -1 }, stdError[2] = { -1, -1 };
227*cdf0e10cSrcweir 	FILE	*i, *o, *e;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 	if (pInputWrite)
230*cdf0e10cSrcweir 		pipe( stdInput);
231*cdf0e10cSrcweir 	if (pOutputRead)
232*cdf0e10cSrcweir 		pipe( stdOutput);
233*cdf0e10cSrcweir 	if (pErrorRead)
234*cdf0e10cSrcweir 		pipe( stdError);
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	fcntl( stdInput[0], F_SETFD, FD_CLOEXEC);
237*cdf0e10cSrcweir 	fcntl( stdInput[1], F_SETFD, FD_CLOEXEC);
238*cdf0e10cSrcweir 	fcntl( stdOutput[0], F_SETFD, FD_CLOEXEC);
239*cdf0e10cSrcweir 	fcntl( stdOutput[1], F_SETFD, FD_CLOEXEC);
240*cdf0e10cSrcweir 	fcntl( stdError[0], F_SETFD, FD_CLOEXEC);
241*cdf0e10cSrcweir 	fcntl( stdError[1], F_SETFD, FD_CLOEXEC);
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 	saveInput = dup( STDIN_FILENO);
244*cdf0e10cSrcweir 	fcntl( saveInput, F_SETFD, FD_CLOEXEC);
245*cdf0e10cSrcweir 	dup2( stdInput[0], STDIN_FILENO );
246*cdf0e10cSrcweir 	close( stdInput[0] );
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 	saveOutput = dup( STDOUT_FILENO);
249*cdf0e10cSrcweir 	fcntl( saveOutput, F_SETFD, FD_CLOEXEC);
250*cdf0e10cSrcweir 	dup2( stdOutput[1], STDOUT_FILENO );
251*cdf0e10cSrcweir 	close( stdOutput[1] );
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 	saveError = dup( STDERR_FILENO);
254*cdf0e10cSrcweir 	fcntl( saveError, F_SETFD, FD_CLOEXEC);
255*cdf0e10cSrcweir 	dup2( stdError[1], STDERR_FILENO );
256*cdf0e10cSrcweir 	close( stdError[1] );
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir     Error = osl_psz_executeProcess(szImagePath,
259*cdf0e10cSrcweir                                    pArguments,
260*cdf0e10cSrcweir                                    Options,
261*cdf0e10cSrcweir                                    Security,
262*cdf0e10cSrcweir                                    pszWorkDir,
263*cdf0e10cSrcweir                                    pEnvironment,
264*cdf0e10cSrcweir                                    pProcess,
265*cdf0e10cSrcweir 								   pInputWrite,
266*cdf0e10cSrcweir 								   pOutputRead,
267*cdf0e10cSrcweir 								   pErrorRead
268*cdf0e10cSrcweir 								   );
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 	if ( pInputWrite )
271*cdf0e10cSrcweir 		*(pInputWrite) = osl_createFileHandleFromFD( stdInput[1] );
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 	if ( pOutputRead )
274*cdf0e10cSrcweir 		*(pOutputRead) = osl_createFileHandleFromFD( stdOutput[0] );
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 	if ( pErrorRead )
277*cdf0e10cSrcweir 		*(pErrorRead) = osl_createFileHandleFromFD( stdError[0] );
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir 	// restore handles
280*cdf0e10cSrcweir 	dup2( saveInput, STDIN_FILENO);
281*cdf0e10cSrcweir 	close( saveInput);
282*cdf0e10cSrcweir 	dup2( saveOutput, STDOUT_FILENO);
283*cdf0e10cSrcweir 	close( saveOutput);
284*cdf0e10cSrcweir 	dup2( saveError, STDERR_FILENO);
285*cdf0e10cSrcweir 	close( saveError);
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir     if ( pArguments != 0 )
288*cdf0e10cSrcweir     {
289*cdf0e10cSrcweir         for ( index = 0 ; index < nArguments ; ++index )
290*cdf0e10cSrcweir         {
291*cdf0e10cSrcweir             if ( pArguments[index] != 0 )
292*cdf0e10cSrcweir             {
293*cdf0e10cSrcweir                 free(pArguments[index]);
294*cdf0e10cSrcweir             }
295*cdf0e10cSrcweir         }
296*cdf0e10cSrcweir         free(pArguments);
297*cdf0e10cSrcweir     }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir     if ( pEnvironment != 0 )
300*cdf0e10cSrcweir     {
301*cdf0e10cSrcweir         for ( index = 0 ; index < nEnvironmentVars ; ++index )
302*cdf0e10cSrcweir         {
303*cdf0e10cSrcweir             if ( pEnvironment[index] != 0 )
304*cdf0e10cSrcweir             {
305*cdf0e10cSrcweir                 free(pEnvironment[index]);
306*cdf0e10cSrcweir             }
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir         free(pEnvironment);
309*cdf0e10cSrcweir     }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir     return Error;
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir /**********************************************
315*cdf0e10cSrcweir  osl_executeProcess
316*cdf0e10cSrcweir  *********************************************/
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir oslProcessError SAL_CALL osl_executeProcess(
319*cdf0e10cSrcweir 											rtl_uString *ustrImageName,
320*cdf0e10cSrcweir 											rtl_uString *ustrArguments[],
321*cdf0e10cSrcweir                                             sal_uInt32   nArguments,
322*cdf0e10cSrcweir 											oslProcessOption Options,
323*cdf0e10cSrcweir 											oslSecurity Security,
324*cdf0e10cSrcweir 											rtl_uString *ustrWorkDir,
325*cdf0e10cSrcweir 											rtl_uString *ustrEnvironment[],
326*cdf0e10cSrcweir                                             sal_uInt32   nEnvironmentVars,
327*cdf0e10cSrcweir 											oslProcess *pProcess
328*cdf0e10cSrcweir 											)
329*cdf0e10cSrcweir {
330*cdf0e10cSrcweir 	return osl_executeProcess_WithRedirectedIO(
331*cdf0e10cSrcweir 		ustrImageName,
332*cdf0e10cSrcweir 		ustrArguments,
333*cdf0e10cSrcweir 		nArguments,
334*cdf0e10cSrcweir 		Options,
335*cdf0e10cSrcweir 		Security,
336*cdf0e10cSrcweir 		ustrWorkDir,
337*cdf0e10cSrcweir 		ustrEnvironment,
338*cdf0e10cSrcweir 		nEnvironmentVars,
339*cdf0e10cSrcweir 		pProcess,
340*cdf0e10cSrcweir 		NULL,
341*cdf0e10cSrcweir 		NULL,
342*cdf0e10cSrcweir 		NULL
343*cdf0e10cSrcweir 		);
344*cdf0e10cSrcweir }
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir /**********************************************
347*cdf0e10cSrcweir  osl_psz_executeProcess
348*cdf0e10cSrcweir  *********************************************/
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir oslProcessError SAL_CALL osl_psz_executeProcess(sal_Char *pszImageName,
351*cdf0e10cSrcweir                                                 sal_Char *pszArguments[],
352*cdf0e10cSrcweir                                                 oslProcessOption Options,
353*cdf0e10cSrcweir                                                 oslSecurity Security,
354*cdf0e10cSrcweir                                                 sal_Char *pszDirectory,
355*cdf0e10cSrcweir                                                 sal_Char *pszEnvironments[],
356*cdf0e10cSrcweir                                                 oslProcess *pProcess,
357*cdf0e10cSrcweir 												oslFileHandle	*pInputWrite,
358*cdf0e10cSrcweir 												oslFileHandle	*pOutputRead,
359*cdf0e10cSrcweir 												oslFileHandle	*pErrorRead
360*cdf0e10cSrcweir 												)
361*cdf0e10cSrcweir {
362*cdf0e10cSrcweir 	ULONG ulSessID	= 0;		  /* Session ID returned		  */
363*cdf0e10cSrcweir 	PID pidProcess;
364*cdf0e10cSrcweir 	APIRET rc;
365*cdf0e10cSrcweir 	sal_Char* pStr;
366*cdf0e10cSrcweir 	sal_Char*		 args;
367*cdf0e10cSrcweir 	sal_Char*		 envs;
368*cdf0e10cSrcweir 	int i;
369*cdf0e10cSrcweir 	int n = 1;
370*cdf0e10cSrcweir 	oslProcessImpl* pProcImpl;
371*cdf0e10cSrcweir     ULONG nAppType, nOwnAppType;
372*cdf0e10cSrcweir     ULONG nCurrentDisk, nDriveMap, nBufSize;
373*cdf0e10cSrcweir    	int	 first = 0;
374*cdf0e10cSrcweir     sal_Char path[ _MAX_PATH ];
375*cdf0e10cSrcweir     sal_Char currentDir[ _MAX_PATH ];
376*cdf0e10cSrcweir     sal_Char ownfilename[ _MAX_PATH ];
377*cdf0e10cSrcweir 	RESULTCODES resultCode;
378*cdf0e10cSrcweir 	char** p;
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 	/* get imagename from arg list, if not specified */
381*cdf0e10cSrcweir 	if (pszImageName == NULL)
382*cdf0e10cSrcweir 		pszImageName = pszArguments[first++];
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir 	OSL_ASSERT(pszImageName != NULL);
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir 	/* check application type */
387*cdf0e10cSrcweir 	rc = DosQueryAppType( (PCSZ) pszImageName, &nAppType );
388*cdf0e10cSrcweir 	if( rc != NO_ERROR )
389*cdf0e10cSrcweir 	{
390*cdf0e10cSrcweir 		if( (rc == ERROR_FILE_NOT_FOUND) || (rc == ERROR_PATH_NOT_FOUND) )
391*cdf0e10cSrcweir 			return osl_Process_E_NotFound;
392*cdf0e10cSrcweir 		else
393*cdf0e10cSrcweir 			return osl_Process_E_Unknown;
394*cdf0e10cSrcweir     }
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir     /* backup current disk information */
397*cdf0e10cSrcweir     if(DosQueryCurrentDisk(&nCurrentDisk, &nDriveMap))
398*cdf0e10cSrcweir     {
399*cdf0e10cSrcweir         nCurrentDisk = 0;
400*cdf0e10cSrcweir     }
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir     /* backup current directory information */
403*cdf0e10cSrcweir     nBufSize = _MAX_PATH;
404*cdf0e10cSrcweir     if(DosQueryCurrentDir(0, (BYTE*)currentDir, &nBufSize))
405*cdf0e10cSrcweir     {
406*cdf0e10cSrcweir         *currentDir = '\0';
407*cdf0e10cSrcweir     }
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir     /* change to working directory */
410*cdf0e10cSrcweir     if(pszDirectory && pszDirectory[1] == ':')
411*cdf0e10cSrcweir     {
412*cdf0e10cSrcweir         BYTE nDrive = toupper(pszDirectory[0]) - 'A' + 1;
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir         if(NO_ERROR == DosSetDefaultDisk(nDrive))
415*cdf0e10cSrcweir         {
416*cdf0e10cSrcweir             DosSetCurrentDir((PSZ) pszDirectory);
417*cdf0e10cSrcweir         }
418*cdf0e10cSrcweir     }
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir 	/* query current executable filename and application type */
421*cdf0e10cSrcweir 	{
422*cdf0e10cSrcweir 		CHAR    szName[CCHMAXPATH];
423*cdf0e10cSrcweir 		PPIB    ppib;
424*cdf0e10cSrcweir 		PTIB    ptib;
425*cdf0e10cSrcweir 		APIRET	rc;
426*cdf0e10cSrcweir 		rc = DosGetInfoBlocks(&ptib, &ppib);
427*cdf0e10cSrcweir 		rc = DosQueryModuleName(ppib->pib_hmte, sizeof(szName), szName);
428*cdf0e10cSrcweir 		DosQueryAppType( (PCSZ)szName, &nOwnAppType );
429*cdf0e10cSrcweir 	}
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir 	/* combination of flags WAIT and DETACHED not supported */
432*cdf0e10cSrcweir 	if( (Options & osl_Process_DETACHED) && (Options & osl_Process_WAIT) )
433*cdf0e10cSrcweir 		Options &= !osl_Process_DETACHED;
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir 	/* start in same session if possible and detached flag not set */
436*cdf0e10cSrcweir 	if( ((nAppType & 0x00000007) == (nOwnAppType & 0x00000007))
437*cdf0e10cSrcweir /*	    && ((Options & osl_Process_DETACHED) == 0) */ )
438*cdf0e10cSrcweir 	{
439*cdf0e10cSrcweir 		CHAR szbuf[CCHMAXPATH];
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir 		/* calculate needed space for arguments */
442*cdf0e10cSrcweir 		n = strlen( pszImageName ) + 1;
443*cdf0e10cSrcweir 		if( pszArguments )
444*cdf0e10cSrcweir 		   	for (i = first; pszArguments[i] != NULL; i++)
445*cdf0e10cSrcweir 			   	n += strlen(pszArguments[i]) + 1;
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir 		/* allocate space for arguments */
448*cdf0e10cSrcweir 		args = (sal_Char*)malloc(n + 1);
449*cdf0e10cSrcweir 		pStr = args;
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir 		/* add program name as first string to arguments */
452*cdf0e10cSrcweir 		memcpy(pStr, pszImageName, strlen( pszImageName ) );
453*cdf0e10cSrcweir 		pStr += strlen( pszImageName );
454*cdf0e10cSrcweir 		*pStr++ = '\0';
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir 		/* add given strings to arguments */
457*cdf0e10cSrcweir 		if( pszArguments )
458*cdf0e10cSrcweir 			for (i = first; pszArguments[i] != NULL; i++)
459*cdf0e10cSrcweir 			{
460*cdf0e10cSrcweir 				memcpy(pStr, pszArguments[i], strlen( pszArguments[i] ) );
461*cdf0e10cSrcweir 				pStr += strlen( pszArguments[i] );
462*cdf0e10cSrcweir 				if (pszArguments[i+1] != NULL)
463*cdf0e10cSrcweir 					*pStr++ = ' ';
464*cdf0e10cSrcweir 			}
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir 		/* set end marker for arguments */
467*cdf0e10cSrcweir 		*pStr++ = '\0';
468*cdf0e10cSrcweir 		*pStr = '\0';
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir 		OSL_TRACE( "osl_executeProcess with DosExecPgm (args: %s)\n", args );
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir 		/* calculate needed space for environment: since enviroment var search
473*cdf0e10cSrcweir 		   is a linear scan of the current enviroment, we place new variables
474*cdf0e10cSrcweir 		   before existing ones; so the child will find new definitions before
475*cdf0e10cSrcweir 		   olders; this doesn't require us to replace existing vars */
476*cdf0e10cSrcweir 		// existing enviroment size
477*cdf0e10cSrcweir 		n = 0;
478*cdf0e10cSrcweir 		p = environ;
479*cdf0e10cSrcweir 		while( *p)
480*cdf0e10cSrcweir 		{
481*cdf0e10cSrcweir 			int l = strlen( *p);
482*cdf0e10cSrcweir 			n += l + 1;
483*cdf0e10cSrcweir 			p++;
484*cdf0e10cSrcweir 		}
485*cdf0e10cSrcweir 		// new env size (if exists)
486*cdf0e10cSrcweir 		if( pszEnvironments )
487*cdf0e10cSrcweir 		{
488*cdf0e10cSrcweir 			for (i = 0; pszEnvironments[i] != NULL; i++)
489*cdf0e10cSrcweir 			   	n += strlen(pszEnvironments[i]) + 1;
490*cdf0e10cSrcweir 		}
491*cdf0e10cSrcweir 		/* allocate space for environment */
492*cdf0e10cSrcweir 		envs = (sal_Char*)malloc(n + 1);
493*cdf0e10cSrcweir 		pStr = envs;
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir 		// add new vars
496*cdf0e10cSrcweir 		if( pszEnvironments )
497*cdf0e10cSrcweir 		{
498*cdf0e10cSrcweir 			/* add given strings to environment */
499*cdf0e10cSrcweir 			for (i = 0; pszEnvironments[i] != NULL; i++)
500*cdf0e10cSrcweir 			{
501*cdf0e10cSrcweir 				memcpy(pStr, pszEnvironments[i], strlen( pszEnvironments[i] ) );
502*cdf0e10cSrcweir 				pStr += strlen( pszEnvironments[i] );
503*cdf0e10cSrcweir 				*pStr++ = '\0';
504*cdf0e10cSrcweir 			}
505*cdf0e10cSrcweir 		}
506*cdf0e10cSrcweir 		// add existing vars
507*cdf0e10cSrcweir 		p = environ;
508*cdf0e10cSrcweir 		while( *p)
509*cdf0e10cSrcweir 		{
510*cdf0e10cSrcweir 			memcpy(pStr, *p, strlen( *p ) );
511*cdf0e10cSrcweir 			pStr += strlen( *p );
512*cdf0e10cSrcweir 			*pStr++ = '\0';
513*cdf0e10cSrcweir 			p++;
514*cdf0e10cSrcweir 		}
515*cdf0e10cSrcweir 		/* set end marker for environment */
516*cdf0e10cSrcweir 		*pStr = '\0';
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir 		if(Options & osl_Process_DETACHED)
520*cdf0e10cSrcweir 		{
521*cdf0e10cSrcweir 			rc = DosExecPgm( szbuf, sizeof( szbuf ), EXEC_BACKGROUND,
522*cdf0e10cSrcweir 							 (PSZ) args, (PSZ) envs, &resultCode, (PSZ) pszImageName );
523*cdf0e10cSrcweir 		}
524*cdf0e10cSrcweir 		else
525*cdf0e10cSrcweir 		{
526*cdf0e10cSrcweir 			rc = DosExecPgm( szbuf, sizeof( szbuf ), EXEC_ASYNCRESULT,
527*cdf0e10cSrcweir 							 (PSZ) args, (PSZ) envs, &resultCode, (PSZ) pszImageName );
528*cdf0e10cSrcweir 		}
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir 		pidProcess = resultCode.codeTerminate;
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir 		/* cleanup */
533*cdf0e10cSrcweir 		free(envs);
534*cdf0e10cSrcweir    		free(args);
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir 		/* error handling */
537*cdf0e10cSrcweir 		if( rc != NO_ERROR )
538*cdf0e10cSrcweir 			return osl_Process_E_Unknown;
539*cdf0e10cSrcweir 	}
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir 	else
542*cdf0e10cSrcweir 	{
543*cdf0e10cSrcweir 		STARTDATA SData = { 0 };
544*cdf0e10cSrcweir 		UCHAR achObjBuf[ 256 ] = { 0 };
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir 		/* combine arguments separated by spaces */
547*cdf0e10cSrcweir 		if( pszArguments )
548*cdf0e10cSrcweir 		{
549*cdf0e10cSrcweir 			for (i = first; pszArguments[i] != NULL; i++)
550*cdf0e10cSrcweir 				n += strlen(pszArguments[i]) + 1;
551*cdf0e10cSrcweir 			// YD DosStartSession requires low-mem buffers!
552*cdf0e10cSrcweir 			args = (sal_Char*)_tmalloc(n);
553*cdf0e10cSrcweir 			*args = '\0';
554*cdf0e10cSrcweir 			for (i = first; pszArguments[i] != NULL; i++)
555*cdf0e10cSrcweir 			{
556*cdf0e10cSrcweir 				strcat(args, pszArguments[i]);
557*cdf0e10cSrcweir 				strcat(args, " ");
558*cdf0e10cSrcweir 			}
559*cdf0e10cSrcweir 		}
560*cdf0e10cSrcweir 		else
561*cdf0e10cSrcweir 			args = NULL;
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir 		/* combine environment separated by NULL */
564*cdf0e10cSrcweir 		if( pszEnvironments )
565*cdf0e10cSrcweir 		{
566*cdf0e10cSrcweir 			for (i = 0; pszEnvironments[i] != NULL; i++)
567*cdf0e10cSrcweir 				n += strlen(pszEnvironments[i]) + 1;
568*cdf0e10cSrcweir 			// YD DosStartSession requires low-mem buffers!
569*cdf0e10cSrcweir 			envs = (sal_Char*)_tmalloc(n + 1);
570*cdf0e10cSrcweir 			pStr = (sal_Char*)envs;
571*cdf0e10cSrcweir 			for (i = 0; pszEnvironments[i] != NULL; i++)
572*cdf0e10cSrcweir 			{
573*cdf0e10cSrcweir 				memcpy(pStr, pszEnvironments[i], strlen( pszEnvironments[i] ) );
574*cdf0e10cSrcweir 				pStr += strlen( pszEnvironments[i] );
575*cdf0e10cSrcweir 				*pStr = '\0';
576*cdf0e10cSrcweir 				pStr++;
577*cdf0e10cSrcweir 			}
578*cdf0e10cSrcweir 			*pStr = '\0';
579*cdf0e10cSrcweir 		}
580*cdf0e10cSrcweir 		else
581*cdf0e10cSrcweir 			envs = NULL;
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir 		/* initialize data structure */
584*cdf0e10cSrcweir 		memset( &SData, 0, sizeof( STARTDATA ) );
585*cdf0e10cSrcweir 		SData.Length  = sizeof(STARTDATA);
586*cdf0e10cSrcweir 
587*cdf0e10cSrcweir 		OSL_TRACE( "osl_executeProcess with DosStartSession (args: %s)\n", args );
588*cdf0e10cSrcweir 
589*cdf0e10cSrcweir 		/* OS/2 Application ? */
590*cdf0e10cSrcweir 		if(nAppType & 0x00000007)
591*cdf0e10cSrcweir 		{
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir 			/* inherit options from parent */
594*cdf0e10cSrcweir 			SData.InheritOpt = SSF_INHERTOPT_PARENT;
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir 			switch (Options & (osl_Process_NORMAL | osl_Process_MINIMIZED |
597*cdf0e10cSrcweir 						    osl_Process_MAXIMIZED | osl_Process_FULLSCREEN))
598*cdf0e10cSrcweir 			{
599*cdf0e10cSrcweir 				case osl_Process_MINIMIZED:
600*cdf0e10cSrcweir 					SData.SessionType = SSF_TYPE_DEFAULT;
601*cdf0e10cSrcweir 					SData.PgmControl |= SSF_CONTROL_MINIMIZE;
602*cdf0e10cSrcweir 					break;
603*cdf0e10cSrcweir 
604*cdf0e10cSrcweir 				case osl_Process_MAXIMIZED:
605*cdf0e10cSrcweir 					SData.SessionType = SSF_TYPE_DEFAULT;
606*cdf0e10cSrcweir 					SData.PgmControl |= SSF_CONTROL_MAXIMIZE;
607*cdf0e10cSrcweir 					break;
608*cdf0e10cSrcweir 
609*cdf0e10cSrcweir 				case osl_Process_FULLSCREEN:
610*cdf0e10cSrcweir 					SData.SessionType = SSF_TYPE_FULLSCREEN;
611*cdf0e10cSrcweir 					break;
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir 				default:
614*cdf0e10cSrcweir 					SData.SessionType = SSF_TYPE_DEFAULT;
615*cdf0e10cSrcweir 			} /* switch */
616*cdf0e10cSrcweir 		}
617*cdf0e10cSrcweir 
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir 		if( Options & osl_Process_DETACHED )
620*cdf0e10cSrcweir 		{
621*cdf0e10cSrcweir 			/* start an independent session */
622*cdf0e10cSrcweir 			SData.Related = SSF_RELATED_INDEPENDENT;
623*cdf0e10cSrcweir 			SData.TermQ = NULL;
624*cdf0e10cSrcweir 		}
625*cdf0e10cSrcweir 		else
626*cdf0e10cSrcweir 		{
627*cdf0e10cSrcweir 			/* start a child session and set Termination Queue */
628*cdf0e10cSrcweir 			SData.Related = SSF_RELATED_CHILD;
629*cdf0e10cSrcweir 
630*cdf0e10cSrcweir 			if(! bInitSessionTerm)
631*cdf0e10cSrcweir 				bInitSessionTerm = InitSessionTerm();
632*cdf0e10cSrcweir 
633*cdf0e10cSrcweir 			SData.TermQ = (BYTE*) SessionTermQueueName;
634*cdf0e10cSrcweir 		}
635*cdf0e10cSrcweir 
636*cdf0e10cSrcweir 		SData.FgBg	= SSF_FGBG_FORE;	  /* start session in foreground  */
637*cdf0e10cSrcweir 		SData.TraceOpt = SSF_TRACEOPT_NONE;	  /* No trace				 */
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir 		SData.PgmTitle = NULL;
640*cdf0e10cSrcweir 		SData.PgmInputs = (BYTE*)args;
641*cdf0e10cSrcweir 		SData.PgmName = (PSZ) pszImageName;
642*cdf0e10cSrcweir 		SData.Environment = (BYTE*)envs;
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir 		if( Options & osl_Process_HIDDEN )
645*cdf0e10cSrcweir 			SData.PgmControl |= SSF_CONTROL_INVISIBLE;
646*cdf0e10cSrcweir 		else
647*cdf0e10cSrcweir 			SData.PgmControl |= SSF_CONTROL_VISIBLE;
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir 		SData.ObjectBuffer  = (PSZ) achObjBuf;
650*cdf0e10cSrcweir 		SData.ObjectBuffLen = (ULONG) sizeof(achObjBuf);
651*cdf0e10cSrcweir 
652*cdf0e10cSrcweir 
653*cdf0e10cSrcweir 		/* Start the session */
654*cdf0e10cSrcweir 		rc = DosStartSession( &SData, &ulSessID, &pidProcess );
655*cdf0e10cSrcweir 
656*cdf0e10cSrcweir 		/* ignore error "session started in background" */
657*cdf0e10cSrcweir 		if( rc == ERROR_SMG_START_IN_BACKGROUND )
658*cdf0e10cSrcweir 			rc = NO_ERROR;
659*cdf0e10cSrcweir 
660*cdf0e10cSrcweir 
661*cdf0e10cSrcweir 		if(envs)
662*cdf0e10cSrcweir 			_tfree(envs);
663*cdf0e10cSrcweir 		if(args)
664*cdf0e10cSrcweir 			_tfree(args);
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir 		if( rc != NO_ERROR )
667*cdf0e10cSrcweir 			return osl_Process_E_Unknown;
668*cdf0e10cSrcweir 
669*cdf0e10cSrcweir 	} /* else */
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir     /* restore current disk */
673*cdf0e10cSrcweir     if(nCurrentDisk)
674*cdf0e10cSrcweir     {
675*cdf0e10cSrcweir         DosSetDefaultDisk(nCurrentDisk);
676*cdf0e10cSrcweir     }
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir     /* restore current drive information */
679*cdf0e10cSrcweir     if(*currentDir)
680*cdf0e10cSrcweir     {
681*cdf0e10cSrcweir         DosSetCurrentDir((PCSZ)currentDir);
682*cdf0e10cSrcweir     }
683*cdf0e10cSrcweir 
684*cdf0e10cSrcweir 	/* allocate intern process structure and store child process ID */
685*cdf0e10cSrcweir 	pProcImpl = (oslProcessImpl*)malloc(sizeof(oslProcessImpl));
686*cdf0e10cSrcweir 	pProcImpl->pProcess = pidProcess;
687*cdf0e10cSrcweir 	pProcImpl->nSessionID = ulSessID;
688*cdf0e10cSrcweir 
689*cdf0e10cSrcweir    	pProcImpl->bResultCodeValid = FALSE;
690*cdf0e10cSrcweir 
691*cdf0e10cSrcweir 	if( Options & osl_Process_WAIT )
692*cdf0e10cSrcweir 		osl_joinProcess(pProcImpl);
693*cdf0e10cSrcweir 
694*cdf0e10cSrcweir 	*pProcess = (oslProcess)pProcImpl;
695*cdf0e10cSrcweir 
696*cdf0e10cSrcweir 	if( rc == NO_ERROR )
697*cdf0e10cSrcweir 		return osl_Process_E_None;
698*cdf0e10cSrcweir 	else
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir 		return osl_Process_E_Unknown;
701*cdf0e10cSrcweir }
702*cdf0e10cSrcweir 
703*cdf0e10cSrcweir /*----------------------------------------------------------------------------*/
704*cdf0e10cSrcweir 
705*cdf0e10cSrcweir oslProcessError SAL_CALL osl_terminateProcess(oslProcess Process)
706*cdf0e10cSrcweir {
707*cdf0e10cSrcweir 	if (Process == NULL)
708*cdf0e10cSrcweir 		return osl_Process_E_Unknown;
709*cdf0e10cSrcweir 
710*cdf0e10cSrcweir 	/* Stop the session */
711*cdf0e10cSrcweir 	DosStopSession( STOP_SESSION_SPECIFIED, ((oslProcessImpl*)Process)->nSessionID );
712*cdf0e10cSrcweir 
713*cdf0e10cSrcweir 	return osl_Process_E_None;
714*cdf0e10cSrcweir }
715*cdf0e10cSrcweir 
716*cdf0e10cSrcweir /*----------------------------------------------------------------------------*/
717*cdf0e10cSrcweir 
718*cdf0e10cSrcweir oslProcess SAL_CALL osl_getProcess(oslProcessIdentifier Ident)
719*cdf0e10cSrcweir {
720*cdf0e10cSrcweir 	HANDLE		  hProcess;
721*cdf0e10cSrcweir 	oslProcessImpl* pProcImpl;
722*cdf0e10cSrcweir 
723*cdf0e10cSrcweir 	/* check, if given PID is a valid process */
724*cdf0e10cSrcweir 	if (FALSE)
725*cdf0e10cSrcweir 	{
726*cdf0e10cSrcweir 		pProcImpl = (oslProcessImpl*)malloc(sizeof(oslProcessImpl));
727*cdf0e10cSrcweir /*
728*cdf0e10cSrcweir 		pProcImpl->pProcess = pidProcess;
729*cdf0e10cSrcweir 		pProcImpl->nSessionID = ulSessID;
730*cdf0e10cSrcweir */
731*cdf0e10cSrcweir 	}
732*cdf0e10cSrcweir 	else
733*cdf0e10cSrcweir 		pProcImpl = NULL;
734*cdf0e10cSrcweir 
735*cdf0e10cSrcweir 	return (pProcImpl);
736*cdf0e10cSrcweir }
737*cdf0e10cSrcweir 
738*cdf0e10cSrcweir /*----------------------------------------------------------------------------*/
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir void SAL_CALL osl_freeProcessHandle(oslProcess Process)
741*cdf0e10cSrcweir {
742*cdf0e10cSrcweir 	/* free intern process structure */
743*cdf0e10cSrcweir 	if (Process != NULL)
744*cdf0e10cSrcweir 		free((oslProcessImpl*)Process);
745*cdf0e10cSrcweir }
746*cdf0e10cSrcweir 
747*cdf0e10cSrcweir /*----------------------------------------------------------------------------*/
748*cdf0e10cSrcweir 
749*cdf0e10cSrcweir oslProcessError SAL_CALL osl_joinProcess(oslProcess Process)
750*cdf0e10cSrcweir {
751*cdf0e10cSrcweir 	oslProcessImpl* pProcImpl = (oslProcessImpl*) Process;
752*cdf0e10cSrcweir 	APIRET rc;
753*cdf0e10cSrcweir 
754*cdf0e10cSrcweir 	if (Process == NULL)
755*cdf0e10cSrcweir 		return osl_Process_E_Unknown;
756*cdf0e10cSrcweir 
757*cdf0e10cSrcweir 	/* process of same session ? */
758*cdf0e10cSrcweir 	if( pProcImpl->nSessionID == 0 )
759*cdf0e10cSrcweir 	{
760*cdf0e10cSrcweir 		RESULTCODES resultCode;
761*cdf0e10cSrcweir 		PID pidEnded;
762*cdf0e10cSrcweir 
763*cdf0e10cSrcweir 		rc = DosWaitChild( DCWA_PROCESS, DCWW_WAIT, &resultCode,
764*cdf0e10cSrcweir 		        &pidEnded, pProcImpl->pProcess );
765*cdf0e10cSrcweir 
766*cdf0e10cSrcweir 		if( rc == NO_ERROR )
767*cdf0e10cSrcweir 		{
768*cdf0e10cSrcweir 			pProcImpl->nResultCode = resultCode.codeResult;
769*cdf0e10cSrcweir 			pProcImpl->bResultCodeValid = TRUE;
770*cdf0e10cSrcweir 
771*cdf0e10cSrcweir 			return osl_Process_E_None;
772*cdf0e10cSrcweir 		}
773*cdf0e10cSrcweir 	}
774*cdf0e10cSrcweir 	else
775*cdf0e10cSrcweir 	{
776*cdf0e10cSrcweir 		ULONG pcbData, ulElement = 0;
777*cdf0e10cSrcweir 		REQUESTDATA rdData;
778*cdf0e10cSrcweir 		BYTE bPriority;
779*cdf0e10cSrcweir 		struct {
780*cdf0e10cSrcweir 			USHORT SessionID;
781*cdf0e10cSrcweir 			USHORT ReturnValue;
782*cdf0e10cSrcweir 		} *pvBuffer;
783*cdf0e10cSrcweir 
784*cdf0e10cSrcweir 		/* search/wait for the correct entry in termination queue */
785*cdf0e10cSrcweir 		while( ( rc = DosPeekQueue( SessionTermQueue, &rdData, &pcbData,
786*cdf0e10cSrcweir 			            (PPVOID) &pvBuffer, &ulElement, DCWW_WAIT,
787*cdf0e10cSrcweir 			            &bPriority, NULLHANDLE )) == NO_ERROR )
788*cdf0e10cSrcweir 		{
789*cdf0e10cSrcweir 
790*cdf0e10cSrcweir 			if( pvBuffer->SessionID == pProcImpl->nSessionID )
791*cdf0e10cSrcweir 			{
792*cdf0e10cSrcweir 				pProcImpl->nResultCode = pvBuffer->ReturnValue;
793*cdf0e10cSrcweir 				pProcImpl->bResultCodeValid = TRUE;
794*cdf0e10cSrcweir 
795*cdf0e10cSrcweir 				/* remove item from queue */
796*cdf0e10cSrcweir 				rc = DosReadQueue( SessionTermQueue, &rdData, &pcbData,
797*cdf0e10cSrcweir 				       (PPVOID)&pvBuffer, ulElement, DCWW_WAIT,
798*cdf0e10cSrcweir 					   &bPriority, NULLHANDLE );
799*cdf0e10cSrcweir 
800*cdf0e10cSrcweir 				if( rc == NO_ERROR )
801*cdf0e10cSrcweir 					return osl_Process_E_None;
802*cdf0e10cSrcweir 				else
803*cdf0e10cSrcweir 					return osl_Process_E_Unknown;
804*cdf0e10cSrcweir 			}
805*cdf0e10cSrcweir 		} /* while */
806*cdf0e10cSrcweir 	}
807*cdf0e10cSrcweir 	return osl_Process_E_Unknown;
808*cdf0e10cSrcweir }
809*cdf0e10cSrcweir 
810*cdf0e10cSrcweir /***************************************************************************/
811*cdf0e10cSrcweir 
812*cdf0e10cSrcweir //YD FIXME incomplete!
813*cdf0e10cSrcweir oslProcessError SAL_CALL osl_joinProcessWithTimeout(oslProcess Process, const TimeValue* pTimeout)
814*cdf0e10cSrcweir {
815*cdf0e10cSrcweir 	return osl_joinProcess( Process);
816*cdf0e10cSrcweir }
817*cdf0e10cSrcweir 
818*cdf0e10cSrcweir /*----------------------------------------------------------------------------*/
819*cdf0e10cSrcweir 
820*cdf0e10cSrcweir oslProcessError SAL_CALL osl_getCommandArgs( sal_Char* pszBuffer, sal_uInt32 Max)
821*cdf0e10cSrcweir {
822*cdf0e10cSrcweir 
823*cdf0e10cSrcweir 	static int  CmdLen = -1;
824*cdf0e10cSrcweir 	static sal_Char CmdLine[_MAX_CMD];
825*cdf0e10cSrcweir 
826*cdf0e10cSrcweir 	OSL_ASSERT(pszBuffer);
827*cdf0e10cSrcweir 	OSL_ASSERT(Max > 1);
828*cdf0e10cSrcweir 
829*cdf0e10cSrcweir 	/* Query commandline during first call of function only */
830*cdf0e10cSrcweir 	if (CmdLen < 0)
831*cdf0e10cSrcweir 	{
832*cdf0e10cSrcweir 		sal_Bool bEscaped = sal_False;
833*cdf0e10cSrcweir 		sal_Bool bSeparated = sal_True;
834*cdf0e10cSrcweir 		sal_Char* pszBufferOrg = pszBuffer;
835*cdf0e10cSrcweir 		sal_Char* pszCmdLine;
836*cdf0e10cSrcweir 
837*cdf0e10cSrcweir 		/* get pointer to commandline */
838*cdf0e10cSrcweir 		{
839*cdf0e10cSrcweir 			PTIB pptib = NULL;
840*cdf0e10cSrcweir 			PPIB pppib = NULL;
841*cdf0e10cSrcweir 
842*cdf0e10cSrcweir 			DosGetInfoBlocks(&pptib, &pppib);
843*cdf0e10cSrcweir 			pszCmdLine = pppib->pib_pchcmd;
844*cdf0e10cSrcweir 		}
845*cdf0e10cSrcweir 
846*cdf0e10cSrcweir 		/* skip first string */
847*cdf0e10cSrcweir 		while( *pszCmdLine )
848*cdf0e10cSrcweir 			pszCmdLine++;
849*cdf0e10cSrcweir 
850*cdf0e10cSrcweir 		/* concatenate commandline arguments for the given string */
851*cdf0e10cSrcweir 		Max -= 2;
852*cdf0e10cSrcweir 		while ( !((*pszCmdLine == '\0') && (*(pszCmdLine + 1) == '\0')) && (Max > 0))
853*cdf0e10cSrcweir         {
854*cdf0e10cSrcweir             /*
855*cdf0e10cSrcweir              *  C-Runtime expects char to be unsigned and so to be
856*cdf0e10cSrcweir              *  preceeded with 00 instead of FF when converting to int
857*cdf0e10cSrcweir              */
858*cdf0e10cSrcweir             int n = *((unsigned char *) pszCmdLine);
859*cdf0e10cSrcweir 			if (! (isspace(n) || (*pszCmdLine == '\0')) )
860*cdf0e10cSrcweir 			{
861*cdf0e10cSrcweir 				if (*pszCmdLine == '"')
862*cdf0e10cSrcweir 				{
863*cdf0e10cSrcweir 					if (*(pszCmdLine + 1) != '"')
864*cdf0e10cSrcweir 						bEscaped = ! bEscaped;
865*cdf0e10cSrcweir 					else
866*cdf0e10cSrcweir 					{
867*cdf0e10cSrcweir 						pszCmdLine++;
868*cdf0e10cSrcweir 						*pszBuffer++ = *pszCmdLine;
869*cdf0e10cSrcweir 						Max--;
870*cdf0e10cSrcweir 					}
871*cdf0e10cSrcweir 				}
872*cdf0e10cSrcweir 				else
873*cdf0e10cSrcweir 				{
874*cdf0e10cSrcweir 					*pszBuffer++ = *pszCmdLine;
875*cdf0e10cSrcweir 					Max--;
876*cdf0e10cSrcweir 				}
877*cdf0e10cSrcweir 				bSeparated = sal_False;
878*cdf0e10cSrcweir 			}
879*cdf0e10cSrcweir 			else
880*cdf0e10cSrcweir 			{
881*cdf0e10cSrcweir 				if (bEscaped)
882*cdf0e10cSrcweir 					*pszBuffer++ = *pszCmdLine;
883*cdf0e10cSrcweir 				else
884*cdf0e10cSrcweir 					if (! bSeparated)
885*cdf0e10cSrcweir 					{
886*cdf0e10cSrcweir 						*pszBuffer++ = '\0';
887*cdf0e10cSrcweir 						bSeparated = sal_True;
888*cdf0e10cSrcweir 					}
889*cdf0e10cSrcweir 				Max--;
890*cdf0e10cSrcweir 			}
891*cdf0e10cSrcweir 
892*cdf0e10cSrcweir 			pszCmdLine++;
893*cdf0e10cSrcweir 		}
894*cdf0e10cSrcweir 
895*cdf0e10cSrcweir 		*pszBuffer++ = '\0';
896*cdf0e10cSrcweir 		*pszBuffer++ = '\0';
897*cdf0e10cSrcweir 
898*cdf0e10cSrcweir 		/* restore pointer and save commandline for next query */
899*cdf0e10cSrcweir 		CmdLen = pszBuffer - pszBufferOrg;
900*cdf0e10cSrcweir 		pszBuffer = pszBufferOrg;
901*cdf0e10cSrcweir 		memcpy( CmdLine, pszBuffer, CmdLen );
902*cdf0e10cSrcweir 	}
903*cdf0e10cSrcweir 	else
904*cdf0e10cSrcweir 	   memcpy( pszBuffer, CmdLine, CmdLen );
905*cdf0e10cSrcweir 
906*cdf0e10cSrcweir 	OSL_TRACE( "osl_getCommandArgs (args: %s)\n", pszBuffer );
907*cdf0e10cSrcweir 
908*cdf0e10cSrcweir 	return osl_Process_E_None;
909*cdf0e10cSrcweir }
910*cdf0e10cSrcweir 
911*cdf0e10cSrcweir /*----------------------------------------------------------------------------*/
912*cdf0e10cSrcweir 
913*cdf0e10cSrcweir oslProcessError SAL_CALL osl_getProcessInfo(oslProcess Process, oslProcessData Fields,
914*cdf0e10cSrcweir 								   oslProcessInfo* pInfo)
915*cdf0e10cSrcweir {
916*cdf0e10cSrcweir 	if (! pInfo || (pInfo->Size != sizeof(oslProcessInfo)))
917*cdf0e10cSrcweir 		return osl_Process_E_Unknown;
918*cdf0e10cSrcweir 
919*cdf0e10cSrcweir 	pInfo->Fields = 0;
920*cdf0e10cSrcweir 
921*cdf0e10cSrcweir 	if (Fields & osl_Process_IDENTIFIER)
922*cdf0e10cSrcweir 	{
923*cdf0e10cSrcweir 		if( Process == NULL )
924*cdf0e10cSrcweir 		{
925*cdf0e10cSrcweir 			PTIB pptib = NULL;
926*cdf0e10cSrcweir 			PPIB pppib = NULL;
927*cdf0e10cSrcweir 
928*cdf0e10cSrcweir 			DosGetInfoBlocks( &pptib, &pppib );
929*cdf0e10cSrcweir 			pInfo->Ident = pppib->pib_ulpid;
930*cdf0e10cSrcweir 		}
931*cdf0e10cSrcweir 		else
932*cdf0e10cSrcweir 			pInfo->Ident = ((oslProcessImpl*)Process)->pProcess;
933*cdf0e10cSrcweir 
934*cdf0e10cSrcweir 		pInfo->Fields |= osl_Process_IDENTIFIER;
935*cdf0e10cSrcweir 	}
936*cdf0e10cSrcweir 
937*cdf0e10cSrcweir 	if (Fields & osl_Process_EXITCODE)
938*cdf0e10cSrcweir 	{
939*cdf0e10cSrcweir 		oslProcessImpl* pProcImpl = (oslProcessImpl*) Process;
940*cdf0e10cSrcweir 
941*cdf0e10cSrcweir 		if( pProcImpl->bResultCodeValid )
942*cdf0e10cSrcweir 		{
943*cdf0e10cSrcweir 			pInfo->Code = pProcImpl->nResultCode;
944*cdf0e10cSrcweir 			pInfo->Fields |= osl_Process_EXITCODE;
945*cdf0e10cSrcweir 		}
946*cdf0e10cSrcweir 		else
947*cdf0e10cSrcweir 		{
948*cdf0e10cSrcweir 			APIRET rc;
949*cdf0e10cSrcweir 
950*cdf0e10cSrcweir 			if( pProcImpl->nSessionID == 0 )
951*cdf0e10cSrcweir 			{
952*cdf0e10cSrcweir 				RESULTCODES resultCode;
953*cdf0e10cSrcweir 				PID pidEnded;
954*cdf0e10cSrcweir 
955*cdf0e10cSrcweir 				rc = DosWaitChild( DCWA_PROCESS, DCWW_WAIT, &resultCode,
956*cdf0e10cSrcweir 				        &pidEnded, pProcImpl->pProcess );
957*cdf0e10cSrcweir 
958*cdf0e10cSrcweir 				if( rc == NO_ERROR )
959*cdf0e10cSrcweir 				{
960*cdf0e10cSrcweir 					pProcImpl->nResultCode = resultCode.codeResult;
961*cdf0e10cSrcweir 					pProcImpl->bResultCodeValid = TRUE;
962*cdf0e10cSrcweir 
963*cdf0e10cSrcweir 					pInfo->Code = pProcImpl->nResultCode;
964*cdf0e10cSrcweir 					pInfo->Fields |= osl_Process_EXITCODE;
965*cdf0e10cSrcweir 
966*cdf0e10cSrcweir 					return osl_Process_E_None;
967*cdf0e10cSrcweir 				}
968*cdf0e10cSrcweir 			}
969*cdf0e10cSrcweir 			else
970*cdf0e10cSrcweir 			{
971*cdf0e10cSrcweir 				ULONG pcbData, ulElement = 0;
972*cdf0e10cSrcweir 				REQUESTDATA rdData;
973*cdf0e10cSrcweir 				BYTE bPriority;
974*cdf0e10cSrcweir 				struct {
975*cdf0e10cSrcweir 					USHORT SessionID;
976*cdf0e10cSrcweir 					USHORT ReturnValue;
977*cdf0e10cSrcweir 				} *pvBuffer;
978*cdf0e10cSrcweir 
979*cdf0e10cSrcweir 				/* search/wait for the correct entry in termination queue */
980*cdf0e10cSrcweir 				while( ( rc = DosPeekQueue( SessionTermQueue, &rdData, &pcbData,
981*cdf0e10cSrcweir 					            (PPVOID) &pvBuffer, &ulElement, DCWW_WAIT,
982*cdf0e10cSrcweir 					            &bPriority, NULLHANDLE )) == NO_ERROR )
983*cdf0e10cSrcweir 				{
984*cdf0e10cSrcweir 
985*cdf0e10cSrcweir 					if( pvBuffer->SessionID == pProcImpl->nSessionID )
986*cdf0e10cSrcweir 					{
987*cdf0e10cSrcweir 						pProcImpl->nResultCode = pvBuffer->ReturnValue;
988*cdf0e10cSrcweir 						pProcImpl->bResultCodeValid = TRUE;
989*cdf0e10cSrcweir 
990*cdf0e10cSrcweir 						pInfo->Code = pProcImpl->nResultCode;
991*cdf0e10cSrcweir 						pInfo->Fields |= osl_Process_EXITCODE;
992*cdf0e10cSrcweir 
993*cdf0e10cSrcweir 						/* remove item from queue */
994*cdf0e10cSrcweir 						rc = DosReadQueue( SessionTermQueue, &rdData, &pcbData,
995*cdf0e10cSrcweir 					   	    (PPVOID)&pvBuffer, ulElement, DCWW_WAIT,
996*cdf0e10cSrcweir 							   &bPriority, NULLHANDLE );
997*cdf0e10cSrcweir 
998*cdf0e10cSrcweir 						break;
999*cdf0e10cSrcweir 					}
1000*cdf0e10cSrcweir 				}
1001*cdf0e10cSrcweir 			}
1002*cdf0e10cSrcweir 		}
1003*cdf0e10cSrcweir 	}
1004*cdf0e10cSrcweir 
1005*cdf0e10cSrcweir 	if (Fields & osl_Process_HEAPUSAGE)
1006*cdf0e10cSrcweir 	{
1007*cdf0e10cSrcweir 	}
1008*cdf0e10cSrcweir 	if (Fields & osl_Process_CPUTIMES)
1009*cdf0e10cSrcweir 	{
1010*cdf0e10cSrcweir 	}
1011*cdf0e10cSrcweir 
1012*cdf0e10cSrcweir 	return (pInfo->Fields == Fields) ? osl_Process_E_None : osl_Process_E_Unknown;
1013*cdf0e10cSrcweir }
1014