edit to put into official format; minor gramatical corrections
[unix-history] / usr / src / sbin / reboot / reboot.c
CommitLineData
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
19char 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
61dbfbf1
KB
25static char sccsid[] = "@(#)reboot.c 5.6 (Berkeley) %G%";
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>
cf5fda66
BJ
39
40main(argc, argv)
41 int argc;
42 char **argv;
43{
44 int howto;
45 register char *argp;
fe6cb666
BJ
46 register i;
47 register ok = 0;
48 register qflag = 0;
b0af3d20 49 int needlog = 1;
bd699e5d
MK
50 char *user, *getlogin();
51 struct passwd *pw, *getpwuid();
cf5fda66 52
f5a538b6 53 openlog("reboot", 0, LOG_AUTH);
cf5fda66
BJ
54 argc--, argv++;
55 howto = 0;
56 while (argc > 0) {
fe6cb666
BJ
57 if (!strcmp(*argv, "-q"))
58 qflag++;
cf5fda66
BJ
59 else if (!strcmp(*argv, "-n"))
60 howto |= RB_NOSYNC;
b0af3d20
MK
61 else if (!strcmp(*argv, "-l"))
62 needlog = 0;
cf5fda66
BJ
63 else {
64 fprintf(stderr,
fe6cb666 65 "usage: reboot [ -n ][ -q ]\n");
cf5fda66
BJ
66 exit(1);
67 }
68 argc--, argv++;
69 }
fe6cb666 70
b0af3d20
MK
71 if (needlog) {
72 user = getlogin();
73 if (user == (char *)0 && (pw = getpwuid(getuid())))
74 user = pw->pw_name;
75 if (user == (char *)0)
76 user = "root";
77 syslog(LOG_CRIT, "rebooted by %s", user);
78 }
bd699e5d 79
7c280ab6 80 signal(SIGHUP, SIG_IGN); /* for remote connections */
fe6cb666
BJ
81 if (kill(1, SIGTSTP) == -1) {
82 fprintf(stderr, "reboot: can't idle init\n");
83 exit(1);
84 }
bd699e5d 85 sleep(1);
b0af3d20
MK
86 (void) kill(-1, SIGTERM); /* one chance to catch it */
87 sleep(5);
fe6cb666
BJ
88
89 if (!qflag) for (i = 1; ; i++) {
90 if (kill(-1, SIGKILL) == -1) {
91 extern int errno;
92
93 if (errno == ESRCH)
94 break;
95
96 perror("reboot: kill");
97 kill(1, SIGHUP);
98 exit(1);
99 }
100 if (i > 5) {
bd699e5d
MK
101 fprintf(stderr,
102 "CAUTION: some process(es) wouldn't die\n");
fe6cb666
BJ
103 break;
104 }
105 setalarm(2 * i);
106 pause();
107 }
108
bd699e5d 109 if (!qflag && (howto & RB_NOSYNC) == 0) {
ffaf5d37 110 logwtmp("~", "shutdown", "");
bd699e5d 111 sync();
fe6cb666
BJ
112 setalarm(5);
113 pause();
114 }
cf5fda66
BJ
115 syscall(55, howto);
116 perror("reboot");
fe6cb666
BJ
117 kill(1, SIGHUP);
118 exit(1);
119}
120
121dingdong()
122{
123 /* RRRIIINNNGGG RRRIIINNNGGG */
124}
125
126setalarm(n)
127{
128 signal(SIGALRM, dingdong);
129 alarm(n);
130}