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