use copyinstr and copystr (add copyoutstr later)
[unix-history] / usr / src / sbin / reboot / halt.c
CommitLineData
37c640e2 1#ifndef lint
ce4fd43b 2static char *sccsid = "@(#)halt.c 4.9 (Berkeley) %G%";
37c640e2
SL
3#endif
4
a2588a32
BJ
5/*
6 * Halt
7 */
8#include <stdio.h>
9#include <sys/reboot.h>
ccb2b0b8 10#include <sys/types.h>
ce4fd43b 11#include <sys/time.h>
fe6cb666
BJ
12#include <errno.h>
13#include <signal.h>
a2588a32 14
ccb2b0b8
BJ
15#define SHUTDOWNLOG "/usr/adm/shutdownlog"
16
a2588a32
BJ
17main(argc, argv)
18 int argc;
19 char **argv;
20{
21 int howto;
037df60e 22 char *ttyn = (char *)ttyname(2);
fe6cb666 23 register i;
821f38b7 24 register qflag = 0;
a2588a32
BJ
25
26 howto = RB_HALT;
27 argc--, argv++;
28 while (argc > 0) {
29 if (!strcmp(*argv, "-n"))
30 howto |= RB_NOSYNC;
037df60e
BJ
31 else if (!strcmp(*argv, "-y"))
32 ttyn = 0;
fe6cb666
BJ
33 else if (!strcmp(*argv, "-q"))
34 qflag++;
a2588a32
BJ
35 else {
36 fprintf(stderr, "usage: halt [ -n ]\n");
37 exit(1);
38 }
39 argc--, argv++;
40 }
037df60e
BJ
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 }
fe6cb666 45
4f509481 46 signal(SIGHUP, SIG_IGN); /* for network connections */
fe6cb666
BJ
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
ccb2b0b8
BJ
71 if ((howto & RB_NOSYNC) == 0)
72 log_entry();
fe6cb666
BJ
73 if (!qflag) {
74 if ((howto & RB_NOSYNC)==0) {
75 markdown();
76 sync();
77 sync();
78 }
79 setalarm(5);
80 pause();
81 }
a2588a32
BJ
82 syscall(55, howto);
83 perror("reboot");
84}
fe6cb666
BJ
85
86dingdong()
87{
88 /* RRRIIINNNGGG RRRIIINNNGGG */
89}
90
91setalarm(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))
99char wtmpf[] = "/usr/adm/wtmp";
100struct utmp wtmp;
101
102markdown()
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");
37c640e2 109 SCPYN(wtmp.ut_host, "");
fe6cb666
BJ
110 time(&wtmp.ut_time);
111 write(f, (char *)&wtmp, sizeof(wtmp));
112 close(f);
113 }
114}
ccb2b0b8
BJ
115
116char *days[] = {
117 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
118};
119
120char *months[] = {
121 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
122 "Oct", "Nov", "Dec"
123};
124
125log_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");
31b4d3e1
BJ
134 if (fp == NULL)
135 return;
ccb2b0b8
BJ
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}