4 * Copyright 1996, 1998 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
17 #include "wine/library.h"
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(process
);
33 DECLARE_DEBUG_CHANNEL(relay
);
34 DECLARE_DEBUG_CHANNEL(win32
);
38 /* Win32 process database */
41 LONG header
[2]; /* 00 Kernel object header */
42 HMODULE module
; /* 08 Main exe module (NT) */
43 void *event
; /* 0c Pointer to an event object (unused) */
44 DWORD exit_code
; /* 10 Process exit code */
45 DWORD unknown2
; /* 14 Unknown */
46 HANDLE heap
; /* 18 Default process heap */
47 HANDLE mem_context
; /* 1c Process memory context */
48 DWORD flags
; /* 20 Flags */
49 void *pdb16
; /* 24 DOS PSP */
50 WORD PSP_sel
; /* 28 Selector to DOS PSP */
51 WORD imte
; /* 2a IMTE for the process module */
52 WORD threads
; /* 2c Number of threads */
53 WORD running_threads
; /* 2e Number of running threads */
54 WORD free_lib_count
; /* 30 Recursion depth of FreeLibrary calls */
55 WORD ring0_threads
; /* 32 Number of ring 0 threads */
56 HANDLE system_heap
; /* 34 System heap to allocate handles */
57 HTASK task
; /* 38 Win16 task */
58 void *mem_map_files
; /* 3c Pointer to mem-mapped files */
59 struct _ENVDB
*env_db
; /* 40 Environment database */
60 void *handle_table
; /* 44 Handle table */
61 struct _PDB
*parent
; /* 48 Parent process */
62 void *modref_list
; /* 4c MODREF list */
63 void *thread_list
; /* 50 List of threads */
64 void *debuggee_CB
; /* 54 Debuggee context block */
65 void *local_heap_free
; /* 58 Head of local heap free list */
66 DWORD unknown4
; /* 5c Unknown */
67 CRITICAL_SECTION crit_section
; /* 60 Critical section */
68 DWORD unknown5
[3]; /* 78 Unknown */
69 void *console
; /* 84 Console */
70 DWORD tls_bits
[2]; /* 88 TLS in-use bits */
71 DWORD process_dword
; /* 90 Unknown */
72 struct _PDB
*group
; /* 94 Process group */
73 void *exe_modref
; /* 98 MODREF for the process EXE */
74 void *top_filter
; /* 9c Top exception filter */
75 DWORD priority
; /* a0 Priority level */
76 HANDLE heap_list
; /* a4 Head of process heap list */
77 void *heap_handles
; /* a8 Head of heap handles list */
78 DWORD unknown6
; /* ac Unknown */
79 void *console_provider
; /* b0 Console provider (??) */
80 WORD env_selector
; /* b4 Selector to process environment */
81 WORD error_mode
; /* b6 Error mode */
82 HANDLE load_done_evt
; /* b8 Event for process loading done */
83 void *UTState
; /* bc Head of Univeral Thunk list */
84 DWORD unknown8
; /* c0 Unknown (NT) */
85 LCID locale
; /* c4 Locale to be queried by GetThreadLocale (NT) */
91 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
92 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
93 #define PDB32_DOS_PROC 0x0010 /* Dos process */
94 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
95 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
96 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
98 static char **main_exe_argv
;
99 static char main_exe_name
[MAX_PATH
];
100 static HANDLE main_exe_file
= INVALID_HANDLE_VALUE
;
102 unsigned int server_startticks
;
104 /* memory/environ.c */
105 extern struct _ENVDB
*ENV_BuildEnvironment(void);
106 extern BOOL
ENV_BuildCommandLine( char **argv
);
107 extern STARTUPINFOA current_startupinfo
;
109 extern BOOL
MAIN_MainInit(void);
112 /***********************************************************************
113 * PROCESS_CallUserSignalProc
115 * FIXME: Some of the signals aren't sent correctly!
117 * The exact meaning of the USER signals is undocumented, but this
118 * should cover the basic idea:
120 * USIG_DLL_UNLOAD_WIN16
121 * This is sent when a 16-bit module is unloaded.
123 * USIG_DLL_UNLOAD_WIN32
124 * This is sent when a 32-bit module is unloaded.
126 * USIG_DLL_UNLOAD_ORPHANS
127 * This is sent after the last Win3.1 module is unloaded,
128 * to allow removal of orphaned menus.
130 * USIG_FAULT_DIALOG_PUSH
131 * USIG_FAULT_DIALOG_POP
132 * These are called to allow USER to prepare for displaying a
133 * fault dialog, even though the fault might have happened while
134 * inside a USER critical section.
137 * This is called from the context of a new thread, as soon as it
138 * has started to run.
141 * This is called, still in its context, just before a thread is
142 * about to terminate.
144 * USIG_PROCESS_CREATE
145 * This is called, in the parent process context, after a new process
149 * This is called in the new process context, just after the main thread
150 * has started execution (after the main thread's USIG_THREAD_INIT has
153 * USIG_PROCESS_LOADED
154 * This is called after the executable file has been loaded into the
155 * new process context.
157 * USIG_PROCESS_RUNNING
158 * This is called immediately before the main entry point is called.
161 * This is called in the context of a process that is about to
162 * terminate (but before the last thread's USIG_THREAD_EXIT has
165 * USIG_PROCESS_DESTROY
166 * This is called after a process has terminated.
169 * The meaning of the dwFlags bits is as follows:
172 * Current process is 32-bit.
175 * Current process is a (Win32) GUI process.
177 * USIG_FLAGS_FEEDBACK
178 * Current process needs 'feedback' (determined from the STARTUPINFO
179 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
182 * The signal is being sent due to a fault.
184 void PROCESS_CallUserSignalProc( UINT uCode
, HMODULE hModule
)
186 DWORD flags
= current_process
.flags
;
187 DWORD startup_flags
= current_startupinfo
.dwFlags
;
190 /* Determine dwFlags */
192 if ( !(flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
194 if ( !(flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
196 if ( dwFlags
& USIG_FLAGS_GUI
)
198 /* Feedback defaults to ON */
199 if ( !(startup_flags
& STARTF_FORCEOFFFEEDBACK
) )
200 dwFlags
|= USIG_FLAGS_FEEDBACK
;
204 /* Feedback defaults to OFF */
205 if (startup_flags
& STARTF_FORCEONFEEDBACK
)
206 dwFlags
|= USIG_FLAGS_FEEDBACK
;
209 /* Convert module handle to 16-bit */
211 if ( HIWORD( hModule
) )
212 hModule
= MapHModuleLS( hModule
);
214 /* Call USER signal proc */
216 if ( Callout
.UserSignalProc
)
218 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
219 Callout
.UserSignalProc( uCode
, GetCurrentThreadId(), dwFlags
, hModule
);
221 Callout
.UserSignalProc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
226 /***********************************************************************
229 * Main process initialisation code
231 static BOOL
process_init( char *argv
[] )
235 /* store the program name */
237 main_exe_argv
= argv
;
239 /* Fill the initial process structure */
240 current_process
.exit_code
= STILL_ACTIVE
;
241 current_process
.threads
= 1;
242 current_process
.running_threads
= 1;
243 current_process
.ring0_threads
= 1;
244 current_process
.group
= ¤t_process
;
245 current_process
.priority
= 8; /* Normal */
247 /* Setup the server connection */
248 NtCurrentTeb()->socket
= CLIENT_InitServer();
249 if (CLIENT_InitThread()) return FALSE
;
251 /* Retrieve startup info from the server */
254 struct init_process_request
*req
= server_alloc_req( sizeof(*req
),
255 sizeof(main_exe_name
)-1 );
257 req
->ldt_copy
= &wine_ldt_copy
;
258 req
->ppid
= getppid();
259 if ((ret
= !server_call( REQ_INIT_PROCESS
)))
261 size_t len
= server_data_size( req
);
262 memcpy( main_exe_name
, server_data_ptr(req
), len
);
263 main_exe_name
[len
] = 0;
264 main_exe_file
= req
->exe_file
;
265 current_startupinfo
.dwFlags
= req
->start_flags
;
266 server_startticks
= req
->server_start
;
267 current_startupinfo
.wShowWindow
= req
->cmd_show
;
268 current_startupinfo
.hStdInput
= req
->hstdin
;
269 current_startupinfo
.hStdOutput
= req
->hstdout
;
270 current_startupinfo
.hStdError
= req
->hstderr
;
271 SetStdHandle( STD_INPUT_HANDLE
, current_startupinfo
.hStdInput
);
272 SetStdHandle( STD_OUTPUT_HANDLE
, current_startupinfo
.hStdOutput
);
273 SetStdHandle( STD_ERROR_HANDLE
, current_startupinfo
.hStdError
);
277 if (!ret
) return FALSE
;
279 /* Create the system and process heaps */
280 if (!HEAP_CreateSystemHeap()) return FALSE
;
281 current_process
.heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
283 /* Copy the parent environment */
284 if (!(current_process
.env_db
= ENV_BuildEnvironment())) return FALSE
;
286 /* Parse command line arguments */
287 OPTIONS_ParseOptions( argv
);
289 return MAIN_MainInit();
293 /***********************************************************************
296 * Startup routine of a new process. Runs on the new process stack.
298 static void start_process(void)
300 int debugged
, console_app
;
301 LPTHREAD_START_ROUTINE entry
;
304 /* build command line */
305 if (!ENV_BuildCommandLine( main_exe_argv
)) goto error
;
307 /* create 32-bit module for main exe */
308 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
310 /* use original argv[0] as name for the main module */
311 if (!main_exe_name
[0])
313 if (!GetLongPathNameA( full_argv0
, main_exe_name
, sizeof(main_exe_name
) ))
314 lstrcpynA( main_exe_name
, full_argv0
, sizeof(main_exe_name
) );
317 /* Retrieve entry point address */
318 entry
= (LPTHREAD_START_ROUTINE
)((char*)current_process
.module
+
319 PE_HEADER(current_process
.module
)->OptionalHeader
.AddressOfEntryPoint
);
320 console_app
= (PE_HEADER(current_process
.module
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
321 if (console_app
) current_process
.flags
|= PDB32_CONSOLE_PROC
;
323 /* Signal the parent process to continue */
326 struct init_process_done_request
*req
= server_alloc_req( sizeof(*req
), 0 );
327 req
->module
= (void *)current_process
.module
;
329 req
->name
= main_exe_name
;
330 req
->gui
= !console_app
;
331 server_call( REQ_INIT_PROCESS_DONE
);
332 debugged
= req
->debugged
;
336 /* Install signal handlers; this cannot be done before, since we cannot
337 * send exceptions to the debugger before the create process event that
338 * is sent by REQ_INIT_PROCESS_DONE */
339 if (!SIGNAL_Init()) goto error
;
341 /* create the main modref and load dependencies */
342 if (!(wm
= PE_CreateModule( current_process
.module
, main_exe_name
, 0, main_exe_file
, FALSE
)))
348 MODULE_DllProcessAttach( NULL
, (LPVOID
)1 );
351 /* Get pointers to USER routines called by KERNEL */
354 /* Call FinalUserInit routine */
355 if (Callout
.FinalUserInit16
) Callout
.FinalUserInit16();
357 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
358 * context of the parent process. Actually, the USER signal proc
359 * doesn't really care about that, but it *does* require that the
360 * startup parameters are correctly set up, so that GetProcessDword
361 * works. Furthermore, before calling the USER signal proc the
362 * 16-bit stack must be set up, which it is only after TASK_Create
363 * in the case of a 16-bit process. Thus, we send the signal here.
365 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
366 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
367 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
368 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
369 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
370 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
372 TRACE_(relay
)( "Starting Win32 process %s (entryproc=%p)\n", main_exe_name
, entry
);
373 if (debugged
) DbgBreakPoint();
374 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
375 * Dunno about other OSs */
376 SetLastError(0); /* clear error code */
377 ExitThread( entry(NULL
) );
380 ExitProcess( GetLastError() );
384 /***********************************************************************
387 * Try to open the Winelib app .so file based on argv[0] or WINEPRELOAD.
389 void *open_winelib_app( char *argv
[] )
395 if ((name
= getenv( "WINEPRELOAD" )))
397 if (!(ret
= wine_dll_load_main_exe( name
, 0 )))
399 MESSAGE( "%s: could not load '%s' as specified in the WINEPRELOAD environment variable\n",
406 /* if argv[0] is "wine", don't try to load anything */
407 if (!(name
= strrchr( argv
[0], '/' ))) name
= argv
[0];
409 if (!strcmp( name
, "wine" )) return NULL
;
411 /* now try argv[0] with ".so" appended */
412 if ((tmp
= HeapAlloc( GetProcessHeap(), 0, strlen(argv
[0]) + 4 )))
414 strcpy( tmp
, argv
[0] );
415 strcat( tmp
, ".so" );
416 /* search in PATH only if there was no '/' in argv[0] */
417 ret
= wine_dll_load_main_exe( tmp
, (name
== argv
[0]) );
418 if (!ret
&& !argv
[1])
420 /* if no argv[1], this will be better than displaying usage */
421 MESSAGE( "%s: could not load library '%s' as Winelib application\n",
425 HeapFree( GetProcessHeap(), 0, tmp
);
431 /***********************************************************************
434 * Wine initialisation: load and start the main exe file.
436 void PROCESS_InitWine( int argc
, char *argv
[] )
438 DWORD stack_size
= 0;
440 /* Initialize everything */
441 if (!process_init( argv
)) exit(1);
443 if (open_winelib_app( argv
)) goto found
; /* try to open argv[0] as a winelib app */
445 main_exe_argv
= ++argv
; /* remove argv[0] (wine itself) */
447 if (!main_exe_name
[0])
449 if (!argv
[0]) OPTIONS_Usage();
451 /* open the exe file */
452 if (!SearchPathA( NULL
, argv
[0], ".exe", sizeof(main_exe_name
), main_exe_name
, NULL
) &&
453 !SearchPathA( NULL
, argv
[0], NULL
, sizeof(main_exe_name
), main_exe_name
, NULL
))
455 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
460 if (main_exe_file
== INVALID_HANDLE_VALUE
)
462 if ((main_exe_file
= CreateFileA( main_exe_name
, GENERIC_READ
, FILE_SHARE_READ
,
463 NULL
, OPEN_EXISTING
, 0, -1 )) == INVALID_HANDLE_VALUE
)
465 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
470 /* first try Win32 format; this will fail if the file is not a PE binary */
471 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 )))
473 if (PE_HEADER(current_process
.module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
474 ExitProcess( ERROR_BAD_EXE_FORMAT
);
475 stack_size
= PE_HEADER(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
479 /* it must be 16-bit or DOS format */
480 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
481 current_process
.flags
|= PDB32_WIN16_PROC
;
482 main_exe_name
[0] = 0;
483 CloseHandle( main_exe_file
);
484 main_exe_file
= INVALID_HANDLE_VALUE
;
488 /* allocate main thread stack */
489 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
, TRUE
)) goto error
;
491 /* switch to the new stack */
492 SYSDEPS_SwitchToThreadStack( start_process
);
495 ExitProcess( GetLastError() );
499 /***********************************************************************
500 * PROCESS_InitWinelib
502 * Initialisation of a new Winelib process.
504 void PROCESS_InitWinelib( int argc
, char *argv
[] )
506 if (!process_init( argv
)) exit(1);
508 /* allocate main thread stack */
509 if (!THREAD_InitStack( NtCurrentTeb(), 0, TRUE
)) ExitProcess( GetLastError() );
511 /* switch to the new stack */
512 SYSDEPS_SwitchToThreadStack( start_process
);
516 /***********************************************************************
519 * Build an argv array from a command-line.
520 * The command-line is modified to insert nulls.
521 * 'reserved' is the number of args to reserve before the first one.
523 static char **build_argv( char *cmdline
, int reserved
)
526 int count
= reserved
+ 1;
529 /* if first word is quoted store it as a single arg */
530 if (*cmdline
== '\"')
532 if ((p
= strchr( cmdline
+ 1, '\"' )))
541 while (*p
&& isspace(*p
)) p
++;
544 while (*p
&& !isspace(*p
)) p
++;
547 if ((argv
= malloc( count
* sizeof(*argv
) )))
549 char **argvptr
= argv
+ reserved
;
551 if (*cmdline
== '\"')
553 if ((p
= strchr( cmdline
+ 1, '\"' )))
555 *argvptr
++ = cmdline
+ 1;
562 while (*p
&& isspace(*p
)) *p
++ = 0;
565 while (*p
&& !isspace(*p
)) p
++;
573 /***********************************************************************
576 * Build the environment of a new child process.
578 static char **build_envp( const char *env
, const char *extra_env
)
584 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
585 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
588 if ((envp
= malloc( count
* sizeof(*envp
) )))
590 extern char **environ
;
591 char **envptr
= envp
;
592 char **unixptr
= environ
;
593 /* first the extra strings */
594 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
595 /* then put PATH, HOME and WINEPREFIX from the unix env */
596 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
597 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
598 !memcmp( *unixptr
, "HOME=", 5 ) ||
599 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
600 /* now put the Windows environment strings */
601 for (p
= env
; *p
; p
+= strlen(p
) + 1)
603 if (memcmp( p
, "PATH=", 5 ) &&
604 memcmp( p
, "HOME=", 5 ) &&
605 memcmp( p
, "WINEPRELOAD=", 12 ) &&
606 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
614 /***********************************************************************
617 * Locate the Wine binary to exec for a new Win32 process.
619 static void exec_wine_binary( char **argv
, char **envp
)
621 const char *path
, *pos
, *ptr
;
623 /* first, try for a WINELOADER environment variable */
624 argv
[0] = getenv("WINELOADER");
626 execve( argv
[0], argv
, envp
);
628 /* next, try bin directory */
629 argv
[0] = BINDIR
"/wine";
630 execve( argv
[0], argv
, envp
);
632 /* now try the path of argv0 of the current binary */
633 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
634 if ((ptr
= strrchr( full_argv0
, '/' )))
636 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
637 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
638 execve( argv
[0], argv
, envp
);
642 /* now search in the Unix path */
643 if ((path
= getenv( "PATH" )))
645 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
649 while (*pos
== ':') pos
++;
651 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
652 memcpy( argv
[0], pos
, ptr
- pos
);
653 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
654 execve( argv
[0], argv
, envp
);
660 /* finally try the current directory */
662 execve( argv
[0], argv
, envp
);
666 /***********************************************************************
669 * Fork and exec a new Unix process, checking for errors.
671 static int fork_and_exec( const char *filename
, char *cmdline
,
672 const char *env
, const char *newdir
)
676 char *extra_env
= NULL
;
680 env
= GetEnvironmentStringsA();
681 extra_env
= DRIVE_BuildEnv();
689 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
690 if (!(pid
= fork())) /* child */
692 char **argv
= build_argv( cmdline
, filename
? 0 : 2 );
693 char **envp
= build_envp( env
, extra_env
);
696 if (newdir
) chdir(newdir
);
703 exec_wine_binary( argv
, envp
);
705 else execve( filename
, argv
, envp
);
708 write( fd
[1], &err
, sizeof(err
) );
712 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
717 if (pid
== -1) FILE_SetDosError();
719 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
724 /***********************************************************************
727 * Create a new process. If hFile is a valid handle we have an exe
728 * file, and we exec a new copy of wine to load it; otherwise we
729 * simply exec the specified filename as a Unix process.
731 BOOL
PROCESS_Create( HFILE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
732 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
733 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
734 LPPROCESS_INFORMATION info
, LPCSTR lpCurrentDirectory
)
738 const char *unixfilename
= NULL
;
739 const char *unixdir
= NULL
;
740 DOS_FULL_NAME full_name
;
741 HANDLE load_done_evt
= (HANDLE
)-1;
743 info
->hThread
= info
->hProcess
= INVALID_HANDLE_VALUE
;
745 if (lpCurrentDirectory
)
747 if (DOSFS_GetFullName( lpCurrentDirectory
, TRUE
, &full_name
))
748 unixdir
= full_name
.long_name
;
753 if (GetCurrentDirectoryA(sizeof(buf
),buf
))
755 if (DOSFS_GetFullName( buf
, TRUE
, &full_name
))
756 unixdir
= full_name
.long_name
;
760 /* create the process on the server side */
764 size_t len
= (hFile
== -1) ? 0 : MAX_PATH
;
765 struct new_process_request
*req
= server_alloc_req( sizeof(*req
), len
);
766 req
->inherit_all
= inherit
;
767 req
->create_flags
= flags
;
768 req
->start_flags
= startup
->dwFlags
;
769 req
->exe_file
= hFile
;
770 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
772 req
->hstdin
= startup
->hStdInput
;
773 req
->hstdout
= startup
->hStdOutput
;
774 req
->hstderr
= startup
->hStdError
;
778 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
779 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
780 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
782 req
->cmd_show
= startup
->wShowWindow
;
785 if (hFile
== -1) /* unix process */
787 unixfilename
= filename
;
788 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
))
789 unixfilename
= full_name
.long_name
;
791 else /* new wine process */
793 if (!GetLongPathNameA( filename
, server_data_ptr(req
), MAX_PATH
))
794 lstrcpynA( server_data_ptr(req
), filename
, MAX_PATH
);
796 ret
= !server_call( REQ_NEW_PROCESS
);
799 if (!ret
) return FALSE
;
801 /* fork and execute */
803 pid
= fork_and_exec( unixfilename
, cmd_line
, env
, unixdir
);
807 struct wait_process_request
*req
= server_alloc_req( sizeof(*req
), 0 );
808 req
->cancel
= (pid
== -1);
809 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
810 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
812 if ((ret
= !server_call( REQ_WAIT_PROCESS
)) && (pid
!= -1))
814 info
->dwProcessId
= (DWORD
)req
->pid
;
815 info
->dwThreadId
= (DWORD
)req
->tid
;
816 info
->hProcess
= req
->phandle
;
817 info
->hThread
= req
->thandle
;
818 load_done_evt
= req
->event
;
822 if (!ret
|| (pid
== -1)) goto error
;
824 /* Wait until process is initialized (or initialization failed) */
825 if (load_done_evt
!= (HANDLE
)-1)
830 handles
[0] = info
->hProcess
;
831 handles
[1] = load_done_evt
;
832 res
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
833 CloseHandle( load_done_evt
);
834 if (res
== STATUS_WAIT_0
) /* the process died */
837 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
838 CloseHandle( info
->hThread
);
839 CloseHandle( info
->hProcess
);
846 if (load_done_evt
!= (HANDLE
)-1) CloseHandle( load_done_evt
);
847 if (info
->hThread
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hThread
);
848 if (info
->hProcess
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hProcess
);
853 /***********************************************************************
854 * ExitProcess (KERNEL32.100)
856 void WINAPI
ExitProcess( DWORD status
)
858 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
861 struct terminate_process_request
*req
= server_alloc_req( sizeof(*req
), 0 );
862 /* send the exit code to the server */
863 req
->handle
= GetCurrentProcess();
864 req
->exit_code
= status
;
865 server_call( REQ_TERMINATE_PROCESS
);
871 /***********************************************************************
872 * ExitProcess16 (KERNEL.466)
874 void WINAPI
ExitProcess16( WORD status
)
877 ReleaseThunkLock( &count
);
878 ExitProcess( status
);
881 /******************************************************************************
882 * TerminateProcess (KERNEL32.684)
884 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
886 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
887 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
892 /***********************************************************************
893 * GetProcessDword (KERNEL32.18) (KERNEL.485)
894 * 'Of course you cannot directly access Windows internal structures'
896 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
901 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
903 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
905 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
911 case GPD_APP_COMPAT_FLAGS
:
912 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
913 return pTask
? pTask
->compat_flags
: 0;
915 case GPD_LOAD_DONE_EVENT
:
916 return current_process
.load_done_evt
;
918 case GPD_HINSTANCE16
:
919 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
920 return pTask
? pTask
->hInstance
: 0;
922 case GPD_WINDOWS_VERSION
:
923 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
924 return pTask
? pTask
->version
: 0;
927 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
930 return (DWORD
)¤t_process
;
932 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
933 return current_startupinfo
.hStdOutput
;
935 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
936 return current_startupinfo
.hStdInput
;
938 case GPD_STARTF_SHOWWINDOW
:
939 return current_startupinfo
.wShowWindow
;
941 case GPD_STARTF_SIZE
:
942 x
= current_startupinfo
.dwXSize
;
943 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
944 y
= current_startupinfo
.dwYSize
;
945 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
946 return MAKELONG( x
, y
);
948 case GPD_STARTF_POSITION
:
949 x
= current_startupinfo
.dwX
;
950 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
951 y
= current_startupinfo
.dwY
;
952 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
953 return MAKELONG( x
, y
);
955 case GPD_STARTF_FLAGS
:
956 return current_startupinfo
.dwFlags
;
962 return current_process
.flags
;
965 return current_process
.process_dword
;
968 ERR_(win32
)("Unknown offset %d\n", offset
);
973 /***********************************************************************
974 * SetProcessDword (KERNEL.484)
975 * 'Of course you cannot directly access Windows internal structures'
977 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
979 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
981 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
983 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
989 case GPD_APP_COMPAT_FLAGS
:
990 case GPD_LOAD_DONE_EVENT
:
991 case GPD_HINSTANCE16
:
992 case GPD_WINDOWS_VERSION
:
995 case GPD_STARTF_SHELLDATA
:
996 case GPD_STARTF_HOTKEY
:
997 case GPD_STARTF_SHOWWINDOW
:
998 case GPD_STARTF_SIZE
:
999 case GPD_STARTF_POSITION
:
1000 case GPD_STARTF_FLAGS
:
1003 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
1007 current_process
.process_dword
= value
;
1011 ERR_(win32
)("Unknown offset %d\n", offset
);
1017 /*********************************************************************
1018 * OpenProcess (KERNEL32.543)
1020 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1025 struct open_process_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1027 req
->pid
= (void *)id
;
1028 req
->access
= access
;
1029 req
->inherit
= inherit
;
1030 if (!server_call( REQ_OPEN_PROCESS
)) ret
= req
->handle
;
1036 /*********************************************************************
1037 * MapProcessHandle (KERNEL.483)
1039 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1044 struct get_process_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1045 req
->handle
= handle
;
1046 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= (DWORD
)req
->pid
;
1052 /***********************************************************************
1053 * SetPriorityClass (KERNEL32.503)
1055 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1060 struct set_process_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1061 req
->handle
= hprocess
;
1062 req
->priority
= priorityclass
;
1063 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1064 ret
= !server_call( REQ_SET_PROCESS_INFO
);
1071 /***********************************************************************
1072 * GetPriorityClass (KERNEL32.250)
1074 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1079 struct get_process_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1080 req
->handle
= hprocess
;
1081 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= req
->priority
;
1088 /***********************************************************************
1089 * SetProcessAffinityMask (KERNEL32.662)
1091 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1096 struct set_process_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1097 req
->handle
= hProcess
;
1098 req
->affinity
= affmask
;
1099 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1100 ret
= !server_call( REQ_SET_PROCESS_INFO
);
1106 /**********************************************************************
1107 * GetProcessAffinityMask (KERNEL32.373)
1109 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1110 LPDWORD lpProcessAffinityMask
,
1111 LPDWORD lpSystemAffinityMask
)
1116 struct get_process_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1117 req
->handle
= hProcess
;
1118 if (!server_call( REQ_GET_PROCESS_INFO
))
1120 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
1121 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
1130 /***********************************************************************
1131 * GetProcessVersion (KERNEL32)
1133 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1135 IMAGE_NT_HEADERS
*nt
;
1137 if (processid
&& processid
!= GetCurrentProcessId())
1138 return 0; /* FIXME: should use ReadProcessMemory */
1139 if ((nt
= RtlImageNtHeader( current_process
.module
)))
1140 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1141 nt
->OptionalHeader
.MinorSubsystemVersion
);
1145 /***********************************************************************
1146 * GetProcessFlags (KERNEL32)
1148 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1150 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1151 return current_process
.flags
;
1155 /***********************************************************************
1156 * SetProcessWorkingSetSize [KERNEL32.662]
1157 * Sets the min/max working set sizes for a specified process.
1160 * hProcess [I] Handle to the process of interest
1161 * minset [I] Specifies minimum working set size
1162 * maxset [I] Specifies maximum working set size
1166 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1169 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1170 if(( minset
== (DWORD
)-1) && (maxset
== (DWORD
)-1)) {
1171 /* Trim the working set to zero */
1172 /* Swap the process out of physical RAM */
1177 /***********************************************************************
1178 * GetProcessWorkingSetSize (KERNEL32)
1180 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1183 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1184 /* 32 MB working set size */
1185 if (minset
) *minset
= 32*1024*1024;
1186 if (maxset
) *maxset
= 32*1024*1024;
1190 /***********************************************************************
1191 * SetProcessShutdownParameters (KERNEL32)
1193 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1194 * Now tracks changes made (but does not act on these changes)
1196 static DWORD shutdown_flags
= 0;
1197 static DWORD shutdown_priority
= 0x280;
1199 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1201 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1202 shutdown_flags
= flags
;
1203 shutdown_priority
= level
;
1208 /***********************************************************************
1209 * GetProcessShutdownParameters (KERNEL32)
1212 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1214 *lpdwLevel
= shutdown_priority
;
1215 *lpdwFlags
= shutdown_flags
;
1220 /***********************************************************************
1221 * SetProcessPriorityBoost (KERNEL32)
1223 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1225 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1226 /* Say we can do it. I doubt the program will notice that we don't. */
1231 /***********************************************************************
1232 * ReadProcessMemory (KERNEL32)
1234 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1235 LPDWORD bytes_read
)
1237 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1238 unsigned int pos
= 0, len
, max
;
1241 if (bytes_read
) *bytes_read
= size
;
1243 /* first time, read total length to check for permissions */
1244 len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1245 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1251 struct read_process_memory_request
*req
= server_alloc_req( sizeof(*req
), max
);
1252 req
->handle
= process
;
1253 req
->addr
= (char *)addr
+ pos
- offset
;
1255 if (!(res
= server_call( REQ_READ_PROCESS_MEMORY
)))
1257 size_t result
= server_data_size( req
);
1258 if (result
> size
+ offset
) result
= size
+ offset
;
1259 memcpy( (char *)buffer
+ pos
, server_data_ptr(req
) + offset
, result
- offset
);
1260 size
-= result
- offset
;
1261 pos
+= result
- offset
;
1267 if (bytes_read
) *bytes_read
= 0;
1270 if (!size
) return TRUE
;
1271 max
= min( REQUEST_MAX_VAR_SIZE
, size
);
1272 len
= (max
+ sizeof(int) - 1) / sizeof(int);
1278 /***********************************************************************
1279 * WriteProcessMemory (KERNEL32)
1281 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1282 LPDWORD bytes_written
)
1284 unsigned int first_offset
, last_offset
;
1285 unsigned int pos
= 0, len
, max
, first_mask
, last_mask
;
1290 SetLastError( ERROR_INVALID_PARAMETER
);
1293 if (bytes_written
) *bytes_written
= size
;
1295 /* compute the mask for the first int */
1297 first_offset
= (unsigned int)addr
% sizeof(int);
1298 memset( &first_mask
, 0, first_offset
);
1300 /* compute the mask for the last int */
1301 last_offset
= (size
+ first_offset
) % sizeof(int);
1303 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1305 /* for the first request, use the total length */
1306 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1307 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1313 struct write_process_memory_request
*req
= server_alloc_req( sizeof(*req
), max
);
1314 req
->handle
= process
;
1315 req
->addr
= (char *)addr
- first_offset
+ pos
;
1317 req
->first_mask
= (!pos
) ? first_mask
: ~0;
1318 if (size
+ first_offset
<= max
) /* last round */
1320 req
->last_mask
= last_mask
;
1321 max
= size
+ first_offset
;
1323 else req
->last_mask
= ~0;
1325 memcpy( (char *)server_data_ptr(req
) + first_offset
, (char *)buffer
+ pos
,
1326 max
- first_offset
);
1327 if (!(res
= server_call( REQ_WRITE_PROCESS_MEMORY
)))
1329 pos
+= max
- first_offset
;
1330 size
-= max
- first_offset
;
1336 if (bytes_written
) *bytes_written
= 0;
1339 if (!size
) return TRUE
;
1341 len
= min( size
+ sizeof(int) - 1, REQUEST_MAX_VAR_SIZE
) / sizeof(int);
1342 max
= len
* sizeof(int);
1347 /***********************************************************************
1348 * RegisterServiceProcess (KERNEL, KERNEL32)
1350 * A service process calls this function to ensure that it continues to run
1351 * even after a user logged off.
1353 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1355 /* I don't think that Wine needs to do anything in that function */
1356 return 1; /* success */
1359 /***********************************************************************
1360 * GetExitCodeProcess [KERNEL32.325]
1362 * Gets termination status of specified process
1368 BOOL WINAPI
GetExitCodeProcess(
1369 HANDLE hProcess
, /* [in] handle to the process */
1370 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1375 struct get_process_info_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1376 req
->handle
= hProcess
;
1377 ret
= !server_call( REQ_GET_PROCESS_INFO
);
1378 if (ret
&& lpExitCode
) *lpExitCode
= req
->exit_code
;
1385 /***********************************************************************
1386 * SetErrorMode (KERNEL32.486)
1388 UINT WINAPI
SetErrorMode( UINT mode
)
1390 UINT old
= current_process
.error_mode
;
1391 current_process
.error_mode
= mode
;
1396 /**********************************************************************
1397 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
1399 * Allocates a thread local storage index
1402 * Success: TLS Index
1403 * Failure: 0xFFFFFFFF
1405 DWORD WINAPI
TlsAlloc( void )
1407 DWORD i
, mask
, ret
= 0;
1408 DWORD
*bits
= current_process
.tls_bits
;
1409 RtlAcquirePebLock();
1410 if (*bits
== 0xffffffff)
1414 if (*bits
== 0xffffffff)
1416 RtlReleasePebLock();
1417 SetLastError( ERROR_NO_MORE_ITEMS
);
1421 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
1423 RtlReleasePebLock();
1428 /**********************************************************************
1429 * TlsFree [KERNEL32.531] Releases a TLS index.
1431 * Releases a thread local storage index, making it available for reuse
1437 BOOL WINAPI
TlsFree(
1438 DWORD index
) /* [in] TLS Index to free */
1440 DWORD mask
= (1 << (index
& 31));
1441 DWORD
*bits
= current_process
.tls_bits
;
1444 SetLastError( ERROR_INVALID_PARAMETER
);
1447 if (index
>= 32) bits
++;
1448 RtlAcquirePebLock();
1449 if (!(*bits
& mask
)) /* already free? */
1451 RtlReleasePebLock();
1452 SetLastError( ERROR_INVALID_PARAMETER
);
1456 NtCurrentTeb()->tls_array
[index
] = 0;
1457 /* FIXME: should zero all other thread values */
1458 RtlReleasePebLock();
1463 /**********************************************************************
1464 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
1467 * Success: Value stored in calling thread's TLS slot for index
1468 * Failure: 0 and GetLastError returns NO_ERROR
1470 LPVOID WINAPI
TlsGetValue(
1471 DWORD index
) /* [in] TLS index to retrieve value for */
1475 SetLastError( ERROR_INVALID_PARAMETER
);
1478 SetLastError( ERROR_SUCCESS
);
1479 return NtCurrentTeb()->tls_array
[index
];
1483 /**********************************************************************
1484 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
1490 BOOL WINAPI
TlsSetValue(
1491 DWORD index
, /* [in] TLS index to set value for */
1492 LPVOID value
) /* [in] Value to be stored */
1496 SetLastError( ERROR_INVALID_PARAMETER
);
1499 NtCurrentTeb()->tls_array
[index
] = value
;
1504 /***********************************************************************
1505 * GetCurrentProcess (KERNEL32.198)
1507 #undef GetCurrentProcess
1508 HANDLE WINAPI
GetCurrentProcess(void)