Commit | Line | Data |
---|---|---|
c018628f | 1 | /* |
61dbfbf1 KB |
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. | |
c018628f DF |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
61dbfbf1 | 20 | "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\ |
c018628f | 21 | All rights reserved.\n"; |
61dbfbf1 | 22 | #endif /* not lint */ |
c018628f | 23 | |
37c640e2 | 24 | #ifndef lint |
77b19d4d | 25 | static char sccsid[] = "@(#)reboot.c 5.7 (Berkeley) %G%"; |
61dbfbf1 | 26 | #endif /* not lint */ |
37c640e2 | 27 | |
cf5fda66 BJ |
28 | /* |
29 | * Reboot | |
30 | */ | |
fe6cb666 BJ |
31 | #include <stdio.h> |
32 | #include <sys/reboot.h> | |
33 | #include <errno.h> | |
34 | #include <signal.h> | |
bd699e5d | 35 | #include <pwd.h> |
3111ba40 | 36 | #include <sys/types.h> |
ce4fd43b | 37 | #include <sys/time.h> |
bd699e5d | 38 | #include <sys/syslog.h> |
77b19d4d | 39 | #include <sys/syscall.h> |
cf5fda66 BJ |
40 | |
41 | main(argc, argv) | |
42 | int argc; | |
43 | char **argv; | |
44 | { | |
45 | int howto; | |
46 | register char *argp; | |
fe6cb666 BJ |
47 | register i; |
48 | register ok = 0; | |
49 | register qflag = 0; | |
b0af3d20 | 50 | int needlog = 1; |
bd699e5d MK |
51 | char *user, *getlogin(); |
52 | struct passwd *pw, *getpwuid(); | |
cf5fda66 | 53 | |
f5a538b6 | 54 | openlog("reboot", 0, LOG_AUTH); |
cf5fda66 BJ |
55 | argc--, argv++; |
56 | howto = 0; | |
57 | while (argc > 0) { | |
fe6cb666 BJ |
58 | if (!strcmp(*argv, "-q")) |
59 | qflag++; | |
cf5fda66 BJ |
60 | else if (!strcmp(*argv, "-n")) |
61 | howto |= RB_NOSYNC; | |
b0af3d20 MK |
62 | else if (!strcmp(*argv, "-l")) |
63 | needlog = 0; | |
cf5fda66 BJ |
64 | else { |
65 | fprintf(stderr, | |
fe6cb666 | 66 | "usage: reboot [ -n ][ -q ]\n"); |
cf5fda66 BJ |
67 | exit(1); |
68 | } | |
69 | argc--, argv++; | |
70 | } | |
fe6cb666 | 71 | |
b0af3d20 MK |
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 | } | |
bd699e5d | 80 | |
7c280ab6 | 81 | signal(SIGHUP, SIG_IGN); /* for remote connections */ |
fe6cb666 BJ |
82 | if (kill(1, SIGTSTP) == -1) { |
83 | fprintf(stderr, "reboot: can't idle init\n"); | |
84 | exit(1); | |
85 | } | |
bd699e5d | 86 | sleep(1); |
b0af3d20 MK |
87 | (void) kill(-1, SIGTERM); /* one chance to catch it */ |
88 | sleep(5); | |
fe6cb666 BJ |
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) { | |
bd699e5d MK |
102 | fprintf(stderr, |
103 | "CAUTION: some process(es) wouldn't die\n"); | |
fe6cb666 BJ |
104 | break; |
105 | } | |
106 | setalarm(2 * i); | |
107 | pause(); | |
108 | } | |
109 | ||
bd699e5d | 110 | if (!qflag && (howto & RB_NOSYNC) == 0) { |
ffaf5d37 | 111 | logwtmp("~", "shutdown", ""); |
bd699e5d | 112 | sync(); |
fe6cb666 BJ |
113 | setalarm(5); |
114 | pause(); | |
115 | } | |
77b19d4d | 116 | syscall(SYS_reboot, howto); |
cf5fda66 | 117 | perror("reboot"); |
fe6cb666 BJ |
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 | } |