| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "ansi-color.h" |
| 4 |
#include "format-table.h" |
| 5 |
#include "help-util.h" |
| 6 |
#include "log.h" |
| 7 |
#include "options.h" |
| 8 |
#include "parse-util.h" |
| 9 |
#include "reboot-util.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "strv.h" |
| 12 |
#include "systemctl.h" |
| 13 |
#include "systemctl-compat-shutdown.h" |
| 14 |
#include "time-util.h" |
| 15 |
|
| 16 |
static int shutdown_help(void) { |
| 17 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 18 |
int r; |
| 19 |
|
| 20 |
r = option_parser_get_help_table_ns("shutdown", &options); |
| 21 |
if (r < 0) |
| 22 |
return r; |
| 23 |
|
| 24 |
/* Note: if you are tempted to add new command line switches here, please do not. Let this |
| 25 |
* compatibility command rest in peace. Its interface is not even owned by us as much as it is by |
| 26 |
* sysvinit. If you add something new, add it to "systemctl halt", "systemctl reboot", "systemctl |
| 27 |
* poweroff" instead. */ |
| 28 |
|
| 29 |
help_cmdline("[OPTIONS…] [TIME] [WALL…]"); |
| 30 |
help_abstract("Shut down the system."); |
| 31 |
|
| 32 |
help_section("Options"); |
| 33 |
r = table_print_or_warn(options); |
| 34 |
if (r < 0) |
| 35 |
return r; |
| 36 |
|
| 37 |
printf("\n%sThis is a compatibility interface, please use the more powerful 'systemctl halt',\n" |
| 38 |
"'systemctl poweroff', 'systemctl reboot' commands instead.%s\n", |
| 39 |
ansi_highlight_red(), ansi_normal()); |
| 40 |
|
| 41 |
help_man_page_reference("shutdown", "8"); |
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
static int parse_shutdown_time_spec(const char *t, usec_t *ret) { |
| 46 |
int r; |
| 47 |
|
| 48 |
assert(t); |
| 49 |
assert(ret); |
| 50 |
|
| 51 |
/* This parses SysV compat time spec. */ |
| 52 |
|
| 53 |
if (streq(t, "now")) |
| 54 |
*ret = 0; |
| 55 |
else if (!strchr(t, ':')) { |
| 56 |
uint64_t u; |
| 57 |
|
| 58 |
if (safe_atou64(t, &u) < 0) |
| 59 |
return -EINVAL; |
| 60 |
|
| 61 |
*ret = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u; |
| 62 |
} else { |
| 63 |
char *e = NULL; |
| 64 |
long hour, minute; |
| 65 |
|
| 66 |
errno = 0; |
| 67 |
hour = strtol(t, &e, 10); |
| 68 |
if (errno > 0 || *e != ':' || hour < 0 || hour > 23) |
| 69 |
return -EINVAL; |
| 70 |
|
| 71 |
minute = strtol(e+1, &e, 10); |
| 72 |
if (errno > 0 || *e != 0 || minute < 0 || minute > 59) |
| 73 |
return -EINVAL; |
| 74 |
|
| 75 |
usec_t n = now(CLOCK_REALTIME); |
| 76 |
struct tm tm = {}; |
| 77 |
|
| 78 |
r = localtime_or_gmtime_usec(n, /* utc= */ false, &tm); |
| 79 |
if (r < 0) |
| 80 |
return r; |
| 81 |
|
| 82 |
tm.tm_hour = (int) hour; |
| 83 |
tm.tm_min = (int) minute; |
| 84 |
tm.tm_sec = 0; |
| 85 |
|
| 86 |
usec_t s; |
| 87 |
r = mktime_or_timegm_usec(&tm, /* utc= */ false, &s); |
| 88 |
if (r < 0) |
| 89 |
return r; |
| 90 |
|
| 91 |
if (s <= n) { |
| 92 |
/* The specified time is today, but in the past. We need to schedule it for tomorrow |
| 93 |
* at the same time. Adding USEC_PER_DAY would be wrong across DST changes, so just |
| 94 |
* let mktime() normalise it. */ |
| 95 |
int requested_hour = tm.tm_hour; |
| 96 |
int requested_min = tm.tm_min; |
| 97 |
|
| 98 |
tm.tm_mday++; |
| 99 |
tm.tm_isdst = -1; |
| 100 |
r = mktime_or_timegm_usec(&tm, /* utc= */ false, &s); |
| 101 |
if (r < 0) |
| 102 |
return r; |
| 103 |
|
| 104 |
if (tm.tm_hour != requested_hour || tm.tm_min != requested_min) |
| 105 |
log_warning("Requested shutdown time %02d:%02d does not exist. " |
| 106 |
"Rescheduling to %02d:%02d.", |
| 107 |
requested_hour, |
| 108 |
requested_min, |
| 109 |
tm.tm_hour, |
| 110 |
tm.tm_min); |
| 111 |
} |
| 112 |
|
| 113 |
*ret = s; |
| 114 |
} |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
int shutdown_parse_argv(int argc, char *argv[], int log_level_shift) { |
| 120 |
int r; |
| 121 |
|
| 122 |
assert(argc >= 0); |
| 123 |
assert(argv); |
| 124 |
|
| 125 |
OptionParser opts = { |
| 126 |
argc, argv, |
| 127 |
.namespace = "shutdown", |
| 128 |
.log_level_shift = log_level_shift, |
| 129 |
}; |
| 130 |
|
| 131 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 132 |
switch (c) { |
| 133 |
|
| 134 |
OPTION_NAMESPACE("shutdown"): {} |
| 135 |
|
| 136 |
OPTION_LONG("help", NULL, "Show this help"): |
| 137 |
return shutdown_help(); |
| 138 |
|
| 139 |
OPTION('H', "halt", NULL, "Halt the machine"): |
| 140 |
arg_action = ACTION_HALT; |
| 141 |
break; |
| 142 |
|
| 143 |
OPTION('P', "poweroff", NULL, "Power-off the machine"): |
| 144 |
arg_action = ACTION_POWEROFF; |
| 145 |
break; |
| 146 |
|
| 147 |
OPTION('r', "reboot", NULL, "Reboot the machine"): |
| 148 |
if (kexec_loaded()) |
| 149 |
arg_action = ACTION_KEXEC; |
| 150 |
else |
| 151 |
arg_action = ACTION_REBOOT; |
| 152 |
break; |
| 153 |
|
| 154 |
OPTION_SHORT('h', NULL, "Equivalent to --poweroff, overridden by --halt"): |
| 155 |
if (arg_action != ACTION_HALT) |
| 156 |
arg_action = ACTION_POWEROFF; |
| 157 |
break; |
| 158 |
|
| 159 |
OPTION_SHORT('k', NULL, "Don't halt/power-off/reboot, just send warnings"): |
| 160 |
arg_dry_run = true; |
| 161 |
break; |
| 162 |
|
| 163 |
OPTION_LONG("no-wall", NULL, "Don't send wall message before halt/power-off/reboot"): |
| 164 |
arg_no_wall = true; |
| 165 |
break; |
| 166 |
|
| 167 |
OPTION_SHORT('c', NULL, "Cancel a pending shutdown"): |
| 168 |
arg_action = ACTION_CANCEL_SHUTDOWN; |
| 169 |
break; |
| 170 |
|
| 171 |
OPTION_LONG("show", NULL, "Show pending shutdown"): |
| 172 |
arg_action = ACTION_SHOW_SHUTDOWN; |
| 173 |
break; |
| 174 |
|
| 175 |
/* Hidden compat options. */ |
| 176 |
OPTION('K', "kexec", NULL, /* help= */ NULL): |
| 177 |
arg_action = ACTION_KEXEC; |
| 178 |
break; |
| 179 |
|
| 180 |
OPTION_SHORT('a', NULL, /* help= */ NULL): {} /* compatibility noops */ |
| 181 |
OPTION_SHORT('f', NULL, /* help= */ NULL): {} |
| 182 |
OPTION_SHORT('F', NULL, /* help= */ NULL): {} |
| 183 |
OPTION_SHORT('t', "ARG", /* help= */ NULL): |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
char **args = option_parser_get_args(&opts); |
| 188 |
size_t n_args = option_parser_get_n_args(&opts); |
| 189 |
|
| 190 |
if (n_args > 0 && arg_action != ACTION_CANCEL_SHUTDOWN) { |
| 191 |
r = parse_shutdown_time_spec(args[0], &arg_when); |
| 192 |
if (r < 0) |
| 193 |
return log_error_errno(r, "Failed to parse time specification: %s", args[0]); |
| 194 |
} else |
| 195 |
arg_when = USEC_INFINITY; /* logind chooses on server side */ |
| 196 |
|
| 197 |
char **wall = NULL; |
| 198 |
if (n_args > 0 && arg_action == ACTION_CANCEL_SHUTDOWN) |
| 199 |
/* No time argument for shutdown cancel */ |
| 200 |
wall = args; |
| 201 |
else if (n_args > 1) |
| 202 |
/* We skip the time argument */ |
| 203 |
wall = args + 1; |
| 204 |
|
| 205 |
if (wall) { |
| 206 |
char **copy = strv_copy(wall); |
| 207 |
if (!copy) |
| 208 |
return log_oom(); |
| 209 |
strv_free_and_replace(arg_wall, copy); |
| 210 |
} |
| 211 |
|
| 212 |
return 1; |
| 213 |
} |