-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreboot.cpp
More file actions
69 lines (62 loc) · 1.74 KB
/
Copy pathreboot.cpp
File metadata and controls
69 lines (62 loc) · 1.74 KB
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
#include <cfbox/error.hpp>
#include <cfbox/help.hpp>
#include <cstring>
#include <sys/reboot.h>
#include <unistd.h>
namespace {
constexpr cfbox::help::HelpEntry HELP_REBOOT = {
.name = "reboot",
.version = CFBOX_VERSION_STRING,
.one_line = "reboot the system",
.usage = "reboot",
.options = "",
.extra = "",
};
constexpr cfbox::help::HelpEntry HELP_POWEROFF = {
.name = "poweroff",
.version = CFBOX_VERSION_STRING,
.one_line = "power off the system",
.usage = "poweroff",
.options = "",
.extra = "",
};
} // namespace
auto reboot_main(int argc, char* argv[]) -> int {
// Honor --help/--version before touching the reboot syscall.
for (int i = 1; i < argc; ++i) {
std::string_view a{argv[i]};
if (a == "--help") {
cfbox::help::print_help(HELP_REBOOT);
return 0;
}
if (a == "--version") {
cfbox::help::print_version(HELP_REBOOT);
return 0;
}
}
sync(); // never reboot with dirty filesystems (gotcha #6)
if (reboot(RB_AUTOBOOT) != 0) {
CFBOX_ERR("reboot", "%s", std::strerror(errno));
return 1;
}
return 0; // unreachable on a successful reboot
}
auto poweroff_main(int argc, char* argv[]) -> int {
for (int i = 1; i < argc; ++i) {
std::string_view a{argv[i]};
if (a == "--help") {
cfbox::help::print_help(HELP_POWEROFF);
return 0;
}
if (a == "--version") {
cfbox::help::print_version(HELP_POWEROFF);
return 0;
}
}
sync();
if (reboot(RB_POWER_OFF) != 0) {
CFBOX_ERR("poweroff", "%s", std::strerror(errno));
return 1;
}
return 0; // unreachable on a successful poweroff
}