4 * Copyright 1996, 1998 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
17 #include "wine/library.h"
23 #include "wine/server.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 char *main_exe_name_ptr
= main_exe_name
;
97 static HANDLE main_exe_file
;
99 unsigned int server_startticks
;
101 /* memory/environ.c */
102 extern struct _ENVDB
*ENV_BuildEnvironment(void);
103 extern BOOL
ENV_BuildCommandLine( char **argv
);
104 extern STARTUPINFOA current_startupinfo
;
106 /* scheduler/pthread.c */
107 extern void PTHREAD_init_done(void);
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
, HMODULE16 hModule
)
186 DWORD flags
= current_process
.flags
;
187 DWORD startup_flags
= current_startupinfo
.dwFlags
;
190 if (!Callout
.UserSignalProc
) return;
192 /* Determine dwFlags */
194 if ( !(flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
196 if ( !(flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
198 if ( dwFlags
& USIG_FLAGS_GUI
)
200 /* Feedback defaults to ON */
201 if ( !(startup_flags
& STARTF_FORCEOFFFEEDBACK
) )
202 dwFlags
|= USIG_FLAGS_FEEDBACK
;
206 /* Feedback defaults to OFF */
207 if (startup_flags
& STARTF_FORCEONFEEDBACK
)
208 dwFlags
|= USIG_FLAGS_FEEDBACK
;
211 /* Call USER signal proc */
213 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
214 Callout
.UserSignalProc( uCode
, GetCurrentThreadId(), dwFlags
, hModule
);
216 Callout
.UserSignalProc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
220 /***********************************************************************
221 * set_console_handles
223 * Set the console handles to use stdin/stdout.
225 static void set_console_handles( HANDLE console
)
227 wine_server_send_fd( 0 );
228 wine_server_send_fd( 1 );
230 SERVER_START_REQ( set_console_fd
)
232 req
->handle
= console
;
242 /***********************************************************************
245 * Main process initialisation code
247 static BOOL
process_init( char *argv
[] )
250 int create_flags
= 0;
252 /* store the program name */
254 main_exe_argv
= argv
;
256 /* Fill the initial process structure */
257 current_process
.exit_code
= STILL_ACTIVE
;
258 current_process
.threads
= 1;
259 current_process
.running_threads
= 1;
260 current_process
.ring0_threads
= 1;
261 current_process
.group
= ¤t_process
;
262 current_process
.priority
= 8; /* Normal */
264 /* Setup the server connection */
267 /* Retrieve startup info from the server */
268 SERVER_START_VAR_REQ( init_process
, sizeof(main_exe_name
)-1 )
270 req
->ldt_copy
= &wine_ldt_copy
;
271 req
->ppid
= getppid();
272 if ((ret
= !SERVER_CALL_ERR()))
274 size_t len
= server_data_size( req
);
275 memcpy( main_exe_name
, server_data_ptr(req
), len
);
276 main_exe_name
[len
] = 0;
277 main_exe_file
= req
->exe_file
;
278 create_flags
= req
->create_flags
;
279 current_startupinfo
.dwFlags
= req
->start_flags
;
280 server_startticks
= req
->server_start
;
281 current_startupinfo
.wShowWindow
= req
->cmd_show
;
282 current_startupinfo
.hStdInput
= req
->hstdin
;
283 current_startupinfo
.hStdOutput
= req
->hstdout
;
284 current_startupinfo
.hStdError
= req
->hstderr
;
288 if (!ret
) return FALSE
;
290 SetStdHandle( STD_INPUT_HANDLE
, current_startupinfo
.hStdInput
);
291 SetStdHandle( STD_OUTPUT_HANDLE
, current_startupinfo
.hStdOutput
);
292 SetStdHandle( STD_ERROR_HANDLE
, current_startupinfo
.hStdError
);
293 if (create_flags
& CREATE_NEW_CONSOLE
)
294 set_console_handles( current_startupinfo
.hStdOutput
);
296 /* Create the process heap */
297 current_process
.heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
299 /* Now we can use the pthreads routines */
302 /* Copy the parent environment */
303 if (!(current_process
.env_db
= ENV_BuildEnvironment())) return FALSE
;
305 /* Parse command line arguments */
306 OPTIONS_ParseOptions( argv
);
308 return MAIN_MainInit();
312 /***********************************************************************
315 * Startup routine of a new process. Runs on the new process stack.
317 static void start_process(void)
319 int debugged
, console_app
;
320 LPTHREAD_START_ROUTINE entry
;
323 /* use original argv[0] as name for the main module */
324 if (!main_exe_name
[0])
326 if (!GetLongPathNameA( full_argv0
, main_exe_name
, sizeof(main_exe_name
) ))
327 lstrcpynA( main_exe_name
, full_argv0
, sizeof(main_exe_name
) );
330 /* Retrieve entry point address */
331 entry
= (LPTHREAD_START_ROUTINE
)((char*)current_process
.module
+
332 PE_HEADER(current_process
.module
)->OptionalHeader
.AddressOfEntryPoint
);
333 console_app
= (PE_HEADER(current_process
.module
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
334 if (console_app
) current_process
.flags
|= PDB32_CONSOLE_PROC
;
336 /* Signal the parent process to continue */
337 SERVER_START_REQ( init_process_done
)
339 req
->module
= (void *)current_process
.module
;
341 /* API requires a double indirection */
342 req
->name
= &main_exe_name_ptr
;
343 req
->exe_file
= main_exe_file
;
344 req
->gui
= !console_app
;
346 debugged
= req
->debugged
;
350 /* Install signal handlers; this cannot be done before, since we cannot
351 * send exceptions to the debugger before the create process event that
352 * is sent by REQ_INIT_PROCESS_DONE */
353 if (!SIGNAL_Init()) goto error
;
355 /* create the main modref and load dependencies */
356 if (!(wm
= PE_CreateModule( current_process
.module
, main_exe_name
, 0, 0, FALSE
)))
362 MODULE_DllProcessAttach( NULL
, (LPVOID
)1 );
365 /* Get pointers to USER routines called by KERNEL */
368 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
369 * context of the parent process. Actually, the USER signal proc
370 * doesn't really care about that, but it *does* require that the
371 * startup parameters are correctly set up, so that GetProcessDword
372 * works. Furthermore, before calling the USER signal proc the
373 * 16-bit stack must be set up, which it is only after TASK_Create
374 * in the case of a 16-bit process. Thus, we send the signal here.
376 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
377 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
378 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
379 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
380 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
381 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
384 DPRINTF( "%08lx:Starting process %s (entryproc=%p)\n",
385 GetCurrentThreadId(), main_exe_name
, entry
);
386 if (debugged
) DbgBreakPoint();
387 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
388 * Dunno about other OSs */
389 SetLastError(0); /* clear error code */
390 ExitThread( entry(NULL
) );
393 ExitProcess( GetLastError() );
397 /***********************************************************************
400 * Try to open the Winelib app .so file based on argv[0] or WINEPRELOAD.
402 void *open_winelib_app( char *argv
[] )
409 if ((name
= getenv( "WINEPRELOAD" )))
411 if (!(ret
= wine_dll_load_main_exe( name
, 0, errStr
, sizeof(errStr
) )))
413 MESSAGE( "%s: could not load '%s' as specified in the WINEPRELOAD environment variable: %s\n",
414 argv
[0], name
, errStr
);
420 const char *argv0
= main_exe_name
;
423 /* if argv[0] is "wine", don't try to load anything */
425 if (!(name
= strrchr( argv0
, '/' ))) name
= argv0
;
427 if (!strcmp( name
, "wine" )) return NULL
;
430 /* now try argv[0] with ".so" appended */
431 if ((tmp
= HeapAlloc( GetProcessHeap(), 0, strlen(argv0
) + 4 )))
433 strcpy( tmp
, argv0
);
434 strcat( tmp
, ".so" );
435 /* search in PATH only if there was no '/' in argv[0] */
436 ret
= wine_dll_load_main_exe( tmp
, (name
== argv0
), errStr
, sizeof(errStr
) );
437 if (!ret
&& !argv
[1])
439 /* if no argv[1], this will be better than displaying usage */
440 MESSAGE( "%s: could not load library '%s' as Winelib application: %s\n",
441 argv
[0], tmp
, errStr
);
444 HeapFree( GetProcessHeap(), 0, tmp
);
450 /***********************************************************************
453 * Wine initialisation: load and start the main exe file.
455 void PROCESS_InitWine( int argc
, char *argv
[], LPSTR win16_exe_name
, HANDLE
*win16_exe_file
)
457 DWORD stack_size
= 0;
459 /* Initialize everything */
460 if (!process_init( argv
)) exit(1);
462 if (open_winelib_app( argv
)) goto found
; /* try to open argv[0] as a winelib app */
464 main_exe_argv
= ++argv
; /* remove argv[0] (wine itself) */
466 if (!main_exe_name
[0])
468 if (!argv
[0]) OPTIONS_Usage();
470 /* open the exe file */
471 if (!SearchPathA( NULL
, argv
[0], ".exe", sizeof(main_exe_name
), main_exe_name
, NULL
) &&
472 !SearchPathA( NULL
, argv
[0], NULL
, sizeof(main_exe_name
), main_exe_name
, NULL
))
474 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
481 if ((main_exe_file
= CreateFileA( main_exe_name
, GENERIC_READ
, FILE_SHARE_READ
,
482 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
)
484 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
489 /* first try Win32 format; this will fail if the file is not a PE binary */
490 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 )))
492 if (PE_HEADER(current_process
.module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
493 ExitProcess( ERROR_BAD_EXE_FORMAT
);
497 /* it must be 16-bit or DOS format */
498 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
499 current_process
.flags
|= PDB32_WIN16_PROC
;
500 strcpy( win16_exe_name
, main_exe_name
);
501 main_exe_name
[0] = 0;
502 *win16_exe_file
= main_exe_file
;
507 /* build command line */
508 if (!ENV_BuildCommandLine( main_exe_argv
)) goto error
;
510 /* create 32-bit module for main exe */
511 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
512 stack_size
= PE_HEADER(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
514 /* allocate main thread stack */
515 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
517 /* switch to the new stack */
518 SYSDEPS_SwitchToThreadStack( start_process
);
521 ExitProcess( GetLastError() );
525 /***********************************************************************
528 * Build an argv array from a command-line.
529 * The command-line is modified to insert nulls.
530 * 'reserved' is the number of args to reserve before the first one.
532 static char **build_argv( char *cmdline
, int reserved
)
535 int count
= reserved
+ 1;
538 /* if first word is quoted store it as a single arg */
539 if (*cmdline
== '\"')
541 if ((p
= strchr( cmdline
+ 1, '\"' )))
550 while (*p
&& isspace(*p
)) p
++;
553 while (*p
&& !isspace(*p
)) p
++;
556 if ((argv
= malloc( count
* sizeof(*argv
) )))
558 char **argvptr
= argv
+ reserved
;
560 if (*cmdline
== '\"')
562 if ((p
= strchr( cmdline
+ 1, '\"' )))
564 *argvptr
++ = cmdline
+ 1;
571 while (*p
&& isspace(*p
)) *p
++ = 0;
574 while (*p
&& !isspace(*p
)) p
++;
582 /***********************************************************************
585 * Build the environment of a new child process.
587 static char **build_envp( const char *env
, const char *extra_env
)
593 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
594 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
597 if ((envp
= malloc( count
* sizeof(*envp
) )))
599 extern char **environ
;
600 char **envptr
= envp
;
601 char **unixptr
= environ
;
602 /* first the extra strings */
603 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
604 /* then put PATH, HOME and WINEPREFIX from the unix env */
605 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
606 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
607 !memcmp( *unixptr
, "HOME=", 5 ) ||
608 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
609 /* now put the Windows environment strings */
610 for (p
= env
; *p
; p
+= strlen(p
) + 1)
612 if (memcmp( p
, "PATH=", 5 ) &&
613 memcmp( p
, "HOME=", 5 ) &&
614 memcmp( p
, "WINEPRELOAD=", 12 ) &&
615 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
623 /***********************************************************************
626 * Locate the Wine binary to exec for a new Win32 process.
628 static void exec_wine_binary( char **argv
, char **envp
)
630 const char *path
, *pos
, *ptr
;
632 /* first, try for a WINELOADER environment variable */
633 argv
[0] = getenv("WINELOADER");
635 execve( argv
[0], argv
, envp
);
637 /* next, try bin directory */
638 argv
[0] = BINDIR
"/wine";
639 execve( argv
[0], argv
, envp
);
641 /* now try the path of argv0 of the current binary */
642 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
643 if ((ptr
= strrchr( full_argv0
, '/' )))
645 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
646 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
647 execve( argv
[0], argv
, envp
);
651 /* now search in the Unix path */
652 if ((path
= getenv( "PATH" )))
654 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
658 while (*pos
== ':') pos
++;
660 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
661 memcpy( argv
[0], pos
, ptr
- pos
);
662 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
663 execve( argv
[0], argv
, envp
);
669 /* finally try the current directory */
671 execve( argv
[0], argv
, envp
);
675 /***********************************************************************
678 * Fork and exec a new Unix process, checking for errors.
680 static int fork_and_exec( const char *filename
, char *cmdline
,
681 const char *env
, const char *newdir
)
685 char *extra_env
= NULL
;
689 env
= GetEnvironmentStringsA();
690 extra_env
= DRIVE_BuildEnv();
698 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
699 if (!(pid
= fork())) /* child */
701 char **argv
= build_argv( cmdline
, filename
? 0 : 2 );
702 char **envp
= build_envp( env
, extra_env
);
705 if (newdir
) chdir(newdir
);
712 exec_wine_binary( argv
, envp
);
714 else execve( filename
, argv
, envp
);
717 write( fd
[1], &err
, sizeof(err
) );
721 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
726 if (pid
== -1) FILE_SetDosError();
728 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
733 /***********************************************************************
736 * Create a new process. If hFile is a valid handle we have an exe
737 * file, and we exec a new copy of wine to load it; otherwise we
738 * simply exec the specified filename as a Unix process.
740 BOOL
PROCESS_Create( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
741 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
742 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
743 LPPROCESS_INFORMATION info
, LPCSTR lpCurrentDirectory
)
747 const char *unixfilename
= NULL
;
748 const char *unixdir
= NULL
;
749 DOS_FULL_NAME full_dir
, full_name
;
750 HANDLE load_done_evt
= 0;
753 info
->hThread
= info
->hProcess
= 0;
755 if (lpCurrentDirectory
)
757 if (DOSFS_GetFullName( lpCurrentDirectory
, TRUE
, &full_dir
))
758 unixdir
= full_dir
.long_name
;
763 if (GetCurrentDirectoryA(sizeof(buf
),buf
))
765 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
))
766 unixdir
= full_dir
.long_name
;
770 /* create the process on the server side */
772 SERVER_START_VAR_REQ( new_process
, MAX_PATH
)
774 req
->inherit_all
= inherit
;
775 req
->create_flags
= flags
;
776 req
->start_flags
= startup
->dwFlags
;
777 req
->exe_file
= hFile
;
778 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
780 req
->hstdin
= startup
->hStdInput
;
781 req
->hstdout
= startup
->hStdOutput
;
782 req
->hstderr
= startup
->hStdError
;
786 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
787 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
788 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
790 req
->cmd_show
= startup
->wShowWindow
;
792 if (!hFile
) /* unix process */
794 unixfilename
= filename
;
795 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
))
796 unixfilename
= full_name
.long_name
;
797 lstrcpynA( server_data_ptr(req
), unixfilename
, MAX_PATH
);
799 else /* new wine process */
801 if (!GetLongPathNameA( filename
, server_data_ptr(req
), MAX_PATH
))
802 lstrcpynA( server_data_ptr(req
), filename
, MAX_PATH
);
804 ret
= !SERVER_CALL_ERR();
805 process_info
= req
->info
;
808 if (!ret
) return FALSE
;
810 /* fork and execute */
812 pid
= fork_and_exec( unixfilename
, cmd_line
, env
, unixdir
);
814 /* wait for the new process info to be ready */
817 if ((pid
!= -1) && (WaitForSingleObject( process_info
, 2000 ) == STATUS_WAIT_0
))
819 SERVER_START_REQ( get_new_process_info
)
821 req
->info
= process_info
;
822 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
823 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
824 if ((ret
= !SERVER_CALL_ERR()))
826 info
->dwProcessId
= (DWORD
)req
->pid
;
827 info
->dwThreadId
= (DWORD
)req
->tid
;
828 info
->hProcess
= req
->phandle
;
829 info
->hThread
= req
->thandle
;
830 load_done_evt
= req
->event
;
835 CloseHandle( process_info
);
836 if (!ret
) goto error
;
838 /* Wait until process is initialized (or initialization failed) */
844 handles
[0] = info
->hProcess
;
845 handles
[1] = load_done_evt
;
846 res
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
847 CloseHandle( load_done_evt
);
848 if (res
== STATUS_WAIT_0
) /* the process died */
851 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
852 CloseHandle( info
->hThread
);
853 CloseHandle( info
->hProcess
);
860 if (load_done_evt
) CloseHandle( load_done_evt
);
861 if (info
->hThread
) CloseHandle( info
->hThread
);
862 if (info
->hProcess
) CloseHandle( info
->hProcess
);
867 /***********************************************************************
868 * ExitProcess (KERNEL32.@)
870 void WINAPI
ExitProcess( DWORD status
)
872 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
873 SERVER_START_REQ( terminate_process
)
875 /* send the exit code to the server */
876 req
->handle
= GetCurrentProcess();
877 req
->exit_code
= status
;
884 /***********************************************************************
885 * ExitProcess (KERNEL.466)
887 void WINAPI
ExitProcess16( WORD status
)
890 ReleaseThunkLock( &count
);
891 ExitProcess( status
);
894 /******************************************************************************
895 * TerminateProcess (KERNEL32.@)
897 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
899 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
900 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
905 /***********************************************************************
906 * GetProcessDword (KERNEL.485)
907 * GetProcessDword (KERNEL32.18)
908 * 'Of course you cannot directly access Windows internal structures'
910 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
914 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
916 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
918 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
924 case GPD_APP_COMPAT_FLAGS
:
925 return GetAppCompatFlags16(0);
927 case GPD_LOAD_DONE_EVENT
:
928 return current_process
.load_done_evt
;
930 case GPD_HINSTANCE16
:
931 return GetTaskDS16();
933 case GPD_WINDOWS_VERSION
:
934 return GetExeVersion16();
937 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
940 return (DWORD
)¤t_process
;
942 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
943 return current_startupinfo
.hStdOutput
;
945 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
946 return current_startupinfo
.hStdInput
;
948 case GPD_STARTF_SHOWWINDOW
:
949 return current_startupinfo
.wShowWindow
;
951 case GPD_STARTF_SIZE
:
952 x
= current_startupinfo
.dwXSize
;
953 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
954 y
= current_startupinfo
.dwYSize
;
955 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
956 return MAKELONG( x
, y
);
958 case GPD_STARTF_POSITION
:
959 x
= current_startupinfo
.dwX
;
960 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
961 y
= current_startupinfo
.dwY
;
962 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
963 return MAKELONG( x
, y
);
965 case GPD_STARTF_FLAGS
:
966 return current_startupinfo
.dwFlags
;
972 return current_process
.flags
;
975 return current_process
.process_dword
;
978 ERR_(win32
)("Unknown offset %d\n", offset
);
983 /***********************************************************************
984 * SetProcessDword (KERNEL.484)
985 * 'Of course you cannot directly access Windows internal structures'
987 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
989 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
991 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
993 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
999 case GPD_APP_COMPAT_FLAGS
:
1000 case GPD_LOAD_DONE_EVENT
:
1001 case GPD_HINSTANCE16
:
1002 case GPD_WINDOWS_VERSION
:
1005 case GPD_STARTF_SHELLDATA
:
1006 case GPD_STARTF_HOTKEY
:
1007 case GPD_STARTF_SHOWWINDOW
:
1008 case GPD_STARTF_SIZE
:
1009 case GPD_STARTF_POSITION
:
1010 case GPD_STARTF_FLAGS
:
1013 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
1017 current_process
.process_dword
= value
;
1021 ERR_(win32
)("Unknown offset %d\n", offset
);
1027 /*********************************************************************
1028 * OpenProcess (KERNEL32.@)
1030 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1033 SERVER_START_REQ( open_process
)
1035 req
->pid
= (void *)id
;
1036 req
->access
= access
;
1037 req
->inherit
= inherit
;
1038 if (!SERVER_CALL_ERR()) ret
= req
->handle
;
1044 /*********************************************************************
1045 * MapProcessHandle (KERNEL.483)
1047 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1050 SERVER_START_REQ( get_process_info
)
1052 req
->handle
= handle
;
1053 if (!SERVER_CALL_ERR()) ret
= (DWORD
)req
->pid
;
1059 /***********************************************************************
1060 * SetPriorityClass (KERNEL32.@)
1062 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1065 SERVER_START_REQ( set_process_info
)
1067 req
->handle
= hprocess
;
1068 req
->priority
= priorityclass
;
1069 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1070 ret
= !SERVER_CALL_ERR();
1077 /***********************************************************************
1078 * GetPriorityClass (KERNEL32.@)
1080 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1083 SERVER_START_REQ( get_process_info
)
1085 req
->handle
= hprocess
;
1086 if (!SERVER_CALL_ERR()) ret
= req
->priority
;
1093 /***********************************************************************
1094 * SetProcessAffinityMask (KERNEL32.@)
1096 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1099 SERVER_START_REQ( set_process_info
)
1101 req
->handle
= hProcess
;
1102 req
->affinity
= affmask
;
1103 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1104 ret
= !SERVER_CALL_ERR();
1110 /**********************************************************************
1111 * GetProcessAffinityMask (KERNEL32.@)
1113 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1114 LPDWORD lpProcessAffinityMask
,
1115 LPDWORD lpSystemAffinityMask
)
1118 SERVER_START_REQ( get_process_info
)
1120 req
->handle
= hProcess
;
1121 if (!SERVER_CALL_ERR())
1123 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
1124 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
1133 /***********************************************************************
1134 * GetProcessVersion (KERNEL32.@)
1136 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1138 IMAGE_NT_HEADERS
*nt
;
1140 if (processid
&& processid
!= GetCurrentProcessId())
1142 FIXME("should use ReadProcessMemory\n");
1145 if ((nt
= RtlImageNtHeader( current_process
.module
)))
1146 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1147 nt
->OptionalHeader
.MinorSubsystemVersion
);
1151 /***********************************************************************
1152 * GetProcessFlags (KERNEL32.@)
1154 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1156 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1157 return current_process
.flags
;
1161 /***********************************************************************
1162 * SetProcessWorkingSetSize [KERNEL32.@]
1163 * Sets the min/max working set sizes for a specified process.
1166 * hProcess [I] Handle to the process of interest
1167 * minset [I] Specifies minimum working set size
1168 * maxset [I] Specifies maximum working set size
1172 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1175 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1176 if(( minset
== (DWORD
)-1) && (maxset
== (DWORD
)-1)) {
1177 /* Trim the working set to zero */
1178 /* Swap the process out of physical RAM */
1183 /***********************************************************************
1184 * GetProcessWorkingSetSize (KERNEL32.@)
1186 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1189 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1190 /* 32 MB working set size */
1191 if (minset
) *minset
= 32*1024*1024;
1192 if (maxset
) *maxset
= 32*1024*1024;
1196 /***********************************************************************
1197 * SetProcessShutdownParameters (KERNEL32.@)
1199 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1200 * Now tracks changes made (but does not act on these changes)
1202 static DWORD shutdown_flags
= 0;
1203 static DWORD shutdown_priority
= 0x280;
1205 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1207 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1208 shutdown_flags
= flags
;
1209 shutdown_priority
= level
;
1214 /***********************************************************************
1215 * GetProcessShutdownParameters (KERNEL32.@)
1218 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1220 *lpdwLevel
= shutdown_priority
;
1221 *lpdwFlags
= shutdown_flags
;
1226 /***********************************************************************
1227 * SetProcessPriorityBoost (KERNEL32.@)
1229 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1231 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1232 /* Say we can do it. I doubt the program will notice that we don't. */
1237 /***********************************************************************
1238 * ReadProcessMemory (KERNEL32.@)
1240 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1241 LPDWORD bytes_read
)
1243 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1244 unsigned int pos
= 0, len
, max
;
1247 if (bytes_read
) *bytes_read
= size
;
1249 /* first time, read total length to check for permissions */
1250 len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1251 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1255 SERVER_START_VAR_REQ( read_process_memory
, max
)
1257 req
->handle
= process
;
1258 req
->addr
= (char *)addr
+ pos
- offset
;
1260 if (!(res
= SERVER_CALL_ERR()))
1262 size_t result
= server_data_size( req
);
1263 if (result
> size
+ offset
) result
= size
+ offset
;
1264 memcpy( (char *)buffer
+ pos
, server_data_ptr(req
) + offset
, result
- offset
);
1265 size
-= result
- offset
;
1266 pos
+= result
- offset
;
1272 if (bytes_read
) *bytes_read
= 0;
1275 if (!size
) return TRUE
;
1276 max
= min( REQUEST_MAX_VAR_SIZE
, size
);
1277 len
= (max
+ sizeof(int) - 1) / sizeof(int);
1283 /***********************************************************************
1284 * WriteProcessMemory (KERNEL32.@)
1286 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1287 LPDWORD bytes_written
)
1289 unsigned int first_offset
, last_offset
;
1290 unsigned int pos
= 0, len
, max
, first_mask
, last_mask
;
1295 SetLastError( ERROR_INVALID_PARAMETER
);
1298 if (bytes_written
) *bytes_written
= size
;
1300 /* compute the mask for the first int */
1302 first_offset
= (unsigned int)addr
% sizeof(int);
1303 memset( &first_mask
, 0, first_offset
);
1305 /* compute the mask for the last int */
1306 last_offset
= (size
+ first_offset
) % sizeof(int);
1308 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1310 /* for the first request, use the total length */
1311 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1312 max
= min( REQUEST_MAX_VAR_SIZE
, len
* sizeof(int) );
1316 SERVER_START_VAR_REQ( write_process_memory
, max
)
1318 req
->handle
= process
;
1319 req
->addr
= (char *)addr
- first_offset
+ pos
;
1321 req
->first_mask
= (!pos
) ? first_mask
: ~0;
1322 if (size
+ first_offset
<= max
) /* last round */
1324 req
->last_mask
= last_mask
;
1325 max
= size
+ first_offset
;
1327 else req
->last_mask
= ~0;
1329 memcpy( (char *)server_data_ptr(req
) + first_offset
, (char *)buffer
+ pos
,
1330 max
- first_offset
);
1331 if (!(res
= SERVER_CALL_ERR()))
1333 pos
+= max
- first_offset
;
1334 size
-= max
- first_offset
;
1340 if (bytes_written
) *bytes_written
= 0;
1343 if (!size
) return TRUE
;
1345 len
= min( size
+ sizeof(int) - 1, REQUEST_MAX_VAR_SIZE
) / sizeof(int);
1346 max
= len
* sizeof(int);
1351 /***********************************************************************
1352 * RegisterServiceProcess (KERNEL.491)
1353 * RegisterServiceProcess (KERNEL32.@)
1355 * A service process calls this function to ensure that it continues to run
1356 * even after a user logged off.
1358 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1360 /* I don't think that Wine needs to do anything in that function */
1361 return 1; /* success */
1364 /***********************************************************************
1365 * GetExitCodeProcess [KERNEL32.@]
1367 * Gets termination status of specified process
1373 BOOL WINAPI
GetExitCodeProcess(
1374 HANDLE hProcess
, /* [in] handle to the process */
1375 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1378 SERVER_START_REQ( get_process_info
)
1380 req
->handle
= hProcess
;
1381 ret
= !SERVER_CALL_ERR();
1382 if (ret
&& lpExitCode
) *lpExitCode
= req
->exit_code
;
1389 /***********************************************************************
1390 * SetErrorMode (KERNEL32.@)
1392 UINT WINAPI
SetErrorMode( UINT mode
)
1394 UINT old
= current_process
.error_mode
;
1395 current_process
.error_mode
= mode
;
1400 /**************************************************************************
1401 * SetFileApisToOEM (KERNEL32.@)
1403 VOID WINAPI
SetFileApisToOEM(void)
1405 current_process
.flags
|= PDB32_FILE_APIS_OEM
;
1409 /**************************************************************************
1410 * SetFileApisToANSI (KERNEL32.@)
1412 VOID WINAPI
SetFileApisToANSI(void)
1414 current_process
.flags
&= ~PDB32_FILE_APIS_OEM
;
1418 /******************************************************************************
1419 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
1422 * TRUE: Set of file functions is using ANSI code page
1423 * FALSE: Set of file functions is using OEM code page
1425 BOOL WINAPI
AreFileApisANSI(void)
1427 return !(current_process
.flags
& PDB32_FILE_APIS_OEM
);
1431 /**********************************************************************
1432 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1434 * Allocates a thread local storage index
1437 * Success: TLS Index
1438 * Failure: 0xFFFFFFFF
1440 DWORD WINAPI
TlsAlloc( void )
1442 DWORD i
, mask
, ret
= 0;
1443 DWORD
*bits
= current_process
.tls_bits
;
1444 RtlAcquirePebLock();
1445 if (*bits
== 0xffffffff)
1449 if (*bits
== 0xffffffff)
1451 RtlReleasePebLock();
1452 SetLastError( ERROR_NO_MORE_ITEMS
);
1456 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
1458 RtlReleasePebLock();
1463 /**********************************************************************
1464 * TlsFree [KERNEL32.@] Releases a TLS index.
1466 * Releases a thread local storage index, making it available for reuse
1472 BOOL WINAPI
TlsFree(
1473 DWORD index
) /* [in] TLS Index to free */
1475 DWORD mask
= (1 << (index
& 31));
1476 DWORD
*bits
= current_process
.tls_bits
;
1479 SetLastError( ERROR_INVALID_PARAMETER
);
1482 if (index
>= 32) bits
++;
1483 RtlAcquirePebLock();
1484 if (!(*bits
& mask
)) /* already free? */
1486 RtlReleasePebLock();
1487 SetLastError( ERROR_INVALID_PARAMETER
);
1491 NtCurrentTeb()->tls_array
[index
] = 0;
1492 /* FIXME: should zero all other thread values */
1493 RtlReleasePebLock();
1498 /**********************************************************************
1499 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1502 * Success: Value stored in calling thread's TLS slot for index
1503 * Failure: 0 and GetLastError returns NO_ERROR
1505 LPVOID WINAPI
TlsGetValue(
1506 DWORD index
) /* [in] TLS index to retrieve value for */
1510 SetLastError( ERROR_INVALID_PARAMETER
);
1513 SetLastError( ERROR_SUCCESS
);
1514 return NtCurrentTeb()->tls_array
[index
];
1518 /**********************************************************************
1519 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1525 BOOL WINAPI
TlsSetValue(
1526 DWORD index
, /* [in] TLS index to set value for */
1527 LPVOID value
) /* [in] Value to be stored */
1531 SetLastError( ERROR_INVALID_PARAMETER
);
1534 NtCurrentTeb()->tls_array
[index
] = value
;
1539 /***********************************************************************
1540 * GetCurrentProcess (KERNEL32.@)
1542 #undef GetCurrentProcess
1543 HANDLE WINAPI
GetCurrentProcess(void)