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