Transmit current directory settings to newly created processes.
[wine] / scheduler / process.c
1 /*
2  * Win32 processes
3  *
4  * Copyright 1996, 1998 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
17 #include "process.h"
18 #include "main.h"
19 #include "module.h"
20 #include "neexe.h"
21 #include "file.h"
22 #include "global.h"
23 #include "heap.h"
24 #include "task.h"
25 #include "ldt.h"
26 #include "syslevel.h"
27 #include "thread.h"
28 #include "winerror.h"
29 #include "pe_image.h"
30 #include "server.h"
31 #include "options.h"
32 #include "callback.h"
33 #include "debugtools.h"
34
35 DEFAULT_DEBUG_CHANNEL(process);
36 DECLARE_DEBUG_CHANNEL(relay);
37 DECLARE_DEBUG_CHANNEL(win32);
38
39
40 static ENVDB initial_envdb;
41 static STARTUPINFOA initial_startup;
42 static char **main_exe_argv;
43 static char *main_exe_name;
44 static HFILE main_exe_file = -1;
45
46
47 /***********************************************************************
48  *           PROCESS_IdToPDB
49  *
50  * Convert a process id to a PDB, making sure it is valid.
51  */
52 PDB *PROCESS_IdToPDB( DWORD pid )
53 {
54     if (!pid || pid == GetCurrentProcessId()) return PROCESS_Current();
55     return NULL;
56 }
57
58
59 /***********************************************************************
60  *           PROCESS_CallUserSignalProc
61  *
62  * FIXME:  Some of the signals aren't sent correctly!
63  *
64  * The exact meaning of the USER signals is undocumented, but this 
65  * should cover the basic idea:
66  *
67  * USIG_DLL_UNLOAD_WIN16
68  *     This is sent when a 16-bit module is unloaded.
69  *
70  * USIG_DLL_UNLOAD_WIN32
71  *     This is sent when a 32-bit module is unloaded.
72  *
73  * USIG_DLL_UNLOAD_ORPHANS
74  *     This is sent after the last Win3.1 module is unloaded,
75  *     to allow removal of orphaned menus.
76  *
77  * USIG_FAULT_DIALOG_PUSH
78  * USIG_FAULT_DIALOG_POP
79  *     These are called to allow USER to prepare for displaying a
80  *     fault dialog, even though the fault might have happened while
81  *     inside a USER critical section.
82  *
83  * USIG_THREAD_INIT
84  *     This is called from the context of a new thread, as soon as it
85  *     has started to run.
86  *
87  * USIG_THREAD_EXIT
88  *     This is called, still in its context, just before a thread is
89  *     about to terminate.
90  *
91  * USIG_PROCESS_CREATE
92  *     This is called, in the parent process context, after a new process
93  *     has been created.
94  *
95  * USIG_PROCESS_INIT
96  *     This is called in the new process context, just after the main thread
97  *     has started execution (after the main thread's USIG_THREAD_INIT has
98  *     been sent).
99  *
100  * USIG_PROCESS_LOADED
101  *     This is called after the executable file has been loaded into the
102  *     new process context.
103  *
104  * USIG_PROCESS_RUNNING
105  *     This is called immediately before the main entry point is called.
106  *
107  * USIG_PROCESS_EXIT
108  *     This is called in the context of a process that is about to
109  *     terminate (but before the last thread's USIG_THREAD_EXIT has
110  *     been sent).
111  *
112  * USIG_PROCESS_DESTROY
113  *     This is called after a process has terminated.
114  *
115  *
116  * The meaning of the dwFlags bits is as follows:
117  *
118  * USIG_FLAGS_WIN32
119  *     Current process is 32-bit.
120  *
121  * USIG_FLAGS_GUI
122  *     Current process is a (Win32) GUI process.
123  *
124  * USIG_FLAGS_FEEDBACK 
125  *     Current process needs 'feedback' (determined from the STARTUPINFO
126  *     flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
127  *
128  * USIG_FLAGS_FAULT
129  *     The signal is being sent due to a fault.
130  */
131 void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
132 {
133     DWORD flags = PROCESS_Current()->flags;
134     DWORD startup_flags = PROCESS_Current()->env_db->startup_info->dwFlags;
135     DWORD dwFlags = 0;
136
137     /* Determine dwFlags */
138
139     if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
140
141     if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
142
143     if ( dwFlags & USIG_FLAGS_GUI )
144     {
145         /* Feedback defaults to ON */
146         if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
147             dwFlags |= USIG_FLAGS_FEEDBACK;
148     }
149     else
150     {
151         /* Feedback defaults to OFF */
152         if (startup_flags & STARTF_FORCEONFEEDBACK)
153             dwFlags |= USIG_FLAGS_FEEDBACK;
154     }
155
156     /* Convert module handle to 16-bit */
157
158     if ( HIWORD( hModule ) )
159         hModule = MapHModuleLS( hModule );
160
161     /* Call USER signal proc */
162
163     if ( Callout.UserSignalProc )
164     {
165         if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
166             Callout.UserSignalProc( uCode, GetCurrentThreadId(), dwFlags, hModule );
167         else
168             Callout.UserSignalProc( uCode, GetCurrentProcessId(), dwFlags, hModule );
169     }
170 }
171
172
173 /***********************************************************************
174  *           PROCESS_Init
175  */
176 BOOL PROCESS_Init(void)
177 {
178     struct init_process_request *req;
179     PDB *pdb = PROCESS_Current();
180
181     /* Fill the initial process structure */
182     pdb->exit_code              = STILL_ACTIVE;
183     pdb->threads                = 1;
184     pdb->running_threads        = 1;
185     pdb->ring0_threads          = 1;
186     pdb->env_db                 = &initial_envdb;
187     pdb->group                  = pdb;
188     pdb->priority               = 8;  /* Normal */
189     pdb->winver                 = 0xffff; /* to be determined */
190     initial_envdb.startup_info  = &initial_startup;
191
192     /* Setup the server connection */
193     NtCurrentTeb()->socket = CLIENT_InitServer();
194     if (CLIENT_InitThread()) return FALSE;
195
196     /* Retrieve startup info from the server */
197     req = get_req_buffer();
198     req->ldt_copy  = ldt_copy;
199     req->ldt_flags = ldt_flags_copy;
200     req->ppid      = getppid();
201     if (server_call( REQ_INIT_PROCESS )) return FALSE;
202     main_exe_file               = req->exe_file;
203     if (req->filename[0]) main_exe_name = strdup( req->filename );
204     initial_startup.dwFlags     = req->start_flags;
205     initial_startup.wShowWindow = req->cmd_show;
206     initial_envdb.hStdin   = initial_startup.hStdInput  = req->hstdin;
207     initial_envdb.hStdout  = initial_startup.hStdOutput = req->hstdout;
208     initial_envdb.hStderr  = initial_startup.hStdError  = req->hstderr;
209
210     /* Remember TEB selector of initial process for emergency use */
211     SYSLEVEL_EmergencyTeb = NtCurrentTeb()->teb_sel;
212
213     /* Create the system and process heaps */
214     if (!HEAP_CreateSystemHeap()) return FALSE;
215     pdb->heap = HeapCreate( HEAP_GROWABLE, 0, 0 );
216
217     /* Copy the parent environment */
218     if (!ENV_BuildEnvironment()) return FALSE;
219
220     /* Create the SEGPTR heap */
221     if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
222
223     /* Initialize the critical sections */
224     InitializeCriticalSection( &pdb->crit_section );
225     InitializeCriticalSection( &initial_envdb.section );
226
227     /* Initialize syslevel handling */
228     SYSLEVEL_Init();
229
230     return TRUE;
231 }
232
233
234 /***********************************************************************
235  *           load_system_dlls
236  *
237  * Load system DLLs into the initial process (and initialize them)
238  */
239 static int load_system_dlls(void)
240 {
241     char driver[MAX_PATH];
242
243     PROFILE_GetWineIniString( "Wine", "GraphicsDriver", "x11drv", driver, sizeof(driver) );
244     if (!LoadLibraryA( driver ))
245     {
246         MESSAGE( "Could not load graphics driver '%s'\n", driver );
247         return 0;
248     }
249
250     if (!LoadLibraryA("USER32.DLL")) return 0;
251
252     /* Get pointers to USER routines called by KERNEL */
253     THUNK_InitCallout();
254
255     /* Call FinalUserInit routine */
256     Callout.FinalUserInit16();
257
258     /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
259      *       context of the parent process.  Actually, the USER signal proc
260      *       doesn't really care about that, but it *does* require that the
261      *       startup parameters are correctly set up, so that GetProcessDword
262      *       works.  Furthermore, before calling the USER signal proc the 
263      *       16-bit stack must be set up, which it is only after TASK_Create
264      *       in the case of a 16-bit process. Thus, we send the signal here.
265      */
266     PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0 );
267     PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
268     PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0 );
269     PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0 );
270
271     return 1;
272 }
273
274
275 /***********************************************************************
276  *           build_command_line
277  *
278  * Build the command-line of a process from the argv array.
279  */
280 static inline char *build_command_line( char **argv )
281 {
282     int len, quote;
283     char *cmdline, *p, **arg;
284
285     for (arg = argv, len = 0; *arg; arg++) len += strlen(*arg) + 1;
286     if ((quote = (strchr( argv[0], ' ' ) != NULL))) len += 2;
287     if (!(p = cmdline = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
288     arg = argv;
289     if (quote)
290     {
291         *p++ = '\"';
292         strcpy( p, *arg );
293         p += strlen(p);
294         *p++ = '\"';
295         *p++ = ' ';
296         arg++;
297     }
298     while (*arg)
299     {
300         strcpy( p, *arg );
301         p += strlen(p);
302         *p++ = ' ';
303         arg++;
304     }
305     if (p > cmdline) p--;  /* remove last space */
306     *p = 0;
307     return cmdline;
308 }
309
310
311 /***********************************************************************
312  *           start_process
313  *
314  * Startup routine of a new process. Runs on the new process stack.
315  */
316 static void start_process(void)
317 {
318     struct init_process_done_request *req = get_req_buffer();
319     int debugged, console_app;
320     HMODULE16 hModule16;
321     UINT cmdShow = SW_SHOWNORMAL;
322     LPTHREAD_START_ROUTINE entry;
323     PDB *pdb = PROCESS_Current();
324     HMODULE module = pdb->exe_modref->module;
325
326     /* Increment EXE refcount */
327     pdb->exe_modref->refCount++;
328
329     /* build command line */
330     if (!(pdb->env_db->cmd_line = build_command_line( main_exe_argv ))) goto error;
331
332     /* Retrieve entry point address */
333     entry = (LPTHREAD_START_ROUTINE)RVA_PTR( module, OptionalHeader.AddressOfEntryPoint );
334     console_app = (PE_HEADER(module)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
335
336     if (console_app) pdb->flags |= PDB32_CONSOLE_PROC;
337
338     /* Signal the parent process to continue */
339     req->module = (void *)module;
340     req->entry  = entry;
341     req->name   = &pdb->exe_modref->filename;
342     req->gui    = !console_app;
343     server_call( REQ_INIT_PROCESS_DONE );
344     debugged = req->debugged;
345
346     /* Install signal handlers; this cannot be done before, since we cannot
347      * send exceptions to the debugger before the create process event that
348      * is sent by REQ_INIT_PROCESS_DONE */
349     if (!SIGNAL_Init()) goto error;
350
351     /* Load KERNEL (necessary for TASK_Create) */
352     if (!LoadLibraryA( "KERNEL32" )) goto error;
353
354     /* Create 16-bit dummy module */
355     if ((hModule16 = MODULE_CreateDummyModule( pdb->exe_modref->filename, module )) < 32)
356         ExitProcess( hModule16 );
357
358     if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
359         cmdShow = pdb->env_db->startup_info->wShowWindow;
360     if (!TASK_Create( (NE_MODULE *)GlobalLock16( hModule16 ), cmdShow,
361                       NtCurrentTeb(), NULL, 0 ))
362         goto error;
363
364     /* Load the system dlls */
365     if (!load_system_dlls()) goto error;
366
367     EnterCriticalSection( &pdb->crit_section );
368     PE_InitTls();
369     MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
370     LeaveCriticalSection( &pdb->crit_section );
371
372     /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
373     if (console_app) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0 );
374
375     TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
376     if (debugged) DbgBreakPoint();
377     /* FIXME: should use _PEB as parameter for NT 3.5 programs !
378      * Dunno about other OSs */
379     ExitThread( entry(NULL) );
380
381  error:
382     ExitProcess( GetLastError() );
383 }
384
385
386 /***********************************************************************
387  *           PROCESS_Start
388  *
389  * Startup routine of a new Win32 process once the main module has been loaded.
390  * The filename is free'd by this routine.
391  */
392 static void PROCESS_Start( HMODULE main_module, LPSTR filename ) WINE_NORETURN;
393 static void PROCESS_Start( HMODULE main_module, LPSTR filename )
394 {
395     if (!filename)
396     {
397         /* if no explicit filename, use argv[0] */
398         if (!(filename = malloc( MAX_PATH ))) ExitProcess(1);
399         if (!GetFullPathNameA( argv0, MAX_PATH, filename, NULL ))
400             lstrcpynA( filename, argv0, MAX_PATH );
401     }
402
403     /* load main module */
404     if (PE_HEADER(main_module)->FileHeader.Characteristics & IMAGE_FILE_DLL)
405         ExitProcess( ERROR_BAD_EXE_FORMAT );
406
407     /* Create 32-bit MODREF */
408     if (!PE_CreateModule( main_module, filename, 0, FALSE ))
409         goto error;
410     free( filename );
411
412     /* allocate main thread stack */
413     if (!THREAD_InitStack( NtCurrentTeb(),
414                            PE_HEADER(main_module)->OptionalHeader.SizeOfStackReserve, TRUE ))
415         goto error;
416
417     /* switch to the new stack */
418     SYSDEPS_SwitchToThreadStack( start_process );
419
420  error:
421     ExitProcess( GetLastError() );
422 }
423
424
425 /***********************************************************************
426  *           PROCESS_InitWine
427  *
428  * Wine initialisation: load and start the main exe file.
429  */
430 void PROCESS_InitWine( int argc, char *argv[] )
431 {
432     DWORD type;
433
434     /* Initialize everything */
435     if (!MAIN_MainInit( argv )) exit(1);
436
437     main_exe_argv = ++argv;  /* remove argv[0] (wine itself) */
438
439     if (!main_exe_name)
440     {
441         char buffer[MAX_PATH];
442         if (!argv[0]) OPTIONS_Usage();
443
444         /* open the exe file */
445         if (!SearchPathA( NULL, argv[0], ".exe", sizeof(buffer), buffer, NULL ) &&
446             !SearchPathA( NULL, argv[0], NULL, sizeof(buffer), buffer, NULL ))
447         {
448             MESSAGE( "%s: cannot find '%s'\n", argv0, argv[0] );
449             goto error;
450         }
451         if (!(main_exe_name = strdup(buffer)))
452         {
453             MESSAGE( "%s: out of memory\n", argv0 );
454             ExitProcess(1);
455         }
456     }
457
458     if (main_exe_file == INVALID_HANDLE_VALUE)
459     {
460         if ((main_exe_file = CreateFileA( main_exe_name, GENERIC_READ, FILE_SHARE_READ,
461                                           NULL, OPEN_EXISTING, 0, -1 )) == INVALID_HANDLE_VALUE)
462         {
463             MESSAGE( "%s: cannot open '%s'\n", argv0, main_exe_name );
464             goto error;
465         }
466     }
467
468     if (!MODULE_GetBinaryType( main_exe_file, main_exe_name, &type ))
469     {
470         MESSAGE( "%s: unrecognized executable '%s'\n", argv0, main_exe_name );
471         goto error;
472     }
473
474     switch (type)
475     {
476     case SCS_32BIT_BINARY:
477         {
478             HMODULE main_module = PE_LoadImage( main_exe_file, main_exe_name );
479             if (main_module) PROCESS_Start( main_module, main_exe_name );
480         }
481         break;
482
483     case SCS_WOW_BINARY:
484         {
485             HMODULE main_module;
486             /* create 32-bit module for main exe */
487             if (!(main_module = BUILTIN32_LoadExeModule())) goto error;
488             NtCurrentTeb()->tibflags &= ~TEBF_WIN32;
489             PROCESS_Current()->flags |= PDB32_WIN16_PROC;
490             SYSLEVEL_EnterWin16Lock();
491             PROCESS_Start( main_module, NULL );
492         }
493         break;
494
495     case SCS_DOS_BINARY:
496         FIXME( "DOS binaries support is broken at the moment; feel free to fix it...\n" );
497         SetLastError( ERROR_BAD_FORMAT );
498         break;
499
500     case SCS_PIF_BINARY:
501     case SCS_POSIX_BINARY:
502     case SCS_OS216_BINARY:
503     default:
504         MESSAGE( "%s: unrecognized executable '%s'\n", argv0, main_exe_name );
505         SetLastError( ERROR_BAD_FORMAT );
506         break;
507     }
508  error:
509     ExitProcess( GetLastError() );
510 }
511
512
513 /***********************************************************************
514  *           PROCESS_InitWinelib
515  *
516  * Initialisation of a new Winelib process.
517  */
518 void PROCESS_InitWinelib( int argc, char *argv[] )
519 {
520     HMODULE main_module;
521
522     if (!MAIN_MainInit( argv )) exit(1);
523
524     /* create 32-bit module for main exe */
525     if (!(main_module = BUILTIN32_LoadExeModule())) ExitProcess( GetLastError() );
526
527     main_exe_argv = argv;
528     PROCESS_Start( main_module, NULL );
529 }
530
531
532 /***********************************************************************
533  *           build_argv
534  *
535  * Build an argv array from a command-line.
536  * The command-line is modified to insert nulls.
537  * 'reserved' is the number of args to reserve before the first one.
538  */
539 static char **build_argv( char *cmdline, int reserved )
540 {
541     char **argv;
542     int count = reserved + 1;
543     char *p = cmdline;
544
545     /* if first word is quoted store it as a single arg */
546     if (*cmdline == '\"')
547     {
548         if ((p = strchr( cmdline + 1, '\"' )))
549         {
550             p++;
551             count++;
552         }
553         else p = cmdline;
554     }
555     while (*p)
556     {
557         while (*p && isspace(*p)) p++;
558         if (!*p) break;
559         count++;
560         while (*p && !isspace(*p)) p++;
561     }
562
563     if ((argv = malloc( count * sizeof(*argv) )))
564     {
565         char **argvptr = argv + reserved;
566         p = cmdline;
567         if (*cmdline == '\"')
568         {
569             if ((p = strchr( cmdline + 1, '\"' )))
570             {
571                 *argvptr++ = cmdline + 1;
572                 *p++ = 0;
573             }
574             else p = cmdline;
575         }
576         while (*p)
577         {
578             while (*p && isspace(*p)) *p++ = 0;
579             if (!*p) break;
580             *argvptr++ = p;
581             while (*p && !isspace(*p)) p++;
582         }
583         *argvptr = 0;
584     }
585     return argv;
586 }
587
588
589 /***********************************************************************
590  *           build_envp
591  *
592  * Build the environment of a new child process.
593  */
594 static char **build_envp( const char *env )
595 {
596     const char *p;
597     char **envp;
598     int count;
599
600     for (p = env, count = 0; *p; count++) p += strlen(p) + 1;
601     count += 3;
602     if ((envp = malloc( count * sizeof(*envp) )))
603     {
604         extern char **environ;
605         char **envptr = envp;
606         char **unixptr = environ;
607         /* first put PATH, HOME and WINEPREFIX from the unix env */
608         for (unixptr = environ; unixptr && *unixptr; unixptr++)
609             if (!memcmp( *unixptr, "PATH=", 5 ) ||
610                 !memcmp( *unixptr, "HOME=", 5 ) ||
611                 !memcmp( *unixptr, "WINEPREFIX=", 11 )) *envptr++ = *unixptr;
612         /* now put the Windows environment strings */
613         for (p = env; *p; p += strlen(p) + 1)
614         {
615             if (memcmp( p, "PATH=", 5 ) &&
616                 memcmp( p, "HOME=", 5 ) &&
617                 memcmp( p, "WINEPREFIX=", 11 )) *envptr++ = (char *)p;
618         }
619         *envptr = 0;
620     }
621     return envp;
622 }
623
624
625 /***********************************************************************
626  *           find_wine_binary
627  *
628  * Locate the Wine binary to exec for a new Win32 process.
629  */
630 static void exec_wine_binary( char **argv, char **envp )
631 {
632     const char *path, *pos, *ptr;
633
634     /* first try bin directory */
635     argv[0] = BINDIR "/wine";
636     execve( argv[0], argv, envp );
637
638     /* now try the path of argv0 of the current binary */
639     if (!(argv[0] = malloc( strlen(argv0) + 6 ))) return;
640     if ((ptr = strrchr( argv0, '/' )))
641     {
642         memcpy( argv[0], argv0, ptr - argv0 );
643         strcpy( argv[0] + (ptr - argv0), "/wine" );
644         execve( argv[0], argv, envp );
645     }
646     free( argv[0] );
647
648     /* now search in the Unix path */
649     if ((path = getenv( "PATH" )))
650     {
651         if (!(argv[0] = malloc( strlen(path) + 6 ))) return;
652         pos = path;
653         for (;;)
654         {
655             while (*pos == ':') pos++;
656             if (!*pos) break;
657             if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
658             memcpy( argv[0], pos, ptr - pos );
659             strcpy( argv[0] + (ptr - pos), "/wine" );
660             execve( argv[0], argv, envp );
661             pos = ptr;
662         }
663     }
664     free( argv[0] );
665
666     /* finally try the current directory */
667     argv[0] = "./wine";
668     execve( argv[0], argv, envp );
669 }
670
671
672 /***********************************************************************
673  *           fork_and_exec
674  *
675  * Fork and exec a new Unix process, checking for errors.
676  */
677 static int fork_and_exec( const char *filename, char *cmdline,
678                           const char *env, const char *newdir )
679 {
680     int fd[2];
681     int pid, err;
682
683     if (pipe(fd) == -1)
684     {
685         FILE_SetDosError();
686         return -1;
687     }
688     fcntl( fd[1], F_SETFD, 1 );  /* set close on exec */
689     if (!(pid = fork()))  /* child */
690     {
691         char **argv = build_argv( cmdline, filename ? 0 : 2 );
692         char **envp = build_envp( env );
693         close( fd[0] );
694
695         if (newdir) chdir(newdir);
696
697         if (argv && envp)
698         {
699             if (!filename)
700             {
701                 argv[1] = "--";
702                 exec_wine_binary( argv, envp );
703             }
704             else execve( filename, argv, envp );
705         }
706         err = errno;
707         write( fd[1], &err, sizeof(err) );
708         _exit(1);
709     }
710     close( fd[1] );
711     if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0))  /* exec failed */
712     {
713         errno = err;
714         pid = -1;
715     }
716     if (pid == -1) FILE_SetDosError();
717     close( fd[0] );
718     return pid;
719 }
720
721
722 /***********************************************************************
723  *           PROCESS_Create
724  *
725  * Create a new process. If hFile is a valid handle we have an exe
726  * file, and we exec a new copy of wine to load it; otherwise we
727  * simply exec the specified filename as a Unix process.
728  */
729 BOOL PROCESS_Create( HFILE hFile, LPCSTR filename, LPSTR cmd_line, LPCSTR env, 
730                      LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
731                      BOOL inherit, DWORD flags, LPSTARTUPINFOA startup,
732                      LPPROCESS_INFORMATION info, LPCSTR lpCurrentDirectory )
733 {
734     int pid;
735     const char *unixfilename = NULL;
736     const char *unixdir = NULL;
737     DOS_FULL_NAME full_name;
738     HANDLE load_done_evt = -1;
739     struct new_process_request *req = get_req_buffer();
740     struct wait_process_request *wait_req = get_req_buffer();
741
742     info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
743     
744     /* create the process on the server side */
745
746     req->inherit_all  = inherit;
747     req->create_flags = flags;
748     req->start_flags  = startup->dwFlags;
749     req->exe_file     = hFile;
750     if (startup->dwFlags & STARTF_USESTDHANDLES)
751     {
752         req->hstdin  = startup->hStdInput;
753         req->hstdout = startup->hStdOutput;
754         req->hstderr = startup->hStdError;
755     }
756     else
757     {
758         req->hstdin  = GetStdHandle( STD_INPUT_HANDLE );
759         req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
760         req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
761     }
762     req->cmd_show = startup->wShowWindow;
763     req->alloc_fd = 0;
764
765     if (lpCurrentDirectory) {
766         if (DOSFS_GetFullName( lpCurrentDirectory, TRUE, &full_name ))
767             unixdir = full_name.long_name;
768     } else {
769         CHAR    buf[260];
770         if (GetCurrentDirectoryA(sizeof(buf),buf)) {
771             if (DOSFS_GetFullName( buf, TRUE, &full_name ))
772                 unixdir = full_name.long_name;
773         }
774     }
775
776     if (hFile == -1)  /* unix process */
777     {
778         unixfilename = filename;
779         if (DOSFS_GetFullName( filename, TRUE, &full_name )) unixfilename = full_name.long_name;
780         req->filename[0] = 0;
781     }
782     else  /* new wine process */
783     {
784         if (!GetFullPathNameA( filename, server_remaining(req->filename), req->filename, NULL ))
785             lstrcpynA( req->filename, filename, server_remaining(req->filename) );
786     }
787     if (server_call( REQ_NEW_PROCESS )) return FALSE;
788
789     /* fork and execute */
790
791     pid = fork_and_exec( unixfilename, cmd_line, env ? env : GetEnvironmentStringsA(), unixdir );
792
793     wait_req->cancel   = (pid == -1);
794     wait_req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
795     wait_req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
796     wait_req->timeout  = 2000;
797     if (server_call( REQ_WAIT_PROCESS ) || (pid == -1)) goto error;
798     info->dwProcessId = (DWORD)wait_req->pid;
799     info->dwThreadId  = (DWORD)wait_req->tid;
800     info->hProcess    = wait_req->phandle;
801     info->hThread     = wait_req->thandle;
802     load_done_evt     = wait_req->event;
803
804     /* Wait until process is initialized (or initialization failed) */
805     if (load_done_evt != -1)
806     {
807         DWORD res;
808         HANDLE handles[2];
809
810         handles[0] = info->hProcess;
811         handles[1] = load_done_evt;
812         res = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
813         CloseHandle( load_done_evt );
814         if (res == STATUS_WAIT_0)  /* the process died */
815         {
816             DWORD exitcode;
817             if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
818             CloseHandle( info->hThread );
819             CloseHandle( info->hProcess );
820             return FALSE;
821         }
822     }
823     return TRUE;
824
825 error:
826     if (load_done_evt != -1) CloseHandle( load_done_evt );
827     if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
828     if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
829     return FALSE;
830 }
831
832
833 /***********************************************************************
834  *           ExitProcess   (KERNEL32.100)
835  */
836 void WINAPI ExitProcess( DWORD status )
837 {
838     struct terminate_process_request *req = get_req_buffer();
839
840     MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
841     /* send the exit code to the server */
842     req->handle    = GetCurrentProcess();
843     req->exit_code = status;
844     server_call( REQ_TERMINATE_PROCESS );
845     exit( status );
846 }
847
848 /***********************************************************************
849  *           ExitProcess16   (KERNEL.466)
850  */
851 void WINAPI ExitProcess16( WORD status )
852 {
853     SYSLEVEL_ReleaseWin16Lock();
854     ExitProcess( status );
855 }
856
857 /******************************************************************************
858  *           TerminateProcess   (KERNEL32.684)
859  */
860 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
861 {
862     BOOL ret;
863     struct terminate_process_request *req = get_req_buffer();
864     req->handle    = handle;
865     req->exit_code = exit_code;
866     if ((ret = !server_call( REQ_TERMINATE_PROCESS )) && req->self) exit( exit_code );
867     return ret;
868 }
869
870
871 /***********************************************************************
872  *           GetProcessDword    (KERNEL32.18) (KERNEL.485)
873  * 'Of course you cannot directly access Windows internal structures'
874  */
875 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
876 {
877     PDB *process = PROCESS_IdToPDB( dwProcessID );
878     TDB *pTask;
879     DWORD x, y;
880
881     TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
882     if ( !process )
883     {
884         ERR("%d: process %lx not accessible\n", offset, dwProcessID);
885         return 0;
886     }
887
888     switch ( offset ) 
889     {
890     case GPD_APP_COMPAT_FLAGS:
891         pTask = (TDB *)GlobalLock16( GetCurrentTask() );
892         return pTask? pTask->compat_flags : 0;
893
894     case GPD_LOAD_DONE_EVENT:
895         return process->load_done_evt;
896
897     case GPD_HINSTANCE16:
898         pTask = (TDB *)GlobalLock16( GetCurrentTask() );
899         return pTask? pTask->hInstance : 0;
900
901     case GPD_WINDOWS_VERSION:
902         pTask = (TDB *)GlobalLock16( GetCurrentTask() );
903         return pTask? pTask->version : 0;
904
905     case GPD_THDB:
906         if ( process != PROCESS_Current() ) return 0;
907         return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
908
909     case GPD_PDB:
910         return (DWORD)process;
911
912     case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
913         return process->env_db->startup_info->hStdOutput;
914
915     case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
916         return process->env_db->startup_info->hStdInput;
917
918     case GPD_STARTF_SHOWWINDOW:
919         return process->env_db->startup_info->wShowWindow;
920
921     case GPD_STARTF_SIZE:
922         x = process->env_db->startup_info->dwXSize;
923         if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
924         y = process->env_db->startup_info->dwYSize;
925         if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
926         return MAKELONG( x, y );
927
928     case GPD_STARTF_POSITION:
929         x = process->env_db->startup_info->dwX;
930         if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
931         y = process->env_db->startup_info->dwY;
932         if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
933         return MAKELONG( x, y );
934
935     case GPD_STARTF_FLAGS:
936         return process->env_db->startup_info->dwFlags;
937
938     case GPD_PARENT:
939         return 0;
940
941     case GPD_FLAGS:
942         return process->flags;
943
944     case GPD_USERDATA:
945         return process->process_dword;
946
947     default:
948         ERR_(win32)("Unknown offset %d\n", offset );
949         return 0;
950     }
951 }
952
953 /***********************************************************************
954  *           SetProcessDword    (KERNEL.484)
955  * 'Of course you cannot directly access Windows internal structures'
956  */
957 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
958 {
959     PDB *process = PROCESS_IdToPDB( dwProcessID );
960
961     TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
962     if ( !process )
963     {
964         ERR("%d: process %lx not accessible\n", offset, dwProcessID);
965         return;
966     }
967
968     switch ( offset ) 
969     {
970     case GPD_APP_COMPAT_FLAGS:
971     case GPD_LOAD_DONE_EVENT:
972     case GPD_HINSTANCE16:
973     case GPD_WINDOWS_VERSION:
974     case GPD_THDB:
975     case GPD_PDB:
976     case GPD_STARTF_SHELLDATA:
977     case GPD_STARTF_HOTKEY:
978     case GPD_STARTF_SHOWWINDOW:
979     case GPD_STARTF_SIZE:
980     case GPD_STARTF_POSITION:
981     case GPD_STARTF_FLAGS:
982     case GPD_PARENT:
983     case GPD_FLAGS:
984         ERR_(win32)("Not allowed to modify offset %d\n", offset );
985         break;
986
987     case GPD_USERDATA:
988         process->process_dword = value; 
989         break;
990
991     default:
992         ERR_(win32)("Unknown offset %d\n", offset );
993         break;
994     }
995 }
996
997
998 /*********************************************************************
999  *           OpenProcess   (KERNEL32.543)
1000  */
1001 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
1002 {
1003     HANDLE ret = 0;
1004     struct open_process_request *req = get_req_buffer();
1005
1006     req->pid     = (void *)id;
1007     req->access  = access;
1008     req->inherit = inherit;
1009     if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
1010     return ret;
1011 }                             
1012
1013 /*********************************************************************
1014  *           MapProcessHandle   (KERNEL.483)
1015  */
1016 DWORD WINAPI MapProcessHandle( HANDLE handle )
1017 {
1018     DWORD ret = 0;
1019     struct get_process_info_request *req = get_req_buffer();
1020     req->handle = handle;
1021     if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
1022     return ret;
1023 }
1024
1025 /***********************************************************************
1026  *           SetPriorityClass   (KERNEL32.503)
1027  */
1028 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
1029 {
1030     struct set_process_info_request *req = get_req_buffer();
1031     req->handle   = hprocess;
1032     req->priority = priorityclass;
1033     req->mask     = SET_PROCESS_INFO_PRIORITY;
1034     return !server_call( REQ_SET_PROCESS_INFO );
1035 }
1036
1037
1038 /***********************************************************************
1039  *           GetPriorityClass   (KERNEL32.250)
1040  */
1041 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
1042 {
1043     DWORD ret = 0;
1044     struct get_process_info_request *req = get_req_buffer();
1045     req->handle = hprocess;
1046     if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
1047     return ret;
1048 }
1049
1050
1051 /***********************************************************************
1052  *          SetProcessAffinityMask   (KERNEL32.662)
1053  */
1054 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
1055 {
1056     struct set_process_info_request *req = get_req_buffer();
1057     req->handle   = hProcess;
1058     req->affinity = affmask;
1059     req->mask     = SET_PROCESS_INFO_AFFINITY;
1060     return !server_call( REQ_SET_PROCESS_INFO );
1061 }
1062
1063 /**********************************************************************
1064  *          GetProcessAffinityMask    (KERNEL32.373)
1065  */
1066 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
1067                                       LPDWORD lpProcessAffinityMask,
1068                                       LPDWORD lpSystemAffinityMask )
1069 {
1070     BOOL ret = FALSE;
1071     struct get_process_info_request *req = get_req_buffer();
1072     req->handle = hProcess;
1073     if (!server_call( REQ_GET_PROCESS_INFO ))
1074     {
1075         if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
1076         if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
1077         ret = TRUE;
1078     }
1079     return ret;
1080 }
1081
1082
1083 /***********************************************************************
1084  *           GetStdHandle    (KERNEL32.276)
1085  */
1086 HANDLE WINAPI GetStdHandle( DWORD std_handle )
1087 {
1088     PDB *pdb = PROCESS_Current();
1089
1090     switch(std_handle)
1091     {
1092     case STD_INPUT_HANDLE:  return pdb->env_db->hStdin;
1093     case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
1094     case STD_ERROR_HANDLE:  return pdb->env_db->hStderr;
1095     }
1096     SetLastError( ERROR_INVALID_PARAMETER );
1097     return INVALID_HANDLE_VALUE;
1098 }
1099
1100
1101 /***********************************************************************
1102  *           SetStdHandle    (KERNEL32.506)
1103  */
1104 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
1105 {
1106     PDB *pdb = PROCESS_Current();
1107     /* FIXME: should we close the previous handle? */
1108     switch(std_handle)
1109     {
1110     case STD_INPUT_HANDLE:
1111         pdb->env_db->hStdin = handle;
1112         return TRUE;
1113     case STD_OUTPUT_HANDLE:
1114         pdb->env_db->hStdout = handle;
1115         return TRUE;
1116     case STD_ERROR_HANDLE:
1117         pdb->env_db->hStderr = handle;
1118         return TRUE;
1119     }
1120     SetLastError( ERROR_INVALID_PARAMETER );
1121     return FALSE;
1122 }
1123
1124 /***********************************************************************
1125  *           GetProcessVersion    (KERNEL32)
1126  */
1127 DWORD WINAPI GetProcessVersion( DWORD processid )
1128 {
1129     TDB *pTask;
1130     PDB *pdb = PROCESS_IdToPDB( processid );
1131
1132     if (!pdb) return 0;
1133     if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
1134     return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
1135 }
1136
1137 /***********************************************************************
1138  *           GetProcessFlags    (KERNEL32)
1139  */
1140 DWORD WINAPI GetProcessFlags( DWORD processid )
1141 {
1142     PDB *pdb = PROCESS_IdToPDB( processid );
1143     if (!pdb) return 0;
1144     return pdb->flags;
1145 }
1146
1147 /***********************************************************************
1148  *              SetProcessWorkingSetSize        [KERNEL32.662]
1149  * Sets the min/max working set sizes for a specified process.
1150  *
1151  * PARAMS
1152  *    hProcess [I] Handle to the process of interest
1153  *    minset   [I] Specifies minimum working set size
1154  *    maxset   [I] Specifies maximum working set size
1155  *
1156  * RETURNS  STD
1157  */
1158 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
1159                                        DWORD maxset)
1160 {
1161     FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
1162     if(( minset == -1) && (maxset == -1)) {
1163         /* Trim the working set to zero */
1164         /* Swap the process out of physical RAM */
1165     }
1166     return TRUE;
1167 }
1168
1169 /***********************************************************************
1170  *           GetProcessWorkingSetSize    (KERNEL32)
1171  */
1172 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1173                                        LPDWORD maxset)
1174 {
1175         FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1176         /* 32 MB working set size */
1177         if (minset) *minset = 32*1024*1024;
1178         if (maxset) *maxset = 32*1024*1024;
1179         return TRUE;
1180 }
1181
1182 /***********************************************************************
1183  *           SetProcessShutdownParameters    (KERNEL32)
1184  *
1185  * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1186  * Now tracks changes made (but does not act on these changes)
1187  * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1188  * It really shouldn't be here, but I'll move it when it's been checked!
1189  */
1190 #define SHUTDOWN_NORETRY 1
1191 static unsigned int shutdown_noretry = 0;
1192 static unsigned int shutdown_priority = 0x280L;
1193 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1194 {
1195     if (flags & SHUTDOWN_NORETRY)
1196       shutdown_noretry = 1;
1197     else
1198       shutdown_noretry = 0;
1199     if (level > 0x100L && level < 0x3FFL)
1200       shutdown_priority = level;
1201     else
1202       {
1203         ERR("invalid priority level 0x%08lx\n", level);
1204         return FALSE;
1205       }
1206     return TRUE;
1207 }
1208
1209
1210 /***********************************************************************
1211  * GetProcessShutdownParameters                 (KERNEL32)
1212  *
1213  */
1214 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1215                                             LPDWORD lpdwFlags )
1216 {
1217   (*lpdwLevel) = shutdown_priority;
1218   (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1219   return TRUE;
1220 }
1221 /***********************************************************************
1222  *           SetProcessPriorityBoost    (KERNEL32)
1223  */
1224 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1225 {
1226     FIXME("(%d,%d): stub\n",hprocess,disableboost);
1227     /* Say we can do it. I doubt the program will notice that we don't. */
1228     return TRUE;
1229 }
1230
1231
1232 /***********************************************************************
1233  *           ReadProcessMemory                  (KERNEL32)
1234  */
1235 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1236                                LPDWORD bytes_read )
1237 {
1238     struct read_process_memory_request *req = get_req_buffer();
1239     unsigned int offset = (unsigned int)addr % sizeof(int);
1240     unsigned int max = server_remaining( req->data );  /* max length in one request */
1241     unsigned int pos;
1242
1243     if (bytes_read) *bytes_read = size;
1244
1245     /* first time, read total length to check for permissions */
1246     req->handle = process;
1247     req->addr   = (char *)addr - offset;
1248     req->len    = (size + offset + sizeof(int) - 1) / sizeof(int);
1249     if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1250
1251     if (size <= max - offset)
1252     {
1253         memcpy( buffer, (char *)req->data + offset, size );
1254         return TRUE;
1255     }
1256
1257     /* now take care of the remaining data */
1258     memcpy( buffer, (char *)req->data + offset, max - offset );
1259     pos = max - offset;
1260     size -= pos;
1261     while (size)
1262     {
1263         if (max > size) max = size;
1264         req->handle = process;
1265         req->addr   = (char *)addr + pos;
1266         req->len    = (max + sizeof(int) - 1) / sizeof(int);
1267         if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1268         memcpy( (char *)buffer + pos, (char *)req->data, max );
1269         size -= max;
1270         pos += max;
1271     }
1272     return TRUE;
1273
1274  error:
1275     if (bytes_read) *bytes_read = 0;
1276     return FALSE;
1277 }
1278
1279
1280 /***********************************************************************
1281  *           WriteProcessMemory                 (KERNEL32)
1282  */
1283 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1284                                 LPDWORD bytes_written )
1285 {
1286     unsigned int first_offset, last_offset;
1287     struct write_process_memory_request *req = get_req_buffer();
1288     unsigned int max = server_remaining( req->data );  /* max length in one request */
1289     unsigned int pos, last_mask;
1290
1291     if (!size)
1292     {
1293         SetLastError( ERROR_INVALID_PARAMETER );
1294         return FALSE;
1295     }
1296     if (bytes_written) *bytes_written = size;
1297
1298     /* compute the mask for the first int */
1299     req->first_mask = ~0;
1300     first_offset = (unsigned int)addr % sizeof(int);
1301     memset( &req->first_mask, 0, first_offset );
1302
1303     /* compute the mask for the last int */
1304     last_offset = (size + first_offset) % sizeof(int);
1305     last_mask = 0;
1306     memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1307
1308     req->handle = process;
1309     req->addr = (char *)addr - first_offset;
1310     /* for the first request, use the total length */
1311     req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1312
1313     if (size + first_offset < max)  /* we can do it in one round */
1314     {
1315         memcpy( (char *)req->data + first_offset, buffer, size );
1316         req->last_mask = last_mask;
1317         if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1318         return TRUE;
1319     }
1320
1321     /* needs multiple server calls */
1322
1323     memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1324     req->last_mask = ~0;
1325     if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1326     pos = max - first_offset;
1327     size -= pos;
1328     while (size)
1329     {
1330         if (size <= max)  /* last one */
1331         {
1332             req->last_mask = last_mask;
1333             max = size;
1334         }
1335         req->handle = process;
1336         req->addr = (char *)addr + pos;
1337         req->len = (max + sizeof(int) - 1) / sizeof(int);
1338         req->first_mask = ~0;
1339         memcpy( req->data, (char *) buffer + pos, max );
1340         if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1341         pos += max;
1342         size -= max;
1343     }
1344     return TRUE;
1345
1346  error:
1347     if (bytes_written) *bytes_written = 0;
1348     return FALSE;
1349
1350 }
1351
1352
1353 /***********************************************************************
1354  *           RegisterServiceProcess             (KERNEL, KERNEL32)
1355  *
1356  * A service process calls this function to ensure that it continues to run
1357  * even after a user logged off.
1358  */
1359 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1360 {
1361         /* I don't think that Wine needs to do anything in that function */
1362         return 1; /* success */
1363 }
1364
1365 /***********************************************************************
1366  * GetExitCodeProcess [KERNEL32.325]
1367  *
1368  * Gets termination status of specified process
1369  * 
1370  * RETURNS
1371  *   Success: TRUE
1372  *   Failure: FALSE
1373  */
1374 BOOL WINAPI GetExitCodeProcess(
1375     HANDLE hProcess,  /* [I] handle to the process */
1376     LPDWORD lpExitCode) /* [O] address to receive termination status */
1377 {
1378     BOOL ret = FALSE;
1379     struct get_process_info_request *req = get_req_buffer();
1380     req->handle = hProcess;
1381     if (!server_call( REQ_GET_PROCESS_INFO ))
1382     {
1383         if (lpExitCode) *lpExitCode = req->exit_code;
1384         ret = TRUE;
1385     }
1386     return ret;
1387 }
1388
1389
1390 /***********************************************************************
1391  *           SetErrorMode   (KERNEL32.486)
1392  */
1393 UINT WINAPI SetErrorMode( UINT mode )
1394 {
1395     UINT old = PROCESS_Current()->error_mode;
1396     PROCESS_Current()->error_mode = mode;
1397     return old;
1398 }
1399
1400 /***********************************************************************
1401  *           GetCurrentProcess   (KERNEL32.198)
1402  */
1403 #undef GetCurrentProcess
1404 HANDLE WINAPI GetCurrentProcess(void)
1405 {
1406     return 0xffffffff;
1407 }