aboutsummaryrefslogtreecommitdiff
path: root/cmd/shutdown.c
blob: 9baaef23e2ff57acac98caefb381c56994a57057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * Copyright (C) 2018 - David Oberhollenzer
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#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' },
	{ "version", no_argument, NULL, 'V' },
	{ "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 },
};

static const char *shortopt = "hVprfn";

static const char *defact_str = "power-off";
static int defact = RB_POWER_OFF;

static NORETURN void usage(const char *progname, int status)
{
	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",
	progname, defact_str);
	exit(status);
}

int main(int argc, char **argv)
{
	int c, ret, flags = 0;
	char *ptr;

	ptr = strrchr(argv[0], '/');
	ptr = (ptr == NULL) ? argv[0] : (ptr + 1);

	if (strcmp(ptr, "reboot") == 0) {
		defact_str = "reboot";
		defact = RB_AUTOBOOT;
	}

	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':
			defact = RB_POWER_OFF;
			break;
		case 'r':
			defact = RB_AUTOBOOT;
			break;
		case 'V':
			print_version(ptr);
		case 'h':
			usage(ptr, EXIT_SUCCESS);
		default:
			usage(ptr, EXIT_FAILURE);
		}
	}

	if (flags & FL_FORCE) {
		if (!(flags & FL_NOSYNC))
			sync();
		reboot(defact);
		perror("reboot system call");
		return EXIT_FAILURE;
	}

	switch (defact) {
	case RB_AUTOBOOT:
		ret = kill(1, SIGINT);
		break;
	case RB_POWER_OFF:
		ret = kill(1, SIGTERM);
		break;
	default:
		return EXIT_SUCCESS;
	}

	if (ret) {
		perror("sending signal to init");
		return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}