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 Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /* Based on the Wine "bootup" handler application
22 * This app handles the various "hooks" windows allows for applications to perform
23 * as part of the bootstrap process. Theses are roughly devided into three types.
24 * Knowledge base articles that explain this are 137367, 179365, 232487 and 232509.
25 * Also, 119941 has some info on grpconv.exe
26 * The operations performed are (by order of execution):
28 * Preboot (prior to fully loading the Windows kernel):
29 * - wininit.exe (rename operations left in wininit.ini - Win 9x only)
30 * - PendingRenameOperations (rename operations left in the registry - Win NT+ only)
32 * Startup (before the user logs in)
33 * - Services (NT, ?semi-synchronous?, not implemented yet)
34 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce (9x, asynch)
35 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices (9x, asynch)
38 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, synch)
39 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
40 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
41 * - Startup folders (all, ?asynch?, no imp)
42 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, asynch)
44 * Somewhere in there is processing the RunOnceEx entries (also no imp)
47 * - If a pending rename registry does not start with \??\ the entry is
48 * processed anyways. I'm not sure that is the Windows behaviour.
49 * - Need to check what is the windows behaviour when trying to delete files
50 * and directories that are read-only
51 * - In the pending rename registry processing - there are no traces of the files
52 * processed (requires translations from Unicode to Ansi).
60 * Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
61 * Returns FALSE if there was an error, or otherwise if all is ok.
68 static BOOL
pendingRename()
70 static const WCHAR ValueName
[] = {'P','e','n','d','i','n','g',
71 'F','i','l','e','R','e','n','a','m','e',
72 'O','p','e','r','a','t','i','o','n','s',0};
73 static const WCHAR SessionW
[] = { 'S','y','s','t','e','m','\\',
74 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
75 'C','o','n','t','r','o','l','\\',
76 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
78 const WCHAR
*src
=NULL
, *dst
=NULL
;
85 if ((res
=RegOpenKeyExW(HKEY_LOCAL_MACHINE
, SessionW
, 0, KEY_ALL_ACCESS
, &hSession
))
88 if (res
==ERROR_FILE_NOT_FOUND
)
90 printf("The key was not found - skipping\n");
95 printf("Couldn't open key, error %ld\n", res
);
102 res
=RegQueryValueExW(hSession
, ValueName
, NULL
, NULL
/* The value type does not really interest us, as it is not
103 truely a REG_MULTI_SZ anyways */,
105 if (res
==ERROR_FILE_NOT_FOUND
)
107 /* No value - nothing to do. Great! */
108 printf("Value not present - nothing to rename\n");
113 if (res
!=ERROR_SUCCESS
)
115 printf("Couldn't query value's length (%ld)\n", res
);
120 buffer
=malloc(dataLength
);
123 printf("Couldn't allocate %lu bytes for the value\n", dataLength
);
128 res
=RegQueryValueExW(hSession
, ValueName
, NULL
, NULL
, (LPBYTE
)buffer
, &dataLength
);
129 if (res
!=ERROR_SUCCESS
)
131 printf("Couldn't query value after successfully querying before (%lu),\n"
132 "please report to wine-devel@winehq.org\n", res
);
137 /* Make sure that the data is long enough and ends with two NULLs. This
138 * simplifies the code later on.
140 if (dataLength
<2*sizeof(buffer
[0]) ||
141 buffer
[dataLength
/sizeof(buffer
[0])-1]!='\0' ||
142 buffer
[dataLength
/sizeof(buffer
[0])-2]!='\0')
144 printf("Improper value format - doesn't end with NULL\n");
149 for(src
=buffer
; (src
-buffer
)*sizeof(src
[0])<dataLength
&& *src
!='\0';
150 src
=dst
+lstrlenW(dst
)+1)
154 printf("processing next command\n");
156 dst
=src
+lstrlenW(src
)+1;
158 /* We need to skip the \??\ header */
159 if (src
[0]=='\\' && src
[1]=='?' && src
[2]=='?' && src
[3]=='\\')
164 dwFlags
|=MOVEFILE_REPLACE_EXISTING
;
168 if (dst
[0]=='\\' && dst
[1]=='?' && dst
[2]=='?' && dst
[3]=='\\')
173 /* Rename the file */
174 MoveFileExW(src
, dst
, dwFlags
);
177 /* Delete the file or directory */
178 res
= GetFileAttributesW (src
);
179 if (res
!= (DWORD
)-1)
181 if ((res
&FILE_ATTRIBUTE_DIRECTORY
)==0)
187 /* It's a directory */
188 RemoveDirectoryW(src
);
192 printf("couldn't get file attributes (%ld)\n", GetLastError());
197 if ((res
=RegDeleteValueW(hSession
, ValueName
))!=ERROR_SUCCESS
)
199 printf("Error deleting the value (%lu)\n", GetLastError());
209 RegCloseKey(hSession
);
215 RUNKEY_RUN
, RUNKEY_RUNONCE
, RUNKEY_RUNSERVICES
, RUNKEY_RUNSERVICESONCE
218 const WCHAR runkeys_names
[][30]=
221 {'R','u','n','O','n','c','e',0},
222 {'R','u','n','S','e','r','v','i','c','e','s',0},
223 {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0}
226 #define INVALID_RUNCMD_RETURN -1
228 * This function runs the specified command in the specified dir.
229 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
230 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
231 * [in] wait - whether to wait for the run program to finish before returning.
232 * [in] minimized - Whether to ask the program to run minimized.
235 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
236 * If wait is FALSE - returns 0 if successful.
237 * If wait is TRUE - returns the program's return value.
239 static int runCmd(LPWSTR cmdline
, LPCWSTR dir
, BOOL wait
, BOOL minimized
)
242 PROCESS_INFORMATION info
;
245 memset(&si
, 0, sizeof(si
));
249 si
.dwFlags
=STARTF_USESHOWWINDOW
;
250 si
.wShowWindow
=SW_MINIMIZE
;
252 memset(&info
, 0, sizeof(info
));
254 if (!CreateProcessW(NULL
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
))
256 printf("Failed to run command (%ld)\n", GetLastError());
258 return INVALID_RUNCMD_RETURN
;
261 printf("Successfully ran command\n"); //%s - Created process handle %p\n",
262 //wine_dbgstr_w(cmdline), info.hProcess);
265 { /* wait for the process to exit */
266 WaitForSingleObject(info
.hProcess
, INFINITE
);
267 GetExitCodeProcess(info
.hProcess
, &exit_code
);
270 CloseHandle(info
.hProcess
);
276 * Process a "Run" type registry key.
277 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
279 * szKeyName is the key holding the actual entries.
280 * bDelete tells whether we should delete each value right before executing it.
281 * bSynchronous tells whether we should wait for the prog to complete before
282 * going on to the next prog.
284 static BOOL
ProcessRunKeys(HKEY hkRoot
, LPCWSTR szKeyName
, BOOL bDelete
,
287 static const WCHAR WINKEY_NAME
[]={'S','o','f','t','w','a','r','e','\\',
288 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
289 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
290 HKEY hkWin
=NULL
, hkRun
=NULL
;
291 LONG res
=ERROR_SUCCESS
;
292 DWORD i
, nMaxCmdLine
=0, nMaxValue
=0;
293 WCHAR
*szCmdLine
=NULL
;
296 if (hkRoot
==HKEY_LOCAL_MACHINE
)
297 wprintf(L
"processing %s entries under HKLM\n", szKeyName
);
299 wprintf(L
"processing %s entries under HKCU\n", szKeyName
);
301 if ((res
=RegOpenKeyExW(hkRoot
, WINKEY_NAME
, 0, KEY_READ
, &hkWin
))!=ERROR_SUCCESS
)
303 printf("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n",
309 if ((res
=RegOpenKeyExW(hkWin
, szKeyName
, 0, bDelete
?KEY_ALL_ACCESS
:KEY_READ
, &hkRun
))!=
312 if (res
==ERROR_FILE_NOT_FOUND
)
314 printf("Key doesn't exist - nothing to be done\n");
319 printf("RegOpenKey failed on run key (%ld)\n", res
);
324 if ((res
=RegQueryInfoKeyW(hkRun
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &i
, &nMaxValue
,
325 &nMaxCmdLine
, NULL
, NULL
))!=ERROR_SUCCESS
)
327 printf("Couldn't query key info (%ld)\n", res
);
334 printf("No commands to execute.\n");
340 if ((szCmdLine
=malloc(nMaxCmdLine
))==NULL
)
342 printf("Couldn't allocate memory for the commands to be executed\n");
344 res
=ERROR_NOT_ENOUGH_MEMORY
;
348 if ((szValue
=malloc((++nMaxValue
)*sizeof(*szValue
)))==NULL
)
350 printf("Couldn't allocate memory for the value names\n");
353 res
=ERROR_NOT_ENOUGH_MEMORY
;
359 DWORD nValLength
=nMaxValue
, nDataLength
=nMaxCmdLine
;
364 if ((res
=RegEnumValueW(hkRun
, i
, szValue
, &nValLength
, 0, &type
,
365 (LPBYTE
)szCmdLine
, &nDataLength
))!=ERROR_SUCCESS
)
367 printf("Couldn't read in value %ld - %ld\n", i
, res
);
372 /* safe mode - force to run if prefixed with asterisk */
373 if (GetSystemMetrics(SM_CLEANBOOT
) && (szValue
[0] != L
'*')) continue;
375 if (bDelete
&& (res
=RegDeleteValueW(hkRun
, szValue
))!=ERROR_SUCCESS
)
377 printf("Couldn't delete value - %ld, %ld. Running command anyways.\n", i
, res
);
382 printf("Incorrect type of value #%ld (%ld)\n", i
, type
);
387 if ((res
=runCmd(szCmdLine
, NULL
, bSynchronous
, FALSE
))==INVALID_RUNCMD_RETURN
)
389 printf("Error running cmd #%ld (%ld)\n", i
, GetLastError());
392 printf("Done processing cmd #%ld\n", i
);
407 return res
==ERROR_SUCCESS
?TRUE
:FALSE
;
410 /// structure holding startup flags
412 BOOL w9xonly
; /* Perform only operations done on Windows 9x */
413 BOOL ntonly
; /* Perform only operations done on Windows NT */
414 BOOL startup
; /* Perform the operations that are performed every boot */
415 BOOL preboot
; /* Perform file renames typically done before the system starts */
416 BOOL prelogin
; /* Perform the operations typically done before the user logs in */
417 BOOL postlogin
; /* Operations done after login */
420 static const struct op_mask
421 SESSION_START
= {FALSE
, FALSE
, TRUE
, TRUE
, TRUE
, TRUE
},
422 SETUP
= {FALSE
, FALSE
, FALSE
, TRUE
, TRUE
, TRUE
};
423 #define DEFAULT SESSION_START
425 int startup(int argc
, const char *argv
[])
427 struct op_mask ops
; /* Which of the ops do we want to perform? */
428 /* First, set the current directory to SystemRoot */
429 TCHAR gen_path
[MAX_PATH
];
432 res
= GetWindowsDirectory(gen_path
, sizeof(gen_path
));
436 printf("Couldn't get the windows directory - error %ld\n",
442 if (res
>=sizeof(gen_path
))
444 printf("Windows path too long (%ld)\n", res
);
449 if (!SetCurrentDirectory(gen_path
))
451 wprintf(L
"Cannot set the dir to %s (%ld)\n", gen_path
, GetLastError());
460 case 'r': /* Restart */
463 case 's': /* Full start */
473 /* do not run certain items in Safe Mode */
474 if(GetSystemMetrics(SM_CLEANBOOT
)) ops
.startup
= FALSE
;
476 /* Perform the ops by order, stopping if one fails, skipping if necessary */
477 /* Shachar: Sorry for the perl syntax */
478 res
=(ops
.ntonly
|| !ops
.preboot
|| wininit()) &&
479 (ops
.w9xonly
|| !ops
.preboot
|| pendingRename()) &&
480 (ops
.ntonly
|| !ops
.prelogin
||
481 ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUNSERVICESONCE
], TRUE
, FALSE
)) &&
482 (ops
.ntonly
|| !ops
.prelogin
|| !ops
.startup
||
483 ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUNSERVICES
], FALSE
, FALSE
)) &&
485 ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUNONCE
], TRUE
, TRUE
)) &&
486 (!ops
.postlogin
|| !ops
.startup
||
487 ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUN
], FALSE
, FALSE
)) &&
488 (!ops
.postlogin
|| !ops
.startup
||
489 ProcessRunKeys(HKEY_CURRENT_USER
, runkeys_names
[RUNKEY_RUN
], FALSE
, FALSE
));
491 printf("Operation done\n");