Some stubs added.
[wine/dcerpc.git] / scheduler / process.c
blob0ffa8019e797344cce237408e1394d220d606137
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 "process.h"
12 #include "module.h"
13 #include "neexe.h"
14 #include "file.h"
15 #include "global.h"
16 #include "heap.h"
17 #include "task.h"
18 #include "ldt.h"
19 #include "syslevel.h"
20 #include "thread.h"
21 #include "winerror.h"
22 #include "pe_image.h"
23 #include "task.h"
24 #include "server.h"
25 #include "callback.h"
26 #include "debugtools.h"
28 DECLARE_DEBUG_CHANNEL(process)
29 DECLARE_DEBUG_CHANNEL(relay)
30 DECLARE_DEBUG_CHANNEL(win32)
33 /* The initial process PDB */
34 static PDB initial_pdb;
36 static PDB *PROCESS_First = &initial_pdb;
38 /***********************************************************************
39 * PROCESS_Current
41 PDB *PROCESS_Current(void)
43 return THREAD_Current()->process;
46 /***********************************************************************
47 * PROCESS_Initial
49 * FIXME: This works only while running all processes in the same
50 * address space (or, at least, the initial process is mapped
51 * into all address spaces as is KERNEL32 in Windows 95)
54 PDB *PROCESS_Initial(void)
56 return &initial_pdb;
59 /***********************************************************************
60 * PROCESS_QueryInfo
62 * Retrieve information about a process
64 static BOOL PROCESS_QueryInfo( HANDLE handle,
65 struct get_process_info_reply *reply )
67 struct get_process_info_request req;
68 req.handle = handle;
69 CLIENT_SendRequest( REQ_GET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
70 return !CLIENT_WaitSimpleReply( reply, sizeof(*reply), NULL );
73 /***********************************************************************
74 * PROCESS_IsCurrent
76 * Check if a handle is to the current process
78 BOOL PROCESS_IsCurrent( HANDLE handle )
80 struct get_process_info_reply reply;
81 return (PROCESS_QueryInfo( handle, &reply ) &&
82 (reply.pid == PROCESS_Current()->server_pid));
86 /***********************************************************************
87 * PROCESS_IdToPDB
89 * Convert a process id to a PDB, making sure it is valid.
91 PDB *PROCESS_IdToPDB( DWORD id )
93 PDB *pdb;
95 if (!id) return PROCESS_Current();
96 pdb = PROCESS_First;
97 while (pdb)
99 if ((DWORD)pdb->server_pid == id) return pdb;
100 pdb = pdb->next;
102 SetLastError( ERROR_INVALID_PARAMETER );
103 return NULL;
107 /***********************************************************************
108 * PROCESS_CallUserSignalProc
110 * FIXME: Some of the signals aren't sent correctly!
112 * The exact meaning of the USER signals is undocumented, but this
113 * should cover the basic idea:
115 * USIG_DLL_UNLOAD_WIN16
116 * This is sent when a 16-bit module is unloaded.
118 * USIG_DLL_UNLOAD_WIN32
119 * This is sent when a 32-bit module is unloaded.
121 * USIG_DLL_UNLOAD_ORPHANS
122 * This is sent after the last Win3.1 module is unloaded,
123 * to allow removal of orphaned menus.
125 * USIG_FAULT_DIALOG_PUSH
126 * USIG_FAULT_DIALOG_POP
127 * These are called to allow USER to prepare for displaying a
128 * fault dialog, even though the fault might have happened while
129 * inside a USER critical section.
131 * USIG_THREAD_INIT
132 * This is called from the context of a new thread, as soon as it
133 * has started to run.
135 * USIG_THREAD_EXIT
136 * This is called, still in its context, just before a thread is
137 * about to terminate.
139 * USIG_PROCESS_CREATE
140 * This is called, in the parent process context, after a new process
141 * has been created.
143 * USIG_PROCESS_INIT
144 * This is called in the new process context, just after the main thread
145 * has started execution (after the main thread's USIG_THREAD_INIT has
146 * been sent).
148 * USIG_PROCESS_LOADED
149 * This is called after the executable file has been loaded into the
150 * new process context.
152 * USIG_PROCESS_RUNNING
153 * This is called immediately before the main entry point is called.
155 * USIG_PROCESS_EXIT
156 * This is called in the context of a process that is about to
157 * terminate (but before the last thread's USIG_THREAD_EXIT has
158 * been sent).
160 * USIG_PROCESS_DESTROY
161 * This is called after a process has terminated.
164 * The meaning of the dwFlags bits is as follows:
166 * USIG_FLAGS_WIN32
167 * Current process is 32-bit.
169 * USIG_FLAGS_GUI
170 * Current process is a (Win32) GUI process.
172 * USIG_FLAGS_FEEDBACK
173 * Current process needs 'feedback' (determined from the STARTUPINFO
174 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
176 * USIG_FLAGS_FAULT
177 * The signal is being sent due to a fault.
179 void PROCESS_CallUserSignalProc( UINT uCode, DWORD dwThreadOrProcessId, HMODULE hModule )
181 PDB *pdb = PROCESS_Current();
182 STARTUPINFOA *startup = pdb->env_db? pdb->env_db->startup_info : NULL;
183 DWORD dwFlags = 0;
185 /* Determine dwFlags */
187 if ( !(pdb->flags & PDB32_WIN16_PROC) )
188 dwFlags |= USIG_FLAGS_WIN32;
190 if ( !(pdb->flags & PDB32_CONSOLE_PROC) )
191 dwFlags |= USIG_FLAGS_GUI;
193 if ( dwFlags & USIG_FLAGS_GUI )
195 /* Feedback defaults to ON */
196 if ( !(startup && (startup->dwFlags & STARTF_FORCEOFFFEEDBACK)) )
197 dwFlags |= USIG_FLAGS_FEEDBACK;
199 else
201 /* Feedback defaults to OFF */
202 if ( startup && (startup->dwFlags & STARTF_FORCEONFEEDBACK) )
203 dwFlags |= USIG_FLAGS_FEEDBACK;
206 /* Get thread or process ID */
208 if ( dwThreadOrProcessId == 0 )
210 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
211 dwThreadOrProcessId = GetCurrentThreadId();
212 else
213 dwThreadOrProcessId = GetCurrentProcessId();
216 /* Convert module handle to 16-bit */
218 if ( HIWORD( hModule ) )
219 hModule = MapHModuleLS( hModule );
221 /* Call USER signal proc */
223 if ( Callout.UserSignalProc )
224 Callout.UserSignalProc( uCode, dwThreadOrProcessId, dwFlags, hModule );
228 /***********************************************************************
229 * PROCESS_BuildEnvDB
231 * Build the env DB for the initial process
233 static BOOL PROCESS_BuildEnvDB( PDB *pdb )
235 /* Allocate the env DB (FIXME: should not be on the system heap) */
237 if (!(pdb->env_db = HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(ENVDB))))
238 return FALSE;
239 InitializeCriticalSection( &pdb->env_db->section );
241 /* Allocate startup info */
242 if (!(pdb->env_db->startup_info =
243 HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
244 return FALSE;
246 /* Allocate the standard handles */
248 pdb->env_db->hStdin = FILE_DupUnixHandle( 0, GENERIC_READ );
249 pdb->env_db->hStdout = FILE_DupUnixHandle( 1, GENERIC_WRITE );
250 pdb->env_db->hStderr = FILE_DupUnixHandle( 2, GENERIC_WRITE );
252 /* Build the command-line */
254 pdb->env_db->cmd_line = HEAP_strdupA( SystemHeap, 0, "kernel32" );
256 /* Build the environment strings */
258 return ENV_BuildEnvironment( pdb );
262 /***********************************************************************
263 * PROCESS_InheritEnvDB
265 static BOOL PROCESS_InheritEnvDB( PDB *pdb, LPCSTR cmd_line, LPCSTR env,
266 BOOL inherit_handles, STARTUPINFOA *startup )
268 if (!(pdb->env_db = HeapAlloc(pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB))))
269 return FALSE;
270 InitializeCriticalSection( &pdb->env_db->section );
272 /* Copy the parent environment */
274 if (!ENV_InheritEnvironment( pdb, env )) return FALSE;
276 /* Copy the command line */
278 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
279 return FALSE;
281 /* Remember startup info */
282 if (!(pdb->env_db->startup_info =
283 HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
284 return FALSE;
285 *pdb->env_db->startup_info = *startup;
287 /* Inherit the standard handles */
288 if (pdb->env_db->startup_info->dwFlags & STARTF_USESTDHANDLES)
290 pdb->env_db->hStdin = pdb->env_db->startup_info->hStdInput;
291 pdb->env_db->hStdout = pdb->env_db->startup_info->hStdOutput;
292 pdb->env_db->hStderr = pdb->env_db->startup_info->hStdError;
294 else if (inherit_handles)
296 pdb->env_db->hStdin = pdb->parent->env_db->hStdin;
297 pdb->env_db->hStdout = pdb->parent->env_db->hStdout;
298 pdb->env_db->hStderr = pdb->parent->env_db->hStderr;
300 /* else will be done later on in PROCESS_Create */
302 return TRUE;
306 /***********************************************************************
307 * PROCESS_CreateEnvDB
309 * Create the env DB for a newly started process.
311 static BOOL PROCESS_CreateEnvDB(void)
313 struct init_process_request req;
314 struct init_process_reply reply;
315 STARTUPINFOA *startup;
316 ENVDB *env_db;
317 PDB *pdb = PROCESS_Current();
319 /* Retrieve startup info from the server */
321 req.dummy = 0;
322 CLIENT_SendRequest( REQ_INIT_PROCESS, -1, 1, &req, sizeof(req) );
323 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
325 #if 0
326 /* Allocate the env DB */
328 if (!(env_db = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
329 return FALSE;
330 pdb->env_db = env_db;
331 InitializeCriticalSection( &env_db->section );
333 /* Allocate and fill the startup info */
334 if (!(startup = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
335 return FALSE;
336 pdb->env_db->startup_info = startup;
337 #else
338 startup = pdb->env_db->startup_info;
339 #endif
340 startup->dwFlags = reply.start_flags;
341 pdb->env_db->hStdin = startup->hStdInput = reply.hstdin;
342 pdb->env_db->hStdout = startup->hStdOutput = reply.hstdout;
343 pdb->env_db->hStderr = startup->hStdError = reply.hstderr;
345 #if 0 /* FIXME */
346 /* Copy the parent environment */
348 if (!ENV_InheritEnvironment( pdb, env )) return FALSE;
350 /* Copy the command line */
352 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
353 return FALSE;
354 #endif
355 return TRUE;
359 /***********************************************************************
360 * PROCESS_FreePDB
362 * Free a PDB and all associated storage.
364 void PROCESS_FreePDB( PDB *pdb )
366 PDB **pptr = &PROCESS_First;
368 ENV_FreeEnvironment( pdb );
369 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
370 if (*pptr) *pptr = pdb->next;
371 if (pdb->heap && (pdb->heap != pdb->system_heap)) HeapDestroy( pdb->heap );
372 HeapFree( SystemHeap, 0, pdb );
376 /***********************************************************************
377 * PROCESS_CreatePDB
379 * Allocate and fill a PDB structure.
380 * Runs in the context of the parent process.
382 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
384 PDB *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB) );
386 if (!pdb) return NULL;
387 pdb->exit_code = 0x103; /* STILL_ACTIVE */
388 pdb->threads = 1;
389 pdb->running_threads = 1;
390 pdb->ring0_threads = 1;
391 pdb->system_heap = SystemHeap;
392 pdb->parent = parent;
393 pdb->group = pdb;
394 pdb->priority = 8; /* Normal */
395 pdb->heap = pdb->system_heap; /* will be changed later on */
396 pdb->next = PROCESS_First;
397 PROCESS_First = pdb;
398 return pdb;
402 /***********************************************************************
403 * PROCESS_Init
405 BOOL PROCESS_Init(void)
407 THDB *thdb;
408 int server_fd;
410 /* Start the server */
411 server_fd = CLIENT_InitServer();
413 /* Fill the initial process structure */
414 initial_pdb.exit_code = 0x103; /* STILL_ACTIVE */
415 initial_pdb.threads = 1;
416 initial_pdb.running_threads = 1;
417 initial_pdb.ring0_threads = 1;
418 initial_pdb.group = &initial_pdb;
419 initial_pdb.priority = 8; /* Normal */
420 initial_pdb.flags = PDB32_WIN16_PROC;
422 /* Initialize virtual memory management */
423 if (!VIRTUAL_Init()) return FALSE;
425 /* Create the initial thread structure and socket pair */
426 if (!(thdb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
428 /* Remember TEB selector of initial process for emergency use */
429 SYSLEVEL_EmergencyTeb = thdb->teb_sel;
431 /* Create the system heap */
432 if (!(SystemHeap = HeapCreate( HEAP_GROWABLE, 0x10000, 0 ))) return FALSE;
433 initial_pdb.system_heap = initial_pdb.heap = SystemHeap;
435 /* Create the environment DB of the first process */
436 if (!PROCESS_BuildEnvDB( &initial_pdb )) return FALSE;
438 /* Create the SEGPTR heap */
439 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
441 /* Initialize the first process critical section */
442 InitializeCriticalSection( &initial_pdb.crit_section );
444 return TRUE;
448 /***********************************************************************
449 * PROCESS_Start
451 * Startup routine of a new process. Called in the context of the new process.
453 void PROCESS_Start(void)
455 UINT cmdShow = 0;
456 LPTHREAD_START_ROUTINE entry;
457 THDB *thdb = THREAD_Current();
458 PDB *pdb = thdb->process;
459 NE_MODULE *pModule = NE_GetPtr( pdb->module );
460 OFSTRUCT *ofs = (OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo);
461 IMAGE_OPTIONAL_HEADER *header = &PE_HEADER(pModule->module32)->OptionalHeader;
463 /* Setup process flags */
464 if (header->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI) pdb->flags |= PDB32_CONSOLE_PROC;
466 /* Map system DLLs into this process (from initial process) */
467 /* FIXME: this is a hack */
468 pdb->modref_list = PROCESS_Initial()->modref_list;
470 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0, 0 ); /* for initial thread */
472 /* Initialize the critical section */
473 InitializeCriticalSection( &pdb->crit_section );
475 #if 0
476 /* Create the heap */
478 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE, header->SizeOfHeapReserve,
479 header->SizeOfHeapCommit ))) goto error;
480 pdb->heap_list = pdb->heap;
481 #endif
483 /* Create the environment db */
484 if (!PROCESS_CreateEnvDB()) goto error;
486 /* Create a task for this process */
487 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
488 cmdShow = pdb->env_db->startup_info->wShowWindow;
489 if (!TASK_Create( thdb, pModule, 0, 0, cmdShow )) goto error;
491 /* Link the task in the task list */
492 TASK_StartTask( pdb->task );
494 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0, 0 );
496 /* Create 32-bit MODREF */
497 if (!PE_CreateModule( pModule->module32, ofs, 0, FALSE )) goto error;
499 /* Increment EXE refcount */
500 assert( pdb->exe_modref );
501 pdb->exe_modref->refCount++;
503 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0, 0 ); /* FIXME: correct location? */
505 /* Initialize thread-local storage */
507 PE_InitTls();
509 if ( pdb->flags & PDB32_CONSOLE_PROC )
510 AllocConsole();
512 /* Now call the entry point */
514 EnterCriticalSection( &pdb->crit_section );
515 MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
516 LeaveCriticalSection( &pdb->crit_section );
518 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0, 0 );
520 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
521 OptionalHeader.AddressOfEntryPoint);
522 TRACE_(relay)("(entryproc=%p)\n", entry );
523 ExitProcess( entry(NULL) );
525 error:
526 ExitProcess(1);
530 /***********************************************************************
531 * PROCESS_Create
533 * Create a new process database and associated info.
535 PDB *PROCESS_Create( NE_MODULE *pModule, LPCSTR cmd_line, LPCSTR env,
536 HINSTANCE16 hInstance, HINSTANCE16 hPrevInstance,
537 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
538 BOOL inherit, STARTUPINFOA *startup,
539 PROCESS_INFORMATION *info )
541 DWORD size, commit;
542 int server_thandle;
543 struct new_process_request req;
544 struct new_process_reply reply;
545 UINT cmdShow = 0;
546 THDB *thdb = NULL;
547 PDB *parent = PROCESS_Current();
548 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
550 if (!pdb) return NULL;
551 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
553 /* Create the process on the server side */
555 req.inherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
556 req.inherit_all = inherit;
557 req.start_flags = startup->dwFlags;
558 if (startup->dwFlags & STARTF_USESTDHANDLES)
560 req.hstdin = startup->hStdInput;
561 req.hstdout = startup->hStdOutput;
562 req.hstderr = startup->hStdError;
564 else
566 req.hstdin = GetStdHandle( STD_INPUT_HANDLE );
567 req.hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
568 req.hstderr = GetStdHandle( STD_ERROR_HANDLE );
570 CLIENT_SendRequest( REQ_NEW_PROCESS, -1, 2,
571 &req, sizeof(req), cmd_line, strlen(cmd_line) + 1 );
572 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
573 pdb->server_pid = reply.pid;
574 info->hProcess = reply.handle;
575 info->dwProcessId = (DWORD)pdb->server_pid;
577 if (pModule->module32)
579 /* Create the main thread */
580 size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfStackReserve;
581 if (!(thdb = THREAD_Create( pdb, 0L, size, TRUE, tsa, &server_thandle )))
582 goto error;
583 info->hThread = server_thandle;
584 info->dwThreadId = (DWORD)thdb->server_tid;
585 thdb->startup = PROCESS_Start;
587 /* Create the heap */
588 size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapReserve;
589 commit = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapCommit;
590 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE, size, commit ))) goto error;
591 pdb->heap_list = pdb->heap;
593 /* Inherit the env DB from the parent */
594 if (!PROCESS_InheritEnvDB( pdb, cmd_line, env, inherit, startup )) goto error;
596 /* Call USER signal proc */
597 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, info->dwProcessId, 0 );
599 /* Set the process module (FIXME: hack) */
600 pdb->module = pModule->self;
601 SYSDEPS_SpawnThread( thdb );
603 else /* Create a 16-bit process */
605 /* Setup process flags */
606 pdb->flags |= PDB32_WIN16_PROC;
608 /* Create the heap */
609 size = 0x10000;
610 commit = 0;
611 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE, size, commit ))) goto error;
612 pdb->heap_list = pdb->heap;
614 /* Inherit the env DB from the parent */
615 if (!PROCESS_InheritEnvDB( pdb, cmd_line, env, inherit, startup )) goto error;
617 /* Create the main thread */
618 if (!(thdb = THREAD_Create( pdb, 0L, 0, hInstance == 0, tsa, &server_thandle )))
619 goto error;
620 info->hThread = server_thandle;
621 info->dwThreadId = (DWORD)thdb->server_tid;
622 thdb->startup = PROCESS_Start;
624 /* Duplicate the standard handles */
625 if ((!(pdb->env_db->startup_info->dwFlags & STARTF_USESTDHANDLES)) && !inherit)
627 DuplicateHandle( GetCurrentProcess(), pdb->parent->env_db->hStdin,
628 info->hProcess, &pdb->env_db->hStdin, 0, TRUE, DUPLICATE_SAME_ACCESS );
629 DuplicateHandle( GetCurrentProcess(), pdb->parent->env_db->hStdout,
630 info->hProcess, &pdb->env_db->hStdout, 0, TRUE, DUPLICATE_SAME_ACCESS );
631 DuplicateHandle( GetCurrentProcess(), pdb->parent->env_db->hStderr,
632 info->hProcess, &pdb->env_db->hStderr, 0, TRUE, DUPLICATE_SAME_ACCESS );
635 /* Call USER signal proc */
636 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, info->dwProcessId, 0 );
638 /* Create a Win16 task for this process */
639 if (startup->dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup->wShowWindow;
640 if (!TASK_Create( thdb, pModule, hInstance, hPrevInstance, cmdShow )) goto error;
642 /* Map system DLLs into this process (from initial process) */
643 /* FIXME: this is a hack */
644 pdb->modref_list = PROCESS_Initial()->modref_list;
646 /* Start the task */
647 TASK_StartTask( pdb->task );
650 return pdb;
652 error:
653 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
654 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
655 PROCESS_FreePDB( pdb );
656 return NULL;
660 /***********************************************************************
661 * ExitProcess (KERNEL32.100)
663 void WINAPI ExitProcess( DWORD status )
665 EnterCriticalSection( &PROCESS_Current()->crit_section );
666 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
667 LeaveCriticalSection( &PROCESS_Current()->crit_section );
669 if ( THREAD_IsWin16( THREAD_Current() ) )
670 TASK_KillCurrentTask( status );
672 TASK_KillTask( 0 );
673 TerminateProcess( GetCurrentProcess(), status );
677 /******************************************************************************
678 * TerminateProcess (KERNEL32.684)
680 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
682 struct terminate_process_request req;
683 req.handle = handle;
684 req.exit_code = exit_code;
685 CLIENT_SendRequest( REQ_TERMINATE_PROCESS, -1, 1, &req, sizeof(req) );
686 return !CLIENT_WaitReply( NULL, NULL, 0 );
690 /***********************************************************************
691 * GetProcessDword (KERNEL32.18) (KERNEL.485)
692 * 'Of course you cannot directly access Windows internal structures'
694 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
696 PDB *process = PROCESS_IdToPDB( dwProcessID );
697 TDB *pTask;
698 DWORD x, y;
700 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
701 if ( !process ) return 0;
703 switch ( offset )
705 case GPD_APP_COMPAT_FLAGS:
706 pTask = (TDB *)GlobalLock16( process->task );
707 return pTask? pTask->compat_flags : 0;
709 case GPD_LOAD_DONE_EVENT:
710 return process->load_done_evt;
712 case GPD_HINSTANCE16:
713 pTask = (TDB *)GlobalLock16( process->task );
714 return pTask? pTask->hInstance : 0;
716 case GPD_WINDOWS_VERSION:
717 pTask = (TDB *)GlobalLock16( process->task );
718 return pTask? pTask->version : 0;
720 case GPD_THDB:
721 if ( process != PROCESS_Current() ) return 0;
722 return (DWORD)THREAD_Current();
724 case GPD_PDB:
725 return (DWORD)process;
727 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
728 return process->env_db->startup_info->hStdOutput;
730 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
731 return process->env_db->startup_info->hStdInput;
733 case GPD_STARTF_SHOWWINDOW:
734 return process->env_db->startup_info->wShowWindow;
736 case GPD_STARTF_SIZE:
737 x = process->env_db->startup_info->dwXSize;
738 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
739 y = process->env_db->startup_info->dwYSize;
740 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
741 return MAKELONG( x, y );
743 case GPD_STARTF_POSITION:
744 x = process->env_db->startup_info->dwX;
745 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
746 y = process->env_db->startup_info->dwY;
747 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
748 return MAKELONG( x, y );
750 case GPD_STARTF_FLAGS:
751 return process->env_db->startup_info->dwFlags;
753 case GPD_PARENT:
754 return (DWORD)process->parent->server_pid;
756 case GPD_FLAGS:
757 return process->flags;
759 case GPD_USERDATA:
760 return process->process_dword;
762 default:
763 ERR_(win32)("Unknown offset %d\n", offset );
764 return 0;
768 /***********************************************************************
769 * SetProcessDword (KERNEL.484)
770 * 'Of course you cannot directly access Windows internal structures'
772 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
774 PDB *process = PROCESS_IdToPDB( dwProcessID );
776 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
777 if ( !process ) return;
779 switch ( offset )
781 case GPD_APP_COMPAT_FLAGS:
782 case GPD_LOAD_DONE_EVENT:
783 case GPD_HINSTANCE16:
784 case GPD_WINDOWS_VERSION:
785 case GPD_THDB:
786 case GPD_PDB:
787 case GPD_STARTF_SHELLDATA:
788 case GPD_STARTF_HOTKEY:
789 case GPD_STARTF_SHOWWINDOW:
790 case GPD_STARTF_SIZE:
791 case GPD_STARTF_POSITION:
792 case GPD_STARTF_FLAGS:
793 case GPD_PARENT:
794 case GPD_FLAGS:
795 ERR_(win32)("Not allowed to modify offset %d\n", offset );
796 break;
798 case GPD_USERDATA:
799 process->process_dword = value;
800 break;
802 default:
803 ERR_(win32)("Unknown offset %d\n", offset );
804 break;
809 /***********************************************************************
810 * GetCurrentProcess (KERNEL32.198)
812 HANDLE WINAPI GetCurrentProcess(void)
814 return CURRENT_PROCESS_PSEUDOHANDLE;
818 /*********************************************************************
819 * OpenProcess (KERNEL32.543)
821 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
823 struct open_process_request req;
824 struct open_process_reply reply;
826 req.pid = (void *)id;
827 req.access = access;
828 req.inherit = inherit;
829 CLIENT_SendRequest( REQ_OPEN_PROCESS, -1, 1, &req, sizeof(req) );
830 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0;
831 return reply.handle;
835 /***********************************************************************
836 * GetCurrentProcessId (KERNEL32.199)
838 DWORD WINAPI GetCurrentProcessId(void)
840 return (DWORD)PROCESS_Current()->server_pid;
844 /***********************************************************************
845 * GetProcessHeap (KERNEL32.259)
847 HANDLE WINAPI GetProcessHeap(void)
849 PDB *pdb = PROCESS_Current();
850 return pdb->heap ? pdb->heap : SystemHeap;
854 /***********************************************************************
855 * GetThreadLocale (KERNEL32.295)
857 LCID WINAPI GetThreadLocale(void)
859 return PROCESS_Current()->locale;
863 /***********************************************************************
864 * SetPriorityClass (KERNEL32.503)
866 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
868 struct set_process_info_request req;
869 req.handle = hprocess;
870 req.priority = priorityclass;
871 req.mask = SET_PROCESS_INFO_PRIORITY;
872 CLIENT_SendRequest( REQ_SET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
873 return !CLIENT_WaitReply( NULL, NULL, 0 );
877 /***********************************************************************
878 * GetPriorityClass (KERNEL32.250)
880 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
882 struct get_process_info_reply reply;
883 if (!PROCESS_QueryInfo( hprocess, &reply )) return 0;
884 return reply.priority;
888 /***********************************************************************
889 * SetProcessAffinityMask (KERNEL32.662)
891 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
893 struct set_process_info_request req;
894 req.handle = hProcess;
895 req.affinity = affmask;
896 req.mask = SET_PROCESS_INFO_AFFINITY;
897 CLIENT_SendRequest( REQ_SET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
898 return !CLIENT_WaitReply( NULL, NULL, 0 );
901 /**********************************************************************
902 * GetProcessAffinityMask (KERNEL32.373)
904 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
905 LPDWORD lpProcessAffinityMask,
906 LPDWORD lpSystemAffinityMask )
908 struct get_process_info_reply reply;
909 if (!PROCESS_QueryInfo( hProcess, &reply )) return FALSE;
910 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply.process_affinity;
911 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply.system_affinity;
912 return TRUE;
916 /***********************************************************************
917 * GetStdHandle (KERNEL32.276)
919 HANDLE WINAPI GetStdHandle( DWORD std_handle )
921 PDB *pdb = PROCESS_Current();
923 switch(std_handle)
925 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
926 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
927 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
929 SetLastError( ERROR_INVALID_PARAMETER );
930 return INVALID_HANDLE_VALUE;
934 /***********************************************************************
935 * SetStdHandle (KERNEL32.506)
937 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
939 PDB *pdb = PROCESS_Current();
940 /* FIXME: should we close the previous handle? */
941 switch(std_handle)
943 case STD_INPUT_HANDLE:
944 pdb->env_db->hStdin = handle;
945 return TRUE;
946 case STD_OUTPUT_HANDLE:
947 pdb->env_db->hStdout = handle;
948 return TRUE;
949 case STD_ERROR_HANDLE:
950 pdb->env_db->hStderr = handle;
951 return TRUE;
953 SetLastError( ERROR_INVALID_PARAMETER );
954 return FALSE;
957 /***********************************************************************
958 * GetProcessVersion (KERNEL32)
960 DWORD WINAPI GetProcessVersion( DWORD processid )
962 TDB *pTask;
963 PDB *pdb = PROCESS_IdToPDB( processid );
965 if (!pdb) return 0;
966 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
967 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
970 /***********************************************************************
971 * GetProcessFlags (KERNEL32)
973 DWORD WINAPI GetProcessFlags( DWORD processid )
975 PDB *pdb = PROCESS_IdToPDB( processid );
976 if (!pdb) return 0;
977 return pdb->flags;
980 /***********************************************************************
981 * SetProcessWorkingSetSize [KERNEL32.662]
982 * Sets the min/max working set sizes for a specified process.
984 * PARAMS
985 * hProcess [I] Handle to the process of interest
986 * minset [I] Specifies minimum working set size
987 * maxset [I] Specifies maximum working set size
989 * RETURNS STD
991 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
992 DWORD maxset)
994 FIXME_(process)("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
995 if(( minset == -1) && (maxset == -1)) {
996 /* Trim the working set to zero */
997 /* Swap the process out of physical RAM */
999 return TRUE;
1002 /***********************************************************************
1003 * GetProcessWorkingSetSize (KERNEL32)
1005 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1006 LPDWORD maxset)
1008 FIXME_(process)("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1009 /* 32 MB working set size */
1010 if (minset) *minset = 32*1024*1024;
1011 if (maxset) *maxset = 32*1024*1024;
1012 return TRUE;
1015 /***********************************************************************
1016 * SetProcessShutdownParameters (KERNEL32)
1018 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1019 * Now tracks changes made (but does not act on these changes)
1020 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1021 * It really shouldn't be here, but I'll move it when it's been checked!
1023 #define SHUTDOWN_NORETRY 1
1024 static unsigned int shutdown_noretry = 0;
1025 static unsigned int shutdown_priority = 0x280L;
1026 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1028 if (flags & SHUTDOWN_NORETRY)
1029 shutdown_noretry = 1;
1030 else
1031 shutdown_noretry = 0;
1032 if (level > 0x100L && level < 0x3FFL)
1033 shutdown_priority = level;
1034 else
1036 ERR_(process)("invalid priority level 0x%08lx\n", level);
1037 return FALSE;
1039 return TRUE;
1043 /***********************************************************************
1044 * GetProcessShutdownParameters (KERNEL32)
1047 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1048 LPDWORD lpdwFlags )
1050 (*lpdwLevel) = shutdown_priority;
1051 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1052 return TRUE;
1054 /***********************************************************************
1055 * SetProcessPriorityBoost (KERNEL32)
1057 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1059 FIXME_(process)("(%d,%d): stub\n",hprocess,disableboost);
1060 /* Say we can do it. I doubt the program will notice that we don't. */
1061 return TRUE;
1064 /***********************************************************************
1065 * ReadProcessMemory (KERNEL32)
1066 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1067 * ... and add a sizecheck
1069 BOOL WINAPI ReadProcessMemory( HANDLE hProcess, LPCVOID lpBaseAddress,
1070 LPVOID lpBuffer, DWORD nSize,
1071 LPDWORD lpNumberOfBytesRead )
1073 memcpy(lpBuffer,lpBaseAddress,nSize);
1074 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = nSize;
1075 return TRUE;
1078 /***********************************************************************
1079 * WriteProcessMemory (KERNEL32)
1080 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1081 * ... and add a sizecheck
1083 BOOL WINAPI WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress,
1084 LPVOID lpBuffer, DWORD nSize,
1085 LPDWORD lpNumberOfBytesWritten )
1087 memcpy(lpBaseAddress,lpBuffer,nSize);
1088 if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = nSize;
1089 return TRUE;
1092 /***********************************************************************
1093 * RegisterServiceProcess (KERNEL, KERNEL32)
1095 * A service process calls this function to ensure that it continues to run
1096 * even after a user logged off.
1098 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1100 /* I don't think that Wine needs to do anything in that function */
1101 return 1; /* success */
1104 /***********************************************************************
1105 * GetExitCodeProcess [KERNEL32.325]
1107 * Gets termination status of specified process
1109 * RETURNS
1110 * Success: TRUE
1111 * Failure: FALSE
1113 BOOL WINAPI GetExitCodeProcess(
1114 HANDLE hProcess, /* [I] handle to the process */
1115 LPDWORD lpExitCode) /* [O] address to receive termination status */
1117 struct get_process_info_reply reply;
1118 if (!PROCESS_QueryInfo( hProcess, &reply )) return FALSE;
1119 if (lpExitCode) *lpExitCode = reply.exit_code;
1120 return TRUE;
1124 /***********************************************************************
1125 * GetProcessHeaps [KERNEL32.376]
1127 DWORD WINAPI GetProcessHeaps(DWORD nrofheaps,HANDLE *heaps) {
1128 FIXME_(win32)("(%ld,%p), incomplete implementation.\n",nrofheaps,heaps);
1130 if (nrofheaps) {
1131 heaps[0] = GetProcessHeap();
1132 /* ... probably SystemHeap too ? */
1133 return 1;
1135 /* number of available heaps */
1136 return 1;