remove empty dir
[reactos.git] / rosapps / sysutils / shutdown.c
1 /* $Id$
2 *
3 * EAU shutdown.c 1.4.1
4 *
5 * Copyright (C) 1997,1998,1999 Emanuele Aliberti
6 *
7 * --------------------------------------------------------------------
8 *
9 * This software is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This software is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this software; see the file COPYING.LIB. If
21 * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
22 * Cambridge, MA 02139, USA.
23 *
24 * --------------------------------------------------------------------
25 * 1999-05-14 (Emanuele Aliberti)
26 * Released version 1.4.1 under GNU GPL for the ReactOS project.
27 * --------------------------------------------------------------------
28 */
29 #define UNICODE
30 #define _UNICODE
31 #include <windows.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <tchar.h>
35 #include "win32err.h"
36
37 #ifndef SE_PRIVILEGE_ENABLED
38 #define NTOS_MODE_USER
39 #include <ntos.h>
40 #endif
41
42
43 struct _EWX
44 {
45 CHAR mode;
46 UINT id;
47 };
48
49 static
50 struct _EWX modes[] =
51 {
52 { 'f', EWX_FORCE },
53 { 'l', EWX_LOGOFF },
54 { 'p', EWX_POWEROFF },
55 { 'r', EWX_REBOOT },
56 { 's', EWX_SHUTDOWN },
57 { 0, 0 }
58 };
59
60
61 static
62 UINT
63 DecodeArg( CHAR * modestr )
64 {
65 register int i;
66
67 if (modestr[0] != '-' && modestr[0] != '/')
68 {
69 return (UINT) -1;
70 }
71 for ( i = 0; modes[i].mode; ++i)
72 {
73 if (modestr[1] == modes[i].mode)
74 {
75 return modes[i].id;
76 }
77 }
78 return (UINT) -1;
79 }
80
81
82
83 static
84 const
85 char * usage = "\
86 Shutdown ver. 1.4.1 (compiled on %s, at %s)\n\
87 Copyright (C) 1997-1999 Emanuele Aliberti\n\n\
88 usage: %s [-f] [-l] [-p] [-r] [-s]\n\
89 f (FORCE) processes are unconditionally terminated\n\
90 l (LOGOFF) logs the current user off\n\
91 p (POWEROFF) turns off the power, if possibile\n\
92 r (REBOOT) reboots the system\n\
93 s (SHUTDOWN) shuts down the system to a point at which\n\
94 it is safe to turn off the power\n\n\
95 Any other letter will print this help message.\n";
96
97 int
98 main(
99 int argc,
100 char * argv []
101 )
102 {
103 UINT mode;
104 HANDLE h;
105 TOKEN_PRIVILEGES tp;
106
107 mode = (argc == 2)
108 ? DecodeArg(argv[1])
109 : DecodeArg("-?");
110 if (mode == (UINT) -1)
111 {
112 fprintf(
113 stderr,
114 usage,
115 __DATE__,
116 __TIME__,
117 argv[0]
118 );
119 return EXIT_SUCCESS;
120 }
121 /*
122 * Get the current process token handle
123 * so we can get shutdown privilege.
124 */
125 if (FALSE == OpenProcessToken(
126 GetCurrentProcess(),
127 (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY),
128 & h
129 )
130 ) {
131 PrintWin32Error(
132 L"while opening the process",
133 GetLastError()
134 );
135 return EXIT_FAILURE;
136 }
137 /*
138 * Get the LUID for shutdown privilege.
139 */
140 if (FALSE == LookupPrivilegeValue(
141 NULL,
142 SE_SHUTDOWN_NAME,
143 & tp.Privileges[0].Luid
144 )
145 ) {
146 PrintWin32Error(
147 L"while looking up privileges",
148 GetLastError()
149 );
150 return EXIT_FAILURE;
151 }
152 tp.PrivilegeCount = 1; /* One privilege to seat */
153 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
154 /*
155 * Get shutdown privilege for this process.
156 */
157 if (FALSE == AdjustTokenPrivileges(
158 h,
159 FALSE,
160 & tp,
161 0,
162 (PTOKEN_PRIVILEGES) NULL,
163 0
164 )
165 ) {
166 PrintWin32Error(
167 L"while adjusting shutdown privilege",
168 GetLastError()
169 );
170 return EXIT_FAILURE;
171 }
172 /* Now really exit! */
173 if (FALSE == ExitWindowsEx(mode, 0))
174 {
175 PrintWin32Error(
176 L"ExitWindowsEx",
177 GetLastError()
178 );
179 return EXIT_FAILURE;
180 }
181 return EXIT_SUCCESS;
182 }
183
184
185 /* EOF */
186