[SERVICES] Control set code clean-up

- Flush the new control set after the control set has been created.
- Make sure that we create a new control set only on the first boot after setup.
- Move the control set copy code to a separate function.
- Get rid of the global control set values.
This commit is contained in:
Eric Kohl 2018-09-29 01:31:02 +02:00
parent 5472c1db82
commit 8f939e057c
3 changed files with 157 additions and 126 deletions

View file

@ -17,24 +17,16 @@
/* GLOBALS *******************************************************************/ /* GLOBALS *******************************************************************/
static DWORD dwCurrentControlSet;
static DWORD dwDefaultControlSet;
static DWORD dwFailedControlSet;
static DWORD dwLastKnownGoodControlSet;
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
#if (_WIN32_WINNT < 0x0600)
static static
DWORD DWORD
ScmCopyKey(HKEY hDstKey, ScmCopyTree(
HKEY hSrcKey) HKEY hSrcKey,
HKEY hDstKey)
{ {
#if (_WIN32_WINNT >= 0x0600)
return RegCopyTreeW(hSrcKey,
NULL,
hDstKey);
#else
FILETIME LastWrite; FILETIME LastWrite;
DWORD dwSubKeys; DWORD dwSubKeys;
DWORD dwValues; DWORD dwValues;
@ -53,7 +45,7 @@ ScmCopyKey(HKEY hDstKey,
HKEY hSrcSubKey; HKEY hSrcSubKey;
DWORD dwError; DWORD dwError;
DPRINT("ScmCopyKey()\n"); DPRINT("ScmCopyTree()\n");
dwError = RegQueryInfoKey(hSrcKey, dwError = RegQueryInfoKey(hSrcKey,
NULL, NULL,
@ -147,8 +139,8 @@ ScmCopyKey(HKEY hDstKey,
return dwError; return dwError;
} }
dwError = ScmCopyKey(hDstSubKey, dwError = ScmCopyTree(hSrcSubKey,
hSrcSubKey); hDstSubKey);
if (dwError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
{ {
DPRINT1("Error: %lu\n", dwError); DPRINT1("Error: %lu\n", dwError);
@ -245,89 +237,93 @@ ScmCopyKey(HKEY hDstKey,
lpNameBuffer); lpNameBuffer);
} }
DPRINT("ScmCopyKey() done \n"); DPRINT("ScmCopyTree() done \n");
return ERROR_SUCCESS; return ERROR_SUCCESS;
#endif
} }
#endif
static static
BOOL DWORD
ScmGetControlSetValues(VOID) ScmGetControlSetValues(
PDWORD pdwCurrentControlSet,
PDWORD pdwDefaultControlSet,
PDWORD pdwFailedControlSet,
PDWORD pdwLastKnownGoodControlSet)
{ {
HKEY hSelectKey; HKEY hSelectKey;
DWORD dwType; DWORD dwType;
DWORD dwSize; DWORD dwSize;
LONG lError; DWORD dwError;
DPRINT("ScmGetControlSetValues() called\n"); DPRINT("ScmGetControlSetValues() called\n");
lError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"System\\Select", L"System\\Select",
0, 0,
KEY_READ, KEY_READ,
&hSelectKey); &hSelectKey);
if (lError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
return FALSE; return dwError;
dwSize = sizeof(DWORD); dwSize = sizeof(DWORD);
lError = RegQueryValueExW(hSelectKey, dwError = RegQueryValueExW(hSelectKey,
L"Current", L"Current",
0, 0,
&dwType, &dwType,
(LPBYTE)&dwCurrentControlSet, (LPBYTE)pdwCurrentControlSet,
&dwSize); &dwSize);
if (lError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
{ {
dwCurrentControlSet = 0; *pdwCurrentControlSet = 0;
} }
dwSize = sizeof(DWORD); dwSize = sizeof(DWORD);
lError = RegQueryValueExW(hSelectKey, dwError = RegQueryValueExW(hSelectKey,
L"Default", L"Default",
0, 0,
&dwType, &dwType,
(LPBYTE)&dwDefaultControlSet, (LPBYTE)pdwDefaultControlSet,
&dwSize); &dwSize);
if (lError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
{ {
dwDefaultControlSet = 0; *pdwDefaultControlSet = 0;
} }
dwSize = sizeof(DWORD); dwSize = sizeof(DWORD);
lError = RegQueryValueExW(hSelectKey, dwError = RegQueryValueExW(hSelectKey,
L"Failed", L"Failed",
0, 0,
&dwType, &dwType,
(LPBYTE)&dwFailedControlSet, (LPBYTE)pdwFailedControlSet,
&dwSize); &dwSize);
if (lError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
{ {
dwFailedControlSet = 0; *pdwFailedControlSet = 0;
} }
dwSize = sizeof(DWORD); dwSize = sizeof(DWORD);
lError = RegQueryValueExW(hSelectKey, dwError = RegQueryValueExW(hSelectKey,
L"LastKnownGood", L"LastKnownGood",
0, 0,
&dwType, &dwType,
(LPBYTE)&dwLastKnownGoodControlSet, (LPBYTE)pdwLastKnownGoodControlSet,
&dwSize); &dwSize);
if (lError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
{ {
dwLastKnownGoodControlSet = 0; *pdwLastKnownGoodControlSet = 0;
} }
RegCloseKey(hSelectKey); RegCloseKey(hSelectKey);
DPRINT("ControlSets:\n"); DPRINT("ControlSets:\n");
DPRINT("Current: %lu\n", dwCurrentControlSet); DPRINT("Current: %lu\n", *pdwCurrentControlSet);
DPRINT("Default: %lu\n", dwDefaultControlSet); DPRINT("Default: %lu\n", *pdwDefaultControlSet);
DPRINT("Failed: %lu\n", dwFailedControlSet); DPRINT("Failed: %lu\n", *pdwFailedControlSet);
DPRINT("LastKnownGood: %lu\n", dwLastKnownGoodControlSet); DPRINT("LastKnownGood: %lu\n", *pdwLastKnownGoodControlSet);
return TRUE; return dwError;
} }
@ -354,6 +350,7 @@ ScmSetLastKnownGoodControlSet(
(LPBYTE)&dwControlSet, (LPBYTE)&dwControlSet,
sizeof(dwControlSet)); sizeof(dwControlSet));
RegFlushKey(hSelectKey);
RegCloseKey(hSelectKey); RegCloseKey(hSelectKey);
return dwError; return dwError;
@ -396,91 +393,124 @@ ScmGetSetupInProgress(VOID)
} }
BOOL static
ScmUpdateControlSets(VOID) DWORD
ScmCopyControlSet(
DWORD dwSourceControlSet,
DWORD dwDestinationControlSet)
{ {
WCHAR szCurrentControlSetName[32]; WCHAR szSourceControlSetName[32];
WCHAR szNewControlSetName[32]; WCHAR szDestinationControlSetName[32];
HKEY hCurrentControlSetKey = NULL; HKEY hSourceControlSetKey = NULL;
HKEY hNewControlSetKey = NULL; HKEY hDestinationControlSetKey = NULL;
DWORD dwNewControlSet, dwDisposition; DWORD dwDisposition;
DWORD dwError; DWORD dwError;
/* Do not create a new control set when the system setup is running */ /* Create the source control set name */
if (ScmGetSetupInProgress() != 0) swprintf(szSourceControlSetName, L"SYSTEM\\ControlSet%03lu", dwSourceControlSet);
{ DPRINT("Source control set: %S\n", szSourceControlSetName);
DPRINT1("No new control set because we are in setup mode!\n");
return TRUE;
}
/* Get the control set values */ /* Create the destination control set name */
if (!ScmGetControlSetValues()) swprintf(szDestinationControlSetName, L"SYSTEM\\ControlSet%03lu", dwDestinationControlSet);
return FALSE; DPRINT("Destination control set: %S\n", szDestinationControlSetName);
/* Search for a new control set number */ /* Open the source control set key */
for (dwNewControlSet = 1; dwNewControlSet < 1000; dwNewControlSet++)
{
if ((dwNewControlSet != dwCurrentControlSet) &&
(dwNewControlSet != dwDefaultControlSet) &&
(dwNewControlSet != dwFailedControlSet) &&
(dwNewControlSet != dwLastKnownGoodControlSet))
break;
}
/* Fail if we did not find an unused control set!*/
if (dwNewControlSet >= 1000)
{
DPRINT1("Too many control sets!\n");
return FALSE;
}
/* Create the current control set name */
swprintf(szCurrentControlSetName, L"SYSTEM\\ControlSet%03lu", dwCurrentControlSet);
DPRINT("Current control set: %S\n", szCurrentControlSetName);
/* Create the new control set name */
swprintf(szNewControlSetName, L"SYSTEM\\ControlSet%03lu", dwNewControlSet);
DPRINT("New control set: %S\n", szNewControlSetName);
/* Open the current control set key */
dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
szCurrentControlSetName, szSourceControlSetName,
0, 0,
KEY_READ, KEY_READ,
&hCurrentControlSetKey); &hSourceControlSetKey);
if (dwError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
goto done; goto done;
/* Create the new control set key */ /* Create the destination control set key */
dwError = RegCreateKeyExW(HKEY_LOCAL_MACHINE, dwError = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
szNewControlSetName, szDestinationControlSetName,
0, 0,
NULL, NULL,
REG_OPTION_NON_VOLATILE, REG_OPTION_NON_VOLATILE,
KEY_WRITE, KEY_WRITE,
NULL, NULL,
&hNewControlSetKey, &hDestinationControlSetKey,
&dwDisposition); &dwDisposition);
if (dwError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
goto done; goto done;
/* Copy the current control set to the new control set */ /* Copy the source control set to the destination control set */
dwError = ScmCopyKey(hNewControlSetKey, #if (_WIN32_WINNT >= 0x0600)
hCurrentControlSetKey); dwError = RegCopyTreeW(hSourceControlSetKey,
NULL,
hDestinationControlSetKey);
#else
dwError = ScmCopyTree(hSourceControlSetKey,
hDestinationControlSetKey);
#endif
if (dwError != ERROR_SUCCESS) if (dwError != ERROR_SUCCESS)
goto done; goto done;
/* Set the new 'LastKnownGood' control set */ RegFlushKey(hDestinationControlSetKey);
dwError = ScmSetLastKnownGoodControlSet(dwNewControlSet);
done: done:
if (hNewControlSetKey != NULL) if (hDestinationControlSetKey != NULL)
RegCloseKey(hNewControlSetKey); RegCloseKey(hDestinationControlSetKey);
if (hCurrentControlSetKey != NULL) if (hSourceControlSetKey != NULL)
RegCloseKey(hCurrentControlSetKey); RegCloseKey(hSourceControlSetKey);
return (dwError == ERROR_SUCCESS); return dwError;
}
DWORD
ScmCreateLastKnownGoodControlSet(VOID)
{
DWORD dwCurrentControlSet, dwDefaultControlSet;
DWORD dwFailedControlSet, dwLastKnownGoodControlSet;
DWORD dwNewControlSet;
DWORD dwError;
/* Get the control set values */
dwError = ScmGetControlSetValues(&dwCurrentControlSet,
&dwDefaultControlSet,
&dwFailedControlSet,
&dwLastKnownGoodControlSet);
if (dwError != ERROR_SUCCESS)
return dwError;
/* First boot after setup? */
if ((ScmGetSetupInProgress() == 0) &&
(dwCurrentControlSet == dwLastKnownGoodControlSet))
{
DPRINT("First boot after setup!\n");
/* Search for a new control set number */
for (dwNewControlSet = 1; dwNewControlSet < 1000; dwNewControlSet++)
{
if ((dwNewControlSet != dwCurrentControlSet) &&
(dwNewControlSet != dwDefaultControlSet) &&
(dwNewControlSet != dwFailedControlSet) &&
(dwNewControlSet != dwLastKnownGoodControlSet))
break;
}
/* Fail if we did not find an unused control set!*/
if (dwNewControlSet >= 1000)
{
DPRINT1("Too many control sets!\n");
return ERROR_NO_MORE_ITEMS;
}
/* Copy the current control set */
dwError = ScmCopyControlSet(dwCurrentControlSet,
dwNewControlSet);
if (dwError != ERROR_SUCCESS)
return dwError;
/* Set the new 'LastKnownGood' control set */
dwError = ScmSetLastKnownGoodControlSet(dwNewControlSet);
}
return dwError;
} }
/* EOF */ /* EOF */

View file

@ -178,10 +178,11 @@ wWinMain(HINSTANCE hInstance,
/* FIXME: more initialization */ /* FIXME: more initialization */
/* Update the control sets */ /* Create the 'Last Known Good' control set */
if (!ScmUpdateControlSets()) dwError = ScmCreateLastKnownGoodControlSet();
if (dwError != ERROR_SUCCESS)
{ {
DPRINT1("SERVICES: Failed to update the control sets\n"); DPRINT1("SERVICES: Failed to create the 'Last Known Good' control set (Error %lu)\n", dwError);
goto done; goto done;
} }

View file

@ -159,8 +159,8 @@ ScmDecryptPassword(
/* controlset.c */ /* controlset.c */
BOOL DWORD
ScmUpdateControlSets(VOID); ScmCreateLastKnownGoodControlSet(VOID);
/* database.c */ /* database.c */