Commented out exit() call on ExitProcess for now.
[wine/multimedia.git] / scheduler / process.c
blobaa460cd4f8d3825e01d00e01f2ed23726617de09
1 /*
2 * Win32 processes
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include "wine/winbase16.h"
13 #include "wine/exception.h"
14 #include "process.h"
15 #include "module.h"
16 #include "neexe.h"
17 #include "file.h"
18 #include "global.h"
19 #include "heap.h"
20 #include "task.h"
21 #include "ldt.h"
22 #include "syslevel.h"
23 #include "thread.h"
24 #include "winerror.h"
25 #include "pe_image.h"
26 #include "server.h"
27 #include "options.h"
28 #include "callback.h"
29 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(process)
32 DECLARE_DEBUG_CHANNEL(relay)
33 DECLARE_DEBUG_CHANNEL(win32)
36 /* The initial process PDB */
37 static PDB initial_pdb;
39 static PDB *PROCESS_First = &initial_pdb;
42 /***********************************************************************
43 * PROCESS_WalkProcess
45 void PROCESS_WalkProcess(void)
47 PDB *pdb;
48 char *name;
50 pdb = PROCESS_First;
51 MESSAGE( " pid PDB #th modref module \n" );
52 while(pdb)
54 if (pdb == &initial_pdb)
55 name = "initial PDB";
56 else
57 name = (pdb->exe_modref) ? pdb->exe_modref->filename : "";
59 MESSAGE( " %8p %8p %5d %8p %s\n", pdb->server_pid, pdb,
60 pdb->threads, pdb->exe_modref, name);
61 pdb = pdb->next;
63 return;
67 /***********************************************************************
68 * PROCESS_IdToPDB
70 * Convert a process id to a PDB, making sure it is valid.
72 PDB *PROCESS_IdToPDB( DWORD pid )
74 PDB *pdb;
76 if (!pid) return PROCESS_Current();
77 pdb = PROCESS_First;
78 while (pdb)
80 if ((DWORD)pdb->server_pid == pid) return pdb;
81 pdb = pdb->next;
83 SetLastError( ERROR_INVALID_PARAMETER );
84 return NULL;
88 /***********************************************************************
89 * PROCESS_CallUserSignalProc
91 * FIXME: Some of the signals aren't sent correctly!
93 * The exact meaning of the USER signals is undocumented, but this
94 * should cover the basic idea:
96 * USIG_DLL_UNLOAD_WIN16
97 * This is sent when a 16-bit module is unloaded.
99 * USIG_DLL_UNLOAD_WIN32
100 * This is sent when a 32-bit module is unloaded.
102 * USIG_DLL_UNLOAD_ORPHANS
103 * This is sent after the last Win3.1 module is unloaded,
104 * to allow removal of orphaned menus.
106 * USIG_FAULT_DIALOG_PUSH
107 * USIG_FAULT_DIALOG_POP
108 * These are called to allow USER to prepare for displaying a
109 * fault dialog, even though the fault might have happened while
110 * inside a USER critical section.
112 * USIG_THREAD_INIT
113 * This is called from the context of a new thread, as soon as it
114 * has started to run.
116 * USIG_THREAD_EXIT
117 * This is called, still in its context, just before a thread is
118 * about to terminate.
120 * USIG_PROCESS_CREATE
121 * This is called, in the parent process context, after a new process
122 * has been created.
124 * USIG_PROCESS_INIT
125 * This is called in the new process context, just after the main thread
126 * has started execution (after the main thread's USIG_THREAD_INIT has
127 * been sent).
129 * USIG_PROCESS_LOADED
130 * This is called after the executable file has been loaded into the
131 * new process context.
133 * USIG_PROCESS_RUNNING
134 * This is called immediately before the main entry point is called.
136 * USIG_PROCESS_EXIT
137 * This is called in the context of a process that is about to
138 * terminate (but before the last thread's USIG_THREAD_EXIT has
139 * been sent).
141 * USIG_PROCESS_DESTROY
142 * This is called after a process has terminated.
145 * The meaning of the dwFlags bits is as follows:
147 * USIG_FLAGS_WIN32
148 * Current process is 32-bit.
150 * USIG_FLAGS_GUI
151 * Current process is a (Win32) GUI process.
153 * USIG_FLAGS_FEEDBACK
154 * Current process needs 'feedback' (determined from the STARTUPINFO
155 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
157 * USIG_FLAGS_FAULT
158 * The signal is being sent due to a fault.
160 void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
162 DWORD flags = PROCESS_Current()->flags;
163 DWORD startup_flags = PROCESS_Current()->env_db->startup_info->dwFlags;
164 DWORD dwFlags = 0;
166 /* Determine dwFlags */
168 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
170 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
172 if ( dwFlags & USIG_FLAGS_GUI )
174 /* Feedback defaults to ON */
175 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
176 dwFlags |= USIG_FLAGS_FEEDBACK;
178 else
180 /* Feedback defaults to OFF */
181 if (startup_flags & STARTF_FORCEONFEEDBACK)
182 dwFlags |= USIG_FLAGS_FEEDBACK;
185 /* Convert module handle to 16-bit */
187 if ( HIWORD( hModule ) )
188 hModule = MapHModuleLS( hModule );
190 /* Call USER signal proc */
192 if ( Callout.UserSignalProc )
194 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
195 Callout.UserSignalProc( uCode, GetCurrentThreadId(), dwFlags, hModule );
196 else
197 Callout.UserSignalProc( uCode, GetCurrentProcessId(), dwFlags, hModule );
201 /***********************************************************************
202 * PROCESS_CreateEnvDB
204 * Create the env DB for a newly started process.
206 static BOOL PROCESS_CreateEnvDB(void)
208 struct init_process_request *req = get_req_buffer();
209 STARTUPINFOA *startup;
210 ENVDB *env_db;
211 char cmd_line[4096];
212 PDB *pdb = PROCESS_Current();
214 /* Allocate the env DB */
216 if (!(env_db = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
217 return FALSE;
218 pdb->env_db = env_db;
219 InitializeCriticalSection( &env_db->section );
221 /* Allocate and fill the startup info */
222 if (!(startup = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
223 return FALSE;
224 env_db->startup_info = startup;
226 /* Retrieve startup info from the server */
228 req->ldt_copy = ldt_copy;
229 req->ldt_flags = ldt_flags_copy;
230 if (server_call( REQ_INIT_PROCESS )) return FALSE;
231 pdb->exe_file = req->exe_file;
232 startup->dwFlags = req->start_flags;
233 startup->wShowWindow = req->cmd_show;
234 env_db->hStdin = startup->hStdInput = req->hstdin;
235 env_db->hStdout = startup->hStdOutput = req->hstdout;
236 env_db->hStderr = startup->hStdError = req->hstderr;
237 lstrcpynA( cmd_line, req->cmdline, sizeof(cmd_line) );
239 /* Copy the parent environment */
241 if (!ENV_InheritEnvironment( req->env_ptr )) return FALSE;
243 /* Copy the command line */
245 if (!(pdb->env_db->cmd_line = HEAP_strdupA( GetProcessHeap(), 0, cmd_line )))
246 return FALSE;
248 return TRUE;
252 /***********************************************************************
253 * PROCESS_FreePDB
255 * Free a PDB and all associated storage.
257 void PROCESS_FreePDB( PDB *pdb )
259 PDB **pptr = &PROCESS_First;
261 ENV_FreeEnvironment( pdb );
262 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
263 if (*pptr) *pptr = pdb->next;
264 HeapFree( GetProcessHeap(), 0, pdb );
268 /***********************************************************************
269 * PROCESS_CreatePDB
271 * Allocate and fill a PDB structure.
272 * Runs in the context of the parent process.
274 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
276 PDB *pdb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PDB) );
278 if (!pdb) return NULL;
279 pdb->exit_code = STILL_ACTIVE;
280 pdb->threads = 1;
281 pdb->running_threads = 1;
282 pdb->ring0_threads = 1;
283 pdb->parent = parent;
284 pdb->group = pdb;
285 pdb->priority = 8; /* Normal */
286 pdb->next = PROCESS_First;
287 pdb->winver = 0xffff; /* to be determined */
288 pdb->main_queue = INVALID_HANDLE_VALUE16;
289 PROCESS_First = pdb;
290 return pdb;
294 /***********************************************************************
295 * PROCESS_Init
297 BOOL PROCESS_Init( BOOL win32 )
299 TEB *teb;
300 int server_fd;
302 /* Start the server */
303 server_fd = CLIENT_InitServer();
305 /* Fill the initial process structure */
306 initial_pdb.exit_code = STILL_ACTIVE;
307 initial_pdb.threads = 1;
308 initial_pdb.running_threads = 1;
309 initial_pdb.ring0_threads = 1;
310 initial_pdb.group = &initial_pdb;
311 initial_pdb.priority = 8; /* Normal */
312 initial_pdb.flags = win32? 0 : PDB32_WIN16_PROC;
313 initial_pdb.winver = 0xffff; /* to be determined */
314 initial_pdb.main_queue = INVALID_HANDLE_VALUE16;
316 /* Initialize virtual memory management */
317 if (!VIRTUAL_Init()) return FALSE;
319 /* Create the initial thread structure and socket pair */
320 if (!(teb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
322 /* Remember TEB selector of initial process for emergency use */
323 SYSLEVEL_EmergencyTeb = teb->teb_sel;
325 /* Create the system and process heaps */
326 if (!HEAP_CreateSystemHeap()) return FALSE;
327 initial_pdb.heap = HeapCreate( HEAP_GROWABLE, 0, 0 );
329 /* Create the idle event for the initial process
330 FIXME 1: Shouldn't we call UserSignalProc for the initial process too?
331 FIXME 2: It seems to me that the initial pdb becomes never freed, so I don't now
332 where to release the idle event for the initial process.
334 initial_pdb.idle_event = CreateEventA ( NULL, TRUE, FALSE, NULL );
335 initial_pdb.idle_event = ConvertToGlobalHandle ( initial_pdb.idle_event );
337 /* Initialize signal handling */
338 if (!SIGNAL_Init()) return FALSE;
340 /* Create the environment DB of the first process */
341 if (!PROCESS_CreateEnvDB()) return FALSE;
343 /* Create the SEGPTR heap */
344 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
346 /* Initialize the first process critical section */
347 InitializeCriticalSection( &initial_pdb.crit_section );
349 return TRUE;
353 /***********************************************************************
354 * PROCESS_Start
356 * Startup routine of a new process. Called in the context of the new process.
358 void PROCESS_Start(void)
360 struct init_process_done_request *req = get_req_buffer();
361 int debugged;
362 UINT cmdShow = SW_SHOWNORMAL;
363 LPTHREAD_START_ROUTINE entry = NULL;
364 PDB *pdb = PROCESS_Current();
365 NE_MODULE *pModule = NE_GetPtr( pdb->module );
366 LPCSTR filename = ((OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo))->szPathName;
367 IMAGE_OPTIONAL_HEADER *header = !pModule->module32? NULL :
368 &PE_HEADER(pModule->module32)->OptionalHeader;
370 /* Get process type */
371 enum { PROC_DOS, PROC_WIN16, PROC_WIN32 } type;
372 if ( pdb->flags & PDB32_DOS_PROC )
373 type = PROC_DOS;
374 else if ( pdb->flags & PDB32_WIN16_PROC )
375 type = PROC_WIN16;
376 else
377 type = PROC_WIN32;
379 /* Initialize the critical section */
380 InitializeCriticalSection( &pdb->crit_section );
382 /* Create the heap */
383 if (!(pdb->heap = GetProcessHeap()))
385 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE,
386 header? header->SizeOfHeapReserve : 0x10000,
387 header? header->SizeOfHeapCommit : 0 )))
388 goto error;
391 /* Create the environment db */
392 if (!PROCESS_CreateEnvDB()) goto error;
394 /* Create a task for this process */
395 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
396 cmdShow = pdb->env_db->startup_info->wShowWindow;
397 if (!TASK_Create( pModule, cmdShow ))
398 goto error;
400 /* Load all process modules */
401 switch ( type )
403 case PROC_WIN16:
404 if ( !NE_InitProcess( pModule ) )
405 goto error;
406 break;
408 case PROC_WIN32:
409 /* Create 32-bit MODREF */
410 if ( !PE_CreateModule( pModule->module32, filename, 0, FALSE ) )
411 goto error;
413 /* Increment EXE refcount */
414 assert( pdb->exe_modref );
415 pdb->exe_modref->refCount++;
417 /* Retrieve entry point address */
418 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
419 OptionalHeader.AddressOfEntryPoint);
420 break;
422 case PROC_DOS:
423 /* FIXME: move DOS startup code here */
424 break;
428 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
429 * context of the parent process. Actually, the USER signal proc
430 * doesn't really care about that, but it *does* require that the
431 * startup parameters are correctly set up, so that GetProcessDword
432 * works. Furthermore, before calling the USER signal proc the
433 * 16-bit stack must be set up, which it is only after TASK_Create
434 * in the case of a 16-bit process. Thus, we send the signal here.
437 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0 );
438 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
439 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0 );
440 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0 );
442 /* Signal the parent process to continue */
443 req->module = (void *)pModule->module32;
444 req->entry = entry;
445 server_call( REQ_INIT_PROCESS_DONE );
446 debugged = req->debugged;
448 if ( (pdb->flags & PDB32_CONSOLE_PROC) || (pdb->flags & PDB32_DOS_PROC) )
449 AllocConsole();
451 /* Perform Win32 specific process initialization */
452 if ( type == PROC_WIN32 )
454 EnterCriticalSection( &pdb->crit_section );
456 PE_InitTls();
457 MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
459 LeaveCriticalSection( &pdb->crit_section );
462 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
463 if ( type != PROC_WIN16 && (pdb->flags & PDB32_CONSOLE_PROC))
464 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0 );
466 switch ( type )
468 case PROC_DOS:
469 TRACE_(relay)( "Starting DOS process\n" );
470 DOSVM_Enter( NULL );
471 ERR_(relay)( "DOSVM_Enter returned; should not happen!\n" );
472 ExitProcess( 0 );
474 case PROC_WIN16:
475 TRACE_(relay)( "Starting Win16 process\n" );
476 TASK_CallToStart();
477 ERR_(relay)( "TASK_CallToStart returned; should not happen!\n" );
478 ExitProcess( 0 );
480 case PROC_WIN32:
481 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
482 if (debugged) DbgBreakPoint();
483 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
484 * Dunno about other OSs */
485 ExitProcess( entry(NULL) );
488 error:
489 ExitProcess( GetLastError() );
493 /***********************************************************************
494 * PROCESS_Create
496 * Create a new process database and associated info.
498 PDB *PROCESS_Create( NE_MODULE *pModule, HFILE hFile, LPCSTR cmd_line, LPCSTR env,
499 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
500 BOOL inherit, DWORD flags, STARTUPINFOA *startup,
501 PROCESS_INFORMATION *info )
503 HANDLE handles[2], load_done_evt = 0;
504 DWORD exitcode, size;
505 BOOL alloc_stack16;
506 int fd = -1;
507 struct new_process_request *req = get_req_buffer();
508 TEB *teb = NULL;
509 PDB *parent = PROCESS_Current();
510 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
512 if (!pdb) return NULL;
513 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
514 if (!(load_done_evt = CreateEventA( NULL, TRUE, FALSE, NULL ))) goto error;
516 /* Create the process on the server side */
518 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
519 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
520 req->inherit_all = inherit;
521 req->create_flags = flags;
522 req->start_flags = startup->dwFlags;
523 req->exe_file = hFile;
524 req->event = load_done_evt;
525 if (startup->dwFlags & STARTF_USESTDHANDLES)
527 req->hstdin = startup->hStdInput;
528 req->hstdout = startup->hStdOutput;
529 req->hstderr = startup->hStdError;
531 else
533 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
534 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
535 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
537 req->cmd_show = startup->wShowWindow;
538 req->env_ptr = (void*)env; /* FIXME: hack */
539 lstrcpynA( req->cmdline, cmd_line, server_remaining(req->cmdline) );
540 if (server_call_fd( REQ_NEW_PROCESS, -1, &fd )) goto error;
541 pdb->server_pid = req->pid;
542 info->hProcess = req->phandle;
543 info->dwProcessId = (DWORD)req->pid;
544 info->hThread = req->thandle;
545 info->dwThreadId = (DWORD)req->tid;
547 if (pModule->module32) /* Win32 process */
549 IMAGE_OPTIONAL_HEADER *header = &PE_HEADER(pModule->module32)->OptionalHeader;
550 size = header->SizeOfStackReserve;
551 if (header->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
552 pdb->flags |= PDB32_CONSOLE_PROC;
553 alloc_stack16 = TRUE;
555 else if (!pModule->dos_image) /* Win16 process */
557 alloc_stack16 = FALSE;
558 size = 0;
559 pdb->flags |= PDB32_WIN16_PROC;
561 else /* DOS process */
563 alloc_stack16 = FALSE;
564 size = 0;
565 pdb->flags |= PDB32_DOS_PROC;
568 /* Create the main thread */
570 if (!(teb = THREAD_Create( pdb, req->tid, fd, flags & CREATE_SUSPENDED,
571 size, alloc_stack16 ))) goto error;
572 teb->tid = (void *)info->dwThreadId;
573 teb->startup = PROCESS_Start;
574 fd = -1; /* don't close it */
576 /* Pass module to new process (FIXME: hack) */
577 pdb->module = pModule->self;
578 SYSDEPS_SpawnThread( teb );
580 /* Wait until process is initialized (or initialization failed) */
581 handles[0] = info->hProcess;
582 handles[1] = load_done_evt;
584 switch ( WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) )
586 default:
587 ERR( "WaitForMultipleObjects failed\n" );
588 break;
590 case 0:
591 /* Child initialization code returns error condition as exitcode */
592 if ( GetExitCodeProcess( info->hProcess, &exitcode ) )
593 SetLastError( exitcode );
594 goto error;
596 case 1:
597 /* Get 16-bit task up and running */
598 if ( pdb->flags & PDB32_WIN16_PROC )
600 /* Post event to start the task */
601 PostEvent16( pdb->task );
603 /* If we ourselves are a 16-bit task, we Yield() directly. */
604 if ( parent->flags & PDB32_WIN16_PROC )
605 OldYield16();
607 break;
610 CloseHandle( load_done_evt );
611 load_done_evt = 0;
613 return pdb;
615 error:
616 if (load_done_evt) CloseHandle( load_done_evt );
617 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
618 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
619 PROCESS_FreePDB( pdb );
620 if (fd != -1) close( fd );
621 return NULL;
625 /***********************************************************************
626 * ExitProcess (KERNEL32.100)
628 void WINAPI ExitProcess( DWORD status )
630 struct terminate_process_request *req = get_req_buffer();
632 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
633 TASK_KillTask( 0 );
635 /* send the exit code to the server */
636 req->handle = GetCurrentProcess();
637 req->exit_code = status;
638 server_call( REQ_TERMINATE_PROCESS );
639 /* FIXME: need separate address spaces for that */
640 /* exit( status ); */
641 SYSDEPS_ExitThread( status );
644 /***********************************************************************
645 * ExitProcess16 (KERNEL.466)
647 void WINAPI ExitProcess16( WORD status )
649 SYSLEVEL_ReleaseWin16Lock();
650 ExitProcess( status );
653 /******************************************************************************
654 * TerminateProcess (KERNEL32.684)
656 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
658 BOOL ret;
659 struct terminate_process_request *req = get_req_buffer();
660 req->handle = handle;
661 req->exit_code = exit_code;
662 if ((ret = !server_call( REQ_TERMINATE_PROCESS )) && req->self) exit( exit_code );
663 return ret;
667 /***********************************************************************
668 * GetProcessDword (KERNEL32.18) (KERNEL.485)
669 * 'Of course you cannot directly access Windows internal structures'
671 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
673 PDB *process = PROCESS_IdToPDB( dwProcessID );
674 TDB *pTask;
675 DWORD x, y;
677 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
678 if ( !process ) return 0;
680 switch ( offset )
682 case GPD_APP_COMPAT_FLAGS:
683 pTask = (TDB *)GlobalLock16( process->task );
684 return pTask? pTask->compat_flags : 0;
686 case GPD_LOAD_DONE_EVENT:
687 return process->load_done_evt;
689 case GPD_HINSTANCE16:
690 pTask = (TDB *)GlobalLock16( process->task );
691 return pTask? pTask->hInstance : 0;
693 case GPD_WINDOWS_VERSION:
694 pTask = (TDB *)GlobalLock16( process->task );
695 return pTask? pTask->version : 0;
697 case GPD_THDB:
698 if ( process != PROCESS_Current() ) return 0;
699 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
701 case GPD_PDB:
702 return (DWORD)process;
704 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
705 return process->env_db->startup_info->hStdOutput;
707 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
708 return process->env_db->startup_info->hStdInput;
710 case GPD_STARTF_SHOWWINDOW:
711 return process->env_db->startup_info->wShowWindow;
713 case GPD_STARTF_SIZE:
714 x = process->env_db->startup_info->dwXSize;
715 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
716 y = process->env_db->startup_info->dwYSize;
717 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
718 return MAKELONG( x, y );
720 case GPD_STARTF_POSITION:
721 x = process->env_db->startup_info->dwX;
722 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
723 y = process->env_db->startup_info->dwY;
724 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
725 return MAKELONG( x, y );
727 case GPD_STARTF_FLAGS:
728 return process->env_db->startup_info->dwFlags;
730 case GPD_PARENT:
731 return process->parent? (DWORD)process->parent->server_pid : 0;
733 case GPD_FLAGS:
734 return process->flags;
736 case GPD_USERDATA:
737 return process->process_dword;
739 default:
740 ERR_(win32)("Unknown offset %d\n", offset );
741 return 0;
745 /***********************************************************************
746 * SetProcessDword (KERNEL.484)
747 * 'Of course you cannot directly access Windows internal structures'
749 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
751 PDB *process = PROCESS_IdToPDB( dwProcessID );
753 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
754 if ( !process ) return;
756 switch ( offset )
758 case GPD_APP_COMPAT_FLAGS:
759 case GPD_LOAD_DONE_EVENT:
760 case GPD_HINSTANCE16:
761 case GPD_WINDOWS_VERSION:
762 case GPD_THDB:
763 case GPD_PDB:
764 case GPD_STARTF_SHELLDATA:
765 case GPD_STARTF_HOTKEY:
766 case GPD_STARTF_SHOWWINDOW:
767 case GPD_STARTF_SIZE:
768 case GPD_STARTF_POSITION:
769 case GPD_STARTF_FLAGS:
770 case GPD_PARENT:
771 case GPD_FLAGS:
772 ERR_(win32)("Not allowed to modify offset %d\n", offset );
773 break;
775 case GPD_USERDATA:
776 process->process_dword = value;
777 break;
779 default:
780 ERR_(win32)("Unknown offset %d\n", offset );
781 break;
786 /***********************************************************************
787 * GetCurrentProcess (KERNEL32.198)
789 HANDLE WINAPI GetCurrentProcess(void)
791 return CURRENT_PROCESS_PSEUDOHANDLE;
795 /*********************************************************************
796 * OpenProcess (KERNEL32.543)
798 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
800 HANDLE ret = 0;
801 struct open_process_request *req = get_req_buffer();
803 req->pid = (void *)id;
804 req->access = access;
805 req->inherit = inherit;
806 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
807 return ret;
810 /*********************************************************************
811 * MapProcessHandle (KERNEL.483)
813 DWORD WINAPI MapProcessHandle( HANDLE handle )
815 DWORD ret = 0;
816 struct get_process_info_request *req = get_req_buffer();
817 req->handle = handle;
818 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
819 return ret;
822 /***********************************************************************
823 * GetCurrentProcessId (KERNEL32.199)
825 DWORD WINAPI GetCurrentProcessId(void)
827 return (DWORD)PROCESS_Current()->server_pid;
831 /***********************************************************************
832 * GetThreadLocale (KERNEL32.295)
834 LCID WINAPI GetThreadLocale(void)
836 return PROCESS_Current()->locale;
840 /***********************************************************************
841 * SetPriorityClass (KERNEL32.503)
843 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
845 struct set_process_info_request *req = get_req_buffer();
846 req->handle = hprocess;
847 req->priority = priorityclass;
848 req->mask = SET_PROCESS_INFO_PRIORITY;
849 return !server_call( REQ_SET_PROCESS_INFO );
853 /***********************************************************************
854 * GetPriorityClass (KERNEL32.250)
856 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
858 DWORD ret = 0;
859 struct get_process_info_request *req = get_req_buffer();
860 req->handle = hprocess;
861 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
862 return ret;
866 /***********************************************************************
867 * SetProcessAffinityMask (KERNEL32.662)
869 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
871 struct set_process_info_request *req = get_req_buffer();
872 req->handle = hProcess;
873 req->affinity = affmask;
874 req->mask = SET_PROCESS_INFO_AFFINITY;
875 return !server_call( REQ_SET_PROCESS_INFO );
878 /**********************************************************************
879 * GetProcessAffinityMask (KERNEL32.373)
881 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
882 LPDWORD lpProcessAffinityMask,
883 LPDWORD lpSystemAffinityMask )
885 BOOL ret = FALSE;
886 struct get_process_info_request *req = get_req_buffer();
887 req->handle = hProcess;
888 if (!server_call( REQ_GET_PROCESS_INFO ))
890 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
891 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
892 ret = TRUE;
894 return ret;
898 /***********************************************************************
899 * GetStdHandle (KERNEL32.276)
901 HANDLE WINAPI GetStdHandle( DWORD std_handle )
903 PDB *pdb = PROCESS_Current();
905 switch(std_handle)
907 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
908 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
909 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
911 SetLastError( ERROR_INVALID_PARAMETER );
912 return INVALID_HANDLE_VALUE;
916 /***********************************************************************
917 * SetStdHandle (KERNEL32.506)
919 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
921 PDB *pdb = PROCESS_Current();
922 /* FIXME: should we close the previous handle? */
923 switch(std_handle)
925 case STD_INPUT_HANDLE:
926 pdb->env_db->hStdin = handle;
927 return TRUE;
928 case STD_OUTPUT_HANDLE:
929 pdb->env_db->hStdout = handle;
930 return TRUE;
931 case STD_ERROR_HANDLE:
932 pdb->env_db->hStderr = handle;
933 return TRUE;
935 SetLastError( ERROR_INVALID_PARAMETER );
936 return FALSE;
939 /***********************************************************************
940 * GetProcessVersion (KERNEL32)
942 DWORD WINAPI GetProcessVersion( DWORD processid )
944 TDB *pTask;
945 PDB *pdb = PROCESS_IdToPDB( processid );
947 if (!pdb) return 0;
948 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
949 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
952 /***********************************************************************
953 * GetProcessFlags (KERNEL32)
955 DWORD WINAPI GetProcessFlags( DWORD processid )
957 PDB *pdb = PROCESS_IdToPDB( processid );
958 if (!pdb) return 0;
959 return pdb->flags;
962 /***********************************************************************
963 * SetProcessWorkingSetSize [KERNEL32.662]
964 * Sets the min/max working set sizes for a specified process.
966 * PARAMS
967 * hProcess [I] Handle to the process of interest
968 * minset [I] Specifies minimum working set size
969 * maxset [I] Specifies maximum working set size
971 * RETURNS STD
973 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
974 DWORD maxset)
976 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
977 if(( minset == -1) && (maxset == -1)) {
978 /* Trim the working set to zero */
979 /* Swap the process out of physical RAM */
981 return TRUE;
984 /***********************************************************************
985 * GetProcessWorkingSetSize (KERNEL32)
987 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
988 LPDWORD maxset)
990 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
991 /* 32 MB working set size */
992 if (minset) *minset = 32*1024*1024;
993 if (maxset) *maxset = 32*1024*1024;
994 return TRUE;
997 /***********************************************************************
998 * SetProcessShutdownParameters (KERNEL32)
1000 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1001 * Now tracks changes made (but does not act on these changes)
1002 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1003 * It really shouldn't be here, but I'll move it when it's been checked!
1005 #define SHUTDOWN_NORETRY 1
1006 static unsigned int shutdown_noretry = 0;
1007 static unsigned int shutdown_priority = 0x280L;
1008 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1010 if (flags & SHUTDOWN_NORETRY)
1011 shutdown_noretry = 1;
1012 else
1013 shutdown_noretry = 0;
1014 if (level > 0x100L && level < 0x3FFL)
1015 shutdown_priority = level;
1016 else
1018 ERR("invalid priority level 0x%08lx\n", level);
1019 return FALSE;
1021 return TRUE;
1025 /***********************************************************************
1026 * GetProcessShutdownParameters (KERNEL32)
1029 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1030 LPDWORD lpdwFlags )
1032 (*lpdwLevel) = shutdown_priority;
1033 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1034 return TRUE;
1036 /***********************************************************************
1037 * SetProcessPriorityBoost (KERNEL32)
1039 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1041 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1042 /* Say we can do it. I doubt the program will notice that we don't. */
1043 return TRUE;
1047 /***********************************************************************
1048 * ReadProcessMemory (KERNEL32)
1050 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1051 LPDWORD bytes_read )
1053 struct read_process_memory_request *req = get_req_buffer();
1054 unsigned int offset = (unsigned int)addr % sizeof(int);
1055 unsigned int max = server_remaining( req->data ); /* max length in one request */
1056 unsigned int pos;
1058 if (bytes_read) *bytes_read = size;
1060 /* first time, read total length to check for permissions */
1061 req->handle = process;
1062 req->addr = (char *)addr - offset;
1063 req->len = (size + offset + sizeof(int) - 1) / sizeof(int);
1064 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1066 if (size <= max - offset)
1068 memcpy( buffer, (char *)req->data + offset, size );
1069 return TRUE;
1072 /* now take care of the remaining data */
1073 memcpy( buffer, (char *)req->data + offset, max - offset );
1074 pos = max - offset;
1075 size -= pos;
1076 while (size)
1078 if (max > size) max = size;
1079 req->handle = process;
1080 req->addr = (char *)addr + pos;
1081 req->len = (max + sizeof(int) - 1) / sizeof(int);
1082 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1083 memcpy( (char *)buffer + pos, (char *)req->data, max );
1084 size -= max;
1085 pos += max;
1087 return TRUE;
1089 error:
1090 if (bytes_read) *bytes_read = 0;
1091 return FALSE;
1095 /***********************************************************************
1096 * WriteProcessMemory (KERNEL32)
1098 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1099 LPDWORD bytes_written )
1101 unsigned int first_offset, last_offset;
1102 struct write_process_memory_request *req = get_req_buffer();
1103 unsigned int max = server_remaining( req->data ); /* max length in one request */
1104 unsigned int pos, last_mask;
1106 if (!size)
1108 SetLastError( ERROR_INVALID_PARAMETER );
1109 return FALSE;
1111 if (bytes_written) *bytes_written = size;
1113 /* compute the mask for the first int */
1114 req->first_mask = ~0;
1115 first_offset = (unsigned int)addr % sizeof(int);
1116 memset( &req->first_mask, 0, first_offset );
1118 /* compute the mask for the last int */
1119 last_offset = (size + first_offset) % sizeof(int);
1120 last_mask = 0;
1121 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1123 req->handle = process;
1124 req->addr = (char *)addr - first_offset;
1125 /* for the first request, use the total length */
1126 req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1128 if (size + first_offset < max) /* we can do it in one round */
1130 memcpy( (char *)req->data + first_offset, buffer, size );
1131 req->last_mask = last_mask;
1132 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1133 return TRUE;
1136 /* needs multiple server calls */
1138 memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1139 req->last_mask = ~0;
1140 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1141 pos = max - first_offset;
1142 size -= pos;
1143 while (size)
1145 if (size <= max) /* last one */
1147 req->last_mask = last_mask;
1148 max = size;
1150 req->handle = process;
1151 req->addr = (char *)addr + pos;
1152 req->len = (max + sizeof(int) - 1) / sizeof(int);
1153 req->first_mask = ~0;
1154 memcpy( req->data, (char *) buffer + pos, max );
1155 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1156 pos += max;
1157 size -= max;
1159 return TRUE;
1161 error:
1162 if (bytes_written) *bytes_written = 0;
1163 return FALSE;
1168 /***********************************************************************
1169 * RegisterServiceProcess (KERNEL, KERNEL32)
1171 * A service process calls this function to ensure that it continues to run
1172 * even after a user logged off.
1174 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1176 /* I don't think that Wine needs to do anything in that function */
1177 return 1; /* success */
1180 /***********************************************************************
1181 * GetExitCodeProcess [KERNEL32.325]
1183 * Gets termination status of specified process
1185 * RETURNS
1186 * Success: TRUE
1187 * Failure: FALSE
1189 BOOL WINAPI GetExitCodeProcess(
1190 HANDLE hProcess, /* [I] handle to the process */
1191 LPDWORD lpExitCode) /* [O] address to receive termination status */
1193 BOOL ret = FALSE;
1194 struct get_process_info_request *req = get_req_buffer();
1195 req->handle = hProcess;
1196 if (!server_call( REQ_GET_PROCESS_INFO ))
1198 if (lpExitCode) *lpExitCode = req->exit_code;
1199 ret = TRUE;
1201 return ret;
1205 /***********************************************************************
1206 * SetErrorMode (KERNEL32.486)
1208 UINT WINAPI SetErrorMode( UINT mode )
1210 UINT old = PROCESS_Current()->error_mode;
1211 PROCESS_Current()->error_mode = mode;
1212 return old;