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