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