4 * Copyright 1996, 1998 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
17 #include "wine/library.h"
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(process
);
29 DECLARE_DEBUG_CHANNEL(relay
);
30 DECLARE_DEBUG_CHANNEL(win32
);
34 /* Win32 process database */
37 LONG header
[2]; /* 00 Kernel object header */
38 HMODULE module
; /* 08 Main exe module (NT) */
39 void *event
; /* 0c Pointer to an event object (unused) */
40 DWORD exit_code
; /* 10 Process exit code */
41 DWORD unknown2
; /* 14 Unknown */
42 HANDLE heap
; /* 18 Default process heap */
43 HANDLE mem_context
; /* 1c Process memory context */
44 DWORD flags
; /* 20 Flags */
45 void *pdb16
; /* 24 DOS PSP */
46 WORD PSP_sel
; /* 28 Selector to DOS PSP */
47 WORD imte
; /* 2a IMTE for the process module */
48 WORD threads
; /* 2c Number of threads */
49 WORD running_threads
; /* 2e Number of running threads */
50 WORD free_lib_count
; /* 30 Recursion depth of FreeLibrary calls */
51 WORD ring0_threads
; /* 32 Number of ring 0 threads */
52 HANDLE system_heap
; /* 34 System heap to allocate handles */
53 HTASK task
; /* 38 Win16 task */
54 void *mem_map_files
; /* 3c Pointer to mem-mapped files */
55 struct _ENVDB
*env_db
; /* 40 Environment database */
56 void *handle_table
; /* 44 Handle table */
57 struct _PDB
*parent
; /* 48 Parent process */
58 void *modref_list
; /* 4c MODREF list */
59 void *thread_list
; /* 50 List of threads */
60 void *debuggee_CB
; /* 54 Debuggee context block */
61 void *local_heap_free
; /* 58 Head of local heap free list */
62 DWORD unknown4
; /* 5c Unknown */
63 CRITICAL_SECTION crit_section
; /* 60 Critical section */
64 DWORD unknown5
[3]; /* 78 Unknown */
65 void *console
; /* 84 Console */
66 DWORD tls_bits
[2]; /* 88 TLS in-use bits */
67 DWORD process_dword
; /* 90 Unknown */
68 struct _PDB
*group
; /* 94 Process group */
69 void *exe_modref
; /* 98 MODREF for the process EXE */
70 void *top_filter
; /* 9c Top exception filter */
71 DWORD priority
; /* a0 Priority level */
72 HANDLE heap_list
; /* a4 Head of process heap list */
73 void *heap_handles
; /* a8 Head of heap handles list */
74 DWORD unknown6
; /* ac Unknown */
75 void *console_provider
; /* b0 Console provider (??) */
76 WORD env_selector
; /* b4 Selector to process environment */
77 WORD error_mode
; /* b6 Error mode */
78 HANDLE load_done_evt
; /* b8 Event for process loading done */
79 void *UTState
; /* bc Head of Univeral Thunk list */
80 DWORD unknown8
; /* c0 Unknown (NT) */
81 LCID locale
; /* c4 Locale to be queried by GetThreadLocale (NT) */
87 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
88 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
89 #define PDB32_DOS_PROC 0x0010 /* Dos process */
90 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
91 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
92 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
94 static char **main_exe_argv
;
95 static char main_exe_name
[MAX_PATH
];
96 static HANDLE main_exe_file
;
98 unsigned int server_startticks
;
100 /* memory/environ.c */
101 extern struct _ENVDB
*ENV_BuildEnvironment(void);
102 extern BOOL
ENV_BuildCommandLine( char **argv
);
103 extern STARTUPINFOA current_startupinfo
;
105 /* scheduler/pthread.c */
106 extern void PTHREAD_init_done(void);
108 extern BOOL
MAIN_MainInit(void);
111 /***********************************************************************
112 * PROCESS_CallUserSignalProc
114 * FIXME: Some of the signals aren't sent correctly!
116 * The exact meaning of the USER signals is undocumented, but this
117 * should cover the basic idea:
119 * USIG_DLL_UNLOAD_WIN16
120 * This is sent when a 16-bit module is unloaded.
122 * USIG_DLL_UNLOAD_WIN32
123 * This is sent when a 32-bit module is unloaded.
125 * USIG_DLL_UNLOAD_ORPHANS
126 * This is sent after the last Win3.1 module is unloaded,
127 * to allow removal of orphaned menus.
129 * USIG_FAULT_DIALOG_PUSH
130 * USIG_FAULT_DIALOG_POP
131 * These are called to allow USER to prepare for displaying a
132 * fault dialog, even though the fault might have happened while
133 * inside a USER critical section.
136 * This is called from the context of a new thread, as soon as it
137 * has started to run.
140 * This is called, still in its context, just before a thread is
141 * about to terminate.
143 * USIG_PROCESS_CREATE
144 * This is called, in the parent process context, after a new process
148 * This is called in the new process context, just after the main thread
149 * has started execution (after the main thread's USIG_THREAD_INIT has
152 * USIG_PROCESS_LOADED
153 * This is called after the executable file has been loaded into the
154 * new process context.
156 * USIG_PROCESS_RUNNING
157 * This is called immediately before the main entry point is called.
160 * This is called in the context of a process that is about to
161 * terminate (but before the last thread's USIG_THREAD_EXIT has
164 * USIG_PROCESS_DESTROY
165 * This is called after a process has terminated.
168 * The meaning of the dwFlags bits is as follows:
171 * Current process is 32-bit.
174 * Current process is a (Win32) GUI process.
176 * USIG_FLAGS_FEEDBACK
177 * Current process needs 'feedback' (determined from the STARTUPINFO
178 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
181 * The signal is being sent due to a fault.
183 void PROCESS_CallUserSignalProc( UINT uCode
, HMODULE hModule
)
185 DWORD flags
= current_process
.flags
;
186 DWORD startup_flags
= current_startupinfo
.dwFlags
;
189 /* Determine dwFlags */
191 if ( !(flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
193 if ( !(flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
195 if ( dwFlags
& USIG_FLAGS_GUI
)
197 /* Feedback defaults to ON */
198 if ( !(startup_flags
& STARTF_FORCEOFFFEEDBACK
) )
199 dwFlags
|= USIG_FLAGS_FEEDBACK
;
203 /* Feedback defaults to OFF */
204 if (startup_flags
& STARTF_FORCEONFEEDBACK
)
205 dwFlags
|= USIG_FLAGS_FEEDBACK
;
208 /* Convert module handle to 16-bit */
210 if ( HIWORD( hModule
) )
211 hModule
= MapHModuleLS( hModule
);
213 /* Call USER signal proc */
215 if ( Callout
.UserSignalProc
)
217 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
218 Callout
.UserSignalProc( uCode
, GetCurrentThreadId(), dwFlags
, hModule
);
220 Callout
.UserSignalProc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
225 /***********************************************************************
226 * set_console_handles
228 * Set the console handles to use stdin/stdout.
230 static void set_console_handles( HANDLE console
)
232 wine_server_send_fd( 0 );
233 wine_server_send_fd( 1 );
235 SERVER_START_REQ( set_console_fd
)
237 req
->handle
= console
;
247 /***********************************************************************
250 * Main process initialisation code
252 static BOOL
process_init( char *argv
[] )
255 int create_flags
= 0;
257 /* store the program name */
259 main_exe_argv
= argv
;
261 /* Fill the initial process structure */
262 current_process
.exit_code
= STILL_ACTIVE
;
263 current_process
.threads
= 1;
264 current_process
.running_threads
= 1;
265 current_process
.ring0_threads
= 1;
266 current_process
.group
= ¤t_process
;
267 current_process
.priority
= 8; /* Normal */
269 /* Setup the server connection */
272 /* Retrieve startup info from the server */
273 SERVER_START_VAR_REQ( init_process
, sizeof(main_exe_name
)-1 )
275 req
->ldt_copy
= &wine_ldt_copy
;
276 req
->ppid
= getppid();
277 if ((ret
= !SERVER_CALL_ERR()))
279 size_t len
= server_data_size( req
);
280 memcpy( main_exe_name
, server_data_ptr(req
), len
);
281 main_exe_name
[len
] = 0;
282 main_exe_file
= req
->exe_file
;
283 create_flags
= req
->create_flags
;
284 current_startupinfo
.dwFlags
= req
->start_flags
;
285 server_startticks
= req
->server_start
;
286 current_startupinfo
.wShowWindow
= req
->cmd_show
;
287 current_startupinfo
.hStdInput
= req
->hstdin
;
288 current_startupinfo
.hStdOutput
= req
->hstdout
;
289 current_startupinfo
.hStdError
= req
->hstderr
;
293 if (!ret
) return FALSE
;
295 SetStdHandle( STD_INPUT_HANDLE
, current_startupinfo
.hStdInput
);
296 SetStdHandle( STD_OUTPUT_HANDLE
, current_startupinfo
.hStdOutput
);
297 SetStdHandle( STD_ERROR_HANDLE
, current_startupinfo
.hStdError
);
298 if (create_flags
& CREATE_NEW_CONSOLE
)
299 set_console_handles( current_startupinfo
.hStdOutput
);
301 /* Create the process heap */
302 current_process
.heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
304 /* Now we can use the pthreads routines */
307 /* Copy the parent environment */
308 if (!(current_process
.env_db
= ENV_BuildEnvironment())) return FALSE
;
310 /* Parse command line arguments */
311 OPTIONS_ParseOptions( argv
);
313 return MAIN_MainInit();
317 /***********************************************************************
320 * Startup routine of a new process. Runs on the new process stack.
322 static void start_process(void)
324 int debugged
, console_app
;
325 LPTHREAD_START_ROUTINE entry
;
328 /* build command line */
329 if (!ENV_BuildCommandLine( main_exe_argv
)) goto error
;
331 /* create 32-bit module for main exe */
332 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
334 /* use original argv[0] as name for the main module */
335 if (!main_exe_name
[0])
337 if (!GetLongPathNameA( full_argv0
, main_exe_name
, sizeof(main_exe_name
) ))
338 lstrcpynA( main_exe_name
, full_argv0
, sizeof(main_exe_name
) );
341 /* Retrieve entry point address */
342 entry
= (LPTHREAD_START_ROUTINE
)((char*)current_process
.module
+
343 PE_HEADER(current_process
.module
)->OptionalHeader
.AddressOfEntryPoint
);
344 console_app
= (PE_HEADER(current_process
.module
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
345 if (console_app
) current_process
.flags
|= PDB32_CONSOLE_PROC
;
347 /* Signal the parent process to continue */
348 SERVER_START_REQ( init_process_done
)
350 req
->module
= (void *)current_process
.module
;
352 req
->name
= main_exe_name
;
353 req
->exe_file
= main_exe_file
;
354 req
->gui
= !console_app
;
356 debugged
= req
->debugged
;
360 /* Install signal handlers; this cannot be done before, since we cannot
361 * send exceptions to the debugger before the create process event that
362 * is sent by REQ_INIT_PROCESS_DONE */
363 if (!SIGNAL_Init()) goto error
;
365 /* create the main modref and load dependencies */
366 if (!(wm
= PE_CreateModule( current_process
.module
, main_exe_name
, 0, 0, FALSE
)))
372 MODULE_DllProcessAttach( NULL
, (LPVOID
)1 );
375 /* Get pointers to USER routines called by KERNEL */
378 /* Call FinalUserInit routine */
379 if (Callout
.FinalUserInit16
) Callout
.FinalUserInit16();
381 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
382 * context of the parent process. Actually, the USER signal proc
383 * doesn't really care about that, but it *does* require that the
384 * startup parameters are correctly set up, so that GetProcessDword
385 * works. Furthermore, before calling the USER signal proc the
386 * 16-bit stack must be set up, which it is only after TASK_Create
387 * in the case of a 16-bit process. Thus, we send the signal here.
389 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
390 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
391 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
392 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
393 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
394 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
396 TRACE_(relay
)( "Starting Win32 process %s (entryproc=%p) tid=%08lx\n",
397 main_exe_name
, entry
, GetCurrentThreadId() );
398 if (debugged
) DbgBreakPoint();
399 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
400 * Dunno about other OSs */
401 SetLastError(0); /* clear error code */
402 ExitThread( entry(NULL
) );
405 ExitProcess( GetLastError() );
409 /***********************************************************************
412 * Try to open the Winelib app .so file based on argv[0] or WINEPRELOAD.
414 void *open_winelib_app( char *argv
[] )
421 if ((name
= getenv( "WINEPRELOAD" )))
423 if (!(ret
= wine_dll_load_main_exe( name
, 0, errStr
, sizeof(errStr
) )))
425 MESSAGE( "%s: could not load '%s' as specified in the WINEPRELOAD environment variable: %s\n",
426 argv
[0], name
, errStr
);
432 const char *argv0
= main_exe_name
;
435 /* if argv[0] is "wine", don't try to load anything */
437 if (!(name
= strrchr( argv0
, '/' ))) name
= argv0
;
439 if (!strcmp( name
, "wine" )) return NULL
;
442 /* now try argv[0] with ".so" appended */
443 if ((tmp
= HeapAlloc( GetProcessHeap(), 0, strlen(argv0
) + 4 )))
445 strcpy( tmp
, argv0
);
446 strcat( tmp
, ".so" );
447 /* search in PATH only if there was no '/' in argv[0] */
448 ret
= wine_dll_load_main_exe( tmp
, (name
== argv0
), errStr
, sizeof(errStr
) );
449 if (!ret
&& !argv
[1])
451 /* if no argv[1], this will be better than displaying usage */
452 MESSAGE( "%s: could not load library '%s' as Winelib application: %s\n",
453 argv
[0], tmp
, errStr
);
456 HeapFree( GetProcessHeap(), 0, tmp
);
462 /***********************************************************************
465 * Wine initialisation: load and start the main exe file.
467 void PROCESS_InitWine( int argc
, char *argv
[], LPSTR win16_exe_name
, HANDLE
*win16_exe_file
)
469 DWORD stack_size
= 0;
471 /* Initialize everything */
472 if (!process_init( argv
)) exit(1);
474 if (open_winelib_app( argv
)) goto found
; /* try to open argv[0] as a winelib app */
476 main_exe_argv
= ++argv
; /* remove argv[0] (wine itself) */
478 if (!main_exe_name
[0])
480 if (!argv
[0]) OPTIONS_Usage();
482 /* open the exe file */
483 if (!SearchPathA( NULL
, argv
[0], ".exe", sizeof(main_exe_name
), main_exe_name
, NULL
) &&
484 !SearchPathA( NULL
, argv
[0], NULL
, sizeof(main_exe_name
), main_exe_name
, NULL
))
486 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
493 if ((main_exe_file
= CreateFileA( main_exe_name
, GENERIC_READ
, FILE_SHARE_READ
,
494 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
)
496 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
501 /* first try Win32 format; this will fail if the file is not a PE binary */
502 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 )))
504 if (PE_HEADER(current_process
.module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
505 ExitProcess( ERROR_BAD_EXE_FORMAT
);
506 stack_size
= PE_HEADER(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
510 /* it must be 16-bit or DOS format */
511 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
512 current_process
.flags
|= PDB32_WIN16_PROC
;
513 strcpy( win16_exe_name
, main_exe_name
);
514 main_exe_name
[0] = 0;
515 *win16_exe_file
= main_exe_file
;
520 /* allocate main thread stack */
521 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
523 /* switch to the new stack */
524 SYSDEPS_SwitchToThreadStack( start_process
);
527 ExitProcess( GetLastError() );
531 /***********************************************************************
532 * PROCESS_InitWinelib
534 * Initialisation of a new Winelib process.
536 void PROCESS_InitWinelib( int argc
, char *argv
[] )
538 if (!process_init( argv
)) exit(1);
540 /* allocate main thread stack */
541 if (!THREAD_InitStack( NtCurrentTeb(), 0 )) ExitProcess( GetLastError() );
543 /* switch to the new stack */
544 SYSDEPS_SwitchToThreadStack( start_process
);
548 /***********************************************************************
551 * Build an argv array from a command-line.
552 * The command-line is modified to insert nulls.
553 * 'reserved' is the number of args to reserve before the first one.
555 static char **build_argv( char *cmdline
, int reserved
)
558 int count
= reserved
+ 1;
561 /* if first word is quoted store it as a single arg */
562 if (*cmdline
== '\"')
564 if ((p
= strchr( cmdline
+ 1, '\"' )))
573 while (*p
&& isspace(*p
)) p
++;
576 while (*p
&& !isspace(*p
)) p
++;
579 if ((argv
= malloc( count
* sizeof(*argv
) )))
581 char **argvptr
= argv
+ reserved
;
583 if (*cmdline
== '\"')
585 if ((p
= strchr( cmdline
+ 1, '\"' )))
587 *argvptr
++ = cmdline
+ 1;
594 while (*p
&& isspace(*p
)) *p
++ = 0;
597 while (*p
&& !isspace(*p
)) p
++;
605 /***********************************************************************
608 * Build the environment of a new child process.
610 static char **build_envp( const char *env
, const char *extra_env
)
616 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
617 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
620 if ((envp
= malloc( count
* sizeof(*envp
) )))
622 extern char **environ
;
623 char **envptr
= envp
;
624 char **unixptr
= environ
;
625 /* first the extra strings */
626 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
627 /* then put PATH, HOME and WINEPREFIX from the unix env */
628 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
629 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
630 !memcmp( *unixptr
, "HOME=", 5 ) ||
631 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
632 /* now put the Windows environment strings */
633 for (p
= env
; *p
; p
+= strlen(p
) + 1)
635 if (memcmp( p
, "PATH=", 5 ) &&
636 memcmp( p
, "HOME=", 5 ) &&
637 memcmp( p
, "WINEPRELOAD=", 12 ) &&
638 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
646 /***********************************************************************
649 * Locate the Wine binary to exec for a new Win32 process.
651 static void exec_wine_binary( char **argv
, char **envp
)
653 const char *path
, *pos
, *ptr
;
655 /* first, try for a WINELOADER environment variable */
656 argv
[0] = getenv("WINELOADER");
658 execve( argv
[0], argv
, envp
);
660 /* next, try bin directory */
661 argv
[0] = BINDIR
"/wine";
662 execve( argv
[0], argv
, envp
);
664 /* now try the path of argv0 of the current binary */
665 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
666 if ((ptr
= strrchr( full_argv0
, '/' )))
668 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
669 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
670 execve( argv
[0], argv
, envp
);
674 /* now search in the Unix path */
675 if ((path
= getenv( "PATH" )))
677 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
681 while (*pos
== ':') pos
++;
683 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
684 memcpy( argv
[0], pos
, ptr
- pos
);
685 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
686 execve( argv
[0], argv
, envp
);
692 /* finally try the current directory */
694 execve( argv
[0], argv
, envp
);
698 /***********************************************************************
701 * Fork and exec a new Unix process, checking for errors.
703 static int fork_and_exec( const char *filename
, char *cmdline
,
704 const char *env
, const char *newdir
)
708 char *extra_env
= NULL
;
712 env
= GetEnvironmentStringsA();
713 extra_env
= DRIVE_BuildEnv();
721 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
722 if (!(pid
= fork())) /* child */
724 char **argv
= build_argv( cmdline
, filename
? 0 : 2 );
725 char **envp
= build_envp( env
, extra_env
);
728 if (newdir
) chdir(newdir
);
735 exec_wine_binary( argv
, envp
);
737 else execve( filename
, argv
, envp
);
740 write( fd
[1], &err
, sizeof(err
) );
744 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
749 if (pid
== -1) FILE_SetDosError();
751 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
756 /***********************************************************************
759 * Create a new process. If hFile is a valid handle we have an exe
760 * file, and we exec a new copy of wine to load it; otherwise we
761 * simply exec the specified filename as a Unix process.
763 BOOL
PROCESS_Create( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
764 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
765 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
766 LPPROCESS_INFORMATION info
, LPCSTR lpCurrentDirectory
)
770 const char *unixfilename
= NULL
;
771 const char *unixdir
= NULL
;
772 DOS_FULL_NAME full_dir
, full_name
;
773 HANDLE load_done_evt
= 0;
776 info
->hThread
= info
->hProcess
= 0;
778 if (lpCurrentDirectory
)
780 if (DOSFS_GetFullName( lpCurrentDirectory
, TRUE
, &full_dir
))
781 unixdir
= full_dir
.long_name
;
786 if (GetCurrentDirectoryA(sizeof(buf
),buf
))
788 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
))
789 unixdir
= full_dir
.long_name
;
793 /* create the process on the server side */
795 SERVER_START_VAR_REQ( new_process
, MAX_PATH
)
797 req
->inherit_all
= inherit
;
798 req
->create_flags
= flags
;
799 req
->start_flags
= startup
->dwFlags
;
800 req
->exe_file
= hFile
;
801 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
803 req
->hstdin
= startup
->hStdInput
;
804 req
->hstdout
= startup
->hStdOutput
;
805 req
->hstderr
= startup
->hStdError
;
809 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
810 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
811 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
813 req
->cmd_show
= startup
->wShowWindow
;
815 if (!hFile
) /* unix process */
817 unixfilename
= filename
;
818 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
))
819 unixfilename
= full_name
.long_name
;
820 lstrcpynA( server_data_ptr(req
), unixfilename
, MAX_PATH
);
822 else /* new wine process */
824 if (!GetLongPathNameA( filename
, server_data_ptr(req
), MAX_PATH
))
825 lstrcpynA( server_data_ptr(req
), filename
, MAX_PATH
);
827 ret
= !SERVER_CALL_ERR();
828 process_info
= req
->info
;
831 if (!ret
) return FALSE
;
833 /* fork and execute */
835 pid
= fork_and_exec( unixfilename
, cmd_line
, env
, unixdir
);
837 /* wait for the new process info to be ready */
840 if ((pid
!= -1) && (WaitForSingleObject( process_info
, 2000 ) == STATUS_WAIT_0
))
842 SERVER_START_REQ( get_new_process_info
)
844 req
->info
= process_info
;
845 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
846 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
847 if ((ret
= !SERVER_CALL_ERR()))
849 info
->dwProcessId
= (DWORD
)req
->pid
;
850 info
->dwThreadId
= (DWORD
)req
->tid
;
851 info
->hProcess
= req
->phandle
;
852 info
->hThread
= req
->thandle
;
853 load_done_evt
= req
->event
;
858 CloseHandle( process_info
);
859 if (!ret
) goto error
;
861 /* Wait until process is initialized (or initialization failed) */
867 handles
[0] = info
->hProcess
;
868 handles
[1] = load_done_evt
;
869 res
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
870 CloseHandle( load_done_evt
);
871 if (res
== STATUS_WAIT_0
) /* the process died */
874 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
875 CloseHandle( info
->hThread
);
876 CloseHandle( info
->hProcess
);
883 if (load_done_evt
) CloseHandle( load_done_evt
);
884 if (info
->hThread
) CloseHandle( info
->hThread
);
885 if (info
->hProcess
) CloseHandle( info
->hProcess
);
890 /***********************************************************************
891 * ExitProcess (KERNEL32.@)
893 void WINAPI
ExitProcess( DWORD status
)
895 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
896 SERVER_START_REQ( terminate_process
)
898 /* send the exit code to the server */
899 req
->handle
= GetCurrentProcess();
900 req
->exit_code
= status
;
907 /***********************************************************************
908 * ExitProcess16 (KERNEL.466)
910 void WINAPI
ExitProcess16( WORD status
)
913 ReleaseThunkLock( &count
);
914 ExitProcess( status
);
917 /******************************************************************************
918 * TerminateProcess (KERNEL32.684)
920 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
922 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
923 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
928 /***********************************************************************
929 * GetProcessDword (KERNEL32.18) (KERNEL.485)
930 * 'Of course you cannot directly access Windows internal structures'
932 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
936 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
938 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
940 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
946 case GPD_APP_COMPAT_FLAGS
:
947 return GetAppCompatFlags16(0);
949 case GPD_LOAD_DONE_EVENT
:
950 return current_process
.load_done_evt
;
952 case GPD_HINSTANCE16
:
953 return GetTaskDS16();
955 case GPD_WINDOWS_VERSION
:
956 return GetExeVersion16();
959 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
962 return (DWORD
)¤t_process
;
964 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
965 return current_startupinfo
.hStdOutput
;
967 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
968 return current_startupinfo
.hStdInput
;
970 case GPD_STARTF_SHOWWINDOW
:
971 return current_startupinfo
.wShowWindow
;
973 case GPD_STARTF_SIZE
:
974 x
= current_startupinfo
.dwXSize
;
975 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
976 y
= current_startupinfo
.dwYSize
;
977 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
978 return MAKELONG( x
, y
);
980 case GPD_STARTF_POSITION
:
981 x
= current_startupinfo
.dwX
;
982 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
983 y
= current_startupinfo
.dwY
;
984 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
985 return MAKELONG( x
, y
);
987 case GPD_STARTF_FLAGS
:
988 return current_startupinfo
.dwFlags
;
994 return current_process
.flags
;
997 return current_process
.process_dword
;
1000 ERR_(win32
)("Unknown offset %d\n", offset
);
1005 /***********************************************************************
1006 * SetProcessDword (KERNEL.484)
1007 * 'Of course you cannot directly access Windows internal structures'
1009 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
1011 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1013 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1015 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1021 case GPD_APP_COMPAT_FLAGS
:
1022 case GPD_LOAD_DONE_EVENT
:
1023 case GPD_HINSTANCE16
:
1024 case GPD_WINDOWS_VERSION
:
1027 case GPD_STARTF_SHELLDATA
:
1028 case GPD_STARTF_HOTKEY
:
1029 case GPD_STARTF_SHOWWINDOW
:
1030 case GPD_STARTF_SIZE
:
1031 case GPD_STARTF_POSITION
:
1032 case GPD_STARTF_FLAGS
:
1035 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
1039 current_process
.process_dword
= value
;
1043 ERR_(win32
)("Unknown offset %d\n", offset
);
1049 /*********************************************************************
1050 * OpenProcess (KERNEL32.543)
1052 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1055 SERVER_START_REQ( open_process
)
1057 req
->pid
= (void *)id
;
1058 req
->access
= access
;
1059 req
->inherit
= inherit
;
1060 if (!SERVER_CALL_ERR()) ret
= req
->handle
;
1066 /*********************************************************************
1067 * MapProcessHandle (KERNEL.483)
1069 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1072 SERVER_START_REQ( get_process_info
)
1074 req
->handle
= handle
;
1075 if (!SERVER_CALL_ERR()) ret
= (DWORD
)req
->pid
;
1081 /***********************************************************************
1082 * SetPriorityClass (KERNEL32.@)
1084 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1087 SERVER_START_REQ( set_process_info
)
1089 req
->handle
= hprocess
;
1090 req
->priority
= priorityclass
;
1091 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1092 ret
= !SERVER_CALL_ERR();
1099 /***********************************************************************
1100 * GetPriorityClass (KERNEL32.@)
1102 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1105 SERVER_START_REQ( get_process_info
)
1107 req
->handle
= hprocess
;
1108 if (!SERVER_CALL_ERR()) ret
= req
->priority
;
1115 /***********************************************************************
1116 * SetProcessAffinityMask (KERNEL32.@)
1118 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1121 SERVER_START_REQ( set_process_info
)
1123 req
->handle
= hProcess
;
1124 req
->affinity
= affmask
;
1125 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1126 ret
= !SERVER_CALL_ERR();
1132 /**********************************************************************
1133 * GetProcessAffinityMask (KERNEL32.373)
1135 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1136 LPDWORD lpProcessAffinityMask
,
1137 LPDWORD lpSystemAffinityMask
)
1140 SERVER_START_REQ( get_process_info
)
1142 req
->handle
= hProcess
;
1143 if (!SERVER_CALL_ERR())
1145 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
1146 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
1155 /***********************************************************************
1156 * GetProcessVersion (KERNEL32.@)
1158 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1160 IMAGE_NT_HEADERS
*nt
;
1162 if (processid
&& processid
!= GetCurrentProcessId())
1164 FIXME("should use ReadProcessMemory\n");
1167 if ((nt
= RtlImageNtHeader( current_process
.module
)))
1168 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1169 nt
->OptionalHeader
.MinorSubsystemVersion
);
1173 /***********************************************************************
1174 * GetProcessFlags (KERNEL32.@)
1176 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1178 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1179 return current_process
.flags
;
1183 /***********************************************************************
1184 * SetProcessWorkingSetSize [KERNEL32.662]
1185 * Sets the min/max working set sizes for a specified process.
1188 * hProcess [I] Handle to the process of interest
1189 * minset [I] Specifies minimum working set size
1190 * maxset [I] Specifies maximum working set size
1194 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1197 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1198 if(( minset
== (DWORD
)-1) && (maxset
== (DWORD
)-1)) {
1199 /* Trim the working set to zero */
1200 /* Swap the process out of physical RAM */
1205 /***********************************************************************
1206 * GetProcessWorkingSetSize (KERNEL32.@)
1208 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1211 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1212 /* 32 MB working set size */
1213 if (minset
) *minset
= 32*1024*1024;
1214 if (maxset
) *maxset
= 32*1024*1024;
1218 /***********************************************************************
1219 * SetProcessShutdownParameters (KERNEL32.@)
1221 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1222 * Now tracks changes made (but does not act on these changes)
1224 static DWORD shutdown_flags
= 0;
1225 static DWORD shutdown_priority
= 0x280;
1227 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1229 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1230 shutdown_flags
= flags
;
1231 shutdown_priority
= level
;
1236 /***********************************************************************
1237 * GetProcessShutdownParameters (KERNEL32.@)
1240 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1242 *lpdwLevel
= shutdown_priority
;
1243 *lpdwFlags
= shutdown_flags
;
1248 /***********************************************************************
1249 * SetProcessPriorityBoost (KERNEL32.@)
1251 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1253 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1254 /* Say we can do it. I doubt the program will notice that we don't. */
1259 /***********************************************************************
1260 * ReadProcessMemory (KERNEL32.@)
1261 * ReadProcessMemory (WIN32S16.6)
1263 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1264 LPDWORD bytes_read
)
1266 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1267 unsigned int pos
= 0, len
, max
;
1270 if (bytes_read
) *bytes_read
= size
;
1272 /* first time, read total length to check for permissions */
1273 len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1274 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1278 SERVER_START_VAR_REQ( read_process_memory
, max
)
1280 req
->handle
= process
;
1281 req
->addr
= (char *)addr
+ pos
- offset
;
1283 if (!(res
= SERVER_CALL_ERR()))
1285 size_t result
= server_data_size( req
);
1286 if (result
> size
+ offset
) result
= size
+ offset
;
1287 memcpy( (char *)buffer
+ pos
, server_data_ptr(req
) + offset
, result
- offset
);
1288 size
-= result
- offset
;
1289 pos
+= result
- offset
;
1295 if (bytes_read
) *bytes_read
= 0;
1298 if (!size
) return TRUE
;
1299 max
= min( REQUEST_MAX_VAR_SIZE
, size
);
1300 len
= (max
+ sizeof(int) - 1) / sizeof(int);
1306 /***********************************************************************
1307 * WriteProcessMemory (KERNEL32.@)
1309 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1310 LPDWORD bytes_written
)
1312 unsigned int first_offset
, last_offset
;
1313 unsigned int pos
= 0, len
, max
, first_mask
, last_mask
;
1318 SetLastError( ERROR_INVALID_PARAMETER
);
1321 if (bytes_written
) *bytes_written
= size
;
1323 /* compute the mask for the first int */
1325 first_offset
= (unsigned int)addr
% sizeof(int);
1326 memset( &first_mask
, 0, first_offset
);
1328 /* compute the mask for the last int */
1329 last_offset
= (size
+ first_offset
) % sizeof(int);
1331 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1333 /* for the first request, use the total length */
1334 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1335 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1339 SERVER_START_VAR_REQ( write_process_memory
, max
)
1341 req
->handle
= process
;
1342 req
->addr
= (char *)addr
- first_offset
+ pos
;
1344 req
->first_mask
= (!pos
) ? first_mask
: ~0;
1345 if (size
+ first_offset
<= max
) /* last round */
1347 req
->last_mask
= last_mask
;
1348 max
= size
+ first_offset
;
1350 else req
->last_mask
= ~0;
1352 memcpy( (char *)server_data_ptr(req
) + first_offset
, (char *)buffer
+ pos
,
1353 max
- first_offset
);
1354 if (!(res
= SERVER_CALL_ERR()))
1356 pos
+= max
- first_offset
;
1357 size
-= max
- first_offset
;
1363 if (bytes_written
) *bytes_written
= 0;
1366 if (!size
) return TRUE
;
1368 len
= min( size
+ sizeof(int) - 1, REQUEST_MAX_VAR_SIZE
) / sizeof(int);
1369 max
= len
* sizeof(int);
1374 /***********************************************************************
1375 * RegisterServiceProcess (KERNEL.491)
1376 * RegisterServiceProcess (KERNEL32.@)
1378 * A service process calls this function to ensure that it continues to run
1379 * even after a user logged off.
1381 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1383 /* I don't think that Wine needs to do anything in that function */
1384 return 1; /* success */
1387 /***********************************************************************
1388 * GetExitCodeProcess [KERNEL32.325]
1390 * Gets termination status of specified process
1396 BOOL WINAPI
GetExitCodeProcess(
1397 HANDLE hProcess
, /* [in] handle to the process */
1398 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1401 SERVER_START_REQ( get_process_info
)
1403 req
->handle
= hProcess
;
1404 ret
= !SERVER_CALL_ERR();
1405 if (ret
&& lpExitCode
) *lpExitCode
= req
->exit_code
;
1412 /***********************************************************************
1413 * SetErrorMode (KERNEL32.@)
1415 UINT WINAPI
SetErrorMode( UINT mode
)
1417 UINT old
= current_process
.error_mode
;
1418 current_process
.error_mode
= mode
;
1423 /**************************************************************************
1424 * SetFileApisToOEM (KERNEL32.@)
1426 VOID WINAPI
SetFileApisToOEM(void)
1428 current_process
.flags
|= PDB32_FILE_APIS_OEM
;
1432 /**************************************************************************
1433 * SetFileApisToANSI (KERNEL32.@)
1435 VOID WINAPI
SetFileApisToANSI(void)
1437 current_process
.flags
&= ~PDB32_FILE_APIS_OEM
;
1441 /******************************************************************************
1442 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
1445 * TRUE: Set of file functions is using ANSI code page
1446 * FALSE: Set of file functions is using OEM code page
1448 BOOL WINAPI
AreFileApisANSI(void)
1450 return !(current_process
.flags
& PDB32_FILE_APIS_OEM
);
1454 /**********************************************************************
1455 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1457 * Allocates a thread local storage index
1460 * Success: TLS Index
1461 * Failure: 0xFFFFFFFF
1463 DWORD WINAPI
TlsAlloc( void )
1465 DWORD i
, mask
, ret
= 0;
1466 DWORD
*bits
= current_process
.tls_bits
;
1467 RtlAcquirePebLock();
1468 if (*bits
== 0xffffffff)
1472 if (*bits
== 0xffffffff)
1474 RtlReleasePebLock();
1475 SetLastError( ERROR_NO_MORE_ITEMS
);
1479 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
1481 RtlReleasePebLock();
1486 /**********************************************************************
1487 * TlsFree [KERNEL32.@] Releases a TLS index.
1489 * Releases a thread local storage index, making it available for reuse
1495 BOOL WINAPI
TlsFree(
1496 DWORD index
) /* [in] TLS Index to free */
1498 DWORD mask
= (1 << (index
& 31));
1499 DWORD
*bits
= current_process
.tls_bits
;
1502 SetLastError( ERROR_INVALID_PARAMETER
);
1505 if (index
>= 32) bits
++;
1506 RtlAcquirePebLock();
1507 if (!(*bits
& mask
)) /* already free? */
1509 RtlReleasePebLock();
1510 SetLastError( ERROR_INVALID_PARAMETER
);
1514 NtCurrentTeb()->tls_array
[index
] = 0;
1515 /* FIXME: should zero all other thread values */
1516 RtlReleasePebLock();
1521 /**********************************************************************
1522 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1525 * Success: Value stored in calling thread's TLS slot for index
1526 * Failure: 0 and GetLastError returns NO_ERROR
1528 LPVOID WINAPI
TlsGetValue(
1529 DWORD index
) /* [in] TLS index to retrieve value for */
1533 SetLastError( ERROR_INVALID_PARAMETER
);
1536 SetLastError( ERROR_SUCCESS
);
1537 return NtCurrentTeb()->tls_array
[index
];
1541 /**********************************************************************
1542 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1548 BOOL WINAPI
TlsSetValue(
1549 DWORD index
, /* [in] TLS index to set value for */
1550 LPVOID value
) /* [in] Value to be stored */
1554 SetLastError( ERROR_INVALID_PARAMETER
);
1557 NtCurrentTeb()->tls_array
[index
] = value
;
1562 /***********************************************************************
1563 * GetCurrentProcess (KERNEL32.@)
1565 #undef GetCurrentProcess
1566 HANDLE WINAPI
GetCurrentProcess(void)