xref: /386bsd/usr/src/sbin/reboot/reboot.c (revision a2142627)
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[] = "@(#)reboot.c	5.11 (Berkeley) 2/27/91";
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/syslog.h>
47 #include <sys/syscall.h>
48 #include <sys/reboot.h>
49 #include <sys/signal.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <errno.h>
53 
main(argc,argv)54 main(argc, argv)
55 	int argc;
56 	char **argv;
57 {
58 	int howto;
59 	register char *argp;
60 	register i;
61 	register ok = 0;
62 	register qflag = 0;
63 	int needlog = 1;
64 	char *user, *getlogin();
65 	struct passwd *pw;
66 
67 	openlog("reboot", 0, LOG_AUTH);
68 	argc--, argv++;
69 	howto = 0;
70 	while (argc > 0) {
71 		if (!strcmp(*argv, "-q"))
72 			qflag++;
73 		else if (!strcmp(*argv, "-n"))
74 			howto |= RB_NOSYNC;
75 		else if (!strcmp(*argv, "-l"))
76 			needlog = 0;
77 		else {
78 			fprintf(stderr,
79 			    "usage: reboot [ -n ][ -q ]\n");
80 			exit(1);
81 		}
82 		argc--, argv++;
83 	}
84 
85 	if (needlog) {
86 		user = getlogin();
87 		if (user == (char *)0 && (pw = getpwuid(getuid())))
88 			user = pw->pw_name;
89 		if (user == (char *)0)
90 			user = "root";
91 		syslog(LOG_CRIT, "rebooted by %s", user);
92 	}
93 
94 	signal(SIGHUP, SIG_IGN);	/* for remote connections */
95 	if (kill(1, SIGTSTP) == -1) {
96 		fprintf(stderr, "reboot: can't idle init\n");
97 		exit(1);
98 	}
99 	sleep(1);
100 	(void) kill(-1, SIGTERM);	/* one chance to catch it */
101 	sleep(5);
102 
103 	if (!qflag) for (i = 1; ; i++) {
104 		if (kill(-1, SIGKILL) == -1) {
105 			extern int errno;
106 
107 			if (errno == ESRCH)
108 				break;
109 
110 			perror("reboot: kill");
111 			kill(1, SIGHUP);
112 			exit(1);
113 		}
114 		if (i > 5) {
115 			fprintf(stderr,
116 			    "CAUTION: some process(es) wouldn't die\n");
117 			break;
118 		}
119 		setalarm(2 * i);
120 		pause();
121 	}
122 
123 	if (!qflag && (howto & RB_NOSYNC) == 0) {
124 		logwtmp("~", "shutdown", "");
125 		sync();
126 		setalarm(5);
127 		pause();
128 	}
129 	syscall(SYS_reboot, howto);
130 	perror("reboot");
131 	kill(1, SIGHUP);
132 	exit(1);
133 }
134 
135 void
dingdong()136 dingdong()
137 {
138 	/* RRRIIINNNGGG RRRIIINNNGGG */
139 }
140 
setalarm(n)141 setalarm(n)
142 	int n;
143 {
144 	signal(SIGALRM, dingdong);
145 	alarm(n);
146 }
147