Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Win32 processes |
| 3 | * |
| 4 | * Copyright 1996, 1998 Alexandre Julliard |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <ctype.h> |
| 26 | #include <errno.h> |
| 27 | #include <locale.h> |
| 28 | #include <signal.h> |
| 29 | #include <stdio.h> |
Alexandre Julliard | 8045ad5 | 2004-03-09 23:51:02 +0000 | [diff] [blame] | 30 | #include <time.h> |
| 31 | #ifdef HAVE_SYS_TIME_H |
| 32 | # include <sys/time.h> |
| 33 | #endif |
| 34 | #include <sys/types.h> |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 35 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 36 | #include "ntstatus.h" |
| 37 | #define WIN32_NO_STATUS |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 38 | #include "wine/winbase16.h" |
| 39 | #include "wine/winuser16.h" |
Alexandre Julliard | bb8f36a | 2004-09-09 00:26:57 +0000 | [diff] [blame] | 40 | #include "winioctl.h" |
Alexandre Julliard | bd000a0 | 2005-06-06 20:13:08 +0000 | [diff] [blame] | 41 | #include "winternl.h" |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 42 | #include "kernel_private.h" |
Alexandre Julliard | 402b79a | 2003-11-27 00:59:36 +0000 | [diff] [blame] | 43 | #include "wine/exception.h" |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 44 | #include "wine/server.h" |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 45 | #include "wine/unicode.h" |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 46 | #include "wine/debug.h" |
| 47 | |
Eric Pouech | c67bc22 | 2006-02-14 13:06:29 +0100 | [diff] [blame] | 48 | #ifdef HAVE_VALGRIND_MEMCHECK_H |
| 49 | #include <valgrind/memcheck.h> |
| 50 | #endif |
| 51 | |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 52 | WINE_DEFAULT_DEBUG_CHANNEL(process); |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 53 | WINE_DECLARE_DEBUG_CHANNEL(file); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 54 | WINE_DECLARE_DEBUG_CHANNEL(relay); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 55 | |
Alexandre Julliard | d3fb25c | 2003-11-17 20:16:38 +0000 | [diff] [blame] | 56 | typedef struct |
| 57 | { |
| 58 | LPSTR lpEnvAddress; |
| 59 | LPSTR lpCmdLine; |
| 60 | LPSTR lpCmdShow; |
| 61 | DWORD dwReserved; |
| 62 | } LOADPARMS32; |
| 63 | |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 64 | static UINT process_error_mode; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 65 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 66 | static DWORD shutdown_flags = 0; |
| 67 | static DWORD shutdown_priority = 0x280; |
| 68 | static DWORD process_dword; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 69 | |
Alexandre Julliard | e92b7ee | 2004-02-12 00:00:55 +0000 | [diff] [blame] | 70 | HMODULE kernel32_handle = 0; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 71 | |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 72 | const WCHAR *DIR_Windows = NULL; |
| 73 | const WCHAR *DIR_System = NULL; |
| 74 | |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 75 | /* Process flags */ |
| 76 | #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */ |
| 77 | #define PDB32_WIN16_PROC 0x0008 /* Win16 process */ |
| 78 | #define PDB32_DOS_PROC 0x0010 /* Dos process */ |
| 79 | #define PDB32_CONSOLE_PROC 0x0020 /* Console process */ |
| 80 | #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */ |
| 81 | #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */ |
| 82 | |
Alexandre Julliard | 55f2dac | 2003-10-28 00:31:52 +0000 | [diff] [blame] | 83 | static const WCHAR comW[] = {'.','c','o','m',0}; |
| 84 | static const WCHAR batW[] = {'.','b','a','t',0}; |
Rein Klazes | 0ed059c | 2005-03-07 19:24:43 +0000 | [diff] [blame] | 85 | static const WCHAR pifW[] = {'.','p','i','f',0}; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 86 | static const WCHAR winevdmW[] = {'w','i','n','e','v','d','m','.','e','x','e',0}; |
| 87 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 88 | extern void SHELL_LoadRegistry(void); |
Mike McCormack | 0f36d12 | 2005-06-14 11:42:34 +0000 | [diff] [blame] | 89 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 90 | |
| 91 | /*********************************************************************** |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 92 | * contains_path |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 93 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 94 | inline static int contains_path( LPCWSTR name ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 95 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 96 | return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\')); |
| 97 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 98 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 99 | |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 100 | /*********************************************************************** |
| 101 | * is_special_env_var |
| 102 | * |
| 103 | * Check if an environment variable needs to be handled specially when |
| 104 | * passed through the Unix environment (i.e. prefixed with "WINE"). |
| 105 | */ |
| 106 | inline static int is_special_env_var( const char *var ) |
| 107 | { |
Alexandre Julliard | 8a37297 | 2004-03-11 23:22:29 +0000 | [diff] [blame] | 108 | return (!strncmp( var, "PATH=", sizeof("PATH=")-1 ) || |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 109 | !strncmp( var, "HOME=", sizeof("HOME=")-1 ) || |
Alexandre Julliard | 8a37297 | 2004-03-11 23:22:29 +0000 | [diff] [blame] | 110 | !strncmp( var, "TEMP=", sizeof("TEMP=")-1 ) || |
| 111 | !strncmp( var, "TMP=", sizeof("TMP=")-1 )); |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 115 | /*************************************************************************** |
| 116 | * get_builtin_path |
| 117 | * |
| 118 | * Get the path of a builtin module when the native file does not exist. |
| 119 | */ |
| 120 | static BOOL get_builtin_path( const WCHAR *libname, const WCHAR *ext, WCHAR *filename, UINT size ) |
| 121 | { |
| 122 | WCHAR *file_part; |
Alexandre Julliard | 0d33e5e | 2004-05-13 20:21:25 +0000 | [diff] [blame] | 123 | UINT len = strlenW( DIR_System ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 124 | |
| 125 | if (contains_path( libname )) |
| 126 | { |
| 127 | if (RtlGetFullPathName_U( libname, size * sizeof(WCHAR), |
| 128 | filename, &file_part ) > size * sizeof(WCHAR)) |
| 129 | return FALSE; /* too long */ |
| 130 | |
Alexandre Julliard | 0d33e5e | 2004-05-13 20:21:25 +0000 | [diff] [blame] | 131 | if (strncmpiW( filename, DIR_System, len ) || filename[len] != '\\') |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 132 | return FALSE; |
| 133 | while (filename[len] == '\\') len++; |
Alexandre Julliard | 464cc10 | 2003-11-17 20:18:19 +0000 | [diff] [blame] | 134 | if (filename + len != file_part) return FALSE; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | { |
| 138 | if (strlenW(libname) + len + 2 >= size) return FALSE; /* too long */ |
Alexandre Julliard | 0d33e5e | 2004-05-13 20:21:25 +0000 | [diff] [blame] | 139 | memcpy( filename, DIR_System, len * sizeof(WCHAR) ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 140 | file_part = filename + len; |
| 141 | if (file_part > filename && file_part[-1] != '\\') *file_part++ = '\\'; |
| 142 | strcpyW( file_part, libname ); |
| 143 | } |
| 144 | if (ext && !strchrW( file_part, '.' )) |
| 145 | { |
| 146 | if (file_part + strlenW(file_part) + strlenW(ext) + 1 > filename + size) |
| 147 | return FALSE; /* too long */ |
| 148 | strcatW( file_part, ext ); |
| 149 | } |
| 150 | return TRUE; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | /*********************************************************************** |
| 155 | * open_builtin_exe_file |
| 156 | * |
| 157 | * Open an exe file for a builtin exe. |
| 158 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 159 | static void *open_builtin_exe_file( const WCHAR *name, char *error, int error_size, |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 160 | int test_only, int *file_exists ) |
| 161 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 162 | char exename[MAX_PATH]; |
| 163 | WCHAR *p; |
| 164 | UINT i, len; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 165 | |
Alex VillacÃs Lasso | efb290b | 2005-11-28 10:35:10 +0100 | [diff] [blame] | 166 | *file_exists = 0; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 167 | if ((p = strrchrW( name, '/' ))) name = p + 1; |
| 168 | if ((p = strrchrW( name, '\\' ))) name = p + 1; |
| 169 | |
| 170 | /* we don't want to depend on the current codepage here */ |
| 171 | len = strlenW( name ) + 1; |
| 172 | if (len >= sizeof(exename)) return NULL; |
| 173 | for (i = 0; i < len; i++) |
| 174 | { |
| 175 | if (name[i] > 127) return NULL; |
| 176 | exename[i] = (char)name[i]; |
| 177 | if (exename[i] >= 'A' && exename[i] <= 'Z') exename[i] += 'a' - 'A'; |
| 178 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 179 | return wine_dll_load_main_exe( exename, error, error_size, test_only, file_exists ); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /*********************************************************************** |
| 184 | * open_exe_file |
| 185 | * |
| 186 | * Open a specific exe file, taking load order into account. |
| 187 | * Returns the file handle or 0 for a builtin exe. |
| 188 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 189 | static HANDLE open_exe_file( const WCHAR *name ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 190 | { |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 191 | HANDLE handle; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 192 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 193 | TRACE("looking for %s\n", debugstr_w(name) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 194 | |
Mike McCormack | d80eb97 | 2004-09-15 18:02:49 +0000 | [diff] [blame] | 195 | if ((handle = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 196 | NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE) |
| 197 | { |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 198 | WCHAR buffer[MAX_PATH]; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 199 | /* file doesn't exist, check for builtin */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 200 | if (!contains_path( name )) goto error; |
| 201 | if (!get_builtin_path( name, NULL, buffer, sizeof(buffer) )) goto error; |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 202 | handle = 0; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 203 | } |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 204 | return handle; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 205 | |
| 206 | error: |
| 207 | SetLastError( ERROR_FILE_NOT_FOUND ); |
| 208 | return INVALID_HANDLE_VALUE; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /*********************************************************************** |
| 213 | * find_exe_file |
| 214 | * |
| 215 | * Open an exe file, and return the full name and file handle. |
| 216 | * Returns FALSE if file could not be found. |
| 217 | * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE. |
| 218 | * If file is a builtin exe, returns TRUE and sets handle to 0. |
| 219 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 220 | static BOOL find_exe_file( const WCHAR *name, WCHAR *buffer, int buflen, HANDLE *handle ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 221 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 222 | static const WCHAR exeW[] = {'.','e','x','e',0}; |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 223 | int file_exists; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 224 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 225 | TRACE("looking for %s\n", debugstr_w(name) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 226 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 227 | if (!SearchPathW( NULL, name, exeW, buflen, buffer, NULL ) && |
| 228 | !get_builtin_path( name, exeW, buffer, buflen )) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 229 | { |
| 230 | /* no builtin found, try native without extension in case it is a Unix app */ |
| 231 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 232 | if (SearchPathW( NULL, name, NULL, buflen, buffer, NULL )) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 233 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 234 | TRACE( "Trying native/Unix binary %s\n", debugstr_w(buffer) ); |
Mike McCormack | d80eb97 | 2004-09-15 18:02:49 +0000 | [diff] [blame] | 235 | if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 236 | NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE) |
| 237 | return TRUE; |
| 238 | } |
| 239 | return FALSE; |
| 240 | } |
| 241 | |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 242 | TRACE( "Trying native exe %s\n", debugstr_w(buffer) ); |
| 243 | if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, |
| 244 | NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE) |
| 245 | return TRUE; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 246 | |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 247 | TRACE( "Trying built-in exe %s\n", debugstr_w(buffer) ); |
| 248 | open_builtin_exe_file( buffer, NULL, 0, 1, &file_exists ); |
| 249 | if (file_exists) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 250 | { |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 251 | *handle = 0; |
| 252 | return TRUE; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 253 | } |
Alexandre Julliard | a877fdc | 2006-02-22 16:21:12 +0100 | [diff] [blame] | 254 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 255 | return FALSE; |
| 256 | } |
| 257 | |
| 258 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 259 | /*********************************************************************** |
Alexandre Julliard | a6ed49a | 2003-10-10 23:33:21 +0000 | [diff] [blame] | 260 | * build_initial_environment |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 261 | * |
| 262 | * Build the Win32 environment from the Unix environment |
| 263 | */ |
Alexandre Julliard | a6ed49a | 2003-10-10 23:33:21 +0000 | [diff] [blame] | 264 | static BOOL build_initial_environment( char **environ ) |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 265 | { |
Alexandre Julliard | f367051 | 2005-09-12 15:35:04 +0000 | [diff] [blame] | 266 | SIZE_T size = 1; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 267 | char **e; |
| 268 | WCHAR *p, *endptr; |
| 269 | void *ptr; |
| 270 | |
| 271 | /* Compute the total size of the Unix environment */ |
| 272 | for (e = environ; *e; e++) |
| 273 | { |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 274 | if (is_special_env_var( *e )) continue; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 275 | size += MultiByteToWideChar( CP_UNIXCP, 0, *e, -1, NULL, 0 ); |
| 276 | } |
| 277 | size *= sizeof(WCHAR); |
| 278 | |
| 279 | /* Now allocate the environment */ |
Robert Shearman | 2050591 | 2004-10-11 20:59:06 +0000 | [diff] [blame] | 280 | ptr = NULL; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 281 | if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size, |
| 282 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS) |
| 283 | return FALSE; |
| 284 | |
| 285 | NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr; |
| 286 | endptr = p + size / sizeof(WCHAR); |
| 287 | |
| 288 | /* And fill it with the Unix environment */ |
| 289 | for (e = environ; *e; e++) |
| 290 | { |
| 291 | char *str = *e; |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 292 | |
| 293 | /* skip Unix special variables and use the Wine variants instead */ |
Alexandre Julliard | 8a37297 | 2004-03-11 23:22:29 +0000 | [diff] [blame] | 294 | if (!strncmp( str, "WINE", 4 )) |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 295 | { |
| 296 | if (is_special_env_var( str + 4 )) str += 4; |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 297 | else if (!strncmp( str, "WINEPRELOADRESERVE=", 19 )) continue; /* skip it */ |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 298 | } |
| 299 | else if (is_special_env_var( str )) continue; /* skip it */ |
| 300 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 301 | MultiByteToWideChar( CP_UNIXCP, 0, str, -1, p, endptr - p ); |
| 302 | p += strlenW(p) + 1; |
| 303 | } |
| 304 | *p = 0; |
| 305 | return TRUE; |
| 306 | } |
| 307 | |
| 308 | |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 309 | /*********************************************************************** |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 310 | * set_registry_variables |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 311 | * |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 312 | * Set environment variables by enumerating the values of a key; |
| 313 | * helper for set_registry_environment(). |
Francois Gouget | 65f1958 | 2004-10-30 02:11:33 +0000 | [diff] [blame] | 314 | * Note that Windows happily truncates the value if it's too big. |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 315 | */ |
Mike McCormack | 5b2d5fd | 2005-06-17 13:58:33 +0000 | [diff] [blame] | 316 | static void set_registry_variables( HANDLE hkey, ULONG type ) |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 317 | { |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 318 | UNICODE_STRING env_name, env_value; |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 319 | NTSTATUS status; |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 320 | DWORD size; |
| 321 | int index; |
Francois Gouget | 65f1958 | 2004-10-30 02:11:33 +0000 | [diff] [blame] | 322 | char buffer[1024*sizeof(WCHAR) + sizeof(KEY_VALUE_FULL_INFORMATION)]; |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 323 | KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer; |
| 324 | |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 325 | for (index = 0; ; index++) |
| 326 | { |
| 327 | status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation, |
| 328 | buffer, sizeof(buffer), &size ); |
Francois Gouget | 65f1958 | 2004-10-30 02:11:33 +0000 | [diff] [blame] | 329 | if (status != STATUS_SUCCESS && status != STATUS_BUFFER_OVERFLOW) |
| 330 | break; |
| 331 | if (info->Type != type) |
| 332 | continue; |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 333 | env_name.Buffer = info->Name; |
| 334 | env_name.Length = env_name.MaximumLength = info->NameLength; |
| 335 | env_value.Buffer = (WCHAR *)(buffer + info->DataOffset); |
| 336 | env_value.Length = env_value.MaximumLength = info->DataLength; |
| 337 | if (env_value.Length && !env_value.Buffer[env_value.Length/sizeof(WCHAR)-1]) |
| 338 | env_value.Length--; /* don't count terminating null if any */ |
Francois Gouget | 65f1958 | 2004-10-30 02:11:33 +0000 | [diff] [blame] | 339 | if (info->Type == REG_EXPAND_SZ) |
| 340 | { |
| 341 | WCHAR buf_expanded[1024]; |
| 342 | UNICODE_STRING env_expanded; |
| 343 | env_expanded.Length = env_expanded.MaximumLength = sizeof(buf_expanded); |
| 344 | env_expanded.Buffer=buf_expanded; |
| 345 | status = RtlExpandEnvironmentStrings_U(NULL, &env_value, &env_expanded, NULL); |
| 346 | if (status == STATUS_SUCCESS || status == STATUS_BUFFER_OVERFLOW) |
| 347 | RtlSetEnvironmentVariable( NULL, &env_name, &env_expanded ); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | RtlSetEnvironmentVariable( NULL, &env_name, &env_value ); |
| 352 | } |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 353 | } |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
| 357 | /*********************************************************************** |
| 358 | * set_registry_environment |
| 359 | * |
| 360 | * Set the environment variables specified in the registry. |
Francois Gouget | 65f1958 | 2004-10-30 02:11:33 +0000 | [diff] [blame] | 361 | * |
| 362 | * Note: Windows handles REG_SZ and REG_EXPAND_SZ in one pass with the |
| 363 | * consequence that REG_EXPAND_SZ cannot be used reliably as it depends |
| 364 | * on the order in which the variables are processed. But on Windows it |
| 365 | * does not really matter since they only use %SystemDrive% and |
| 366 | * %SystemRoot% which are predefined. But Wine defines these in the |
| 367 | * registry, so we need two passes. |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 368 | */ |
| 369 | static void set_registry_environment(void) |
| 370 | { |
| 371 | static const WCHAR env_keyW[] = {'M','a','c','h','i','n','e','\\', |
| 372 | 'S','y','s','t','e','m','\\', |
| 373 | 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\', |
| 374 | 'C','o','n','t','r','o','l','\\', |
| 375 | 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\', |
| 376 | 'E','n','v','i','r','o','n','m','e','n','t',0}; |
| 377 | static const WCHAR envW[] = {'E','n','v','i','r','o','n','m','e','n','t',0}; |
| 378 | |
| 379 | OBJECT_ATTRIBUTES attr; |
| 380 | UNICODE_STRING nameW; |
Mike McCormack | 5b2d5fd | 2005-06-17 13:58:33 +0000 | [diff] [blame] | 381 | HANDLE hkey; |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 382 | |
| 383 | attr.Length = sizeof(attr); |
| 384 | attr.RootDirectory = 0; |
| 385 | attr.ObjectName = &nameW; |
| 386 | attr.Attributes = 0; |
| 387 | attr.SecurityDescriptor = NULL; |
| 388 | attr.SecurityQualityOfService = NULL; |
| 389 | |
| 390 | /* first the system environment variables */ |
| 391 | RtlInitUnicodeString( &nameW, env_keyW ); |
| 392 | if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) == STATUS_SUCCESS) |
| 393 | { |
Francois Gouget | 65f1958 | 2004-10-30 02:11:33 +0000 | [diff] [blame] | 394 | set_registry_variables( hkey, REG_SZ ); |
| 395 | set_registry_variables( hkey, REG_EXPAND_SZ ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 396 | NtClose( hkey ); |
| 397 | } |
| 398 | |
| 399 | /* then the ones for the current user */ |
Mike McCormack | 5b2d5fd | 2005-06-17 13:58:33 +0000 | [diff] [blame] | 400 | if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &attr.RootDirectory ) != STATUS_SUCCESS) return; |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 401 | RtlInitUnicodeString( &nameW, envW ); |
| 402 | if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) == STATUS_SUCCESS) |
| 403 | { |
Francois Gouget | 65f1958 | 2004-10-30 02:11:33 +0000 | [diff] [blame] | 404 | set_registry_variables( hkey, REG_SZ ); |
| 405 | set_registry_variables( hkey, REG_EXPAND_SZ ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 406 | NtClose( hkey ); |
| 407 | } |
| 408 | NtClose( attr.RootDirectory ); |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
| 412 | /*********************************************************************** |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 413 | * set_library_wargv |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 414 | * |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 415 | * Set the Wine library Unicode argv global variables. |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 416 | */ |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 417 | static void set_library_wargv( char **argv ) |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 418 | { |
| 419 | int argc; |
Alexandre Julliard | 905e75f | 2004-08-18 21:03:32 +0000 | [diff] [blame] | 420 | char *q; |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 421 | WCHAR *p; |
| 422 | WCHAR **wargv; |
| 423 | DWORD total = 0; |
| 424 | |
| 425 | for (argc = 0; argv[argc]; argc++) |
| 426 | total += MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, NULL, 0 ); |
| 427 | |
| 428 | wargv = RtlAllocateHeap( GetProcessHeap(), 0, |
| 429 | total * sizeof(WCHAR) + (argc + 1) * sizeof(*wargv) ); |
| 430 | p = (WCHAR *)(wargv + argc + 1); |
| 431 | for (argc = 0; argv[argc]; argc++) |
| 432 | { |
| 433 | DWORD reslen = MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, p, total ); |
| 434 | wargv[argc] = p; |
| 435 | p += reslen; |
| 436 | total -= reslen; |
| 437 | } |
| 438 | wargv[argc] = NULL; |
Alexandre Julliard | 905e75f | 2004-08-18 21:03:32 +0000 | [diff] [blame] | 439 | |
| 440 | /* convert argv back from Unicode since it has to be in the Ansi codepage not the Unix one */ |
| 441 | |
| 442 | for (argc = 0; wargv[argc]; argc++) |
| 443 | total += WideCharToMultiByte( CP_ACP, 0, wargv[argc], -1, NULL, 0, NULL, NULL ); |
| 444 | |
| 445 | argv = RtlAllocateHeap( GetProcessHeap(), 0, total + (argc + 1) * sizeof(*argv) ); |
| 446 | q = (char *)(argv + argc + 1); |
| 447 | for (argc = 0; wargv[argc]; argc++) |
| 448 | { |
| 449 | DWORD reslen = WideCharToMultiByte( CP_ACP, 0, wargv[argc], -1, q, total, NULL, NULL ); |
| 450 | argv[argc] = q; |
| 451 | q += reslen; |
| 452 | total -= reslen; |
| 453 | } |
| 454 | argv[argc] = NULL; |
| 455 | |
| 456 | __wine_main_argv = argv; |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 457 | __wine_main_wargv = wargv; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | /*********************************************************************** |
| 462 | * build_command_line |
| 463 | * |
| 464 | * Build the command line of a process from the argv array. |
| 465 | * |
| 466 | * Note that it does NOT necessarily include the file name. |
| 467 | * Sometimes we don't even have any command line options at all. |
| 468 | * |
| 469 | * We must quote and escape characters so that the argv array can be rebuilt |
| 470 | * from the command line: |
| 471 | * - spaces and tabs must be quoted |
| 472 | * 'a b' -> '"a b"' |
| 473 | * - quotes must be escaped |
| 474 | * '"' -> '\"' |
| 475 | * - if '\'s are followed by a '"', they must be doubled and followed by '\"', |
| 476 | * resulting in an odd number of '\' followed by a '"' |
| 477 | * '\"' -> '\\\"' |
| 478 | * '\\"' -> '\\\\\"' |
| 479 | * - '\'s that are not followed by a '"' can be left as is |
| 480 | * 'a\b' == 'a\b' |
| 481 | * 'a\\b' == 'a\\b' |
| 482 | */ |
| 483 | static BOOL build_command_line( WCHAR **argv ) |
| 484 | { |
| 485 | int len; |
| 486 | WCHAR **arg; |
| 487 | LPWSTR p; |
| 488 | RTL_USER_PROCESS_PARAMETERS* rupp = NtCurrentTeb()->Peb->ProcessParameters; |
| 489 | |
| 490 | if (rupp->CommandLine.Buffer) return TRUE; /* already got it from the server */ |
| 491 | |
| 492 | len = 0; |
| 493 | for (arg = argv; *arg; arg++) |
| 494 | { |
| 495 | int has_space,bcount; |
| 496 | WCHAR* a; |
| 497 | |
| 498 | has_space=0; |
| 499 | bcount=0; |
| 500 | a=*arg; |
| 501 | if( !*a ) has_space=1; |
| 502 | while (*a!='\0') { |
| 503 | if (*a=='\\') { |
| 504 | bcount++; |
| 505 | } else { |
| 506 | if (*a==' ' || *a=='\t') { |
| 507 | has_space=1; |
| 508 | } else if (*a=='"') { |
Francois Gouget | fbb3343 | 2005-03-02 13:53:50 +0000 | [diff] [blame] | 509 | /* doubling of '\' preceding a '"', |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 510 | * plus escaping of said '"' |
| 511 | */ |
| 512 | len+=2*bcount+1; |
| 513 | } |
| 514 | bcount=0; |
| 515 | } |
| 516 | a++; |
| 517 | } |
| 518 | len+=(a-*arg)+1 /* for the separating space */; |
| 519 | if (has_space) |
| 520 | len+=2; /* for the quotes */ |
| 521 | } |
| 522 | |
| 523 | if (!(rupp->CommandLine.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR)))) |
| 524 | return FALSE; |
| 525 | |
| 526 | p = rupp->CommandLine.Buffer; |
| 527 | rupp->CommandLine.Length = (len - 1) * sizeof(WCHAR); |
| 528 | rupp->CommandLine.MaximumLength = len * sizeof(WCHAR); |
| 529 | for (arg = argv; *arg; arg++) |
| 530 | { |
| 531 | int has_space,has_quote; |
| 532 | WCHAR* a; |
| 533 | |
| 534 | /* Check for quotes and spaces in this argument */ |
| 535 | has_space=has_quote=0; |
| 536 | a=*arg; |
| 537 | if( !*a ) has_space=1; |
| 538 | while (*a!='\0') { |
| 539 | if (*a==' ' || *a=='\t') { |
| 540 | has_space=1; |
| 541 | if (has_quote) |
| 542 | break; |
| 543 | } else if (*a=='"') { |
| 544 | has_quote=1; |
| 545 | if (has_space) |
| 546 | break; |
| 547 | } |
| 548 | a++; |
| 549 | } |
| 550 | |
| 551 | /* Now transfer it to the command line */ |
| 552 | if (has_space) |
| 553 | *p++='"'; |
| 554 | if (has_quote) { |
| 555 | int bcount; |
| 556 | WCHAR* a; |
| 557 | |
| 558 | bcount=0; |
| 559 | a=*arg; |
| 560 | while (*a!='\0') { |
| 561 | if (*a=='\\') { |
| 562 | *p++=*a; |
| 563 | bcount++; |
| 564 | } else { |
| 565 | if (*a=='"') { |
| 566 | int i; |
| 567 | |
Francois Gouget | fbb3343 | 2005-03-02 13:53:50 +0000 | [diff] [blame] | 568 | /* Double all the '\\' preceding this '"', plus one */ |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 569 | for (i=0;i<=bcount;i++) |
| 570 | *p++='\\'; |
| 571 | *p++='"'; |
| 572 | } else { |
| 573 | *p++=*a; |
| 574 | } |
| 575 | bcount=0; |
| 576 | } |
| 577 | a++; |
| 578 | } |
| 579 | } else { |
| 580 | WCHAR* x = *arg; |
| 581 | while ((*p=*x++)) p++; |
| 582 | } |
| 583 | if (has_space) |
| 584 | *p++='"'; |
| 585 | *p++=' '; |
| 586 | } |
| 587 | if (p > rupp->CommandLine.Buffer) |
| 588 | p--; /* remove last space */ |
| 589 | *p = '\0'; |
| 590 | |
| 591 | return TRUE; |
| 592 | } |
| 593 | |
| 594 | |
Dimitrie O. Paun | f950d97 | 2004-05-06 23:51:43 +0000 | [diff] [blame] | 595 | static void version(void) |
| 596 | { |
| 597 | MESSAGE( "%s\n", PACKAGE_STRING ); |
| 598 | ExitProcess(0); |
| 599 | } |
| 600 | |
| 601 | static void usage(void) |
| 602 | { |
| 603 | MESSAGE( "%s\n", PACKAGE_STRING ); |
| 604 | MESSAGE( "Usage: wine PROGRAM [ARGUMENTS...] Run the specified program\n" ); |
| 605 | MESSAGE( " wine --help Display this help and exit\n"); |
| 606 | MESSAGE( " wine --version Output version information and exit\n"); |
| 607 | ExitProcess(0); |
| 608 | } |
| 609 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 610 | |
| 611 | /*********************************************************************** |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 612 | * init_current_directory |
| 613 | * |
| 614 | * Initialize the current directory from the Unix cwd or the parent info. |
| 615 | */ |
| 616 | static void init_current_directory( CURDIR *cur_dir ) |
| 617 | { |
| 618 | UNICODE_STRING dir_str; |
| 619 | char *cwd; |
| 620 | int size; |
| 621 | |
| 622 | /* if we received a cur dir from the parent, try this first */ |
| 623 | |
| 624 | if (cur_dir->DosPath.Length) |
| 625 | { |
| 626 | if (RtlSetCurrentDirectory_U( &cur_dir->DosPath ) == STATUS_SUCCESS) goto done; |
| 627 | } |
| 628 | |
| 629 | /* now try to get it from the Unix cwd */ |
| 630 | |
| 631 | for (size = 256; ; size *= 2) |
| 632 | { |
| 633 | if (!(cwd = HeapAlloc( GetProcessHeap(), 0, size ))) break; |
| 634 | if (getcwd( cwd, size )) break; |
| 635 | HeapFree( GetProcessHeap(), 0, cwd ); |
| 636 | if (errno == ERANGE) continue; |
| 637 | cwd = NULL; |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | if (cwd) |
| 642 | { |
| 643 | WCHAR *dirW; |
| 644 | int lenW = MultiByteToWideChar( CP_UNIXCP, 0, cwd, -1, NULL, 0 ); |
| 645 | if ((dirW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) |
| 646 | { |
| 647 | MultiByteToWideChar( CP_UNIXCP, 0, cwd, -1, dirW, lenW ); |
| 648 | RtlInitUnicodeString( &dir_str, dirW ); |
| 649 | RtlSetCurrentDirectory_U( &dir_str ); |
| 650 | RtlFreeUnicodeString( &dir_str ); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (!cur_dir->DosPath.Length) /* still not initialized */ |
| 655 | { |
| 656 | MESSAGE("Warning: could not find DOS drive for current working directory '%s', " |
| 657 | "starting in the Windows directory.\n", cwd ? cwd : "" ); |
| 658 | RtlInitUnicodeString( &dir_str, DIR_Windows ); |
| 659 | RtlSetCurrentDirectory_U( &dir_str ); |
| 660 | } |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 661 | HeapFree( GetProcessHeap(), 0, cwd ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 662 | |
| 663 | done: |
| 664 | if (!cur_dir->Handle) chdir("/"); /* change to root directory so as not to lock cdroms */ |
| 665 | TRACE( "starting in %s %p\n", debugstr_w( cur_dir->DosPath.Buffer ), cur_dir->Handle ); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | /*********************************************************************** |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 670 | * init_windows_dirs |
| 671 | * |
| 672 | * Initialize the windows and system directories from the environment. |
| 673 | */ |
| 674 | static void init_windows_dirs(void) |
| 675 | { |
Alexandre Julliard | 9f58ee7 | 2004-07-15 22:07:21 +0000 | [diff] [blame] | 676 | extern void __wine_init_windows_dir( const WCHAR *windir, const WCHAR *sysdir ); |
| 677 | |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 678 | static const WCHAR windirW[] = {'w','i','n','d','i','r',0}; |
| 679 | static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0}; |
| 680 | static const WCHAR default_windirW[] = {'c',':','\\','w','i','n','d','o','w','s',0}; |
Alexandre Julliard | 19c1af5 | 2005-08-15 14:53:35 +0000 | [diff] [blame] | 681 | static const WCHAR default_sysdirW[] = {'\\','s','y','s','t','e','m','3','2',0}; |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 682 | |
| 683 | DWORD len; |
| 684 | WCHAR *buffer; |
| 685 | |
| 686 | if ((len = GetEnvironmentVariableW( windirW, NULL, 0 ))) |
| 687 | { |
| 688 | buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ); |
| 689 | GetEnvironmentVariableW( windirW, buffer, len ); |
| 690 | DIR_Windows = buffer; |
| 691 | } |
Alexandre Julliard | 9f58ee7 | 2004-07-15 22:07:21 +0000 | [diff] [blame] | 692 | else DIR_Windows = default_windirW; |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 693 | |
| 694 | if ((len = GetEnvironmentVariableW( winsysdirW, NULL, 0 ))) |
| 695 | { |
| 696 | buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ); |
| 697 | GetEnvironmentVariableW( winsysdirW, buffer, len ); |
| 698 | DIR_System = buffer; |
| 699 | } |
| 700 | else |
| 701 | { |
Alexandre Julliard | 9f58ee7 | 2004-07-15 22:07:21 +0000 | [diff] [blame] | 702 | len = strlenW( DIR_Windows ); |
| 703 | buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) + sizeof(default_sysdirW) ); |
| 704 | memcpy( buffer, DIR_Windows, len * sizeof(WCHAR) ); |
| 705 | memcpy( buffer + len, default_sysdirW, sizeof(default_sysdirW) ); |
| 706 | DIR_System = buffer; |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (GetFileAttributesW( DIR_Windows ) == INVALID_FILE_ATTRIBUTES) |
| 710 | MESSAGE( "Warning: the specified Windows directory %s is not accessible.\n", |
| 711 | debugstr_w(DIR_Windows) ); |
| 712 | if (GetFileAttributesW( DIR_System ) == INVALID_FILE_ATTRIBUTES) |
| 713 | MESSAGE( "Warning: the specified System directory %s is not accessible.\n", |
| 714 | debugstr_w(DIR_System) ); |
| 715 | |
| 716 | TRACE_(file)( "WindowsDir = %s\n", debugstr_w(DIR_Windows) ); |
| 717 | TRACE_(file)( "SystemDir = %s\n", debugstr_w(DIR_System) ); |
Alexandre Julliard | 9f58ee7 | 2004-07-15 22:07:21 +0000 | [diff] [blame] | 718 | |
| 719 | /* set the directories in ntdll too */ |
| 720 | __wine_init_windows_dir( DIR_Windows, DIR_System ); |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | |
| 724 | /*********************************************************************** |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 725 | * process_init |
| 726 | * |
| 727 | * Main process initialisation code |
| 728 | */ |
Alexandre Julliard | 9f58ee7 | 2004-07-15 22:07:21 +0000 | [diff] [blame] | 729 | static BOOL process_init(void) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 730 | { |
Alexandre Julliard | e92b7ee | 2004-02-12 00:00:55 +0000 | [diff] [blame] | 731 | static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2',0}; |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 732 | PEB *peb = NtCurrentTeb()->Peb; |
Alexandre Julliard | 477b3bf | 2006-02-22 09:57:19 +0100 | [diff] [blame] | 733 | RTL_USER_PROCESS_PARAMETERS *params = peb->ProcessParameters; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 734 | |
Alexandre Julliard | af54207 | 2004-01-07 04:50:11 +0000 | [diff] [blame] | 735 | PTHREAD_Init(); |
| 736 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 737 | setbuf(stdout,NULL); |
| 738 | setbuf(stderr,NULL); |
| 739 | setlocale(LC_CTYPE,""); |
| 740 | |
Alexandre Julliard | e92b7ee | 2004-02-12 00:00:55 +0000 | [diff] [blame] | 741 | kernel32_handle = GetModuleHandleW(kernel32W); |
| 742 | |
Alexandre Julliard | a3c4568 | 2003-10-14 05:32:30 +0000 | [diff] [blame] | 743 | LOCALE_Init(); |
| 744 | |
Alexandre Julliard | 477b3bf | 2006-02-22 09:57:19 +0100 | [diff] [blame] | 745 | if (!params->Environment) |
Alexandre Julliard | 52f692f | 2003-09-18 23:27:19 +0000 | [diff] [blame] | 746 | { |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 747 | /* Copy the parent environment */ |
| 748 | if (!build_initial_environment( __wine_main_environ )) return FALSE; |
Alexandre Julliard | 52f692f | 2003-09-18 23:27:19 +0000 | [diff] [blame] | 749 | |
Alexandre Julliard | 4a334ae | 2005-04-20 15:43:36 +0000 | [diff] [blame] | 750 | /* convert old configuration to new format */ |
| 751 | convert_old_config(); |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 752 | |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 753 | set_registry_environment(); |
| 754 | } |
Alexandre Julliard | 0a19a07 | 2004-05-05 22:05:18 +0000 | [diff] [blame] | 755 | |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 756 | init_windows_dirs(); |
Alexandre Julliard | 477b3bf | 2006-02-22 09:57:19 +0100 | [diff] [blame] | 757 | init_current_directory( ¶ms->CurrentDirectory ); |
| 758 | |
| 759 | /* convert value from server: |
| 760 | * + 0 => INVALID_HANDLE_VALUE |
| 761 | * + console handle needs to be mapped |
| 762 | */ |
| 763 | if (!params->hStdInput) |
| 764 | params->hStdInput = INVALID_HANDLE_VALUE; |
| 765 | else if (VerifyConsoleIoHandle(console_handle_map(params->hStdInput))) |
| 766 | params->hStdInput = console_handle_map(params->hStdInput); |
| 767 | |
| 768 | if (!params->hStdOutput) |
| 769 | params->hStdOutput = INVALID_HANDLE_VALUE; |
| 770 | else if (VerifyConsoleIoHandle(console_handle_map(params->hStdOutput))) |
| 771 | params->hStdOutput = console_handle_map(params->hStdOutput); |
| 772 | |
| 773 | if (!params->hStdError) |
| 774 | params->hStdError = INVALID_HANDLE_VALUE; |
| 775 | else if (VerifyConsoleIoHandle(console_handle_map(params->hStdError))) |
| 776 | params->hStdError = console_handle_map(params->hStdError); |
Alexandre Julliard | ad54691 | 2004-05-14 21:43:18 +0000 | [diff] [blame] | 777 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 778 | return TRUE; |
| 779 | } |
| 780 | |
| 781 | |
| 782 | /*********************************************************************** |
Alexandre Julliard | 8a8a94a | 2005-12-12 17:20:29 +0100 | [diff] [blame] | 783 | * init_stack |
| 784 | * |
| 785 | * Allocate the stack of new process. |
| 786 | */ |
| 787 | static void *init_stack(void) |
| 788 | { |
| 789 | void *base; |
| 790 | SIZE_T stack_size, page_size = getpagesize(); |
| 791 | IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress ); |
| 792 | |
| 793 | stack_size = max( nt->OptionalHeader.SizeOfStackReserve, nt->OptionalHeader.SizeOfStackCommit ); |
Alexandre Julliard | f3dad37 | 2005-12-12 17:28:32 +0100 | [diff] [blame] | 794 | stack_size += page_size; /* for the guard page */ |
| 795 | stack_size = (stack_size + 0xffff) & ~0xffff; /* round to 64K boundary */ |
Alexandre Julliard | 8a8a94a | 2005-12-12 17:20:29 +0100 | [diff] [blame] | 796 | if (stack_size < 1024 * 1024) stack_size = 1024 * 1024; /* Xlib needs a large stack */ |
| 797 | |
| 798 | if (!(base = VirtualAlloc( NULL, stack_size, MEM_COMMIT, PAGE_READWRITE ))) |
| 799 | { |
| 800 | ERR( "failed to allocate main process stack\n" ); |
| 801 | ExitProcess( 1 ); |
| 802 | } |
| 803 | |
| 804 | /* note: limit is lower than base since the stack grows down */ |
| 805 | NtCurrentTeb()->DeallocationStack = base; |
| 806 | NtCurrentTeb()->Tib.StackBase = (char *)base + stack_size; |
Alexandre Julliard | f3dad37 | 2005-12-12 17:28:32 +0100 | [diff] [blame] | 807 | NtCurrentTeb()->Tib.StackLimit = (char *)base + page_size; |
Alexandre Julliard | 8a8a94a | 2005-12-12 17:20:29 +0100 | [diff] [blame] | 808 | |
Eric Pouech | c67bc22 | 2006-02-14 13:06:29 +0100 | [diff] [blame] | 809 | #ifdef VALGRIND_STACK_REGISTER |
| 810 | /* no need to de-register the stack as it's the one of the main thread */ |
| 811 | VALGRIND_STACK_REGISTER(NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase); |
| 812 | #endif |
| 813 | |
Alexandre Julliard | 8a8a94a | 2005-12-12 17:20:29 +0100 | [diff] [blame] | 814 | /* setup guard page */ |
Alexandre Julliard | efffa66 | 2005-12-17 18:47:03 +0100 | [diff] [blame] | 815 | VirtualProtect( base, page_size, PAGE_NOACCESS, NULL ); |
Alexandre Julliard | 8a8a94a | 2005-12-12 17:20:29 +0100 | [diff] [blame] | 816 | return NtCurrentTeb()->Tib.StackBase; |
| 817 | } |
| 818 | |
| 819 | |
| 820 | /*********************************************************************** |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 821 | * start_process |
| 822 | * |
| 823 | * Startup routine of a new process. Runs on the new process stack. |
| 824 | */ |
| 825 | static void start_process( void *arg ) |
| 826 | { |
| 827 | __TRY |
| 828 | { |
Alexandre Julliard | f9df57d | 2003-10-24 00:32:36 +0000 | [diff] [blame] | 829 | PEB *peb = NtCurrentTeb()->Peb; |
| 830 | IMAGE_NT_HEADERS *nt; |
| 831 | LPTHREAD_START_ROUTINE entry; |
| 832 | |
Alexandre Julliard | 9669ff8 | 2006-02-08 15:22:24 +0100 | [diff] [blame] | 833 | LdrInitializeThunk( 0, 0, 0, 0 ); |
Alexandre Julliard | f9df57d | 2003-10-24 00:32:36 +0000 | [diff] [blame] | 834 | |
| 835 | nt = RtlImageNtHeader( peb->ImageBaseAddress ); |
| 836 | entry = (LPTHREAD_START_ROUTINE)((char *)peb->ImageBaseAddress + |
| 837 | nt->OptionalHeader.AddressOfEntryPoint); |
| 838 | |
| 839 | if (TRACE_ON(relay)) |
| 840 | DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n", GetCurrentThreadId(), |
| 841 | debugstr_w(peb->ProcessParameters->ImagePathName.Buffer), entry ); |
| 842 | |
| 843 | SetLastError( 0 ); /* clear error code */ |
| 844 | if (peb->BeingDebugged) DbgBreakPoint(); |
| 845 | ExitProcess( entry( peb ) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 846 | } |
| 847 | __EXCEPT(UnhandledExceptionFilter) |
| 848 | { |
| 849 | TerminateThread( GetCurrentThread(), GetExceptionCode() ); |
| 850 | } |
| 851 | __ENDTRY |
| 852 | } |
| 853 | |
| 854 | |
| 855 | /*********************************************************************** |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 856 | * __wine_kernel_init |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 857 | * |
| 858 | * Wine initialisation: load and start the main exe file. |
| 859 | */ |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 860 | void __wine_kernel_init(void) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 861 | { |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 862 | static const WCHAR dotW[] = {'.',0}; |
| 863 | static const WCHAR exeW[] = {'.','e','x','e',0}; |
| 864 | |
| 865 | WCHAR *p, main_exe_name[MAX_PATH]; |
| 866 | HMODULE module; |
| 867 | DWORD type, error = 0; |
Alexandre Julliard | 20e73d7 | 2003-10-09 04:59:03 +0000 | [diff] [blame] | 868 | PEB *peb = NtCurrentTeb()->Peb; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 869 | |
| 870 | /* Initialize everything */ |
Alexandre Julliard | 9f58ee7 | 2004-07-15 22:07:21 +0000 | [diff] [blame] | 871 | if (!process_init()) exit(1); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 872 | |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 873 | __wine_main_argv++; /* remove argv[0] (wine itself) */ |
| 874 | __wine_main_argc--; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 875 | |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 876 | if (peb->ProcessParameters->ImagePathName.Buffer) |
Alexandre Julliard | c30cefb | 2003-09-30 01:04:19 +0000 | [diff] [blame] | 877 | { |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 878 | strcpyW( main_exe_name, peb->ProcessParameters->ImagePathName.Buffer ); |
| 879 | } |
| 880 | else |
| 881 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 882 | WCHAR exe_nameW[MAX_PATH]; |
| 883 | |
Dimitrie O. Paun | f950d97 | 2004-05-06 23:51:43 +0000 | [diff] [blame] | 884 | if (!__wine_main_argv[0]) usage(); |
| 885 | if (__wine_main_argc == 1) |
| 886 | { |
| 887 | if (strcmp(__wine_main_argv[0], "--help") == 0) usage(); |
| 888 | if (strcmp(__wine_main_argv[0], "--version") == 0) version(); |
| 889 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 890 | |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 891 | MultiByteToWideChar( CP_UNIXCP, 0, __wine_main_argv[0], -1, exe_nameW, MAX_PATH ); |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 892 | if (!SearchPathW( NULL, exe_nameW, exeW, MAX_PATH, main_exe_name, NULL ) && |
| 893 | !get_builtin_path( exe_nameW, exeW, main_exe_name, MAX_PATH )) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 894 | { |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 895 | MESSAGE( "wine: cannot find '%s'\n", __wine_main_argv[0] ); |
Alexandre Julliard | 477b3bf | 2006-02-22 09:57:19 +0100 | [diff] [blame] | 896 | ExitProcess( GetLastError() ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 897 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 900 | /* if there's no extension, append a dot to prevent LoadLibrary from appending .dll */ |
| 901 | p = strrchrW( main_exe_name, '.' ); |
| 902 | if (!p || strchrW( p, '/' ) || strchrW( p, '\\' )) strcatW( main_exe_name, dotW ); |
| 903 | |
Alexandre Julliard | 477b3bf | 2006-02-22 09:57:19 +0100 | [diff] [blame] | 904 | TRACE( "starting process name=%s argv[0]=%s\n", |
| 905 | debugstr_w(main_exe_name), debugstr_a(__wine_main_argv[0]) ); |
Alexandre Julliard | c30cefb | 2003-09-30 01:04:19 +0000 | [diff] [blame] | 906 | |
Alexandre Julliard | af16aac | 2004-03-25 23:41:04 +0000 | [diff] [blame] | 907 | RtlInitUnicodeString( &NtCurrentTeb()->Peb->ProcessParameters->DllPath, |
Alexandre Julliard | 3229e29 | 2006-02-22 17:02:25 +0100 | [diff] [blame] | 908 | MODULE_get_dll_load_path(main_exe_name) ); |
Alexandre Julliard | 4137aec | 2003-10-02 04:58:49 +0000 | [diff] [blame] | 909 | |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 910 | if (!(module = LoadLibraryExW( main_exe_name, 0, DONT_RESOLVE_DLL_REFERENCES ))) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 911 | { |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 912 | error = GetLastError(); |
| 913 | /* check for a DOS binary and start winevdm if needed */ |
| 914 | if (error == ERROR_BAD_EXE_FORMAT && GetBinaryTypeW( main_exe_name, &type )) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 915 | { |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 916 | if (type == SCS_WOW_BINARY || type == SCS_DOS_BINARY || |
| 917 | type == SCS_OS216_BINARY || type == SCS_PIF_BINARY) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 918 | { |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 919 | __wine_main_argv--; |
| 920 | __wine_main_argc++; |
| 921 | __wine_main_argv[0] = "winevdm.exe"; |
| 922 | module = LoadLibraryExW( winevdmW, 0, DONT_RESOLVE_DLL_REFERENCES ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 923 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 927 | if (!module) |
| 928 | { |
| 929 | char msg[1024]; |
| 930 | FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, msg, sizeof(msg), NULL ); |
| 931 | MESSAGE( "wine: could not load %s: %s", debugstr_w(main_exe_name), msg ); |
Alexandre Julliard | 477b3bf | 2006-02-22 09:57:19 +0100 | [diff] [blame] | 932 | ExitProcess( error ); |
Alexandre Julliard | 2df3ad6 | 2006-02-21 19:48:18 +0100 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | peb->ImageBaseAddress = module; |
Alexandre Julliard | 9669ff8 | 2006-02-08 15:22:24 +0100 | [diff] [blame] | 936 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 937 | /* build command line */ |
Alexandre Julliard | 410e6b7 | 2003-10-10 00:12:17 +0000 | [diff] [blame] | 938 | set_library_wargv( __wine_main_argv ); |
Alexandre Julliard | 0de2fde | 2003-10-08 22:38:52 +0000 | [diff] [blame] | 939 | if (!build_command_line( __wine_main_wargv )) goto error; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 940 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 941 | /* switch to the new stack */ |
Alexandre Julliard | 8a8a94a | 2005-12-12 17:20:29 +0100 | [diff] [blame] | 942 | wine_switch_to_stack( start_process, NULL, init_stack() ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 943 | |
| 944 | error: |
| 945 | ExitProcess( GetLastError() ); |
| 946 | } |
| 947 | |
| 948 | |
| 949 | /*********************************************************************** |
| 950 | * build_argv |
| 951 | * |
| 952 | * Build an argv array from a command-line. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 953 | * 'reserved' is the number of args to reserve before the first one. |
| 954 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 955 | static char **build_argv( const WCHAR *cmdlineW, int reserved ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 956 | { |
| 957 | int argc; |
| 958 | char** argv; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 959 | char *arg,*s,*d,*cmdline; |
| 960 | int in_quotes,bcount,len; |
| 961 | |
| 962 | len = WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, NULL, 0, NULL, NULL ); |
| 963 | if (!(cmdline = malloc(len))) return NULL; |
| 964 | WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, cmdline, len, NULL, NULL ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 965 | |
| 966 | argc=reserved+1; |
| 967 | bcount=0; |
| 968 | in_quotes=0; |
| 969 | s=cmdline; |
| 970 | while (1) { |
| 971 | if (*s=='\0' || ((*s==' ' || *s=='\t') && !in_quotes)) { |
| 972 | /* space */ |
| 973 | argc++; |
| 974 | /* skip the remaining spaces */ |
| 975 | while (*s==' ' || *s=='\t') { |
| 976 | s++; |
| 977 | } |
| 978 | if (*s=='\0') |
| 979 | break; |
| 980 | bcount=0; |
| 981 | continue; |
| 982 | } else if (*s=='\\') { |
| 983 | /* '\', count them */ |
| 984 | bcount++; |
| 985 | } else if ((*s=='"') && ((bcount & 1)==0)) { |
| 986 | /* unescaped '"' */ |
| 987 | in_quotes=!in_quotes; |
| 988 | bcount=0; |
| 989 | } else { |
| 990 | /* a regular character */ |
| 991 | bcount=0; |
| 992 | } |
| 993 | s++; |
| 994 | } |
| 995 | argv=malloc(argc*sizeof(*argv)); |
| 996 | if (!argv) |
| 997 | return NULL; |
| 998 | |
| 999 | arg=d=s=cmdline; |
| 1000 | bcount=0; |
| 1001 | in_quotes=0; |
| 1002 | argc=reserved; |
| 1003 | while (*s) { |
| 1004 | if ((*s==' ' || *s=='\t') && !in_quotes) { |
| 1005 | /* Close the argument and copy it */ |
| 1006 | *d=0; |
| 1007 | argv[argc++]=arg; |
| 1008 | |
| 1009 | /* skip the remaining spaces */ |
| 1010 | do { |
| 1011 | s++; |
| 1012 | } while (*s==' ' || *s=='\t'); |
| 1013 | |
| 1014 | /* Start with a new argument */ |
| 1015 | arg=d=s; |
| 1016 | bcount=0; |
| 1017 | } else if (*s=='\\') { |
| 1018 | /* '\\' */ |
| 1019 | *d++=*s++; |
| 1020 | bcount++; |
| 1021 | } else if (*s=='"') { |
| 1022 | /* '"' */ |
| 1023 | if ((bcount & 1)==0) { |
Jon Griffiths | eb5bf7d | 2005-02-25 14:07:56 +0000 | [diff] [blame] | 1024 | /* Preceded by an even number of '\', this is half that |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1025 | * number of '\', plus a '"' which we discard. |
| 1026 | */ |
| 1027 | d-=bcount/2; |
| 1028 | s++; |
| 1029 | in_quotes=!in_quotes; |
| 1030 | } else { |
Jon Griffiths | eb5bf7d | 2005-02-25 14:07:56 +0000 | [diff] [blame] | 1031 | /* Preceded by an odd number of '\', this is half that |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1032 | * number of '\' followed by a '"' |
| 1033 | */ |
| 1034 | d=d-bcount/2-1; |
| 1035 | *d++='"'; |
| 1036 | s++; |
| 1037 | } |
| 1038 | bcount=0; |
| 1039 | } else { |
| 1040 | /* a regular character */ |
| 1041 | *d++=*s++; |
| 1042 | bcount=0; |
| 1043 | } |
| 1044 | } |
| 1045 | if (*arg) { |
| 1046 | *d='\0'; |
| 1047 | argv[argc++]=arg; |
| 1048 | } |
| 1049 | argv[argc]=NULL; |
| 1050 | |
| 1051 | return argv; |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | /*********************************************************************** |
Alexandre Julliard | a6ed49a | 2003-10-10 23:33:21 +0000 | [diff] [blame] | 1056 | * alloc_env_string |
| 1057 | * |
| 1058 | * Allocate an environment string; helper for build_envp |
| 1059 | */ |
| 1060 | static char *alloc_env_string( const char *name, const char *value ) |
| 1061 | { |
| 1062 | char *ret = malloc( strlen(name) + strlen(value) + 1 ); |
| 1063 | strcpy( ret, name ); |
| 1064 | strcat( ret, value ); |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
| 1068 | /*********************************************************************** |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1069 | * build_envp |
| 1070 | * |
| 1071 | * Build the environment of a new child process. |
| 1072 | */ |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1073 | static char **build_envp( const WCHAR *envW ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1074 | { |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1075 | const WCHAR *end; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1076 | char **envp; |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1077 | char *env, *p; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1078 | int count = 0, length; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1079 | |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1080 | for (end = envW; *end; count++) end += strlenW(end) + 1; |
| 1081 | end++; |
| 1082 | length = WideCharToMultiByte( CP_UNIXCP, 0, envW, end - envW, NULL, 0, NULL, NULL ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1083 | if (!(env = malloc( length ))) return NULL; |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1084 | WideCharToMultiByte( CP_UNIXCP, 0, envW, end - envW, env, length, NULL, NULL ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1085 | |
Alexandre Julliard | a6ed49a | 2003-10-10 23:33:21 +0000 | [diff] [blame] | 1086 | count += 4; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1087 | |
| 1088 | if ((envp = malloc( count * sizeof(*envp) ))) |
| 1089 | { |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1090 | char **envptr = envp; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1091 | |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1092 | /* some variables must not be modified, so we get them directly from the unix env */ |
Alexandre Julliard | a6ed49a | 2003-10-10 23:33:21 +0000 | [diff] [blame] | 1093 | if ((p = getenv("PATH"))) *envptr++ = alloc_env_string( "PATH=", p ); |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 1094 | if ((p = getenv("TEMP"))) *envptr++ = alloc_env_string( "TEMP=", p ); |
| 1095 | if ((p = getenv("TMP"))) *envptr++ = alloc_env_string( "TMP=", p ); |
Alexandre Julliard | a6ed49a | 2003-10-10 23:33:21 +0000 | [diff] [blame] | 1096 | if ((p = getenv("HOME"))) *envptr++ = alloc_env_string( "HOME=", p ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1097 | /* now put the Windows environment strings */ |
| 1098 | for (p = env; *p; p += strlen(p) + 1) |
| 1099 | { |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1100 | if (*p == '=') continue; /* skip drive curdirs, this crashes some unix apps */ |
| 1101 | if (!strncmp( p, "WINEPRELOADRESERVE=", sizeof("WINEPRELOADRESERVE=")-1 )) continue; |
Alexandre Julliard | a98a37c | 2004-03-11 00:49:01 +0000 | [diff] [blame] | 1102 | if (is_special_env_var( p )) /* prefix it with "WINE" */ |
| 1103 | *envptr++ = alloc_env_string( "WINE", p ); |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1104 | else |
| 1105 | *envptr++ = p; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1106 | } |
| 1107 | *envptr = 0; |
| 1108 | } |
| 1109 | return envp; |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | /*********************************************************************** |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1114 | * fork_and_exec |
| 1115 | * |
| 1116 | * Fork and exec a new Unix binary, checking for errors. |
| 1117 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1118 | static int fork_and_exec( const char *filename, const WCHAR *cmdline, |
Alexandre Julliard | a401f3c | 2006-03-06 22:22:30 +0100 | [diff] [blame] | 1119 | const WCHAR *env, const char *newdir, DWORD flags ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1120 | { |
| 1121 | int fd[2]; |
| 1122 | int pid, err; |
| 1123 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1124 | if (!env) env = GetEnvironmentStringsW(); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1125 | |
| 1126 | if (pipe(fd) == -1) |
| 1127 | { |
Alexandre Julliard | 716878c | 2004-04-16 23:32:40 +0000 | [diff] [blame] | 1128 | SetLastError( ERROR_TOO_MANY_OPEN_FILES ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1129 | return -1; |
| 1130 | } |
| 1131 | fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */ |
| 1132 | if (!(pid = fork())) /* child */ |
| 1133 | { |
| 1134 | char **argv = build_argv( cmdline, 0 ); |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1135 | char **envp = build_envp( env ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1136 | close( fd[0] ); |
| 1137 | |
Alexandre Julliard | a401f3c | 2006-03-06 22:22:30 +0100 | [diff] [blame] | 1138 | if (flags & (CREATE_NEW_PROCESS_GROUP | CREATE_NEW_CONSOLE | DETACHED_PROCESS)) setsid(); |
| 1139 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1140 | /* Reset signals that we previously set to SIG_IGN */ |
| 1141 | signal( SIGPIPE, SIG_DFL ); |
| 1142 | signal( SIGCHLD, SIG_DFL ); |
| 1143 | |
| 1144 | if (newdir) chdir(newdir); |
| 1145 | |
| 1146 | if (argv && envp) execve( filename, argv, envp ); |
| 1147 | err = errno; |
| 1148 | write( fd[1], &err, sizeof(err) ); |
| 1149 | _exit(1); |
| 1150 | } |
| 1151 | close( fd[1] ); |
| 1152 | if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */ |
| 1153 | { |
| 1154 | errno = err; |
| 1155 | pid = -1; |
| 1156 | } |
| 1157 | if (pid == -1) FILE_SetDosError(); |
| 1158 | close( fd[0] ); |
| 1159 | return pid; |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | /*********************************************************************** |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1164 | * create_user_params |
| 1165 | */ |
| 1166 | static RTL_USER_PROCESS_PARAMETERS *create_user_params( LPCWSTR filename, LPCWSTR cmdline, |
Alexandre Julliard | 645c0cc | 2006-02-05 12:20:40 +0100 | [diff] [blame] | 1167 | LPCWSTR cur_dir, LPWSTR env, DWORD flags, |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1168 | const STARTUPINFOW *startup ) |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1169 | { |
| 1170 | RTL_USER_PROCESS_PARAMETERS *params; |
Eric Pouech | 0bfb1cb | 2004-09-03 01:05:30 +0000 | [diff] [blame] | 1171 | UNICODE_STRING image_str, cmdline_str, curdir_str, desktop, title, runtime; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1172 | NTSTATUS status; |
| 1173 | WCHAR buffer[MAX_PATH]; |
| 1174 | |
Mike McCormack | edf9eb6 | 2004-02-05 01:23:42 +0000 | [diff] [blame] | 1175 | if(!GetLongPathNameW( filename, buffer, MAX_PATH )) |
| 1176 | lstrcpynW( buffer, filename, MAX_PATH ); |
| 1177 | if(!GetFullPathNameW( buffer, MAX_PATH, buffer, NULL )) |
| 1178 | lstrcpynW( buffer, filename, MAX_PATH ); |
| 1179 | RtlInitUnicodeString( &image_str, buffer ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1180 | |
| 1181 | RtlInitUnicodeString( &cmdline_str, cmdline ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1182 | if (cur_dir) RtlInitUnicodeString( &curdir_str, cur_dir ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1183 | if (startup->lpDesktop) RtlInitUnicodeString( &desktop, startup->lpDesktop ); |
| 1184 | if (startup->lpTitle) RtlInitUnicodeString( &title, startup->lpTitle ); |
Eric Pouech | 0bfb1cb | 2004-09-03 01:05:30 +0000 | [diff] [blame] | 1185 | if (startup->lpReserved2 && startup->cbReserved2) |
| 1186 | { |
Eric Pouech | 4bb5d3a | 2004-10-25 21:47:23 +0000 | [diff] [blame] | 1187 | runtime.Length = 0; |
| 1188 | runtime.MaximumLength = startup->cbReserved2; |
Eric Pouech | 0bfb1cb | 2004-09-03 01:05:30 +0000 | [diff] [blame] | 1189 | runtime.Buffer = (WCHAR*)startup->lpReserved2; |
| 1190 | } |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1191 | |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1192 | status = RtlCreateProcessParameters( ¶ms, &image_str, NULL, |
| 1193 | cur_dir ? &curdir_str : NULL, |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1194 | &cmdline_str, env, |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1195 | startup->lpTitle ? &title : NULL, |
| 1196 | startup->lpDesktop ? &desktop : NULL, |
Eric Pouech | 0bfb1cb | 2004-09-03 01:05:30 +0000 | [diff] [blame] | 1197 | NULL, |
| 1198 | (startup->lpReserved2 && startup->cbReserved2) ? &runtime : NULL ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1199 | if (status != STATUS_SUCCESS) |
| 1200 | { |
| 1201 | SetLastError( RtlNtStatusToDosError(status) ); |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | |
Alexandre Julliard | 645c0cc | 2006-02-05 12:20:40 +0100 | [diff] [blame] | 1205 | if (flags & CREATE_NEW_PROCESS_GROUP) params->ConsoleFlags = 1; |
Alexandre Julliard | 626669e | 2006-02-05 12:24:43 +0100 | [diff] [blame] | 1206 | if (flags & CREATE_NEW_CONSOLE) params->ConsoleHandle = (HANDLE)1; /* FIXME: cf. kernel_main.c */ |
Alexandre Julliard | 645c0cc | 2006-02-05 12:20:40 +0100 | [diff] [blame] | 1207 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1208 | params->hStdInput = startup->hStdInput; |
| 1209 | params->hStdOutput = startup->hStdOutput; |
| 1210 | params->hStdError = startup->hStdError; |
| 1211 | params->dwX = startup->dwX; |
| 1212 | params->dwY = startup->dwY; |
| 1213 | params->dwXSize = startup->dwXSize; |
| 1214 | params->dwYSize = startup->dwYSize; |
| 1215 | params->dwXCountChars = startup->dwXCountChars; |
| 1216 | params->dwYCountChars = startup->dwYCountChars; |
| 1217 | params->dwFillAttribute = startup->dwFillAttribute; |
| 1218 | params->dwFlags = startup->dwFlags; |
| 1219 | params->wShowWindow = startup->wShowWindow; |
| 1220 | return params; |
| 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | /*********************************************************************** |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1225 | * create_process |
| 1226 | * |
| 1227 | * Create a new process. If hFile is a valid handle we have an exe |
| 1228 | * file, otherwise it is a Winelib app. |
| 1229 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1230 | static BOOL create_process( HANDLE hFile, LPCWSTR filename, LPWSTR cmd_line, LPWSTR env, |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1231 | LPCWSTR cur_dir, LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa, |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1232 | BOOL inherit, DWORD flags, LPSTARTUPINFOW startup, |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1233 | LPPROCESS_INFORMATION info, LPCSTR unixdir, |
| 1234 | void *res_start, void *res_end ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1235 | { |
| 1236 | BOOL ret, success = FALSE; |
| 1237 | HANDLE process_info; |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1238 | WCHAR *env_end; |
Alexandre Julliard | ce1196a | 2005-10-04 18:09:51 +0000 | [diff] [blame] | 1239 | char *winedebug = NULL; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1240 | RTL_USER_PROCESS_PARAMETERS *params; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1241 | int startfd[2]; |
| 1242 | int execfd[2]; |
| 1243 | pid_t pid; |
| 1244 | int err; |
| 1245 | char dummy = 0; |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1246 | char preloader_reserve[64]; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1247 | |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1248 | if (!env) RtlAcquirePebLock(); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1249 | |
Alexandre Julliard | 645c0cc | 2006-02-05 12:20:40 +0100 | [diff] [blame] | 1250 | if (!(params = create_user_params( filename, cmd_line, cur_dir, env, flags, startup ))) |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1251 | { |
| 1252 | if (!env) RtlReleasePebLock(); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1253 | return FALSE; |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1254 | } |
| 1255 | env_end = params->Environment; |
Alexandre Julliard | ce1196a | 2005-10-04 18:09:51 +0000 | [diff] [blame] | 1256 | while (*env_end) |
| 1257 | { |
| 1258 | static const WCHAR WINEDEBUG[] = {'W','I','N','E','D','E','B','U','G','=',0}; |
| 1259 | if (!winedebug && !strncmpW( env_end, WINEDEBUG, sizeof(WINEDEBUG)/sizeof(WCHAR) - 1 )) |
| 1260 | { |
| 1261 | DWORD len = WideCharToMultiByte( CP_UNIXCP, 0, env_end, -1, NULL, 0, NULL, NULL ); |
| 1262 | if ((winedebug = HeapAlloc( GetProcessHeap(), 0, len ))) |
| 1263 | WideCharToMultiByte( CP_UNIXCP, 0, env_end, -1, winedebug, len, NULL, NULL ); |
| 1264 | } |
| 1265 | env_end += strlenW(env_end) + 1; |
| 1266 | } |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1267 | env_end++; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1268 | |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1269 | sprintf( preloader_reserve, "WINEPRELOADRESERVE=%lx-%lx%c", |
| 1270 | (unsigned long)res_start, (unsigned long)res_end, 0 ); |
| 1271 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1272 | /* create the synchronization pipes */ |
| 1273 | |
| 1274 | if (pipe( startfd ) == -1) |
| 1275 | { |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1276 | if (!env) RtlReleasePebLock(); |
Alexandre Julliard | ce1196a | 2005-10-04 18:09:51 +0000 | [diff] [blame] | 1277 | HeapFree( GetProcessHeap(), 0, winedebug ); |
Alexandre Julliard | 716878c | 2004-04-16 23:32:40 +0000 | [diff] [blame] | 1278 | SetLastError( ERROR_TOO_MANY_OPEN_FILES ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1279 | RtlDestroyProcessParameters( params ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1280 | return FALSE; |
| 1281 | } |
| 1282 | if (pipe( execfd ) == -1) |
| 1283 | { |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1284 | if (!env) RtlReleasePebLock(); |
Alexandre Julliard | ce1196a | 2005-10-04 18:09:51 +0000 | [diff] [blame] | 1285 | HeapFree( GetProcessHeap(), 0, winedebug ); |
Alexandre Julliard | 716878c | 2004-04-16 23:32:40 +0000 | [diff] [blame] | 1286 | SetLastError( ERROR_TOO_MANY_OPEN_FILES ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1287 | close( startfd[0] ); |
| 1288 | close( startfd[1] ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1289 | RtlDestroyProcessParameters( params ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1290 | return FALSE; |
| 1291 | } |
| 1292 | fcntl( execfd[1], F_SETFD, 1 ); /* set close on exec */ |
| 1293 | |
| 1294 | /* create the child process */ |
| 1295 | |
| 1296 | if (!(pid = fork())) /* child */ |
| 1297 | { |
| 1298 | char **argv = build_argv( cmd_line, 1 ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1299 | |
| 1300 | close( startfd[1] ); |
| 1301 | close( execfd[0] ); |
| 1302 | |
| 1303 | /* wait for parent to tell us to start */ |
| 1304 | if (read( startfd[0], &dummy, 1 ) != 1) _exit(1); |
| 1305 | |
| 1306 | close( startfd[0] ); |
Alexandre Julliard | a401f3c | 2006-03-06 22:22:30 +0100 | [diff] [blame] | 1307 | if (flags & (CREATE_NEW_PROCESS_GROUP | CREATE_NEW_CONSOLE | DETACHED_PROCESS)) setsid(); |
| 1308 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1309 | /* Reset signals that we previously set to SIG_IGN */ |
| 1310 | signal( SIGPIPE, SIG_DFL ); |
| 1311 | signal( SIGCHLD, SIG_DFL ); |
| 1312 | |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1313 | putenv( preloader_reserve ); |
Alexandre Julliard | ce1196a | 2005-10-04 18:09:51 +0000 | [diff] [blame] | 1314 | if (winedebug) putenv( winedebug ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1315 | if (unixdir) chdir(unixdir); |
| 1316 | |
Alexandre Julliard | 9dffd13 | 2006-02-16 20:26:55 +0100 | [diff] [blame] | 1317 | if (argv) wine_exec_wine_binary( NULL, argv, getenv("WINELOADER") ); |
| 1318 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1319 | err = errno; |
| 1320 | write( execfd[1], &err, sizeof(err) ); |
| 1321 | _exit(1); |
| 1322 | } |
| 1323 | |
| 1324 | /* this is the parent */ |
| 1325 | |
| 1326 | close( startfd[0] ); |
| 1327 | close( execfd[1] ); |
Alexandre Julliard | ce1196a | 2005-10-04 18:09:51 +0000 | [diff] [blame] | 1328 | HeapFree( GetProcessHeap(), 0, winedebug ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1329 | if (pid == -1) |
| 1330 | { |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1331 | if (!env) RtlReleasePebLock(); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1332 | close( startfd[1] ); |
| 1333 | close( execfd[0] ); |
| 1334 | FILE_SetDosError(); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1335 | RtlDestroyProcessParameters( params ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1336 | return FALSE; |
| 1337 | } |
| 1338 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1339 | /* create the process on the server side */ |
| 1340 | |
| 1341 | SERVER_START_REQ( new_process ) |
| 1342 | { |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1343 | req->inherit_all = inherit; |
| 1344 | req->create_flags = flags; |
| 1345 | req->unix_pid = pid; |
| 1346 | req->exe_file = hFile; |
| 1347 | if (startup->dwFlags & STARTF_USESTDHANDLES) |
| 1348 | { |
| 1349 | req->hstdin = startup->hStdInput; |
| 1350 | req->hstdout = startup->hStdOutput; |
| 1351 | req->hstderr = startup->hStdError; |
| 1352 | } |
| 1353 | else |
| 1354 | { |
| 1355 | req->hstdin = GetStdHandle( STD_INPUT_HANDLE ); |
| 1356 | req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE ); |
| 1357 | req->hstderr = GetStdHandle( STD_ERROR_HANDLE ); |
| 1358 | } |
| 1359 | |
| 1360 | if ((flags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)) != 0) |
| 1361 | { |
| 1362 | /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */ |
| 1363 | if (is_console_handle(req->hstdin)) req->hstdin = INVALID_HANDLE_VALUE; |
| 1364 | if (is_console_handle(req->hstdout)) req->hstdout = INVALID_HANDLE_VALUE; |
| 1365 | if (is_console_handle(req->hstderr)) req->hstderr = INVALID_HANDLE_VALUE; |
| 1366 | } |
| 1367 | else |
| 1368 | { |
| 1369 | if (is_console_handle(req->hstdin)) req->hstdin = console_handle_unmap(req->hstdin); |
| 1370 | if (is_console_handle(req->hstdout)) req->hstdout = console_handle_unmap(req->hstdout); |
| 1371 | if (is_console_handle(req->hstderr)) req->hstderr = console_handle_unmap(req->hstderr); |
| 1372 | } |
| 1373 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1374 | wine_server_add_data( req, params, params->Size ); |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1375 | wine_server_add_data( req, params->Environment, (env_end-params->Environment)*sizeof(WCHAR) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1376 | ret = !wine_server_call_err( req ); |
| 1377 | process_info = reply->info; |
| 1378 | } |
| 1379 | SERVER_END_REQ; |
| 1380 | |
Alexandre Julliard | c4ec210 | 2004-07-16 03:54:35 +0000 | [diff] [blame] | 1381 | if (!env) RtlReleasePebLock(); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1382 | RtlDestroyProcessParameters( params ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1383 | if (!ret) |
| 1384 | { |
| 1385 | close( startfd[1] ); |
| 1386 | close( execfd[0] ); |
| 1387 | return FALSE; |
| 1388 | } |
| 1389 | |
| 1390 | /* tell child to start and wait for it to exec */ |
| 1391 | |
| 1392 | write( startfd[1], &dummy, 1 ); |
| 1393 | close( startfd[1] ); |
| 1394 | |
| 1395 | if (read( execfd[0], &err, sizeof(err) ) > 0) /* exec failed */ |
| 1396 | { |
| 1397 | errno = err; |
| 1398 | FILE_SetDosError(); |
| 1399 | close( execfd[0] ); |
| 1400 | CloseHandle( process_info ); |
| 1401 | return FALSE; |
| 1402 | } |
Francois Gouget | 52bf6c2 | 2004-01-20 02:11:06 +0000 | [diff] [blame] | 1403 | close( execfd[0] ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1404 | |
| 1405 | /* wait for the new process info to be ready */ |
| 1406 | |
| 1407 | WaitForSingleObject( process_info, INFINITE ); |
| 1408 | SERVER_START_REQ( get_new_process_info ) |
| 1409 | { |
Alexandre Julliard | f2d7dd6 | 2005-12-09 12:13:11 +0100 | [diff] [blame] | 1410 | req->info = process_info; |
| 1411 | req->process_access = PROCESS_ALL_ACCESS; |
| 1412 | req->process_attr = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle) ? OBJ_INHERIT : 0; |
| 1413 | req->thread_access = THREAD_ALL_ACCESS; |
| 1414 | req->thread_attr = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle) ? OBJ_INHERIT : 0; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1415 | if ((ret = !wine_server_call_err( req ))) |
| 1416 | { |
| 1417 | info->dwProcessId = (DWORD)reply->pid; |
| 1418 | info->dwThreadId = (DWORD)reply->tid; |
| 1419 | info->hProcess = reply->phandle; |
| 1420 | info->hThread = reply->thandle; |
| 1421 | success = reply->success; |
| 1422 | } |
| 1423 | } |
| 1424 | SERVER_END_REQ; |
| 1425 | |
| 1426 | if (ret && !success) /* new process failed to start */ |
| 1427 | { |
| 1428 | DWORD exitcode; |
| 1429 | if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode ); |
| 1430 | CloseHandle( info->hThread ); |
| 1431 | CloseHandle( info->hProcess ); |
| 1432 | ret = FALSE; |
| 1433 | } |
| 1434 | CloseHandle( process_info ); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | |
| 1438 | |
| 1439 | /*********************************************************************** |
| 1440 | * create_vdm_process |
| 1441 | * |
| 1442 | * Create a new VDM process for a 16-bit or DOS application. |
| 1443 | */ |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1444 | static BOOL create_vdm_process( LPCWSTR filename, LPWSTR cmd_line, LPWSTR env, LPCWSTR cur_dir, |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1445 | LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa, |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1446 | BOOL inherit, DWORD flags, LPSTARTUPINFOW startup, |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1447 | LPPROCESS_INFORMATION info, LPCSTR unixdir ) |
| 1448 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1449 | static const WCHAR argsW[] = {'%','s',' ','-','-','a','p','p','-','n','a','m','e',' ','"','%','s','"',' ','%','s',0}; |
| 1450 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1451 | BOOL ret; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1452 | LPWSTR new_cmd_line = HeapAlloc( GetProcessHeap(), 0, |
| 1453 | (strlenW(filename) + strlenW(cmd_line) + 30) * sizeof(WCHAR) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (!new_cmd_line) |
| 1456 | { |
| 1457 | SetLastError( ERROR_OUTOFMEMORY ); |
| 1458 | return FALSE; |
| 1459 | } |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1460 | sprintfW( new_cmd_line, argsW, winevdmW, filename, cmd_line ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1461 | ret = create_process( 0, winevdmW, new_cmd_line, env, cur_dir, psa, tsa, inherit, |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1462 | flags, startup, info, unixdir, NULL, NULL ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1463 | HeapFree( GetProcessHeap(), 0, new_cmd_line ); |
| 1464 | return ret; |
| 1465 | } |
| 1466 | |
| 1467 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1468 | /*********************************************************************** |
| 1469 | * create_cmd_process |
| 1470 | * |
| 1471 | * Create a new cmd shell process for a .BAT file. |
| 1472 | */ |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1473 | static BOOL create_cmd_process( LPCWSTR filename, LPWSTR cmd_line, LPVOID env, LPCWSTR cur_dir, |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1474 | LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa, |
| 1475 | BOOL inherit, DWORD flags, LPSTARTUPINFOW startup, |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1476 | LPPROCESS_INFORMATION info ) |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1477 | |
| 1478 | { |
| 1479 | static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0}; |
| 1480 | static const WCHAR slashcW[] = {' ','/','c',' ',0}; |
| 1481 | WCHAR comspec[MAX_PATH]; |
| 1482 | WCHAR *newcmdline; |
| 1483 | BOOL ret; |
| 1484 | |
| 1485 | if (!GetEnvironmentVariableW( comspecW, comspec, sizeof(comspec)/sizeof(WCHAR) )) |
| 1486 | return FALSE; |
| 1487 | if (!(newcmdline = HeapAlloc( GetProcessHeap(), 0, |
| 1488 | (strlenW(comspec) + 4 + strlenW(cmd_line) + 1) * sizeof(WCHAR)))) |
| 1489 | return FALSE; |
| 1490 | |
| 1491 | strcpyW( newcmdline, comspec ); |
| 1492 | strcatW( newcmdline, slashcW ); |
| 1493 | strcatW( newcmdline, cmd_line ); |
| 1494 | ret = CreateProcessW( comspec, newcmdline, psa, tsa, inherit, |
| 1495 | flags, env, cur_dir, startup, info ); |
| 1496 | HeapFree( GetProcessHeap(), 0, newcmdline ); |
| 1497 | return ret; |
| 1498 | } |
| 1499 | |
| 1500 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1501 | /************************************************************************* |
| 1502 | * get_file_name |
| 1503 | * |
| 1504 | * Helper for CreateProcess: retrieve the file name to load from the |
| 1505 | * app name and command line. Store the file name in buffer, and |
| 1506 | * return a possibly modified command line. |
| 1507 | * Also returns a handle to the opened file if it's a Windows binary. |
| 1508 | */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1509 | static LPWSTR get_file_name( LPCWSTR appname, LPWSTR cmdline, LPWSTR buffer, |
| 1510 | int buflen, HANDLE *handle ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1511 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1512 | static const WCHAR quotesW[] = {'"','%','s','"',0}; |
| 1513 | |
| 1514 | WCHAR *name, *pos, *ret = NULL; |
| 1515 | const WCHAR *p; |
Alexandre Julliard | 68df918 | 2005-12-19 21:25:23 +0100 | [diff] [blame] | 1516 | BOOL got_space; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1517 | |
| 1518 | /* if we have an app name, everything is easy */ |
| 1519 | |
| 1520 | if (appname) |
| 1521 | { |
| 1522 | /* use the unmodified app name as file name */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1523 | lstrcpynW( buffer, appname, buflen ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1524 | *handle = open_exe_file( buffer ); |
| 1525 | if (!(ret = cmdline) || !cmdline[0]) |
| 1526 | { |
| 1527 | /* no command-line, create one */ |
Mike McCormack | 6a83224 | 2003-10-30 23:06:10 +0000 | [diff] [blame] | 1528 | if ((ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(appname) + 3) * sizeof(WCHAR) ))) |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1529 | sprintfW( ret, quotesW, appname ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1530 | } |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | if (!cmdline) |
| 1535 | { |
| 1536 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | |
| 1540 | /* first check for a quoted file name */ |
| 1541 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1542 | if ((cmdline[0] == '"') && ((p = strchrW( cmdline + 1, '"' )))) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1543 | { |
| 1544 | int len = p - cmdline - 1; |
| 1545 | /* extract the quoted portion as file name */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1546 | if (!(name = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL; |
| 1547 | memcpy( name, cmdline + 1, len * sizeof(WCHAR) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1548 | name[len] = 0; |
| 1549 | |
| 1550 | if (find_exe_file( name, buffer, buflen, handle )) |
| 1551 | ret = cmdline; /* no change necessary */ |
| 1552 | goto done; |
| 1553 | } |
| 1554 | |
| 1555 | /* now try the command-line word by word */ |
| 1556 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1557 | if (!(name = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 1) * sizeof(WCHAR) ))) |
| 1558 | return NULL; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1559 | pos = name; |
| 1560 | p = cmdline; |
Alexandre Julliard | 68df918 | 2005-12-19 21:25:23 +0100 | [diff] [blame] | 1561 | got_space = FALSE; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1562 | |
| 1563 | while (*p) |
| 1564 | { |
Alexandre Julliard | 68df918 | 2005-12-19 21:25:23 +0100 | [diff] [blame] | 1565 | do *pos++ = *p++; while (*p && *p != ' ' && *p != '\t'); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1566 | *pos = 0; |
| 1567 | if (find_exe_file( name, buffer, buflen, handle )) |
| 1568 | { |
| 1569 | ret = cmdline; |
| 1570 | break; |
| 1571 | } |
Alexandre Julliard | 68df918 | 2005-12-19 21:25:23 +0100 | [diff] [blame] | 1572 | if (*p) got_space = TRUE; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Alexandre Julliard | 68df918 | 2005-12-19 21:25:23 +0100 | [diff] [blame] | 1575 | if (ret && got_space) /* now build a new command-line with quotes */ |
| 1576 | { |
| 1577 | if (!(ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 3) * sizeof(WCHAR) ))) |
| 1578 | goto done; |
| 1579 | sprintfW( ret, quotesW, name ); |
| 1580 | strcatW( ret, p ); |
| 1581 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1582 | |
| 1583 | done: |
| 1584 | HeapFree( GetProcessHeap(), 0, name ); |
| 1585 | return ret; |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | /********************************************************************** |
| 1590 | * CreateProcessA (KERNEL32.@) |
| 1591 | */ |
| 1592 | BOOL WINAPI CreateProcessA( LPCSTR app_name, LPSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr, |
| 1593 | LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, |
| 1594 | DWORD flags, LPVOID env, LPCSTR cur_dir, |
| 1595 | LPSTARTUPINFOA startup_info, LPPROCESS_INFORMATION info ) |
| 1596 | { |
Alexandre Julliard | 0d33e5e | 2004-05-13 20:21:25 +0000 | [diff] [blame] | 1597 | BOOL ret = FALSE; |
| 1598 | WCHAR *app_nameW = NULL, *cmd_lineW = NULL, *cur_dirW = NULL; |
| 1599 | UNICODE_STRING desktopW, titleW; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1600 | STARTUPINFOW infoW; |
| 1601 | |
Alexandre Julliard | 0d33e5e | 2004-05-13 20:21:25 +0000 | [diff] [blame] | 1602 | desktopW.Buffer = NULL; |
| 1603 | titleW.Buffer = NULL; |
| 1604 | if (app_name && !(app_nameW = FILE_name_AtoW( app_name, TRUE ))) goto done; |
| 1605 | if (cmd_line && !(cmd_lineW = FILE_name_AtoW( cmd_line, TRUE ))) goto done; |
| 1606 | if (cur_dir && !(cur_dirW = FILE_name_AtoW( cur_dir, TRUE ))) goto done; |
| 1607 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1608 | if (startup_info->lpDesktop) RtlCreateUnicodeStringFromAsciiz( &desktopW, startup_info->lpDesktop ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1609 | if (startup_info->lpTitle) RtlCreateUnicodeStringFromAsciiz( &titleW, startup_info->lpTitle ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1610 | |
| 1611 | memcpy( &infoW, startup_info, sizeof(infoW) ); |
| 1612 | infoW.lpDesktop = desktopW.Buffer; |
| 1613 | infoW.lpTitle = titleW.Buffer; |
| 1614 | |
| 1615 | if (startup_info->lpReserved) |
| 1616 | FIXME("StartupInfo.lpReserved is used, please report (%s)\n", |
| 1617 | debugstr_a(startup_info->lpReserved)); |
| 1618 | |
Alexandre Julliard | 0d33e5e | 2004-05-13 20:21:25 +0000 | [diff] [blame] | 1619 | ret = CreateProcessW( app_nameW, cmd_lineW, process_attr, thread_attr, |
| 1620 | inherit, flags, env, cur_dirW, &infoW, info ); |
| 1621 | done: |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 1622 | HeapFree( GetProcessHeap(), 0, app_nameW ); |
| 1623 | HeapFree( GetProcessHeap(), 0, cmd_lineW ); |
| 1624 | HeapFree( GetProcessHeap(), 0, cur_dirW ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1625 | RtlFreeUnicodeString( &desktopW ); |
| 1626 | RtlFreeUnicodeString( &titleW ); |
| 1627 | return ret; |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | /********************************************************************** |
| 1632 | * CreateProcessW (KERNEL32.@) |
| 1633 | */ |
| 1634 | BOOL WINAPI CreateProcessW( LPCWSTR app_name, LPWSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr, |
| 1635 | LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, DWORD flags, |
| 1636 | LPVOID env, LPCWSTR cur_dir, LPSTARTUPINFOW startup_info, |
| 1637 | LPPROCESS_INFORMATION info ) |
| 1638 | { |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1639 | BOOL retv = FALSE; |
| 1640 | HANDLE hFile = 0; |
Alexandre Julliard | 1cb0cf4 | 2004-04-14 19:40:53 +0000 | [diff] [blame] | 1641 | char *unixdir = NULL; |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1642 | WCHAR name[MAX_PATH]; |
| 1643 | WCHAR *tidy_cmdline, *p, *envW = env; |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1644 | void *res_start, *res_end; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1645 | |
| 1646 | /* Process the AppName and/or CmdLine to get module name and path */ |
| 1647 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1648 | TRACE("app %s cmdline %s\n", debugstr_w(app_name), debugstr_w(cmd_line) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1649 | |
Marcus Meissner | 4a76661 | 2005-10-10 10:28:48 +0000 | [diff] [blame] | 1650 | if (!(tidy_cmdline = get_file_name( app_name, cmd_line, name, sizeof(name)/sizeof(WCHAR), &hFile ))) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1651 | return FALSE; |
| 1652 | if (hFile == INVALID_HANDLE_VALUE) goto done; |
| 1653 | |
| 1654 | /* Warn if unsupported features are used */ |
| 1655 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1656 | if (flags & (IDLE_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | REALTIME_PRIORITY_CLASS | |
| 1657 | CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM | CREATE_SHARED_WOW_VDM | |
| 1658 | CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW | |
| 1659 | PROFILE_USER | PROFILE_KERNEL | PROFILE_SERVER)) |
| 1660 | WARN("(%s,...): ignoring some flags in %lx\n", debugstr_w(name), flags); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1661 | |
| 1662 | if (cur_dir) |
| 1663 | { |
Alexandre Julliard | 1cb0cf4 | 2004-04-14 19:40:53 +0000 | [diff] [blame] | 1664 | unixdir = wine_get_unix_file_name( cur_dir ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1665 | } |
| 1666 | else |
| 1667 | { |
| 1668 | WCHAR buf[MAX_PATH]; |
Alexandre Julliard | 1cb0cf4 | 2004-04-14 19:40:53 +0000 | [diff] [blame] | 1669 | if (GetCurrentDirectoryW(MAX_PATH, buf)) unixdir = wine_get_unix_file_name( buf ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1672 | if (env && !(flags & CREATE_UNICODE_ENVIRONMENT)) /* convert environment to unicode */ |
| 1673 | { |
| 1674 | char *p = env; |
| 1675 | DWORD lenW; |
| 1676 | |
| 1677 | while (*p) p += strlen(p) + 1; |
| 1678 | p++; /* final null */ |
| 1679 | lenW = MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, NULL, 0 ); |
| 1680 | envW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ); |
| 1681 | MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, envW, lenW ); |
| 1682 | flags |= CREATE_UNICODE_ENVIRONMENT; |
| 1683 | } |
| 1684 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1685 | info->hThread = info->hProcess = 0; |
| 1686 | info->dwProcessId = info->dwThreadId = 0; |
| 1687 | |
| 1688 | /* Determine executable type */ |
| 1689 | |
| 1690 | if (!hFile) /* builtin exe */ |
| 1691 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1692 | TRACE( "starting %s as Winelib app\n", debugstr_w(name) ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1693 | retv = create_process( 0, name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr, |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1694 | inherit, flags, startup_info, info, unixdir, NULL, NULL ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1695 | goto done; |
| 1696 | } |
| 1697 | |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1698 | switch( MODULE_GetBinaryType( hFile, &res_start, &res_end )) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1699 | { |
| 1700 | case BINARY_PE_EXE: |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1701 | TRACE( "starting %s as Win32 binary (%p-%p)\n", debugstr_w(name), res_start, res_end ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1702 | retv = create_process( hFile, name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr, |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1703 | inherit, flags, startup_info, info, unixdir, res_start, res_end ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1704 | break; |
Andreas Mohr | 85b0683 | 2004-09-10 21:16:02 +0000 | [diff] [blame] | 1705 | case BINARY_OS216: |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1706 | case BINARY_WIN16: |
| 1707 | case BINARY_DOS: |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1708 | TRACE( "starting %s as Win16/DOS binary\n", debugstr_w(name) ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1709 | retv = create_vdm_process( name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr, |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1710 | inherit, flags, startup_info, info, unixdir ); |
| 1711 | break; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1712 | case BINARY_PE_DLL: |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1713 | TRACE( "not starting %s since it is a dll\n", debugstr_w(name) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1714 | SetLastError( ERROR_BAD_EXE_FORMAT ); |
| 1715 | break; |
| 1716 | case BINARY_UNIX_LIB: |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1717 | TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_w(name) ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1718 | retv = create_process( hFile, name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr, |
Alexandre Julliard | 10e6f57 | 2004-06-03 00:25:08 +0000 | [diff] [blame] | 1719 | inherit, flags, startup_info, info, unixdir, NULL, NULL ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1720 | break; |
| 1721 | case BINARY_UNKNOWN: |
| 1722 | /* check for .com or .bat extension */ |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1723 | if ((p = strrchrW( name, '.' ))) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1724 | { |
Rein Klazes | 0ed059c | 2005-03-07 19:24:43 +0000 | [diff] [blame] | 1725 | if (!strcmpiW( p, comW ) || !strcmpiW( p, pifW )) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1726 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1727 | TRACE( "starting %s as DOS binary\n", debugstr_w(name) ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1728 | retv = create_vdm_process( name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr, |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1729 | inherit, flags, startup_info, info, unixdir ); |
| 1730 | break; |
| 1731 | } |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1732 | if (!strcmpiW( p, batW )) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1733 | { |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1734 | TRACE( "starting %s as batch binary\n", debugstr_w(name) ); |
Alexandre Julliard | 6ab8827 | 2004-05-14 05:03:29 +0000 | [diff] [blame] | 1735 | retv = create_cmd_process( name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr, |
| 1736 | inherit, flags, startup_info, info ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1737 | break; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | /* fall through */ |
| 1741 | case BINARY_UNIX_EXE: |
| 1742 | { |
| 1743 | /* unknown file, try as unix executable */ |
Alexandre Julliard | 1cb0cf4 | 2004-04-14 19:40:53 +0000 | [diff] [blame] | 1744 | char *unix_name; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1745 | |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1746 | TRACE( "starting %s as Unix binary\n", debugstr_w(name) ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1747 | |
Alexandre Julliard | 1cb0cf4 | 2004-04-14 19:40:53 +0000 | [diff] [blame] | 1748 | if ((unix_name = wine_get_unix_file_name( name ))) |
| 1749 | { |
Alexandre Julliard | a401f3c | 2006-03-06 22:22:30 +0100 | [diff] [blame] | 1750 | retv = (fork_and_exec( unix_name, tidy_cmdline, envW, unixdir, flags ) != -1); |
Alexandre Julliard | 1cb0cf4 | 2004-04-14 19:40:53 +0000 | [diff] [blame] | 1751 | HeapFree( GetProcessHeap(), 0, unix_name ); |
| 1752 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1753 | } |
| 1754 | break; |
| 1755 | } |
| 1756 | CloseHandle( hFile ); |
| 1757 | |
| 1758 | done: |
| 1759 | if (tidy_cmdline != cmd_line) HeapFree( GetProcessHeap(), 0, tidy_cmdline ); |
Alexandre Julliard | 841f898 | 2003-10-04 04:09:41 +0000 | [diff] [blame] | 1760 | if (envW != env) HeapFree( GetProcessHeap(), 0, envW ); |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 1761 | HeapFree( GetProcessHeap(), 0, unixdir ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1762 | return retv; |
| 1763 | } |
| 1764 | |
| 1765 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1766 | /*********************************************************************** |
| 1767 | * wait_input_idle |
| 1768 | * |
| 1769 | * Wrapper to call WaitForInputIdle USER function |
| 1770 | */ |
| 1771 | typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut ); |
| 1772 | |
| 1773 | static DWORD wait_input_idle( HANDLE process, DWORD timeout ) |
| 1774 | { |
| 1775 | HMODULE mod = GetModuleHandleA( "user32.dll" ); |
| 1776 | if (mod) |
| 1777 | { |
| 1778 | WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" ); |
| 1779 | if (ptr) return ptr( process, timeout ); |
| 1780 | } |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | /*********************************************************************** |
| 1786 | * WinExec (KERNEL32.@) |
| 1787 | */ |
| 1788 | UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow ) |
| 1789 | { |
| 1790 | PROCESS_INFORMATION info; |
| 1791 | STARTUPINFOA startup; |
| 1792 | char *cmdline; |
| 1793 | UINT ret; |
| 1794 | |
| 1795 | memset( &startup, 0, sizeof(startup) ); |
| 1796 | startup.cb = sizeof(startup); |
| 1797 | startup.dwFlags = STARTF_USESHOWWINDOW; |
| 1798 | startup.wShowWindow = nCmdShow; |
| 1799 | |
| 1800 | /* cmdline needs to be writeable for CreateProcess */ |
| 1801 | if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0; |
| 1802 | strcpy( cmdline, lpCmdLine ); |
| 1803 | |
| 1804 | if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE, |
| 1805 | 0, NULL, NULL, &startup, &info )) |
| 1806 | { |
| 1807 | /* Give 30 seconds to the app to come up */ |
Alexandre Julliard | 2aa8a83 | 2003-10-16 19:31:11 +0000 | [diff] [blame] | 1808 | if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1809 | WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() ); |
| 1810 | ret = 33; |
| 1811 | /* Close off the handles */ |
| 1812 | CloseHandle( info.hThread ); |
| 1813 | CloseHandle( info.hProcess ); |
| 1814 | } |
| 1815 | else if ((ret = GetLastError()) >= 32) |
| 1816 | { |
| 1817 | FIXME("Strange error set by CreateProcess: %d\n", ret ); |
| 1818 | ret = 11; |
| 1819 | } |
| 1820 | HeapFree( GetProcessHeap(), 0, cmdline ); |
| 1821 | return ret; |
| 1822 | } |
| 1823 | |
| 1824 | |
| 1825 | /********************************************************************** |
| 1826 | * LoadModule (KERNEL32.@) |
| 1827 | */ |
| 1828 | HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock ) |
| 1829 | { |
Alexandre Julliard | d3fb25c | 2003-11-17 20:16:38 +0000 | [diff] [blame] | 1830 | LOADPARMS32 *params = paramBlock; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1831 | PROCESS_INFORMATION info; |
| 1832 | STARTUPINFOA startup; |
| 1833 | HINSTANCE hInstance; |
| 1834 | LPSTR cmdline, p; |
| 1835 | char filename[MAX_PATH]; |
| 1836 | BYTE len; |
| 1837 | |
| 1838 | if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND; |
| 1839 | |
| 1840 | if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) && |
| 1841 | !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL )) |
| 1842 | return (HINSTANCE)GetLastError(); |
| 1843 | |
| 1844 | len = (BYTE)params->lpCmdLine[0]; |
| 1845 | if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 ))) |
| 1846 | return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY; |
| 1847 | |
| 1848 | strcpy( cmdline, filename ); |
| 1849 | p = cmdline + strlen(cmdline); |
| 1850 | *p++ = ' '; |
| 1851 | memcpy( p, params->lpCmdLine + 1, len ); |
| 1852 | p[len] = 0; |
| 1853 | |
| 1854 | memset( &startup, 0, sizeof(startup) ); |
| 1855 | startup.cb = sizeof(startup); |
| 1856 | if (params->lpCmdShow) |
| 1857 | { |
| 1858 | startup.dwFlags = STARTF_USESHOWWINDOW; |
Alexandre Julliard | d3fb25c | 2003-11-17 20:16:38 +0000 | [diff] [blame] | 1859 | startup.wShowWindow = ((WORD *)params->lpCmdShow)[1]; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0, |
| 1863 | params->lpEnvAddress, NULL, &startup, &info )) |
| 1864 | { |
| 1865 | /* Give 30 seconds to the app to come up */ |
Alexandre Julliard | 2aa8a83 | 2003-10-16 19:31:11 +0000 | [diff] [blame] | 1866 | if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1867 | WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() ); |
| 1868 | hInstance = (HINSTANCE)33; |
| 1869 | /* Close off the handles */ |
| 1870 | CloseHandle( info.hThread ); |
| 1871 | CloseHandle( info.hProcess ); |
| 1872 | } |
| 1873 | else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32) |
| 1874 | { |
| 1875 | FIXME("Strange error set by CreateProcess: %p\n", hInstance ); |
| 1876 | hInstance = (HINSTANCE)11; |
| 1877 | } |
| 1878 | |
| 1879 | HeapFree( GetProcessHeap(), 0, cmdline ); |
| 1880 | return hInstance; |
| 1881 | } |
| 1882 | |
| 1883 | |
| 1884 | /****************************************************************************** |
| 1885 | * TerminateProcess (KERNEL32.@) |
| 1886 | */ |
| 1887 | BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code ) |
| 1888 | { |
| 1889 | NTSTATUS status = NtTerminateProcess( handle, exit_code ); |
| 1890 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 1891 | return !status; |
| 1892 | } |
| 1893 | |
| 1894 | |
| 1895 | /*********************************************************************** |
Alexandre Julliard | b81d9e7 | 2003-10-08 03:57:02 +0000 | [diff] [blame] | 1896 | * ExitProcess (KERNEL32.@) |
| 1897 | */ |
| 1898 | void WINAPI ExitProcess( DWORD status ) |
| 1899 | { |
| 1900 | LdrShutdownProcess(); |
Ivan Leo Puoti | 6e50876 | 2005-09-06 09:25:19 +0000 | [diff] [blame] | 1901 | NtTerminateProcess(GetCurrentProcess(), status); |
| 1902 | exit(status); |
Alexandre Julliard | b81d9e7 | 2003-10-08 03:57:02 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | |
| 1906 | /*********************************************************************** |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1907 | * GetExitCodeProcess [KERNEL32.@] |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1908 | * |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1909 | * Gets termination status of specified process. |
| 1910 | * |
| 1911 | * PARAMS |
| 1912 | * hProcess [in] Handle to the process. |
| 1913 | * lpExitCode [out] Address to receive termination status. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1914 | * |
| 1915 | * RETURNS |
| 1916 | * Success: TRUE |
| 1917 | * Failure: FALSE |
| 1918 | */ |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1919 | BOOL WINAPI GetExitCodeProcess( HANDLE hProcess, LPDWORD lpExitCode ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1920 | { |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 1921 | NTSTATUS status; |
| 1922 | PROCESS_BASIC_INFORMATION pbi; |
| 1923 | |
| 1924 | status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi, |
| 1925 | sizeof(pbi), NULL); |
| 1926 | if (status == STATUS_SUCCESS) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1927 | { |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 1928 | if (lpExitCode) *lpExitCode = pbi.ExitStatus; |
| 1929 | return TRUE; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1930 | } |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 1931 | SetLastError( RtlNtStatusToDosError(status) ); |
| 1932 | return FALSE; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | |
| 1936 | /*********************************************************************** |
| 1937 | * SetErrorMode (KERNEL32.@) |
| 1938 | */ |
| 1939 | UINT WINAPI SetErrorMode( UINT mode ) |
| 1940 | { |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 1941 | UINT old = process_error_mode; |
| 1942 | process_error_mode = mode; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1943 | return old; |
| 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | /********************************************************************** |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1948 | * TlsAlloc [KERNEL32.@] |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1949 | * |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1950 | * Allocates a thread local storage index. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1951 | * |
| 1952 | * RETURNS |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1953 | * Success: TLS index. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1954 | * Failure: 0xFFFFFFFF |
| 1955 | */ |
| 1956 | DWORD WINAPI TlsAlloc( void ) |
| 1957 | { |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 1958 | DWORD index; |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 1959 | PEB * const peb = NtCurrentTeb()->Peb; |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 1960 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1961 | RtlAcquirePebLock(); |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 1962 | index = RtlFindClearBitsAndSet( peb->TlsBitmap, 1, 0 ); |
Alexandre Julliard | 7c1925a0 | 2005-09-13 15:00:32 +0000 | [diff] [blame] | 1963 | if (index != ~0U) NtCurrentTeb()->TlsSlots[index] = 0; /* clear the value */ |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 1964 | else |
| 1965 | { |
| 1966 | index = RtlFindClearBitsAndSet( peb->TlsExpansionBitmap, 1, 0 ); |
Alexandre Julliard | 7c1925a0 | 2005-09-13 15:00:32 +0000 | [diff] [blame] | 1967 | if (index != ~0U) |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 1968 | { |
| 1969 | if (!NtCurrentTeb()->TlsExpansionSlots && |
| 1970 | !(NtCurrentTeb()->TlsExpansionSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, |
| 1971 | 8 * sizeof(peb->TlsExpansionBitmapBits) * sizeof(void*) ))) |
| 1972 | { |
| 1973 | RtlClearBits( peb->TlsExpansionBitmap, index, 1 ); |
Alexandre Julliard | 7c1925a0 | 2005-09-13 15:00:32 +0000 | [diff] [blame] | 1974 | index = ~0U; |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 1975 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 1976 | } |
| 1977 | else |
| 1978 | { |
| 1979 | NtCurrentTeb()->TlsExpansionSlots[index] = 0; /* clear the value */ |
| 1980 | index += TLS_MINIMUM_AVAILABLE; |
| 1981 | } |
| 1982 | } |
| 1983 | else SetLastError( ERROR_NO_MORE_ITEMS ); |
| 1984 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1985 | RtlReleasePebLock(); |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 1986 | return index; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | /********************************************************************** |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1991 | * TlsFree [KERNEL32.@] |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1992 | * |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 1993 | * Releases a thread local storage index, making it available for reuse. |
| 1994 | * |
| 1995 | * PARAMS |
| 1996 | * index [in] TLS index to free. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 1997 | * |
| 1998 | * RETURNS |
| 1999 | * Success: TRUE |
| 2000 | * Failure: FALSE |
| 2001 | */ |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 2002 | BOOL WINAPI TlsFree( DWORD index ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2003 | { |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 2004 | BOOL ret; |
| 2005 | |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2006 | RtlAcquirePebLock(); |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2007 | if (index >= TLS_MINIMUM_AVAILABLE) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2008 | { |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2009 | ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsExpansionBitmap, index - TLS_MINIMUM_AVAILABLE, 1 ); |
| 2010 | if (ret) RtlClearBits( NtCurrentTeb()->Peb->TlsExpansionBitmap, index - TLS_MINIMUM_AVAILABLE, 1 ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2011 | } |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2012 | else |
| 2013 | { |
| 2014 | ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsBitmap, index, 1 ); |
| 2015 | if (ret) RtlClearBits( NtCurrentTeb()->Peb->TlsBitmap, index, 1 ); |
| 2016 | } |
| 2017 | if (ret) NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) ); |
Alexandre Julliard | 5cb5aa6 | 2003-10-09 19:58:56 +0000 | [diff] [blame] | 2018 | else SetLastError( ERROR_INVALID_PARAMETER ); |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2019 | RtlReleasePebLock(); |
| 2020 | return TRUE; |
| 2021 | } |
| 2022 | |
| 2023 | |
| 2024 | /********************************************************************** |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 2025 | * TlsGetValue [KERNEL32.@] |
| 2026 | * |
| 2027 | * Gets value in a thread's TLS slot. |
| 2028 | * |
| 2029 | * PARAMS |
| 2030 | * index [in] TLS index to retrieve value for. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2031 | * |
| 2032 | * RETURNS |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 2033 | * Success: Value stored in calling thread's TLS slot for index. |
| 2034 | * Failure: 0 and GetLastError() returns NO_ERROR. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2035 | */ |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 2036 | LPVOID WINAPI TlsGetValue( DWORD index ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2037 | { |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2038 | LPVOID ret; |
| 2039 | |
| 2040 | if (index < TLS_MINIMUM_AVAILABLE) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2041 | { |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2042 | ret = NtCurrentTeb()->TlsSlots[index]; |
| 2043 | } |
| 2044 | else |
| 2045 | { |
| 2046 | index -= TLS_MINIMUM_AVAILABLE; |
| 2047 | if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits)) |
| 2048 | { |
| 2049 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 2050 | return NULL; |
| 2051 | } |
| 2052 | if (!NtCurrentTeb()->TlsExpansionSlots) ret = NULL; |
| 2053 | else ret = NtCurrentTeb()->TlsExpansionSlots[index]; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2054 | } |
| 2055 | SetLastError( ERROR_SUCCESS ); |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2056 | return ret; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | |
| 2060 | /********************************************************************** |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 2061 | * TlsSetValue [KERNEL32.@] |
| 2062 | * |
| 2063 | * Stores a value in the thread's TLS slot. |
| 2064 | * |
| 2065 | * PARAMS |
| 2066 | * index [in] TLS index to set value for. |
| 2067 | * value [in] Value to be stored. |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2068 | * |
| 2069 | * RETURNS |
| 2070 | * Success: TRUE |
| 2071 | * Failure: FALSE |
| 2072 | */ |
Hans Leidekker | fa57544 | 2005-12-21 20:44:49 +0100 | [diff] [blame] | 2073 | BOOL WINAPI TlsSetValue( DWORD index, LPVOID value ) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2074 | { |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2075 | if (index < TLS_MINIMUM_AVAILABLE) |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2076 | { |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2077 | NtCurrentTeb()->TlsSlots[index] = value; |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2078 | } |
Alexandre Julliard | f1020bc | 2005-03-19 17:14:12 +0000 | [diff] [blame] | 2079 | else |
| 2080 | { |
| 2081 | index -= TLS_MINIMUM_AVAILABLE; |
| 2082 | if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits)) |
| 2083 | { |
| 2084 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 2085 | return FALSE; |
| 2086 | } |
| 2087 | if (!NtCurrentTeb()->TlsExpansionSlots && |
| 2088 | !(NtCurrentTeb()->TlsExpansionSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, |
| 2089 | 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits) * sizeof(void*) ))) |
| 2090 | { |
| 2091 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 2092 | return FALSE; |
| 2093 | } |
| 2094 | NtCurrentTeb()->TlsExpansionSlots[index] = value; |
| 2095 | } |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2096 | return TRUE; |
| 2097 | } |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2098 | |
| 2099 | |
| 2100 | /*********************************************************************** |
| 2101 | * GetProcessFlags (KERNEL32.@) |
| 2102 | */ |
| 2103 | DWORD WINAPI GetProcessFlags( DWORD processid ) |
| 2104 | { |
| 2105 | IMAGE_NT_HEADERS *nt; |
| 2106 | DWORD flags = 0; |
| 2107 | |
| 2108 | if (processid && processid != GetCurrentProcessId()) return 0; |
| 2109 | |
| 2110 | if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress ))) |
| 2111 | { |
| 2112 | if (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI) |
| 2113 | flags |= PDB32_CONSOLE_PROC; |
| 2114 | } |
| 2115 | if (!AreFileApisANSI()) flags |= PDB32_FILE_APIS_OEM; |
| 2116 | if (IsDebuggerPresent()) flags |= PDB32_DEBUGGED; |
| 2117 | return flags; |
| 2118 | } |
| 2119 | |
| 2120 | |
| 2121 | /*********************************************************************** |
| 2122 | * GetProcessDword (KERNEL.485) |
| 2123 | * GetProcessDword (KERNEL32.18) |
| 2124 | * 'Of course you cannot directly access Windows internal structures' |
| 2125 | */ |
| 2126 | DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset ) |
| 2127 | { |
| 2128 | DWORD x, y; |
| 2129 | STARTUPINFOW siw; |
| 2130 | |
| 2131 | TRACE("(%ld, %d)\n", dwProcessID, offset ); |
| 2132 | |
| 2133 | if (dwProcessID && dwProcessID != GetCurrentProcessId()) |
| 2134 | { |
| 2135 | ERR("%d: process %lx not accessible\n", offset, dwProcessID); |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
| 2139 | switch ( offset ) |
| 2140 | { |
| 2141 | case GPD_APP_COMPAT_FLAGS: |
| 2142 | return GetAppCompatFlags16(0); |
| 2143 | case GPD_LOAD_DONE_EVENT: |
| 2144 | return 0; |
| 2145 | case GPD_HINSTANCE16: |
| 2146 | return GetTaskDS16(); |
| 2147 | case GPD_WINDOWS_VERSION: |
| 2148 | return GetExeVersion16(); |
| 2149 | case GPD_THDB: |
| 2150 | return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */; |
| 2151 | case GPD_PDB: |
| 2152 | return (DWORD)NtCurrentTeb()->Peb; |
| 2153 | case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */ |
| 2154 | GetStartupInfoW(&siw); |
| 2155 | return (DWORD)siw.hStdOutput; |
| 2156 | case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */ |
| 2157 | GetStartupInfoW(&siw); |
| 2158 | return (DWORD)siw.hStdInput; |
| 2159 | case GPD_STARTF_SHOWWINDOW: |
| 2160 | GetStartupInfoW(&siw); |
| 2161 | return siw.wShowWindow; |
| 2162 | case GPD_STARTF_SIZE: |
| 2163 | GetStartupInfoW(&siw); |
| 2164 | x = siw.dwXSize; |
| 2165 | if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16; |
| 2166 | y = siw.dwYSize; |
| 2167 | if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16; |
| 2168 | return MAKELONG( x, y ); |
| 2169 | case GPD_STARTF_POSITION: |
| 2170 | GetStartupInfoW(&siw); |
| 2171 | x = siw.dwX; |
| 2172 | if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16; |
| 2173 | y = siw.dwY; |
| 2174 | if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16; |
| 2175 | return MAKELONG( x, y ); |
| 2176 | case GPD_STARTF_FLAGS: |
| 2177 | GetStartupInfoW(&siw); |
| 2178 | return siw.dwFlags; |
| 2179 | case GPD_PARENT: |
| 2180 | return 0; |
| 2181 | case GPD_FLAGS: |
| 2182 | return GetProcessFlags(0); |
| 2183 | case GPD_USERDATA: |
| 2184 | return process_dword; |
| 2185 | default: |
| 2186 | ERR("Unknown offset %d\n", offset ); |
| 2187 | return 0; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | /*********************************************************************** |
| 2192 | * SetProcessDword (KERNEL.484) |
| 2193 | * 'Of course you cannot directly access Windows internal structures' |
| 2194 | */ |
| 2195 | void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value ) |
| 2196 | { |
| 2197 | TRACE("(%ld, %d)\n", dwProcessID, offset ); |
| 2198 | |
| 2199 | if (dwProcessID && dwProcessID != GetCurrentProcessId()) |
| 2200 | { |
| 2201 | ERR("%d: process %lx not accessible\n", offset, dwProcessID); |
| 2202 | return; |
| 2203 | } |
| 2204 | |
| 2205 | switch ( offset ) |
| 2206 | { |
| 2207 | case GPD_APP_COMPAT_FLAGS: |
| 2208 | case GPD_LOAD_DONE_EVENT: |
| 2209 | case GPD_HINSTANCE16: |
| 2210 | case GPD_WINDOWS_VERSION: |
| 2211 | case GPD_THDB: |
| 2212 | case GPD_PDB: |
| 2213 | case GPD_STARTF_SHELLDATA: |
| 2214 | case GPD_STARTF_HOTKEY: |
| 2215 | case GPD_STARTF_SHOWWINDOW: |
| 2216 | case GPD_STARTF_SIZE: |
| 2217 | case GPD_STARTF_POSITION: |
| 2218 | case GPD_STARTF_FLAGS: |
| 2219 | case GPD_PARENT: |
| 2220 | case GPD_FLAGS: |
| 2221 | ERR("Not allowed to modify offset %d\n", offset ); |
| 2222 | break; |
| 2223 | case GPD_USERDATA: |
| 2224 | process_dword = value; |
| 2225 | break; |
| 2226 | default: |
| 2227 | ERR("Unknown offset %d\n", offset ); |
| 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | |
| 2233 | /*********************************************************************** |
| 2234 | * ExitProcess (KERNEL.466) |
| 2235 | */ |
| 2236 | void WINAPI ExitProcess16( WORD status ) |
| 2237 | { |
| 2238 | DWORD count; |
| 2239 | ReleaseThunkLock( &count ); |
| 2240 | ExitProcess( status ); |
| 2241 | } |
| 2242 | |
| 2243 | |
| 2244 | /********************************************************************* |
| 2245 | * OpenProcess (KERNEL32.@) |
| 2246 | */ |
| 2247 | HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id ) |
| 2248 | { |
Eric Pouech | 6d68a03 | 2005-06-27 11:12:11 +0000 | [diff] [blame] | 2249 | NTSTATUS status; |
| 2250 | HANDLE handle; |
| 2251 | OBJECT_ATTRIBUTES attr; |
| 2252 | CLIENT_ID cid; |
| 2253 | |
| 2254 | cid.UniqueProcess = (HANDLE)id; |
| 2255 | cid.UniqueThread = 0; /* FIXME ? */ |
| 2256 | |
| 2257 | attr.Length = sizeof(OBJECT_ATTRIBUTES); |
| 2258 | attr.RootDirectory = NULL; |
| 2259 | attr.Attributes = inherit ? OBJ_INHERIT : 0; |
| 2260 | attr.SecurityDescriptor = NULL; |
| 2261 | attr.SecurityQualityOfService = NULL; |
| 2262 | attr.ObjectName = NULL; |
| 2263 | |
| 2264 | status = NtOpenProcess(&handle, access, &attr, &cid); |
| 2265 | if (status != STATUS_SUCCESS) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2266 | { |
Eric Pouech | 6d68a03 | 2005-06-27 11:12:11 +0000 | [diff] [blame] | 2267 | SetLastError( RtlNtStatusToDosError(status) ); |
| 2268 | return NULL; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2269 | } |
Eric Pouech | 6d68a03 | 2005-06-27 11:12:11 +0000 | [diff] [blame] | 2270 | return handle; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | |
| 2274 | /********************************************************************* |
| 2275 | * MapProcessHandle (KERNEL.483) |
Robert Shearman | 3ae44f7 | 2004-08-24 18:46:05 +0000 | [diff] [blame] | 2276 | * GetProcessId (KERNEL32.@) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2277 | */ |
Robert Shearman | 3ae44f7 | 2004-08-24 18:46:05 +0000 | [diff] [blame] | 2278 | DWORD WINAPI GetProcessId( HANDLE hProcess ) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2279 | { |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 2280 | NTSTATUS status; |
| 2281 | PROCESS_BASIC_INFORMATION pbi; |
| 2282 | |
| 2283 | status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi, |
| 2284 | sizeof(pbi), NULL); |
| 2285 | if (status == STATUS_SUCCESS) return pbi.UniqueProcessId; |
| 2286 | SetLastError( RtlNtStatusToDosError(status) ); |
| 2287 | return 0; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | |
Alexandre Julliard | 687ecfa | 2003-10-08 04:24:49 +0000 | [diff] [blame] | 2291 | /********************************************************************* |
| 2292 | * CloseW32Handle (KERNEL.474) |
| 2293 | * CloseHandle (KERNEL32.@) |
| 2294 | */ |
| 2295 | BOOL WINAPI CloseHandle( HANDLE handle ) |
| 2296 | { |
| 2297 | NTSTATUS status; |
| 2298 | |
| 2299 | /* stdio handles need special treatment */ |
| 2300 | if ((handle == (HANDLE)STD_INPUT_HANDLE) || |
| 2301 | (handle == (HANDLE)STD_OUTPUT_HANDLE) || |
| 2302 | (handle == (HANDLE)STD_ERROR_HANDLE)) |
| 2303 | handle = GetStdHandle( (DWORD)handle ); |
| 2304 | |
| 2305 | if (is_console_handle(handle)) |
| 2306 | return CloseConsoleHandle(handle); |
| 2307 | |
| 2308 | status = NtClose( handle ); |
| 2309 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2310 | return !status; |
| 2311 | } |
| 2312 | |
| 2313 | |
| 2314 | /********************************************************************* |
| 2315 | * GetHandleInformation (KERNEL32.@) |
| 2316 | */ |
| 2317 | BOOL WINAPI GetHandleInformation( HANDLE handle, LPDWORD flags ) |
| 2318 | { |
Alexandre Julliard | 73cb765 | 2005-08-23 18:20:59 +0000 | [diff] [blame] | 2319 | OBJECT_DATA_INFORMATION info; |
| 2320 | NTSTATUS status = NtQueryObject( handle, ObjectDataInformation, &info, sizeof(info), NULL ); |
| 2321 | |
| 2322 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2323 | else if (flags) |
Alexandre Julliard | 687ecfa | 2003-10-08 04:24:49 +0000 | [diff] [blame] | 2324 | { |
Alexandre Julliard | 73cb765 | 2005-08-23 18:20:59 +0000 | [diff] [blame] | 2325 | *flags = 0; |
| 2326 | if (info.InheritHandle) *flags |= HANDLE_FLAG_INHERIT; |
| 2327 | if (info.ProtectFromClose) *flags |= HANDLE_FLAG_PROTECT_FROM_CLOSE; |
Alexandre Julliard | 687ecfa | 2003-10-08 04:24:49 +0000 | [diff] [blame] | 2328 | } |
Alexandre Julliard | 73cb765 | 2005-08-23 18:20:59 +0000 | [diff] [blame] | 2329 | return !status; |
Alexandre Julliard | 687ecfa | 2003-10-08 04:24:49 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | /********************************************************************* |
| 2334 | * SetHandleInformation (KERNEL32.@) |
| 2335 | */ |
| 2336 | BOOL WINAPI SetHandleInformation( HANDLE handle, DWORD mask, DWORD flags ) |
| 2337 | { |
Alexandre Julliard | 73cb765 | 2005-08-23 18:20:59 +0000 | [diff] [blame] | 2338 | OBJECT_DATA_INFORMATION info; |
| 2339 | NTSTATUS status; |
| 2340 | |
| 2341 | /* if not setting both fields, retrieve current value first */ |
| 2342 | if ((mask & (HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE)) != |
| 2343 | (HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE)) |
Alexandre Julliard | 687ecfa | 2003-10-08 04:24:49 +0000 | [diff] [blame] | 2344 | { |
Alexandre Julliard | 73cb765 | 2005-08-23 18:20:59 +0000 | [diff] [blame] | 2345 | if ((status = NtQueryObject( handle, ObjectDataInformation, &info, sizeof(info), NULL ))) |
| 2346 | { |
| 2347 | SetLastError( RtlNtStatusToDosError(status) ); |
| 2348 | return FALSE; |
| 2349 | } |
Alexandre Julliard | 687ecfa | 2003-10-08 04:24:49 +0000 | [diff] [blame] | 2350 | } |
Alexandre Julliard | 73cb765 | 2005-08-23 18:20:59 +0000 | [diff] [blame] | 2351 | if (mask & HANDLE_FLAG_INHERIT) |
| 2352 | info.InheritHandle = (flags & HANDLE_FLAG_INHERIT) != 0; |
| 2353 | if (mask & HANDLE_FLAG_PROTECT_FROM_CLOSE) |
| 2354 | info.ProtectFromClose = (flags & HANDLE_FLAG_PROTECT_FROM_CLOSE) != 0; |
| 2355 | |
| 2356 | status = NtSetInformationObject( handle, ObjectDataInformation, &info, sizeof(info) ); |
| 2357 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2358 | return !status; |
Alexandre Julliard | 687ecfa | 2003-10-08 04:24:49 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | /********************************************************************* |
| 2363 | * DuplicateHandle (KERNEL32.@) |
| 2364 | */ |
| 2365 | BOOL WINAPI DuplicateHandle( HANDLE source_process, HANDLE source, |
| 2366 | HANDLE dest_process, HANDLE *dest, |
| 2367 | DWORD access, BOOL inherit, DWORD options ) |
| 2368 | { |
| 2369 | NTSTATUS status; |
| 2370 | |
| 2371 | if (is_console_handle(source)) |
| 2372 | { |
| 2373 | /* FIXME: this test is not sufficient, we need to test process ids, not handles */ |
| 2374 | if (source_process != dest_process || |
| 2375 | source_process != GetCurrentProcess()) |
| 2376 | { |
| 2377 | SetLastError(ERROR_INVALID_PARAMETER); |
| 2378 | return FALSE; |
| 2379 | } |
| 2380 | *dest = DuplicateConsoleHandle( source, access, inherit, options ); |
| 2381 | return (*dest != INVALID_HANDLE_VALUE); |
| 2382 | } |
| 2383 | status = NtDuplicateObject( source_process, source, dest_process, dest, |
| 2384 | access, inherit ? OBJ_INHERIT : 0, options ); |
| 2385 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2386 | return !status; |
| 2387 | } |
| 2388 | |
| 2389 | |
| 2390 | /*********************************************************************** |
| 2391 | * ConvertToGlobalHandle (KERNEL.476) |
| 2392 | * ConvertToGlobalHandle (KERNEL32.@) |
| 2393 | */ |
| 2394 | HANDLE WINAPI ConvertToGlobalHandle(HANDLE hSrc) |
| 2395 | { |
| 2396 | HANDLE ret = INVALID_HANDLE_VALUE; |
| 2397 | DuplicateHandle( GetCurrentProcess(), hSrc, GetCurrentProcess(), &ret, 0, FALSE, |
| 2398 | DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE ); |
| 2399 | return ret; |
| 2400 | } |
| 2401 | |
| 2402 | |
| 2403 | /*********************************************************************** |
| 2404 | * SetHandleContext (KERNEL32.@) |
| 2405 | */ |
| 2406 | BOOL WINAPI SetHandleContext(HANDLE hnd,DWORD context) |
| 2407 | { |
| 2408 | FIXME("(%p,%ld), stub. In case this got called by WSOCK32/WS2_32: " |
| 2409 | "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd,context); |
| 2410 | SetLastError(ERROR_CALL_NOT_IMPLEMENTED); |
| 2411 | return FALSE; |
| 2412 | } |
| 2413 | |
| 2414 | |
| 2415 | /*********************************************************************** |
| 2416 | * GetHandleContext (KERNEL32.@) |
| 2417 | */ |
| 2418 | DWORD WINAPI GetHandleContext(HANDLE hnd) |
| 2419 | { |
| 2420 | FIXME("(%p), stub. In case this got called by WSOCK32/WS2_32: " |
| 2421 | "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd); |
| 2422 | SetLastError(ERROR_CALL_NOT_IMPLEMENTED); |
| 2423 | return 0; |
| 2424 | } |
| 2425 | |
| 2426 | |
| 2427 | /*********************************************************************** |
| 2428 | * CreateSocketHandle (KERNEL32.@) |
| 2429 | */ |
| 2430 | HANDLE WINAPI CreateSocketHandle(void) |
| 2431 | { |
| 2432 | FIXME("(), stub. In case this got called by WSOCK32/WS2_32: " |
| 2433 | "the external WINSOCK DLLs won't work with WINE, don't use them.\n"); |
| 2434 | SetLastError(ERROR_CALL_NOT_IMPLEMENTED); |
| 2435 | return INVALID_HANDLE_VALUE; |
| 2436 | } |
| 2437 | |
| 2438 | |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2439 | /*********************************************************************** |
| 2440 | * SetPriorityClass (KERNEL32.@) |
| 2441 | */ |
| 2442 | BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass ) |
| 2443 | { |
Eric Pouech | b3badc7 | 2005-09-06 10:25:11 +0000 | [diff] [blame] | 2444 | NTSTATUS status; |
| 2445 | PROCESS_PRIORITY_CLASS ppc; |
| 2446 | |
| 2447 | ppc.Foreground = FALSE; |
| 2448 | switch (priorityclass) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2449 | { |
Eric Pouech | b3badc7 | 2005-09-06 10:25:11 +0000 | [diff] [blame] | 2450 | case IDLE_PRIORITY_CLASS: |
| 2451 | ppc.PriorityClass = PROCESS_PRIOCLASS_IDLE; break; |
| 2452 | case BELOW_NORMAL_PRIORITY_CLASS: |
| 2453 | ppc.PriorityClass = PROCESS_PRIOCLASS_BELOW_NORMAL; break; |
| 2454 | case NORMAL_PRIORITY_CLASS: |
| 2455 | ppc.PriorityClass = PROCESS_PRIOCLASS_NORMAL; break; |
| 2456 | case ABOVE_NORMAL_PRIORITY_CLASS: |
| 2457 | ppc.PriorityClass = PROCESS_PRIOCLASS_ABOVE_NORMAL; break; |
| 2458 | case HIGH_PRIORITY_CLASS: |
| 2459 | ppc.PriorityClass = PROCESS_PRIOCLASS_HIGH; break; |
| 2460 | case REALTIME_PRIORITY_CLASS: |
| 2461 | ppc.PriorityClass = PROCESS_PRIOCLASS_REALTIME; break; |
| 2462 | default: |
| 2463 | SetLastError(ERROR_INVALID_PARAMETER); |
| 2464 | return FALSE; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2465 | } |
Eric Pouech | b3badc7 | 2005-09-06 10:25:11 +0000 | [diff] [blame] | 2466 | |
| 2467 | status = NtSetInformationProcess(hprocess, ProcessPriorityClass, |
| 2468 | &ppc, sizeof(ppc)); |
| 2469 | |
| 2470 | if (status != STATUS_SUCCESS) |
| 2471 | { |
| 2472 | SetLastError( RtlNtStatusToDosError(status) ); |
| 2473 | return FALSE; |
| 2474 | } |
| 2475 | return TRUE; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | |
| 2479 | /*********************************************************************** |
| 2480 | * GetPriorityClass (KERNEL32.@) |
| 2481 | */ |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 2482 | DWORD WINAPI GetPriorityClass(HANDLE hProcess) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2483 | { |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 2484 | NTSTATUS status; |
| 2485 | PROCESS_BASIC_INFORMATION pbi; |
| 2486 | |
| 2487 | status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi, |
| 2488 | sizeof(pbi), NULL); |
Eric Pouech | b3badc7 | 2005-09-06 10:25:11 +0000 | [diff] [blame] | 2489 | if (status != STATUS_SUCCESS) |
| 2490 | { |
| 2491 | SetLastError( RtlNtStatusToDosError(status) ); |
| 2492 | return 0; |
| 2493 | } |
| 2494 | switch (pbi.BasePriority) |
| 2495 | { |
| 2496 | case PROCESS_PRIOCLASS_IDLE: return IDLE_PRIORITY_CLASS; |
| 2497 | case PROCESS_PRIOCLASS_BELOW_NORMAL: return BELOW_NORMAL_PRIORITY_CLASS; |
| 2498 | case PROCESS_PRIOCLASS_NORMAL: return NORMAL_PRIORITY_CLASS; |
| 2499 | case PROCESS_PRIOCLASS_ABOVE_NORMAL: return ABOVE_NORMAL_PRIORITY_CLASS; |
| 2500 | case PROCESS_PRIOCLASS_HIGH: return HIGH_PRIORITY_CLASS; |
| 2501 | case PROCESS_PRIOCLASS_REALTIME: return REALTIME_PRIORITY_CLASS; |
| 2502 | } |
| 2503 | SetLastError( ERROR_INVALID_PARAMETER ); |
Eric Pouech | b0fd2ad | 2004-06-14 17:02:00 +0000 | [diff] [blame] | 2504 | return 0; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | |
| 2508 | /*********************************************************************** |
| 2509 | * SetProcessAffinityMask (KERNEL32.@) |
| 2510 | */ |
Vincent Béron | 6c644d6 | 2004-10-19 23:55:15 +0000 | [diff] [blame] | 2511 | BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD_PTR affmask ) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2512 | { |
Eric Pouech | b09582a | 2005-09-27 10:52:10 +0000 | [diff] [blame] | 2513 | NTSTATUS status; |
| 2514 | |
| 2515 | status = NtSetInformationProcess(hProcess, ProcessAffinityMask, |
| 2516 | &affmask, sizeof(DWORD_PTR)); |
| 2517 | if (!status) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2518 | { |
Eric Pouech | b09582a | 2005-09-27 10:52:10 +0000 | [diff] [blame] | 2519 | SetLastError( RtlNtStatusToDosError(status) ); |
| 2520 | return FALSE; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2521 | } |
Eric Pouech | b09582a | 2005-09-27 10:52:10 +0000 | [diff] [blame] | 2522 | return TRUE; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | |
| 2526 | /********************************************************************** |
| 2527 | * GetProcessAffinityMask (KERNEL32.@) |
| 2528 | */ |
| 2529 | BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess, |
Eric Pouech | b09582a | 2005-09-27 10:52:10 +0000 | [diff] [blame] | 2530 | PDWORD_PTR lpProcessAffinityMask, |
| 2531 | PDWORD_PTR lpSystemAffinityMask ) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2532 | { |
Eric Pouech | b09582a | 2005-09-27 10:52:10 +0000 | [diff] [blame] | 2533 | PROCESS_BASIC_INFORMATION pbi; |
| 2534 | NTSTATUS status; |
| 2535 | |
| 2536 | status = NtQueryInformationProcess(hProcess, |
| 2537 | ProcessBasicInformation, |
| 2538 | &pbi, sizeof(pbi), NULL); |
| 2539 | if (status) |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2540 | { |
Eric Pouech | b09582a | 2005-09-27 10:52:10 +0000 | [diff] [blame] | 2541 | SetLastError( RtlNtStatusToDosError(status) ); |
| 2542 | return FALSE; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2543 | } |
Eric Pouech | b09582a | 2005-09-27 10:52:10 +0000 | [diff] [blame] | 2544 | if (lpProcessAffinityMask) *lpProcessAffinityMask = pbi.AffinityMask; |
| 2545 | /* FIXME */ |
| 2546 | if (lpSystemAffinityMask) *lpSystemAffinityMask = 1; |
| 2547 | return TRUE; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | |
| 2551 | /*********************************************************************** |
| 2552 | * GetProcessVersion (KERNEL32.@) |
| 2553 | */ |
| 2554 | DWORD WINAPI GetProcessVersion( DWORD processid ) |
| 2555 | { |
| 2556 | IMAGE_NT_HEADERS *nt; |
| 2557 | |
| 2558 | if (processid && processid != GetCurrentProcessId()) |
| 2559 | { |
| 2560 | FIXME("should use ReadProcessMemory\n"); |
| 2561 | return 0; |
| 2562 | } |
| 2563 | if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress ))) |
| 2564 | return ((nt->OptionalHeader.MajorSubsystemVersion << 16) | |
| 2565 | nt->OptionalHeader.MinorSubsystemVersion); |
| 2566 | return 0; |
| 2567 | } |
| 2568 | |
| 2569 | |
| 2570 | /*********************************************************************** |
| 2571 | * SetProcessWorkingSetSize [KERNEL32.@] |
| 2572 | * Sets the min/max working set sizes for a specified process. |
| 2573 | * |
| 2574 | * PARAMS |
| 2575 | * hProcess [I] Handle to the process of interest |
| 2576 | * minset [I] Specifies minimum working set size |
| 2577 | * maxset [I] Specifies maximum working set size |
| 2578 | * |
Jon Griffiths | 7b18895 | 2004-12-15 10:48:18 +0000 | [diff] [blame] | 2579 | * RETURNS |
| 2580 | * Success: TRUE |
| 2581 | * Failure: FALSE |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2582 | */ |
| 2583 | BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T minset, |
| 2584 | SIZE_T maxset) |
| 2585 | { |
| 2586 | FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess,minset,maxset); |
| 2587 | if(( minset == (SIZE_T)-1) && (maxset == (SIZE_T)-1)) { |
| 2588 | /* Trim the working set to zero */ |
| 2589 | /* Swap the process out of physical RAM */ |
| 2590 | } |
| 2591 | return TRUE; |
| 2592 | } |
| 2593 | |
| 2594 | /*********************************************************************** |
| 2595 | * GetProcessWorkingSetSize (KERNEL32.@) |
| 2596 | */ |
| 2597 | BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess, PSIZE_T minset, |
| 2598 | PSIZE_T maxset) |
| 2599 | { |
| 2600 | FIXME("(%p,%p,%p): stub\n",hProcess,minset,maxset); |
| 2601 | /* 32 MB working set size */ |
| 2602 | if (minset) *minset = 32*1024*1024; |
| 2603 | if (maxset) *maxset = 32*1024*1024; |
| 2604 | return TRUE; |
| 2605 | } |
| 2606 | |
| 2607 | |
| 2608 | /*********************************************************************** |
| 2609 | * SetProcessShutdownParameters (KERNEL32.@) |
| 2610 | */ |
| 2611 | BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags) |
| 2612 | { |
| 2613 | FIXME("(%08lx, %08lx): partial stub.\n", level, flags); |
| 2614 | shutdown_flags = flags; |
| 2615 | shutdown_priority = level; |
| 2616 | return TRUE; |
| 2617 | } |
| 2618 | |
| 2619 | |
| 2620 | /*********************************************************************** |
| 2621 | * GetProcessShutdownParameters (KERNEL32.@) |
| 2622 | * |
| 2623 | */ |
| 2624 | BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags ) |
| 2625 | { |
| 2626 | *lpdwLevel = shutdown_priority; |
| 2627 | *lpdwFlags = shutdown_flags; |
| 2628 | return TRUE; |
| 2629 | } |
| 2630 | |
| 2631 | |
| 2632 | /*********************************************************************** |
| 2633 | * GetProcessPriorityBoost (KERNEL32.@) |
| 2634 | */ |
| 2635 | BOOL WINAPI GetProcessPriorityBoost(HANDLE hprocess,PBOOL pDisablePriorityBoost) |
| 2636 | { |
| 2637 | FIXME("(%p,%p): semi-stub\n", hprocess, pDisablePriorityBoost); |
| 2638 | |
| 2639 | /* Report that no boost is present.. */ |
| 2640 | *pDisablePriorityBoost = FALSE; |
| 2641 | |
| 2642 | return TRUE; |
| 2643 | } |
| 2644 | |
| 2645 | /*********************************************************************** |
| 2646 | * SetProcessPriorityBoost (KERNEL32.@) |
| 2647 | */ |
| 2648 | BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost) |
| 2649 | { |
| 2650 | FIXME("(%p,%d): stub\n",hprocess,disableboost); |
| 2651 | /* Say we can do it. I doubt the program will notice that we don't. */ |
| 2652 | return TRUE; |
| 2653 | } |
| 2654 | |
| 2655 | |
| 2656 | /*********************************************************************** |
| 2657 | * ReadProcessMemory (KERNEL32.@) |
| 2658 | */ |
| 2659 | BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, SIZE_T size, |
| 2660 | SIZE_T *bytes_read ) |
| 2661 | { |
Alexandre Julliard | 4f4b980 | 2003-07-08 21:18:45 +0000 | [diff] [blame] | 2662 | NTSTATUS status = NtReadVirtualMemory( process, addr, buffer, size, bytes_read ); |
| 2663 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2664 | return !status; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | |
| 2668 | /*********************************************************************** |
| 2669 | * WriteProcessMemory (KERNEL32.@) |
| 2670 | */ |
| 2671 | BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPCVOID buffer, SIZE_T size, |
| 2672 | SIZE_T *bytes_written ) |
| 2673 | { |
Alexandre Julliard | 4f4b980 | 2003-07-08 21:18:45 +0000 | [diff] [blame] | 2674 | NTSTATUS status = NtWriteVirtualMemory( process, addr, buffer, size, bytes_written ); |
| 2675 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2676 | return !status; |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Alexandre Julliard | 240b0a9 | 2003-09-24 05:26:00 +0000 | [diff] [blame] | 2679 | |
| 2680 | /**************************************************************************** |
| 2681 | * FlushInstructionCache (KERNEL32.@) |
| 2682 | */ |
| 2683 | BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize) |
| 2684 | { |
Mike McCormack | e3d8853 | 2004-08-04 18:15:04 +0000 | [diff] [blame] | 2685 | NTSTATUS status; |
Alexandre Julliard | 240b0a9 | 2003-09-24 05:26:00 +0000 | [diff] [blame] | 2686 | if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */ |
Mike McCormack | e3d8853 | 2004-08-04 18:15:04 +0000 | [diff] [blame] | 2687 | status = NtFlushInstructionCache( hProcess, lpBaseAddress, dwSize ); |
| 2688 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2689 | return !status; |
Alexandre Julliard | 240b0a9 | 2003-09-24 05:26:00 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | |
Eric Pouech | c3e6c09 | 2003-09-19 00:06:45 +0000 | [diff] [blame] | 2693 | /****************************************************************** |
| 2694 | * GetProcessIoCounters (KERNEL32.@) |
Eric Pouech | c3e6c09 | 2003-09-19 00:06:45 +0000 | [diff] [blame] | 2695 | */ |
| 2696 | BOOL WINAPI GetProcessIoCounters(HANDLE hProcess, PIO_COUNTERS ioc) |
| 2697 | { |
| 2698 | NTSTATUS status; |
| 2699 | |
| 2700 | status = NtQueryInformationProcess(hProcess, ProcessIoCounters, |
| 2701 | ioc, sizeof(*ioc), NULL); |
| 2702 | if (status) SetLastError( RtlNtStatusToDosError(status) ); |
| 2703 | return !status; |
| 2704 | } |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2705 | |
| 2706 | /*********************************************************************** |
Alexandre Julliard | aaf3503 | 2003-09-17 05:31:32 +0000 | [diff] [blame] | 2707 | * ProcessIdToSessionId (KERNEL32.@) |
| 2708 | * This function is available on Terminal Server 4SP4 and Windows 2000 |
| 2709 | */ |
| 2710 | BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr ) |
| 2711 | { |
| 2712 | /* According to MSDN, if the calling process is not in a terminal |
| 2713 | * services environment, then the sessionid returned is zero. |
| 2714 | */ |
| 2715 | *sessionid_ptr = 0; |
| 2716 | return TRUE; |
| 2717 | } |
| 2718 | |
| 2719 | |
| 2720 | /*********************************************************************** |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2721 | * RegisterServiceProcess (KERNEL.491) |
| 2722 | * RegisterServiceProcess (KERNEL32.@) |
| 2723 | * |
| 2724 | * A service process calls this function to ensure that it continues to run |
| 2725 | * even after a user logged off. |
| 2726 | */ |
| 2727 | DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType) |
| 2728 | { |
Jon Griffiths | 7b18895 | 2004-12-15 10:48:18 +0000 | [diff] [blame] | 2729 | /* I don't think that Wine needs to do anything in this function */ |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2730 | return 1; /* success */ |
| 2731 | } |
| 2732 | |
| 2733 | |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2734 | /*********************************************************************** |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2735 | * GetCurrentProcess (KERNEL32.@) |
Jon Griffiths | 7b18895 | 2004-12-15 10:48:18 +0000 | [diff] [blame] | 2736 | * |
| 2737 | * Get a handle to the current process. |
| 2738 | * |
| 2739 | * PARAMS |
| 2740 | * None. |
| 2741 | * |
| 2742 | * RETURNS |
| 2743 | * A handle representing the current process. |
Alexandre Julliard | 4cb2120 | 2003-06-27 04:08:04 +0000 | [diff] [blame] | 2744 | */ |
| 2745 | #undef GetCurrentProcess |
| 2746 | HANDLE WINAPI GetCurrentProcess(void) |
| 2747 | { |
| 2748 | return (HANDLE)0xffffffff; |
| 2749 | } |
Mike McCormack | a7e36dd | 2006-02-22 20:54:16 +0900 | [diff] [blame] | 2750 | |
| 2751 | /*********************************************************************** |
| 2752 | * CmdBatNotification (KERNEL32.@) |
| 2753 | * |
| 2754 | * Called by cmd.exe with |
| 2755 | * (1) when a batch file is started |
| 2756 | * (0) when a batch file finishes executing |
| 2757 | */ |
| 2758 | BOOL WINAPI CmdBatNotification( BOOL bBatchRunning ) |
| 2759 | { |
| 2760 | FIXME("%d\n", bBatchRunning); |
| 2761 | return FALSE; |
| 2762 | } |