-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreboot.cpp
More file actions
232 lines (201 loc) · 7.74 KB
/
reboot.cpp
File metadata and controls
232 lines (201 loc) · 7.74 KB
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
// reboot.cpp
// Cross-platform immediate reboot/poweroff/halt using only native APIs
// No output on success. English messages only.
#include <iostream>
#include <cstring>
#include <string>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#include <sys/reboot.h>
#include <errno.h>
#ifdef __linux__
#include <linux/reboot.h>
#endif
#ifdef __APPLE__
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>
#endif
#endif
enum Action { ACTION_REBOOT, ACTION_HALT, ACTION_POWEROFF };
struct Options {
Action action;
bool force;
bool wtmp_only;
bool no_wtmp;
bool no_wall;
};
static void print_help(const char* progname) {
std::string action;
if (std::strstr(progname, "reboot")) action = "Reboot";
else if (std::strstr(progname, "poweroff")) action = "Power off";
else if (std::strstr(progname, "halt")) action = "Halt";
else action = "Reboot";
std::cout << "Usage: " << progname << " [OPTIONS...]\n"
"\n"
"\033[1m" << action << " the system.\033[0m\n"
"\n"
"Options:\n"
" --help Show this help\n"
" --halt Halt the machine\n"
" -p --poweroff Switch off the machine\n"
" --reboot Reboot the machine\n"
" -f --force Force immediate halt/power-off/reboot\n"
" -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
" -d --no-wtmp Don't write wtmp record\n"
" --no-wall Don't send wall message before halt/power-off/reboot\n"
"\n"
"See the halt(8) man page for details.\n";
}
static bool parse_options(int argc, char* argv[], Options& opts, const char* progname) {
// Set default action based on program name
if (std::strstr(progname, "reboot")) opts.action = ACTION_REBOOT;
else if (std::strstr(progname, "poweroff")) opts.action = ACTION_POWEROFF;
else if (std::strstr(progname, "halt")) opts.action = ACTION_HALT;
else opts.action = ACTION_REBOOT;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help") {
print_help(progname);
return false; // indicate to exit
} else if (arg == "--halt") {
opts.action = ACTION_HALT;
} else if (arg == "-p" || arg == "--poweroff") {
opts.action = ACTION_POWEROFF;
} else if (arg == "--reboot") {
opts.action = ACTION_REBOOT;
} else if (arg == "-f" || arg == "--force") {
opts.force = true;
} else if (arg == "-w" || arg == "--wtmp-only") {
opts.wtmp_only = true;
} else if (arg == "-d" || arg == "--no-wtmp") {
opts.no_wtmp = true;
} else if (arg == "--no-wall") {
opts.no_wall = true;
} else {
std::cerr << "Error: Unknown option '" << arg << "'. Use --help for help.\n";
return false;
}
}
return true;
}
#ifdef __APPLE__
static OSStatus SendAppleEventToSystemProcess(AEEventID eventToSendID) {
AEAddressDesc targetDesc;
static const ProcessSerialNumber kPSNOfSystemProcess = {0, kSystemProcess};
AppleEvent eventReply = {typeNull, NULL};
AppleEvent eventToSend = {typeNull, NULL};
OSStatus status = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess,
sizeof(kPSNOfSystemProcess), &targetDesc);
if (status != noErr) return status;
status = AECreateAppleEvent(kCoreEventClass, eventToSendID, &targetDesc,
kAutoGenerateReturnID, kAnyTransactionID, &eventToSend);
AEDisposeDesc(&targetDesc);
if (status != noErr) return status;
status = AESendMessage(&eventToSend, &eventReply, kAENormalPriority, kAEDefaultTimeout);
AEDisposeDesc(&eventToSend);
if (status != noErr) return status;
AEDisposeDesc(&eventReply);
return status;
}
#endif
int main(int argc, char* argv[]) {
// Extract program name
const char* progname = argv[0];
const char* basename = std::strrchr(progname, '\\');
if (!basename) basename = std::strrchr(progname, '/');
if (basename) progname = basename + 1;
Options opts = {ACTION_REBOOT, false, false, false, false};
if (!parse_options(argc, argv, opts, progname)) {
return 1; // error or help shown
}
// If wtmp_only, just simulate (do nothing for now, as wtmp is Linux-specific)
if (opts.wtmp_only) {
// On non-Linux, just exit
return 0;
}
#ifdef _WIN32
HANDLE hToken;
TOKEN_PRIVILEGES tkp{};
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
std::cerr << "Error: Failed to open process token.\n";
return 1;
}
if (!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid)) {
CloseHandle(hToken);
std::cerr << "Error: LookupPrivilegeValue failed.\n";
return 1;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0)) {
CloseHandle(hToken);
std::cerr << "Error: AdjustTokenPrivileges failed.\n";
return 1;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
CloseHandle(hToken);
std::cerr << "Error: Insufficient privileges. Run as Administrator.\n";
return 1;
}
// Determine shutdown type
BOOL bRebootAfterShutdown = (opts.action == ACTION_REBOOT);
DWORD dwShutdownFlags = SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_RECONFIG;
BOOL ret = InitiateSystemShutdownEx(
NULL, NULL, 0, TRUE, bRebootAfterShutdown, dwShutdownFlags
);
CloseHandle(hToken);
if (!ret) {
std::string action_str = (opts.action == ACTION_REBOOT) ? "reboot" :
(opts.action == ACTION_POWEROFF) ? "poweroff" : "halt";
std::cerr << "Error: Failed to initiate " << action_str << " (code: " << GetLastError() << ").\n";
return 1;
}
#else
// Unix-like
#ifdef __APPLE__
if (geteuid() != 0) {
std::cerr << "Error: Root privileges required.\n";
return 1;
}
AEEventID eventID;
if (opts.action == ACTION_REBOOT) {
eventID = kAERestart;
} else {
eventID = kAEShutDown; // for halt and poweroff
}
OSStatus status = SendAppleEventToSystemProcess(eventID);
if (status != noErr) {
std::string action_str = (opts.action == ACTION_REBOOT) ? "restart" : "shutdown";
std::cerr << "Error: Failed to send " << action_str << " event (code: " << status << "). Requires root or System Settings permission.\n";
return 1;
}
#else
// Linux: direct syscall
if (geteuid() != 0) {
std::cerr << "Error: Root privileges required.\n";
return 1;
}
sync(); // Flush filesystems
int reboot_cmd;
if (opts.action == ACTION_REBOOT) {
reboot_cmd = RB_AUTOBOOT;
} else if (opts.action == ACTION_HALT) {
reboot_cmd = RB_HALT_SYSTEM;
} else { // ACTION_POWEROFF
reboot_cmd = RB_POWER_OFF;
}
int ret = reboot(reboot_cmd);
if (ret == -1) {
std::string action_str = (opts.action == ACTION_REBOOT) ? "reboot" :
(opts.action == ACTION_HALT) ? "halt" : "poweroff";
std::cerr << "Error: " << action_str << "() syscall failed: " << strerror(errno) << "\n";
return 1;
}
#endif
#endif
// Success: no output
return 0;
}