Implemented PROCESS_CallUserSignalProc().
[wine/hacks.git] / scheduler / process.c
blob4e8f4be7ed363de2bb4074c08c0736778638267c
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 "debug.h"
29 /* The initial process PDB */
30 static PDB initial_pdb;
32 static PDB *PROCESS_First = &initial_pdb;
34 /***********************************************************************
35 * PROCESS_Current
37 PDB *PROCESS_Current(void)
39 return THREAD_Current()->process;
42 /***********************************************************************
43 * PROCESS_Initial
45 * FIXME: This works only while running all processes in the same
46 * address space (or, at least, the initial process is mapped
47 * into all address spaces as is KERNEL32 in Windows 95)
50 PDB *PROCESS_Initial(void)
52 return &initial_pdb;
55 /***********************************************************************
56 * PROCESS_QueryInfo
58 * Retrieve information about a process
60 static BOOL PROCESS_QueryInfo( HANDLE handle,
61 struct get_process_info_reply *reply )
63 struct get_process_info_request req;
64 req.handle = handle;
65 CLIENT_SendRequest( REQ_GET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
66 return !CLIENT_WaitSimpleReply( reply, sizeof(*reply), NULL );
69 /***********************************************************************
70 * PROCESS_IsCurrent
72 * Check if a handle is to the current process
74 BOOL PROCESS_IsCurrent( HANDLE handle )
76 struct get_process_info_reply reply;
77 return (PROCESS_QueryInfo( handle, &reply ) &&
78 (reply.pid == PROCESS_Current()->server_pid));
82 /***********************************************************************
83 * PROCESS_IdToPDB
85 * Convert a process id to a PDB, making sure it is valid.
87 PDB *PROCESS_IdToPDB( DWORD id )
89 PDB *pdb;
91 if (!id) return PROCESS_Current();
92 pdb = PROCESS_First;
93 while (pdb)
95 if ((DWORD)pdb->server_pid == id) return pdb;
96 pdb = pdb->next;
98 SetLastError( ERROR_INVALID_PARAMETER );
99 return NULL;
103 /***********************************************************************
104 * PROCESS_CallUserSignalProc
106 * FIXME: Some of the signals aren't sent correctly!
108 * The exact meaning of the USER signals is undocumented, but this
109 * should cover the basic idea:
111 * USIG_DLL_UNLOAD_WIN16
112 * This is sent when a 16-bit module is unloaded.
114 * USIG_DLL_UNLOAD_WIN32
115 * This is sent when a 32-bit module is unloaded.
117 * USIG_DLL_UNLOAD_ORPHANS
118 * This is sent after the last Win3.1 module is unloaded,
119 * to allow removal of orphaned menus.
121 * USIG_FAULT_DIALOG_PUSH
122 * USIG_FAULT_DIALOG_POP
123 * These are called to allow USER to prepare for displaying a
124 * fault dialog, even though the fault might have happened while
125 * inside a USER critical section.
127 * USIG_THREAD_INIT
128 * This is called from the context of a new thread, as soon as it
129 * has started to run.
131 * USIG_THREAD_EXIT
132 * This is called, still in its context, just before a thread is
133 * about to terminate.
135 * USIG_PROCESS_CREATE
136 * This is called, in the parent process context, after a new process
137 * has been created.
139 * USIG_PROCESS_INIT
140 * This is called in the new process context, just after the main thread
141 * has started execution (after the main thread's USIG_THREAD_INIT has
142 * been sent).
144 * USIG_PROCESS_LOADED
145 * This is called after the executable file has been loaded into the
146 * new process context.
148 * USIG_PROCESS_RUNNING
149 * This is called immediately before the main entry point is called.
151 * USIG_PROCESS_EXIT
152 * This is called in the context of a process that is about to
153 * terminate (but before the last thread's USIG_THREAD_EXIT has
154 * been sent).
156 * USIG_PROCESS_DESTROY
157 * This is called after a process has terminated.
160 * The meaning of the dwFlags bits is as follows:
162 * USIG_FLAGS_WIN32
163 * Current process is 32-bit.
165 * USIG_FLAGS_GUI
166 * Current process is a (Win32) GUI process.
168 * USIG_FLAGS_FEEDBACK
169 * Current process needs 'feedback' (determined from the STARTUPINFO
170 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
172 * USIG_FLAGS_FAULT
173 * The signal is being sent due to a fault.
175 void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
177 PDB *pdb = PROCESS_Current();
178 STARTUPINFOA *startup = pdb->env_db? pdb->env_db->startup_info : NULL;
179 DWORD dwFlags = 0, dwThreadOrProcessID;
181 /* Determine dwFlags */
183 if ( !(pdb->flags & PDB32_WIN16_PROC) )
184 dwFlags |= USIG_FLAGS_WIN32;
186 if ( !(pdb->flags & PDB32_CONSOLE_PROC) )
187 dwFlags |= USIG_FLAGS_GUI;
189 if ( dwFlags & USIG_FLAGS_GUI )
191 /* Feedback defaults to ON */
192 if ( !(startup && (startup->dwFlags & STARTF_FORCEOFFFEEDBACK)) )
193 dwFlags |= USIG_FLAGS_FEEDBACK;
195 else
197 /* Feedback defaults to OFF */
198 if ( startup && (startup->dwFlags & STARTF_FORCEONFEEDBACK) )
199 dwFlags |= USIG_FLAGS_FEEDBACK;
202 /* Get thread or process ID */
204 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
205 dwThreadOrProcessID = GetCurrentThreadId();
206 else
207 dwThreadOrProcessID = GetCurrentProcessId();
209 /* Convert module handle to 16-bit */
211 if ( HIWORD( hModule ) )
212 hModule = MapHModuleLS( hModule );
214 /* Call USER signal proc */
216 if ( Callout.UserSignalProc )
217 Callout.UserSignalProc( uCode, dwThreadOrProcessID, dwFlags, hModule );
221 /***********************************************************************
222 * PROCESS_BuildEnvDB
224 * Build the env DB for the initial process
226 static BOOL PROCESS_BuildEnvDB( PDB *pdb )
228 /* Allocate the env DB (FIXME: should not be on the system heap) */
230 if (!(pdb->env_db = HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(ENVDB))))
231 return FALSE;
232 InitializeCriticalSection( &pdb->env_db->section );
234 /* Allocate startup info */
235 if (!(pdb->env_db->startup_info =
236 HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
237 return FALSE;
239 /* Allocate the standard handles */
241 pdb->env_db->hStdin = FILE_DupUnixHandle( 0, GENERIC_READ );
242 pdb->env_db->hStdout = FILE_DupUnixHandle( 1, GENERIC_WRITE );
243 pdb->env_db->hStderr = FILE_DupUnixHandle( 2, GENERIC_WRITE );
245 /* Build the command-line */
247 pdb->env_db->cmd_line = HEAP_strdupA( SystemHeap, 0, "kernel32" );
249 /* Build the environment strings */
251 return ENV_BuildEnvironment( pdb );
255 /***********************************************************************
256 * PROCESS_InheritEnvDB
258 static BOOL PROCESS_InheritEnvDB( PDB *pdb, LPCSTR cmd_line, LPCSTR env,
259 BOOL inherit_handles, STARTUPINFOA *startup )
261 if (!(pdb->env_db = HeapAlloc(pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB))))
262 return FALSE;
263 InitializeCriticalSection( &pdb->env_db->section );
265 /* Copy the parent environment */
267 if (!ENV_InheritEnvironment( pdb, env )) return FALSE;
269 /* Copy the command line */
271 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
272 return FALSE;
274 /* Remember startup info */
275 if (!(pdb->env_db->startup_info =
276 HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
277 return FALSE;
278 *pdb->env_db->startup_info = *startup;
280 /* Inherit the standard handles */
281 if (pdb->env_db->startup_info->dwFlags & STARTF_USESTDHANDLES)
283 pdb->env_db->hStdin = pdb->env_db->startup_info->hStdInput;
284 pdb->env_db->hStdout = pdb->env_db->startup_info->hStdOutput;
285 pdb->env_db->hStderr = pdb->env_db->startup_info->hStdError;
287 else if (inherit_handles)
289 pdb->env_db->hStdin = pdb->parent->env_db->hStdin;
290 pdb->env_db->hStdout = pdb->parent->env_db->hStdout;
291 pdb->env_db->hStderr = pdb->parent->env_db->hStderr;
293 /* else will be done later on in PROCESS_Create */
295 return TRUE;
299 /***********************************************************************
300 * PROCESS_CreateEnvDB
302 * Create the env DB for a newly started process.
304 static BOOL PROCESS_CreateEnvDB(void)
306 struct init_process_request req;
307 struct init_process_reply reply;
308 STARTUPINFOA *startup;
309 ENVDB *env_db;
310 PDB *pdb = PROCESS_Current();
312 /* Retrieve startup info from the server */
314 req.dummy = 0;
315 CLIENT_SendRequest( REQ_INIT_PROCESS, -1, 1, &req, sizeof(req) );
316 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
318 /* Allocate the env DB */
320 if (!(env_db = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
321 return FALSE;
322 pdb->env_db = env_db;
323 InitializeCriticalSection( &env_db->section );
325 /* Allocate and fill the startup info */
326 if (!(startup = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
327 return FALSE;
328 pdb->env_db->startup_info = startup;
329 startup->dwFlags = reply.start_flags;
330 pdb->env_db->hStdin = startup->hStdInput = reply.hstdin;
331 pdb->env_db->hStdout = startup->hStdOutput = reply.hstdout;
332 pdb->env_db->hStderr = startup->hStdError = reply.hstderr;
334 #if 0 /* FIXME */
335 /* Copy the parent environment */
337 if (!ENV_InheritEnvironment( pdb, env )) return FALSE;
339 /* Copy the command line */
341 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
342 return FALSE;
343 #endif
344 return TRUE;
348 /***********************************************************************
349 * PROCESS_FreePDB
351 * Free a PDB and all associated storage.
353 void PROCESS_FreePDB( PDB *pdb )
355 PDB **pptr = &PROCESS_First;
357 ENV_FreeEnvironment( pdb );
358 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
359 if (*pptr) *pptr = pdb->next;
360 if (pdb->heap && (pdb->heap != pdb->system_heap)) HeapDestroy( pdb->heap );
361 HeapFree( SystemHeap, 0, pdb );
365 /***********************************************************************
366 * PROCESS_CreatePDB
368 * Allocate and fill a PDB structure.
369 * Runs in the context of the parent process.
371 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
373 PDB *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB) );
375 if (!pdb) return NULL;
376 pdb->exit_code = 0x103; /* STILL_ACTIVE */
377 pdb->threads = 1;
378 pdb->running_threads = 1;
379 pdb->ring0_threads = 1;
380 pdb->system_heap = SystemHeap;
381 pdb->parent = parent;
382 pdb->group = pdb;
383 pdb->priority = 8; /* Normal */
384 pdb->heap = pdb->system_heap; /* will be changed later on */
385 pdb->next = PROCESS_First;
386 PROCESS_First = pdb;
387 return pdb;
391 /***********************************************************************
392 * PROCESS_Init
394 BOOL PROCESS_Init(void)
396 THDB *thdb;
397 int server_fd;
399 /* Start the server */
400 server_fd = CLIENT_InitServer();
402 /* Fill the initial process structure */
403 initial_pdb.exit_code = 0x103; /* STILL_ACTIVE */
404 initial_pdb.threads = 1;
405 initial_pdb.running_threads = 1;
406 initial_pdb.ring0_threads = 1;
407 initial_pdb.group = &initial_pdb;
408 initial_pdb.priority = 8; /* Normal */
409 initial_pdb.flags = PDB32_WIN16_PROC;
411 /* Initialize virtual memory management */
412 if (!VIRTUAL_Init()) return FALSE;
414 /* Create the initial thread structure and socket pair */
415 if (!(thdb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
417 /* Remember TEB selector of initial process for emergency use */
418 SYSLEVEL_EmergencyTeb = thdb->teb_sel;
420 /* Create the system heap */
421 if (!(SystemHeap = HeapCreate( HEAP_GROWABLE, 0x10000, 0 ))) return FALSE;
422 initial_pdb.system_heap = initial_pdb.heap = SystemHeap;
424 /* Create the environment DB of the first process */
425 if (!PROCESS_BuildEnvDB( &initial_pdb )) return FALSE;
427 /* Create the SEGPTR heap */
428 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
430 /* Initialize the first process critical section */
431 InitializeCriticalSection( &initial_pdb.crit_section );
433 return TRUE;
437 /***********************************************************************
438 * PROCESS_Start
440 * Startup routine of a new process. Called in the context of the new process.
442 void PROCESS_Start(void)
444 DWORD size, commit;
445 UINT cmdShow = 0;
446 LPTHREAD_START_ROUTINE entry;
447 THDB *thdb = THREAD_Current();
448 PDB *pdb = thdb->process;
449 TDB *pTask = (TDB *)GlobalLock16( pdb->task );
450 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
451 OFSTRUCT *ofs = (OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo);
453 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 ); /* for initial thread */
455 #if 0
456 /* Initialize the critical section */
458 InitializeCriticalSection( &pdb->crit_section );
460 /* Create the heap */
462 size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapReserve;
463 commit = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapCommit;
464 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE, size, commit ))) goto error;
465 pdb->heap_list = pdb->heap;
467 /* Create the environment db */
469 if (!PROCESS_CreateEnvDB()) goto error;
471 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
472 cmdShow = pdb->env_db->startup_info->wShowWindow;
473 if (!TASK_Create( thdb, pModule, 0, 0, cmdShow )) goto error;
475 #endif
477 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0 );
479 /* Map system DLLs into this process (from initial process) */
480 /* FIXME: this is a hack */
481 pdb->modref_list = PROCESS_Initial()->modref_list;
483 /* Create 32-bit MODREF */
484 if (!PE_CreateModule( pModule->module32, ofs, 0, FALSE )) goto error;
486 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0 ); /* FIXME: correct location? */
488 /* Initialize thread-local storage */
490 PE_InitTls();
492 if ( pdb->flags & PDB32_CONSOLE_PROC )
493 AllocConsole();
495 /* Now call the entry point */
497 MODULE_InitializeDLLs( 0, DLL_PROCESS_ATTACH, (LPVOID)1 );
499 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0 );
501 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
502 OptionalHeader.AddressOfEntryPoint);
503 TRACE(relay, "(entryproc=%p)\n", entry );
504 ExitProcess( entry(NULL) );
506 error:
507 ExitProcess(1);
511 /***********************************************************************
512 * PROCESS_Create
514 * Create a new process database and associated info.
516 PDB *PROCESS_Create( NE_MODULE *pModule, LPCSTR cmd_line, LPCSTR env,
517 HINSTANCE16 hInstance, HINSTANCE16 hPrevInstance,
518 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
519 BOOL inherit, STARTUPINFOA *startup,
520 PROCESS_INFORMATION *info )
522 DWORD size, commit;
523 int server_thandle;
524 struct new_process_request req;
525 struct new_process_reply reply;
526 UINT cmdShow = 0;
527 THDB *thdb = NULL;
528 PDB *parent = PROCESS_Current();
529 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
531 if (!pdb) return NULL;
532 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
534 /* Create the process on the server side */
536 req.inherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
537 req.inherit_all = inherit;
538 req.start_flags = startup->dwFlags;
539 if (startup->dwFlags & STARTF_USESTDHANDLES)
541 req.hstdin = startup->hStdInput;
542 req.hstdout = startup->hStdOutput;
543 req.hstderr = startup->hStdError;
545 else
547 req.hstdin = GetStdHandle( STD_INPUT_HANDLE );
548 req.hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
549 req.hstderr = GetStdHandle( STD_ERROR_HANDLE );
551 CLIENT_SendRequest( REQ_NEW_PROCESS, -1, 2,
552 &req, sizeof(req), cmd_line, strlen(cmd_line) + 1 );
553 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
554 pdb->server_pid = reply.pid;
555 info->hProcess = reply.handle;
556 info->dwProcessId = (DWORD)pdb->server_pid;
558 /* Initialize the critical section */
560 InitializeCriticalSection( &pdb->crit_section );
562 /* Setup process flags */
564 if ( !pModule->module32 )
565 pdb->flags |= PDB32_WIN16_PROC;
567 else if ( PE_HEADER(pModule->module32)->OptionalHeader.Subsystem
568 == IMAGE_SUBSYSTEM_WINDOWS_CUI )
569 pdb->flags |= PDB32_CONSOLE_PROC;
571 /* Create the heap */
573 if (pModule->module32)
575 size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapReserve;
576 commit = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapCommit;
578 else
580 size = 0x10000;
581 commit = 0;
583 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE, size, commit ))) goto error;
584 pdb->heap_list = pdb->heap;
586 /* Inherit the env DB from the parent */
588 if (!PROCESS_InheritEnvDB( pdb, cmd_line, env, inherit, startup )) goto error;
590 /* Create the main thread */
592 if (pModule->module32)
593 size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfStackReserve;
594 else
595 size = 0;
596 if (!(thdb = THREAD_Create( pdb, 0L, size, hInstance == 0, tsa, &server_thandle )))
597 goto error;
598 info->hThread = server_thandle;
599 info->dwThreadId = (DWORD)thdb->server_tid;
600 thdb->startup = PROCESS_Start;
602 /* Duplicate the standard handles */
604 if ((!(pdb->env_db->startup_info->dwFlags & STARTF_USESTDHANDLES)) && !inherit)
606 DuplicateHandle( GetCurrentProcess(), pdb->parent->env_db->hStdin,
607 info->hProcess, &pdb->env_db->hStdin, 0, TRUE, DUPLICATE_SAME_ACCESS );
608 DuplicateHandle( GetCurrentProcess(), pdb->parent->env_db->hStdout,
609 info->hProcess, &pdb->env_db->hStdout, 0, TRUE, DUPLICATE_SAME_ACCESS );
610 DuplicateHandle( GetCurrentProcess(), pdb->parent->env_db->hStderr,
611 info->hProcess, &pdb->env_db->hStderr, 0, TRUE, DUPLICATE_SAME_ACCESS );
614 /* Create a Win16 task for this process */
616 if (startup->dwFlags & STARTF_USESHOWWINDOW)
617 cmdShow = startup->wShowWindow;
619 if ( !TASK_Create( thdb, pModule, hInstance, hPrevInstance, cmdShow) )
620 goto error;
623 /* Map system DLLs into this process (from initial process) */
624 /* FIXME: this is a hack */
625 pdb->modref_list = PROCESS_Initial()->modref_list;
628 /* Start the task */
630 TASK_StartTask( pdb->task );
632 return pdb;
634 error:
635 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
636 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
637 PROCESS_FreePDB( pdb );
638 return NULL;
642 /***********************************************************************
643 * ExitProcess (KERNEL32.100)
645 void WINAPI ExitProcess( DWORD status )
647 MODULE_InitializeDLLs( 0, DLL_PROCESS_DETACH, (LPVOID)1 );
649 if ( THREAD_IsWin16( THREAD_Current() ) )
650 TASK_KillCurrentTask( status );
652 TASK_KillTask( 0 );
653 TerminateProcess( GetCurrentProcess(), status );
657 /******************************************************************************
658 * TerminateProcess (KERNEL32.684)
660 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
662 struct terminate_process_request req;
663 req.handle = handle;
664 req.exit_code = exit_code;
665 CLIENT_SendRequest( REQ_TERMINATE_PROCESS, -1, 1, &req, sizeof(req) );
666 return !CLIENT_WaitReply( NULL, NULL, 0 );
670 /***********************************************************************
671 * GetProcessDword (KERNEL32.18) (KERNEL.485)
672 * 'Of course you cannot directly access Windows internal structures'
674 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
676 PDB *process = PROCESS_IdToPDB( dwProcessID );
677 TDB *pTask;
678 DWORD x, y;
680 TRACE( win32, "(%ld, %d)\n", dwProcessID, offset );
681 if ( !process ) return 0;
683 switch ( offset )
685 case GPD_APP_COMPAT_FLAGS:
686 pTask = (TDB *)GlobalLock16( process->task );
687 return pTask? pTask->compat_flags : 0;
689 case GPD_LOAD_DONE_EVENT:
690 return process->load_done_evt;
692 case GPD_HINSTANCE16:
693 pTask = (TDB *)GlobalLock16( process->task );
694 return pTask? pTask->hInstance : 0;
696 case GPD_WINDOWS_VERSION:
697 pTask = (TDB *)GlobalLock16( process->task );
698 return pTask? pTask->version : 0;
700 case GPD_THDB:
701 if ( process != PROCESS_Current() ) return 0;
702 return (DWORD)THREAD_Current();
704 case GPD_PDB:
705 return (DWORD)process;
707 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
708 return process->env_db->startup_info->hStdOutput;
710 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
711 return process->env_db->startup_info->hStdInput;
713 case GPD_STARTF_SHOWWINDOW:
714 return process->env_db->startup_info->wShowWindow;
716 case GPD_STARTF_SIZE:
717 x = process->env_db->startup_info->dwXSize;
718 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
719 y = process->env_db->startup_info->dwYSize;
720 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
721 return MAKELONG( x, y );
723 case GPD_STARTF_POSITION:
724 x = process->env_db->startup_info->dwX;
725 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
726 y = process->env_db->startup_info->dwY;
727 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
728 return MAKELONG( x, y );
730 case GPD_STARTF_FLAGS:
731 return process->env_db->startup_info->dwFlags;
733 case GPD_PARENT_PDB:
734 return (DWORD)process->parent;
736 case GPD_FLAGS:
737 return process->flags;
739 case GPD_USERDATA:
740 return process->process_dword;
742 default:
743 ERR( win32, "Unknown offset %d\n", offset );
744 return 0;
748 /***********************************************************************
749 * SetProcessDword (KERNEL.484)
750 * 'Of course you cannot directly access Windows internal structures'
752 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
754 PDB *process = PROCESS_IdToPDB( dwProcessID );
756 TRACE( win32, "(%ld, %d)\n", dwProcessID, offset );
757 if ( !process ) return;
759 switch ( offset )
761 case GPD_APP_COMPAT_FLAGS:
762 case GPD_LOAD_DONE_EVENT:
763 case GPD_HINSTANCE16:
764 case GPD_WINDOWS_VERSION:
765 case GPD_THDB:
766 case GPD_PDB:
767 case GPD_STARTF_SHELLDATA:
768 case GPD_STARTF_HOTKEY:
769 case GPD_STARTF_SHOWWINDOW:
770 case GPD_STARTF_SIZE:
771 case GPD_STARTF_POSITION:
772 case GPD_STARTF_FLAGS:
773 case GPD_PARENT_PDB:
774 case GPD_FLAGS:
775 ERR( win32, "Not allowed to modify offset %d\n", offset );
776 break;
778 case GPD_USERDATA:
779 process->process_dword = value;
780 break;
782 default:
783 ERR( win32, "Unknown offset %d\n", offset );
784 break;
789 /***********************************************************************
790 * GetCurrentProcess (KERNEL32.198)
792 HANDLE WINAPI GetCurrentProcess(void)
794 return CURRENT_PROCESS_PSEUDOHANDLE;
798 /*********************************************************************
799 * OpenProcess (KERNEL32.543)
801 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
803 struct open_process_request req;
804 struct open_process_reply reply;
806 req.pid = (void *)id;
807 req.access = access;
808 req.inherit = inherit;
809 CLIENT_SendRequest( REQ_OPEN_PROCESS, -1, 1, &req, sizeof(req) );
810 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0;
811 return reply.handle;
815 /***********************************************************************
816 * GetCurrentProcessId (KERNEL32.199)
818 DWORD WINAPI GetCurrentProcessId(void)
820 return (DWORD)PROCESS_Current()->server_pid;
824 /***********************************************************************
825 * GetProcessHeap (KERNEL32.259)
827 HANDLE WINAPI GetProcessHeap(void)
829 PDB *pdb = PROCESS_Current();
830 return pdb->heap ? pdb->heap : SystemHeap;
834 /***********************************************************************
835 * GetThreadLocale (KERNEL32.295)
837 LCID WINAPI GetThreadLocale(void)
839 return PROCESS_Current()->locale;
843 /***********************************************************************
844 * SetPriorityClass (KERNEL32.503)
846 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
848 struct set_process_info_request req;
849 req.handle = hprocess;
850 req.priority = priorityclass;
851 req.mask = SET_PROCESS_INFO_PRIORITY;
852 CLIENT_SendRequest( REQ_SET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
853 return !CLIENT_WaitReply( NULL, NULL, 0 );
857 /***********************************************************************
858 * GetPriorityClass (KERNEL32.250)
860 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
862 struct get_process_info_reply reply;
863 if (!PROCESS_QueryInfo( hprocess, &reply )) return 0;
864 return reply.priority;
868 /***********************************************************************
869 * SetProcessAffinityMask (KERNEL32.662)
871 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
873 struct set_process_info_request req;
874 req.handle = hProcess;
875 req.affinity = affmask;
876 req.mask = SET_PROCESS_INFO_AFFINITY;
877 CLIENT_SendRequest( REQ_SET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
878 return !CLIENT_WaitReply( NULL, NULL, 0 );
881 /**********************************************************************
882 * GetProcessAffinityMask (KERNEL32.373)
884 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
885 LPDWORD lpProcessAffinityMask,
886 LPDWORD lpSystemAffinityMask )
888 struct get_process_info_reply reply;
889 if (!PROCESS_QueryInfo( hProcess, &reply )) return FALSE;
890 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply.process_affinity;
891 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply.system_affinity;
892 return TRUE;
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(process,"(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(process,"(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(process,"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(process,"(%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;
1044 /***********************************************************************
1045 * ReadProcessMemory (KERNEL32)
1046 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1047 * ... and add a sizecheck
1049 BOOL WINAPI ReadProcessMemory( HANDLE hProcess, LPCVOID lpBaseAddress,
1050 LPVOID lpBuffer, DWORD nSize,
1051 LPDWORD lpNumberOfBytesRead )
1053 memcpy(lpBuffer,lpBaseAddress,nSize);
1054 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = nSize;
1055 return TRUE;
1058 /***********************************************************************
1059 * WriteProcessMemory (KERNEL32)
1060 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1061 * ... and add a sizecheck
1063 BOOL WINAPI WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress,
1064 LPVOID lpBuffer, DWORD nSize,
1065 LPDWORD lpNumberOfBytesWritten )
1067 memcpy(lpBaseAddress,lpBuffer,nSize);
1068 if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = nSize;
1069 return TRUE;
1072 /***********************************************************************
1073 * RegisterServiceProcess (KERNEL, KERNEL32)
1075 * A service process calls this function to ensure that it continues to run
1076 * even after a user logged off.
1078 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1080 /* I don't think that Wine needs to do anything in that function */
1081 return 1; /* success */
1084 /***********************************************************************
1085 * GetExitCodeProcess [KERNEL32.325]
1087 * Gets termination status of specified process
1089 * RETURNS
1090 * Success: TRUE
1091 * Failure: FALSE
1093 BOOL WINAPI GetExitCodeProcess(
1094 HANDLE hProcess, /* [I] handle to the process */
1095 LPDWORD lpExitCode) /* [O] address to receive termination status */
1097 struct get_process_info_reply reply;
1098 if (!PROCESS_QueryInfo( hProcess, &reply )) return FALSE;
1099 if (lpExitCode) *lpExitCode = reply.exit_code;
1100 return TRUE;
1104 /***********************************************************************
1105 * GetProcessHeaps [KERNEL32.376]
1107 DWORD WINAPI GetProcessHeaps(DWORD nrofheaps,HANDLE *heaps) {
1108 FIXME(win32,"(%ld,%p), incomplete implementation.\n",nrofheaps,heaps);
1110 if (nrofheaps) {
1111 heaps[0] = GetProcessHeap();
1112 /* ... probably SystemHeap too ? */
1113 return 1;
1115 /* number of available heaps */
1116 return 1;
1119 /***********************************************************************
1120 * PROCESS_SuspendOtherThreads
1123 void PROCESS_SuspendOtherThreads(void)
1125 #if 0
1126 PDB *pdb;
1127 THREAD_ENTRY *entry;
1129 SYSTEM_LOCK();
1131 pdb = PROCESS_Current();
1132 entry = pdb->thread_list->next;
1133 for (;;)
1135 if (entry->thread != THREAD_Current() && !THREAD_IsWin16(entry->thread))
1137 HANDLE handle = HANDLE_Alloc( PROCESS_Current(),
1138 &entry->thread->header,
1139 THREAD_ALL_ACCESS, FALSE, -1 );
1140 SuspendThread(handle);
1141 CloseHandle(handle);
1143 if (entry == pdb->thread_list) break;
1144 entry = entry->next;
1147 SYSTEM_UNLOCK();
1148 #endif
1151 /***********************************************************************
1152 * PROCESS_ResumeOtherThreads
1155 void PROCESS_ResumeOtherThreads(void)
1157 #if 0
1158 PDB *pdb;
1159 THREAD_ENTRY *entry;
1161 SYSTEM_LOCK();
1163 pdb = PROCESS_Current();
1164 entry = pdb->thread_list->next;
1165 for (;;)
1167 if (entry->thread != THREAD_Current() && !THREAD_IsWin16(entry->thread))
1169 HANDLE handle = HANDLE_Alloc( PROCESS_Current(),
1170 &entry->thread->header,
1171 THREAD_ALL_ACCESS, FALSE, -1 );
1172 ResumeThread(handle);
1173 CloseHandle(handle);
1175 if (entry == pdb->thread_list) break;
1176 entry = entry->next;
1179 SYSTEM_UNLOCK();
1180 #endif