xref: /netbsd-src/sbin/reboot/reboot.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
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, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)reboot.c	5.11 (Berkeley) 2/27/91";*/
42 static char rcsid[] = "$Id: reboot.c,v 1.5 1994/12/18 00:22:53 cgd Exp $";
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/syslog.h>
48 #include <sys/syscall.h>
49 #include <sys/reboot.h>
50 #include <sys/signal.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <errno.h>
54 #include <signal.h>
55 #include <unistd.h>
56 
57 void setalarm __P((int));
58 
59 int
60 main(argc, argv)
61 	int argc;
62 	char **argv;
63 {
64 	int howto;
65 	register i;
66 	register qflag = 0;
67 	int needlog = 1;
68 	char *user, *getlogin();
69 	struct passwd *pw;
70 
71 	openlog("reboot", 0, LOG_AUTH);
72 	argc--, argv++;
73 	howto = 0;
74 	while (argc > 0) {
75 		if (!strcmp(*argv, "-q"))
76 			qflag++;
77 		else if (!strcmp(*argv, "-n"))
78 			howto |= RB_NOSYNC;
79 		else if (!strcmp(*argv, "-l"))
80 			needlog = 0;
81 		else {
82 			fprintf(stderr,
83 			    "usage: reboot [ -n ][ -q ]\n");
84 			exit(1);
85 		}
86 		argc--, argv++;
87 	}
88 
89 	if (needlog) {
90 		user = getlogin();
91 		if (user == (char *)0 && (pw = getpwuid(getuid())))
92 			user = pw->pw_name;
93 		if (user == (char *)0)
94 			user = "root";
95 		syslog(LOG_CRIT, "rebooted by %s", user);
96 	}
97 
98 	signal(SIGHUP, SIG_IGN);	/* for remote connections */
99 	if (kill(1, SIGTSTP) == -1) {
100 		fprintf(stderr, "reboot: can't idle init\n");
101 		exit(1);
102 	}
103 	sleep(1);
104 	(void) kill(-1, SIGTERM);	/* one chance to catch it */
105 	sleep(5);
106 
107 	if (!qflag) for (i = 1; ; i++) {
108 		if (kill(-1, SIGKILL) == -1) {
109 			extern int errno;
110 
111 			if (errno == ESRCH)
112 				break;
113 
114 			perror("reboot: kill");
115 			kill(1, SIGHUP);
116 			exit(1);
117 		}
118 		if (i > 5) {
119 			fprintf(stderr,
120 			    "CAUTION: some process(es) wouldn't die\n");
121 			break;
122 		}
123 		setalarm(2 * i);
124 		pause();
125 	}
126 
127 	if (!qflag && (howto & RB_NOSYNC) == 0) {
128 		logwtmp("~", "shutdown", "");
129 		sync();
130 		setalarm(5);
131 		pause();
132 	}
133 	syscall(SYS_reboot, howto);
134 	perror("reboot");
135 	kill(1, SIGHUP);
136 	exit(1);
137 }
138 
139 void
140 dingdong()
141 {
142 	/* RRRIIINNNGGG RRRIIINNNGGG */
143 }
144 
145 void
146 setalarm(n)
147 	int n;
148 {
149 	signal(SIGALRM, dingdong);
150 	alarm(n);
151 }
152