Add start menu directories.
[wine/wine-kai.git] / scheduler / process.c
blob2ffe57dec7ccb0eea2a10035f0e8e513f44b7876
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 exit( status );
642 /***********************************************************************
643 * ExitProcess16 (KERNEL.466)
645 void WINAPI ExitProcess16( WORD status )
647 SYSLEVEL_ReleaseWin16Lock();
648 ExitProcess( status );
651 /******************************************************************************
652 * TerminateProcess (KERNEL32.684)
654 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
656 BOOL ret;
657 struct terminate_process_request *req = get_req_buffer();
658 req->handle = handle;
659 req->exit_code = exit_code;
660 if ((ret = !server_call( REQ_TERMINATE_PROCESS )) && req->self) exit( exit_code );
661 return ret;
665 /***********************************************************************
666 * GetProcessDword (KERNEL32.18) (KERNEL.485)
667 * 'Of course you cannot directly access Windows internal structures'
669 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
671 PDB *process = PROCESS_IdToPDB( dwProcessID );
672 TDB *pTask;
673 DWORD x, y;
675 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
676 if ( !process ) return 0;
678 switch ( offset )
680 case GPD_APP_COMPAT_FLAGS:
681 pTask = (TDB *)GlobalLock16( process->task );
682 return pTask? pTask->compat_flags : 0;
684 case GPD_LOAD_DONE_EVENT:
685 return process->load_done_evt;
687 case GPD_HINSTANCE16:
688 pTask = (TDB *)GlobalLock16( process->task );
689 return pTask? pTask->hInstance : 0;
691 case GPD_WINDOWS_VERSION:
692 pTask = (TDB *)GlobalLock16( process->task );
693 return pTask? pTask->version : 0;
695 case GPD_THDB:
696 if ( process != PROCESS_Current() ) return 0;
697 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
699 case GPD_PDB:
700 return (DWORD)process;
702 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
703 return process->env_db->startup_info->hStdOutput;
705 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
706 return process->env_db->startup_info->hStdInput;
708 case GPD_STARTF_SHOWWINDOW:
709 return process->env_db->startup_info->wShowWindow;
711 case GPD_STARTF_SIZE:
712 x = process->env_db->startup_info->dwXSize;
713 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
714 y = process->env_db->startup_info->dwYSize;
715 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
716 return MAKELONG( x, y );
718 case GPD_STARTF_POSITION:
719 x = process->env_db->startup_info->dwX;
720 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
721 y = process->env_db->startup_info->dwY;
722 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
723 return MAKELONG( x, y );
725 case GPD_STARTF_FLAGS:
726 return process->env_db->startup_info->dwFlags;
728 case GPD_PARENT:
729 return process->parent? (DWORD)process->parent->server_pid : 0;
731 case GPD_FLAGS:
732 return process->flags;
734 case GPD_USERDATA:
735 return process->process_dword;
737 default:
738 ERR_(win32)("Unknown offset %d\n", offset );
739 return 0;
743 /***********************************************************************
744 * SetProcessDword (KERNEL.484)
745 * 'Of course you cannot directly access Windows internal structures'
747 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
749 PDB *process = PROCESS_IdToPDB( dwProcessID );
751 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
752 if ( !process ) return;
754 switch ( offset )
756 case GPD_APP_COMPAT_FLAGS:
757 case GPD_LOAD_DONE_EVENT:
758 case GPD_HINSTANCE16:
759 case GPD_WINDOWS_VERSION:
760 case GPD_THDB:
761 case GPD_PDB:
762 case GPD_STARTF_SHELLDATA:
763 case GPD_STARTF_HOTKEY:
764 case GPD_STARTF_SHOWWINDOW:
765 case GPD_STARTF_SIZE:
766 case GPD_STARTF_POSITION:
767 case GPD_STARTF_FLAGS:
768 case GPD_PARENT:
769 case GPD_FLAGS:
770 ERR_(win32)("Not allowed to modify offset %d\n", offset );
771 break;
773 case GPD_USERDATA:
774 process->process_dword = value;
775 break;
777 default:
778 ERR_(win32)("Unknown offset %d\n", offset );
779 break;
784 /***********************************************************************
785 * GetCurrentProcess (KERNEL32.198)
787 HANDLE WINAPI GetCurrentProcess(void)
789 return CURRENT_PROCESS_PSEUDOHANDLE;
793 /*********************************************************************
794 * OpenProcess (KERNEL32.543)
796 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
798 HANDLE ret = 0;
799 struct open_process_request *req = get_req_buffer();
801 req->pid = (void *)id;
802 req->access = access;
803 req->inherit = inherit;
804 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
805 return ret;
808 /*********************************************************************
809 * MapProcessHandle (KERNEL.483)
811 DWORD WINAPI MapProcessHandle( HANDLE handle )
813 DWORD ret = 0;
814 struct get_process_info_request *req = get_req_buffer();
815 req->handle = handle;
816 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
817 return ret;
820 /***********************************************************************
821 * GetCurrentProcessId (KERNEL32.199)
823 DWORD WINAPI GetCurrentProcessId(void)
825 return (DWORD)PROCESS_Current()->server_pid;
829 /***********************************************************************
830 * GetThreadLocale (KERNEL32.295)
832 LCID WINAPI GetThreadLocale(void)
834 return PROCESS_Current()->locale;
838 /***********************************************************************
839 * SetPriorityClass (KERNEL32.503)
841 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
843 struct set_process_info_request *req = get_req_buffer();
844 req->handle = hprocess;
845 req->priority = priorityclass;
846 req->mask = SET_PROCESS_INFO_PRIORITY;
847 return !server_call( REQ_SET_PROCESS_INFO );
851 /***********************************************************************
852 * GetPriorityClass (KERNEL32.250)
854 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
856 DWORD ret = 0;
857 struct get_process_info_request *req = get_req_buffer();
858 req->handle = hprocess;
859 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
860 return ret;
864 /***********************************************************************
865 * SetProcessAffinityMask (KERNEL32.662)
867 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
869 struct set_process_info_request *req = get_req_buffer();
870 req->handle = hProcess;
871 req->affinity = affmask;
872 req->mask = SET_PROCESS_INFO_AFFINITY;
873 return !server_call( REQ_SET_PROCESS_INFO );
876 /**********************************************************************
877 * GetProcessAffinityMask (KERNEL32.373)
879 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
880 LPDWORD lpProcessAffinityMask,
881 LPDWORD lpSystemAffinityMask )
883 BOOL ret = FALSE;
884 struct get_process_info_request *req = get_req_buffer();
885 req->handle = hProcess;
886 if (!server_call( REQ_GET_PROCESS_INFO ))
888 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
889 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
890 ret = TRUE;
892 return ret;
896 /***********************************************************************
897 * GetStdHandle (KERNEL32.276)
899 HANDLE WINAPI GetStdHandle( DWORD std_handle )
901 PDB *pdb = PROCESS_Current();
903 switch(std_handle)
905 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
906 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
907 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
909 SetLastError( ERROR_INVALID_PARAMETER );
910 return INVALID_HANDLE_VALUE;
914 /***********************************************************************
915 * SetStdHandle (KERNEL32.506)
917 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
919 PDB *pdb = PROCESS_Current();
920 /* FIXME: should we close the previous handle? */
921 switch(std_handle)
923 case STD_INPUT_HANDLE:
924 pdb->env_db->hStdin = handle;
925 return TRUE;
926 case STD_OUTPUT_HANDLE:
927 pdb->env_db->hStdout = handle;
928 return TRUE;
929 case STD_ERROR_HANDLE:
930 pdb->env_db->hStderr = handle;
931 return TRUE;
933 SetLastError( ERROR_INVALID_PARAMETER );
934 return FALSE;
937 /***********************************************************************
938 * GetProcessVersion (KERNEL32)
940 DWORD WINAPI GetProcessVersion( DWORD processid )
942 TDB *pTask;
943 PDB *pdb = PROCESS_IdToPDB( processid );
945 if (!pdb) return 0;
946 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
947 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
950 /***********************************************************************
951 * GetProcessFlags (KERNEL32)
953 DWORD WINAPI GetProcessFlags( DWORD processid )
955 PDB *pdb = PROCESS_IdToPDB( processid );
956 if (!pdb) return 0;
957 return pdb->flags;
960 /***********************************************************************
961 * SetProcessWorkingSetSize [KERNEL32.662]
962 * Sets the min/max working set sizes for a specified process.
964 * PARAMS
965 * hProcess [I] Handle to the process of interest
966 * minset [I] Specifies minimum working set size
967 * maxset [I] Specifies maximum working set size
969 * RETURNS STD
971 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
972 DWORD maxset)
974 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
975 if(( minset == -1) && (maxset == -1)) {
976 /* Trim the working set to zero */
977 /* Swap the process out of physical RAM */
979 return TRUE;
982 /***********************************************************************
983 * GetProcessWorkingSetSize (KERNEL32)
985 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
986 LPDWORD maxset)
988 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
989 /* 32 MB working set size */
990 if (minset) *minset = 32*1024*1024;
991 if (maxset) *maxset = 32*1024*1024;
992 return TRUE;
995 /***********************************************************************
996 * SetProcessShutdownParameters (KERNEL32)
998 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
999 * Now tracks changes made (but does not act on these changes)
1000 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1001 * It really shouldn't be here, but I'll move it when it's been checked!
1003 #define SHUTDOWN_NORETRY 1
1004 static unsigned int shutdown_noretry = 0;
1005 static unsigned int shutdown_priority = 0x280L;
1006 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1008 if (flags & SHUTDOWN_NORETRY)
1009 shutdown_noretry = 1;
1010 else
1011 shutdown_noretry = 0;
1012 if (level > 0x100L && level < 0x3FFL)
1013 shutdown_priority = level;
1014 else
1016 ERR("invalid priority level 0x%08lx\n", level);
1017 return FALSE;
1019 return TRUE;
1023 /***********************************************************************
1024 * GetProcessShutdownParameters (KERNEL32)
1027 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1028 LPDWORD lpdwFlags )
1030 (*lpdwLevel) = shutdown_priority;
1031 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1032 return TRUE;
1034 /***********************************************************************
1035 * SetProcessPriorityBoost (KERNEL32)
1037 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1039 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1040 /* Say we can do it. I doubt the program will notice that we don't. */
1041 return TRUE;
1045 /***********************************************************************
1046 * ReadProcessMemory (KERNEL32)
1048 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1049 LPDWORD bytes_read )
1051 struct read_process_memory_request *req = get_req_buffer();
1052 unsigned int offset = (unsigned int)addr % sizeof(int);
1053 unsigned int max = server_remaining( req->data ); /* max length in one request */
1054 unsigned int pos;
1056 if (bytes_read) *bytes_read = size;
1058 /* first time, read total length to check for permissions */
1059 req->handle = process;
1060 req->addr = (char *)addr - offset;
1061 req->len = (size + offset + sizeof(int) - 1) / sizeof(int);
1062 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1064 if (size <= max - offset)
1066 memcpy( buffer, (char *)req->data + offset, size );
1067 return TRUE;
1070 /* now take care of the remaining data */
1071 memcpy( buffer, (char *)req->data + offset, max - offset );
1072 pos = max - offset;
1073 size -= pos;
1074 while (size)
1076 if (max > size) max = size;
1077 req->handle = process;
1078 req->addr = (char *)addr + pos;
1079 req->len = (max + sizeof(int) - 1) / sizeof(int);
1080 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1081 memcpy( (char *)buffer + pos, (char *)req->data, max );
1082 size -= max;
1083 pos += max;
1085 return TRUE;
1087 error:
1088 if (bytes_read) *bytes_read = 0;
1089 return FALSE;
1093 /***********************************************************************
1094 * WriteProcessMemory (KERNEL32)
1096 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1097 LPDWORD bytes_written )
1099 unsigned int first_offset, last_offset;
1100 struct write_process_memory_request *req = get_req_buffer();
1101 unsigned int max = server_remaining( req->data ); /* max length in one request */
1102 unsigned int pos, last_mask;
1104 if (!size)
1106 SetLastError( ERROR_INVALID_PARAMETER );
1107 return FALSE;
1109 if (bytes_written) *bytes_written = size;
1111 /* compute the mask for the first int */
1112 req->first_mask = ~0;
1113 first_offset = (unsigned int)addr % sizeof(int);
1114 memset( &req->first_mask, 0, first_offset );
1116 /* compute the mask for the last int */
1117 last_offset = (size + first_offset) % sizeof(int);
1118 last_mask = 0;
1119 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1121 req->handle = process;
1122 req->addr = (char *)addr - first_offset;
1123 /* for the first request, use the total length */
1124 req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1126 if (size + first_offset < max) /* we can do it in one round */
1128 memcpy( (char *)req->data + first_offset, buffer, size );
1129 req->last_mask = last_mask;
1130 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1131 return TRUE;
1134 /* needs multiple server calls */
1136 memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1137 req->last_mask = ~0;
1138 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1139 pos = max - first_offset;
1140 size -= pos;
1141 while (size)
1143 if (size <= max) /* last one */
1145 req->last_mask = last_mask;
1146 max = size;
1148 req->handle = process;
1149 req->addr = (char *)addr + pos;
1150 req->len = (max + sizeof(int) - 1) / sizeof(int);
1151 req->first_mask = ~0;
1152 memcpy( req->data, (char *) buffer + pos, max );
1153 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1154 pos += max;
1155 size -= max;
1157 return TRUE;
1159 error:
1160 if (bytes_written) *bytes_written = 0;
1161 return FALSE;
1166 /***********************************************************************
1167 * RegisterServiceProcess (KERNEL, KERNEL32)
1169 * A service process calls this function to ensure that it continues to run
1170 * even after a user logged off.
1172 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1174 /* I don't think that Wine needs to do anything in that function */
1175 return 1; /* success */
1178 /***********************************************************************
1179 * GetExitCodeProcess [KERNEL32.325]
1181 * Gets termination status of specified process
1183 * RETURNS
1184 * Success: TRUE
1185 * Failure: FALSE
1187 BOOL WINAPI GetExitCodeProcess(
1188 HANDLE hProcess, /* [I] handle to the process */
1189 LPDWORD lpExitCode) /* [O] address to receive termination status */
1191 BOOL ret = FALSE;
1192 struct get_process_info_request *req = get_req_buffer();
1193 req->handle = hProcess;
1194 if (!server_call( REQ_GET_PROCESS_INFO ))
1196 if (lpExitCode) *lpExitCode = req->exit_code;
1197 ret = TRUE;
1199 return ret;
1203 /***********************************************************************
1204 * SetErrorMode (KERNEL32.486)
1206 UINT WINAPI SetErrorMode( UINT mode )
1208 UINT old = PROCESS_Current()->error_mode;
1209 PROCESS_Current()->error_mode = mode;
1210 return old;