-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
shutdown.c
366 lines (319 loc) · 10.7 KB
/
shutdown.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS shutdown/logoff utility
* FILE: base/applications/shutdown/shutdown.c
* PURPOSE: Initiate logoff, shutdown or reboot of the system
*/
#include "precomp.h"
#include <stdlib.h>
#include <tchar.h>
#include <powrprof.h>
/*
* Takes the commandline arguments, and creates a
* struct which matches the arguments supplied.
*/
static DWORD
ParseArguments(struct CommandLineOptions* pOpts, int argc, WCHAR *argv[])
{
int index;
if (!pOpts)
return ERROR_INVALID_PARAMETER;
/* Reset all flags in struct */
pOpts->abort = FALSE;
pOpts->force = FALSE;
pOpts->logoff = FALSE;
pOpts->restart = FALSE;
pOpts->shutdown = FALSE;
pOpts->document_reason = FALSE;
pOpts->hibernate = FALSE;
pOpts->shutdown_delay = 30;
pOpts->remote_system = NULL;
pOpts->reason = ParseReasonCode(NULL); /* NOTE: NEVER use 0 here since it can delay the shutdown. */
pOpts->message = NULL;
pOpts->show_gui = FALSE;
/*
* Determine which flags the user has specified
* to the program so we can use them later.
*/
for (index = 1; index < argc; index++)
{
if (argv[index][0] == L'-' || argv[index][0] == L'/')
{
switch (towlower(argv[index][1]))
{
case L'?': /* Help */
ConResPuts(StdOut, IDS_USAGE);
return ERROR_SUCCESS;
case L'a': /* Cancel delayed shutdown */
pOpts->abort = TRUE;
break;
case L'c': /* Comment on reason for shutdown */
if (index+1 >= argc)
return ERROR_INVALID_DATA;
if (!argv[index+1] || wcslen(argv[index+1]) <= 512)
{
pOpts->message = argv[index+1];
index++;
}
else
{
ConResPuts(StdErr, IDS_ERROR_MAX_COMMENT_LENGTH);
return ERROR_BAD_LENGTH;
}
break;
case L'd': /* Reason code [p|u:]xx:yy */
if (index+1 >= argc)
return ERROR_INVALID_DATA;
pOpts->reason = ParseReasonCode(argv[index+1]);
index++;
break;
case L'e': /* Documents reason for shutdown */
/* TODO: Determine what this flag does exactly. */
pOpts->document_reason = TRUE;
break;
case L'f': /* Force shutdown without warning */
pOpts->force = TRUE;
break;
case L'h': /* Hibernate the local computer */
pOpts->hibernate = TRUE;
break;
case L'i': /* Shows GUI version of the tool */
pOpts->show_gui = TRUE;
break;
case L'l': /* Logoff the current user */
pOpts->logoff = TRUE;
break;
case L'm': /* Target remote systems (UNC name/IP address) */
if (index+1 >= argc)
return ERROR_INVALID_DATA;
pOpts->remote_system = argv[index+1];
index++;
break;
case L'p': /* Turn off local computer with no warning/time-out */
pOpts->force = TRUE;
pOpts->shutdown_delay = 0;
break;
case L'r': /* Restart computer */
pOpts->restart = TRUE;
break;
case L's': /* Shutdown */
pOpts->shutdown = TRUE;
break;
case L't': /* Shutdown delay */
if (index+1 >= argc)
return ERROR_INVALID_DATA;
pOpts->shutdown_delay = _wtoi(argv[index+1]);
if (pOpts->shutdown_delay > 0)
pOpts->force = TRUE;
index++;
break;
default:
/* Unknown arguments will exit the program. */
ConResPuts(StdOut, IDS_USAGE);
return ERROR_SUCCESS;
}
}
}
return ERROR_SUCCESS;
}
static DWORD
EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
{
DWORD dwRet = ERROR_SUCCESS;
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
if (LookupPrivilegeValueW(NULL,
lpszPrivilegeName,
&tp.Privileges[0].Luid))
{
if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL))
{
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
dwRet = ERROR_NOT_ALL_ASSIGNED;
}
else
{
dwRet = GetLastError();
}
}
else
{
dwRet = GetLastError();
}
CloseHandle(hToken);
}
else
{
dwRet = GetLastError();
}
/* Display the error description if any */
if (dwRet != ERROR_SUCCESS) DisplayError(dwRet);
return dwRet;
}
/* Main entry for program */
int wmain(int argc, WCHAR *argv[])
{
DWORD error = ERROR_SUCCESS;
struct CommandLineOptions opts;
/* Initialize the Console Standard Streams */
ConInitStdStreams();
if (argc == 1) /* i.e. no commandline arguments given */
{
ConResPuts(StdOut, IDS_USAGE);
return EXIT_SUCCESS;
}
error = ParseArguments(&opts, argc, argv);
if (error != ERROR_SUCCESS)
{
DisplayError(error);
return EXIT_FAILURE;
}
/* If the user wants to abort a shutdown */
if (opts.abort)
{
/* First, the program has to determine if the shutdown/restart is local
or remote. This is done since each one requires separate privileges. */
if (opts.remote_system == NULL)
EnablePrivilege(SE_SHUTDOWN_NAME, TRUE);
else
EnablePrivilege(SE_REMOTE_SHUTDOWN_NAME, TRUE);
/* Abort the delayed system shutdown specified. */
if (!AbortSystemShutdownW(opts.remote_system))
{
ConResPuts(StdErr, IDS_ERROR_ABORT);
DisplayError(GetLastError());
return EXIT_FAILURE;
}
else
{
return EXIT_SUCCESS;
}
}
/*
* If the user wants to hibernate the computer. Assume
* that the user wants to wake the computer up from
* hibernation and it should not force it on the system.
*/
if (opts.hibernate)
{
if (IsPwrHibernateAllowed())
{
EnablePrivilege(SE_SHUTDOWN_NAME, TRUE);
/* The shutdown utility cannot hibernate remote systems */
if (opts.remote_system != NULL)
{
return EXIT_FAILURE;
}
if (!SetSuspendState(TRUE, FALSE, FALSE))
{
ConResPuts(StdErr, IDS_ERROR_HIBERNATE);
DisplayError(GetLastError());
return EXIT_FAILURE;
}
else
{
ConResPuts(StdOut, IDS_ERROR_HIBERNATE_ENABLED);
return EXIT_SUCCESS;
}
}
else
{
return EXIT_FAILURE;
}
}
/* Both shutdown and restart flags cannot both be true */
if (opts.shutdown && opts.restart)
{
ConResPuts(StdErr, IDS_ERROR_SHUTDOWN_REBOOT);
return EXIT_FAILURE;
}
/* Ensure that the timeout amount is not too high or a negative number */
if (opts.shutdown_delay > MAX_SHUTDOWN_TIMEOUT)
{
ConResPrintf(StdErr, IDS_ERROR_TIMEOUT, opts.shutdown_delay);
return EXIT_FAILURE;
}
/* If the user wants a GUI environment */
if (opts.show_gui)
{
if (ShutdownGuiMain(opts))
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
if (opts.logoff && (opts.remote_system == NULL))
{
/*
* NOTE: Sometimes, shutdown and logoff are used together. If the logoff
* flag is used by itself, then simply logoff. But if used with shutdown,
* then skip logging off of the computer and eventually go to the action
* for shutdown.
*/
if (!opts.shutdown && !opts.restart)
{
EnablePrivilege(SE_SHUTDOWN_NAME, TRUE);
if (ExitWindowsEx(EWX_LOGOFF, opts.reason))
{
return EXIT_SUCCESS;
}
else
{
ConResPuts(StdErr, IDS_ERROR_LOGOFF);
DisplayError(GetLastError());
return EXIT_FAILURE;
}
}
}
/*
* Since both shutting down the system and restarting calls the exact same
* function, all we need to know is if we wanted to restart or shutdown.
*/
if (opts.shutdown || opts.restart)
{
/*
* First, the program has to determine if the shutdown/restart is local
* or remote. This is done since each one requires separate privileges.
*/
if (opts.remote_system == NULL)
{
EnablePrivilege(SE_SHUTDOWN_NAME, TRUE);
}
else
{
/* TODO: Remote shutdown is not supported yet */
// EnablePrivilege(SE_REMOTE_SHUTDOWN_NAME, TRUE);
return EXIT_SUCCESS;
}
/* Initiate the shutdown */
if (!InitiateSystemShutdownExW(opts.remote_system,
opts.message,
opts.shutdown_delay,
opts.force,
opts.restart,
opts.reason))
{
/*
* If there is an error, give the proper output depending
* on whether the user wanted to shutdown or restart.
*/
if (opts.restart)
ConResPuts(StdErr, IDS_ERROR_RESTART);
else
ConResPuts(StdErr, IDS_ERROR_SHUTDOWN);
DisplayError(GetLastError());
return EXIT_FAILURE;
}
else
{
return EXIT_SUCCESS;
}
}
return EXIT_SUCCESS;
}
/* EOF */