2018-11-23 13:13:23 +01:00
|
|
|
/* SPDX-License-Identifier: ISC */
|
2018-02-25 14:33:19 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-08-24 10:35:23 +02:00
|
|
|
#include <signal.h>
|
2018-02-25 14:33:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
#include <linux/reboot.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define FL_FORCE 0x01
|
|
|
|
#define FL_NOSYNC 0x02
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "poweroff", no_argument, NULL, 'p' },
|
|
|
|
{ "reboot", no_argument, NULL, 'r' },
|
|
|
|
{ "force", no_argument, NULL, 'f' },
|
|
|
|
{ "no-sync", no_argument, NULL, 'n' },
|
|
|
|
{ NULL, 0, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
2018-11-23 14:13:52 +01:00
|
|
|
static const char *shortopt = "hprfn";
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
static const char *defact_str = "power-off";
|
2018-08-24 10:35:23 +02:00
|
|
|
static int defact = RB_POWER_OFF;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-08-24 10:35:23 +02:00
|
|
|
static NORETURN void usage(const char *progname, int status)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
|
|
|
fprintf(status == EXIT_SUCCESS ? stdout : stderr,
|
|
|
|
"%s [OPTIONS...]\n\n"
|
|
|
|
"Perform a system shutdown or reboot.\n\n"
|
|
|
|
" -h, --help Display this help text and exit.\n"
|
|
|
|
" -V, --version Display version information and exit.\n"
|
|
|
|
" -p, --poweroff Power-off the machine.\n"
|
|
|
|
" -r, --reboot Reboot the machine.\n"
|
|
|
|
" -f, --force Force immediate power-off or reboot. Do not contact the\n"
|
|
|
|
" init system.\n"
|
|
|
|
" -n, --no-sync Don't sync storage media before power-off or reboot.\n\n"
|
|
|
|
"If no option is specified, the default action is %s.\n",
|
2018-08-24 10:35:23 +02:00
|
|
|
progname, defact_str);
|
2018-02-25 14:33:19 +01:00
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-08-24 10:35:23 +02:00
|
|
|
int c, ret, flags = 0;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ptr = strrchr(argv[0], '/');
|
|
|
|
ptr = (ptr == NULL) ? argv[0] : (ptr + 1);
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-08-24 10:35:23 +02:00
|
|
|
if (strcmp(ptr, "reboot") == 0) {
|
2018-02-25 14:33:19 +01:00
|
|
|
defact_str = "reboot";
|
2018-08-24 10:35:23 +02:00
|
|
|
defact = RB_AUTOBOOT;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
c = getopt_long(argc, argv, shortopt, options, NULL);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'f':
|
|
|
|
flags |= FL_FORCE;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
flags |= FL_NOSYNC;
|
|
|
|
break;
|
|
|
|
case 'p':
|
2018-08-24 10:35:23 +02:00
|
|
|
defact = RB_POWER_OFF;
|
2018-02-25 14:33:19 +01:00
|
|
|
break;
|
|
|
|
case 'r':
|
2018-08-24 10:35:23 +02:00
|
|
|
defact = RB_AUTOBOOT;
|
2018-02-25 14:33:19 +01:00
|
|
|
break;
|
|
|
|
case 'h':
|
2018-08-24 10:35:23 +02:00
|
|
|
usage(ptr, EXIT_SUCCESS);
|
2018-02-25 14:33:19 +01:00
|
|
|
default:
|
2018-08-24 10:35:23 +02:00
|
|
|
usage(ptr, EXIT_FAILURE);
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FL_FORCE) {
|
|
|
|
if (!(flags & FL_NOSYNC))
|
|
|
|
sync();
|
2018-08-24 10:35:23 +02:00
|
|
|
reboot(defact);
|
2018-02-25 14:33:19 +01:00
|
|
|
perror("reboot system call");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-08-24 10:35:23 +02:00
|
|
|
switch (defact) {
|
|
|
|
case RB_AUTOBOOT:
|
|
|
|
ret = kill(1, SIGINT);
|
|
|
|
break;
|
|
|
|
case RB_POWER_OFF:
|
|
|
|
ret = kill(1, SIGTERM);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-08-24 10:35:23 +02:00
|
|
|
if (ret) {
|
|
|
|
perror("sending signal to init");
|
2018-02-25 14:33:19 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|