xref: /netbsd-src/sbin/reboot/reboot.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: reboot.c,v 1.7 1995/03/18 16:11:54 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1986, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
45 #else
46 static char rcsid[] = "$NetBSD: reboot.c,v 1.7 1995/03/18 16:11:54 cgd Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/reboot.h>
51 #include <signal.h>
52 #include <pwd.h>
53 #include <errno.h>
54 #include <syslog.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 void err __P((const char *fmt, ...));
61 void usage __P((void));
62 
63 int dohalt;
64 
65 int
66 main(argc, argv)
67 	int argc;
68 	char *argv[];
69 {
70 	register int i;
71 	struct passwd *pw;
72 	int ch, howto, lflag, nflag, qflag, sverrno;
73 	char *p, *user;
74 
75 	if (!strcmp((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
76 		dohalt = 1;
77 		howto = RB_HALT;
78 	} else
79 		howto = 0;
80 	lflag = nflag = qflag = 0;
81 	while ((ch = getopt(argc, argv, "lnq")) != EOF)
82 		switch(ch) {
83 		case 'l':		/* Undocumented; used by shutdown. */
84 			lflag = 1;
85 			break;
86 		case 'n':
87 			nflag = 1;
88 			howto |= RB_NOSYNC;
89 			break;
90 		case 'q':
91 			qflag = 1;
92 			break;
93 		case '?':
94 		default:
95 			usage();
96 		}
97 	argc -= optind;
98 	argv += optind;
99 
100 	if (geteuid())
101 		err("%s", strerror(EPERM));
102 
103 	if (qflag) {
104 		reboot(howto);
105 		err("%s", strerror(errno));
106 	}
107 
108 	/* Log the reboot. */
109 	if (!lflag)  {
110 		if ((user = getlogin()) == NULL)
111 			user = (pw = getpwuid(getuid())) ?
112 			    pw->pw_name : "???";
113 		if (dohalt) {
114 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
115 			syslog(LOG_CRIT, "halted by %s", user);
116 		} else {
117 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
118 			syslog(LOG_CRIT, "rebooted by %s", user);
119 		}
120 	}
121 	logwtmp("~", "shutdown", "");
122 
123 	/*
124 	 * Do a sync early on, so disks start transfers while we're off
125 	 * killing processes.  Don't worry about writes done before the
126 	 * processes die, the reboot system call syncs the disks.
127 	 */
128 	if (!nflag)
129 		sync();
130 
131 	/* Just stop init -- if we fail, we'll restart it. */
132 	if (kill(1, SIGTSTP) == -1)
133 		err("SIGTSTP init: %s", strerror(errno));
134 
135 	/* Ignore the SIGHUP we get when our parent shell dies. */
136 	(void)signal(SIGHUP, SIG_IGN);
137 
138 	/* Send a SIGTERM first, a chance to save the buffers. */
139 	if (kill(-1, SIGTERM) == -1)
140 		err("SIGTERM processes: %s", strerror(errno));
141 
142 	/*
143 	 * After the processes receive the signal, start the rest of the
144 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
145 	 * the SIGKILL to give everybody a chance.
146 	 */
147 	sleep(2);
148 	if (!nflag)
149 		sync();
150 	sleep(3);
151 
152 	for (i = 1;; ++i) {
153 		if (kill(-1, SIGKILL) == -1) {
154 			if (errno == ESRCH)
155 				break;
156 			goto restart;
157 		}
158 		if (i > 5) {
159 			(void)fprintf(stderr,
160 			    "WARNING: some process(es) wouldn't die\n");
161 			break;
162 		}
163 		(void)sleep(2 * i);
164 	}
165 
166 	reboot(howto);
167 	/* FALLTHROUGH */
168 
169 restart:
170 	sverrno = errno;
171 	err("%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
172 	    strerror(sverrno));
173 	/* NOTREACHED */
174 }
175 
176 void
177 usage()
178 {
179 	(void)fprintf(stderr, "usage: %s [-nq]\n", dohalt ? "halt" : "reboot");
180 	exit(1);
181 }
182 
183 #if __STDC__
184 #include <stdarg.h>
185 #else
186 #include <varargs.h>
187 #endif
188 
189 void
190 #if __STDC__
191 err(const char *fmt, ...)
192 #else
193 err(fmt, va_alist)
194 	char *fmt;
195         va_dcl
196 #endif
197 {
198 	va_list ap;
199 #if __STDC__
200 	va_start(ap, fmt);
201 #else
202 	va_start(ap);
203 #endif
204 	(void)fprintf(stderr, "%s: ", dohalt ? "halt" : "reboot");
205 	(void)vfprintf(stderr, fmt, ap);
206 	va_end(ap);
207 	(void)fprintf(stderr, "\n");
208 	exit(1);
209 	/* NOTREACHED */
210 }
211