Declare ExecuteCab and TranslateInfString.
[wine/multimedia.git] / programs / wineboot / wineboot.c
blobf56143ae92d8dea765d92091d10ef8366a4da0d3
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. Theses are roughly devided 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, ?semi-synchronous?, not implemented yet)
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?, no imp)
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 <stdio.h>
55 #include <windows.h>
56 #include <wine/debug.h>
58 WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
60 #define MAX_LINE_LENGTH (2*MAX_PATH+2)
62 static BOOL GetLine( HANDLE hFile, char *buf, size_t buflen )
64 unsigned int i=0;
65 DWORD r;
66 buf[0]='\0';
70 DWORD read;
71 if( !ReadFile( hFile, buf, 1, &read, NULL ) || read!=1 )
73 return FALSE;
76 } while( isspace( *buf ) );
78 while( buf[i]!='\n' && i<=buflen &&
79 ReadFile( hFile, buf+i+1, 1, &r, NULL ) )
81 ++i;
85 if( buf[i]!='\n' )
87 return FALSE;
90 if( i>0 && buf[i-1]=='\r' )
91 --i;
93 buf[i]='\0';
95 return TRUE;
98 /* Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
99 * Returns FALSE if there was an error, or otherwise if all is ok.
101 static BOOL wininit(void)
103 const char * const RENAME_FILE="wininit.ini";
104 const char * const RENAME_FILE_TO="wininit.bak";
105 const char * const RENAME_FILE_SECTION="[rename]";
106 char buffer[MAX_LINE_LENGTH];
107 HANDLE hFile;
110 hFile=CreateFileA(RENAME_FILE, GENERIC_READ,
111 FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
112 NULL );
114 if( hFile==INVALID_HANDLE_VALUE )
116 DWORD err=GetLastError();
118 if( err==ERROR_FILE_NOT_FOUND )
120 /* No file - nothing to do. Great! */
121 WINE_TRACE("Wininit.ini not present - no renaming to do\n");
123 return TRUE;
126 WINE_ERR("There was an error in reading wininit.ini file - %ld\n",
127 GetLastError() );
129 return FALSE;
132 printf("Wine is finalizing your software installation. This may take a few minutes,\n");
133 printf("though it never actually does.\n");
135 while( GetLine( hFile, buffer, sizeof(buffer) ) &&
136 lstrcmpiA(buffer,RENAME_FILE_SECTION)!=0 )
137 ; /* Read the lines until we match the rename section */
139 while( GetLine( hFile, buffer, sizeof(buffer) ) && buffer[0]!='[' )
141 /* First, make sure this is not a comment */
142 if( buffer[0]!=';' && buffer[0]!='\0' )
144 char * value;
146 value=strchr(buffer, '=');
148 if( value==NULL )
150 WINE_WARN("Line with no \"=\" in it in wininit.ini - %s\n",
151 buffer);
152 } else
154 /* split the line into key and value */
155 *(value++)='\0';
157 if( lstrcmpiA( "NUL", buffer )==0 )
159 WINE_TRACE("Deleting file \"%s\"\n", value );
160 /* A file to delete */
161 if( !DeleteFileA( value ) )
162 WINE_WARN("Error deleting file \"%s\"\n", value);
163 } else
165 WINE_TRACE("Renaming file \"%s\" to \"%s\"\n", value,
166 buffer );
168 if( !MoveFileExA(value, buffer, MOVEFILE_COPY_ALLOWED|
169 MOVEFILE_REPLACE_EXISTING) )
171 WINE_WARN("Error renaming \"%s\" to \"%s\"\n", value,
172 buffer );
179 CloseHandle( hFile );
181 if( !MoveFileExA( RENAME_FILE, RENAME_FILE_TO, MOVEFILE_REPLACE_EXISTING) )
183 WINE_ERR("Couldn't rename wininit.ini, error %ld\n", GetLastError() );
185 return FALSE;
188 return TRUE;
191 static BOOL pendingRename(void)
193 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
194 'F','i','l','e','R','e','n','a','m','e',
195 'O','p','e','r','a','t','i','o','n','s',0};
196 static const WCHAR SessionW[] = { 'S','y','s','t','e','m','\\',
197 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
198 'C','o','n','t','r','o','l','\\',
199 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
200 WCHAR *buffer=NULL;
201 const WCHAR *src=NULL, *dst=NULL;
202 DWORD dataLength=0;
203 HKEY hSession=NULL;
204 DWORD res;
206 WINE_TRACE("Entered\n");
208 if( (res=RegOpenKeyExW( HKEY_LOCAL_MACHINE, SessionW, 0, KEY_ALL_ACCESS, &hSession ))
209 !=ERROR_SUCCESS )
211 if( res==ERROR_FILE_NOT_FOUND )
213 WINE_TRACE("The key was not found - skipping\n");
214 res=TRUE;
216 else
218 WINE_ERR("Couldn't open key, error %ld\n", res );
219 res=FALSE;
222 goto end;
225 res=RegQueryValueExW( hSession, ValueName, NULL, NULL /* The value type does not really interest us, as it is not
226 truly a REG_MULTI_SZ anyways */,
227 NULL, &dataLength );
228 if( res==ERROR_FILE_NOT_FOUND )
230 /* No value - nothing to do. Great! */
231 WINE_TRACE("Value not present - nothing to rename\n");
232 res=TRUE;
233 goto end;
236 if( res!=ERROR_SUCCESS )
238 WINE_ERR("Couldn't query value's length (%ld)\n", res );
239 res=FALSE;
240 goto end;
243 buffer=malloc( dataLength );
244 if( buffer==NULL )
246 WINE_ERR("Couldn't allocate %lu bytes for the value\n", dataLength );
247 res=FALSE;
248 goto end;
251 res=RegQueryValueExW( hSession, ValueName, NULL, NULL, (LPBYTE)buffer, &dataLength );
252 if( res!=ERROR_SUCCESS )
254 WINE_ERR("Couldn't query value after successfully querying before (%lu),\n"
255 "please report to wine-devel@winehq.org\n", res);
256 res=FALSE;
257 goto end;
260 /* Make sure that the data is long enough and ends with two NULLs. This
261 * simplifies the code later on.
263 if( dataLength<2*sizeof(buffer[0]) ||
264 buffer[dataLength/sizeof(buffer[0])-1]!='\0' ||
265 buffer[dataLength/sizeof(buffer[0])-2]!='\0' )
267 WINE_ERR("Improper value format - doesn't end with NULL\n");
268 res=FALSE;
269 goto end;
272 for( src=buffer; (src-buffer)*sizeof(src[0])<dataLength && *src!='\0';
273 src=dst+lstrlenW(dst)+1 )
275 DWORD dwFlags=0;
277 WINE_TRACE("processing next command\n");
279 dst=src+lstrlenW(src)+1;
281 /* We need to skip the \??\ header */
282 if( src[0]=='\\' && src[1]=='?' && src[2]=='?' && src[3]=='\\' )
283 src+=4;
285 if( dst[0]=='!' )
287 dwFlags|=MOVEFILE_REPLACE_EXISTING;
288 dst++;
291 if( dst[0]=='\\' && dst[1]=='?' && dst[2]=='?' && dst[3]=='\\' )
292 dst+=4;
294 if( *dst!='\0' )
296 /* Rename the file */
297 MoveFileExW( src, dst, dwFlags );
298 } else
300 /* Delete the file or directory */
301 if( (res=GetFileAttributesW(src))!=INVALID_FILE_ATTRIBUTES )
303 if( (res&FILE_ATTRIBUTE_DIRECTORY)==0 )
305 /* It's a file */
306 DeleteFileW(src);
307 } else
309 /* It's a directory */
310 RemoveDirectoryW(src);
312 } else
314 WINE_ERR("couldn't get file attributes (%ld)\n", GetLastError() );
319 if((res=RegDeleteValueW(hSession, ValueName))!=ERROR_SUCCESS )
321 WINE_ERR("Error deleting the value (%lu)\n", GetLastError() );
322 res=FALSE;
323 } else
324 res=TRUE;
326 end:
327 if( buffer!=NULL )
328 free(buffer);
330 if( hSession!=NULL )
331 RegCloseKey( hSession );
333 return res;
336 enum runkeys {
337 RUNKEY_RUN, RUNKEY_RUNONCE, RUNKEY_RUNSERVICES, RUNKEY_RUNSERVICESONCE
340 const WCHAR runkeys_names[][30]=
342 {'R','u','n',0},
343 {'R','u','n','O','n','c','e',0},
344 {'R','u','n','S','e','r','v','i','c','e','s',0},
345 {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0}
348 #define INVALID_RUNCMD_RETURN -1
350 * This function runs the specified command in the specified dir.
351 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
352 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
353 * [in] wait - whether to wait for the run program to finish before returning.
354 * [in] minimized - Whether to ask the program to run minimized.
356 * Returns:
357 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
358 * If wait is FALSE - returns 0 if successful.
359 * If wait is TRUE - returns the program's return value.
361 static DWORD runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
363 STARTUPINFOW si;
364 PROCESS_INFORMATION info;
365 DWORD exit_code=0;
367 memset(&si, 0, sizeof(si));
368 si.cb=sizeof(si);
369 if( minimized )
371 si.dwFlags=STARTF_USESHOWWINDOW;
372 si.wShowWindow=SW_MINIMIZE;
374 memset(&info, 0, sizeof(info));
376 if( !CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, dir, &si, &info) )
378 WINE_ERR("Failed to run command %s (%ld)\n", wine_dbgstr_w(cmdline),
379 GetLastError() );
381 return INVALID_RUNCMD_RETURN;
384 WINE_TRACE("Successfully ran command %s - Created process handle %p\n",
385 wine_dbgstr_w(cmdline), info.hProcess );
387 if(wait)
388 { /* wait for the process to exit */
389 WaitForSingleObject(info.hProcess, INFINITE);
390 GetExitCodeProcess(info.hProcess, &exit_code);
393 CloseHandle( info.hProcess );
395 return exit_code;
399 * Process a "Run" type registry key.
400 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
401 * opened.
402 * szKeyName is the key holding the actual entries.
403 * bDelete tells whether we should delete each value right before executing it.
404 * bSynchronous tells whether we should wait for the prog to complete before
405 * going on to the next prog.
407 static BOOL ProcessRunKeys( HKEY hkRoot, LPCWSTR szKeyName, BOOL bDelete,
408 BOOL bSynchronous )
410 static const WCHAR WINKEY_NAME[]={'S','o','f','t','w','a','r','e','\\',
411 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
412 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
413 HKEY hkWin=NULL, hkRun=NULL;
414 DWORD res=ERROR_SUCCESS;
415 DWORD i, nMaxCmdLine=0, nMaxValue=0;
416 WCHAR *szCmdLine=NULL;
417 WCHAR *szValue=NULL;
419 if (hkRoot==HKEY_LOCAL_MACHINE)
420 WINE_TRACE("processing %s entries under HKLM\n",wine_dbgstr_w(szKeyName) );
421 else
422 WINE_TRACE("processing %s entries under HKCU\n",wine_dbgstr_w(szKeyName) );
424 if( (res=RegOpenKeyExW( hkRoot, WINKEY_NAME, 0, KEY_READ, &hkWin ))!=ERROR_SUCCESS )
426 WINE_ERR("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n",
427 res);
429 goto end;
432 if( (res=RegOpenKeyExW( hkWin, szKeyName, 0, bDelete?KEY_ALL_ACCESS:KEY_READ, &hkRun ))!=
433 ERROR_SUCCESS)
435 if( res==ERROR_FILE_NOT_FOUND )
437 WINE_TRACE("Key doesn't exist - nothing to be done\n");
439 res=ERROR_SUCCESS;
441 else
442 WINE_ERR("RegOpenKey failed on run key (%ld)\n", res);
444 goto end;
447 if( (res=RegQueryInfoKeyW( hkRun, NULL, NULL, NULL, NULL, NULL, NULL, &i, &nMaxValue,
448 &nMaxCmdLine, NULL, NULL ))!=ERROR_SUCCESS )
450 WINE_ERR("Couldn't query key info (%ld)\n", res );
452 goto end;
455 if( i==0 )
457 WINE_TRACE("No commands to execute.\n");
459 res=ERROR_SUCCESS;
460 goto end;
463 if( (szCmdLine=malloc(nMaxCmdLine))==NULL )
465 WINE_ERR("Couldn't allocate memory for the commands to be executed\n");
467 res=ERROR_NOT_ENOUGH_MEMORY;
468 goto end;
471 if( (szValue=malloc((++nMaxValue)*sizeof(*szValue)))==NULL )
473 WINE_ERR("Couldn't allocate memory for the value names\n");
475 res=ERROR_NOT_ENOUGH_MEMORY;
476 goto end;
479 while( i>0 )
481 DWORD nValLength=nMaxValue, nDataLength=nMaxCmdLine;
482 DWORD type;
484 --i;
486 if( (res=RegEnumValueW( hkRun, i, szValue, &nValLength, 0, &type,
487 (LPBYTE)szCmdLine, &nDataLength ))!=ERROR_SUCCESS )
489 WINE_ERR("Couldn't read in value %ld - %ld\n", i, res );
491 continue;
494 if( bDelete && (res=RegDeleteValueW( hkRun, szValue ))!=ERROR_SUCCESS )
496 WINE_ERR("Couldn't delete value - %ld, %ld. Running command anyways.\n", i, res );
499 if( type!=REG_SZ )
501 WINE_ERR("Incorrect type of value #%ld (%ld)\n", i, type );
503 continue;
506 if( (res=runCmd(szCmdLine, NULL, bSynchronous, FALSE ))==INVALID_RUNCMD_RETURN )
508 WINE_ERR("Error running cmd #%ld (%ld)\n", i, GetLastError() );
511 WINE_TRACE("Done processing cmd #%ld\n", i);
514 res=ERROR_SUCCESS;
516 end:
517 if( hkRun!=NULL )
518 RegCloseKey( hkRun );
519 if( hkWin!=NULL )
520 RegCloseKey( hkWin );
522 WINE_TRACE("done\n");
524 return res==ERROR_SUCCESS?TRUE:FALSE;
527 struct op_mask {
528 BOOL w9xonly; /* Perform only operations done on Windows 9x */
529 BOOL ntonly; /* Perform only operations done on Windows NT */
530 BOOL startup; /* Perform the operations that are performed every boot */
531 BOOL preboot; /* Perform file renames typically done before the system starts */
532 BOOL prelogin; /* Perform the operations typically done before the user logs in */
533 BOOL postlogin; /* Operations done after login */
536 static const struct op_mask SESSION_START={FALSE, FALSE, TRUE, TRUE, TRUE, TRUE},
537 SETUP={FALSE, FALSE, FALSE, TRUE, TRUE, TRUE};
538 #define DEFAULT SESSION_START
540 int main( int argc, char *argv[] )
542 struct op_mask ops; /* Which of the ops do we want to perform? */
543 /* First, set the current directory to SystemRoot */
544 TCHAR gen_path[MAX_PATH];
545 DWORD res;
547 res=GetWindowsDirectory( gen_path, sizeof(gen_path) );
549 if( res==0 )
551 WINE_ERR("Couldn't get the windows directory - error %ld\n",
552 GetLastError() );
554 return 100;
557 if( res>=sizeof(gen_path) )
559 WINE_ERR("Windows path too long (%ld)\n", res );
561 return 100;
564 if( !SetCurrentDirectory( gen_path ) )
566 WINE_ERR("Cannot set the dir to %s (%ld)\n", gen_path, GetLastError() );
568 return 100;
571 if( argc>1 )
573 switch( argv[1][0] )
575 case 'r': /* Restart */
576 ops=SETUP;
577 break;
578 case 's': /* Full start */
579 ops=SESSION_START;
580 break;
581 default:
582 ops=DEFAULT;
583 break;
585 } else
586 ops=DEFAULT;
588 /* Perform the ops by order, stopping if one fails, skipping if necessary */
589 /* Shachar: Sorry for the perl syntax */
590 res=(ops.ntonly || !ops.preboot || wininit())&&
591 (ops.w9xonly || !ops.preboot || pendingRename()) &&
592 (ops.ntonly || !ops.prelogin ||
593 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICESONCE],
594 TRUE, FALSE )) &&
595 (ops.ntonly || !ops.prelogin || !ops.startup ||
596 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICES],
597 FALSE, FALSE )) &&
598 (!ops.postlogin ||
599 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNONCE],
600 TRUE, TRUE )) &&
601 (!ops.postlogin || !ops.startup ||
602 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUN],
603 FALSE, FALSE )) &&
604 (!ops.postlogin || !ops.startup ||
605 ProcessRunKeys( HKEY_CURRENT_USER, runkeys_names[RUNKEY_RUN],
606 FALSE, FALSE ));
608 WINE_TRACE("Operation done\n");
610 return res?0:101;