| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-daemon.h" |
| 4 |
|
| 5 |
#include "ansi-color.h" |
| 6 |
#include "format-table.h" |
| 7 |
#include "help-util.h" |
| 8 |
#include "log.h" |
| 9 |
#include "options.h" |
| 10 |
#include "process-util.h" |
| 11 |
#include "reboot-util.h" |
| 12 |
#include "systemctl.h" |
| 13 |
#include "systemctl-compat-halt.h" |
| 14 |
#include "systemctl-logind.h" |
| 15 |
#include "systemctl-start-unit.h" |
| 16 |
#include "systemctl-util.h" |
| 17 |
#include "utmp-wtmp.h" |
| 18 |
|
| 19 |
static int halt_help(void) { |
| 20 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 21 |
int r; |
| 22 |
|
| 23 |
r = option_parser_get_help_table_ns("halt", &options); |
| 24 |
if (r < 0) |
| 25 |
return r; |
| 26 |
|
| 27 |
/* Note: if you are tempted to add new command line switches here, please do not. Let this |
| 28 |
* compatibility command rest in peace. Its interface is not even owned by us as much as it is by |
| 29 |
* sysvinit. If you add something new, add it to "systemctl halt", "systemctl reboot", "systemctl |
| 30 |
* poweroff" instead. */ |
| 31 |
|
| 32 |
help_cmdline(arg_action == ACTION_REBOOT ? "[OPTIONS…] [ARG]" : "[OPTIONS…]"); |
| 33 |
help_abstract(arg_action == ACTION_REBOOT ? "Reboot the system." : |
| 34 |
arg_action == ACTION_POWEROFF ? "Power off the system." : |
| 35 |
"Halt the system."); |
| 36 |
|
| 37 |
help_section("Options"); |
| 38 |
r = table_print_or_warn(options); |
| 39 |
if (r < 0) |
| 40 |
return r; |
| 41 |
|
| 42 |
printf("\n%sThis is a compatibility interface, please use the more powerful 'systemctl %s' command instead.%s\n", |
| 43 |
ansi_highlight_red(), |
| 44 |
arg_action == ACTION_REBOOT ? "reboot" : |
| 45 |
arg_action == ACTION_POWEROFF ? "poweroff" : |
| 46 |
"halt", |
| 47 |
ansi_normal()); |
| 48 |
|
| 49 |
help_man_page_reference("halt", "8"); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
int halt_parse_argv(int argc, char *argv[], int log_level_shift) { |
| 54 |
int r; |
| 55 |
|
| 56 |
assert(argc >= 0); |
| 57 |
assert(argv); |
| 58 |
|
| 59 |
OptionParser opts = { |
| 60 |
argc, argv, |
| 61 |
.namespace = "halt", |
| 62 |
.log_level_shift = log_level_shift, |
| 63 |
}; |
| 64 |
|
| 65 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 66 |
switch (c) { |
| 67 |
|
| 68 |
OPTION_NAMESPACE("halt"): {} |
| 69 |
|
| 70 |
OPTION_LONG("help", NULL, "Show this help"): |
| 71 |
return halt_help(); |
| 72 |
|
| 73 |
OPTION_LONG("halt", NULL, "Halt the machine"): |
| 74 |
arg_action = ACTION_HALT; |
| 75 |
break; |
| 76 |
|
| 77 |
OPTION('p', "poweroff", NULL, "Switch off the machine"): |
| 78 |
if (arg_action != ACTION_REBOOT) |
| 79 |
arg_action = ACTION_POWEROFF; |
| 80 |
break; |
| 81 |
|
| 82 |
OPTION_LONG("reboot", NULL, "Reboot the machine"): |
| 83 |
arg_action = ACTION_REBOOT; |
| 84 |
break; |
| 85 |
|
| 86 |
OPTION('f', "force", NULL, "Force immediate halt/power-off/reboot"): |
| 87 |
arg_force = 2; |
| 88 |
break; |
| 89 |
|
| 90 |
OPTION('w', "wtmp-only", NULL, "Don't halt/power-off/reboot, just write wtmp record"): |
| 91 |
arg_dry_run = true; |
| 92 |
break; |
| 93 |
|
| 94 |
OPTION('d', "no-wtmp", NULL, "Don't write wtmp record"): |
| 95 |
arg_no_wtmp = true; |
| 96 |
break; |
| 97 |
|
| 98 |
OPTION_LONG("no-wall", NULL, "Don't send wall message before halt/power-off/reboot"): |
| 99 |
arg_no_wall = true; |
| 100 |
break; |
| 101 |
|
| 102 |
/* Hidden compat-only options. */ |
| 103 |
OPTION('n', "no-sync", NULL, /* help= */ NULL): |
| 104 |
arg_no_sync = true; |
| 105 |
break; |
| 106 |
|
| 107 |
OPTION_SHORT('i', NULL, /* help= */ NULL): {} |
| 108 |
OPTION_SHORT('h', NULL, /* help= */ NULL): |
| 109 |
/* Compatibility nops */ |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
char **args = option_parser_get_args(&opts); |
| 114 |
size_t n_args = option_parser_get_n_args(&opts); |
| 115 |
|
| 116 |
if (arg_action == ACTION_REBOOT && n_args <= 1) { |
| 117 |
r = update_reboot_parameter_and_warn(args[0], false); |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
} else if (n_args > 0) |
| 121 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 122 |
"Too many arguments."); |
| 123 |
|
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
int halt_main(void) { |
| 128 |
int r; |
| 129 |
|
| 130 |
if (arg_force == 0) { |
| 131 |
/* always try logind first */ |
| 132 |
if (arg_when > 0) |
| 133 |
r = logind_schedule_shutdown(arg_action); |
| 134 |
else { |
| 135 |
r = logind_check_inhibitors(arg_action); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
r = logind_reboot(arg_action); |
| 140 |
} |
| 141 |
if (r >= 0) |
| 142 |
return r; |
| 143 |
if (IN_SET(r, -EACCES, -EOPNOTSUPP, -EINPROGRESS)) |
| 144 |
/* Requested operation requires auth, is not supported on the local system or already in |
| 145 |
* progress */ |
| 146 |
return r; |
| 147 |
/* on all other errors, try low-level operation */ |
| 148 |
|
| 149 |
/* In order to minimize the difference between operation with and without logind, we explicitly |
| 150 |
* enable non-blocking mode for this, as logind's shutdown operations are always non-blocking. */ |
| 151 |
arg_no_block = true; |
| 152 |
|
| 153 |
if (!arg_dry_run) |
| 154 |
return verb_start(0, NULL, /* data= */ 0, NULL); |
| 155 |
} |
| 156 |
|
| 157 |
r = must_be_root(); |
| 158 |
if (r < 0) |
| 159 |
return r; |
| 160 |
|
| 161 |
if (!arg_no_wtmp) { |
| 162 |
if (sd_booted() > 0) |
| 163 |
log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); |
| 164 |
else { |
| 165 |
r = utmp_put_shutdown(); |
| 166 |
if (r < 0) |
| 167 |
log_warning_errno(r, "Failed to write utmp record: %m"); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if (arg_dry_run) |
| 172 |
return 0; |
| 173 |
|
| 174 |
r = halt_now(arg_action); |
| 175 |
return log_error_errno(r, "Failed to %s: %m", action_table[arg_action].verb); |
| 176 |
} |