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