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