xref: /original-bsd/sbin/reboot/reboot.c (revision 4aa23bb2)
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 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.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)reboot.c	5.8 (Berkeley) 03/05/89";
26 #endif /* not lint */
27 
28 /*
29  * Reboot
30  */
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/syslog.h>
34 #include <sys/syscall.h>
35 #include <sys/reboot.h>
36 #include <sys/signal.h>
37 #include <pwd.h>
38 #include <stdio.h>
39 #include <errno.h>
40 
41 main(argc, argv)
42 	int argc;
43 	char **argv;
44 {
45 	int howto;
46 	register char *argp;
47 	register i;
48 	register ok = 0;
49 	register qflag = 0;
50 	int needlog = 1;
51 	char *user, *getlogin();
52 	struct passwd *pw, *getpwuid();
53 
54 	openlog("reboot", 0, LOG_AUTH);
55 	argc--, argv++;
56 	howto = 0;
57 	while (argc > 0) {
58 		if (!strcmp(*argv, "-q"))
59 			qflag++;
60 		else if (!strcmp(*argv, "-n"))
61 			howto |= RB_NOSYNC;
62 		else if (!strcmp(*argv, "-l"))
63 			needlog = 0;
64 		else {
65 			fprintf(stderr,
66 			    "usage: reboot [ -n ][ -q ]\n");
67 			exit(1);
68 		}
69 		argc--, argv++;
70 	}
71 
72 	if (needlog) {
73 		user = getlogin();
74 		if (user == (char *)0 && (pw = getpwuid(getuid())))
75 			user = pw->pw_name;
76 		if (user == (char *)0)
77 			user = "root";
78 		syslog(LOG_CRIT, "rebooted by %s", user);
79 	}
80 
81 	signal(SIGHUP, SIG_IGN);	/* for remote connections */
82 	if (kill(1, SIGTSTP) == -1) {
83 		fprintf(stderr, "reboot: can't idle init\n");
84 		exit(1);
85 	}
86 	sleep(1);
87 	(void) kill(-1, SIGTERM);	/* one chance to catch it */
88 	sleep(5);
89 
90 	if (!qflag) for (i = 1; ; i++) {
91 		if (kill(-1, SIGKILL) == -1) {
92 			extern int errno;
93 
94 			if (errno == ESRCH)
95 				break;
96 
97 			perror("reboot: kill");
98 			kill(1, SIGHUP);
99 			exit(1);
100 		}
101 		if (i > 5) {
102 			fprintf(stderr,
103 			    "CAUTION: some process(es) wouldn't die\n");
104 			break;
105 		}
106 		setalarm(2 * i);
107 		pause();
108 	}
109 
110 	if (!qflag && (howto & RB_NOSYNC) == 0) {
111 		logwtmp("~", "shutdown", "");
112 		sync();
113 		setalarm(5);
114 		pause();
115 	}
116 	syscall(SYS_reboot, howto);
117 	perror("reboot");
118 	kill(1, SIGHUP);
119 	exit(1);
120 }
121 
122 dingdong()
123 {
124 	/* RRRIIINNNGGG RRRIIINNNGGG */
125 }
126 
127 setalarm(n)
128 {
129 	signal(SIGALRM, dingdong);
130 	alarm(n);
131 }
132