X Tutup
Skip to content

Commit a9c3cc8

Browse files
lnusselkeszybz
authored andcommitted
systemctl: add shutdown --show option
Shows the scheduled shutdown action and time if there's one.
1 parent b55093c commit a9c3cc8

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

man/shutdown.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@
125125
<literal>now</literal>.</para></listitem>
126126
</varlistentry>
127127

128+
<varlistentry>
129+
<term><option>--show</option></term>
130+
131+
<listitem><para>Show a pending shutdown action and time if
132+
there is any.</para></listitem>
133+
</varlistentry>
134+
128135
</variablelist>
129136
</refsect1>
130137

src/systemctl/systemctl-compat-shutdown.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static int shutdown_help(void) {
2828
" -k Don't halt/power-off/reboot, just send warnings\n"
2929
" --no-wall Don't send wall message before halt/power-off/reboot\n"
3030
" -c Cancel a pending shutdown\n"
31+
" --show Show pending shutdown\n"
3132
"\nSee the %s for details.\n",
3233
program_invocation_short_name,
3334
ansi_highlight(),
@@ -40,7 +41,8 @@ static int shutdown_help(void) {
4041
int shutdown_parse_argv(int argc, char *argv[]) {
4142
enum {
4243
ARG_HELP = 0x100,
43-
ARG_NO_WALL
44+
ARG_NO_WALL,
45+
ARG_SHOW
4446
};
4547

4648
static const struct option options[] = {
@@ -50,6 +52,7 @@ int shutdown_parse_argv(int argc, char *argv[]) {
5052
{ "reboot", no_argument, NULL, 'r' },
5153
{ "kexec", no_argument, NULL, 'K' }, /* not documented extension */
5254
{ "no-wall", no_argument, NULL, ARG_NO_WALL },
55+
{ "show", no_argument, NULL, ARG_SHOW },
5356
{}
5457
};
5558

@@ -108,6 +111,10 @@ int shutdown_parse_argv(int argc, char *argv[]) {
108111
arg_action = ACTION_CANCEL_SHUTDOWN;
109112
break;
110113

114+
case ARG_SHOW:
115+
arg_action = ACTION_SHOW_SHUTDOWN;
116+
break;
117+
111118
case '?':
112119
return -EINVAL;
113120

src/systemctl/systemctl-logind.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,41 @@ int logind_cancel_shutdown(void) {
372372
#endif
373373
}
374374

375+
int logind_show_shutdown(void) {
376+
#if ENABLE_LOGIND
377+
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
378+
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
379+
sd_bus *bus;
380+
const char *action = NULL;
381+
uint64_t elapse;
382+
int r;
383+
384+
r = acquire_bus(BUS_FULL, &bus);
385+
if (r < 0)
386+
return r;
387+
388+
r = bus_get_property(bus, bus_login_mgr, "ScheduledShutdown", &error, &reply, "(st)");
389+
if (r < 0)
390+
return log_error_errno(r, "Failed to query scheduled shutdown: %s", bus_error_message(&error, r));
391+
392+
r = sd_bus_message_read(reply, "(st)", &action, &elapse);
393+
if (r < 0)
394+
return r;
395+
396+
if (isempty(action))
397+
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "No scheduled shutdown.");
398+
399+
log_info("%s scheduled for %s, use 'shutdown -c' to cancel.",
400+
action,
401+
FORMAT_TIMESTAMP_STYLE(arg_when, arg_timestamp_style));
402+
403+
return 0;
404+
#else
405+
return log_error_errno(SYNTHETIC_ERRNO(ENOSYS),
406+
"Not compiled with logind support, cannot show scheduled shutdowns.");
407+
#endif
408+
}
409+
375410
int help_boot_loader_entry(void) {
376411
#if ENABLE_LOGIND
377412
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;

src/systemctl/systemctl-logind.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ int prepare_boot_loader_entry(void);
1414

1515
int logind_schedule_shutdown(void);
1616
int logind_cancel_shutdown(void);
17+
int logind_show_shutdown(void);
1718

1819
int help_boot_loader_entry(void);

src/systemctl/systemctl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,10 @@ static int run(int argc, char *argv[]) {
11691169
r = logind_cancel_shutdown();
11701170
break;
11711171

1172+
case ACTION_SHOW_SHUTDOWN:
1173+
r = logind_show_shutdown();
1174+
break;
1175+
11721176
case ACTION_RUNLEVEL:
11731177
r = runlevel_main();
11741178
break;

src/systemctl/systemctl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum action {
3232
ACTION_RUNLEVEL,
3333
ACTION_TELINIT,
3434
ACTION_CANCEL_SHUTDOWN,
35+
ACTION_SHOW_SHUTDOWN,
3536
_ACTION_MAX,
3637
_ACTION_INVALID = -EINVAL,
3738
};

0 commit comments

Comments
 (0)
X Tutup