file reorg, pathnames.h, paths.h
[unix-history] / usr / src / sbin / reboot / halt.c
CommitLineData
8a3f4fd4 1/*
614e49dd
KB
2 * Copyright (c) 1980, 1986 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
8a3f4fd4
DF
16 */
17
18#ifndef lint
19char copyright[] =
614e49dd 20"@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
8a3f4fd4 21 All rights reserved.\n";
614e49dd 22#endif /* not lint */
8a3f4fd4 23
37c640e2 24#ifndef lint
7abf8d65 25static char sccsid[] = "@(#)halt.c 5.6 (Berkeley) %G%";
614e49dd 26#endif /* not lint */
37c640e2 27
a2588a32
BJ
28/*
29 * Halt
30 */
ccb2b0b8 31#include <sys/types.h>
7abf8d65 32#include <sys/reboot.h>
ce4fd43b 33#include <sys/time.h>
bd699e5d 34#include <sys/syslog.h>
7abf8d65 35#include <sys/signal.h>
fe6cb666 36#include <errno.h>
bd699e5d 37#include <pwd.h>
7abf8d65
KB
38#include <stdio.h>
39#include <paths.h>
ccb2b0b8 40
a2588a32
BJ
41main(argc, argv)
42 int argc;
43 char **argv;
44{
614e49dd
KB
45 register int i;
46 register int qflag = 0;
bd699e5d 47 struct passwd *pw, *getpwuid();
614e49dd
KB
48 int ch, howto, needlog = 1;
49 char *user, *ttyn, *getlogin(), *ttyname();
a2588a32
BJ
50
51 howto = RB_HALT;
614e49dd
KB
52 ttyn = ttyname(2);
53 while ((ch = getopt(argc, argv, "lnqy")) != EOF)
54 switch((char)ch) {
55 case 'l': /* undocumented; for shutdown(8) */
56 needlog = 0;
57 break;
58 case 'n':
a2588a32 59 howto |= RB_NOSYNC;
614e49dd
KB
60 break;
61 case 'q':
fe6cb666 62 qflag++;
614e49dd
KB
63 break;
64 case 'y':
65 ttyn = 0;
66 break;
67 case '?':
68 default:
69 fprintf(stderr, "usage: halt [-nqy]\n");
a2588a32
BJ
70 exit(1);
71 }
614e49dd 72
7abf8d65 73 if (ttyn && ttyn[sizeof(_PATH_TTY) - 1] == 'd') {
037df60e
BJ
74 fprintf(stderr, "halt: dangerous on a dialup; use ``halt -y'' if you are really sure\n");
75 exit(1);
76 }
fe6cb666 77
b0af3d20 78 if (needlog) {
614e49dd
KB
79 openlog("halt", 0, LOG_AUTH);
80 if ((user = getlogin()) == NULL)
81 if ((pw = getpwuid(getuid())))
82 user = pw->pw_name;
83 else
84 user = "???";
b0af3d20
MK
85 syslog(LOG_CRIT, "halted by %s", user);
86 }
bd699e5d 87
4f509481 88 signal(SIGHUP, SIG_IGN); /* for network connections */
fe6cb666 89 if (kill(1, SIGTSTP) == -1) {
614e49dd 90 fprintf(stderr, "halt: can't idle init\n");
fe6cb666
BJ
91 exit(1);
92 }
bd699e5d 93 sleep(1);
b0af3d20
MK
94 (void) kill(-1, SIGTERM); /* one chance to catch it */
95 sleep(5);
fe6cb666
BJ
96
97 if (!qflag) for (i = 1; ; i++) {
98 if (kill(-1, SIGKILL) == -1) {
99 extern int errno;
100
101 if (errno == ESRCH)
102 break;
103
614e49dd 104 perror("halt: kill");
fe6cb666
BJ
105 kill(1, SIGHUP);
106 exit(1);
107 }
108 if (i > 5) {
bd699e5d
MK
109 fprintf(stderr,
110 "CAUTION: some process(es) wouldn't die\n");
fe6cb666
BJ
111 break;
112 }
113 setalarm(2 * i);
114 pause();
115 }
116
bd699e5d 117 if (!qflag && (howto & RB_NOSYNC) == 0) {
614e49dd 118 logwtmp("~", "shutdown", "");
bd699e5d 119 sync();
fe6cb666
BJ
120 setalarm(5);
121 pause();
122 }
a2588a32 123 syscall(55, howto);
614e49dd 124 perror("halt");
a2588a32 125}
fe6cb666
BJ
126
127dingdong()
128{
129 /* RRRIIINNNGGG RRRIIINNNGGG */
130}
131
132setalarm(n)
133{
134 signal(SIGALRM, dingdong);
135 alarm(n);
136}