📄 shutdown.c
字号:
/* * shutdown.c Shut the system down. * * Usage: shutdown [-krhfnc] time [warning message] * -k: don't really shutdown, only warn. * -r: reboot after shutdown. * -h: halt after shutdown. * -f: do a 'fast' reboot (skip fsck). * -F: Force fsck on reboot. * -n: do not go through init but do it ourselves. * -c: cancel an already running shutdown. * -t secs: delay between SIGTERM and SIGKILL for init. * * Author: Miquel van Smoorenburg, miquels@cistron.nl * * Version: @(#)shutdown 2.86-1 31-Jul-2004 miquels@cistron.nl * * This file is part of the sysvinit suite, * Copyright 1991-2004 Miquel van Smoorenburg. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#include <sys/types.h>#include <sys/stat.h>#include <sys/wait.h>#include <time.h>#include <string.h>#include <unistd.h>#include <errno.h>#include <stdlib.h>#include <stdio.h> #include <signal.h>#include <fcntl.h>#include <stdarg.h>#include <utmp.h>#include <syslog.h>#include "paths.h"#include "reboot.h"#include "initreq.h"char *Version = "@(#) shutdown 2.86-1 31-Jul-2004 miquels@cistron.nl";#define MESSAGELEN 256int dontshut = 0; /* Don't shutdown, only warn */char down_level[2]; /* What runlevel to go to. */int dosync = 1; /* Sync before reboot or halt */int fastboot = 0; /* Do a 'fast' reboot */int forcefsck = 0; /* Force fsck on reboot */char message[MESSAGELEN]; /* Warning message */char *sltime = 0; /* Sleep time */char newstate[64]; /* What are we gonna do */int doself = 0; /* Don't use init */int got_alrm = 0;char *clean_env[] = { "HOME=/", "PATH=/bin:/usr/bin:/sbin:/usr/sbin", "TERM=dumb", NULL,};/* From "wall.c" */extern void wall(char *, int, int);/* From "utmp.c" */extern void write_wtmp(char *user, char *id, int pid, int type, char *line);/* * Sleep without being interrupted. */void hardsleep(int secs){ struct timespec ts, rem; ts.tv_sec = secs; ts.tv_nsec = 0; while(nanosleep(&ts, &rem) < 0 && errno == EINTR) ts = rem;}/* * Break off an already running shutdown. */void stopit(int sig){ unlink(NOLOGIN); unlink(FASTBOOT); unlink(FORCEFSCK); unlink(SDPID); printf("\r\nShutdown cancelled.\r\n"); exit(0);}/* * Show usage message. */void usage(void){ fprintf(stderr, "Usage:\t shutdown [-akrhHPfnc] [-t secs] time [warning message]\n" "\t\t -a: use /etc/shutdown.allow\n" "\t\t -k: don't really shutdown, only warn.\n" "\t\t -r: reboot after shutdown.\n" "\t\t -h: halt after shutdown.\n" "\t\t -P: halt action is to turn off power.\n" "\t\t -H: halt action is to just halt.\n" "\t\t -f: do a 'fast' reboot (skip fsck).\n" "\t\t -F: Force fsck on reboot.\n" "\t\t -n: do not go through \"init\" but go down real fast.\n" "\t\t -c: cancel a running shutdown.\n" "\t\t -t secs: delay between warning and kill signal.\n" "\t\t ** the \"time\" argument is mandatory! (try \"now\") **\n"); exit(1);}void alrm_handler(int sig){ got_alrm = sig;}/* * Set environment variables in the init process. */int init_setenv(char *name, char *value){ struct init_request request; struct sigaction sa; int fd; int nl, vl; memset(&request, 0, sizeof(request)); request.magic = INIT_MAGIC; request.cmd = INIT_CMD_SETENV; nl = strlen(name); vl = value ? strlen(value) : 0; if (nl + vl + 3 >= sizeof(request.i.data)) return -1; memcpy(request.i.data, name, nl); if (value) { request.i.data[nl] = '='; memcpy(request.i.data + nl + 1, value, vl); } /* * Open the fifo and write the command. * Make sure we don't hang on opening /dev/initctl */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = alrm_handler; sigaction(SIGALRM, &sa, NULL); got_alrm = 0; alarm(3); if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0 && write(fd, &request, sizeof(request)) == sizeof(request)) { close(fd); alarm(0); return 0; } fprintf(stderr, "shutdown: "); if (got_alrm) { fprintf(stderr, "timeout opening/writing control channel %s\n", INIT_FIFO); } else { perror(INIT_FIFO); } return -1;}/* * Tell everyone the system is going down in 'mins' minutes. */void warn(int mins){ char buf[MESSAGELEN + sizeof(newstate)]; int len; buf[0] = 0; strncat(buf, message, sizeof(buf) - 1); len = strlen(buf); if (mins == 0) snprintf(buf + len, sizeof(buf) - len, "\rThe system is going down %s NOW!\r\n", newstate); else snprintf(buf + len, sizeof(buf) - len, "\rThe system is going DOWN %s in %d minute%s!\r\n", newstate, mins, mins == 1 ? "" : "s"); wall(buf, 1, 0);}/* * Create the /etc/nologin file. */void donologin(int min){ FILE *fp; time_t t; time(&t); t += 60 * min; if ((fp = fopen(NOLOGIN, "w")) != NULL) { fprintf(fp, "\rThe system is going down on %s\r\n", ctime(&t)); if (message[0]) fputs(message, fp); fclose(fp); }}/* * Spawn an external program. */int spawn(int noerr, char *prog, ...){ va_list ap; pid_t pid, rc; int i; char *argv[8]; i = 0; while ((pid = fork()) < 0 && i < 10) { perror("fork"); sleep(5); i++; } if (pid < 0) return -1; if (pid > 0) { while((rc = wait(&i)) != pid) if (rc < 0 && errno == ECHILD) break; return (rc == pid) ? WEXITSTATUS(i) : -1; } if (noerr) fclose(stderr); argv[0] = prog; va_start(ap, prog); for (i = 1; i < 7 && (argv[i] = va_arg(ap, char *)) != NULL; i++) ; argv[i] = NULL; va_end(ap); chdir("/"); environ = clean_env; execvp(argv[0], argv); perror(argv[0]); exit(1); /*NOTREACHED*/ return 0;}/* * Kill all processes, call /etc/init.d/halt (if present) */void fastdown(){ int do_halt = (down_level[0] == '0'); int i;#if 0 char cmd[128]; char *script; /* * Currently, the halt script is either init.d/halt OR rc.d/rc.0, * likewise for the reboot script. Test for the presence * of either. */ if (do_halt) { if (access(HALTSCRIPT1, X_OK) == 0) script = HALTSCRIPT1; else script = HALTSCRIPT2; } else { if (access(REBOOTSCRIPT1, X_OK) == 0) script = REBOOTSCRIPT1; else script = REBOOTSCRIPT2; }#endif /* First close all files. */ for(i = 0; i < 3; i++) if (!isatty(i)) { close(i); open("/dev/null", O_RDWR); } for(i = 3; i < 20; i++) close(i); close(255); /* First idle init. */ if (kill(1, SIGTSTP) < 0) { fprintf(stderr, "shutdown: can't idle init.\r\n"); exit(1); } /* Kill all processes. */ fprintf(stderr, "shutdown: sending all processes the TERM signal...\r\n"); kill(-1, SIGTERM); sleep(sltime ? atoi(sltime) : 3); fprintf(stderr, "shutdown: sending all processes the KILL signal.\r\n"); (void) kill(-1, SIGKILL);#if 0 /* See if we can run /etc/init.d/halt */ if (access(script, X_OK) == 0) { spawn(1, cmd, "fast", NULL); fprintf(stderr, "shutdown: %s returned - falling back " "on default routines\r\n", script); }#endif /* script failed or not present: do it ourself. */ sleep(1); /* Give init the chance to collect zombies. */ /* Record the fact that we're going down */ write_wtmp("shutdown", "~~", 0, RUN_LVL, "~~"); /* This is for those who have quota installed. */ spawn(1, "accton", NULL); spawn(1, "quotaoff", "-a", NULL); sync(); fprintf(stderr, "shutdown: turning off swap\r\n"); spawn(0, "swapoff", "-a", NULL); fprintf(stderr, "shutdown: unmounting all file systems\r\n"); spawn(0, "umount", "-a", NULL); /* We're done, halt or reboot now. */ if (do_halt) { fprintf(stderr, "The system is halted. Press CTRL-ALT-DEL " "or turn off power\r\n"); init_reboot(BMAGIC_HALT); exit(0); } fprintf(stderr, "Please stand by while rebooting the system.\r\n"); init_reboot(BMAGIC_REBOOT); exit(0);}/* * Go to runlevel 0, 1 or 6.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -