Hook into the (stubbed) resize code in msdos int10.
[wine/multimedia.git] / scheduler / process.c
blob1432ea74d232776d2f7c22bee502a15d286096b5
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 "file.h"
14 #include "global.h"
15 #include "heap.h"
16 #include "task.h"
17 #include "ldt.h"
18 #include "syslevel.h"
19 #include "thread.h"
20 #include "winerror.h"
21 #include "pe_image.h"
22 #include "task.h"
23 #include "server.h"
24 #include "debug.h"
26 static BOOL32 PROCESS_Signaled( K32OBJ *obj, DWORD thread_id );
27 static BOOL32 PROCESS_Satisfied( K32OBJ *obj, DWORD thread_id );
28 static void PROCESS_AddWait( K32OBJ *obj, DWORD thread_id );
29 static void PROCESS_RemoveWait( K32OBJ *obj, DWORD thread_id );
30 static void PROCESS_Destroy( K32OBJ *obj );
32 const K32OBJ_OPS PROCESS_Ops =
34 PROCESS_Signaled, /* signaled */
35 PROCESS_Satisfied, /* satisfied */
36 PROCESS_AddWait, /* add_wait */
37 PROCESS_RemoveWait, /* remove_wait */
38 NULL, /* read */
39 NULL, /* write */
40 PROCESS_Destroy /* destroy */
43 static DWORD PROCESS_InitialProcessID = 0;
44 static PDB32 *PROCESS_PDBList = NULL;
45 static DWORD PROCESS_PDBList_Size = 0;
47 /***********************************************************************
48 * PROCESS_Current
50 PDB32 *PROCESS_Current(void)
52 return THREAD_Current()->process;
55 /***********************************************************************
56 * PROCESS_Initial
58 * FIXME: This works only while running all processes in the same
59 * address space (or, at least, the initial process is mapped
60 * into all address spaces as is KERNEL32 in Windows 95)
63 PDB32 *PROCESS_Initial(void)
65 return PROCESS_IdToPDB( PROCESS_InitialProcessID );
68 /***********************************************************************
69 * PROCESS_GetPtr
71 * Get a process from a handle, incrementing the PDB refcount.
73 PDB32 *PROCESS_GetPtr( HANDLE32 handle, DWORD access, int *server_handle )
75 return (PDB32 *)HANDLE_GetObjPtr( PROCESS_Current(), handle,
76 K32OBJ_PROCESS, access, server_handle );
80 /***********************************************************************
81 * PROCESS_IdToPDB
83 * Convert a process id to a PDB, making sure it is valid.
85 PDB32 *PROCESS_IdToPDB( DWORD id )
87 PDB32 *pdb;
89 if (!id) return PROCESS_Current();
90 pdb = PROCESS_ID_TO_PDB( id );
91 if (!K32OBJ_IsValid( &pdb->header, K32OBJ_PROCESS ))
93 SetLastError( ERROR_INVALID_PARAMETER );
94 return NULL;
96 return pdb;
101 /***********************************************************************
102 * PROCESS_BuildEnvDB
104 * Build the env DB for the initial process
106 static BOOL32 PROCESS_BuildEnvDB( PDB32 *pdb )
108 /* Allocate the env DB (FIXME: should not be on the system heap) */
110 if (!(pdb->env_db = HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(ENVDB))))
111 return FALSE;
112 InitializeCriticalSection( &pdb->env_db->section );
114 /* Allocate startup info */
115 if (!(pdb->env_db->startup_info =
116 HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFO32A) )))
117 return FALSE;
119 /* Allocate the standard handles */
121 pdb->env_db->hStdin = FILE_DupUnixHandle( 0 );
122 pdb->env_db->hStdout = FILE_DupUnixHandle( 1 );
123 pdb->env_db->hStderr = FILE_DupUnixHandle( 2 );
124 FILE_SetFileType( pdb->env_db->hStdin, FILE_TYPE_CHAR );
125 FILE_SetFileType( pdb->env_db->hStdout, FILE_TYPE_CHAR );
126 FILE_SetFileType( pdb->env_db->hStderr, FILE_TYPE_CHAR );
128 /* Build the command-line */
130 pdb->env_db->cmd_line = HEAP_strdupA( SystemHeap, 0, "kernel32" );
132 /* Build the environment strings */
134 return ENV_BuildEnvironment( pdb );
138 /***********************************************************************
139 * PROCESS_InheritEnvDB
141 static BOOL32 PROCESS_InheritEnvDB( PDB32 *pdb, LPCSTR cmd_line, LPCSTR env,
142 STARTUPINFO32A *startup )
144 if (!(pdb->env_db = HeapAlloc(pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB))))
145 return FALSE;
146 InitializeCriticalSection( &pdb->env_db->section );
148 /* Copy the parent environment */
150 if (!ENV_InheritEnvironment( pdb, env )) return FALSE;
152 /* Copy the command line */
154 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
155 return FALSE;
157 /* Remember startup info */
158 if (!(pdb->env_db->startup_info =
159 HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFO32A) )))
160 return FALSE;
161 *pdb->env_db->startup_info = *startup;
163 /* Inherit the standard handles */
164 if (pdb->env_db->startup_info->dwFlags & STARTF_USESTDHANDLES)
166 pdb->env_db->hStdin = pdb->env_db->startup_info->hStdInput;
167 pdb->env_db->hStdout = pdb->env_db->startup_info->hStdOutput;
168 pdb->env_db->hStderr = pdb->env_db->startup_info->hStdError;
170 else
172 pdb->env_db->hStdin = pdb->parent->env_db->hStdin;
173 pdb->env_db->hStdout = pdb->parent->env_db->hStdout;
174 pdb->env_db->hStderr = pdb->parent->env_db->hStderr;
177 return TRUE;
180 /***********************************************************************
181 * PROCESS_PDBList_Insert
182 * Insert this PDB into the global PDB list
185 static void PROCESS_PDBList_Insert (PDB32 *pdb)
187 TRACE (process, "Inserting PDB 0x%0lx, #%ld current\n",
188 PDB_TO_PROCESS_ID (pdb), PROCESS_PDBList_Size);
190 SYSTEM_LOCK (); /* FIXME: Do I need to worry about this ?
191 * I.e., could more than one process be
192 * created at once ?
194 if (PROCESS_PDBList == NULL)
196 PROCESS_PDBList = pdb;
197 pdb->list_next = NULL;
198 pdb->list_prev = NULL;
200 else
202 PDB32 *first = PROCESS_PDBList, *last = PROCESS_PDBList;
203 if (first->list_prev) last = first->list_prev;
205 PROCESS_PDBList = pdb;
206 pdb->list_next = first;
207 pdb->list_prev = last;
208 last->list_next = pdb;
209 first->list_prev = pdb;
211 PROCESS_PDBList_Size ++;
212 SYSTEM_UNLOCK ();
215 /***********************************************************************
216 * PROCESS_PDBList_Remove
217 * Remove this PDB from the global PDB list
220 static void PROCESS_PDBList_Remove (PDB32 *pdb)
222 PDB32 *next = pdb->list_next, *prev = pdb->list_prev;
224 TRACE (process, "Removing PDB 0x%0lx, #%ld current\n",
225 PDB_TO_PROCESS_ID (pdb), PROCESS_PDBList_Size);
227 SYSTEM_LOCK ();
229 if (prev == next)
231 next->list_prev = NULL;
232 next->list_next = NULL;
234 else
236 if (next) next->list_prev = prev;
237 if (prev) prev->list_next = next;
240 if (pdb == PROCESS_PDBList)
242 PROCESS_PDBList = next ? next : prev;
244 PROCESS_PDBList_Size --;
246 SYSTEM_UNLOCK ();
249 /***********************************************************************
250 * PROCESS_PDBList_Getsize
251 * Return the number of items in the global PDB list
254 int PROCESS_PDBList_Getsize ()
256 return PROCESS_PDBList_Size;
259 /***********************************************************************
260 * PROCESS_PDBList_Getfirst
261 * Return the head of the PDB list
264 PDB32* PROCESS_PDBList_Getfirst ()
266 return PROCESS_PDBList;
269 /***********************************************************************
270 * PROCESS_PDBList_Getnext
271 * Return the "next" pdb as referenced from the argument.
272 * If at the end of the list, return NULL.
275 PDB32* PROCESS_PDBList_Getnext (PDB32 *pdb)
277 return (pdb->list_next != PROCESS_PDBList) ? pdb->list_next : NULL;
280 /***********************************************************************
281 * PROCESS_FreePDB
283 * Free a PDB and all associated storage.
285 static void PROCESS_FreePDB( PDB32 *pdb )
288 * FIXME:
289 * If this routine is called because PROCESS_CreatePDB fails, the
290 * following call to PROCESS_PDBList_Remove will probably screw
291 * up.
293 PROCESS_PDBList_Remove (pdb);
294 pdb->header.type = K32OBJ_UNKNOWN;
295 if (pdb->handle_table) HANDLE_CloseAll( pdb, NULL );
296 ENV_FreeEnvironment( pdb );
297 if (pdb->heap && (pdb->heap != pdb->system_heap)) HeapDestroy( pdb->heap );
298 if (pdb->load_done_evt) K32OBJ_DecCount( pdb->load_done_evt );
299 if (pdb->event) K32OBJ_DecCount( pdb->event );
300 DeleteCriticalSection( &pdb->crit_section );
301 HeapFree( SystemHeap, 0, pdb );
305 /***********************************************************************
306 * PROCESS_CreatePDB
308 * Allocate and fill a PDB structure.
309 * Runs in the context of the parent process.
311 static PDB32 *PROCESS_CreatePDB( PDB32 *parent )
313 PDB32 *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB32) );
315 if (!pdb) return NULL;
316 pdb->header.type = K32OBJ_PROCESS;
317 pdb->header.refcount = 1;
318 pdb->exit_code = 0x103; /* STILL_ACTIVE */
319 pdb->threads = 1;
320 pdb->running_threads = 1;
321 pdb->ring0_threads = 1;
322 pdb->system_heap = SystemHeap;
323 pdb->parent = parent;
324 pdb->group = pdb;
325 pdb->priority = 8; /* Normal */
326 pdb->heap = pdb->system_heap; /* will be changed later on */
328 InitializeCriticalSection( &pdb->crit_section );
330 /* Allocate the events */
332 if (!(pdb->event = EVENT_Create( TRUE, FALSE ))) goto error;
333 if (!(pdb->load_done_evt = EVENT_Create( TRUE, FALSE ))) goto error;
335 /* Create the handle table */
337 if (!HANDLE_CreateTable( pdb, TRUE )) goto error;
339 PROCESS_PDBList_Insert (pdb);
341 return pdb;
343 error:
344 PROCESS_FreePDB( pdb );
345 return NULL;
349 /***********************************************************************
350 * PROCESS_Init
352 BOOL32 PROCESS_Init(void)
354 PDB32 *pdb;
355 THDB *thdb;
357 /* Initialize virtual memory management */
358 if (!VIRTUAL_Init()) return FALSE;
360 /* Create the system and SEGPTR heaps */
361 if (!(SystemHeap = HeapCreate( HEAP_GROWABLE, 0x10000, 0 ))) return FALSE;
362 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
364 /* Create the initial process and thread structures */
365 if (!(pdb = PROCESS_CreatePDB( NULL ))) return FALSE;
366 if (!(thdb = THREAD_Create( pdb, 0, FALSE, NULL, NULL, NULL, NULL ))) return FALSE;
367 thdb->unix_pid = getpid();
369 PROCESS_InitialProcessID = PDB_TO_PROCESS_ID(pdb);
371 /* Remember TEB selector of initial process for emergency use */
372 SYSLEVEL_EmergencyTeb = thdb->teb_sel;
374 /* Create the environment DB of the first process */
375 if (!PROCESS_BuildEnvDB( pdb )) return FALSE;
377 /* Initialize the first thread */
378 if (CLIENT_InitThread()) return FALSE;
380 return TRUE;
384 /***********************************************************************
385 * PROCESS_Create
387 * Create a new process database and associated info.
389 PDB32 *PROCESS_Create( NE_MODULE *pModule, LPCSTR cmd_line, LPCSTR env,
390 HINSTANCE16 hInstance, HINSTANCE16 hPrevInstance,
391 STARTUPINFO32A *startup, PROCESS_INFORMATION *info )
393 DWORD size, commit;
394 int server_thandle, server_phandle;
395 UINT32 cmdShow = 0;
396 THDB *thdb = NULL;
397 PDB32 *parent = PROCESS_Current();
398 PDB32 *pdb = PROCESS_CreatePDB( parent );
399 TDB *pTask;
401 if (!pdb) return NULL;
402 info->hThread = info->hProcess = INVALID_HANDLE_VALUE32;
404 /* Create the heap */
406 if (pModule->module32)
408 size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapReserve;
409 commit = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfHeapCommit;
411 else
413 size = 0x10000;
414 commit = 0;
415 pdb->flags |= PDB32_WIN16_PROC; /* This is a Win16 process */
417 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE, size, commit ))) goto error;
418 pdb->heap_list = pdb->heap;
420 /* Inherit the env DB from the parent */
422 if (!PROCESS_InheritEnvDB( pdb, cmd_line, env, startup )) goto error;
424 /* Create the main thread */
426 if (pModule->module32)
427 size = PE_HEADER(pModule->module32)->OptionalHeader.SizeOfStackReserve;
428 else
429 size = 0;
430 if (!(thdb = THREAD_Create( pdb, size, FALSE, &server_thandle, &server_phandle,
431 NULL, NULL ))) goto error;
432 if ((info->hThread = HANDLE_Alloc( parent, &thdb->header, THREAD_ALL_ACCESS,
433 FALSE, server_thandle )) == INVALID_HANDLE_VALUE32)
434 goto error;
435 if ((info->hProcess = HANDLE_Alloc( parent, &pdb->header, PROCESS_ALL_ACCESS,
436 FALSE, server_phandle )) == INVALID_HANDLE_VALUE32)
437 goto error;
438 info->dwProcessId = PDB_TO_PROCESS_ID(pdb);
439 info->dwThreadId = THDB_TO_THREAD_ID(thdb);
441 #if 0
442 thdb->unix_pid = getpid(); /* FIXME: wrong here ... */
443 #else
444 /* All Win16 'threads' have the same unix_pid, no matter by which thread
445 they were created ! */
446 pTask = (TDB *)GlobalLock16( parent->task );
447 thdb->unix_pid = pTask? pTask->thdb->unix_pid : THREAD_Current()->unix_pid;
448 #endif
450 /* Create a Win16 task for this process */
452 if (startup->dwFlags & STARTF_USESHOWWINDOW)
453 cmdShow = startup->wShowWindow;
455 pdb->task = TASK_Create( thdb, pModule, hInstance, hPrevInstance, cmdShow);
456 if (!pdb->task) goto error;
459 /* Map system DLLs into this process (from initial process) */
460 /* FIXME: this is a hack */
461 pdb->modref_list = PROCESS_Initial()->modref_list;
464 return pdb;
466 error:
467 if (info->hThread != INVALID_HANDLE_VALUE32) CloseHandle( info->hThread );
468 if (info->hProcess != INVALID_HANDLE_VALUE32) CloseHandle( info->hProcess );
469 if (thdb) K32OBJ_DecCount( &thdb->header );
470 PROCESS_FreePDB( pdb );
471 return NULL;
475 /***********************************************************************
476 * PROCESS_Signaled
478 static BOOL32 PROCESS_Signaled( K32OBJ *obj, DWORD thread_id )
480 PDB32 *pdb = (PDB32 *)obj;
481 assert( obj->type == K32OBJ_PROCESS );
482 return K32OBJ_OPS( pdb->event )->signaled( pdb->event, thread_id );
486 /***********************************************************************
487 * PROCESS_Satisfied
489 * Wait on this object has been satisfied.
491 static BOOL32 PROCESS_Satisfied( K32OBJ *obj, DWORD thread_id )
493 PDB32 *pdb = (PDB32 *)obj;
494 assert( obj->type == K32OBJ_PROCESS );
495 return K32OBJ_OPS( pdb->event )->satisfied( pdb->event, thread_id );
499 /***********************************************************************
500 * PROCESS_AddWait
502 * Add thread to object wait queue.
504 static void PROCESS_AddWait( K32OBJ *obj, DWORD thread_id )
506 PDB32 *pdb = (PDB32 *)obj;
507 assert( obj->type == K32OBJ_PROCESS );
508 return K32OBJ_OPS( pdb->event )->add_wait( pdb->event, thread_id );
512 /***********************************************************************
513 * PROCESS_RemoveWait
515 * Remove thread from object wait queue.
517 static void PROCESS_RemoveWait( K32OBJ *obj, DWORD thread_id )
519 PDB32 *pdb = (PDB32 *)obj;
520 assert( obj->type == K32OBJ_PROCESS );
521 return K32OBJ_OPS( pdb->event )->remove_wait( pdb->event, thread_id );
525 /***********************************************************************
526 * PROCESS_Destroy
528 static void PROCESS_Destroy( K32OBJ *ptr )
530 PDB32 *pdb = (PDB32 *)ptr;
531 assert( ptr->type == K32OBJ_PROCESS );
533 /* Free everything */
535 ptr->type = K32OBJ_UNKNOWN;
536 PROCESS_FreePDB( pdb );
540 /***********************************************************************
541 * ExitProcess (KERNEL32.100)
543 void WINAPI ExitProcess( DWORD status )
545 PDB32 *pdb = PROCESS_Current();
546 TDB *pTask = (TDB *)GlobalLock16( pdb->task );
547 if ( pTask ) pTask->nEvents++;
549 if ( pTask && pTask->thdb != THREAD_Current() )
550 ExitThread( status );
552 SYSTEM_LOCK();
553 /* FIXME: should kill all running threads of this process */
554 pdb->exit_code = status;
555 EVENT_Set( pdb->event );
556 if (pdb->console) FreeConsole();
557 SYSTEM_UNLOCK();
559 __RESTORE_ES; /* Necessary for Pietrek's showseh example program */
560 TASK_KillCurrentTask( status );
564 /******************************************************************************
565 * TerminateProcess (KERNEL32.684)
567 BOOL32 WINAPI TerminateProcess( HANDLE32 handle, DWORD exit_code )
569 int server_handle;
570 BOOL32 ret;
571 PDB32 *pdb = PROCESS_GetPtr( handle, PROCESS_TERMINATE, &server_handle );
572 if (!pdb) return FALSE;
573 ret = !CLIENT_TerminateProcess( server_handle, exit_code );
574 K32OBJ_DecCount( &pdb->header );
575 return ret;
578 /***********************************************************************
579 * GetCurrentProcess (KERNEL32.198)
581 HANDLE32 WINAPI GetCurrentProcess(void)
583 return CURRENT_PROCESS_PSEUDOHANDLE;
587 /*********************************************************************
588 * OpenProcess (KERNEL32.543)
590 HANDLE32 WINAPI OpenProcess( DWORD access, BOOL32 inherit, DWORD id )
592 int server_handle;
593 PDB32 *pdb = PROCESS_ID_TO_PDB(id);
594 if (!K32OBJ_IsValid( &pdb->header, K32OBJ_PROCESS ))
596 SetLastError( ERROR_INVALID_HANDLE );
597 return 0;
599 if ((server_handle = CLIENT_OpenProcess( pdb->server_pid, access, inherit )) == -1)
601 SetLastError( ERROR_INVALID_HANDLE );
602 return 0;
604 return HANDLE_Alloc( PROCESS_Current(), &pdb->header, access,
605 inherit, server_handle );
609 /***********************************************************************
610 * GetCurrentProcessId (KERNEL32.199)
612 DWORD WINAPI GetCurrentProcessId(void)
614 PDB32 *pdb = PROCESS_Current();
615 return PDB_TO_PROCESS_ID( pdb );
619 /***********************************************************************
620 * GetProcessHeap (KERNEL32.259)
622 HANDLE32 WINAPI GetProcessHeap(void)
624 PDB32 *pdb = PROCESS_Current();
625 return pdb->heap ? pdb->heap : SystemHeap;
629 /***********************************************************************
630 * GetThreadLocale (KERNEL32.295)
632 LCID WINAPI GetThreadLocale(void)
634 return PROCESS_Current()->locale;
638 /***********************************************************************
639 * SetPriorityClass (KERNEL32.503)
641 BOOL32 WINAPI SetPriorityClass( HANDLE32 hprocess, DWORD priorityclass )
643 PDB32 *pdb = PROCESS_GetPtr( hprocess, PROCESS_SET_INFORMATION, NULL );
644 if (!pdb) return FALSE;
645 switch (priorityclass)
647 case NORMAL_PRIORITY_CLASS:
648 pdb->priority = 0x00000008;
649 break;
650 case IDLE_PRIORITY_CLASS:
651 pdb->priority = 0x00000004;
652 break;
653 case HIGH_PRIORITY_CLASS:
654 pdb->priority = 0x0000000d;
655 break;
656 case REALTIME_PRIORITY_CLASS:
657 pdb->priority = 0x00000018;
658 break;
659 default:
660 WARN(process,"Unknown priority class %ld\n",priorityclass);
661 break;
663 K32OBJ_DecCount( &pdb->header );
664 return TRUE;
668 /***********************************************************************
669 * GetPriorityClass (KERNEL32.250)
671 DWORD WINAPI GetPriorityClass(HANDLE32 hprocess)
673 PDB32 *pdb = PROCESS_GetPtr( hprocess, PROCESS_QUERY_INFORMATION, NULL );
674 DWORD ret = 0;
675 if (pdb)
677 switch (pdb->priority)
679 case 0x00000008:
680 ret = NORMAL_PRIORITY_CLASS;
681 break;
682 case 0x00000004:
683 ret = IDLE_PRIORITY_CLASS;
684 break;
685 case 0x0000000d:
686 ret = HIGH_PRIORITY_CLASS;
687 break;
688 case 0x00000018:
689 ret = REALTIME_PRIORITY_CLASS;
690 break;
691 default:
692 WARN(process,"Unknown priority %ld\n",pdb->priority);
694 K32OBJ_DecCount( &pdb->header );
696 return ret;
700 /***********************************************************************
701 * GetStdHandle (KERNEL32.276)
703 * FIXME: These should be allocated when a console is created, or inherited
704 * from the parent.
706 HANDLE32 WINAPI GetStdHandle( DWORD std_handle )
708 HFILE32 hFile;
709 int fd;
710 PDB32 *pdb = PROCESS_Current();
712 switch(std_handle)
714 case STD_INPUT_HANDLE:
715 if (pdb->env_db->hStdin) return pdb->env_db->hStdin;
716 fd = 0;
717 break;
718 case STD_OUTPUT_HANDLE:
719 if (pdb->env_db->hStdout) return pdb->env_db->hStdout;
720 fd = 1;
721 break;
722 case STD_ERROR_HANDLE:
723 if (pdb->env_db->hStderr) return pdb->env_db->hStderr;
724 fd = 2;
725 break;
726 default:
727 SetLastError( ERROR_INVALID_PARAMETER );
728 return INVALID_HANDLE_VALUE32;
730 hFile = FILE_DupUnixHandle( fd );
731 if (hFile != HFILE_ERROR32)
733 FILE_SetFileType( hFile, FILE_TYPE_CHAR );
734 switch(std_handle)
736 case STD_INPUT_HANDLE: pdb->env_db->hStdin = hFile; break;
737 case STD_OUTPUT_HANDLE: pdb->env_db->hStdout = hFile; break;
738 case STD_ERROR_HANDLE: pdb->env_db->hStderr = hFile; break;
741 return hFile;
745 /***********************************************************************
746 * SetStdHandle (KERNEL32.506)
748 BOOL32 WINAPI SetStdHandle( DWORD std_handle, HANDLE32 handle )
750 PDB32 *pdb = PROCESS_Current();
751 /* FIXME: should we close the previous handle? */
752 switch(std_handle)
754 case STD_INPUT_HANDLE:
755 pdb->env_db->hStdin = handle;
756 return TRUE;
757 case STD_OUTPUT_HANDLE:
758 pdb->env_db->hStdout = handle;
759 return TRUE;
760 case STD_ERROR_HANDLE:
761 pdb->env_db->hStderr = handle;
762 return TRUE;
764 SetLastError( ERROR_INVALID_PARAMETER );
765 return FALSE;
768 /***********************************************************************
769 * GetProcessVersion (KERNEL32)
771 DWORD WINAPI GetProcessVersion( DWORD processid )
773 TDB *pTask;
774 PDB32 *pdb = PROCESS_IdToPDB( processid );
776 if (!pdb) return 0;
777 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
778 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
781 /***********************************************************************
782 * GetProcessFlags (KERNEL32)
784 DWORD WINAPI GetProcessFlags( DWORD processid )
786 PDB32 *pdb = PROCESS_IdToPDB( processid );
787 if (!pdb) return 0;
788 return pdb->flags;
791 /***********************************************************************
792 * SetProcessWorkingSetSize [KERNEL32.662]
793 * Sets the min/max working set sizes for a specified process.
795 * PARAMS
796 * hProcess [I] Handle to the process of interest
797 * minset [I] Specifies minimum working set size
798 * maxset [I] Specifies maximum working set size
800 * RETURNS STD
802 BOOL32 WINAPI SetProcessWorkingSetSize(HANDLE32 hProcess,DWORD minset,
803 DWORD maxset)
805 FIXME(process,"(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
806 if(( minset == -1) && (maxset == -1)) {
807 /* Trim the working set to zero */
808 /* Swap the process out of physical RAM */
810 return TRUE;
813 /***********************************************************************
814 * GetProcessWorkingSetSize (KERNEL32)
816 BOOL32 WINAPI GetProcessWorkingSetSize(HANDLE32 hProcess,LPDWORD minset,
817 LPDWORD maxset)
819 FIXME(process,"(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
820 /* 32 MB working set size */
821 if (minset) *minset = 32*1024*1024;
822 if (maxset) *maxset = 32*1024*1024;
823 return TRUE;
826 /***********************************************************************
827 * SetProcessShutdownParameters (KERNEL32)
829 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
830 * Now tracks changes made (but does not act on these changes)
831 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
832 * It really shouldn't be here, but I'll move it when it's been checked!
834 #define SHUTDOWN_NORETRY 1
835 extern unsigned int shutdown_noretry = 0;
836 extern unsigned int shutdown_priority = 0x280L;
837 BOOL32 WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
839 if (flags & SHUTDOWN_NORETRY)
840 shutdown_noretry = 1;
841 else
842 shutdown_noretry = 0;
843 if (level > 0x100L && level < 0x3FFL)
844 shutdown_priority = level;
845 else
847 ERR(process,"invalid priority level 0x%08lx\n", level);
848 return FALSE;
850 return TRUE;
854 /***********************************************************************
855 * GetProcessShutdownParameters (KERNEL32)
858 BOOL32 WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
859 LPDWORD lpdwFlags )
861 (*lpdwLevel) = shutdown_priority;
862 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
863 return TRUE;
865 /***********************************************************************
866 * SetProcessPriorityBoost (KERNEL32)
868 BOOL32 WINAPI SetProcessPriorityBoost(HANDLE32 hprocess,BOOL32 disableboost)
870 FIXME(process,"(%d,%d): stub\n",hprocess,disableboost);
871 /* Say we can do it. I doubt the program will notice that we don't. */
872 return TRUE;
875 /***********************************************************************
876 * ReadProcessMemory (KERNEL32)
877 * FIXME: check this, if we ever run win32 binaries in different addressspaces
878 * ... and add a sizecheck
880 BOOL32 WINAPI ReadProcessMemory( HANDLE32 hProcess, LPCVOID lpBaseAddress,
881 LPVOID lpBuffer, DWORD nSize,
882 LPDWORD lpNumberOfBytesRead )
884 memcpy(lpBuffer,lpBaseAddress,nSize);
885 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = nSize;
886 return TRUE;
889 /***********************************************************************
890 * WriteProcessMemory (KERNEL32)
891 * FIXME: check this, if we ever run win32 binaries in different addressspaces
892 * ... and add a sizecheck
894 BOOL32 WINAPI WriteProcessMemory(HANDLE32 hProcess, LPVOID lpBaseAddress,
895 LPVOID lpBuffer, DWORD nSize,
896 LPDWORD lpNumberOfBytesWritten )
898 memcpy(lpBaseAddress,lpBuffer,nSize);
899 if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = nSize;
900 return TRUE;
903 /***********************************************************************
904 * ConvertToGlobalHandle (KERNEL32)
906 HANDLE32 WINAPI ConvertToGlobalHandle(HANDLE32 hSrc)
908 HANDLE32 hProcessInit, hDest;
910 /* Get a handle to the initial process */
911 hProcessInit = OpenProcess( PROCESS_ALL_ACCESS, FALSE, PROCESS_InitialProcessID );
913 /* Duplicate the handle into the initial process */
914 if ( !DuplicateHandle( GetCurrentProcess(), hSrc, hProcessInit, &hDest,
915 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE ) )
916 hDest = 0;
918 /* Close initial process handle */
919 CloseHandle( hProcessInit );
921 /* Return obfuscated global handle */
922 return hDest? HANDLE_LOCAL_TO_GLOBAL( hDest ) : 0;
925 /***********************************************************************
926 * RegisterServiceProcess (KERNEL, KERNEL32)
928 * A service process calls this function to ensure that it continues to run
929 * even after a user logged off.
931 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
933 /* I don't think that Wine needs to do anything in that function */
934 return 1; /* success */
937 /***********************************************************************
938 * GetExitCodeProcess [KERNEL32.325]
940 * Gets termination status of specified process
942 * RETURNS
943 * Success: TRUE
944 * Failure: FALSE
946 * FIXME
947 * Should call SetLastError (but doesn't).
949 BOOL32 WINAPI GetExitCodeProcess(
950 HANDLE32 hProcess, /* [I] handle to the process */
951 LPDWORD lpExitCode) /* [O] address to receive termination status */
953 PDB32 *process;
954 int server_handle;
955 struct get_process_info_reply info;
957 if (!(process = PROCESS_GetPtr( hProcess, PROCESS_QUERY_INFORMATION,
958 &server_handle )))
959 return FALSE;
960 if (server_handle != -1)
962 CLIENT_GetProcessInfo( server_handle, &info );
963 if (lpExitCode) *lpExitCode = info.exit_code;
965 else if (lpExitCode) *lpExitCode = process->exit_code;
966 K32OBJ_DecCount( &process->header );
967 return TRUE;
970 /***********************************************************************
971 * GetProcessHeaps [KERNEL32.376]
973 DWORD WINAPI GetProcessHeaps(DWORD nrofheaps,HANDLE32 *heaps) {
974 FIXME(win32,"(%ld,%p), incomplete implementation.\n",nrofheaps,heaps);
976 if (nrofheaps) {
977 heaps[0] = GetProcessHeap();
978 /* ... probably SystemHeap too ? */
979 return 1;
981 /* number of available heaps */
982 return 1;
985 /***********************************************************************
986 * PROCESS_SuspendOtherThreads
989 void PROCESS_SuspendOtherThreads(void)
991 PDB32 *pdb;
992 THREAD_ENTRY *entry;
994 SYSTEM_LOCK();
996 pdb = PROCESS_Current();
997 entry = pdb->thread_list->next;
998 for (;;)
1000 if (entry->thread != THREAD_Current() && !THREAD_IsWin16(entry->thread))
1002 HANDLE32 handle = HANDLE_Alloc( PROCESS_Current(),
1003 &entry->thread->header,
1004 THREAD_ALL_ACCESS, FALSE, -1 );
1005 SuspendThread(handle);
1006 CloseHandle(handle);
1008 if (entry == pdb->thread_list) break;
1009 entry = entry->next;
1012 SYSTEM_UNLOCK();
1015 /***********************************************************************
1016 * PROCESS_ResumeOtherThreads
1019 void PROCESS_ResumeOtherThreads(void)
1021 PDB32 *pdb;
1022 THREAD_ENTRY *entry;
1024 SYSTEM_LOCK();
1026 pdb = PROCESS_Current();
1027 entry = pdb->thread_list->next;
1028 for (;;)
1030 if (entry->thread != THREAD_Current() && !THREAD_IsWin16(entry->thread))
1032 HANDLE32 handle = HANDLE_Alloc( PROCESS_Current(),
1033 &entry->thread->header,
1034 THREAD_ALL_ACCESS, FALSE, -1 );
1035 ResumeThread(handle);
1036 CloseHandle(handle);
1038 if (entry == pdb->thread_list) break;
1039 entry = entry->next;
1042 SYSTEM_UNLOCK();