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