d3dx9: Use an alpha-capable format when creating textures with a color key.
[wine/multimedia.git] / programs / wineboot / wineboot.c
bloba20b4e147696a04f148f395afa2b40804508075b
1 /*
2 * Copyright (C) 2002 Andreas Mohr
3 * Copyright (C) 2002 Shachar Shemesh
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 /* Wine "bootup" handler application
21 * This app handles the various "hooks" windows allows for applications to perform
22 * as part of the bootstrap process. These are roughly divided into three types.
23 * Knowledge base articles that explain this are 137367, 179365, 232487 and 232509.
24 * Also, 119941 has some info on grpconv.exe
25 * The operations performed are (by order of execution):
27 * Preboot (prior to fully loading the Windows kernel):
28 * - wininit.exe (rename operations left in wininit.ini - Win 9x only)
29 * - PendingRenameOperations (rename operations left in the registry - Win NT+ only)
31 * Startup (before the user logs in)
32 * - Services (NT)
33 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce (9x, asynch)
34 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices (9x, asynch)
36 * After log in
37 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, synch)
38 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
39 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
40 * - Startup folders (all, ?asynch?)
41 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, asynch)
43 * Somewhere in there is processing the RunOnceEx entries (also no imp)
45 * Bugs:
46 * - If a pending rename registry does not start with \??\ the entry is
47 * processed anyways. I'm not sure that is the Windows behaviour.
48 * - Need to check what is the windows behaviour when trying to delete files
49 * and directories that are read-only
50 * - In the pending rename registry processing - there are no traces of the files
51 * processed (requires translations from Unicode to Ansi).
54 #include "config.h"
55 #include "wine/port.h"
57 #define COBJMACROS
58 #define WIN32_LEAN_AND_MEAN
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #ifdef HAVE_GETOPT_H
65 # include <getopt.h>
66 #endif
67 #ifdef HAVE_SYS_STAT_H
68 # include <sys/stat.h>
69 #endif
70 #ifdef HAVE_UNISTD_H
71 # include <unistd.h>
72 #endif
73 #include <windows.h>
74 #include <winternl.h>
75 #include <wine/svcctl.h>
76 #include <wine/unicode.h>
77 #include <wine/library.h>
78 #include <wine/debug.h>
80 #include <shlobj.h>
81 #include <shobjidl.h>
82 #include <shlwapi.h>
83 #include <shellapi.h>
84 #include "resource.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
88 #define MAX_LINE_LENGTH (2*MAX_PATH+2)
90 extern BOOL shutdown_close_windows( BOOL force );
91 extern BOOL shutdown_all_desktops( BOOL force );
92 extern void kill_processes( BOOL kill_desktop );
94 static WCHAR windowsdir[MAX_PATH];
96 /* retrieve the (unix) path to the wine.inf file */
97 static char *get_wine_inf_path(void)
99 const char *build_dir, *data_dir;
100 char *name = NULL;
102 if ((data_dir = wine_get_data_dir()))
104 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(data_dir) + sizeof("/wine.inf") )))
105 return NULL;
106 strcpy( name, data_dir );
107 strcat( name, "/wine.inf" );
109 else if ((build_dir = wine_get_build_dir()))
111 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/loader/wine.inf") )))
112 return NULL;
113 strcpy( name, build_dir );
114 strcat( name, "/loader/wine.inf" );
116 return name;
119 /* update the timestamp if different from the reference time */
120 static BOOL update_timestamp( const char *config_dir, unsigned long timestamp )
122 BOOL ret = FALSE;
123 int fd, count;
124 char buffer[100];
125 char *file = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/.update-timestamp") );
127 if (!file) return FALSE;
128 strcpy( file, config_dir );
129 strcat( file, "/.update-timestamp" );
131 if ((fd = open( file, O_RDWR )) != -1)
133 if ((count = read( fd, buffer, sizeof(buffer) - 1 )) >= 0)
135 buffer[count] = 0;
136 if (!strncmp( buffer, "disable", sizeof("disable")-1 )) goto done;
137 if (timestamp == strtoul( buffer, NULL, 10 )) goto done;
139 lseek( fd, 0, SEEK_SET );
140 ftruncate( fd, 0 );
142 else
144 if (errno != ENOENT) goto done;
145 if ((fd = open( file, O_WRONLY | O_CREAT | O_TRUNC, 0666 )) == -1) goto done;
148 count = sprintf( buffer, "%lu\n", timestamp );
149 if (write( fd, buffer, count ) != count)
151 WINE_WARN( "failed to update timestamp in %s\n", file );
152 ftruncate( fd, 0 );
154 else ret = TRUE;
156 done:
157 if (fd != -1) close( fd );
158 HeapFree( GetProcessHeap(), 0, file );
159 return ret;
162 /* wrapper for RegSetValueExW */
163 static DWORD set_reg_value( HKEY hkey, const WCHAR *name, const WCHAR *value )
165 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, (strlenW(value) + 1) * sizeof(WCHAR) );
168 /* create the volatile hardware registry keys */
169 static void create_hardware_registry_keys(void)
171 static const WCHAR SystemW[] = {'H','a','r','d','w','a','r','e','\\',
172 'D','e','s','c','r','i','p','t','i','o','n','\\',
173 'S','y','s','t','e','m',0};
174 static const WCHAR fpuW[] = {'F','l','o','a','t','i','n','g','P','o','i','n','t','P','r','o','c','e','s','s','o','r',0};
175 static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
176 static const WCHAR FeatureSetW[] = {'F','e','a','t','u','r','e','S','e','t',0};
177 static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
178 static const WCHAR ProcessorNameStringW[] = {'P','r','o','c','e','s','s','o','r','N','a','m','e','S','t','r','i','n','g',0};
179 static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
180 static const WCHAR ARMSysidW[] = {'A','R','M',' ','p','r','o','c','e','s','s','o','r',' ','f','a','m','i','l','y',0};
181 static const WCHAR mhzKeyW[] = {'~','M','H','z',0};
182 static const WCHAR VendorIdentifierW[] = {'V','e','n','d','o','r','I','d','e','n','t','i','f','i','e','r',0};
183 static const WCHAR VenidIntelW[] = {'G','e','n','u','i','n','e','I','n','t','e','l',0};
184 /* static const WCHAR VenidAMDW[] = {'A','u','t','h','e','n','t','i','c','A','M','D',0}; */
185 static const WCHAR PercentDW[] = {'%','d',0};
186 static const WCHAR IntelCpuDescrW[] = {'x','8','6',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
187 ' ','S','t','e','p','p','i','n','g',' ','%','d',0};
188 static const WCHAR ARMCpuDescrW[] = {'A','R','M',' ','F','a','m','i','l','y',' ','7',' ','M','o','d','e','l',' ','%','X',
189 ' ','R','e','v','i','s','i','o','n',' ','%','d',0};
190 static const WCHAR IntelCpuStringW[] = {'I','n','t','e','l','(','R',')',' ','P','e','n','t','i','u','m','(','R',')',' ','4',' ',
191 'C','P','U',' ','2','.','4','0','G','H','z',0};
192 unsigned int i;
193 HKEY hkey, system_key, cpu_key, fpu_key;
194 SYSTEM_CPU_INFORMATION sci;
195 PROCESSOR_POWER_INFORMATION* power_info;
196 ULONG sizeof_power_info = sizeof(PROCESSOR_POWER_INFORMATION) * NtCurrentTeb()->Peb->NumberOfProcessors;
197 WCHAR idW[60];
199 NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
201 power_info = HeapAlloc( GetProcessHeap(), 0, sizeof_power_info );
202 if (power_info == NULL)
203 return;
204 if (NtPowerInformation( ProcessorInformation, NULL, 0, power_info, sizeof_power_info ))
205 memset( power_info, 0, sizeof_power_info );
207 /*TODO: report 64bit processors properly*/
208 switch(sci.Architecture)
210 case PROCESSOR_ARCHITECTURE_ARM:
211 sprintfW( idW, ARMCpuDescrW, sci.Level, sci.Revision );
212 break;
213 default:
214 case PROCESSOR_ARCHITECTURE_INTEL:
215 sprintfW( idW, IntelCpuDescrW, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
216 break;
219 if (RegCreateKeyExW( HKEY_LOCAL_MACHINE, SystemW, 0, NULL, REG_OPTION_VOLATILE,
220 KEY_ALL_ACCESS, NULL, &system_key, NULL ))
222 HeapFree( GetProcessHeap(), 0, power_info );
223 return;
226 switch(sci.Architecture)
228 case PROCESSOR_ARCHITECTURE_ARM:
229 set_reg_value( system_key, IdentifierW, ARMSysidW );
230 break;
231 default:
232 case PROCESSOR_ARCHITECTURE_INTEL:
233 set_reg_value( system_key, IdentifierW, SysidW );
234 break;
237 if (sci.Architecture == PROCESSOR_ARCHITECTURE_ARM ||
238 RegCreateKeyExW( system_key, fpuW, 0, NULL, REG_OPTION_VOLATILE,
239 KEY_ALL_ACCESS, NULL, &fpu_key, NULL ))
240 fpu_key = 0;
241 if (RegCreateKeyExW( system_key, cpuW, 0, NULL, REG_OPTION_VOLATILE,
242 KEY_ALL_ACCESS, NULL, &cpu_key, NULL ))
243 cpu_key = 0;
245 for (i = 0; i < NtCurrentTeb()->Peb->NumberOfProcessors; i++)
247 WCHAR numW[10];
249 sprintfW( numW, PercentDW, i );
250 if (!RegCreateKeyExW( cpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
251 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
253 RegSetValueExW( hkey, FeatureSetW, 0, REG_DWORD, (BYTE *)&sci.FeatureSet, sizeof(DWORD) );
254 set_reg_value( hkey, IdentifierW, idW );
255 /*TODO; report ARM and AMD properly*/
256 set_reg_value( hkey, ProcessorNameStringW, IntelCpuStringW );
257 set_reg_value( hkey, VendorIdentifierW, VenidIntelW );
258 RegSetValueExW( hkey, mhzKeyW, 0, REG_DWORD, (BYTE *)&power_info[i].MaxMhz, sizeof(DWORD) );
259 RegCloseKey( hkey );
261 if (sci.Architecture != PROCESSOR_ARCHITECTURE_ARM &&
262 !RegCreateKeyExW( fpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
263 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
265 set_reg_value( hkey, IdentifierW, idW );
266 RegCloseKey( hkey );
269 RegCloseKey( fpu_key );
270 RegCloseKey( cpu_key );
271 RegCloseKey( system_key );
272 HeapFree( GetProcessHeap(), 0, power_info );
276 /* create the DynData registry keys */
277 static void create_dynamic_registry_keys(void)
279 static const WCHAR StatDataW[] = {'P','e','r','f','S','t','a','t','s','\\',
280 'S','t','a','t','D','a','t','a',0};
281 static const WCHAR ConfigManagerW[] = {'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
282 'E','n','u','m',0};
283 HKEY key;
285 if (!RegCreateKeyExW( HKEY_DYN_DATA, StatDataW, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
286 RegCloseKey( key );
287 if (!RegCreateKeyExW( HKEY_DYN_DATA, ConfigManagerW, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
288 RegCloseKey( key );
291 /* create the platform-specific environment registry keys */
292 static void create_environment_registry_keys( void )
294 static const WCHAR EnvironW[] = {'S','y','s','t','e','m','\\',
295 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
296 'C','o','n','t','r','o','l','\\',
297 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
298 'E','n','v','i','r','o','n','m','e','n','t',0};
299 static const WCHAR NumProcW[] = {'N','U','M','B','E','R','_','O','F','_','P','R','O','C','E','S','S','O','R','S',0};
300 static const WCHAR ProcArchW[] = {'P','R','O','C','E','S','S','O','R','_','A','R','C','H','I','T','E','C','T','U','R','E',0};
301 static const WCHAR x86W[] = {'x','8','6',0};
302 static const WCHAR armW[] = {'A','R','M',0};
303 static const WCHAR AMD64W[] = {'A','M','D','6','4',0};
304 static const WCHAR ProcIdW[] = {'P','R','O','C','E','S','S','O','R','_','I','D','E','N','T','I','F','I','E','R',0};
305 static const WCHAR ProcLvlW[] = {'P','R','O','C','E','S','S','O','R','_','L','E','V','E','L',0};
306 static const WCHAR ProcRevW[] = {'P','R','O','C','E','S','S','O','R','_','R','E','V','I','S','I','O','N',0};
307 static const WCHAR PercentDW[] = {'%','d',0};
308 static const WCHAR Percent04XW[] = {'%','0','4','x',0};
309 static const WCHAR IntelCpuDescrW[] = {'%','s',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
310 ' ','S','t','e','p','p','i','n','g',' ','%','d',',',' ','G','e','n','u','i','n','e','I','n','t','e','l',0};
311 static const WCHAR ARMCpuDescrW[] = {'A','R','M',' ','F','a','m','i','l','y',' ','7',' ','M','o','d','e','l',' ','%','X',
312 ' ','R','e','v','i','s','i','o','n',' ','%','d',0};
314 HKEY env_key;
315 SYSTEM_CPU_INFORMATION sci;
316 WCHAR buffer[60];
317 const WCHAR *arch;
319 NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
321 if (RegCreateKeyW( HKEY_LOCAL_MACHINE, EnvironW, &env_key )) return;
323 sprintfW( buffer, PercentDW, NtCurrentTeb()->Peb->NumberOfProcessors );
324 set_reg_value( env_key, NumProcW, buffer );
326 switch(sci.Architecture)
328 case PROCESSOR_ARCHITECTURE_AMD64: arch = AMD64W; break;
329 case PROCESSOR_ARCHITECTURE_ARM: arch = armW; break;
330 default:
331 case PROCESSOR_ARCHITECTURE_INTEL: arch = x86W; break;
333 set_reg_value( env_key, ProcArchW, arch );
335 switch(sci.Architecture)
337 case PROCESSOR_ARCHITECTURE_ARM:
338 sprintfW( buffer, ARMCpuDescrW, sci.Level, sci.Revision );
339 break;
340 default:
341 case PROCESSOR_ARCHITECTURE_INTEL:
342 sprintfW( buffer, IntelCpuDescrW, arch, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
343 break;
345 set_reg_value( env_key, ProcIdW, buffer );
347 sprintfW( buffer, PercentDW, sci.Level );
348 set_reg_value( env_key, ProcLvlW, buffer );
350 /* Properly report model/stepping */
351 sprintfW( buffer, Percent04XW, sci.Revision );
352 set_reg_value( env_key, ProcRevW, buffer );
354 RegCloseKey( env_key );
357 static void create_volatile_environment_registry_key(void)
359 static const WCHAR VolatileEnvW[] = {'V','o','l','a','t','i','l','e',' ','E','n','v','i','r','o','n','m','e','n','t',0};
360 static const WCHAR AppDataW[] = {'A','P','P','D','A','T','A',0};
361 static const WCHAR ClientNameW[] = {'C','L','I','E','N','T','N','A','M','E',0};
362 static const WCHAR HomeDriveW[] = {'H','O','M','E','D','R','I','V','E',0};
363 static const WCHAR HomePathW[] = {'H','O','M','E','P','A','T','H',0};
364 static const WCHAR HomeShareW[] = {'H','O','M','E','S','H','A','R','E',0};
365 static const WCHAR LocalAppDataW[] = {'L','O','C','A','L','A','P','P','D','A','T','A',0};
366 static const WCHAR LogonServerW[] = {'L','O','G','O','N','S','E','R','V','E','R',0};
367 static const WCHAR SessionNameW[] = {'S','E','S','S','I','O','N','N','A','M','E',0};
368 static const WCHAR UserNameW[] = {'U','S','E','R','N','A','M','E',0};
369 static const WCHAR UserDomainW[] = {'U','S','E','R','D','O','M','A','I','N',0};
370 static const WCHAR UserProfileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
371 static const WCHAR ConsoleW[] = {'C','o','n','s','o','l','e',0};
372 static const WCHAR EmptyW[] = {0};
373 WCHAR path[MAX_PATH];
374 WCHAR computername[MAX_COMPUTERNAME_LENGTH + 1 + 2];
375 DWORD size;
376 HKEY hkey;
377 HRESULT hr;
379 if (RegCreateKeyExW( HKEY_CURRENT_USER, VolatileEnvW, 0, NULL, REG_OPTION_VOLATILE,
380 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
381 return;
383 hr = SHGetFolderPathW( NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path );
384 if (SUCCEEDED(hr)) set_reg_value( hkey, AppDataW, path );
386 set_reg_value( hkey, ClientNameW, ConsoleW );
388 /* Write the profile path's drive letter and directory components into
389 * HOMEDRIVE and HOMEPATH respectively. */
390 hr = SHGetFolderPathW( NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, path );
391 if (SUCCEEDED(hr))
393 set_reg_value( hkey, UserProfileW, path );
394 set_reg_value( hkey, HomePathW, path + 2 );
395 path[2] = '\0';
396 set_reg_value( hkey, HomeDriveW, path );
399 size = sizeof(path)/sizeof(path[0]);
400 if (GetUserNameW( path, &size )) set_reg_value( hkey, UserNameW, path );
402 set_reg_value( hkey, HomeShareW, EmptyW );
404 hr = SHGetFolderPathW( NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path );
405 if (SUCCEEDED(hr))
406 set_reg_value( hkey, LocalAppDataW, path );
408 size = (sizeof(computername)/sizeof(WCHAR)) - 2;
409 if (GetComputerNameW(&computername[2], &size))
411 set_reg_value( hkey, UserDomainW, &computername[2] );
412 computername[0] = computername[1] = '\\';
413 set_reg_value( hkey, LogonServerW, computername );
416 set_reg_value( hkey, SessionNameW, ConsoleW );
417 RegCloseKey( hkey );
420 /* Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
421 * Returns FALSE if there was an error, or otherwise if all is ok.
423 static BOOL wininit(void)
425 static const WCHAR nulW[] = {'N','U','L',0};
426 static const WCHAR renameW[] = {'r','e','n','a','m','e',0};
427 static const WCHAR wininitW[] = {'w','i','n','i','n','i','t','.','i','n','i',0};
428 static const WCHAR wininitbakW[] = {'w','i','n','i','n','i','t','.','b','a','k',0};
429 WCHAR initial_buffer[1024];
430 WCHAR *str, *buffer = initial_buffer;
431 DWORD size = sizeof(initial_buffer)/sizeof(WCHAR);
432 DWORD res;
434 for (;;)
436 if (!(res = GetPrivateProfileSectionW( renameW, buffer, size, wininitW ))) return TRUE;
437 if (res < size - 2) break;
438 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
439 size *= 2;
440 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
443 for (str = buffer; *str; str += strlenW(str) + 1)
445 WCHAR *value;
447 if (*str == ';') continue; /* comment */
448 if (!(value = strchrW( str, '=' ))) continue;
450 /* split the line into key and value */
451 *value++ = 0;
453 if (!lstrcmpiW( nulW, str ))
455 WINE_TRACE("Deleting file %s\n", wine_dbgstr_w(value) );
456 if( !DeleteFileW( value ) )
457 WINE_WARN("Error deleting file %s\n", wine_dbgstr_w(value) );
459 else
461 WINE_TRACE("Renaming file %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
463 if( !MoveFileExW(value, str, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) )
464 WINE_WARN("Error renaming %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
466 str = value;
469 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
471 if( !MoveFileExW( wininitW, wininitbakW, MOVEFILE_REPLACE_EXISTING) )
473 WINE_ERR("Couldn't rename wininit.ini, error %d\n", GetLastError() );
475 return FALSE;
478 return TRUE;
481 static BOOL pendingRename(void)
483 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
484 'F','i','l','e','R','e','n','a','m','e',
485 'O','p','e','r','a','t','i','o','n','s',0};
486 static const WCHAR SessionW[] = { 'S','y','s','t','e','m','\\',
487 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
488 'C','o','n','t','r','o','l','\\',
489 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
490 WCHAR *buffer=NULL;
491 const WCHAR *src=NULL, *dst=NULL;
492 DWORD dataLength=0;
493 HKEY hSession=NULL;
494 DWORD res;
496 WINE_TRACE("Entered\n");
498 if( (res=RegOpenKeyExW( HKEY_LOCAL_MACHINE, SessionW, 0, KEY_ALL_ACCESS, &hSession ))
499 !=ERROR_SUCCESS )
501 WINE_TRACE("The key was not found - skipping\n");
502 return TRUE;
505 res=RegQueryValueExW( hSession, ValueName, NULL, NULL /* The value type does not really interest us, as it is not
506 truly a REG_MULTI_SZ anyways */,
507 NULL, &dataLength );
508 if( res==ERROR_FILE_NOT_FOUND )
510 /* No value - nothing to do. Great! */
511 WINE_TRACE("Value not present - nothing to rename\n");
512 res=TRUE;
513 goto end;
516 if( res!=ERROR_SUCCESS )
518 WINE_ERR("Couldn't query value's length (%d)\n", res );
519 res=FALSE;
520 goto end;
523 buffer=HeapAlloc( GetProcessHeap(),0,dataLength );
524 if( buffer==NULL )
526 WINE_ERR("Couldn't allocate %u bytes for the value\n", dataLength );
527 res=FALSE;
528 goto end;
531 res=RegQueryValueExW( hSession, ValueName, NULL, NULL, (LPBYTE)buffer, &dataLength );
532 if( res!=ERROR_SUCCESS )
534 WINE_ERR("Couldn't query value after successfully querying before (%u),\n"
535 "please report to wine-devel@winehq.org\n", res);
536 res=FALSE;
537 goto end;
540 /* Make sure that the data is long enough and ends with two NULLs. This
541 * simplifies the code later on.
543 if( dataLength<2*sizeof(buffer[0]) ||
544 buffer[dataLength/sizeof(buffer[0])-1]!='\0' ||
545 buffer[dataLength/sizeof(buffer[0])-2]!='\0' )
547 WINE_ERR("Improper value format - doesn't end with NULL\n");
548 res=FALSE;
549 goto end;
552 for( src=buffer; (src-buffer)*sizeof(src[0])<dataLength && *src!='\0';
553 src=dst+lstrlenW(dst)+1 )
555 DWORD dwFlags=0;
557 WINE_TRACE("processing next command\n");
559 dst=src+lstrlenW(src)+1;
561 /* We need to skip the \??\ header */
562 if( src[0]=='\\' && src[1]=='?' && src[2]=='?' && src[3]=='\\' )
563 src+=4;
565 if( dst[0]=='!' )
567 dwFlags|=MOVEFILE_REPLACE_EXISTING;
568 dst++;
571 if( dst[0]=='\\' && dst[1]=='?' && dst[2]=='?' && dst[3]=='\\' )
572 dst+=4;
574 if( *dst!='\0' )
576 /* Rename the file */
577 MoveFileExW( src, dst, dwFlags );
578 } else
580 /* Delete the file or directory */
581 if (!RemoveDirectoryW( src ) && GetLastError() == ERROR_DIRECTORY) DeleteFileW( src );
585 if((res=RegDeleteValueW(hSession, ValueName))!=ERROR_SUCCESS )
587 WINE_ERR("Error deleting the value (%u)\n", GetLastError() );
588 res=FALSE;
589 } else
590 res=TRUE;
592 end:
593 HeapFree(GetProcessHeap(), 0, buffer);
595 if( hSession!=NULL )
596 RegCloseKey( hSession );
598 return res;
601 #define INVALID_RUNCMD_RETURN -1
603 * This function runs the specified command in the specified dir.
604 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
605 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
606 * [in] wait - whether to wait for the run program to finish before returning.
607 * [in] minimized - Whether to ask the program to run minimized.
609 * Returns:
610 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
611 * If wait is FALSE - returns 0 if successful.
612 * If wait is TRUE - returns the program's return value.
614 static DWORD runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
616 STARTUPINFOW si;
617 PROCESS_INFORMATION info;
618 DWORD exit_code=0;
620 memset(&si, 0, sizeof(si));
621 si.cb=sizeof(si);
622 if( minimized )
624 si.dwFlags=STARTF_USESHOWWINDOW;
625 si.wShowWindow=SW_MINIMIZE;
627 memset(&info, 0, sizeof(info));
629 if( !CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, dir, &si, &info) )
631 WINE_WARN("Failed to run command %s (%d)\n", wine_dbgstr_w(cmdline), GetLastError() );
632 return INVALID_RUNCMD_RETURN;
635 WINE_TRACE("Successfully ran command %s - Created process handle %p\n",
636 wine_dbgstr_w(cmdline), info.hProcess );
638 if(wait)
639 { /* wait for the process to exit */
640 WaitForSingleObject(info.hProcess, INFINITE);
641 GetExitCodeProcess(info.hProcess, &exit_code);
644 CloseHandle( info.hThread );
645 CloseHandle( info.hProcess );
647 return exit_code;
651 * Process a "Run" type registry key.
652 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
653 * opened.
654 * szKeyName is the key holding the actual entries.
655 * bDelete tells whether we should delete each value right before executing it.
656 * bSynchronous tells whether we should wait for the prog to complete before
657 * going on to the next prog.
659 static BOOL ProcessRunKeys( HKEY hkRoot, LPCWSTR szKeyName, BOOL bDelete,
660 BOOL bSynchronous )
662 static const WCHAR WINKEY_NAME[]={'S','o','f','t','w','a','r','e','\\',
663 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
664 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
665 HKEY hkWin, hkRun;
666 DWORD res, dispos;
667 DWORD i, nMaxCmdLine=0, nMaxValue=0;
668 WCHAR *szCmdLine=NULL;
669 WCHAR *szValue=NULL;
671 if (hkRoot==HKEY_LOCAL_MACHINE)
672 WINE_TRACE("processing %s entries under HKLM\n",wine_dbgstr_w(szKeyName) );
673 else
674 WINE_TRACE("processing %s entries under HKCU\n",wine_dbgstr_w(szKeyName) );
676 if (RegCreateKeyExW( hkRoot, WINKEY_NAME, 0, NULL, 0, KEY_READ, NULL, &hkWin, NULL ) != ERROR_SUCCESS)
677 return TRUE;
679 if ((res = RegCreateKeyExW( hkWin, szKeyName, 0, NULL, 0, bDelete ? KEY_ALL_ACCESS : KEY_READ,
680 NULL, &hkRun, &dispos ) != ERROR_SUCCESS))
682 RegCloseKey( hkWin );
683 return TRUE;
685 RegCloseKey( hkWin );
686 if (dispos == REG_CREATED_NEW_KEY) goto end;
688 if( (res=RegQueryInfoKeyW( hkRun, NULL, NULL, NULL, NULL, NULL, NULL, &i, &nMaxValue,
689 &nMaxCmdLine, NULL, NULL ))!=ERROR_SUCCESS )
690 goto end;
692 if( i==0 )
694 WINE_TRACE("No commands to execute.\n");
696 res=ERROR_SUCCESS;
697 goto end;
700 if( (szCmdLine=HeapAlloc(GetProcessHeap(),0,nMaxCmdLine))==NULL )
702 WINE_ERR("Couldn't allocate memory for the commands to be executed\n");
704 res=ERROR_NOT_ENOUGH_MEMORY;
705 goto end;
708 if( (szValue=HeapAlloc(GetProcessHeap(),0,(++nMaxValue)*sizeof(*szValue)))==NULL )
710 WINE_ERR("Couldn't allocate memory for the value names\n");
712 res=ERROR_NOT_ENOUGH_MEMORY;
713 goto end;
716 while( i>0 )
718 DWORD nValLength=nMaxValue, nDataLength=nMaxCmdLine;
719 DWORD type;
721 --i;
723 if( (res=RegEnumValueW( hkRun, i, szValue, &nValLength, 0, &type,
724 (LPBYTE)szCmdLine, &nDataLength ))!=ERROR_SUCCESS )
726 WINE_ERR("Couldn't read in value %d - %d\n", i, res );
728 continue;
731 if( bDelete && (res=RegDeleteValueW( hkRun, szValue ))!=ERROR_SUCCESS )
733 WINE_ERR("Couldn't delete value - %d, %d. Running command anyways.\n", i, res );
736 if( type!=REG_SZ )
738 WINE_ERR("Incorrect type of value #%d (%d)\n", i, type );
740 continue;
743 if( (res=runCmd(szCmdLine, NULL, bSynchronous, FALSE ))==INVALID_RUNCMD_RETURN )
745 WINE_ERR("Error running cmd %s (%d)\n", wine_dbgstr_w(szCmdLine), GetLastError() );
748 WINE_TRACE("Done processing cmd #%d\n", i);
751 res=ERROR_SUCCESS;
753 end:
754 HeapFree( GetProcessHeap(), 0, szValue );
755 HeapFree( GetProcessHeap(), 0, szCmdLine );
757 if( hkRun!=NULL )
758 RegCloseKey( hkRun );
760 WINE_TRACE("done\n");
762 return res==ERROR_SUCCESS;
766 * WFP is Windows File Protection, in NT5 and Windows 2000 it maintains a cache
767 * of known good dlls and scans through and replaces corrupted DLLs with these
768 * known good versions. The only programs that should install into this dll
769 * cache are Windows Updates and IE (which is treated like a Windows Update)
771 * Implementing this allows installing ie in win2k mode to actually install the
772 * system dlls that we expect and need
774 static int ProcessWindowsFileProtection(void)
776 static const WCHAR winlogonW[] = {'S','o','f','t','w','a','r','e','\\',
777 'M','i','c','r','o','s','o','f','t','\\',
778 'W','i','n','d','o','w','s',' ','N','T','\\',
779 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
780 'W','i','n','l','o','g','o','n',0};
781 static const WCHAR cachedirW[] = {'S','F','C','D','l','l','C','a','c','h','e','D','i','r',0};
782 static const WCHAR dllcacheW[] = {'\\','d','l','l','c','a','c','h','e','\\','*',0};
783 static const WCHAR wildcardW[] = {'\\','*',0};
784 WIN32_FIND_DATAW finddata;
785 HANDLE find_handle;
786 BOOL find_rc;
787 DWORD rc;
788 HKEY hkey;
789 LPWSTR dllcache = NULL;
791 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, winlogonW, &hkey ))
793 DWORD sz = 0;
794 if (!RegQueryValueExW( hkey, cachedirW, 0, NULL, NULL, &sz))
796 sz += sizeof(WCHAR);
797 dllcache = HeapAlloc(GetProcessHeap(),0,sz + sizeof(wildcardW));
798 RegQueryValueExW( hkey, cachedirW, 0, NULL, (LPBYTE)dllcache, &sz);
799 strcatW( dllcache, wildcardW );
802 RegCloseKey(hkey);
804 if (!dllcache)
806 DWORD sz = GetSystemDirectoryW( NULL, 0 );
807 dllcache = HeapAlloc( GetProcessHeap(), 0, sz * sizeof(WCHAR) + sizeof(dllcacheW));
808 GetSystemDirectoryW( dllcache, sz );
809 strcatW( dllcache, dllcacheW );
812 find_handle = FindFirstFileW(dllcache,&finddata);
813 dllcache[ strlenW(dllcache) - 2] = 0; /* strip off wildcard */
814 find_rc = find_handle != INVALID_HANDLE_VALUE;
815 while (find_rc)
817 static const WCHAR dotW[] = {'.',0};
818 static const WCHAR dotdotW[] = {'.','.',0};
819 WCHAR targetpath[MAX_PATH];
820 WCHAR currentpath[MAX_PATH];
821 UINT sz;
822 UINT sz2;
823 WCHAR tempfile[MAX_PATH];
825 if (strcmpW(finddata.cFileName,dotW) == 0 || strcmpW(finddata.cFileName,dotdotW) == 0)
827 find_rc = FindNextFileW(find_handle,&finddata);
828 continue;
831 sz = MAX_PATH;
832 sz2 = MAX_PATH;
833 VerFindFileW(VFFF_ISSHAREDFILE, finddata.cFileName, windowsdir,
834 windowsdir, currentpath, &sz, targetpath, &sz2);
835 sz = MAX_PATH;
836 rc = VerInstallFileW(0, finddata.cFileName, finddata.cFileName,
837 dllcache, targetpath, currentpath, tempfile, &sz);
838 if (rc != ERROR_SUCCESS)
840 WINE_WARN("WFP: %s error 0x%x\n",wine_dbgstr_w(finddata.cFileName),rc);
841 DeleteFileW(tempfile);
844 /* now delete the source file so that we don't try to install it over and over again */
845 lstrcpynW( targetpath, dllcache, MAX_PATH - 1 );
846 sz = strlenW( targetpath );
847 targetpath[sz++] = '\\';
848 lstrcpynW( targetpath + sz, finddata.cFileName, MAX_PATH - sz );
849 if (!DeleteFileW( targetpath ))
850 WINE_WARN( "failed to delete %s: error %u\n", wine_dbgstr_w(targetpath), GetLastError() );
852 find_rc = FindNextFileW(find_handle,&finddata);
854 FindClose(find_handle);
855 HeapFree(GetProcessHeap(),0,dllcache);
856 return 1;
859 static BOOL start_services_process(void)
861 static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
862 static const WCHAR services[] = {'\\','s','e','r','v','i','c','e','s','.','e','x','e',0};
863 PROCESS_INFORMATION pi;
864 STARTUPINFOW si;
865 HANDLE wait_handles[2];
866 WCHAR path[MAX_PATH];
868 if (!GetSystemDirectoryW(path, MAX_PATH - strlenW(services)))
869 return FALSE;
870 strcatW(path, services);
871 ZeroMemory(&si, sizeof(si));
872 si.cb = sizeof(si);
873 if (!CreateProcessW(path, path, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
875 WINE_ERR("Couldn't start services.exe: error %u\n", GetLastError());
876 return FALSE;
878 CloseHandle(pi.hThread);
880 wait_handles[0] = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
881 wait_handles[1] = pi.hProcess;
883 /* wait for the event to become available or the process to exit */
884 if ((WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE)) == WAIT_OBJECT_0 + 1)
886 DWORD exit_code;
887 GetExitCodeProcess(pi.hProcess, &exit_code);
888 WINE_ERR("Unexpected termination of services.exe - exit code %d\n", exit_code);
889 CloseHandle(pi.hProcess);
890 CloseHandle(wait_handles[0]);
891 return FALSE;
894 CloseHandle(pi.hProcess);
895 CloseHandle(wait_handles[0]);
896 return TRUE;
899 static INT_PTR CALLBACK wait_dlgproc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
901 switch (msg)
903 case WM_INITDIALOG:
905 WCHAR *buffer, text[1024];
906 const WCHAR *name = (WCHAR *)lp;
907 HICON icon = LoadImageW( 0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 48, 48, LR_SHARED );
908 SendDlgItemMessageW( hwnd, IDC_WAITICON, STM_SETICON, (WPARAM)icon, 0 );
909 SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_GETTEXT, 1024, (LPARAM)text );
910 buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(text) + strlenW(name) + 1) * sizeof(WCHAR) );
911 sprintfW( buffer, text, name );
912 SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_SETTEXT, 0, (LPARAM)buffer );
913 HeapFree( GetProcessHeap(), 0, buffer );
915 break;
917 return 0;
920 static HWND show_wait_window(void)
922 const char *config_dir = wine_get_config_dir();
923 WCHAR *name;
924 HWND hwnd;
925 DWORD len;
927 len = MultiByteToWideChar( CP_UNIXCP, 0, config_dir, -1, NULL, 0 );
928 name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
929 MultiByteToWideChar( CP_UNIXCP, 0, config_dir, -1, name, len );
930 hwnd = CreateDialogParamW( GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_WAITDLG), 0,
931 wait_dlgproc, (LPARAM)name );
932 ShowWindow( hwnd, SW_SHOWNORMAL );
933 HeapFree( GetProcessHeap(), 0, name );
934 return hwnd;
937 static HANDLE start_rundll32( const char *inf_path, BOOL wow64 )
939 static const WCHAR rundll[] = {'\\','r','u','n','d','l','l','3','2','.','e','x','e',0};
940 static const WCHAR setupapi[] = {' ','s','e','t','u','p','a','p','i',',',
941 'I','n','s','t','a','l','l','H','i','n','f','S','e','c','t','i','o','n',0};
942 static const WCHAR definstall[] = {' ','D','e','f','a','u','l','t','I','n','s','t','a','l','l',0};
943 static const WCHAR wowinstall[] = {' ','W','o','w','6','4','I','n','s','t','a','l','l',0};
944 static const WCHAR inf[] = {' ','1','2','8',' ','\\','\\','?','\\','u','n','i','x',0 };
946 WCHAR app[MAX_PATH + sizeof(rundll)/sizeof(WCHAR)];
947 STARTUPINFOW si;
948 PROCESS_INFORMATION pi;
949 WCHAR *buffer;
950 DWORD inf_len, cmd_len;
952 memset( &si, 0, sizeof(si) );
953 si.cb = sizeof(si);
955 if (wow64)
957 if (!GetSystemWow64DirectoryW( app, MAX_PATH )) return 0; /* not on 64-bit */
959 else GetSystemDirectoryW( app, MAX_PATH );
961 strcatW( app, rundll );
963 cmd_len = strlenW(app) * sizeof(WCHAR) + sizeof(setupapi) + sizeof(definstall) + sizeof(inf);
964 inf_len = MultiByteToWideChar( CP_UNIXCP, 0, inf_path, -1, NULL, 0 );
966 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, cmd_len + inf_len * sizeof(WCHAR) ))) return 0;
968 strcpyW( buffer, app );
969 strcatW( buffer, setupapi );
970 strcatW( buffer, wow64 ? wowinstall : definstall );
971 strcatW( buffer, inf );
972 MultiByteToWideChar( CP_UNIXCP, 0, inf_path, -1, buffer + strlenW(buffer), inf_len );
974 if (CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
975 CloseHandle( pi.hThread );
976 else
977 pi.hProcess = 0;
979 HeapFree( GetProcessHeap(), 0, buffer );
980 return pi.hProcess;
983 /* execute rundll32 on the wine.inf file if necessary */
984 static void update_wineprefix( BOOL force )
986 const char *config_dir = wine_get_config_dir();
987 char *inf_path = get_wine_inf_path();
988 int fd;
989 struct stat st;
991 if (!inf_path)
993 WINE_MESSAGE( "wine: failed to update %s, wine.inf not found\n", config_dir );
994 return;
996 if ((fd = open( inf_path, O_RDONLY )) == -1)
998 WINE_MESSAGE( "wine: failed to update %s with %s: %s\n",
999 config_dir, inf_path, strerror(errno) );
1000 goto done;
1002 fstat( fd, &st );
1003 close( fd );
1005 if (update_timestamp( config_dir, st.st_mtime ) || force)
1007 HANDLE process;
1008 DWORD count = 0;
1010 if ((process = start_rundll32( inf_path, FALSE )))
1012 HWND hwnd = show_wait_window();
1013 for (;;)
1015 MSG msg;
1016 DWORD res = MsgWaitForMultipleObjects( 1, &process, FALSE, INFINITE, QS_ALLINPUT );
1017 if (res == WAIT_OBJECT_0)
1019 CloseHandle( process );
1020 if (count++ || !(process = start_rundll32( inf_path, TRUE ))) break;
1022 else while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
1024 DestroyWindow( hwnd );
1026 WINE_MESSAGE( "wine: configuration in '%s' has been updated.\n", config_dir );
1029 done:
1030 HeapFree( GetProcessHeap(), 0, inf_path );
1033 /* Process items in the StartUp group of the user's Programs under the Start Menu. Some installers put
1034 * shell links here to restart themselves after boot. */
1035 static BOOL ProcessStartupItems(void)
1037 BOOL ret = FALSE;
1038 HRESULT hr;
1039 IMalloc *ppM = NULL;
1040 IShellFolder *psfDesktop = NULL, *psfStartup = NULL;
1041 LPITEMIDLIST pidlStartup = NULL, pidlItem;
1042 ULONG NumPIDLs;
1043 IEnumIDList *iEnumList = NULL;
1044 STRRET strret;
1045 WCHAR wszCommand[MAX_PATH];
1047 WINE_TRACE("Processing items in the StartUp folder.\n");
1049 hr = SHGetMalloc(&ppM);
1050 if (FAILED(hr))
1052 WINE_ERR("Couldn't get IMalloc object.\n");
1053 goto done;
1056 hr = SHGetDesktopFolder(&psfDesktop);
1057 if (FAILED(hr))
1059 WINE_ERR("Couldn't get desktop folder.\n");
1060 goto done;
1063 hr = SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &pidlStartup);
1064 if (FAILED(hr))
1066 WINE_TRACE("Couldn't get StartUp folder location.\n");
1067 goto done;
1070 hr = IShellFolder_BindToObject(psfDesktop, pidlStartup, NULL, &IID_IShellFolder, (LPVOID*)&psfStartup);
1071 if (FAILED(hr))
1073 WINE_TRACE("Couldn't bind IShellFolder to StartUp folder.\n");
1074 goto done;
1077 hr = IShellFolder_EnumObjects(psfStartup, NULL, SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
1078 if (FAILED(hr))
1080 WINE_TRACE("Unable to enumerate StartUp objects.\n");
1081 goto done;
1084 while (IEnumIDList_Next(iEnumList, 1, &pidlItem, &NumPIDLs) == S_OK &&
1085 (NumPIDLs) == 1)
1087 hr = IShellFolder_GetDisplayNameOf(psfStartup, pidlItem, SHGDN_FORPARSING, &strret);
1088 if (FAILED(hr))
1089 WINE_TRACE("Unable to get display name of enumeration item.\n");
1090 else
1092 hr = StrRetToBufW(&strret, pidlItem, wszCommand, MAX_PATH);
1093 if (FAILED(hr))
1094 WINE_TRACE("Unable to parse display name.\n");
1095 else
1097 HINSTANCE hinst;
1099 hinst = ShellExecuteW(NULL, NULL, wszCommand, NULL, NULL, SW_SHOWNORMAL);
1100 if (PtrToUlong(hinst) <= 32)
1101 WINE_WARN("Error %p executing command %s.\n", hinst, wine_dbgstr_w(wszCommand));
1105 IMalloc_Free(ppM, pidlItem);
1108 /* Return success */
1109 ret = TRUE;
1111 done:
1112 if (iEnumList) IEnumIDList_Release(iEnumList);
1113 if (psfStartup) IShellFolder_Release(psfStartup);
1114 if (pidlStartup) IMalloc_Free(ppM, pidlStartup);
1116 return ret;
1119 static void usage(void)
1121 WINE_MESSAGE( "Usage: wineboot [options]\n" );
1122 WINE_MESSAGE( "Options;\n" );
1123 WINE_MESSAGE( " -h,--help Display this help message\n" );
1124 WINE_MESSAGE( " -e,--end-session End the current session cleanly\n" );
1125 WINE_MESSAGE( " -f,--force Force exit for processes that don't exit cleanly\n" );
1126 WINE_MESSAGE( " -i,--init Perform initialization for first Wine instance\n" );
1127 WINE_MESSAGE( " -k,--kill Kill running processes without any cleanup\n" );
1128 WINE_MESSAGE( " -r,--restart Restart only, don't do normal startup operations\n" );
1129 WINE_MESSAGE( " -s,--shutdown Shutdown only, don't reboot\n" );
1130 WINE_MESSAGE( " -u,--update Update the wineprefix directory\n" );
1133 static const char short_options[] = "efhikrsu";
1135 static const struct option long_options[] =
1137 { "help", 0, 0, 'h' },
1138 { "end-session", 0, 0, 'e' },
1139 { "force", 0, 0, 'f' },
1140 { "init" , 0, 0, 'i' },
1141 { "kill", 0, 0, 'k' },
1142 { "restart", 0, 0, 'r' },
1143 { "shutdown", 0, 0, 's' },
1144 { "update", 0, 0, 'u' },
1145 { NULL, 0, 0, 0 }
1148 int main( int argc, char *argv[] )
1150 extern HANDLE CDECL __wine_make_process_system(void);
1151 static const WCHAR RunW[] = {'R','u','n',0};
1152 static const WCHAR RunOnceW[] = {'R','u','n','O','n','c','e',0};
1153 static const WCHAR RunServicesW[] = {'R','u','n','S','e','r','v','i','c','e','s',0};
1154 static const WCHAR RunServicesOnceW[] = {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0};
1155 static const WCHAR wineboot_eventW[] = {'_','_','w','i','n','e','b','o','o','t','_','e','v','e','n','t',0};
1157 /* First, set the current directory to SystemRoot */
1158 int optc;
1159 BOOL end_session, force, init, kill, restart, shutdown, update;
1160 HANDLE event;
1161 SECURITY_ATTRIBUTES sa;
1162 BOOL is_wow64;
1164 end_session = force = init = kill = restart = shutdown = update = FALSE;
1165 GetWindowsDirectoryW( windowsdir, MAX_PATH );
1166 if( !SetCurrentDirectoryW( windowsdir ) )
1167 WINE_ERR("Cannot set the dir to %s (%d)\n", wine_dbgstr_w(windowsdir), GetLastError() );
1169 if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
1171 STARTUPINFOW si;
1172 PROCESS_INFORMATION pi;
1173 WCHAR filename[MAX_PATH];
1174 void *redir;
1175 DWORD exit_code;
1177 memset( &si, 0, sizeof(si) );
1178 si.cb = sizeof(si);
1179 GetModuleFileNameW( 0, filename, MAX_PATH );
1181 Wow64DisableWow64FsRedirection( &redir );
1182 if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
1184 WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
1185 WaitForSingleObject( pi.hProcess, INFINITE );
1186 GetExitCodeProcess( pi.hProcess, &exit_code );
1187 ExitProcess( exit_code );
1189 else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename), GetLastError() );
1190 Wow64RevertWow64FsRedirection( redir );
1193 while ((optc = getopt_long(argc, argv, short_options, long_options, NULL )) != -1)
1195 switch(optc)
1197 case 'e': end_session = TRUE; break;
1198 case 'f': force = TRUE; break;
1199 case 'i': init = TRUE; break;
1200 case 'k': kill = TRUE; break;
1201 case 'r': restart = TRUE; break;
1202 case 's': shutdown = TRUE; break;
1203 case 'u': update = TRUE; break;
1204 case 'h': usage(); return 0;
1205 case '?': usage(); return 1;
1209 if (end_session)
1211 if (kill)
1213 if (!shutdown_all_desktops( force )) return 1;
1215 else if (!shutdown_close_windows( force )) return 1;
1218 if (kill) kill_processes( shutdown );
1220 if (shutdown) return 0;
1222 sa.nLength = sizeof(sa);
1223 sa.lpSecurityDescriptor = NULL;
1224 sa.bInheritHandle = TRUE; /* so that services.exe inherits it */
1225 event = CreateEventW( &sa, TRUE, FALSE, wineboot_eventW );
1227 ResetEvent( event ); /* in case this is a restart */
1229 create_hardware_registry_keys();
1230 create_dynamic_registry_keys();
1231 create_environment_registry_keys();
1232 wininit();
1233 pendingRename();
1235 ProcessWindowsFileProtection();
1236 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesOnceW, TRUE, FALSE );
1238 if (init || (kill && !restart))
1240 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesW, FALSE, FALSE );
1241 start_services_process();
1243 if (init || update) update_wineprefix( update );
1245 create_volatile_environment_registry_key();
1247 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunOnceW, TRUE, TRUE );
1249 if (!init && !restart)
1251 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunW, FALSE, FALSE );
1252 ProcessRunKeys( HKEY_CURRENT_USER, RunW, FALSE, FALSE );
1253 ProcessStartupItems();
1256 WINE_TRACE("Operation done\n");
1258 SetEvent( event );
1259 return 0;