Commit | Line | Data |
---|---|---|
7c280ab6 | 1 | static char *sccsid = "@(#)reboot.c 4.6 (Berkeley) %G%"; |
cf5fda66 BJ |
2 | /* |
3 | * Reboot | |
4 | */ | |
fe6cb666 BJ |
5 | #include <stdio.h> |
6 | #include <sys/reboot.h> | |
7 | #include <errno.h> | |
8 | #include <signal.h> | |
3111ba40 BJ |
9 | #include <sys/types.h> |
10 | #include <time.h> | |
11 | ||
12 | #define SHUTDOWNLOG "/usr/adm/shutdownlog" | |
cf5fda66 BJ |
13 | |
14 | main(argc, argv) | |
15 | int argc; | |
16 | char **argv; | |
17 | { | |
18 | int howto; | |
19 | register char *argp; | |
fe6cb666 BJ |
20 | register i; |
21 | register ok = 0; | |
22 | register qflag = 0; | |
cf5fda66 BJ |
23 | |
24 | argc--, argv++; | |
25 | howto = 0; | |
26 | while (argc > 0) { | |
fe6cb666 BJ |
27 | if (!strcmp(*argv, "-q")) |
28 | qflag++; | |
cf5fda66 BJ |
29 | else if (!strcmp(*argv, "-n")) |
30 | howto |= RB_NOSYNC; | |
cf5fda66 BJ |
31 | else { |
32 | fprintf(stderr, | |
fe6cb666 | 33 | "usage: reboot [ -n ][ -q ]\n"); |
cf5fda66 BJ |
34 | exit(1); |
35 | } | |
36 | argc--, argv++; | |
37 | } | |
fe6cb666 | 38 | |
7c280ab6 | 39 | signal(SIGHUP, SIG_IGN); /* for remote connections */ |
fe6cb666 BJ |
40 | if (kill(1, SIGTSTP) == -1) { |
41 | fprintf(stderr, "reboot: can't idle init\n"); | |
42 | exit(1); | |
43 | } | |
44 | ||
45 | if (!qflag) for (i = 1; ; i++) { | |
46 | if (kill(-1, SIGKILL) == -1) { | |
47 | extern int errno; | |
48 | ||
49 | if (errno == ESRCH) | |
50 | break; | |
51 | ||
52 | perror("reboot: kill"); | |
53 | kill(1, SIGHUP); | |
54 | exit(1); | |
55 | } | |
56 | if (i > 5) { | |
57 | fprintf(stderr, "CAUTION: some process(es) wouldn't die\n"); | |
58 | break; | |
59 | } | |
60 | setalarm(2 * i); | |
61 | pause(); | |
62 | } | |
63 | ||
e8d9b3bd BJ |
64 | if ((howto & RB_NOSYNC) == 0) |
65 | log_entry(); | |
fe6cb666 BJ |
66 | if (!qflag) { |
67 | if (!(howto & RB_NOSYNC)) { | |
68 | markdown(); | |
69 | sync(); | |
70 | sync(); | |
71 | } | |
72 | setalarm(5); | |
73 | pause(); | |
74 | } | |
cf5fda66 BJ |
75 | syscall(55, howto); |
76 | perror("reboot"); | |
fe6cb666 BJ |
77 | kill(1, SIGHUP); |
78 | exit(1); | |
79 | } | |
80 | ||
81 | dingdong() | |
82 | { | |
83 | /* RRRIIINNNGGG RRRIIINNNGGG */ | |
84 | } | |
85 | ||
86 | setalarm(n) | |
87 | { | |
88 | signal(SIGALRM, dingdong); | |
89 | alarm(n); | |
90 | } | |
91 | ||
92 | #include <utmp.h> | |
93 | #define SCPYN(a, b) strncpy(a, b, sizeof(a)) | |
94 | char wtmpf[] = "/usr/adm/wtmp"; | |
95 | struct utmp wtmp; | |
96 | ||
97 | markdown() | |
98 | { | |
99 | register f = open(wtmpf, 1); | |
100 | if (f >= 0) { | |
101 | lseek(f, 0L, 2); | |
102 | SCPYN(wtmp.ut_line, "~"); | |
103 | SCPYN(wtmp.ut_name, "shutdown"); | |
104 | time(&wtmp.ut_time); | |
105 | write(f, (char *)&wtmp, sizeof(wtmp)); | |
106 | close(f); | |
107 | } | |
cf5fda66 | 108 | } |
3111ba40 BJ |
109 | |
110 | char *days[] = { | |
111 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | |
112 | }; | |
113 | ||
114 | char *months[] = { | |
115 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", | |
116 | "Oct", "Nov", "Dec" | |
117 | }; | |
118 | ||
119 | log_entry() | |
120 | { | |
121 | FILE *fp; | |
122 | struct tm *tm, *localtime(); | |
123 | time_t now; | |
124 | ||
125 | time(&now); | |
126 | tm = localtime(&now); | |
127 | fp = fopen(SHUTDOWNLOG, "a"); | |
e1fc0b0e BJ |
128 | if (fp == 0) |
129 | return; | |
3111ba40 BJ |
130 | fseek(fp, 0L, 2); |
131 | fprintf(fp, "%02d:%02d %s %s %2d, %4d. Halted for reboot.\n", tm->tm_hour, | |
132 | tm->tm_min, days[tm->tm_wday], months[tm->tm_mon], | |
133 | tm->tm_mday, tm->tm_year + 1900); | |
134 | fclose(fp); | |
135 | } |