-
Notifications
You must be signed in to change notification settings - Fork 63
/
halt.c
134 lines (114 loc) · 2.71 KB
/
halt.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
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <utmp.h>
extern char *__progname;
typedef enum {NOOP, HALT, REBOOT, POWEROFF} action_type;
#ifndef OUR_WTMP
#define OUR_WTMP "/var/log/wtmp"
#endif
#ifndef OUR_UTMP
#define OUR_UTMP "/run/utmp"
#endif
void write_wtmp(int boot) {
int fd;
if ((fd = open(OUR_WTMP, O_WRONLY|O_APPEND)) < 0)
return;
struct utmp utmp = {0};
struct utsname uname_buf;
struct timeval tv;
gettimeofday(&tv, 0);
utmp.ut_tv.tv_sec = tv.tv_sec;
utmp.ut_tv.tv_usec = tv.tv_usec;
utmp.ut_type = boot ? BOOT_TIME : RUN_LVL;
strncpy(utmp.ut_name, boot ? "reboot" : "shutdown", sizeof utmp.ut_name);
strncpy(utmp.ut_id , "~~", sizeof utmp.ut_id);
strncpy(utmp.ut_line, boot ? "~" : "~~", sizeof utmp.ut_line);
if (uname(&uname_buf) == 0)
strncpy(utmp.ut_host, uname_buf.release, sizeof utmp.ut_host);
write(fd, (char *)&utmp, sizeof utmp);
close(fd);
if (boot) {
if ((fd = open(OUR_UTMP, O_WRONLY|O_APPEND)) < 0)
return;
write(fd, (char *)&utmp, sizeof utmp);
close(fd);
}
}
int main(int argc, char *argv[]) {
int do_sync = 1;
int do_force = 0;
int do_wtmp = 1;
int opt;
action_type action = NOOP;
if (strncmp(__progname, "halt", 4) == 0)
action = HALT;
else if (strncmp(__progname, "reboot", 6) == 0)
action = REBOOT;
else if (strncmp(__progname, "poweroff", 8) == 0)
action = POWEROFF;
else
warnx("no default behavior, needs to be called as halt/reboot/poweroff.");
while ((opt = getopt(argc, argv, "dfhinwB")) != -1)
switch (opt) {
case 'n':
do_sync = 0;
break;
case 'w':
action = NOOP;
do_sync = 0;
break;
case 'd':
do_wtmp = 0;
break;
case 'h':
case 'i':
/* silently ignored. */
break;
case 'f':
do_force = 1;
break;
case 'B':
write_wtmp(1);
return 0;
default:
errx(1, "Usage: %s [-n] [-f] [-d] [-w] [-B]", __progname);
}
if (do_wtmp)
write_wtmp(0);
if (do_sync)
sync();
switch (action) {
case HALT:
if (do_force)
reboot(RB_HALT_SYSTEM);
else
execl("/bin/runit-init", "init", "0", (char*)0);
err(1, "halt failed");
break;
case POWEROFF:
if (do_force)
reboot(RB_POWER_OFF);
else
execl("/bin/runit-init", "init", "0", (char*)0);
err(1, "poweroff failed");
break;
case REBOOT:
if (do_force)
reboot(RB_AUTOBOOT);
else
execl("/bin/runit-init", "init", "6", (char*)0);
err(1, "reboot failed");
break;
case NOOP:
break;
}
return 0;
}