1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "pretty-print.h"
7 #include "systemctl-compat-shutdown.h"
8 #include "systemctl-sysv-compat.h"
10 #include "terminal-util.h"
12 static int shutdown_help(void) {
13 _cleanup_free_
char *link
= NULL
;
16 r
= terminal_urlify_man("shutdown", "8", &link
);
20 printf("%s [OPTIONS...] [TIME] [WALL...]\n"
21 "\n%sShut down the system.%s\n"
23 " --help Show this help\n"
24 " -H --halt Halt the machine\n"
25 " -P --poweroff Power-off the machine\n"
26 " -r --reboot Reboot the machine\n"
27 " -h Equivalent to --poweroff, overridden by --halt\n"
28 " -k Don't halt/power-off/reboot, just send warnings\n"
29 " --no-wall Don't send wall message before halt/power-off/reboot\n"
30 " -c Cancel a pending shutdown\n"
31 "\nSee the %s for details.\n"
32 , program_invocation_short_name
33 , ansi_highlight(), ansi_normal()
40 int shutdown_parse_argv(int argc
, char *argv
[]) {
46 static const struct option options
[] = {
47 { "help", no_argument
, NULL
, ARG_HELP
},
48 { "halt", no_argument
, NULL
, 'H' },
49 { "poweroff", no_argument
, NULL
, 'P' },
50 { "reboot", no_argument
, NULL
, 'r' },
51 { "kexec", no_argument
, NULL
, 'K' }, /* not documented extension */
52 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
62 while ((c
= getopt_long(argc
, argv
, "HPrhkKat:fFc", options
, NULL
)) >= 0)
66 return shutdown_help();
69 arg_action
= ACTION_HALT
;
73 arg_action
= ACTION_POWEROFF
;
78 arg_action
= ACTION_KEXEC
;
80 arg_action
= ACTION_REBOOT
;
84 arg_action
= ACTION_KEXEC
;
88 if (arg_action
!= ACTION_HALT
)
89 arg_action
= ACTION_POWEROFF
;
101 case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */
104 /* Compatibility nops */
108 arg_action
= ACTION_CANCEL_SHUTDOWN
;
115 assert_not_reached("Unhandled option");
118 if (argc
> optind
&& arg_action
!= ACTION_CANCEL_SHUTDOWN
) {
119 r
= parse_shutdown_time_spec(argv
[optind
], &arg_when
);
121 log_error("Failed to parse time specification: %s", argv
[optind
]);
125 arg_when
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
;
127 if (argc
> optind
&& arg_action
== ACTION_CANCEL_SHUTDOWN
)
128 /* No time argument for shutdown cancel */
129 wall
= argv
+ optind
;
130 else if (argc
> optind
+ 1)
131 /* We skip the time argument */
132 wall
= argv
+ optind
+ 1;
135 arg_wall
= strv_copy(wall
);