Better implementation of inline functions SetLastError and
[wine.git] / scheduler / process.c
blob56167a65d16e306a0c8bd145614cc12033e373c9
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 "debugger.h"
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(process)
33 DECLARE_DEBUG_CHANNEL(relay)
34 DECLARE_DEBUG_CHANNEL(win32)
37 /* The initial process PDB */
38 static PDB initial_pdb;
40 static PDB *PROCESS_First = &initial_pdb;
43 /***********************************************************************
44 * PROCESS_WalkProcess
46 void PROCESS_WalkProcess(void)
48 PDB *pdb;
49 char *name;
51 pdb = PROCESS_First;
52 MESSAGE( " pid PDB #th modref module \n" );
53 while(pdb)
55 if (pdb == &initial_pdb)
56 name = "initial PDB";
57 else
58 name = (pdb->exe_modref) ? pdb->exe_modref->filename : "";
60 MESSAGE( " %8p %8p %5d %8p %s\n", pdb->server_pid, pdb,
61 pdb->threads, pdb->exe_modref, name);
62 pdb = pdb->next;
64 return;
67 /***********************************************************************
68 * PROCESS_IsCurrent
70 * Check if a handle is to the current process
72 BOOL PROCESS_IsCurrent( HANDLE handle )
74 struct get_process_info_request *req = get_req_buffer();
75 req->handle = handle;
76 return (!server_call( REQ_GET_PROCESS_INFO ) &&
77 (req->pid == PROCESS_Current()->server_pid));
81 /***********************************************************************
82 * PROCESS_IdToPDB
84 * Convert a process id to a PDB, making sure it is valid.
86 PDB *PROCESS_IdToPDB( DWORD pid )
88 PDB *pdb;
90 if (!pid) return PROCESS_Current();
91 pdb = PROCESS_First;
92 while (pdb)
94 if ((DWORD)pdb->server_pid == pid) return pdb;
95 pdb = pdb->next;
97 SetLastError( ERROR_INVALID_PARAMETER );
98 return NULL;
102 /***********************************************************************
103 * PROCESS_CallUserSignalProc
105 * FIXME: Some of the signals aren't sent correctly!
107 * The exact meaning of the USER signals is undocumented, but this
108 * should cover the basic idea:
110 * USIG_DLL_UNLOAD_WIN16
111 * This is sent when a 16-bit module is unloaded.
113 * USIG_DLL_UNLOAD_WIN32
114 * This is sent when a 32-bit module is unloaded.
116 * USIG_DLL_UNLOAD_ORPHANS
117 * This is sent after the last Win3.1 module is unloaded,
118 * to allow removal of orphaned menus.
120 * USIG_FAULT_DIALOG_PUSH
121 * USIG_FAULT_DIALOG_POP
122 * These are called to allow USER to prepare for displaying a
123 * fault dialog, even though the fault might have happened while
124 * inside a USER critical section.
126 * USIG_THREAD_INIT
127 * This is called from the context of a new thread, as soon as it
128 * has started to run.
130 * USIG_THREAD_EXIT
131 * This is called, still in its context, just before a thread is
132 * about to terminate.
134 * USIG_PROCESS_CREATE
135 * This is called, in the parent process context, after a new process
136 * has been created.
138 * USIG_PROCESS_INIT
139 * This is called in the new process context, just after the main thread
140 * has started execution (after the main thread's USIG_THREAD_INIT has
141 * been sent).
143 * USIG_PROCESS_LOADED
144 * This is called after the executable file has been loaded into the
145 * new process context.
147 * USIG_PROCESS_RUNNING
148 * This is called immediately before the main entry point is called.
150 * USIG_PROCESS_EXIT
151 * This is called in the context of a process that is about to
152 * terminate (but before the last thread's USIG_THREAD_EXIT has
153 * been sent).
155 * USIG_PROCESS_DESTROY
156 * This is called after a process has terminated.
159 * The meaning of the dwFlags bits is as follows:
161 * USIG_FLAGS_WIN32
162 * Current process is 32-bit.
164 * USIG_FLAGS_GUI
165 * Current process is a (Win32) GUI process.
167 * USIG_FLAGS_FEEDBACK
168 * Current process needs 'feedback' (determined from the STARTUPINFO
169 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
171 * USIG_FLAGS_FAULT
172 * The signal is being sent due to a fault.
174 void PROCESS_CallUserSignalProc( UINT uCode, DWORD dwThreadId, HMODULE hModule )
176 DWORD flags = PROCESS_Current()->flags;
177 DWORD startup_flags = PROCESS_Current()->env_db->startup_info->dwFlags;
178 DWORD dwFlags = 0;
180 /* Determine dwFlags */
182 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
184 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
186 if ( dwFlags & USIG_FLAGS_GUI )
188 /* Feedback defaults to ON */
189 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
190 dwFlags |= USIG_FLAGS_FEEDBACK;
192 else
194 /* Feedback defaults to OFF */
195 if (startup_flags & STARTF_FORCEONFEEDBACK)
196 dwFlags |= USIG_FLAGS_FEEDBACK;
199 /* Convert module handle to 16-bit */
201 if ( HIWORD( hModule ) )
202 hModule = MapHModuleLS( hModule );
204 /* Call USER signal proc */
206 if ( Callout.UserSignalProc )
208 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
209 Callout.UserSignalProc( uCode, dwThreadId, dwFlags, hModule );
210 else
211 Callout.UserSignalProc( uCode, GetCurrentProcessId(), dwFlags, hModule );
215 /***********************************************************************
216 * PROCESS_CreateEnvDB
218 * Create the env DB for a newly started process.
220 static BOOL PROCESS_CreateEnvDB(void)
222 struct init_process_request *req = get_req_buffer();
223 STARTUPINFOA *startup;
224 ENVDB *env_db;
225 char cmd_line[4096];
226 PDB *pdb = PROCESS_Current();
228 /* Allocate the env DB */
230 if (!(env_db = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
231 return FALSE;
232 pdb->env_db = env_db;
233 InitializeCriticalSection( &env_db->section );
235 /* Allocate and fill the startup info */
236 if (!(startup = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
237 return FALSE;
238 env_db->startup_info = startup;
240 /* Retrieve startup info from the server */
242 req->ldt_copy = ldt_copy;
243 req->ldt_flags = ldt_flags_copy;
244 if (server_call( REQ_INIT_PROCESS )) return FALSE;
245 pdb->exe_file = req->exe_file;
246 startup->dwFlags = req->start_flags;
247 startup->wShowWindow = req->cmd_show;
248 env_db->hStdin = startup->hStdInput = req->hstdin;
249 env_db->hStdout = startup->hStdOutput = req->hstdout;
250 env_db->hStderr = startup->hStdError = req->hstderr;
251 lstrcpynA( cmd_line, req->cmdline, sizeof(cmd_line) );
253 /* Copy the parent environment */
255 if (!ENV_InheritEnvironment( req->env_ptr )) return FALSE;
257 /* Copy the command line */
259 if (!(pdb->env_db->cmd_line = HEAP_strdupA( GetProcessHeap(), 0, cmd_line )))
260 return FALSE;
262 return TRUE;
266 /***********************************************************************
267 * PROCESS_FreePDB
269 * Free a PDB and all associated storage.
271 void PROCESS_FreePDB( PDB *pdb )
273 PDB **pptr = &PROCESS_First;
275 ENV_FreeEnvironment( pdb );
276 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
277 if (*pptr) *pptr = pdb->next;
278 HeapFree( SystemHeap, 0, pdb );
282 /***********************************************************************
283 * PROCESS_CreatePDB
285 * Allocate and fill a PDB structure.
286 * Runs in the context of the parent process.
288 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
290 PDB *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB) );
292 if (!pdb) return NULL;
293 pdb->exit_code = STILL_ACTIVE;
294 pdb->threads = 1;
295 pdb->running_threads = 1;
296 pdb->ring0_threads = 1;
297 pdb->parent = parent;
298 pdb->group = pdb;
299 pdb->priority = 8; /* Normal */
300 pdb->next = PROCESS_First;
301 pdb->winver = 0xffff; /* to be determined */
302 pdb->main_queue = INVALID_HANDLE_VALUE16;
303 PROCESS_First = pdb;
304 return pdb;
308 /***********************************************************************
309 * PROCESS_Init
311 BOOL PROCESS_Init( BOOL win32 )
313 TEB *teb;
314 int server_fd;
316 /* Start the server */
317 server_fd = CLIENT_InitServer();
319 /* Fill the initial process structure */
320 initial_pdb.exit_code = STILL_ACTIVE;
321 initial_pdb.threads = 1;
322 initial_pdb.running_threads = 1;
323 initial_pdb.ring0_threads = 1;
324 initial_pdb.group = &initial_pdb;
325 initial_pdb.priority = 8; /* Normal */
326 initial_pdb.flags = win32? 0 : PDB32_WIN16_PROC;
327 initial_pdb.winver = 0xffff; /* to be determined */
328 initial_pdb.main_queue = INVALID_HANDLE_VALUE16;
330 /* Initialize virtual memory management */
331 if (!VIRTUAL_Init()) return FALSE;
333 /* Create the initial thread structure and socket pair */
334 if (!(teb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
336 /* Remember TEB selector of initial process for emergency use */
337 SYSLEVEL_EmergencyTeb = teb->teb_sel;
339 /* Create the system and process heaps */
340 if (!HEAP_CreateSystemHeap()) return FALSE;
341 initial_pdb.heap = HeapCreate( HEAP_GROWABLE, 0, 0 );
343 /* Create the idle event for the initial process
344 FIXME 1: Shouldn't we call UserSignalProc for the initial process too?
345 FIXME 2: It seems to me that the initial pdb becomes never freed, so I don't now
346 where to release the idle event for the initial process.
348 initial_pdb.idle_event = CreateEventA ( NULL, TRUE, FALSE, NULL );
349 initial_pdb.idle_event = ConvertToGlobalHandle ( initial_pdb.idle_event );
351 /* Initialize signal handling */
352 if (!SIGNAL_Init()) return FALSE;
354 /* Create the environment DB of the first process */
355 if (!PROCESS_CreateEnvDB()) return FALSE;
357 /* Create the SEGPTR heap */
358 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
360 /* Initialize the first process critical section */
361 InitializeCriticalSection( &initial_pdb.crit_section );
363 return TRUE;
367 /***********************************************************************
368 * PROCESS_Start
370 * Startup routine of a new process. Called in the context of the new process.
372 void PROCESS_Start(void)
374 UINT cmdShow = SW_SHOWNORMAL;
375 LPTHREAD_START_ROUTINE entry = NULL;
376 PDB *pdb = PROCESS_Current();
377 NE_MODULE *pModule = NE_GetPtr( pdb->module );
378 LPCSTR filename = ((OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo))->szPathName;
379 IMAGE_OPTIONAL_HEADER *header = !pModule->module32? NULL :
380 &PE_HEADER(pModule->module32)->OptionalHeader;
382 /* Get process type */
383 enum { PROC_DOS, PROC_WIN16, PROC_WIN32 } type;
384 if ( pdb->flags & PDB32_DOS_PROC )
385 type = PROC_DOS;
386 else if ( pdb->flags & PDB32_WIN16_PROC )
387 type = PROC_WIN16;
388 else
389 type = PROC_WIN32;
391 /* Initialize the critical section */
392 InitializeCriticalSection( &pdb->crit_section );
394 /* Create the heap */
395 if (!(pdb->heap = GetProcessHeap()))
397 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE,
398 header? header->SizeOfHeapReserve : 0x10000,
399 header? header->SizeOfHeapCommit : 0 )))
400 goto error;
403 /* Create the environment db */
404 if (!PROCESS_CreateEnvDB()) goto error;
406 /* Create a task for this process */
407 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
408 cmdShow = pdb->env_db->startup_info->wShowWindow;
409 if (!TASK_Create( pModule, cmdShow ))
410 goto error;
412 /* Load all process modules */
413 switch ( type )
415 case PROC_WIN16:
416 if ( !NE_InitProcess( pModule ) )
417 goto error;
418 break;
420 case PROC_WIN32:
421 /* Create 32-bit MODREF */
422 if ( !PE_CreateModule( pModule->module32, filename, 0, FALSE ) )
423 goto error;
425 /* Increment EXE refcount */
426 assert( pdb->exe_modref );
427 pdb->exe_modref->refCount++;
429 /* Retrieve entry point address */
430 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
431 OptionalHeader.AddressOfEntryPoint);
432 break;
434 case PROC_DOS:
435 /* FIXME: move DOS startup code here */
436 break;
440 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
441 * context of the parent process. Actually, the USER signal proc
442 * doesn't really care about that, but it *does* require that the
443 * startup parameters are correctly set up, so that GetProcessDword
444 * works. Furthermore, before calling the USER signal proc the
445 * 16-bit stack must be set up, which it is only after TASK_Create
446 * in the case of a 16-bit process. Thus, we send the signal here.
449 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0, 0 );
450 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, GetCurrentThreadId(), 0 );
451 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0, 0 );
452 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0, 0 );
454 /* Signal the parent process to continue */
455 server_call( REQ_INIT_PROCESS_DONE );
457 /* Send all required start-up debugger events */
458 if ( type == PROC_WIN32 && (pdb->flags & PDB32_DEBUGGED) )
460 EnterCriticalSection( &pdb->crit_section );
462 DEBUG_SendCreateProcessEvent( -1 /*FIXME*/, pModule->module32, entry );
463 MODULE_SendLoadDLLEvents();
465 LeaveCriticalSection( &pdb->crit_section );
468 if ( (pdb->flags & PDB32_CONSOLE_PROC) || (pdb->flags & PDB32_DOS_PROC) )
469 AllocConsole();
471 /* Perform Win32 specific process initialization */
472 if ( type == PROC_WIN32 )
474 EnterCriticalSection( &pdb->crit_section );
476 PE_InitTls();
477 MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
479 LeaveCriticalSection( &pdb->crit_section );
482 /* If requested, add entry point breakpoint */
483 if ( Options.debug || (pdb->flags & PDB32_DEBUGGED) )
484 DEBUG_AddTaskEntryBreakpoint( pdb->task );
486 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
487 if ( type != PROC_WIN16 && (pdb->flags & PDB32_CONSOLE_PROC))
488 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0, 0 );
490 switch ( type )
492 case PROC_DOS:
493 TRACE_(relay)( "Starting DOS process\n" );
494 DOSVM_Enter( NULL );
495 ERR_(relay)( "DOSVM_Enter returned; should not happen!\n" );
496 ExitProcess( 0 );
498 case PROC_WIN16:
499 TRACE_(relay)( "Starting Win16 process\n" );
500 TASK_CallToStart();
501 ERR_(relay)( "TASK_CallToStart returned; should not happen!\n" );
502 ExitProcess( 0 );
504 case PROC_WIN32:
505 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
506 if (pdb->flags & PDB32_DEBUGGED) DebugBreak();
507 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
508 * Dunno about other OSs */
509 ExitProcess( entry(NULL) );
512 error:
513 ExitProcess( GetLastError() );
517 /***********************************************************************
518 * PROCESS_Create
520 * Create a new process database and associated info.
522 PDB *PROCESS_Create( NE_MODULE *pModule, HFILE hFile, LPCSTR cmd_line, LPCSTR env,
523 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
524 BOOL inherit, DWORD flags, STARTUPINFOA *startup,
525 PROCESS_INFORMATION *info )
527 HANDLE handles[2], load_done_evt = 0;
528 DWORD exitcode, size;
529 BOOL alloc_stack16;
530 int server_thandle, fd = -1;
531 struct new_process_request *req = get_req_buffer();
532 TEB *teb = NULL;
533 PDB *parent = PROCESS_Current();
534 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
536 if (!pdb) return NULL;
537 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
538 if (!(load_done_evt = CreateEventA( NULL, TRUE, FALSE, NULL ))) goto error;
540 /* Create the process on the server side */
542 req->inherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
543 req->inherit_all = inherit;
544 req->create_flags = flags;
545 req->start_flags = startup->dwFlags;
546 req->exe_file = hFile;
547 req->event = load_done_evt;
548 if (startup->dwFlags & STARTF_USESTDHANDLES)
550 req->hstdin = startup->hStdInput;
551 req->hstdout = startup->hStdOutput;
552 req->hstderr = startup->hStdError;
554 else
556 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
557 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
558 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
560 req->cmd_show = startup->wShowWindow;
561 req->env_ptr = (void*)env; /* FIXME: hack */
562 lstrcpynA( req->cmdline, cmd_line, server_remaining(req->cmdline) );
563 if (server_call_fd( REQ_NEW_PROCESS, -1, &fd )) goto error;
564 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
565 pdb->server_pid = req->pid;
566 info->hProcess = req->phandle;
567 info->dwProcessId = (DWORD)req->pid;
568 info->hThread = req->thandle;
569 info->dwThreadId = (DWORD)req->tid;
571 if ((flags & (DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS)) ||
572 ((parent->flags & PDB32_DEBUGGED) && !(flags & DEBUG_ONLY_THIS_PROCESS)))
573 pdb->flags |= PDB32_DEBUGGED;
575 if (pModule->module32) /* Win32 process */
577 IMAGE_OPTIONAL_HEADER *header = &PE_HEADER(pModule->module32)->OptionalHeader;
578 size = header->SizeOfStackReserve;
579 if (header->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
580 pdb->flags |= PDB32_CONSOLE_PROC;
581 alloc_stack16 = TRUE;
583 else if (!pModule->dos_image) /* Win16 process */
585 alloc_stack16 = FALSE;
586 size = 0;
587 pdb->flags |= PDB32_WIN16_PROC;
589 else /* DOS process */
591 alloc_stack16 = FALSE;
592 size = 0;
593 pdb->flags |= PDB32_DOS_PROC;
596 /* Create the main thread */
598 if (!(teb = THREAD_Create( pdb, fd, flags & CREATE_SUSPENDED, size,
599 alloc_stack16, tsa, &server_thandle ))) goto error;
600 teb->tid = (void *)info->dwThreadId;
601 teb->startup = PROCESS_Start;
602 fd = -1; /* don't close it */
604 /* Pass module to new process (FIXME: hack) */
605 pdb->module = pModule->self;
606 SYSDEPS_SpawnThread( teb );
608 /* Wait until process is initialized (or initialization failed) */
609 handles[0] = info->hProcess;
610 handles[1] = load_done_evt;
612 switch ( WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) )
614 default:
615 ERR( "WaitForMultipleObjects failed\n" );
616 break;
618 case 0:
619 /* Child initialization code returns error condition as exitcode */
620 if ( GetExitCodeProcess( info->hProcess, &exitcode ) )
621 SetLastError( exitcode );
622 goto error;
624 case 1:
625 /* Get 16-bit task up and running */
626 if ( pdb->flags & PDB32_WIN16_PROC )
628 /* Post event to start the task */
629 PostEvent16( pdb->task );
631 /* If we ourselves are a 16-bit task, we Yield() directly. */
632 if ( parent->flags & PDB32_WIN16_PROC )
633 OldYield16();
635 break;
638 CloseHandle( load_done_evt );
639 load_done_evt = 0;
641 return pdb;
643 error:
644 if (load_done_evt) CloseHandle( load_done_evt );
645 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
646 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
647 PROCESS_FreePDB( pdb );
648 if (fd != -1) close( fd );
649 return NULL;
653 /***********************************************************************
654 * ExitProcess (KERNEL32.100)
656 void WINAPI ExitProcess( DWORD status )
658 EnterCriticalSection( &PROCESS_Current()->crit_section );
659 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
660 LeaveCriticalSection( &PROCESS_Current()->crit_section );
662 TASK_KillTask( 0 );
663 TerminateProcess( GetCurrentProcess(), status );
666 /***********************************************************************
667 * ExitProcess16 (KERNEL.466)
669 void WINAPI ExitProcess16( WORD status )
671 SYSLEVEL_ReleaseWin16Lock();
672 ExitProcess( status );
675 /******************************************************************************
676 * TerminateProcess (KERNEL32.684)
678 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
680 struct terminate_process_request *req = get_req_buffer();
681 req->handle = handle;
682 req->exit_code = exit_code;
683 return !server_call( REQ_TERMINATE_PROCESS );
687 /***********************************************************************
688 * GetProcessDword (KERNEL32.18) (KERNEL.485)
689 * 'Of course you cannot directly access Windows internal structures'
691 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
693 PDB *process = PROCESS_IdToPDB( dwProcessID );
694 TDB *pTask;
695 DWORD x, y;
697 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
698 if ( !process ) return 0;
700 switch ( offset )
702 case GPD_APP_COMPAT_FLAGS:
703 pTask = (TDB *)GlobalLock16( process->task );
704 return pTask? pTask->compat_flags : 0;
706 case GPD_LOAD_DONE_EVENT:
707 return process->load_done_evt;
709 case GPD_HINSTANCE16:
710 pTask = (TDB *)GlobalLock16( process->task );
711 return pTask? pTask->hInstance : 0;
713 case GPD_WINDOWS_VERSION:
714 pTask = (TDB *)GlobalLock16( process->task );
715 return pTask? pTask->version : 0;
717 case GPD_THDB:
718 if ( process != PROCESS_Current() ) return 0;
719 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
721 case GPD_PDB:
722 return (DWORD)process;
724 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
725 return process->env_db->startup_info->hStdOutput;
727 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
728 return process->env_db->startup_info->hStdInput;
730 case GPD_STARTF_SHOWWINDOW:
731 return process->env_db->startup_info->wShowWindow;
733 case GPD_STARTF_SIZE:
734 x = process->env_db->startup_info->dwXSize;
735 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
736 y = process->env_db->startup_info->dwYSize;
737 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
738 return MAKELONG( x, y );
740 case GPD_STARTF_POSITION:
741 x = process->env_db->startup_info->dwX;
742 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
743 y = process->env_db->startup_info->dwY;
744 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
745 return MAKELONG( x, y );
747 case GPD_STARTF_FLAGS:
748 return process->env_db->startup_info->dwFlags;
750 case GPD_PARENT:
751 return process->parent? (DWORD)process->parent->server_pid : 0;
753 case GPD_FLAGS:
754 return process->flags;
756 case GPD_USERDATA:
757 return process->process_dword;
759 default:
760 ERR_(win32)("Unknown offset %d\n", offset );
761 return 0;
765 /***********************************************************************
766 * SetProcessDword (KERNEL.484)
767 * 'Of course you cannot directly access Windows internal structures'
769 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
771 PDB *process = PROCESS_IdToPDB( dwProcessID );
773 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
774 if ( !process ) return;
776 switch ( offset )
778 case GPD_APP_COMPAT_FLAGS:
779 case GPD_LOAD_DONE_EVENT:
780 case GPD_HINSTANCE16:
781 case GPD_WINDOWS_VERSION:
782 case GPD_THDB:
783 case GPD_PDB:
784 case GPD_STARTF_SHELLDATA:
785 case GPD_STARTF_HOTKEY:
786 case GPD_STARTF_SHOWWINDOW:
787 case GPD_STARTF_SIZE:
788 case GPD_STARTF_POSITION:
789 case GPD_STARTF_FLAGS:
790 case GPD_PARENT:
791 case GPD_FLAGS:
792 ERR_(win32)("Not allowed to modify offset %d\n", offset );
793 break;
795 case GPD_USERDATA:
796 process->process_dword = value;
797 break;
799 default:
800 ERR_(win32)("Unknown offset %d\n", offset );
801 break;
806 /***********************************************************************
807 * GetCurrentProcess (KERNEL32.198)
809 HANDLE WINAPI GetCurrentProcess(void)
811 return CURRENT_PROCESS_PSEUDOHANDLE;
815 /*********************************************************************
816 * OpenProcess (KERNEL32.543)
818 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
820 HANDLE ret = 0;
821 struct open_process_request *req = get_req_buffer();
823 req->pid = (void *)id;
824 req->access = access;
825 req->inherit = inherit;
826 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
827 return ret;
830 /*********************************************************************
831 * MapProcessHandle (KERNEL.483)
833 DWORD WINAPI MapProcessHandle( HANDLE handle )
835 DWORD ret = 0;
836 struct get_process_info_request *req = get_req_buffer();
837 req->handle = handle;
838 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
839 return ret;
842 /***********************************************************************
843 * GetCurrentProcessId (KERNEL32.199)
845 DWORD WINAPI GetCurrentProcessId(void)
847 return (DWORD)PROCESS_Current()->server_pid;
851 /***********************************************************************
852 * GetThreadLocale (KERNEL32.295)
854 LCID WINAPI GetThreadLocale(void)
856 return PROCESS_Current()->locale;
860 /***********************************************************************
861 * SetPriorityClass (KERNEL32.503)
863 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
865 struct set_process_info_request *req = get_req_buffer();
866 req->handle = hprocess;
867 req->priority = priorityclass;
868 req->mask = SET_PROCESS_INFO_PRIORITY;
869 return !server_call( REQ_SET_PROCESS_INFO );
873 /***********************************************************************
874 * GetPriorityClass (KERNEL32.250)
876 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
878 DWORD ret = 0;
879 struct get_process_info_request *req = get_req_buffer();
880 req->handle = hprocess;
881 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
882 return ret;
886 /***********************************************************************
887 * SetProcessAffinityMask (KERNEL32.662)
889 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
891 struct set_process_info_request *req = get_req_buffer();
892 req->handle = hProcess;
893 req->affinity = affmask;
894 req->mask = SET_PROCESS_INFO_AFFINITY;
895 return !server_call( REQ_SET_PROCESS_INFO );
898 /**********************************************************************
899 * GetProcessAffinityMask (KERNEL32.373)
901 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
902 LPDWORD lpProcessAffinityMask,
903 LPDWORD lpSystemAffinityMask )
905 BOOL ret = FALSE;
906 struct get_process_info_request *req = get_req_buffer();
907 req->handle = hProcess;
908 if (!server_call( REQ_GET_PROCESS_INFO ))
910 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
911 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
912 ret = TRUE;
914 return ret;
918 /***********************************************************************
919 * GetStdHandle (KERNEL32.276)
921 HANDLE WINAPI GetStdHandle( DWORD std_handle )
923 PDB *pdb = PROCESS_Current();
925 switch(std_handle)
927 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
928 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
929 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
931 SetLastError( ERROR_INVALID_PARAMETER );
932 return INVALID_HANDLE_VALUE;
936 /***********************************************************************
937 * SetStdHandle (KERNEL32.506)
939 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
941 PDB *pdb = PROCESS_Current();
942 /* FIXME: should we close the previous handle? */
943 switch(std_handle)
945 case STD_INPUT_HANDLE:
946 pdb->env_db->hStdin = handle;
947 return TRUE;
948 case STD_OUTPUT_HANDLE:
949 pdb->env_db->hStdout = handle;
950 return TRUE;
951 case STD_ERROR_HANDLE:
952 pdb->env_db->hStderr = handle;
953 return TRUE;
955 SetLastError( ERROR_INVALID_PARAMETER );
956 return FALSE;
959 /***********************************************************************
960 * GetProcessVersion (KERNEL32)
962 DWORD WINAPI GetProcessVersion( DWORD processid )
964 TDB *pTask;
965 PDB *pdb = PROCESS_IdToPDB( processid );
967 if (!pdb) return 0;
968 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
969 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
972 /***********************************************************************
973 * GetProcessFlags (KERNEL32)
975 DWORD WINAPI GetProcessFlags( DWORD processid )
977 PDB *pdb = PROCESS_IdToPDB( processid );
978 if (!pdb) return 0;
979 return pdb->flags;
982 /***********************************************************************
983 * SetProcessWorkingSetSize [KERNEL32.662]
984 * Sets the min/max working set sizes for a specified process.
986 * PARAMS
987 * hProcess [I] Handle to the process of interest
988 * minset [I] Specifies minimum working set size
989 * maxset [I] Specifies maximum working set size
991 * RETURNS STD
993 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
994 DWORD maxset)
996 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
997 if(( minset == -1) && (maxset == -1)) {
998 /* Trim the working set to zero */
999 /* Swap the process out of physical RAM */
1001 return TRUE;
1004 /***********************************************************************
1005 * GetProcessWorkingSetSize (KERNEL32)
1007 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1008 LPDWORD maxset)
1010 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1011 /* 32 MB working set size */
1012 if (minset) *minset = 32*1024*1024;
1013 if (maxset) *maxset = 32*1024*1024;
1014 return TRUE;
1017 /***********************************************************************
1018 * SetProcessShutdownParameters (KERNEL32)
1020 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1021 * Now tracks changes made (but does not act on these changes)
1022 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1023 * It really shouldn't be here, but I'll move it when it's been checked!
1025 #define SHUTDOWN_NORETRY 1
1026 static unsigned int shutdown_noretry = 0;
1027 static unsigned int shutdown_priority = 0x280L;
1028 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1030 if (flags & SHUTDOWN_NORETRY)
1031 shutdown_noretry = 1;
1032 else
1033 shutdown_noretry = 0;
1034 if (level > 0x100L && level < 0x3FFL)
1035 shutdown_priority = level;
1036 else
1038 ERR("invalid priority level 0x%08lx\n", level);
1039 return FALSE;
1041 return TRUE;
1045 /***********************************************************************
1046 * GetProcessShutdownParameters (KERNEL32)
1049 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1050 LPDWORD lpdwFlags )
1052 (*lpdwLevel) = shutdown_priority;
1053 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1054 return TRUE;
1056 /***********************************************************************
1057 * SetProcessPriorityBoost (KERNEL32)
1059 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1061 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1062 /* Say we can do it. I doubt the program will notice that we don't. */
1063 return TRUE;
1067 /***********************************************************************
1068 * ReadProcessMemory (KERNEL32)
1070 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1071 LPDWORD bytes_read )
1073 struct read_process_memory_request *req = get_req_buffer();
1074 unsigned int offset = (unsigned int)addr % sizeof(int);
1075 unsigned int max = server_remaining( req->data ); /* max length in one request */
1076 unsigned int pos;
1078 if (bytes_read) *bytes_read = size;
1080 /* first time, read total length to check for permissions */
1081 req->handle = process;
1082 req->addr = (char *)addr - offset;
1083 req->len = (size + offset + sizeof(int) - 1) / sizeof(int);
1084 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1086 if (size <= max - offset)
1088 memcpy( buffer, (char *)req->data + offset, size );
1089 return TRUE;
1092 /* now take care of the remaining data */
1093 memcpy( buffer, (char *)req->data + offset, max - offset );
1094 pos = max - offset;
1095 size -= pos;
1096 while (size)
1098 if (max > size) max = size;
1099 req->handle = process;
1100 req->addr = (char *)addr + pos;
1101 req->len = (max + sizeof(int) - 1) / sizeof(int);
1102 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1103 memcpy( (char *)buffer + pos, (char *)req->data, max );
1104 size -= max;
1105 pos += max;
1107 return TRUE;
1109 error:
1110 if (bytes_read) *bytes_read = 0;
1111 return FALSE;
1115 /***********************************************************************
1116 * WriteProcessMemory (KERNEL32)
1118 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1119 LPDWORD bytes_written )
1121 unsigned int first_offset, last_offset;
1122 struct write_process_memory_request *req = get_req_buffer();
1123 unsigned int max = server_remaining( req->data ); /* max length in one request */
1124 unsigned int pos, last_mask;
1126 if (!size)
1128 SetLastError( ERROR_INVALID_PARAMETER );
1129 return FALSE;
1131 if (bytes_written) *bytes_written = size;
1133 /* compute the mask for the first int */
1134 req->first_mask = ~0;
1135 first_offset = (unsigned int)addr % sizeof(int);
1136 memset( &req->first_mask, 0, first_offset );
1138 /* compute the mask for the last int */
1139 last_offset = (size + first_offset) % sizeof(int);
1140 last_mask = 0;
1141 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1143 req->handle = process;
1144 req->addr = (char *)addr - first_offset;
1145 /* for the first request, use the total length */
1146 req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1148 if (size + first_offset < max) /* we can do it in one round */
1150 memcpy( (char *)req->data + first_offset, buffer, size );
1151 req->last_mask = last_mask;
1152 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1153 return TRUE;
1156 /* needs multiple server calls */
1158 memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1159 req->last_mask = ~0;
1160 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1161 pos = max - first_offset;
1162 size -= pos;
1163 while (size)
1165 if (size <= max) /* last one */
1167 req->last_mask = last_mask;
1168 max = size;
1170 req->handle = process;
1171 req->addr = (char *)addr + pos;
1172 req->len = (max + sizeof(int) - 1) / sizeof(int);
1173 req->first_mask = ~0;
1174 memcpy( req->data, (char *) buffer + pos, max );
1175 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1176 pos += max;
1177 size -= max;
1179 return TRUE;
1181 error:
1182 if (bytes_written) *bytes_written = 0;
1183 return FALSE;
1188 /***********************************************************************
1189 * RegisterServiceProcess (KERNEL, KERNEL32)
1191 * A service process calls this function to ensure that it continues to run
1192 * even after a user logged off.
1194 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1196 /* I don't think that Wine needs to do anything in that function */
1197 return 1; /* success */
1200 /***********************************************************************
1201 * GetExitCodeProcess [KERNEL32.325]
1203 * Gets termination status of specified process
1205 * RETURNS
1206 * Success: TRUE
1207 * Failure: FALSE
1209 BOOL WINAPI GetExitCodeProcess(
1210 HANDLE hProcess, /* [I] handle to the process */
1211 LPDWORD lpExitCode) /* [O] address to receive termination status */
1213 BOOL ret = FALSE;
1214 struct get_process_info_request *req = get_req_buffer();
1215 req->handle = hProcess;
1216 if (!server_call( REQ_GET_PROCESS_INFO ))
1218 if (lpExitCode) *lpExitCode = req->exit_code;
1219 ret = TRUE;
1221 return ret;
1225 /***********************************************************************
1226 * SetErrorMode (KERNEL32.486)
1228 UINT WINAPI SetErrorMode( UINT mode )
1230 UINT old = PROCESS_Current()->error_mode;
1231 PROCESS_Current()->error_mode = mode;
1232 return old;