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