Release 3.8.
[wine.git] / programs / wineboot / wineboot.c
blob2a65b9e8b368195f90805e1dea01f0431a252837
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 extern BOOL shutdown_close_windows( BOOL force );
89 extern BOOL shutdown_all_desktops( BOOL force );
90 extern void kill_processes( BOOL kill_desktop );
92 static WCHAR windowsdir[MAX_PATH];
94 /* retrieve the (unix) path to the wine.inf file */
95 static char *get_wine_inf_path(void)
97 const char *build_dir, *data_dir;
98 char *name = NULL;
100 if ((data_dir = wine_get_data_dir()))
102 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(data_dir) + sizeof("/wine.inf") )))
103 return NULL;
104 strcpy( name, data_dir );
105 strcat( name, "/wine.inf" );
107 else if ((build_dir = wine_get_build_dir()))
109 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/loader/wine.inf") )))
110 return NULL;
111 strcpy( name, build_dir );
112 strcat( name, "/loader/wine.inf" );
114 return name;
117 /* update the timestamp if different from the reference time */
118 static BOOL update_timestamp( const char *config_dir, unsigned long timestamp )
120 BOOL ret = FALSE;
121 int fd, count;
122 char buffer[100];
123 char *file = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/.update-timestamp") );
125 if (!file) return FALSE;
126 strcpy( file, config_dir );
127 strcat( file, "/.update-timestamp" );
129 if ((fd = open( file, O_RDWR )) != -1)
131 if ((count = read( fd, buffer, sizeof(buffer) - 1 )) >= 0)
133 buffer[count] = 0;
134 if (!strncmp( buffer, "disable", sizeof("disable")-1 )) goto done;
135 if (timestamp == strtoul( buffer, NULL, 10 )) goto done;
137 lseek( fd, 0, SEEK_SET );
138 ftruncate( fd, 0 );
140 else
142 if (errno != ENOENT) goto done;
143 if ((fd = open( file, O_WRONLY | O_CREAT | O_TRUNC, 0666 )) == -1) goto done;
146 count = sprintf( buffer, "%lu\n", timestamp );
147 if (write( fd, buffer, count ) != count)
149 WINE_WARN( "failed to update timestamp in %s\n", file );
150 ftruncate( fd, 0 );
152 else ret = TRUE;
154 done:
155 if (fd != -1) close( fd );
156 HeapFree( GetProcessHeap(), 0, file );
157 return ret;
160 /* wrapper for RegSetValueExW */
161 static DWORD set_reg_value( HKEY hkey, const WCHAR *name, const WCHAR *value )
163 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, (strlenW(value) + 1) * sizeof(WCHAR) );
166 /* create the volatile hardware registry keys */
167 static void create_hardware_registry_keys(void)
169 static const WCHAR SystemW[] = {'H','a','r','d','w','a','r','e','\\',
170 'D','e','s','c','r','i','p','t','i','o','n','\\',
171 'S','y','s','t','e','m',0};
172 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};
173 static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
174 static const WCHAR FeatureSetW[] = {'F','e','a','t','u','r','e','S','e','t',0};
175 static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
176 static const WCHAR ProcessorNameStringW[] = {'P','r','o','c','e','s','s','o','r','N','a','m','e','S','t','r','i','n','g',0};
177 static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
178 static const WCHAR ARMSysidW[] = {'A','R','M',' ','p','r','o','c','e','s','s','o','r',' ','f','a','m','i','l','y',0};
179 static const WCHAR mhzKeyW[] = {'~','M','H','z',0};
180 static const WCHAR VendorIdentifierW[] = {'V','e','n','d','o','r','I','d','e','n','t','i','f','i','e','r',0};
181 static const WCHAR VenidIntelW[] = {'G','e','n','u','i','n','e','I','n','t','e','l',0};
182 /* static const WCHAR VenidAMDW[] = {'A','u','t','h','e','n','t','i','c','A','M','D',0}; */
183 static const WCHAR PercentDW[] = {'%','d',0};
184 static const WCHAR IntelCpuDescrW[] = {'x','8','6',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
185 ' ','S','t','e','p','p','i','n','g',' ','%','d',0};
186 static const WCHAR ARMCpuDescrW[] = {'A','R','M',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
187 ' ','R','e','v','i','s','i','o','n',' ','%','d',0};
188 static const WCHAR IntelCpuStringW[] = {'I','n','t','e','l','(','R',')',' ','P','e','n','t','i','u','m','(','R',')',' ','4',' ',
189 'C','P','U',' ','2','.','4','0','G','H','z',0};
190 unsigned int i;
191 HKEY hkey, system_key, cpu_key, fpu_key;
192 SYSTEM_CPU_INFORMATION sci;
193 PROCESSOR_POWER_INFORMATION* power_info;
194 ULONG sizeof_power_info = sizeof(PROCESSOR_POWER_INFORMATION) * NtCurrentTeb()->Peb->NumberOfProcessors;
195 WCHAR idW[60];
197 NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
199 power_info = HeapAlloc( GetProcessHeap(), 0, sizeof_power_info );
200 if (power_info == NULL)
201 return;
202 if (NtPowerInformation( ProcessorInformation, NULL, 0, power_info, sizeof_power_info ))
203 memset( power_info, 0, sizeof_power_info );
205 /*TODO: report 64bit processors properly*/
206 switch(sci.Architecture)
208 case PROCESSOR_ARCHITECTURE_ARM:
209 case PROCESSOR_ARCHITECTURE_ARM64:
210 sprintfW( idW, ARMCpuDescrW, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
211 break;
212 default:
213 case PROCESSOR_ARCHITECTURE_INTEL:
214 sprintfW( idW, IntelCpuDescrW, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
215 break;
218 if (RegCreateKeyExW( HKEY_LOCAL_MACHINE, SystemW, 0, NULL, REG_OPTION_VOLATILE,
219 KEY_ALL_ACCESS, NULL, &system_key, NULL ))
221 HeapFree( GetProcessHeap(), 0, power_info );
222 return;
225 switch(sci.Architecture)
227 case PROCESSOR_ARCHITECTURE_ARM:
228 case PROCESSOR_ARCHITECTURE_ARM64:
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 sci.Architecture == PROCESSOR_ARCHITECTURE_ARM64 ||
239 RegCreateKeyExW( system_key, fpuW, 0, NULL, REG_OPTION_VOLATILE,
240 KEY_ALL_ACCESS, NULL, &fpu_key, NULL ))
241 fpu_key = 0;
242 if (RegCreateKeyExW( system_key, cpuW, 0, NULL, REG_OPTION_VOLATILE,
243 KEY_ALL_ACCESS, NULL, &cpu_key, NULL ))
244 cpu_key = 0;
246 for (i = 0; i < NtCurrentTeb()->Peb->NumberOfProcessors; i++)
248 WCHAR numW[10];
250 sprintfW( numW, PercentDW, i );
251 if (!RegCreateKeyExW( cpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
252 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
254 RegSetValueExW( hkey, FeatureSetW, 0, REG_DWORD, (BYTE *)&sci.FeatureSet, sizeof(DWORD) );
255 set_reg_value( hkey, IdentifierW, idW );
256 /*TODO; report ARM and AMD properly*/
257 set_reg_value( hkey, ProcessorNameStringW, IntelCpuStringW );
258 set_reg_value( hkey, VendorIdentifierW, VenidIntelW );
259 RegSetValueExW( hkey, mhzKeyW, 0, REG_DWORD, (BYTE *)&power_info[i].MaxMhz, sizeof(DWORD) );
260 RegCloseKey( hkey );
262 if (sci.Architecture != PROCESSOR_ARCHITECTURE_ARM &&
263 sci.Architecture != PROCESSOR_ARCHITECTURE_ARM64 &&
264 !RegCreateKeyExW( fpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
265 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
267 set_reg_value( hkey, IdentifierW, idW );
268 RegCloseKey( hkey );
271 RegCloseKey( fpu_key );
272 RegCloseKey( cpu_key );
273 RegCloseKey( system_key );
274 HeapFree( GetProcessHeap(), 0, power_info );
278 /* create the DynData registry keys */
279 static void create_dynamic_registry_keys(void)
281 static const WCHAR StatDataW[] = {'P','e','r','f','S','t','a','t','s','\\',
282 'S','t','a','t','D','a','t','a',0};
283 static const WCHAR ConfigManagerW[] = {'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
284 'E','n','u','m',0};
285 HKEY key;
287 if (!RegCreateKeyExW( HKEY_DYN_DATA, StatDataW, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
288 RegCloseKey( key );
289 if (!RegCreateKeyExW( HKEY_DYN_DATA, ConfigManagerW, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
290 RegCloseKey( key );
293 /* create the platform-specific environment registry keys */
294 static void create_environment_registry_keys( void )
296 static const WCHAR EnvironW[] = {'S','y','s','t','e','m','\\',
297 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
298 'C','o','n','t','r','o','l','\\',
299 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
300 'E','n','v','i','r','o','n','m','e','n','t',0};
301 static const WCHAR NumProcW[] = {'N','U','M','B','E','R','_','O','F','_','P','R','O','C','E','S','S','O','R','S',0};
302 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};
303 static const WCHAR x86W[] = {'x','8','6',0};
304 static const WCHAR armW[] = {'A','R','M',0};
305 static const WCHAR arm64W[] = {'A','R','M','6','4',0};
306 static const WCHAR AMD64W[] = {'A','M','D','6','4',0};
307 static const WCHAR ProcIdW[] = {'P','R','O','C','E','S','S','O','R','_','I','D','E','N','T','I','F','I','E','R',0};
308 static const WCHAR ProcLvlW[] = {'P','R','O','C','E','S','S','O','R','_','L','E','V','E','L',0};
309 static const WCHAR ProcRevW[] = {'P','R','O','C','E','S','S','O','R','_','R','E','V','I','S','I','O','N',0};
310 static const WCHAR PercentDW[] = {'%','d',0};
311 static const WCHAR Percent04XW[] = {'%','0','4','x',0};
312 static const WCHAR IntelCpuDescrW[] = {'%','s',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
313 ' ','S','t','e','p','p','i','n','g',' ','%','d',',',' ','G','e','n','u','i','n','e','I','n','t','e','l',0};
314 static const WCHAR ARMCpuDescrW[] = {'A','R','M',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
315 ' ','R','e','v','i','s','i','o','n',' ','%','d',0};
317 HKEY env_key;
318 SYSTEM_CPU_INFORMATION sci;
319 WCHAR buffer[60];
320 const WCHAR *arch;
322 if (RegCreateKeyW( HKEY_LOCAL_MACHINE, EnvironW, &env_key )) return;
324 NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
326 sprintfW( buffer, PercentDW, NtCurrentTeb()->Peb->NumberOfProcessors );
327 set_reg_value( env_key, NumProcW, buffer );
329 switch(sci.Architecture)
331 case PROCESSOR_ARCHITECTURE_AMD64: arch = AMD64W; break;
332 case PROCESSOR_ARCHITECTURE_ARM: arch = armW; break;
333 case PROCESSOR_ARCHITECTURE_ARM64: arch = arm64W; break;
334 default:
335 case PROCESSOR_ARCHITECTURE_INTEL: arch = x86W; break;
337 set_reg_value( env_key, ProcArchW, arch );
339 switch(sci.Architecture)
341 case PROCESSOR_ARCHITECTURE_ARM:
342 case PROCESSOR_ARCHITECTURE_ARM64:
343 sprintfW( buffer, ARMCpuDescrW, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
344 break;
345 default:
346 case PROCESSOR_ARCHITECTURE_INTEL:
347 sprintfW( buffer, IntelCpuDescrW, arch, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
348 break;
350 set_reg_value( env_key, ProcIdW, buffer );
352 sprintfW( buffer, PercentDW, sci.Level );
353 set_reg_value( env_key, ProcLvlW, buffer );
355 /* Properly report model/stepping */
356 sprintfW( buffer, Percent04XW, sci.Revision );
357 set_reg_value( env_key, ProcRevW, buffer );
359 RegCloseKey( env_key );
362 static void create_volatile_environment_registry_key(void)
364 static const WCHAR VolatileEnvW[] = {'V','o','l','a','t','i','l','e',' ','E','n','v','i','r','o','n','m','e','n','t',0};
365 static const WCHAR AppDataW[] = {'A','P','P','D','A','T','A',0};
366 static const WCHAR ClientNameW[] = {'C','L','I','E','N','T','N','A','M','E',0};
367 static const WCHAR HomeDriveW[] = {'H','O','M','E','D','R','I','V','E',0};
368 static const WCHAR HomePathW[] = {'H','O','M','E','P','A','T','H',0};
369 static const WCHAR HomeShareW[] = {'H','O','M','E','S','H','A','R','E',0};
370 static const WCHAR LocalAppDataW[] = {'L','O','C','A','L','A','P','P','D','A','T','A',0};
371 static const WCHAR LogonServerW[] = {'L','O','G','O','N','S','E','R','V','E','R',0};
372 static const WCHAR SessionNameW[] = {'S','E','S','S','I','O','N','N','A','M','E',0};
373 static const WCHAR UserNameW[] = {'U','S','E','R','N','A','M','E',0};
374 static const WCHAR UserDomainW[] = {'U','S','E','R','D','O','M','A','I','N',0};
375 static const WCHAR UserProfileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
376 static const WCHAR ConsoleW[] = {'C','o','n','s','o','l','e',0};
377 static const WCHAR EmptyW[] = {0};
378 WCHAR path[MAX_PATH];
379 WCHAR computername[MAX_COMPUTERNAME_LENGTH + 1 + 2];
380 DWORD size;
381 HKEY hkey;
382 HRESULT hr;
384 if (RegCreateKeyExW( HKEY_CURRENT_USER, VolatileEnvW, 0, NULL, REG_OPTION_VOLATILE,
385 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
386 return;
388 hr = SHGetFolderPathW( NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path );
389 if (SUCCEEDED(hr)) set_reg_value( hkey, AppDataW, path );
391 set_reg_value( hkey, ClientNameW, ConsoleW );
393 /* Write the profile path's drive letter and directory components into
394 * HOMEDRIVE and HOMEPATH respectively. */
395 hr = SHGetFolderPathW( NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, path );
396 if (SUCCEEDED(hr))
398 set_reg_value( hkey, UserProfileW, path );
399 set_reg_value( hkey, HomePathW, path + 2 );
400 path[2] = '\0';
401 set_reg_value( hkey, HomeDriveW, path );
404 size = sizeof(path)/sizeof(path[0]);
405 if (GetUserNameW( path, &size )) set_reg_value( hkey, UserNameW, path );
407 set_reg_value( hkey, HomeShareW, EmptyW );
409 hr = SHGetFolderPathW( NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path );
410 if (SUCCEEDED(hr))
411 set_reg_value( hkey, LocalAppDataW, path );
413 size = (sizeof(computername)/sizeof(WCHAR)) - 2;
414 if (GetComputerNameW(&computername[2], &size))
416 set_reg_value( hkey, UserDomainW, &computername[2] );
417 computername[0] = computername[1] = '\\';
418 set_reg_value( hkey, LogonServerW, computername );
421 set_reg_value( hkey, SessionNameW, ConsoleW );
422 RegCloseKey( hkey );
425 /* Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
426 * Returns FALSE if there was an error, or otherwise if all is ok.
428 static BOOL wininit(void)
430 static const WCHAR nulW[] = {'N','U','L',0};
431 static const WCHAR renameW[] = {'r','e','n','a','m','e',0};
432 static const WCHAR wininitW[] = {'w','i','n','i','n','i','t','.','i','n','i',0};
433 static const WCHAR wininitbakW[] = {'w','i','n','i','n','i','t','.','b','a','k',0};
434 WCHAR initial_buffer[1024];
435 WCHAR *str, *buffer = initial_buffer;
436 DWORD size = sizeof(initial_buffer)/sizeof(WCHAR);
437 DWORD res;
439 for (;;)
441 if (!(res = GetPrivateProfileSectionW( renameW, buffer, size, wininitW ))) return TRUE;
442 if (res < size - 2) break;
443 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
444 size *= 2;
445 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
448 for (str = buffer; *str; str += strlenW(str) + 1)
450 WCHAR *value;
452 if (*str == ';') continue; /* comment */
453 if (!(value = strchrW( str, '=' ))) continue;
455 /* split the line into key and value */
456 *value++ = 0;
458 if (!lstrcmpiW( nulW, str ))
460 WINE_TRACE("Deleting file %s\n", wine_dbgstr_w(value) );
461 if( !DeleteFileW( value ) )
462 WINE_WARN("Error deleting file %s\n", wine_dbgstr_w(value) );
464 else
466 WINE_TRACE("Renaming file %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
468 if( !MoveFileExW(value, str, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) )
469 WINE_WARN("Error renaming %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
471 str = value;
474 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
476 if( !MoveFileExW( wininitW, wininitbakW, MOVEFILE_REPLACE_EXISTING) )
478 WINE_ERR("Couldn't rename wininit.ini, error %d\n", GetLastError() );
480 return FALSE;
483 return TRUE;
486 static BOOL pendingRename(void)
488 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
489 'F','i','l','e','R','e','n','a','m','e',
490 'O','p','e','r','a','t','i','o','n','s',0};
491 static const WCHAR SessionW[] = { 'S','y','s','t','e','m','\\',
492 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
493 'C','o','n','t','r','o','l','\\',
494 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
495 WCHAR *buffer=NULL;
496 const WCHAR *src=NULL, *dst=NULL;
497 DWORD dataLength=0;
498 HKEY hSession=NULL;
499 DWORD res;
501 WINE_TRACE("Entered\n");
503 if( (res=RegOpenKeyExW( HKEY_LOCAL_MACHINE, SessionW, 0, KEY_ALL_ACCESS, &hSession ))
504 !=ERROR_SUCCESS )
506 WINE_TRACE("The key was not found - skipping\n");
507 return TRUE;
510 res=RegQueryValueExW( hSession, ValueName, NULL, NULL /* The value type does not really interest us, as it is not
511 truly a REG_MULTI_SZ anyways */,
512 NULL, &dataLength );
513 if( res==ERROR_FILE_NOT_FOUND )
515 /* No value - nothing to do. Great! */
516 WINE_TRACE("Value not present - nothing to rename\n");
517 res=TRUE;
518 goto end;
521 if( res!=ERROR_SUCCESS )
523 WINE_ERR("Couldn't query value's length (%d)\n", res );
524 res=FALSE;
525 goto end;
528 buffer=HeapAlloc( GetProcessHeap(),0,dataLength );
529 if( buffer==NULL )
531 WINE_ERR("Couldn't allocate %u bytes for the value\n", dataLength );
532 res=FALSE;
533 goto end;
536 res=RegQueryValueExW( hSession, ValueName, NULL, NULL, (LPBYTE)buffer, &dataLength );
537 if( res!=ERROR_SUCCESS )
539 WINE_ERR("Couldn't query value after successfully querying before (%u),\n"
540 "please report to wine-devel@winehq.org\n", res);
541 res=FALSE;
542 goto end;
545 /* Make sure that the data is long enough and ends with two NULLs. This
546 * simplifies the code later on.
548 if( dataLength<2*sizeof(buffer[0]) ||
549 buffer[dataLength/sizeof(buffer[0])-1]!='\0' ||
550 buffer[dataLength/sizeof(buffer[0])-2]!='\0' )
552 WINE_ERR("Improper value format - doesn't end with NULL\n");
553 res=FALSE;
554 goto end;
557 for( src=buffer; (src-buffer)*sizeof(src[0])<dataLength && *src!='\0';
558 src=dst+lstrlenW(dst)+1 )
560 DWORD dwFlags=0;
562 WINE_TRACE("processing next command\n");
564 dst=src+lstrlenW(src)+1;
566 /* We need to skip the \??\ header */
567 if( src[0]=='\\' && src[1]=='?' && src[2]=='?' && src[3]=='\\' )
568 src+=4;
570 if( dst[0]=='!' )
572 dwFlags|=MOVEFILE_REPLACE_EXISTING;
573 dst++;
576 if( dst[0]=='\\' && dst[1]=='?' && dst[2]=='?' && dst[3]=='\\' )
577 dst+=4;
579 if( *dst!='\0' )
581 /* Rename the file */
582 MoveFileExW( src, dst, dwFlags );
583 } else
585 /* Delete the file or directory */
586 if (!RemoveDirectoryW( src ) && GetLastError() == ERROR_DIRECTORY) DeleteFileW( src );
590 if((res=RegDeleteValueW(hSession, ValueName))!=ERROR_SUCCESS )
592 WINE_ERR("Error deleting the value (%u)\n", GetLastError() );
593 res=FALSE;
594 } else
595 res=TRUE;
597 end:
598 HeapFree(GetProcessHeap(), 0, buffer);
600 if( hSession!=NULL )
601 RegCloseKey( hSession );
603 return res;
606 #define INVALID_RUNCMD_RETURN -1
608 * This function runs the specified command in the specified dir.
609 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
610 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
611 * [in] wait - whether to wait for the run program to finish before returning.
612 * [in] minimized - Whether to ask the program to run minimized.
614 * Returns:
615 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
616 * If wait is FALSE - returns 0 if successful.
617 * If wait is TRUE - returns the program's return value.
619 static DWORD runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
621 STARTUPINFOW si;
622 PROCESS_INFORMATION info;
623 DWORD exit_code=0;
625 memset(&si, 0, sizeof(si));
626 si.cb=sizeof(si);
627 if( minimized )
629 si.dwFlags=STARTF_USESHOWWINDOW;
630 si.wShowWindow=SW_MINIMIZE;
632 memset(&info, 0, sizeof(info));
634 if( !CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, dir, &si, &info) )
636 WINE_WARN("Failed to run command %s (%d)\n", wine_dbgstr_w(cmdline), GetLastError() );
637 return INVALID_RUNCMD_RETURN;
640 WINE_TRACE("Successfully ran command %s - Created process handle %p\n",
641 wine_dbgstr_w(cmdline), info.hProcess );
643 if(wait)
644 { /* wait for the process to exit */
645 WaitForSingleObject(info.hProcess, INFINITE);
646 GetExitCodeProcess(info.hProcess, &exit_code);
649 CloseHandle( info.hThread );
650 CloseHandle( info.hProcess );
652 return exit_code;
656 * Process a "Run" type registry key.
657 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
658 * opened.
659 * szKeyName is the key holding the actual entries.
660 * bDelete tells whether we should delete each value right before executing it.
661 * bSynchronous tells whether we should wait for the prog to complete before
662 * going on to the next prog.
664 static BOOL ProcessRunKeys( HKEY hkRoot, LPCWSTR szKeyName, BOOL bDelete,
665 BOOL bSynchronous )
667 static const WCHAR WINKEY_NAME[]={'S','o','f','t','w','a','r','e','\\',
668 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
669 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
670 HKEY hkWin, hkRun;
671 DWORD res, dispos;
672 DWORD i, nMaxCmdLine=0, nMaxValue=0;
673 WCHAR *szCmdLine=NULL;
674 WCHAR *szValue=NULL;
676 if (hkRoot==HKEY_LOCAL_MACHINE)
677 WINE_TRACE("processing %s entries under HKLM\n",wine_dbgstr_w(szKeyName) );
678 else
679 WINE_TRACE("processing %s entries under HKCU\n",wine_dbgstr_w(szKeyName) );
681 if (RegCreateKeyExW( hkRoot, WINKEY_NAME, 0, NULL, 0, KEY_READ, NULL, &hkWin, NULL ) != ERROR_SUCCESS)
682 return TRUE;
684 if ((res = RegCreateKeyExW( hkWin, szKeyName, 0, NULL, 0, bDelete ? KEY_ALL_ACCESS : KEY_READ,
685 NULL, &hkRun, &dispos )) != ERROR_SUCCESS)
687 RegCloseKey( hkWin );
688 return TRUE;
690 RegCloseKey( hkWin );
691 if (dispos == REG_CREATED_NEW_KEY) goto end;
693 if( (res=RegQueryInfoKeyW( hkRun, NULL, NULL, NULL, NULL, NULL, NULL, &i, &nMaxValue,
694 &nMaxCmdLine, NULL, NULL ))!=ERROR_SUCCESS )
695 goto end;
697 if( i==0 )
699 WINE_TRACE("No commands to execute.\n");
701 res=ERROR_SUCCESS;
702 goto end;
705 if( (szCmdLine=HeapAlloc(GetProcessHeap(),0,nMaxCmdLine))==NULL )
707 WINE_ERR("Couldn't allocate memory for the commands to be executed\n");
709 res=ERROR_NOT_ENOUGH_MEMORY;
710 goto end;
713 if( (szValue=HeapAlloc(GetProcessHeap(),0,(++nMaxValue)*sizeof(*szValue)))==NULL )
715 WINE_ERR("Couldn't allocate memory for the value names\n");
717 res=ERROR_NOT_ENOUGH_MEMORY;
718 goto end;
721 while( i>0 )
723 DWORD nValLength=nMaxValue, nDataLength=nMaxCmdLine;
724 DWORD type;
726 --i;
728 if( (res=RegEnumValueW( hkRun, i, szValue, &nValLength, 0, &type,
729 (LPBYTE)szCmdLine, &nDataLength ))!=ERROR_SUCCESS )
731 WINE_ERR("Couldn't read in value %d - %d\n", i, res );
733 continue;
736 if( bDelete && (res=RegDeleteValueW( hkRun, szValue ))!=ERROR_SUCCESS )
738 WINE_ERR("Couldn't delete value - %d, %d. Running command anyways.\n", i, res );
741 if( type!=REG_SZ )
743 WINE_ERR("Incorrect type of value #%d (%d)\n", i, type );
745 continue;
748 if( (res=runCmd(szCmdLine, NULL, bSynchronous, FALSE ))==INVALID_RUNCMD_RETURN )
750 WINE_ERR("Error running cmd %s (%d)\n", wine_dbgstr_w(szCmdLine), GetLastError() );
753 WINE_TRACE("Done processing cmd #%d\n", i);
756 res=ERROR_SUCCESS;
758 end:
759 HeapFree( GetProcessHeap(), 0, szValue );
760 HeapFree( GetProcessHeap(), 0, szCmdLine );
762 if( hkRun!=NULL )
763 RegCloseKey( hkRun );
765 WINE_TRACE("done\n");
767 return res==ERROR_SUCCESS;
771 * WFP is Windows File Protection, in NT5 and Windows 2000 it maintains a cache
772 * of known good dlls and scans through and replaces corrupted DLLs with these
773 * known good versions. The only programs that should install into this dll
774 * cache are Windows Updates and IE (which is treated like a Windows Update)
776 * Implementing this allows installing ie in win2k mode to actually install the
777 * system dlls that we expect and need
779 static int ProcessWindowsFileProtection(void)
781 static const WCHAR winlogonW[] = {'S','o','f','t','w','a','r','e','\\',
782 'M','i','c','r','o','s','o','f','t','\\',
783 'W','i','n','d','o','w','s',' ','N','T','\\',
784 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
785 'W','i','n','l','o','g','o','n',0};
786 static const WCHAR cachedirW[] = {'S','F','C','D','l','l','C','a','c','h','e','D','i','r',0};
787 static const WCHAR dllcacheW[] = {'\\','d','l','l','c','a','c','h','e','\\','*',0};
788 static const WCHAR wildcardW[] = {'\\','*',0};
789 WIN32_FIND_DATAW finddata;
790 HANDLE find_handle;
791 BOOL find_rc;
792 DWORD rc;
793 HKEY hkey;
794 LPWSTR dllcache = NULL;
796 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, winlogonW, &hkey ))
798 DWORD sz = 0;
799 if (!RegQueryValueExW( hkey, cachedirW, 0, NULL, NULL, &sz))
801 sz += sizeof(WCHAR);
802 dllcache = HeapAlloc(GetProcessHeap(),0,sz + sizeof(wildcardW));
803 RegQueryValueExW( hkey, cachedirW, 0, NULL, (LPBYTE)dllcache, &sz);
804 strcatW( dllcache, wildcardW );
807 RegCloseKey(hkey);
809 if (!dllcache)
811 DWORD sz = GetSystemDirectoryW( NULL, 0 );
812 dllcache = HeapAlloc( GetProcessHeap(), 0, sz * sizeof(WCHAR) + sizeof(dllcacheW));
813 GetSystemDirectoryW( dllcache, sz );
814 strcatW( dllcache, dllcacheW );
817 find_handle = FindFirstFileW(dllcache,&finddata);
818 dllcache[ strlenW(dllcache) - 2] = 0; /* strip off wildcard */
819 find_rc = find_handle != INVALID_HANDLE_VALUE;
820 while (find_rc)
822 static const WCHAR dotW[] = {'.',0};
823 static const WCHAR dotdotW[] = {'.','.',0};
824 WCHAR targetpath[MAX_PATH];
825 WCHAR currentpath[MAX_PATH];
826 UINT sz;
827 UINT sz2;
828 WCHAR tempfile[MAX_PATH];
830 if (strcmpW(finddata.cFileName,dotW) == 0 || strcmpW(finddata.cFileName,dotdotW) == 0)
832 find_rc = FindNextFileW(find_handle,&finddata);
833 continue;
836 sz = MAX_PATH;
837 sz2 = MAX_PATH;
838 VerFindFileW(VFFF_ISSHAREDFILE, finddata.cFileName, windowsdir,
839 windowsdir, currentpath, &sz, targetpath, &sz2);
840 sz = MAX_PATH;
841 rc = VerInstallFileW(0, finddata.cFileName, finddata.cFileName,
842 dllcache, targetpath, currentpath, tempfile, &sz);
843 if (rc != ERROR_SUCCESS)
845 WINE_WARN("WFP: %s error 0x%x\n",wine_dbgstr_w(finddata.cFileName),rc);
846 DeleteFileW(tempfile);
849 /* now delete the source file so that we don't try to install it over and over again */
850 lstrcpynW( targetpath, dllcache, MAX_PATH - 1 );
851 sz = strlenW( targetpath );
852 targetpath[sz++] = '\\';
853 lstrcpynW( targetpath + sz, finddata.cFileName, MAX_PATH - sz );
854 if (!DeleteFileW( targetpath ))
855 WINE_WARN( "failed to delete %s: error %u\n", wine_dbgstr_w(targetpath), GetLastError() );
857 find_rc = FindNextFileW(find_handle,&finddata);
859 FindClose(find_handle);
860 HeapFree(GetProcessHeap(),0,dllcache);
861 return 1;
864 static BOOL start_services_process(void)
866 static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
867 static const WCHAR services[] = {'\\','s','e','r','v','i','c','e','s','.','e','x','e',0};
868 PROCESS_INFORMATION pi;
869 STARTUPINFOW si;
870 HANDLE wait_handles[2];
871 WCHAR path[MAX_PATH];
873 if (!GetSystemDirectoryW(path, MAX_PATH - strlenW(services)))
874 return FALSE;
875 strcatW(path, services);
876 ZeroMemory(&si, sizeof(si));
877 si.cb = sizeof(si);
878 if (!CreateProcessW(path, path, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
880 WINE_ERR("Couldn't start services.exe: error %u\n", GetLastError());
881 return FALSE;
883 CloseHandle(pi.hThread);
885 wait_handles[0] = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
886 wait_handles[1] = pi.hProcess;
888 /* wait for the event to become available or the process to exit */
889 if ((WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE)) == WAIT_OBJECT_0 + 1)
891 DWORD exit_code;
892 GetExitCodeProcess(pi.hProcess, &exit_code);
893 WINE_ERR("Unexpected termination of services.exe - exit code %d\n", exit_code);
894 CloseHandle(pi.hProcess);
895 CloseHandle(wait_handles[0]);
896 return FALSE;
899 CloseHandle(pi.hProcess);
900 CloseHandle(wait_handles[0]);
901 return TRUE;
904 static INT_PTR CALLBACK wait_dlgproc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
906 switch (msg)
908 case WM_INITDIALOG:
910 WCHAR *buffer, text[1024];
911 const WCHAR *name = (WCHAR *)lp;
912 HICON icon = LoadImageW( 0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 48, 48, LR_SHARED );
913 SendDlgItemMessageW( hwnd, IDC_WAITICON, STM_SETICON, (WPARAM)icon, 0 );
914 SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_GETTEXT, 1024, (LPARAM)text );
915 buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(text) + strlenW(name) + 1) * sizeof(WCHAR) );
916 sprintfW( buffer, text, name );
917 SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_SETTEXT, 0, (LPARAM)buffer );
918 HeapFree( GetProcessHeap(), 0, buffer );
920 break;
922 return 0;
925 static HWND show_wait_window(void)
927 const char *config_dir = wine_get_config_dir();
928 WCHAR *name;
929 HWND hwnd;
930 DWORD len;
932 len = MultiByteToWideChar( CP_UNIXCP, 0, config_dir, -1, NULL, 0 );
933 name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
934 MultiByteToWideChar( CP_UNIXCP, 0, config_dir, -1, name, len );
935 hwnd = CreateDialogParamW( GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_WAITDLG), 0,
936 wait_dlgproc, (LPARAM)name );
937 ShowWindow( hwnd, SW_SHOWNORMAL );
938 HeapFree( GetProcessHeap(), 0, name );
939 return hwnd;
942 static HANDLE start_rundll32( const char *inf_path, BOOL wow64 )
944 static const WCHAR rundll[] = {'\\','r','u','n','d','l','l','3','2','.','e','x','e',0};
945 static const WCHAR setupapi[] = {' ','s','e','t','u','p','a','p','i',',',
946 'I','n','s','t','a','l','l','H','i','n','f','S','e','c','t','i','o','n',0};
947 static const WCHAR definstall[] = {' ','D','e','f','a','u','l','t','I','n','s','t','a','l','l',0};
948 static const WCHAR wowinstall[] = {' ','W','o','w','6','4','I','n','s','t','a','l','l',0};
949 static const WCHAR inf[] = {' ','1','2','8',' ','\\','\\','?','\\','u','n','i','x',0 };
951 WCHAR app[MAX_PATH + sizeof(rundll)/sizeof(WCHAR)];
952 STARTUPINFOW si;
953 PROCESS_INFORMATION pi;
954 WCHAR *buffer;
955 DWORD inf_len, cmd_len;
957 memset( &si, 0, sizeof(si) );
958 si.cb = sizeof(si);
960 if (wow64)
962 if (!GetSystemWow64DirectoryW( app, MAX_PATH )) return 0; /* not on 64-bit */
964 else GetSystemDirectoryW( app, MAX_PATH );
966 strcatW( app, rundll );
968 cmd_len = strlenW(app) * sizeof(WCHAR) + sizeof(setupapi) + sizeof(definstall) + sizeof(inf);
969 inf_len = MultiByteToWideChar( CP_UNIXCP, 0, inf_path, -1, NULL, 0 );
971 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, cmd_len + inf_len * sizeof(WCHAR) ))) return 0;
973 strcpyW( buffer, app );
974 strcatW( buffer, setupapi );
975 strcatW( buffer, wow64 ? wowinstall : definstall );
976 strcatW( buffer, inf );
977 MultiByteToWideChar( CP_UNIXCP, 0, inf_path, -1, buffer + strlenW(buffer), inf_len );
979 if (CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
980 CloseHandle( pi.hThread );
981 else
982 pi.hProcess = 0;
984 HeapFree( GetProcessHeap(), 0, buffer );
985 return pi.hProcess;
988 /* execute rundll32 on the wine.inf file if necessary */
989 static void update_wineprefix( BOOL force )
991 const char *config_dir = wine_get_config_dir();
992 char *inf_path = get_wine_inf_path();
993 int fd;
994 struct stat st;
996 if (!inf_path)
998 WINE_MESSAGE( "wine: failed to update %s, wine.inf not found\n", config_dir );
999 return;
1001 if ((fd = open( inf_path, O_RDONLY )) == -1)
1003 WINE_MESSAGE( "wine: failed to update %s with %s: %s\n",
1004 config_dir, inf_path, strerror(errno) );
1005 goto done;
1007 fstat( fd, &st );
1008 close( fd );
1010 if (update_timestamp( config_dir, st.st_mtime ) || force)
1012 HANDLE process;
1013 DWORD count = 0;
1015 if ((process = start_rundll32( inf_path, FALSE )))
1017 HWND hwnd = show_wait_window();
1018 for (;;)
1020 MSG msg;
1021 DWORD res = MsgWaitForMultipleObjects( 1, &process, FALSE, INFINITE, QS_ALLINPUT );
1022 if (res == WAIT_OBJECT_0)
1024 CloseHandle( process );
1025 if (count++ || !(process = start_rundll32( inf_path, TRUE ))) break;
1027 else while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
1029 DestroyWindow( hwnd );
1031 WINE_MESSAGE( "wine: configuration in '%s' has been updated.\n", config_dir );
1034 done:
1035 HeapFree( GetProcessHeap(), 0, inf_path );
1038 /* Process items in the StartUp group of the user's Programs under the Start Menu. Some installers put
1039 * shell links here to restart themselves after boot. */
1040 static BOOL ProcessStartupItems(void)
1042 BOOL ret = FALSE;
1043 HRESULT hr;
1044 IMalloc *ppM = NULL;
1045 IShellFolder *psfDesktop = NULL, *psfStartup = NULL;
1046 LPITEMIDLIST pidlStartup = NULL, pidlItem;
1047 ULONG NumPIDLs;
1048 IEnumIDList *iEnumList = NULL;
1049 STRRET strret;
1050 WCHAR wszCommand[MAX_PATH];
1052 WINE_TRACE("Processing items in the StartUp folder.\n");
1054 hr = SHGetMalloc(&ppM);
1055 if (FAILED(hr))
1057 WINE_ERR("Couldn't get IMalloc object.\n");
1058 goto done;
1061 hr = SHGetDesktopFolder(&psfDesktop);
1062 if (FAILED(hr))
1064 WINE_ERR("Couldn't get desktop folder.\n");
1065 goto done;
1068 hr = SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &pidlStartup);
1069 if (FAILED(hr))
1071 WINE_TRACE("Couldn't get StartUp folder location.\n");
1072 goto done;
1075 hr = IShellFolder_BindToObject(psfDesktop, pidlStartup, NULL, &IID_IShellFolder, (LPVOID*)&psfStartup);
1076 if (FAILED(hr))
1078 WINE_TRACE("Couldn't bind IShellFolder to StartUp folder.\n");
1079 goto done;
1082 hr = IShellFolder_EnumObjects(psfStartup, NULL, SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
1083 if (FAILED(hr))
1085 WINE_TRACE("Unable to enumerate StartUp objects.\n");
1086 goto done;
1089 while (IEnumIDList_Next(iEnumList, 1, &pidlItem, &NumPIDLs) == S_OK &&
1090 (NumPIDLs) == 1)
1092 hr = IShellFolder_GetDisplayNameOf(psfStartup, pidlItem, SHGDN_FORPARSING, &strret);
1093 if (FAILED(hr))
1094 WINE_TRACE("Unable to get display name of enumeration item.\n");
1095 else
1097 hr = StrRetToBufW(&strret, pidlItem, wszCommand, MAX_PATH);
1098 if (FAILED(hr))
1099 WINE_TRACE("Unable to parse display name.\n");
1100 else
1102 HINSTANCE hinst;
1104 hinst = ShellExecuteW(NULL, NULL, wszCommand, NULL, NULL, SW_SHOWNORMAL);
1105 if (PtrToUlong(hinst) <= 32)
1106 WINE_WARN("Error %p executing command %s.\n", hinst, wine_dbgstr_w(wszCommand));
1110 IMalloc_Free(ppM, pidlItem);
1113 /* Return success */
1114 ret = TRUE;
1116 done:
1117 if (iEnumList) IEnumIDList_Release(iEnumList);
1118 if (psfStartup) IShellFolder_Release(psfStartup);
1119 if (pidlStartup) IMalloc_Free(ppM, pidlStartup);
1121 return ret;
1124 static void usage(void)
1126 WINE_MESSAGE( "Usage: wineboot [options]\n" );
1127 WINE_MESSAGE( "Options;\n" );
1128 WINE_MESSAGE( " -h,--help Display this help message\n" );
1129 WINE_MESSAGE( " -e,--end-session End the current session cleanly\n" );
1130 WINE_MESSAGE( " -f,--force Force exit for processes that don't exit cleanly\n" );
1131 WINE_MESSAGE( " -i,--init Perform initialization for first Wine instance\n" );
1132 WINE_MESSAGE( " -k,--kill Kill running processes without any cleanup\n" );
1133 WINE_MESSAGE( " -r,--restart Restart only, don't do normal startup operations\n" );
1134 WINE_MESSAGE( " -s,--shutdown Shutdown only, don't reboot\n" );
1135 WINE_MESSAGE( " -u,--update Update the wineprefix directory\n" );
1138 static const char short_options[] = "efhikrsu";
1140 static const struct option long_options[] =
1142 { "help", 0, 0, 'h' },
1143 { "end-session", 0, 0, 'e' },
1144 { "force", 0, 0, 'f' },
1145 { "init" , 0, 0, 'i' },
1146 { "kill", 0, 0, 'k' },
1147 { "restart", 0, 0, 'r' },
1148 { "shutdown", 0, 0, 's' },
1149 { "update", 0, 0, 'u' },
1150 { NULL, 0, 0, 0 }
1153 int main( int argc, char *argv[] )
1155 static const WCHAR RunW[] = {'R','u','n',0};
1156 static const WCHAR RunOnceW[] = {'R','u','n','O','n','c','e',0};
1157 static const WCHAR RunServicesW[] = {'R','u','n','S','e','r','v','i','c','e','s',0};
1158 static const WCHAR RunServicesOnceW[] = {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0};
1159 static const WCHAR wineboot_eventW[] = {'_','_','w','i','n','e','b','o','o','t','_','e','v','e','n','t',0};
1161 /* First, set the current directory to SystemRoot */
1162 int optc;
1163 BOOL end_session, force, init, kill, restart, shutdown, update;
1164 HANDLE event;
1165 SECURITY_ATTRIBUTES sa;
1166 BOOL is_wow64;
1168 end_session = force = init = kill = restart = shutdown = update = FALSE;
1169 GetWindowsDirectoryW( windowsdir, MAX_PATH );
1170 if( !SetCurrentDirectoryW( windowsdir ) )
1171 WINE_ERR("Cannot set the dir to %s (%d)\n", wine_dbgstr_w(windowsdir), GetLastError() );
1173 if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
1175 STARTUPINFOW si;
1176 PROCESS_INFORMATION pi;
1177 WCHAR filename[MAX_PATH];
1178 void *redir;
1179 DWORD exit_code;
1181 memset( &si, 0, sizeof(si) );
1182 si.cb = sizeof(si);
1183 GetModuleFileNameW( 0, filename, MAX_PATH );
1185 Wow64DisableWow64FsRedirection( &redir );
1186 if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
1188 WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
1189 WaitForSingleObject( pi.hProcess, INFINITE );
1190 GetExitCodeProcess( pi.hProcess, &exit_code );
1191 ExitProcess( exit_code );
1193 else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename), GetLastError() );
1194 Wow64RevertWow64FsRedirection( redir );
1197 while ((optc = getopt_long(argc, argv, short_options, long_options, NULL )) != -1)
1199 switch(optc)
1201 case 'e': end_session = TRUE; break;
1202 case 'f': force = TRUE; break;
1203 case 'i': init = TRUE; break;
1204 case 'k': kill = TRUE; break;
1205 case 'r': restart = TRUE; break;
1206 case 's': shutdown = TRUE; break;
1207 case 'u': update = TRUE; break;
1208 case 'h': usage(); return 0;
1209 case '?': usage(); return 1;
1213 if (end_session)
1215 if (kill)
1217 if (!shutdown_all_desktops( force )) return 1;
1219 else if (!shutdown_close_windows( force )) return 1;
1222 if (kill) kill_processes( shutdown );
1224 if (shutdown) return 0;
1226 sa.nLength = sizeof(sa);
1227 sa.lpSecurityDescriptor = NULL;
1228 sa.bInheritHandle = TRUE; /* so that services.exe inherits it */
1229 event = CreateEventW( &sa, TRUE, FALSE, wineboot_eventW );
1231 ResetEvent( event ); /* in case this is a restart */
1233 create_hardware_registry_keys();
1234 create_dynamic_registry_keys();
1235 create_environment_registry_keys();
1236 wininit();
1237 pendingRename();
1239 ProcessWindowsFileProtection();
1240 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesOnceW, TRUE, FALSE );
1242 if (init || (kill && !restart))
1244 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesW, FALSE, FALSE );
1245 start_services_process();
1247 if (init || update) update_wineprefix( update );
1249 create_volatile_environment_registry_key();
1251 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunOnceW, TRUE, TRUE );
1253 if (!init && !restart)
1255 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunW, FALSE, FALSE );
1256 ProcessRunKeys( HKEY_CURRENT_USER, RunW, FALSE, FALSE );
1257 ProcessStartupItems();
1260 WINE_TRACE("Operation done\n");
1262 SetEvent( event );
1263 return 0;