Adjust newsyslog(8) and pkill(1) for the new PID_MAX.
[dragonfly.git] / sbin / shutdown / shutdown.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. 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.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
1de703da
MD
28 *
29 * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)shutdown.c 8.4 (Berkeley) 4/28/95
31 * $FreeBSD: src/sbin/shutdown/shutdown.c,v 1.21.2.1 2001/07/30 10:38:08 dd Exp $
984263bc
MD
32 */
33
984263bc
MD
34#include <sys/param.h>
35#include <sys/time.h>
36#include <sys/resource.h>
37#include <sys/syslog.h>
38
39#include <ctype.h>
40#include <err.h>
41#include <fcntl.h>
42#include <pwd.h>
43#include <setjmp.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50#include "pathnames.h"
51
52#ifdef DEBUG
53#undef _PATH_NOLOGIN
54#define _PATH_NOLOGIN "./nologin"
55#endif
56
57#define H *60*60
58#define M *60
59#define S *1
60#define NOLOG_TIME 5*60
26e3ecb3 61static struct interval {
984263bc
MD
62 int timeleft, timetowait;
63} tlist[] = {
64 { 10 H, 5 H },
65 { 5 H, 3 H },
66 { 2 H, 1 H },
67 { 1 H, 30 M },
68 { 30 M, 10 M },
69 { 20 M, 10 M },
70 { 10 M, 5 M },
71 { 5 M, 3 M },
72 { 2 M, 1 M },
73 { 1 M, 30 S },
74 { 30 S, 30 S },
75 { 0 , 0 }
76};
77#undef H
78#undef M
79#undef S
80
81static time_t offset, shuttime;
82static int dohalt, dopower, doreboot, killflg, mbuflen, oflag;
83static char mbuf[BUFSIZ];
84static const char *nosync, *whom;
85
26e3ecb3
MD
86static void badtime(void);
87static void die_you_gravy_sucking_pig_dog(void);
88static void finish(int);
89static void getoffset(char *);
90static void loop(void);
91static void nolog(void);
92static void timeout(int);
93static void timewarn(int);
94static void usage(const char *);
95
96extern const char **environ;
984263bc
MD
97
98int
b5744197 99main(int argc, char **argv)
984263bc
MD
100{
101 char *p, *endp;
102 struct passwd *pw;
103 int arglen, ch, len, readstdin;
104
105#ifndef DEBUG
106 if (geteuid())
107 errx(1, "NOT super-user");
108#endif
26e3ecb3 109
984263bc
MD
110 nosync = NULL;
111 readstdin = 0;
26e3ecb3
MD
112
113 /*
114 * Test for the special case where the utility is called as
115 * "poweroff", for which it runs 'shutdown -p now'.
116 */
117 if ((p = strrchr(argv[0], '/')) == NULL)
118 p = argv[0];
119 else
120 ++p;
121 if (strcmp(p, "poweroff") == 0) {
122 if (getopt(argc, argv, "") != -1)
79ac1d4a 123 usage(NULL);
26e3ecb3
MD
124 argc -= optind;
125 argv += optind;
126 if (argc != 0)
79ac1d4a 127 usage(NULL);
26e3ecb3
MD
128 dopower = 1;
129 offset = 0;
130 (void)time(&shuttime);
131 goto poweroff;
132 }
133
984263bc
MD
134 while ((ch = getopt(argc, argv, "-hknopr")) != -1)
135 switch (ch) {
136 case '-':
137 readstdin = 1;
138 break;
139 case 'h':
140 dohalt = 1;
141 break;
142 case 'k':
143 killflg = 1;
144 break;
145 case 'n':
146 nosync = "-n";
147 break;
148 case 'o':
149 oflag = 1;
150 break;
151 case 'p':
152 dopower = 1;
153 break;
154 case 'r':
155 doreboot = 1;
156 break;
157 case '?':
158 default:
2038fb68 159 usage(NULL);
984263bc
MD
160 }
161 argc -= optind;
162 argv += optind;
163
164 if (argc < 1)
2038fb68 165 usage(NULL);
984263bc
MD
166
167 if (killflg + doreboot + dohalt + dopower > 1)
168 usage("incompatible switches -h, -k, -p and -r");
169
170 if (oflag && !(dohalt || dopower || doreboot))
171 usage("-o requires -h, -p or -r");
172
173 if (nosync != NULL && !oflag)
174 usage("-n requires -o");
175
176 getoffset(*argv++);
177
26e3ecb3 178poweroff:
984263bc
MD
179 if (*argv) {
180 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
181 arglen = strlen(*argv);
182 if ((len -= arglen) <= 2)
183 break;
184 if (p != mbuf)
185 *p++ = ' ';
186 memmove(p, *argv, arglen);
187 p += arglen;
188 }
189 *p = '\n';
190 *++p = '\0';
191 }
192
193 if (readstdin) {
194 p = mbuf;
195 endp = mbuf + sizeof(mbuf) - 2;
196 for (;;) {
197 if (!fgets(p, endp - p + 1, stdin))
198 break;
199 for (; *p && p < endp; ++p);
200 if (p == endp) {
201 *p = '\n';
202 *++p = '\0';
203 break;
204 }
205 }
206 }
207 mbuflen = strlen(mbuf);
208
209 if (offset)
7cee7052 210 printf("Shutdown at %.24s.\n", ctime(&shuttime));
984263bc 211 else
7cee7052 212 printf("Shutdown NOW!\n");
984263bc
MD
213
214 if (!(whom = getlogin()))
215 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
216
217#ifdef DEBUG
7cee7052 218 putc('\n', stdout);
984263bc 219#else
7cee7052 220 setpriority(PRIO_PROCESS, 0, PRIO_MIN);
984263bc
MD
221 {
222 int forkpid;
223
224 forkpid = fork();
225 if (forkpid == -1)
226 err(1, "fork");
227 if (forkpid)
228 errx(0, "[pid %d]", forkpid);
229 }
230 setsid();
231#endif
232 openlog("shutdown", LOG_CONS, LOG_AUTH);
233 loop();
234 return(0);
235}
236
26e3ecb3 237static void
b5744197 238loop(void)
984263bc
MD
239{
240 struct interval *tp;
241 u_int sltime;
242 int logged;
243
244 if (offset <= NOLOG_TIME) {
245 logged = 1;
246 nolog();
247 }
248 else
249 logged = 0;
250 tp = tlist;
251 if (tp->timeleft < offset)
7cee7052 252 sleep((u_int)(offset - tp->timeleft));
984263bc
MD
253 else {
254 while (tp->timeleft && offset < tp->timeleft)
255 ++tp;
256 /*
257 * Warn now, if going to sleep more than a fifth of
258 * the next wait time.
259 */
260 if ((sltime = offset - tp->timeleft)) {
261 if (sltime > (u_int)(tp->timetowait / 5))
262 timewarn(offset);
7cee7052 263 sleep(sltime);
984263bc
MD
264 }
265 }
266 for (;; ++tp) {
267 timewarn(tp->timeleft);
268 if (!logged && tp->timeleft <= NOLOG_TIME) {
269 logged = 1;
270 nolog();
271 }
7cee7052 272 sleep((u_int)tp->timetowait);
984263bc
MD
273 if (!tp->timeleft)
274 break;
275 }
276 die_you_gravy_sucking_pig_dog();
277}
278
279static jmp_buf alarmbuf;
280
281static const char *restricted_environ[] = {
282 "PATH=" _PATH_STDPATH,
283 NULL
284};
285
26e3ecb3 286static void
b5744197 287timewarn(int timeleft)
984263bc
MD
288{
289 static int first;
290 static char hostname[MAXHOSTNAMELEN + 1];
291 FILE *pf;
292 char wcmd[MAXPATHLEN + 4];
984263bc
MD
293
294 if (!first++)
7cee7052 295 gethostname(hostname, sizeof(hostname));
984263bc
MD
296
297 /* undoc -n option to wall suppresses normal wall banner */
7cee7052 298 snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
984263bc
MD
299 environ = restricted_environ;
300 if (!(pf = popen(wcmd, "w"))) {
301 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
302 return;
303 }
304
7cee7052 305 fprintf(pf,
984263bc
MD
306 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
307 timeleft ? "": "FINAL ", whom, hostname);
308
309 if (timeleft > 10*60)
7cee7052 310 fprintf(pf, "System going down at %5.5s\n\n",
984263bc
MD
311 ctime(&shuttime) + 11);
312 else if (timeleft > 59)
7cee7052 313 fprintf(pf, "System going down in %d minute%s\n\n",
984263bc
MD
314 timeleft / 60, (timeleft > 60) ? "s" : "");
315 else if (timeleft)
7cee7052 316 fprintf(pf, "System going down in 30 seconds\n\n");
984263bc 317 else
7cee7052 318 fprintf(pf, "System going down IMMEDIATELY\n\n");
984263bc
MD
319
320 if (mbuflen)
7cee7052 321 fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
984263bc
MD
322
323 /*
324 * play some games, just in case wall doesn't come back
325 * probably unnecessary, given that wall is careful.
326 */
327 if (!setjmp(alarmbuf)) {
7cee7052
SW
328 signal(SIGALRM, timeout);
329 alarm((u_int)30);
330 pclose(pf);
331 alarm((u_int)0);
332 signal(SIGALRM, SIG_DFL);
984263bc
MD
333 }
334}
335
26e3ecb3 336static void
b5744197 337timeout(int signo __unused)
984263bc
MD
338{
339 longjmp(alarmbuf, 1);
340}
341
26e3ecb3 342static void
b5744197 343die_you_gravy_sucking_pig_dog(void)
984263bc
MD
344{
345 char *empty_environ[] = { NULL };
346
347 syslog(LOG_NOTICE, "%s by %s: %s",
26e3ecb3 348 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
984263bc 349 "shutdown", whom, mbuf);
7cee7052 350 sleep(2);
984263bc 351
7cee7052 352 printf("\r\nSystem shutdown time has arrived\007\007\r\n");
984263bc 353 if (killflg) {
7cee7052 354 printf("\rbut you'll have to do it yourself\r\n");
984263bc
MD
355 exit(0);
356 }
357#ifdef DEBUG
358 if (doreboot)
7cee7052 359 printf("reboot");
984263bc 360 else if (dohalt)
7cee7052 361 printf("halt");
984263bc 362 else if (dopower)
7cee7052 363 printf("power-down");
984263bc 364 if (nosync != NULL)
7cee7052
SW
365 printf(" no sync");
366 printf("\nkill -HUP 1\n");
984263bc
MD
367#else
368 if (!oflag) {
7cee7052
SW
369 kill(1, doreboot ? SIGINT : /* reboot */
370 dohalt ? SIGUSR1 : /* halt */
371 dopower ? SIGUSR2 : /* power-down */
372 SIGTERM); /* single-user */
984263bc
MD
373 } else {
374 if (doreboot) {
26e3ecb3 375 execle(_PATH_REBOOT, "reboot", "-l", nosync,
2038fb68 376 NULL, empty_environ);
984263bc
MD
377 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
378 _PATH_REBOOT);
379 warn(_PATH_REBOOT);
380 }
381 else if (dohalt) {
382 execle(_PATH_HALT, "halt", "-l", nosync,
2038fb68 383 NULL, empty_environ);
984263bc
MD
384 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
385 _PATH_HALT);
386 warn(_PATH_HALT);
387 }
388 else if (dopower) {
389 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
2038fb68 390 NULL, empty_environ);
984263bc
MD
391 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
392 _PATH_HALT);
393 warn(_PATH_HALT);
394 }
7cee7052 395 kill(1, SIGTERM); /* to single-user */
984263bc
MD
396 }
397#endif
398 finish(0);
399}
400
401#define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
402
26e3ecb3 403static void
b5744197 404getoffset(char *timearg)
984263bc
MD
405{
406 struct tm *lt;
407 char *p;
408 time_t now;
409 int this_year;
410
7cee7052 411 time(&now);
984263bc
MD
412
413 if (!strcasecmp(timearg, "now")) { /* now */
414 offset = 0;
415 shuttime = now;
416 return;
417 }
418
419 if (*timearg == '+') { /* +minutes */
420 if (!isdigit(*++timearg))
421 badtime();
422 if ((offset = atoi(timearg) * 60) < 0)
423 badtime();
424 shuttime = now + offset;
425 return;
426 }
427
428 /* handle hh:mm by getting rid of the colon */
429 for (p = timearg; *p; ++p)
430 if (!isascii(*p) || !isdigit(*p)) {
431 if (*p == ':' && strlen(p) == 3) {
432 p[0] = p[1];
433 p[1] = p[2];
434 p[2] = '\0';
435 }
436 else
437 badtime();
438 }
439
440 unsetenv("TZ"); /* OUR timezone */
441 lt = localtime(&now); /* current time val */
442
443 switch(strlen(timearg)) {
444 case 10:
445 this_year = lt->tm_year;
446 lt->tm_year = ATOI2(timearg);
447 /*
448 * check if the specified year is in the next century.
449 * allow for one year of user error as many people will
450 * enter n - 1 at the start of year n.
451 */
452 if (lt->tm_year < (this_year % 100) - 1)
453 lt->tm_year += 100;
454 /* adjust for the year 2000 and beyond */
455 lt->tm_year += (this_year - (this_year % 100));
456 /* FALLTHROUGH */
457 case 8:
458 lt->tm_mon = ATOI2(timearg);
459 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
460 badtime();
461 /* FALLTHROUGH */
462 case 6:
463 lt->tm_mday = ATOI2(timearg);
464 if (lt->tm_mday < 1 || lt->tm_mday > 31)
465 badtime();
466 /* FALLTHROUGH */
467 case 4:
468 lt->tm_hour = ATOI2(timearg);
469 if (lt->tm_hour < 0 || lt->tm_hour > 23)
470 badtime();
471 lt->tm_min = ATOI2(timearg);
472 if (lt->tm_min < 0 || lt->tm_min > 59)
473 badtime();
474 lt->tm_sec = 0;
475 if ((shuttime = mktime(lt)) == -1)
476 badtime();
477 if ((offset = shuttime - now) < 0)
478 errx(1, "that time is already past.");
479 break;
480 default:
481 badtime();
482 }
483}
484
485#define NOMSG "\n\nNO LOGINS: System going down at "
26e3ecb3 486static void
b5744197 487nolog(void)
984263bc
MD
488{
489 int logfd;
490 char *ct;
491
7cee7052
SW
492 unlink(_PATH_NOLOGIN); /* in case linked to another file */
493 signal(SIGINT, finish);
494 signal(SIGHUP, finish);
495 signal(SIGQUIT, finish);
496 signal(SIGTERM, finish);
984263bc
MD
497 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
498 0664)) >= 0) {
7cee7052 499 write(logfd, NOMSG, sizeof(NOMSG) - 1);
984263bc 500 ct = ctime(&shuttime);
7cee7052
SW
501 write(logfd, ct + 11, 5);
502 write(logfd, "\n\n", 2);
503 write(logfd, mbuf, strlen(mbuf));
504 close(logfd);
984263bc
MD
505 }
506}
507
26e3ecb3 508static void
b5744197 509finish(int signo __unused)
984263bc
MD
510{
511 if (!killflg)
7cee7052 512 unlink(_PATH_NOLOGIN);
984263bc
MD
513 exit(0);
514}
515
26e3ecb3 516static void
b5744197 517badtime(void)
984263bc
MD
518{
519 errx(1, "bad time format");
520}
521
26e3ecb3 522static void
b5744197 523usage(const char *cp)
984263bc
MD
524{
525 if (cp != NULL)
526 warnx("%s", cp);
7cee7052 527 fprintf(stderr,
984263bc 528 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
26e3ecb3
MD
529 " time [warning-message ...]\n"
530 " poweroff\n");
984263bc
MD
531 exit(1);
532}