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