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).
59 EXTERN_C HRESULT WINAPI
SHCreateSessionKey(REGSAM samDesired
, PHKEY phKey
);
62 * Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
63 * Returns FALSE if there was an error, or otherwise if all is ok.
70 static BOOL
pendingRename()
72 static const WCHAR ValueName
[] = {'P','e','n','d','i','n','g',
73 'F','i','l','e','R','e','n','a','m','e',
74 'O','p','e','r','a','t','i','o','n','s',0};
75 static const WCHAR SessionW
[] = { 'S','y','s','t','e','m','\\',
76 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
77 'C','o','n','t','r','o','l','\\',
78 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
80 const WCHAR
*src
=NULL
, *dst
=NULL
;
87 if ((res
=RegOpenKeyExW(HKEY_LOCAL_MACHINE
, SessionW
, 0, KEY_ALL_ACCESS
, &hSession
))
90 if (res
==ERROR_FILE_NOT_FOUND
)
92 printf("The key was not found - skipping\n");
97 printf("Couldn't open key, error %ld\n", res
);
104 res
=RegQueryValueExW(hSession
, ValueName
, NULL
, NULL
/* The value type does not really interest us, as it is not
105 truely a REG_MULTI_SZ anyways */,
107 if (res
==ERROR_FILE_NOT_FOUND
)
109 /* No value - nothing to do. Great! */
110 printf("Value not present - nothing to rename\n");
115 if (res
!=ERROR_SUCCESS
)
117 printf("Couldn't query value's length (%ld)\n", res
);
122 buffer
=malloc(dataLength
);
125 printf("Couldn't allocate %lu bytes for the value\n", dataLength
);
130 res
=RegQueryValueExW(hSession
, ValueName
, NULL
, NULL
, (LPBYTE
)buffer
, &dataLength
);
131 if (res
!=ERROR_SUCCESS
)
133 printf("Couldn't query value after successfully querying before (%lu),\n"
134 "please report to wine-devel@winehq.org\n", res
);
139 /* Make sure that the data is long enough and ends with two NULLs. This
140 * simplifies the code later on.
142 if (dataLength
<2*sizeof(buffer
[0]) ||
143 buffer
[dataLength
/sizeof(buffer
[0])-1]!='\0' ||
144 buffer
[dataLength
/sizeof(buffer
[0])-2]!='\0')
146 printf("Improper value format - doesn't end with NULL\n");
151 for(src
=buffer
; (src
-buffer
)*sizeof(src
[0])<dataLength
&& *src
!='\0';
152 src
=dst
+lstrlenW(dst
)+1)
156 printf("processing next command\n");
158 dst
=src
+lstrlenW(src
)+1;
160 /* We need to skip the \??\ header */
161 if (src
[0]=='\\' && src
[1]=='?' && src
[2]=='?' && src
[3]=='\\')
166 dwFlags
|=MOVEFILE_REPLACE_EXISTING
;
170 if (dst
[0]=='\\' && dst
[1]=='?' && dst
[2]=='?' && dst
[3]=='\\')
175 /* Rename the file */
176 MoveFileExW(src
, dst
, dwFlags
);
179 /* Delete the file or directory */
180 res
= GetFileAttributesW (src
);
181 if (res
!= (DWORD
)-1)
183 if ((res
&FILE_ATTRIBUTE_DIRECTORY
)==0)
189 /* It's a directory */
190 RemoveDirectoryW(src
);
194 printf("couldn't get file attributes (%ld)\n", GetLastError());
199 if ((res
=RegDeleteValueW(hSession
, ValueName
))!=ERROR_SUCCESS
)
201 printf("Error deleting the value (%lu)\n", GetLastError());
211 RegCloseKey(hSession
);
217 RUNKEY_RUN
, RUNKEY_RUNONCE
, RUNKEY_RUNSERVICES
, RUNKEY_RUNSERVICESONCE
220 const WCHAR runkeys_names
[][30]=
223 {'R','u','n','O','n','c','e',0},
224 {'R','u','n','S','e','r','v','i','c','e','s',0},
225 {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0}
228 #define INVALID_RUNCMD_RETURN -1
230 * This function runs the specified command in the specified dir.
231 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
232 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
233 * [in] wait - whether to wait for the run program to finish before returning.
234 * [in] minimized - Whether to ask the program to run minimized.
237 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
238 * If wait is FALSE - returns 0 if successful.
239 * If wait is TRUE - returns the program's return value.
241 static int runCmd(LPWSTR cmdline
, LPCWSTR dir
, BOOL wait
, BOOL minimized
)
244 PROCESS_INFORMATION info
;
246 WCHAR szCmdLineExp
[MAX_PATH
+1]= L
"\0";
248 ExpandEnvironmentStrings(cmdline
, szCmdLineExp
, sizeof(szCmdLineExp
));
250 memset(&si
, 0, sizeof(si
));
254 si
.dwFlags
=STARTF_USESHOWWINDOW
;
255 si
.wShowWindow
=SW_MINIMIZE
;
257 memset(&info
, 0, sizeof(info
));
259 if (!CreateProcessW(NULL
, szCmdLineExp
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
))
261 printf("Failed to run command (%ld)\n", GetLastError());
263 return INVALID_RUNCMD_RETURN
;
266 printf("Successfully ran command\n"); //%s - Created process handle %p\n",
267 //wine_dbgstr_w(szCmdLineExp), info.hProcess);
270 { /* wait for the process to exit */
271 WaitForSingleObject(info
.hProcess
, INFINITE
);
272 GetExitCodeProcess(info
.hProcess
, &exit_code
);
275 CloseHandle(info
.hThread
);
276 CloseHandle(info
.hProcess
);
282 * Process a "Run" type registry key.
283 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
285 * szKeyName is the key holding the actual entries.
286 * bDelete tells whether we should delete each value right before executing it.
287 * bSynchronous tells whether we should wait for the prog to complete before
288 * going on to the next prog.
290 static BOOL
ProcessRunKeys(HKEY hkRoot
, LPCWSTR szKeyName
, BOOL bDelete
,
293 static const WCHAR WINKEY_NAME
[]={'S','o','f','t','w','a','r','e','\\',
294 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
295 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
296 HKEY hkWin
=NULL
, hkRun
=NULL
;
297 LONG res
=ERROR_SUCCESS
;
298 DWORD i
, nMaxCmdLine
=0, nMaxValue
=0;
299 WCHAR
*szCmdLine
=NULL
;
302 if (hkRoot
==HKEY_LOCAL_MACHINE
)
303 wprintf(L
"processing %s entries under HKLM\n", szKeyName
);
305 wprintf(L
"processing %s entries under HKCU\n", szKeyName
);
307 if ((res
=RegOpenKeyExW(hkRoot
, WINKEY_NAME
, 0, KEY_READ
, &hkWin
))!=ERROR_SUCCESS
)
309 printf("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n",
315 if ((res
=RegOpenKeyExW(hkWin
, szKeyName
, 0, bDelete
?KEY_ALL_ACCESS
:KEY_READ
, &hkRun
))!=
318 if (res
==ERROR_FILE_NOT_FOUND
)
320 printf("Key doesn't exist - nothing to be done\n");
325 printf("RegOpenKey failed on run key (%ld)\n", res
);
330 if ((res
=RegQueryInfoKeyW(hkRun
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &i
, &nMaxValue
,
331 &nMaxCmdLine
, NULL
, NULL
))!=ERROR_SUCCESS
)
333 printf("Couldn't query key info (%ld)\n", res
);
340 printf("No commands to execute.\n");
346 if ((szCmdLine
=malloc(nMaxCmdLine
))==NULL
)
348 printf("Couldn't allocate memory for the commands to be executed\n");
350 res
=ERROR_NOT_ENOUGH_MEMORY
;
354 if ((szValue
=malloc((++nMaxValue
)*sizeof(*szValue
)))==NULL
)
356 printf("Couldn't allocate memory for the value names\n");
359 res
=ERROR_NOT_ENOUGH_MEMORY
;
365 DWORD nValLength
=nMaxValue
, nDataLength
=nMaxCmdLine
;
367 WCHAR szCmdLineExp
[MAX_PATH
+1]= L
"\0";
371 if ((res
=RegEnumValueW(hkRun
, i
, szValue
, &nValLength
, 0, &type
,
372 (LPBYTE
)szCmdLine
, &nDataLength
))!=ERROR_SUCCESS
)
374 printf("Couldn't read in value %ld - %ld\n", i
, res
);
379 /* safe mode - force to run if prefixed with asterisk */
380 if (GetSystemMetrics(SM_CLEANBOOT
) && (szValue
[0] != L
'*')) continue;
382 if (bDelete
&& (res
=RegDeleteValueW(hkRun
, szValue
))!=ERROR_SUCCESS
)
384 printf("Couldn't delete value - %ld, %ld. Running command anyways.\n", i
, res
);
389 printf("Incorrect type of value #%ld (%ld)\n", i
, type
);
394 ExpandEnvironmentStrings(szCmdLine
, szCmdLineExp
, sizeof(szCmdLineExp
));
395 if ((res
=runCmd(szCmdLineExp
, NULL
, bSynchronous
, FALSE
))==INVALID_RUNCMD_RETURN
)
397 printf("Error running cmd #%ld (%ld)\n", i
, GetLastError());
400 printf("Done processing cmd #%ld\n", i
);
415 return res
==ERROR_SUCCESS
?TRUE
:FALSE
;
418 /// structure holding startup flags
420 BOOL w9xonly
; /* Perform only operations done on Windows 9x */
421 BOOL ntonly
; /* Perform only operations done on Windows NT */
422 BOOL startup
; /* Perform the operations that are performed every boot */
423 BOOL preboot
; /* Perform file renames typically done before the system starts */
424 BOOL prelogin
; /* Perform the operations typically done before the user logs in */
425 BOOL postlogin
; /* Operations done after login */
428 static const struct op_mask
429 SESSION_START
= {FALSE
, FALSE
, TRUE
, TRUE
, TRUE
, TRUE
},
430 SETUP
= {FALSE
, FALSE
, FALSE
, TRUE
, TRUE
, TRUE
};
431 #define DEFAULT SESSION_START
433 int startup(int argc
, const char *argv
[])
435 struct op_mask ops
; /* Which of the ops do we want to perform? */
436 /* First, set the current directory to SystemRoot */
437 TCHAR gen_path
[MAX_PATH
];
439 HKEY hSessionKey
, hKey
;
442 res
= GetWindowsDirectory(gen_path
, sizeof(gen_path
));
446 printf("Couldn't get the windows directory - error %ld\n",
452 if (!SetCurrentDirectory(gen_path
))
454 wprintf(L
"Cannot set the dir to %s (%ld)\n", gen_path
, GetLastError());
459 hr
= SHCreateSessionKey(KEY_WRITE
, &hSessionKey
);
465 Error
= RegCreateKeyEx(hSessionKey
, L
"StartupHasBeenRun", 0, NULL
, REG_OPTION_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, &dwDisp
);
466 RegCloseKey(hSessionKey
);
467 if (Error
== ERROR_SUCCESS
)
470 if (dwDisp
== REG_OPENED_EXISTING_KEY
)
472 /* Startup programs has already been run */
482 case 'r': /* Restart */
485 case 's': /* Full start */
495 /* do not run certain items in Safe Mode */
496 if(GetSystemMetrics(SM_CLEANBOOT
)) ops
.startup
= FALSE
;
498 /* Perform the ops by order, stopping if one fails, skipping if necessary */
499 /* Shachar: Sorry for the perl syntax */
501 if (res
&& !ops
.ntonly
&& ops
.preboot
)
503 if (res
&& !ops
.w9xonly
&& ops
.preboot
)
504 res
= pendingRename();
505 if (res
&& !ops
.ntonly
&& ops
.prelogin
)
506 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUNSERVICESONCE
], TRUE
, FALSE
);
507 if (res
&& !ops
.ntonly
&& ops
.prelogin
&& ops
.startup
)
508 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUNSERVICES
], FALSE
, FALSE
);
509 if (res
&& ops
.postlogin
)
510 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUNONCE
], TRUE
, TRUE
);
511 if (res
&& ops
.postlogin
&& ops
.startup
)
512 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, runkeys_names
[RUNKEY_RUN
], FALSE
, FALSE
);
513 if (res
&& ops
.postlogin
&& ops
.startup
)
514 res
= ProcessRunKeys(HKEY_CURRENT_USER
, runkeys_names
[RUNKEY_RUN
], FALSE
, FALSE
);
515 if (res
&& ops
.postlogin
&& ops
.startup
)
516 res
= ProcessRunKeys(HKEY_CURRENT_USER
, runkeys_names
[RUNKEY_RUNONCE
], TRUE
, FALSE
);
518 printf("Operation done\n");
520 return res
? 0 : 101;