Commit | Line | Data |
---|---|---|
8a3f4fd4 | 1 | /* |
614e49dd KB |
2 | * Copyright (c) 1980, 1986 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
70ab3c27 | 5 | * %sccs.include.redist.c% |
8a3f4fd4 DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
614e49dd | 10 | "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\ |
8a3f4fd4 | 11 | All rights reserved.\n"; |
614e49dd | 12 | #endif /* not lint */ |
8a3f4fd4 | 13 | |
37c640e2 | 14 | #ifndef lint |
c4e725c7 | 15 | static char sccsid[] = "@(#)halt.c 5.10 (Berkeley) %G%"; |
614e49dd | 16 | #endif /* not lint */ |
37c640e2 | 17 | |
a2588a32 BJ |
18 | /* |
19 | * Halt | |
20 | */ | |
ccb2b0b8 | 21 | #include <sys/types.h> |
7abf8d65 | 22 | #include <sys/reboot.h> |
ce4fd43b | 23 | #include <sys/time.h> |
bd699e5d | 24 | #include <sys/syslog.h> |
7abf8d65 | 25 | #include <sys/signal.h> |
fe6cb666 | 26 | #include <errno.h> |
bd699e5d | 27 | #include <pwd.h> |
7abf8d65 KB |
28 | #include <stdio.h> |
29 | #include <paths.h> | |
ccb2b0b8 | 30 | |
a2588a32 BJ |
31 | main(argc, argv) |
32 | int argc; | |
33 | char **argv; | |
34 | { | |
614e49dd KB |
35 | register int i; |
36 | register int qflag = 0; | |
f0d98f59 | 37 | struct passwd *pw; |
614e49dd KB |
38 | int ch, howto, needlog = 1; |
39 | char *user, *ttyn, *getlogin(), *ttyname(); | |
a2588a32 BJ |
40 | |
41 | howto = RB_HALT; | |
614e49dd KB |
42 | ttyn = ttyname(2); |
43 | while ((ch = getopt(argc, argv, "lnqy")) != EOF) | |
44 | switch((char)ch) { | |
45 | case 'l': /* undocumented; for shutdown(8) */ | |
46 | needlog = 0; | |
47 | break; | |
48 | case 'n': | |
a2588a32 | 49 | howto |= RB_NOSYNC; |
614e49dd KB |
50 | break; |
51 | case 'q': | |
fe6cb666 | 52 | qflag++; |
614e49dd KB |
53 | break; |
54 | case 'y': | |
55 | ttyn = 0; | |
56 | break; | |
57 | case '?': | |
58 | default: | |
59 | fprintf(stderr, "usage: halt [-nqy]\n"); | |
a2588a32 BJ |
60 | exit(1); |
61 | } | |
614e49dd | 62 | |
7abf8d65 | 63 | if (ttyn && ttyn[sizeof(_PATH_TTY) - 1] == 'd') { |
037df60e BJ |
64 | fprintf(stderr, "halt: dangerous on a dialup; use ``halt -y'' if you are really sure\n"); |
65 | exit(1); | |
66 | } | |
fe6cb666 | 67 | |
b0af3d20 | 68 | if (needlog) { |
614e49dd KB |
69 | openlog("halt", 0, LOG_AUTH); |
70 | if ((user = getlogin()) == NULL) | |
71 | if ((pw = getpwuid(getuid()))) | |
72 | user = pw->pw_name; | |
73 | else | |
74 | user = "???"; | |
b0af3d20 MK |
75 | syslog(LOG_CRIT, "halted by %s", user); |
76 | } | |
bd699e5d | 77 | |
4f509481 | 78 | signal(SIGHUP, SIG_IGN); /* for network connections */ |
fe6cb666 | 79 | if (kill(1, SIGTSTP) == -1) { |
614e49dd | 80 | fprintf(stderr, "halt: can't idle init\n"); |
fe6cb666 BJ |
81 | exit(1); |
82 | } | |
bd699e5d | 83 | sleep(1); |
b0af3d20 MK |
84 | (void) kill(-1, SIGTERM); /* one chance to catch it */ |
85 | sleep(5); | |
fe6cb666 BJ |
86 | |
87 | if (!qflag) for (i = 1; ; i++) { | |
88 | if (kill(-1, SIGKILL) == -1) { | |
89 | extern int errno; | |
90 | ||
91 | if (errno == ESRCH) | |
92 | break; | |
93 | ||
614e49dd | 94 | perror("halt: kill"); |
fe6cb666 BJ |
95 | kill(1, SIGHUP); |
96 | exit(1); | |
97 | } | |
98 | if (i > 5) { | |
bd699e5d MK |
99 | fprintf(stderr, |
100 | "CAUTION: some process(es) wouldn't die\n"); | |
fe6cb666 BJ |
101 | break; |
102 | } | |
103 | setalarm(2 * i); | |
104 | pause(); | |
105 | } | |
106 | ||
bd699e5d | 107 | if (!qflag && (howto & RB_NOSYNC) == 0) { |
614e49dd | 108 | logwtmp("~", "shutdown", ""); |
bd699e5d | 109 | sync(); |
fe6cb666 BJ |
110 | setalarm(5); |
111 | pause(); | |
112 | } | |
c4e725c7 | 113 | reboot(howto); |
614e49dd | 114 | perror("halt"); |
a2588a32 | 115 | } |
fe6cb666 | 116 | |
04b79a9c | 117 | void |
fe6cb666 BJ |
118 | dingdong() |
119 | { | |
120 | /* RRRIIINNNGGG RRRIIINNNGGG */ | |
121 | } | |
122 | ||
123 | setalarm(n) | |
f0d98f59 | 124 | int n; |
fe6cb666 BJ |
125 | { |
126 | signal(SIGALRM, dingdong); | |
127 | alarm(n); | |
128 | } |