-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreboot_u.c
174 lines (166 loc) · 4.15 KB
/
reboot_u.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
/* reboot - toolbox
Copyright 2015 libdll.so
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#ifdef _WIN32
#define RB_AUTOBOOT 0x01234567
#define RB_HALT_SYSTEM 0xcdef0123
#include <errno.h>
#include <windows.h>
#ifdef _WIN32_WCE
#ifdef _USE_KIOCTL
#if 0
#include <pkfuncs.h>
#else
#include <winioctl.h>
#define IOCTL_HAL_REBOOT CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS)
extern int WINAPI KernelIoControl(unsigned long int, void *, unsigned long int, void *, unsigned long int, unsigned long int *);
#endif
#else
#include <pm.h>
#endif
#else
#ifdef _WINDOWSNT_NATIVE
#include <nt.h>
#else
#ifndef SE_SHUTDOWN_PRIVILEGE
#define SE_SHUTDOWN_PRIVILEGE (19)
#endif
//#ifndef _WIN32_WCE
// Custom errno mapping for reboot
static int __set_errno_from_oserror() {
switch(GetLastError()) {
case ERROR_INVALID_FUNCTION:
return errno = ENOSYS;
case ERROR_ACCESS_DENIED:
return errno = EPERM;
default:
return errno = EINVAL;
}
}
//#endif
#endif
static int enable_shutdown_privilege() {
void *token;
TOKEN_PRIVILEGES privilege;
memset(&privilege, 0, sizeof privilege);
privilege.PrivilegeCount = 1;
privilege.Privileges[0].Luid.LowPart = SE_SHUTDOWN_PRIVILEGE;
privilege.Privileges[0].Luid.HighPart = 0;
privilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
#ifdef _WINDOWSNT_NATIVE
long int status = NtOpenProcessToken((void *)-1, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token);
if(status < 0) {
__set_errno_from_ntstatus(status);
return -1;
}
status = NtAdjustPrivilegesToken(token, 0, &privilege, sizeof privilege, NULL, NULL);
NtClose(token);
if(status < 0) {
__set_errno_from_ntstatus(status);
return -1;
}
return 0;
#else
//LUID id;
//memset(&id, 0, sizeof id);
if(!OpenProcessToken((void *)-1, TOKEN_ADJUST_PRIVILEGES, &token)) {
__set_errno_from_oserror();
return -1;
}
//if(!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &id)) return -1;
int ok = AdjustTokenPrivileges(token, 0, &privilege, sizeof privilege, NULL, NULL);
if(!ok) __set_errno_from_oserror();
CloseHandle(token);
return ok ? 0 : -1;
#endif
}
#endif /* _WIN32_WCE */
static int reboot(int flags) {
#ifdef _WIN32_WCE
if(flags != RB_AUTOBOOT) {
errno = EINVAL;
return -1;
}
#ifdef _USE_KIOCTL
if(!KernelIoControl(IOCTL_HAL_REBOOT, NULL, 0, NULL, 0, NULL)) return -1;
#else
unsigned long int e = SetSystemPowerState(NULL, POWER_STATE_RESET, 0);
if(e) {
SetLastError(e);
return -1;
}
#endif
#else
if(enable_shutdown_privilege() < 0) return -1;
#ifdef _WINDOWSNT_NATIVE
SHUTDOWN_ACTION action;
switch(flags) {
case RB_AUTOBOOT:
action = ShutdownReboot;
break;
case RB_HALT_SYSTEM:
action = ShutdownNoReboot;
break;
default:
errno = EINVAL;
return -1;
}
long int status = NtShutdownSystem(action);
if(status < 0) {
__set_errno_from_ntstatus(status);
return -1;
}
#else
#ifdef _USE_EWX
unsigned int wflags = EWX_FORCE;
switch(flags) {
case RB_AUTOBOOT:
flags |= EWX_REBOOT;
break;
case RB_HALT_SYSTEM:
flags |= EWX_SHUTDOWN;
break;
default:
errno = EINVAL;
return -1;
}
if(!ExitWindowsEx(wflags, 0)) {
__set_errno_from_oserror();
return -1;
}
#else
if(!InitiateSystemShutdownW(NULL, NULL, 0, 1, flags & EWX_REBOOT)) {
__set_errno_from_oserror();
return -1;
}
#endif
#endif /* _WINDOWSNT_NATIVE */
#endif /* _WIN32_WCE */
while(1) sleep(10000);
}
#else
#include <sys/reboot.h>
#ifdef __sun
#define reboot(_howto) reboot(_howto,NULL)
#endif
#endif
int reboot_main(int argc, char **argv) {
int flags = RB_AUTOBOOT;
#ifndef _WIN32
if(argc < 2 || strcmp(argv[1], "-n")) sync();
#ifdef RB_NOSYNC
else flags |= RB_NOSYNC;
#endif
#endif
if(reboot(flags) < 0) {
perror("reboot");
return 1;
}
// Never reached?
return 0;
}