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)\n", main_exe_name
, entry
);
397 if (debugged
) DbgBreakPoint();
398 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
399 * Dunno about other OSs */
400 SetLastError(0); /* clear error code */
401 ExitThread( entry(NULL
) );
404 ExitProcess( GetLastError() );
408 /***********************************************************************
411 * Try to open the Winelib app .so file based on argv[0] or WINEPRELOAD.
413 void *open_winelib_app( char *argv
[] )
420 if ((name
= getenv( "WINEPRELOAD" )))
422 if (!(ret
= wine_dll_load_main_exe( name
, 0, errStr
, sizeof(errStr
) )))
424 MESSAGE( "%s: could not load '%s' as specified in the WINEPRELOAD environment variable: %s\n",
425 argv
[0], name
, errStr
);
431 const char *argv0
= main_exe_name
;
434 /* if argv[0] is "wine", don't try to load anything */
436 if (!(name
= strrchr( argv0
, '/' ))) name
= argv0
;
438 if (!strcmp( name
, "wine" )) return NULL
;
441 /* now try argv[0] with ".so" appended */
442 if ((tmp
= HeapAlloc( GetProcessHeap(), 0, strlen(argv0
) + 4 )))
444 strcpy( tmp
, argv0
);
445 strcat( tmp
, ".so" );
446 /* search in PATH only if there was no '/' in argv[0] */
447 ret
= wine_dll_load_main_exe( tmp
, (name
== argv0
), errStr
, sizeof(errStr
) );
448 if (!ret
&& !argv
[1])
450 /* if no argv[1], this will be better than displaying usage */
451 MESSAGE( "%s: could not load library '%s' as Winelib application: %s\n",
452 argv
[0], tmp
, errStr
);
455 HeapFree( GetProcessHeap(), 0, tmp
);
461 /***********************************************************************
464 * Wine initialisation: load and start the main exe file.
466 void PROCESS_InitWine( int argc
, char *argv
[], LPSTR win16_exe_name
, HANDLE
*win16_exe_file
)
468 DWORD stack_size
= 0;
470 /* Initialize everything */
471 if (!process_init( argv
)) exit(1);
473 if (open_winelib_app( argv
)) goto found
; /* try to open argv[0] as a winelib app */
475 main_exe_argv
= ++argv
; /* remove argv[0] (wine itself) */
477 if (!main_exe_name
[0])
479 if (!argv
[0]) OPTIONS_Usage();
481 /* open the exe file */
482 if (!SearchPathA( NULL
, argv
[0], ".exe", sizeof(main_exe_name
), main_exe_name
, NULL
) &&
483 !SearchPathA( NULL
, argv
[0], NULL
, sizeof(main_exe_name
), main_exe_name
, NULL
))
485 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
492 if ((main_exe_file
= CreateFileA( main_exe_name
, GENERIC_READ
, FILE_SHARE_READ
,
493 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
)
495 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
500 /* first try Win32 format; this will fail if the file is not a PE binary */
501 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 )))
503 if (PE_HEADER(current_process
.module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
504 ExitProcess( ERROR_BAD_EXE_FORMAT
);
505 stack_size
= PE_HEADER(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
509 /* it must be 16-bit or DOS format */
510 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
511 current_process
.flags
|= PDB32_WIN16_PROC
;
512 strcpy( win16_exe_name
, main_exe_name
);
513 main_exe_name
[0] = 0;
514 *win16_exe_file
= main_exe_file
;
519 /* allocate main thread stack */
520 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
522 /* switch to the new stack */
523 SYSDEPS_SwitchToThreadStack( start_process
);
526 ExitProcess( GetLastError() );
530 /***********************************************************************
531 * PROCESS_InitWinelib
533 * Initialisation of a new Winelib process.
535 void PROCESS_InitWinelib( int argc
, char *argv
[] )
537 if (!process_init( argv
)) exit(1);
539 /* allocate main thread stack */
540 if (!THREAD_InitStack( NtCurrentTeb(), 0 )) ExitProcess( GetLastError() );
542 /* switch to the new stack */
543 SYSDEPS_SwitchToThreadStack( start_process
);
547 /***********************************************************************
550 * Build an argv array from a command-line.
551 * The command-line is modified to insert nulls.
552 * 'reserved' is the number of args to reserve before the first one.
554 static char **build_argv( char *cmdline
, int reserved
)
557 int count
= reserved
+ 1;
560 /* if first word is quoted store it as a single arg */
561 if (*cmdline
== '\"')
563 if ((p
= strchr( cmdline
+ 1, '\"' )))
572 while (*p
&& isspace(*p
)) p
++;
575 while (*p
&& !isspace(*p
)) p
++;
578 if ((argv
= malloc( count
* sizeof(*argv
) )))
580 char **argvptr
= argv
+ reserved
;
582 if (*cmdline
== '\"')
584 if ((p
= strchr( cmdline
+ 1, '\"' )))
586 *argvptr
++ = cmdline
+ 1;
593 while (*p
&& isspace(*p
)) *p
++ = 0;
596 while (*p
&& !isspace(*p
)) p
++;
604 /***********************************************************************
607 * Build the environment of a new child process.
609 static char **build_envp( const char *env
, const char *extra_env
)
615 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
616 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
619 if ((envp
= malloc( count
* sizeof(*envp
) )))
621 extern char **environ
;
622 char **envptr
= envp
;
623 char **unixptr
= environ
;
624 /* first the extra strings */
625 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
626 /* then put PATH, HOME and WINEPREFIX from the unix env */
627 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
628 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
629 !memcmp( *unixptr
, "HOME=", 5 ) ||
630 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
631 /* now put the Windows environment strings */
632 for (p
= env
; *p
; p
+= strlen(p
) + 1)
634 if (memcmp( p
, "PATH=", 5 ) &&
635 memcmp( p
, "HOME=", 5 ) &&
636 memcmp( p
, "WINEPRELOAD=", 12 ) &&
637 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
645 /***********************************************************************
648 * Locate the Wine binary to exec for a new Win32 process.
650 static void exec_wine_binary( char **argv
, char **envp
)
652 const char *path
, *pos
, *ptr
;
654 /* first, try for a WINELOADER environment variable */
655 argv
[0] = getenv("WINELOADER");
657 execve( argv
[0], argv
, envp
);
659 /* next, try bin directory */
660 argv
[0] = BINDIR
"/wine";
661 execve( argv
[0], argv
, envp
);
663 /* now try the path of argv0 of the current binary */
664 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
665 if ((ptr
= strrchr( full_argv0
, '/' )))
667 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
668 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
669 execve( argv
[0], argv
, envp
);
673 /* now search in the Unix path */
674 if ((path
= getenv( "PATH" )))
676 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
680 while (*pos
== ':') pos
++;
682 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
683 memcpy( argv
[0], pos
, ptr
- pos
);
684 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
685 execve( argv
[0], argv
, envp
);
691 /* finally try the current directory */
693 execve( argv
[0], argv
, envp
);
697 /***********************************************************************
700 * Fork and exec a new Unix process, checking for errors.
702 static int fork_and_exec( const char *filename
, char *cmdline
,
703 const char *env
, const char *newdir
)
707 char *extra_env
= NULL
;
711 env
= GetEnvironmentStringsA();
712 extra_env
= DRIVE_BuildEnv();
720 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
721 if (!(pid
= fork())) /* child */
723 char **argv
= build_argv( cmdline
, filename
? 0 : 2 );
724 char **envp
= build_envp( env
, extra_env
);
727 if (newdir
) chdir(newdir
);
734 exec_wine_binary( argv
, envp
);
736 else execve( filename
, argv
, envp
);
739 write( fd
[1], &err
, sizeof(err
) );
743 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
748 if (pid
== -1) FILE_SetDosError();
750 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
755 /***********************************************************************
758 * Create a new process. If hFile is a valid handle we have an exe
759 * file, and we exec a new copy of wine to load it; otherwise we
760 * simply exec the specified filename as a Unix process.
762 BOOL
PROCESS_Create( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
763 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
764 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
765 LPPROCESS_INFORMATION info
, LPCSTR lpCurrentDirectory
)
769 const char *unixfilename
= NULL
;
770 const char *unixdir
= NULL
;
771 DOS_FULL_NAME full_dir
, full_name
;
772 HANDLE load_done_evt
= 0;
775 info
->hThread
= info
->hProcess
= 0;
777 if (lpCurrentDirectory
)
779 if (DOSFS_GetFullName( lpCurrentDirectory
, TRUE
, &full_dir
))
780 unixdir
= full_dir
.long_name
;
785 if (GetCurrentDirectoryA(sizeof(buf
),buf
))
787 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
))
788 unixdir
= full_dir
.long_name
;
792 /* create the process on the server side */
794 SERVER_START_VAR_REQ( new_process
, MAX_PATH
)
796 req
->inherit_all
= inherit
;
797 req
->create_flags
= flags
;
798 req
->start_flags
= startup
->dwFlags
;
799 req
->exe_file
= hFile
;
800 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
802 req
->hstdin
= startup
->hStdInput
;
803 req
->hstdout
= startup
->hStdOutput
;
804 req
->hstderr
= startup
->hStdError
;
808 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
809 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
810 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
812 req
->cmd_show
= startup
->wShowWindow
;
814 if (!hFile
) /* unix process */
816 unixfilename
= filename
;
817 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
))
818 unixfilename
= full_name
.long_name
;
819 lstrcpynA( server_data_ptr(req
), unixfilename
, MAX_PATH
);
821 else /* new wine process */
823 if (!GetLongPathNameA( filename
, server_data_ptr(req
), MAX_PATH
))
824 lstrcpynA( server_data_ptr(req
), filename
, MAX_PATH
);
826 ret
= !SERVER_CALL_ERR();
827 process_info
= req
->info
;
830 if (!ret
) return FALSE
;
832 /* fork and execute */
834 pid
= fork_and_exec( unixfilename
, cmd_line
, env
, unixdir
);
836 /* wait for the new process info to be ready */
839 if ((pid
!= -1) && (WaitForSingleObject( process_info
, 2000 ) == STATUS_WAIT_0
))
841 SERVER_START_REQ( get_new_process_info
)
843 req
->info
= process_info
;
844 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
845 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
846 if ((ret
= !SERVER_CALL_ERR()))
848 info
->dwProcessId
= (DWORD
)req
->pid
;
849 info
->dwThreadId
= (DWORD
)req
->tid
;
850 info
->hProcess
= req
->phandle
;
851 info
->hThread
= req
->thandle
;
852 load_done_evt
= req
->event
;
857 CloseHandle( process_info
);
858 if (!ret
) goto error
;
860 /* Wait until process is initialized (or initialization failed) */
866 handles
[0] = info
->hProcess
;
867 handles
[1] = load_done_evt
;
868 res
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
869 CloseHandle( load_done_evt
);
870 if (res
== STATUS_WAIT_0
) /* the process died */
873 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
874 CloseHandle( info
->hThread
);
875 CloseHandle( info
->hProcess
);
882 if (load_done_evt
) CloseHandle( load_done_evt
);
883 if (info
->hThread
) CloseHandle( info
->hThread
);
884 if (info
->hProcess
) CloseHandle( info
->hProcess
);
889 /***********************************************************************
890 * ExitProcess (KERNEL32.@)
892 void WINAPI
ExitProcess( DWORD status
)
894 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
895 SERVER_START_REQ( terminate_process
)
897 /* send the exit code to the server */
898 req
->handle
= GetCurrentProcess();
899 req
->exit_code
= status
;
906 /***********************************************************************
907 * ExitProcess16 (KERNEL.466)
909 void WINAPI
ExitProcess16( WORD status
)
912 ReleaseThunkLock( &count
);
913 ExitProcess( status
);
916 /******************************************************************************
917 * TerminateProcess (KERNEL32.684)
919 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
921 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
922 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
927 /***********************************************************************
928 * GetProcessDword (KERNEL32.18) (KERNEL.485)
929 * 'Of course you cannot directly access Windows internal structures'
931 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
935 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
937 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
939 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
945 case GPD_APP_COMPAT_FLAGS
:
946 return GetAppCompatFlags16(0);
948 case GPD_LOAD_DONE_EVENT
:
949 return current_process
.load_done_evt
;
951 case GPD_HINSTANCE16
:
952 return GetTaskDS16();
954 case GPD_WINDOWS_VERSION
:
955 return GetExeVersion16();
958 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
961 return (DWORD
)¤t_process
;
963 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
964 return current_startupinfo
.hStdOutput
;
966 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
967 return current_startupinfo
.hStdInput
;
969 case GPD_STARTF_SHOWWINDOW
:
970 return current_startupinfo
.wShowWindow
;
972 case GPD_STARTF_SIZE
:
973 x
= current_startupinfo
.dwXSize
;
974 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
975 y
= current_startupinfo
.dwYSize
;
976 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
977 return MAKELONG( x
, y
);
979 case GPD_STARTF_POSITION
:
980 x
= current_startupinfo
.dwX
;
981 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
982 y
= current_startupinfo
.dwY
;
983 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
984 return MAKELONG( x
, y
);
986 case GPD_STARTF_FLAGS
:
987 return current_startupinfo
.dwFlags
;
993 return current_process
.flags
;
996 return current_process
.process_dword
;
999 ERR_(win32
)("Unknown offset %d\n", offset
);
1004 /***********************************************************************
1005 * SetProcessDword (KERNEL.484)
1006 * 'Of course you cannot directly access Windows internal structures'
1008 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
1010 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1012 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1014 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1020 case GPD_APP_COMPAT_FLAGS
:
1021 case GPD_LOAD_DONE_EVENT
:
1022 case GPD_HINSTANCE16
:
1023 case GPD_WINDOWS_VERSION
:
1026 case GPD_STARTF_SHELLDATA
:
1027 case GPD_STARTF_HOTKEY
:
1028 case GPD_STARTF_SHOWWINDOW
:
1029 case GPD_STARTF_SIZE
:
1030 case GPD_STARTF_POSITION
:
1031 case GPD_STARTF_FLAGS
:
1034 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
1038 current_process
.process_dword
= value
;
1042 ERR_(win32
)("Unknown offset %d\n", offset
);
1048 /*********************************************************************
1049 * OpenProcess (KERNEL32.543)
1051 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1054 SERVER_START_REQ( open_process
)
1056 req
->pid
= (void *)id
;
1057 req
->access
= access
;
1058 req
->inherit
= inherit
;
1059 if (!SERVER_CALL_ERR()) ret
= req
->handle
;
1065 /*********************************************************************
1066 * MapProcessHandle (KERNEL.483)
1068 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1071 SERVER_START_REQ( get_process_info
)
1073 req
->handle
= handle
;
1074 if (!SERVER_CALL_ERR()) ret
= (DWORD
)req
->pid
;
1080 /***********************************************************************
1081 * SetPriorityClass (KERNEL32.@)
1083 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1086 SERVER_START_REQ( set_process_info
)
1088 req
->handle
= hprocess
;
1089 req
->priority
= priorityclass
;
1090 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1091 ret
= !SERVER_CALL_ERR();
1098 /***********************************************************************
1099 * GetPriorityClass (KERNEL32.@)
1101 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1104 SERVER_START_REQ( get_process_info
)
1106 req
->handle
= hprocess
;
1107 if (!SERVER_CALL_ERR()) ret
= req
->priority
;
1114 /***********************************************************************
1115 * SetProcessAffinityMask (KERNEL32.@)
1117 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1120 SERVER_START_REQ( set_process_info
)
1122 req
->handle
= hProcess
;
1123 req
->affinity
= affmask
;
1124 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1125 ret
= !SERVER_CALL_ERR();
1131 /**********************************************************************
1132 * GetProcessAffinityMask (KERNEL32.373)
1134 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1135 LPDWORD lpProcessAffinityMask
,
1136 LPDWORD lpSystemAffinityMask
)
1139 SERVER_START_REQ( get_process_info
)
1141 req
->handle
= hProcess
;
1142 if (!SERVER_CALL_ERR())
1144 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
1145 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
1154 /***********************************************************************
1155 * GetProcessVersion (KERNEL32.@)
1157 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1159 IMAGE_NT_HEADERS
*nt
;
1161 if (processid
&& processid
!= GetCurrentProcessId())
1163 FIXME("should use ReadProcessMemory\n");
1166 if ((nt
= RtlImageNtHeader( current_process
.module
)))
1167 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1168 nt
->OptionalHeader
.MinorSubsystemVersion
);
1172 /***********************************************************************
1173 * GetProcessFlags (KERNEL32.@)
1175 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1177 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1178 return current_process
.flags
;
1182 /***********************************************************************
1183 * SetProcessWorkingSetSize [KERNEL32.662]
1184 * Sets the min/max working set sizes for a specified process.
1187 * hProcess [I] Handle to the process of interest
1188 * minset [I] Specifies minimum working set size
1189 * maxset [I] Specifies maximum working set size
1193 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1196 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1197 if(( minset
== (DWORD
)-1) && (maxset
== (DWORD
)-1)) {
1198 /* Trim the working set to zero */
1199 /* Swap the process out of physical RAM */
1204 /***********************************************************************
1205 * GetProcessWorkingSetSize (KERNEL32.@)
1207 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1210 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1211 /* 32 MB working set size */
1212 if (minset
) *minset
= 32*1024*1024;
1213 if (maxset
) *maxset
= 32*1024*1024;
1217 /***********************************************************************
1218 * SetProcessShutdownParameters (KERNEL32.@)
1220 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1221 * Now tracks changes made (but does not act on these changes)
1223 static DWORD shutdown_flags
= 0;
1224 static DWORD shutdown_priority
= 0x280;
1226 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1228 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1229 shutdown_flags
= flags
;
1230 shutdown_priority
= level
;
1235 /***********************************************************************
1236 * GetProcessShutdownParameters (KERNEL32.@)
1239 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1241 *lpdwLevel
= shutdown_priority
;
1242 *lpdwFlags
= shutdown_flags
;
1247 /***********************************************************************
1248 * SetProcessPriorityBoost (KERNEL32.@)
1250 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1252 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1253 /* Say we can do it. I doubt the program will notice that we don't. */
1258 /***********************************************************************
1259 * ReadProcessMemory (KERNEL32.@)
1260 * ReadProcessMemory (WIN32S16.6)
1262 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1263 LPDWORD bytes_read
)
1265 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1266 unsigned int pos
= 0, len
, max
;
1269 if (bytes_read
) *bytes_read
= size
;
1271 /* first time, read total length to check for permissions */
1272 len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1273 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1277 SERVER_START_VAR_REQ( read_process_memory
, max
)
1279 req
->handle
= process
;
1280 req
->addr
= (char *)addr
+ pos
- offset
;
1282 if (!(res
= SERVER_CALL_ERR()))
1284 size_t result
= server_data_size( req
);
1285 if (result
> size
+ offset
) result
= size
+ offset
;
1286 memcpy( (char *)buffer
+ pos
, server_data_ptr(req
) + offset
, result
- offset
);
1287 size
-= result
- offset
;
1288 pos
+= result
- offset
;
1294 if (bytes_read
) *bytes_read
= 0;
1297 if (!size
) return TRUE
;
1298 max
= min( REQUEST_MAX_VAR_SIZE
, size
);
1299 len
= (max
+ sizeof(int) - 1) / sizeof(int);
1305 /***********************************************************************
1306 * WriteProcessMemory (KERNEL32.@)
1308 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1309 LPDWORD bytes_written
)
1311 unsigned int first_offset
, last_offset
;
1312 unsigned int pos
= 0, len
, max
, first_mask
, last_mask
;
1317 SetLastError( ERROR_INVALID_PARAMETER
);
1320 if (bytes_written
) *bytes_written
= size
;
1322 /* compute the mask for the first int */
1324 first_offset
= (unsigned int)addr
% sizeof(int);
1325 memset( &first_mask
, 0, first_offset
);
1327 /* compute the mask for the last int */
1328 last_offset
= (size
+ first_offset
) % sizeof(int);
1330 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1332 /* for the first request, use the total length */
1333 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1334 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1338 SERVER_START_VAR_REQ( write_process_memory
, max
)
1340 req
->handle
= process
;
1341 req
->addr
= (char *)addr
- first_offset
+ pos
;
1343 req
->first_mask
= (!pos
) ? first_mask
: ~0;
1344 if (size
+ first_offset
<= max
) /* last round */
1346 req
->last_mask
= last_mask
;
1347 max
= size
+ first_offset
;
1349 else req
->last_mask
= ~0;
1351 memcpy( (char *)server_data_ptr(req
) + first_offset
, (char *)buffer
+ pos
,
1352 max
- first_offset
);
1353 if (!(res
= SERVER_CALL_ERR()))
1355 pos
+= max
- first_offset
;
1356 size
-= max
- first_offset
;
1362 if (bytes_written
) *bytes_written
= 0;
1365 if (!size
) return TRUE
;
1367 len
= min( size
+ sizeof(int) - 1, REQUEST_MAX_VAR_SIZE
) / sizeof(int);
1368 max
= len
* sizeof(int);
1373 /***********************************************************************
1374 * RegisterServiceProcess (KERNEL.491)
1375 * RegisterServiceProcess (KERNEL32.@)
1377 * A service process calls this function to ensure that it continues to run
1378 * even after a user logged off.
1380 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1382 /* I don't think that Wine needs to do anything in that function */
1383 return 1; /* success */
1386 /***********************************************************************
1387 * GetExitCodeProcess [KERNEL32.325]
1389 * Gets termination status of specified process
1395 BOOL WINAPI
GetExitCodeProcess(
1396 HANDLE hProcess
, /* [in] handle to the process */
1397 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1400 SERVER_START_REQ( get_process_info
)
1402 req
->handle
= hProcess
;
1403 ret
= !SERVER_CALL_ERR();
1404 if (ret
&& lpExitCode
) *lpExitCode
= req
->exit_code
;
1411 /***********************************************************************
1412 * SetErrorMode (KERNEL32.@)
1414 UINT WINAPI
SetErrorMode( UINT mode
)
1416 UINT old
= current_process
.error_mode
;
1417 current_process
.error_mode
= mode
;
1422 /**************************************************************************
1423 * SetFileApisToOEM (KERNEL32.@)
1425 VOID WINAPI
SetFileApisToOEM(void)
1427 current_process
.flags
|= PDB32_FILE_APIS_OEM
;
1431 /**************************************************************************
1432 * SetFileApisToANSI (KERNEL32.@)
1434 VOID WINAPI
SetFileApisToANSI(void)
1436 current_process
.flags
&= ~PDB32_FILE_APIS_OEM
;
1440 /******************************************************************************
1441 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
1444 * TRUE: Set of file functions is using ANSI code page
1445 * FALSE: Set of file functions is using OEM code page
1447 BOOL WINAPI
AreFileApisANSI(void)
1449 return !(current_process
.flags
& PDB32_FILE_APIS_OEM
);
1453 /**********************************************************************
1454 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1456 * Allocates a thread local storage index
1459 * Success: TLS Index
1460 * Failure: 0xFFFFFFFF
1462 DWORD WINAPI
TlsAlloc( void )
1464 DWORD i
, mask
, ret
= 0;
1465 DWORD
*bits
= current_process
.tls_bits
;
1466 RtlAcquirePebLock();
1467 if (*bits
== 0xffffffff)
1471 if (*bits
== 0xffffffff)
1473 RtlReleasePebLock();
1474 SetLastError( ERROR_NO_MORE_ITEMS
);
1478 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
1480 RtlReleasePebLock();
1485 /**********************************************************************
1486 * TlsFree [KERNEL32.@] Releases a TLS index.
1488 * Releases a thread local storage index, making it available for reuse
1494 BOOL WINAPI
TlsFree(
1495 DWORD index
) /* [in] TLS Index to free */
1497 DWORD mask
= (1 << (index
& 31));
1498 DWORD
*bits
= current_process
.tls_bits
;
1501 SetLastError( ERROR_INVALID_PARAMETER
);
1504 if (index
>= 32) bits
++;
1505 RtlAcquirePebLock();
1506 if (!(*bits
& mask
)) /* already free? */
1508 RtlReleasePebLock();
1509 SetLastError( ERROR_INVALID_PARAMETER
);
1513 NtCurrentTeb()->tls_array
[index
] = 0;
1514 /* FIXME: should zero all other thread values */
1515 RtlReleasePebLock();
1520 /**********************************************************************
1521 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1524 * Success: Value stored in calling thread's TLS slot for index
1525 * Failure: 0 and GetLastError returns NO_ERROR
1527 LPVOID WINAPI
TlsGetValue(
1528 DWORD index
) /* [in] TLS index to retrieve value for */
1532 SetLastError( ERROR_INVALID_PARAMETER
);
1535 SetLastError( ERROR_SUCCESS
);
1536 return NtCurrentTeb()->tls_array
[index
];
1540 /**********************************************************************
1541 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1547 BOOL WINAPI
TlsSetValue(
1548 DWORD index
, /* [in] TLS index to set value for */
1549 LPVOID value
) /* [in] Value to be stored */
1553 SetLastError( ERROR_INVALID_PARAMETER
);
1556 NtCurrentTeb()->tls_array
[index
] = value
;
1561 /***********************************************************************
1562 * GetCurrentProcess (KERNEL32.@)
1564 #undef GetCurrentProcess
1565 HANDLE WINAPI
GetCurrentProcess(void)