Made sleep_on usable from all requests.
[wine/multimedia.git] / scheduler / process.c
blobd9ff8fd1291ea01ea9def97f863265f55829a0f7
1 /*
2 * Win32 processes
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include "wine/winbase16.h"
12 #include "wine/exception.h"
13 #include "process.h"
14 #include "module.h"
15 #include "neexe.h"
16 #include "file.h"
17 #include "global.h"
18 #include "heap.h"
19 #include "task.h"
20 #include "ldt.h"
21 #include "syslevel.h"
22 #include "thread.h"
23 #include "winerror.h"
24 #include "pe_image.h"
25 #include "server.h"
26 #include "options.h"
27 #include "callback.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(process)
31 DECLARE_DEBUG_CHANNEL(relay)
32 DECLARE_DEBUG_CHANNEL(win32)
35 /* The initial process PDB */
36 static PDB initial_pdb;
38 static PDB *PROCESS_First = &initial_pdb;
40 /* Pointer to debugger callback routine */
41 void (*TASK_AddTaskEntryBreakpoint)( HTASK16 hTask ) = NULL;
44 /***********************************************************************
45 * PROCESS_WalkProcess
47 void PROCESS_WalkProcess(void)
49 PDB *pdb;
50 char *name;
52 pdb = PROCESS_First;
53 MESSAGE( " pid PDB #th modref module \n" );
54 while(pdb)
56 if (pdb == &initial_pdb)
57 name = "initial PDB";
58 else
59 name = (pdb->exe_modref) ? pdb->exe_modref->filename : "";
61 MESSAGE( " %8p %8p %5d %8p %s\n", pdb->server_pid, pdb,
62 pdb->threads, pdb->exe_modref, name);
63 pdb = pdb->next;
65 return;
68 /***********************************************************************
69 * PROCESS_Initial
71 * FIXME: This works only while running all processes in the same
72 * address space (or, at least, the initial process is mapped
73 * into all address spaces as is KERNEL32 in Windows 95)
76 PDB *PROCESS_Initial(void)
78 return &initial_pdb;
81 /***********************************************************************
82 * PROCESS_IsCurrent
84 * Check if a handle is to the current process
86 BOOL PROCESS_IsCurrent( HANDLE handle )
88 struct get_process_info_request *req = get_req_buffer();
89 req->handle = handle;
90 return (!server_call( REQ_GET_PROCESS_INFO ) &&
91 (req->pid == PROCESS_Current()->server_pid));
95 /***********************************************************************
96 * PROCESS_IdToPDB
98 * Convert a process id to a PDB, making sure it is valid.
100 PDB *PROCESS_IdToPDB( DWORD id )
102 PDB *pdb;
104 if (!id) return PROCESS_Current();
105 pdb = PROCESS_First;
106 while (pdb)
108 if ((DWORD)pdb->server_pid == id) return pdb;
109 pdb = pdb->next;
111 SetLastError( ERROR_INVALID_PARAMETER );
112 return NULL;
116 /***********************************************************************
117 * PROCESS_CallUserSignalProc
119 * FIXME: Some of the signals aren't sent correctly!
121 * The exact meaning of the USER signals is undocumented, but this
122 * should cover the basic idea:
124 * USIG_DLL_UNLOAD_WIN16
125 * This is sent when a 16-bit module is unloaded.
127 * USIG_DLL_UNLOAD_WIN32
128 * This is sent when a 32-bit module is unloaded.
130 * USIG_DLL_UNLOAD_ORPHANS
131 * This is sent after the last Win3.1 module is unloaded,
132 * to allow removal of orphaned menus.
134 * USIG_FAULT_DIALOG_PUSH
135 * USIG_FAULT_DIALOG_POP
136 * These are called to allow USER to prepare for displaying a
137 * fault dialog, even though the fault might have happened while
138 * inside a USER critical section.
140 * USIG_THREAD_INIT
141 * This is called from the context of a new thread, as soon as it
142 * has started to run.
144 * USIG_THREAD_EXIT
145 * This is called, still in its context, just before a thread is
146 * about to terminate.
148 * USIG_PROCESS_CREATE
149 * This is called, in the parent process context, after a new process
150 * has been created.
152 * USIG_PROCESS_INIT
153 * This is called in the new process context, just after the main thread
154 * has started execution (after the main thread's USIG_THREAD_INIT has
155 * been sent).
157 * USIG_PROCESS_LOADED
158 * This is called after the executable file has been loaded into the
159 * new process context.
161 * USIG_PROCESS_RUNNING
162 * This is called immediately before the main entry point is called.
164 * USIG_PROCESS_EXIT
165 * This is called in the context of a process that is about to
166 * terminate (but before the last thread's USIG_THREAD_EXIT has
167 * been sent).
169 * USIG_PROCESS_DESTROY
170 * This is called after a process has terminated.
173 * The meaning of the dwFlags bits is as follows:
175 * USIG_FLAGS_WIN32
176 * Current process is 32-bit.
178 * USIG_FLAGS_GUI
179 * Current process is a (Win32) GUI process.
181 * USIG_FLAGS_FEEDBACK
182 * Current process needs 'feedback' (determined from the STARTUPINFO
183 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
185 * USIG_FLAGS_FAULT
186 * The signal is being sent due to a fault.
188 void PROCESS_CallUserSignalProc( UINT uCode, DWORD dwThreadId, HMODULE hModule )
190 DWORD flags = PROCESS_Current()->flags;
191 DWORD startup_flags = PROCESS_Current()->env_db->startup_info->dwFlags;
192 DWORD dwFlags = 0;
194 /* Determine dwFlags */
196 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
198 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
200 if ( dwFlags & USIG_FLAGS_GUI )
202 /* Feedback defaults to ON */
203 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
204 dwFlags |= USIG_FLAGS_FEEDBACK;
206 else
208 /* Feedback defaults to OFF */
209 if (startup_flags & STARTF_FORCEONFEEDBACK)
210 dwFlags |= USIG_FLAGS_FEEDBACK;
213 /* Convert module handle to 16-bit */
215 if ( HIWORD( hModule ) )
216 hModule = MapHModuleLS( hModule );
218 /* Call USER signal proc */
220 if ( Callout.UserSignalProc )
222 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
223 Callout.UserSignalProc( uCode, dwThreadId, dwFlags, hModule );
224 else
225 Callout.UserSignalProc( uCode, GetCurrentProcessId(), dwFlags, hModule );
229 /***********************************************************************
230 * PROCESS_CreateEnvDB
232 * Create the env DB for a newly started process.
234 static BOOL PROCESS_CreateEnvDB(void)
236 struct init_process_request *req = get_req_buffer();
237 STARTUPINFOA *startup;
238 ENVDB *env_db;
239 char cmd_line[4096];
240 PDB *pdb = PROCESS_Current();
242 /* Allocate the env DB */
244 if (!(env_db = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
245 return FALSE;
246 pdb->env_db = env_db;
247 InitializeCriticalSection( &env_db->section );
249 /* Allocate and fill the startup info */
250 if (!(startup = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
251 return FALSE;
252 env_db->startup_info = startup;
254 /* Retrieve startup info from the server */
256 if (server_call( REQ_INIT_PROCESS )) return FALSE;
257 startup->dwFlags = req->start_flags;
258 startup->wShowWindow = req->cmd_show;
259 env_db->hStdin = startup->hStdInput = req->hstdin;
260 env_db->hStdout = startup->hStdOutput = req->hstdout;
261 env_db->hStderr = startup->hStdError = req->hstderr;
262 lstrcpynA( cmd_line, req->cmdline, sizeof(cmd_line) );
264 /* Copy the parent environment */
266 if (!ENV_InheritEnvironment( pdb, req->env_ptr )) return FALSE;
268 /* Copy the command line */
270 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
271 return FALSE;
273 return TRUE;
277 /***********************************************************************
278 * PROCESS_FreePDB
280 * Free a PDB and all associated storage.
282 void PROCESS_FreePDB( PDB *pdb )
284 PDB **pptr = &PROCESS_First;
286 ENV_FreeEnvironment( pdb );
287 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
288 if (*pptr) *pptr = pdb->next;
289 if (pdb->heap && (pdb->heap != pdb->system_heap)) HeapDestroy( pdb->heap );
290 HeapFree( SystemHeap, 0, pdb );
294 /***********************************************************************
295 * PROCESS_CreatePDB
297 * Allocate and fill a PDB structure.
298 * Runs in the context of the parent process.
300 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
302 PDB *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB) );
304 if (!pdb) return NULL;
305 pdb->exit_code = 0x103; /* STILL_ACTIVE */
306 pdb->threads = 1;
307 pdb->running_threads = 1;
308 pdb->ring0_threads = 1;
309 pdb->system_heap = SystemHeap;
310 pdb->parent = parent;
311 pdb->group = pdb;
312 pdb->priority = 8; /* Normal */
313 pdb->heap = pdb->system_heap; /* will be changed later on */
314 pdb->next = PROCESS_First;
315 pdb->winver = 0xffff; /* to be determined */
316 pdb->main_queue = INVALID_HANDLE_VALUE16;
317 PROCESS_First = pdb;
318 return pdb;
322 /***********************************************************************
323 * PROCESS_Init
325 BOOL PROCESS_Init(void)
327 TEB *teb;
328 int server_fd;
330 /* Start the server */
331 server_fd = CLIENT_InitServer();
333 /* Fill the initial process structure */
334 initial_pdb.exit_code = 0x103; /* STILL_ACTIVE */
335 initial_pdb.threads = 1;
336 initial_pdb.running_threads = 1;
337 initial_pdb.ring0_threads = 1;
338 initial_pdb.group = &initial_pdb;
339 initial_pdb.priority = 8; /* Normal */
340 initial_pdb.flags = PDB32_WIN16_PROC;
341 initial_pdb.winver = 0xffff; /* to be determined */
342 initial_pdb.main_queue = INVALID_HANDLE_VALUE16;
344 /* Initialize virtual memory management */
345 if (!VIRTUAL_Init()) return FALSE;
347 /* Create the initial thread structure and socket pair */
348 if (!(teb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
350 /* Remember TEB selector of initial process for emergency use */
351 SYSLEVEL_EmergencyTeb = teb->teb_sel;
353 /* Create the system heap */
354 if (!(SystemHeap = HeapCreate( HEAP_GROWABLE, 0x10000, 0 ))) return FALSE;
355 initial_pdb.system_heap = initial_pdb.heap = SystemHeap;
357 /* Create the idle event for the initial process
358 FIXME 1: Shouldn't we call UserSignalProc for the initial process too?
359 FIXME 2: It seems to me that the initial pdb becomes never freed, so I don't now
360 where to release the idle event for the initial process.
362 initial_pdb.idle_event = CreateEventA ( NULL, TRUE, FALSE, NULL );
363 initial_pdb.idle_event = ConvertToGlobalHandle ( initial_pdb.idle_event );
365 /* Initialize signal handling */
366 if (!SIGNAL_Init()) return FALSE;
368 /* Create the environment DB of the first process */
369 if (!PROCESS_CreateEnvDB()) return FALSE;
371 /* Create the SEGPTR heap */
372 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
374 /* Initialize the first process critical section */
375 InitializeCriticalSection( &initial_pdb.crit_section );
377 return TRUE;
381 /***********************************************************************
382 * PROCESS_Start
384 * Startup routine of a new process. Called in the context of the new process.
386 void PROCESS_Start(void)
388 UINT cmdShow = SW_SHOWNORMAL;
389 LPTHREAD_START_ROUTINE entry = NULL;
390 PDB *pdb = PROCESS_Current();
391 NE_MODULE *pModule = NE_GetPtr( pdb->module );
392 LPCSTR filename = ((OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo))->szPathName;
393 IMAGE_OPTIONAL_HEADER *header = !pModule->module32? NULL :
394 &PE_HEADER(pModule->module32)->OptionalHeader;
396 /* Get process type */
397 enum { PROC_DOS, PROC_WIN16, PROC_WIN32 } type;
398 if ( pdb->flags & PDB32_DOS_PROC )
399 type = PROC_DOS;
400 else if ( pdb->flags & PDB32_WIN16_PROC )
401 type = PROC_WIN16;
402 else
403 type = PROC_WIN32;
405 /* Initialize the critical section */
406 InitializeCriticalSection( &pdb->crit_section );
408 /* Create the heap */
409 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE,
410 header? header->SizeOfHeapReserve : 0x10000,
411 header? header->SizeOfHeapCommit : 0 )))
412 goto error;
413 pdb->heap_list = pdb->heap;
415 /* Create the environment db */
416 if (!PROCESS_CreateEnvDB()) goto error;
418 /* Create a task for this process */
419 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
420 cmdShow = pdb->env_db->startup_info->wShowWindow;
421 if (!TASK_Create( pModule, cmdShow ))
422 goto error;
424 /* Load all process modules */
425 switch ( type )
427 case PROC_WIN16:
428 if ( !NE_InitProcess( pModule ) )
429 goto error;
430 break;
432 case PROC_WIN32:
433 /* Create 32-bit MODREF */
434 if ( !PE_CreateModule( pModule->module32, filename, 0, FALSE ) )
435 goto error;
437 /* Increment EXE refcount */
438 assert( pdb->exe_modref );
439 pdb->exe_modref->refCount++;
441 /* Retrieve entry point address */
442 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
443 OptionalHeader.AddressOfEntryPoint);
444 break;
446 case PROC_DOS:
447 /* FIXME: move DOS startup code here */
448 break;
452 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
453 * context of the parent process. Actually, the USER signal proc
454 * doesn't really care about that, but it *does* require that the
455 * startup parameters are correctly set up, so that GetProcessDword
456 * works. Furthermore, before calling the USER signal proc the
457 * 16-bit stack must be set up, which it is only after TASK_Create
458 * in the case of a 16-bit process. Thus, we send the signal here.
461 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0, 0 );
462 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, GetCurrentThreadId(), 0 );
463 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0, 0 );
464 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0, 0 );
466 /* Signal the parent process to continue */
467 server_call( REQ_INIT_PROCESS_DONE );
469 /* Send all required start-up debugger events */
470 if ( type == PROC_WIN32 && (pdb->flags & PDB32_DEBUGGED) )
472 EnterCriticalSection( &pdb->crit_section );
474 DEBUG_SendCreateProcessEvent( -1 /*FIXME*/, pModule->module32, entry );
475 MODULE_SendLoadDLLEvents();
477 LeaveCriticalSection( &pdb->crit_section );
480 if ( (pdb->flags & PDB32_CONSOLE_PROC) || (pdb->flags & PDB32_DOS_PROC) )
481 AllocConsole();
483 /* Perform Win32 specific process initialization */
484 if ( type == PROC_WIN32 )
486 EnterCriticalSection( &pdb->crit_section );
488 PE_InitTls();
489 MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
491 LeaveCriticalSection( &pdb->crit_section );
494 /* If requested, add entry point breakpoint */
495 if ( Options.debug && TASK_AddTaskEntryBreakpoint )
496 TASK_AddTaskEntryBreakpoint( pdb->task );
498 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
499 if ( type != PROC_WIN16 && (pdb->flags & PDB32_CONSOLE_PROC))
500 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0, 0 );
502 switch ( type )
504 case PROC_DOS:
505 TRACE_(relay)( "Starting DOS process\n" );
506 DOSVM_Enter( NULL );
507 ERR_(relay)( "DOSVM_Enter returned; should not happen!\n" );
508 ExitProcess( 0 );
510 case PROC_WIN16:
511 TRACE_(relay)( "Starting Win16 process\n" );
512 TASK_CallToStart();
513 ERR_(relay)( "TASK_CallToStart returned; should not happen!\n" );
514 ExitProcess( 0 );
516 case PROC_WIN32:
517 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
518 ExitProcess( entry(NULL) );
521 error:
522 ExitProcess( GetLastError() );
526 /***********************************************************************
527 * PROCESS_Create
529 * Create a new process database and associated info.
531 PDB *PROCESS_Create( NE_MODULE *pModule, LPCSTR cmd_line, LPCSTR env,
532 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
533 BOOL inherit, DWORD flags, STARTUPINFOA *startup,
534 PROCESS_INFORMATION *info )
536 HANDLE handles[2], load_done_evt = 0;
537 DWORD exitcode, size;
538 BOOL alloc_stack16;
539 int server_thandle;
540 struct new_process_request *req = get_req_buffer();
541 TEB *teb = NULL;
542 PDB *parent = PROCESS_Current();
543 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
545 if (!pdb) return NULL;
546 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
547 if (!(load_done_evt = CreateEventA( NULL, TRUE, FALSE, NULL ))) goto error;
549 /* Create the process on the server side */
551 req->inherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
552 req->inherit_all = inherit;
553 req->create_flags = flags;
554 req->start_flags = startup->dwFlags;
555 req->event = load_done_evt;
556 if (startup->dwFlags & STARTF_USESTDHANDLES)
558 req->hstdin = startup->hStdInput;
559 req->hstdout = startup->hStdOutput;
560 req->hstderr = startup->hStdError;
562 else
564 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
565 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
566 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
568 req->cmd_show = startup->wShowWindow;
569 req->env_ptr = (void*)env; /* FIXME: hack */
570 lstrcpynA( req->cmdline, cmd_line, server_remaining(req->cmdline) );
571 if (server_call( REQ_NEW_PROCESS )) goto error;
572 pdb->server_pid = req->pid;
573 info->hProcess = req->handle;
574 info->dwProcessId = (DWORD)pdb->server_pid;
576 if ((flags & DEBUG_PROCESS) ||
577 ((parent->flags & PDB32_DEBUGGED) && !(flags & DEBUG_ONLY_THIS_PROCESS)))
578 pdb->flags |= PDB32_DEBUGGED;
580 if (pModule->module32) /* Win32 process */
582 IMAGE_OPTIONAL_HEADER *header = &PE_HEADER(pModule->module32)->OptionalHeader;
583 size = header->SizeOfStackReserve;
584 if (header->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
585 pdb->flags |= PDB32_CONSOLE_PROC;
586 alloc_stack16 = TRUE;
588 else if (!pModule->dos_image) /* Win16 process */
590 alloc_stack16 = FALSE;
591 size = 0;
592 pdb->flags |= PDB32_WIN16_PROC;
594 else /* DOS process */
596 alloc_stack16 = FALSE;
597 size = 0;
598 pdb->flags |= PDB32_DOS_PROC;
601 /* Create the main thread */
603 if (!(teb = THREAD_Create( pdb, flags & CREATE_SUSPENDED, size,
604 alloc_stack16, tsa, &server_thandle ))) goto error;
605 info->hThread = server_thandle;
606 info->dwThreadId = (DWORD)teb->tid;
607 teb->startup = PROCESS_Start;
609 /* Pass module to new process (FIXME: hack) */
610 pdb->module = pModule->self;
611 SYSDEPS_SpawnThread( teb );
613 /* Wait until process is initialized (or initialization failed) */
614 handles[0] = info->hProcess;
615 handles[1] = load_done_evt;
617 switch ( WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) )
619 default:
620 ERR( "WaitForMultipleObjects failed\n" );
621 break;
623 case 0:
624 /* Child initialization code returns error condition as exitcode */
625 if ( GetExitCodeProcess( info->hProcess, &exitcode ) )
626 SetLastError( exitcode );
627 goto error;
629 case 1:
630 /* Get 16-bit task up and running */
631 if ( pdb->flags & PDB32_WIN16_PROC )
633 /* Post event to start the task */
634 PostEvent16( pdb->task );
636 /* If we ourselves are a 16-bit task, we Yield() directly. */
637 if ( parent->flags & PDB32_WIN16_PROC )
638 OldYield16();
640 break;
643 CloseHandle( load_done_evt );
644 load_done_evt = 0;
646 return pdb;
648 error:
649 if (load_done_evt) CloseHandle( load_done_evt );
650 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
651 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
652 PROCESS_FreePDB( pdb );
653 return NULL;
657 /***********************************************************************
658 * ExitProcess (KERNEL32.100)
660 void WINAPI ExitProcess( DWORD status )
662 EnterCriticalSection( &PROCESS_Current()->crit_section );
663 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
664 LeaveCriticalSection( &PROCESS_Current()->crit_section );
666 TASK_KillTask( 0 );
667 TerminateProcess( GetCurrentProcess(), status );
670 /***********************************************************************
671 * ExitProcess16 (KERNEL.466)
673 void WINAPI ExitProcess16( WORD status )
675 SYSLEVEL_ReleaseWin16Lock();
676 ExitProcess( status );
679 /******************************************************************************
680 * TerminateProcess (KERNEL32.684)
682 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
684 struct terminate_process_request *req = get_req_buffer();
685 req->handle = handle;
686 req->exit_code = exit_code;
687 return !server_call( REQ_TERMINATE_PROCESS );
691 /***********************************************************************
692 * GetProcessDword (KERNEL32.18) (KERNEL.485)
693 * 'Of course you cannot directly access Windows internal structures'
695 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
697 PDB *process = PROCESS_IdToPDB( dwProcessID );
698 TDB *pTask;
699 DWORD x, y;
701 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
702 if ( !process ) return 0;
704 switch ( offset )
706 case GPD_APP_COMPAT_FLAGS:
707 pTask = (TDB *)GlobalLock16( process->task );
708 return pTask? pTask->compat_flags : 0;
710 case GPD_LOAD_DONE_EVENT:
711 return process->load_done_evt;
713 case GPD_HINSTANCE16:
714 pTask = (TDB *)GlobalLock16( process->task );
715 return pTask? pTask->hInstance : 0;
717 case GPD_WINDOWS_VERSION:
718 pTask = (TDB *)GlobalLock16( process->task );
719 return pTask? pTask->version : 0;
721 case GPD_THDB:
722 if ( process != PROCESS_Current() ) return 0;
723 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
725 case GPD_PDB:
726 return (DWORD)process;
728 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
729 return process->env_db->startup_info->hStdOutput;
731 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
732 return process->env_db->startup_info->hStdInput;
734 case GPD_STARTF_SHOWWINDOW:
735 return process->env_db->startup_info->wShowWindow;
737 case GPD_STARTF_SIZE:
738 x = process->env_db->startup_info->dwXSize;
739 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
740 y = process->env_db->startup_info->dwYSize;
741 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
742 return MAKELONG( x, y );
744 case GPD_STARTF_POSITION:
745 x = process->env_db->startup_info->dwX;
746 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
747 y = process->env_db->startup_info->dwY;
748 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
749 return MAKELONG( x, y );
751 case GPD_STARTF_FLAGS:
752 return process->env_db->startup_info->dwFlags;
754 case GPD_PARENT:
755 return (DWORD)process->parent->server_pid;
757 case GPD_FLAGS:
758 return process->flags;
760 case GPD_USERDATA:
761 return process->process_dword;
763 default:
764 ERR_(win32)("Unknown offset %d\n", offset );
765 return 0;
769 /***********************************************************************
770 * SetProcessDword (KERNEL.484)
771 * 'Of course you cannot directly access Windows internal structures'
773 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
775 PDB *process = PROCESS_IdToPDB( dwProcessID );
777 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
778 if ( !process ) return;
780 switch ( offset )
782 case GPD_APP_COMPAT_FLAGS:
783 case GPD_LOAD_DONE_EVENT:
784 case GPD_HINSTANCE16:
785 case GPD_WINDOWS_VERSION:
786 case GPD_THDB:
787 case GPD_PDB:
788 case GPD_STARTF_SHELLDATA:
789 case GPD_STARTF_HOTKEY:
790 case GPD_STARTF_SHOWWINDOW:
791 case GPD_STARTF_SIZE:
792 case GPD_STARTF_POSITION:
793 case GPD_STARTF_FLAGS:
794 case GPD_PARENT:
795 case GPD_FLAGS:
796 ERR_(win32)("Not allowed to modify offset %d\n", offset );
797 break;
799 case GPD_USERDATA:
800 process->process_dword = value;
801 break;
803 default:
804 ERR_(win32)("Unknown offset %d\n", offset );
805 break;
810 /***********************************************************************
811 * GetCurrentProcess (KERNEL32.198)
813 HANDLE WINAPI GetCurrentProcess(void)
815 return CURRENT_PROCESS_PSEUDOHANDLE;
819 /*********************************************************************
820 * OpenProcess (KERNEL32.543)
822 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
824 HANDLE ret = 0;
825 struct open_process_request *req = get_req_buffer();
827 req->pid = (void *)id;
828 req->access = access;
829 req->inherit = inherit;
830 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
831 return ret;
834 /*********************************************************************
835 * MapProcessHandle (KERNEL.483)
837 DWORD WINAPI MapProcessHandle( HANDLE handle )
839 DWORD ret = 0;
840 struct get_process_info_request *req = get_req_buffer();
841 req->handle = handle;
842 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
843 return ret;
846 /***********************************************************************
847 * GetCurrentProcessId (KERNEL32.199)
849 DWORD WINAPI GetCurrentProcessId(void)
851 return (DWORD)PROCESS_Current()->server_pid;
855 /***********************************************************************
856 * GetProcessHeap (KERNEL32.259)
858 HANDLE WINAPI GetProcessHeap(void)
860 PDB *pdb = PROCESS_Current();
861 return pdb->heap ? pdb->heap : SystemHeap;
865 /***********************************************************************
866 * GetThreadLocale (KERNEL32.295)
868 LCID WINAPI GetThreadLocale(void)
870 return PROCESS_Current()->locale;
874 /***********************************************************************
875 * SetPriorityClass (KERNEL32.503)
877 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
879 struct set_process_info_request *req = get_req_buffer();
880 req->handle = hprocess;
881 req->priority = priorityclass;
882 req->mask = SET_PROCESS_INFO_PRIORITY;
883 return !server_call( REQ_SET_PROCESS_INFO );
887 /***********************************************************************
888 * GetPriorityClass (KERNEL32.250)
890 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
892 DWORD ret = 0;
893 struct get_process_info_request *req = get_req_buffer();
894 req->handle = hprocess;
895 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
896 return ret;
900 /***********************************************************************
901 * SetProcessAffinityMask (KERNEL32.662)
903 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
905 struct set_process_info_request *req = get_req_buffer();
906 req->handle = hProcess;
907 req->affinity = affmask;
908 req->mask = SET_PROCESS_INFO_AFFINITY;
909 return !server_call( REQ_SET_PROCESS_INFO );
912 /**********************************************************************
913 * GetProcessAffinityMask (KERNEL32.373)
915 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
916 LPDWORD lpProcessAffinityMask,
917 LPDWORD lpSystemAffinityMask )
919 BOOL ret = FALSE;
920 struct get_process_info_request *req = get_req_buffer();
921 req->handle = hProcess;
922 if (!server_call( REQ_GET_PROCESS_INFO ))
924 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
925 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
926 ret = TRUE;
928 return ret;
932 /***********************************************************************
933 * GetStdHandle (KERNEL32.276)
935 HANDLE WINAPI GetStdHandle( DWORD std_handle )
937 PDB *pdb = PROCESS_Current();
939 switch(std_handle)
941 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
942 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
943 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
945 SetLastError( ERROR_INVALID_PARAMETER );
946 return INVALID_HANDLE_VALUE;
950 /***********************************************************************
951 * SetStdHandle (KERNEL32.506)
953 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
955 PDB *pdb = PROCESS_Current();
956 /* FIXME: should we close the previous handle? */
957 switch(std_handle)
959 case STD_INPUT_HANDLE:
960 pdb->env_db->hStdin = handle;
961 return TRUE;
962 case STD_OUTPUT_HANDLE:
963 pdb->env_db->hStdout = handle;
964 return TRUE;
965 case STD_ERROR_HANDLE:
966 pdb->env_db->hStderr = handle;
967 return TRUE;
969 SetLastError( ERROR_INVALID_PARAMETER );
970 return FALSE;
973 /***********************************************************************
974 * GetProcessVersion (KERNEL32)
976 DWORD WINAPI GetProcessVersion( DWORD processid )
978 TDB *pTask;
979 PDB *pdb = PROCESS_IdToPDB( processid );
981 if (!pdb) return 0;
982 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
983 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
986 /***********************************************************************
987 * GetProcessFlags (KERNEL32)
989 DWORD WINAPI GetProcessFlags( DWORD processid )
991 PDB *pdb = PROCESS_IdToPDB( processid );
992 if (!pdb) return 0;
993 return pdb->flags;
996 /***********************************************************************
997 * SetProcessWorkingSetSize [KERNEL32.662]
998 * Sets the min/max working set sizes for a specified process.
1000 * PARAMS
1001 * hProcess [I] Handle to the process of interest
1002 * minset [I] Specifies minimum working set size
1003 * maxset [I] Specifies maximum working set size
1005 * RETURNS STD
1007 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
1008 DWORD maxset)
1010 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
1011 if(( minset == -1) && (maxset == -1)) {
1012 /* Trim the working set to zero */
1013 /* Swap the process out of physical RAM */
1015 return TRUE;
1018 /***********************************************************************
1019 * GetProcessWorkingSetSize (KERNEL32)
1021 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1022 LPDWORD maxset)
1024 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1025 /* 32 MB working set size */
1026 if (minset) *minset = 32*1024*1024;
1027 if (maxset) *maxset = 32*1024*1024;
1028 return TRUE;
1031 /***********************************************************************
1032 * SetProcessShutdownParameters (KERNEL32)
1034 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1035 * Now tracks changes made (but does not act on these changes)
1036 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1037 * It really shouldn't be here, but I'll move it when it's been checked!
1039 #define SHUTDOWN_NORETRY 1
1040 static unsigned int shutdown_noretry = 0;
1041 static unsigned int shutdown_priority = 0x280L;
1042 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1044 if (flags & SHUTDOWN_NORETRY)
1045 shutdown_noretry = 1;
1046 else
1047 shutdown_noretry = 0;
1048 if (level > 0x100L && level < 0x3FFL)
1049 shutdown_priority = level;
1050 else
1052 ERR("invalid priority level 0x%08lx\n", level);
1053 return FALSE;
1055 return TRUE;
1059 /***********************************************************************
1060 * GetProcessShutdownParameters (KERNEL32)
1063 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1064 LPDWORD lpdwFlags )
1066 (*lpdwLevel) = shutdown_priority;
1067 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1068 return TRUE;
1070 /***********************************************************************
1071 * SetProcessPriorityBoost (KERNEL32)
1073 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1075 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1076 /* Say we can do it. I doubt the program will notice that we don't. */
1077 return TRUE;
1081 /***********************************************************************
1082 * ReadProcessMemory (KERNEL32)
1084 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1085 LPDWORD bytes_read )
1087 struct read_process_memory_request *req = get_req_buffer();
1088 unsigned int offset = (unsigned int)addr % sizeof(int);
1089 unsigned int max = server_remaining( req->data ); /* max length in one request */
1090 unsigned int pos;
1092 if (bytes_read) *bytes_read = size;
1094 /* first time, read total length to check for permissions */
1095 req->handle = process;
1096 req->addr = (char *)addr - offset;
1097 req->len = (size + offset + sizeof(int) - 1) / sizeof(int);
1098 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1100 if (size <= max - offset)
1102 memcpy( buffer, (char *)req->data + offset, size );
1103 return TRUE;
1106 /* now take care of the remaining data */
1107 memcpy( buffer, (char *)req->data + offset, max - offset );
1108 pos = max - offset;
1109 size -= pos;
1110 while (size)
1112 if (max > size) max = size;
1113 req->handle = process;
1114 req->addr = (char *)addr + pos;
1115 req->len = (max + sizeof(int) - 1) / sizeof(int);
1116 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1117 memcpy( (char *)buffer + pos, (char *)req->data, max );
1118 size -= max;
1119 pos += max;
1121 return TRUE;
1123 error:
1124 if (bytes_read) *bytes_read = 0;
1125 return FALSE;
1129 /***********************************************************************
1130 * WriteProcessMemory (KERNEL32)
1132 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1133 LPDWORD bytes_written )
1135 unsigned int first_offset, last_offset;
1136 struct write_process_memory_request *req = get_req_buffer();
1137 unsigned int max = server_remaining( req->data ); /* max length in one request */
1138 unsigned int pos, last_mask;
1140 if (!size)
1142 SetLastError( ERROR_INVALID_PARAMETER );
1143 return FALSE;
1145 if (bytes_written) *bytes_written = size;
1147 /* compute the mask for the first int */
1148 req->first_mask = ~0;
1149 first_offset = (unsigned int)addr % sizeof(int);
1150 memset( &req->first_mask, 0, first_offset );
1152 /* compute the mask for the last int */
1153 last_offset = (size + first_offset) % sizeof(int);
1154 last_mask = 0;
1155 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1157 req->handle = process;
1158 req->addr = (char *)addr - first_offset;
1159 /* for the first request, use the total length */
1160 req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1162 if (size + first_offset < max) /* we can do it in one round */
1164 memcpy( (char *)req->data + first_offset, buffer, size );
1165 req->last_mask = last_mask;
1166 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1167 return TRUE;
1170 /* needs multiple server calls */
1172 memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1173 req->last_mask = ~0;
1174 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1175 pos = max - first_offset;
1176 size -= pos;
1177 while (size)
1179 if (size <= max) /* last one */
1181 req->last_mask = last_mask;
1182 max = size;
1184 req->handle = process;
1185 req->addr = (char *)addr + pos;
1186 req->len = (max + sizeof(int) - 1) / sizeof(int);
1187 req->first_mask = ~0;
1188 memcpy( req->data, buffer + pos, max );
1189 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1190 pos += max;
1191 size -= max;
1193 return TRUE;
1195 error:
1196 if (bytes_written) *bytes_written = 0;
1197 return FALSE;
1202 /***********************************************************************
1203 * RegisterServiceProcess (KERNEL, KERNEL32)
1205 * A service process calls this function to ensure that it continues to run
1206 * even after a user logged off.
1208 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1210 /* I don't think that Wine needs to do anything in that function */
1211 return 1; /* success */
1214 /***********************************************************************
1215 * GetExitCodeProcess [KERNEL32.325]
1217 * Gets termination status of specified process
1219 * RETURNS
1220 * Success: TRUE
1221 * Failure: FALSE
1223 BOOL WINAPI GetExitCodeProcess(
1224 HANDLE hProcess, /* [I] handle to the process */
1225 LPDWORD lpExitCode) /* [O] address to receive termination status */
1227 BOOL ret = FALSE;
1228 struct get_process_info_request *req = get_req_buffer();
1229 req->handle = hProcess;
1230 if (!server_call( REQ_GET_PROCESS_INFO ))
1232 if (lpExitCode) *lpExitCode = req->exit_code;
1233 ret = TRUE;
1235 return ret;
1239 /***********************************************************************
1240 * GetProcessHeaps [KERNEL32.376]
1242 DWORD WINAPI GetProcessHeaps(DWORD nrofheaps,HANDLE *heaps) {
1243 FIXME_(win32)("(%ld,%p), incomplete implementation.\n",nrofheaps,heaps);
1245 if (nrofheaps) {
1246 heaps[0] = GetProcessHeap();
1247 /* ... probably SystemHeap too ? */
1248 return 1;
1250 /* number of available heaps */
1251 return 1;
1255 /***********************************************************************
1256 * SetErrorMode (KERNEL32.486)
1258 UINT WINAPI SetErrorMode( UINT mode )
1260 UINT old = PROCESS_Current()->error_mode;
1261 PROCESS_Current()->error_mode = mode;
1262 return old;