update
[unix-history] / usr / src / sbin / reboot / halt.c
CommitLineData
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
8char 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
f5a538b6 14static char sccsid[] = "@(#)halt.c 5.2 (Berkeley) %G%";
8a3f4fd4 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>
f5a538b6 26#include <syslog.h>
ccb2b0b8 27
a2588a32
BJ
28main(argc, argv)
29 int argc;
30 char **argv;
31{
32 int howto;
037df60e 33 char *ttyn = (char *)ttyname(2);
fe6cb666 34 register i;
821f38b7 35 register qflag = 0;
a2588a32 36
f5a538b6 37 openlog("halt", 0, LOG_AUTH);
a2588a32
BJ
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 83 if ((howto & RB_NOSYNC) == 0)
f5a538b6 84 syslog(LOG_CRIT, "halted");
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
98dingdong()
99{
100 /* RRRIIINNNGGG RRRIIINNNGGG */
101}
102
103setalarm(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))
111char wtmpf[] = "/usr/adm/wtmp";
112struct utmp wtmp;
113
114markdown()
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}