Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
8.2 KiB

  1. //==========================================================================;
  2. //
  3. // reboot.c
  4. //
  5. // Copyright (c) 1991-2002 Microsoft Corporation. All Rights Reserved.
  6. //
  7. // Description:
  8. //
  9. //
  10. // History:
  11. // 07/02 tsharp (Trey Sharp);
  12. //
  13. //
  14. //==========================================================================;
  15. #include "mmcpl.h"
  16. #include <windowsx.h>
  17. #include <mmsystem.h>
  18. #include <dbt.h>
  19. #include <ks.h>
  20. #include <ksmedia.h>
  21. #include <mmddkp.h>
  22. #include <mmreg.h>
  23. #include <msacm.h>
  24. #include <msacmdrv.h>
  25. #include <msacmdlg.h>
  26. #include <stdlib.h>
  27. #include "gfxui.h"
  28. #include "drivers.h"
  29. #include "advaudio.h"
  30. #include "roland.h"
  31. #include <objbase.h>
  32. #include <setupapi.h>
  33. #include <cfgmgr32.h>
  34. #include <initguid.h>
  35. #include <devguid.h>
  36. #include <memory.h>
  37. #include <commctrl.h>
  38. #include <prsht.h>
  39. #include <regstr.h>
  40. #include "trayvol.h"
  41. #include "utils.h"
  42. #include "medhelp.h"
  43. #include "start.h"
  44. /*
  45. ***************************************************************
  46. * Typedefs
  47. ***************************************************************
  48. */
  49. /*
  50. ***************************************************************
  51. * File Globals
  52. ***************************************************************
  53. */
  54. /*
  55. ***************************************************************
  56. * extern
  57. ***************************************************************
  58. */
  59. /*
  60. ***************************************************************
  61. * Prototypes
  62. ***************************************************************
  63. */
  64. BOOL PASCAL DoRebootCommand(HWND hDlg, int id, HWND hwndCtl, UINT codeNotify);
  65. STATIC void REBOOTInit(HWND hDlg)
  66. {
  67. // Add initializing code here
  68. }
  69. const static DWORD aRebootHelpIds[] = { // Context Help IDs
  70. IDC_GROUPBOX_REBOOT_1, NO_HELP,
  71. IDC_ICON_REBOOT_1, NO_HELP,
  72. IDC_TEXT_REBOOT_1, NO_HELP,
  73. IDC_TEXT_REBOOT_2, NO_HELP,
  74. 0, 0
  75. };
  76. BOOL CALLBACK RebootDlg(HWND hDlg, UINT uMsg, WPARAM wParam,
  77. LPARAM lParam)
  78. {
  79. NMHDR FAR *lpnm;
  80. switch (uMsg)
  81. {
  82. case WM_NOTIFY:
  83. {
  84. lpnm = (NMHDR FAR *)lParam;
  85. switch(lpnm->code)
  86. {
  87. case PSN_KILLACTIVE:
  88. FORWARD_WM_COMMAND(hDlg, IDOK, 0, 0, SendMessage);
  89. break;
  90. case PSN_APPLY:
  91. FORWARD_WM_COMMAND(hDlg, ID_APPLY, 0, 0, SendMessage);
  92. break;
  93. case PSN_SETACTIVE:
  94. FORWARD_WM_COMMAND(hDlg, ID_INIT, 0, 0, SendMessage);
  95. break;
  96. case PSN_RESET:
  97. FORWARD_WM_COMMAND(hDlg, IDCANCEL, 0, 0, SendMessage);
  98. break;
  99. }
  100. }
  101. break;
  102. case WM_INITDIALOG:
  103. {
  104. REBOOTInit(hDlg);
  105. }
  106. break;
  107. case WM_DESTROY:
  108. {
  109. }
  110. break;
  111. case WM_CONTEXTMENU:
  112. {
  113. WinHelp ((HWND) wParam, NULL, HELP_CONTEXTMENU, (DWORD_PTR) (LPSTR) aRebootHelpIds);
  114. return TRUE;
  115. }
  116. break;
  117. case WM_HELP:
  118. {
  119. LPHELPINFO lphi = (LPVOID) lParam;
  120. WinHelp (lphi->hItemHandle, NULL, HELP_WM_HELP, (DWORD_PTR) (LPSTR) aRebootHelpIds);
  121. return TRUE;
  122. }
  123. break;
  124. case WM_COMMAND:
  125. {
  126. HANDLE_WM_COMMAND(hDlg, wParam, lParam, DoRebootCommand);
  127. }
  128. break;
  129. default:
  130. {
  131. }
  132. break;
  133. }
  134. return FALSE;
  135. }
  136. BOOL PASCAL DoRebootCommand(HWND hDlg, int id, HWND hwndCtl, UINT codeNotify)
  137. {
  138. switch (id)
  139. {
  140. case IDOK:
  141. {
  142. HANDLE hToken;
  143. DWORD dwError;
  144. dwError = RebootSystem( hDlg, FALSE, FALSE, FALSE );
  145. while( dwError == ERROR_PRIVILEGE_NOT_HELD ||
  146. dwError == ERROR_NOT_ALL_ASSIGNED ||
  147. dwError == ERROR_ACCESS_DENIED )
  148. {
  149. hToken = GetAdminPrivilege( IDS_CREDUI_REBOOT_PROMPT );
  150. if( hToken )
  151. {
  152. dwError = RebootSystem( hDlg, TRUE, FALSE, FALSE );
  153. ReleaseAdminPrivilege( hToken );
  154. }
  155. else
  156. {
  157. // User cancelled credentials UI, so force CPL to remain open
  158. gfRedisplayCPL = TRUE;
  159. // Force break from while statement
  160. dwError = ERROR_CANCELLED;
  161. }
  162. }
  163. }
  164. break;
  165. }
  166. return FALSE;
  167. }
  168. BOOL RebootNeeded(void)
  169. {
  170. HKEY hkTmp;
  171. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
  172. , REGSTR_TEMP_REBOOT
  173. , 0
  174. , KEY_READ
  175. , &hkTmp ) == ERROR_SUCCESS)
  176. {
  177. RegCloseKey(hkTmp);
  178. return TRUE;
  179. }
  180. return FALSE;
  181. }
  182. DWORD RebootSystem( HWND hDlg, BOOL fUseThreadToken, BOOL fAskUser, BOOL fDisplayPrivilegeError )
  183. {
  184. HANDLE hToken;
  185. TOKEN_PRIVILEGES tkp;
  186. DWORD dwError;
  187. if( fAskUser )
  188. {
  189. TCHAR achTitle[CREDUI_REBOOT_TITLE_MAX_LENGTH];
  190. TCHAR achPrompt[CREDUI_REBOOT_PROMPT_MAX_LENGTH];
  191. LoadString( ghInstance, IDS_REBOOT_TITLE, achTitle, CREDUI_REBOOT_TITLE_MAX_LENGTH );
  192. LoadString( ghInstance, IDS_REBOOT_PROMPT, achPrompt, CREDUI_REBOOT_PROMPT_MAX_LENGTH );
  193. if( IDNO == MessageBox(hDlg, achPrompt, achTitle, MB_YESNO | MB_ICONQUESTION | MB_TASKMODAL) )
  194. {
  195. // User cancelled reboot, so force CPL to remain open
  196. gfRedisplayCPL = TRUE;
  197. return ERROR_CANCELLED;
  198. }
  199. }
  200. if( fUseThreadToken )
  201. {
  202. // Get a token for this thread
  203. if( !OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, TRUE, &hToken) )
  204. {
  205. hToken = NULL;
  206. }
  207. }
  208. else
  209. {
  210. // Get a token for this process
  211. if( !OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) )
  212. {
  213. hToken = NULL;
  214. }
  215. }
  216. if( hToken )
  217. {
  218. // Get the LUID for the shutdown privilege.
  219. LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid );
  220. tkp.PrivilegeCount = 1; // one privilege to set
  221. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  222. // Get the shutdown privilege for this process.
  223. AdjustTokenPrivileges( hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0 );
  224. // Close the access token handle
  225. CloseHandle( hToken );
  226. // Shut down the system
  227. if( ExitWindowsEx(EWX_REBOOT, SHTDN_REASON_FLAG_PLANNED | SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_RECONFIG) )
  228. {
  229. return NO_ERROR;
  230. }
  231. }
  232. // Display error message
  233. dwError = GetLastError();
  234. if( dwError != NO_ERROR )
  235. {
  236. TCHAR achTitle[CREDUI_TITLE_MAX_LENGTH];
  237. TCHAR achCantRestart[80];
  238. UINT nResourceId;
  239. if( dwError == ERROR_PRIVILEGE_NOT_HELD
  240. || dwError == ERROR_NOT_ALL_ASSIGNED
  241. || dwError == ERROR_ACCESS_DENIED )
  242. {
  243. nResourceId = IDS_CANNOT_RESTART_PRIVILEGE;
  244. }
  245. else
  246. {
  247. nResourceId = IDS_CANNOT_RESTART_UNKNOWN;
  248. }
  249. if( nResourceId == IDS_CANNOT_RESTART_UNKNOWN
  250. || fDisplayPrivilegeError )
  251. {
  252. // We are 'displaying' an error message, so force CPL to remain open
  253. gfRedisplayCPL = TRUE;
  254. LoadString( ghInstance, IDS_CREDUI_TITLE, achTitle, CREDUI_TITLE_MAX_LENGTH );
  255. LoadString( ghInstance, nResourceId, achCantRestart, sizeof(achCantRestart)/sizeof(TCHAR) );
  256. MessageBox( hDlg, achCantRestart, achTitle, MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL );
  257. }
  258. }
  259. else
  260. {
  261. // Something did not work as expected, so force CPL to remain open
  262. gfRedisplayCPL = TRUE;
  263. }
  264. return dwError;
  265. }