shutdown: add time argument, wall message, and cancel

shutdown ignored everything after its options, so a delayed shutdown
went down right away:

    shutdown -r +10 "Upgrading, back in five"

Take a time, now, +MIN, or the next HH:MM, and an optional message.
Finit keeps the countdown, so it survives logging out:

  - warns logged-in users at 60, 30, 15 and 10 minutes left, then
    every minute from five, and on the console
  - writes the message to /etc/nologin from five minutes before,
    leaving one already in place alone
  - shutdown -c cancels, and a new shutdown TIME replaces it

Without a time, or with now and no message, shutdown acts right away
as before.  An immediate reboot, halt, or power-off, also over D-Bus,
cancels a scheduled one.  The warnings are written by a child, reading
utmp and closing a serial tty can block.

Fixes #149

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-17 11:00:49 +01:00
1 parent d51f521bf8
commit 20c422af69
12 files changed
+542 -53

No files matched your search

+107 -9
View File
@@ -28,15 +28,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "finit.h"
#include "util.h"
typedef enum {
CMD_UNKNOWN = -1,
CMD_REBOOT = 0,
CMD_HALT,
CMD_POWEROFF,
CMD_SUSPEND,
CMD_UNKNOWN = -1,
CMD_REBOOT = INIT_CMD_REBOOT,
CMD_HALT = INIT_CMD_HALT,
CMD_POWEROFF = INIT_CMD_POWEROFF,
CMD_SUSPEND = INIT_CMD_SUSPEND,
} cmd_t;
static cmd_t cmd = CMD_UNKNOWN;
@@ -49,6 +51,8 @@ extern int do_reboot (char *arg);
extern int do_halt (char *arg);
extern int do_poweroff(char *arg);
extern int do_suspend (char *arg);
extern int do_shutdown_at(int cmd, int sec, const char *msg);
extern int do_shutdown_cancel(void);
static void transform(char *nm)
@@ -101,18 +105,83 @@ static void do_kill(int signo, char *msg)
do_sleep(5);
}
#define MAX_MIN (365 * 24 * 60) /* Finit takes no more than a year */
/* now, +MIN, or HH:MM, as seconds from now, or -1 */
static int parse_time(const char *arg)
{
struct tm tm;
time_t t, at;
int hh, mm;
char *end;
long min;
char c;
if (!strcmp(arg, "now"))
return 0;
if (arg[0] == '+') {
min = strtol(&arg[1], &end, 10);
if (end == &arg[1] || *end || min < 0 || min > MAX_MIN)
return -1;
return (int)min * 60;
}
if (sscanf(arg, "%d:%d%c", &hh, &mm, &c) != 2 || hh < 0 || hh > 23 || mm < 0 || mm > 59)
return -1;
/* Next time the clock shows HH:MM, today or tomorrow */
t = time(NULL);
localtime_r(&t, &tm);
tm.tm_hour = hh;
tm.tm_min = mm;
tm.tm_sec = 0;
tm.tm_isdst = -1;
at = mktime(&tm);
if (at <= t) {
tm.tm_mday++;
tm.tm_isdst = -1;
at = mktime(&tm);
}
return (int)(at - t);
}
static int schedule(int sec, const char *text)
{
char tm[32];
time_t at;
if (do_shutdown_at(cmd, sec, text))
errx(1, "Failed scheduling %s.", msg);
if (sec > 0) {
at = time(NULL) + sec;
strftime(tm, sizeof(tm), "%a %Y-%m-%d %H:%M", localtime(&at));
printf("Scheduled %s at %s, use 'shutdown -c' to cancel.\n", msg, tm);
}
return 0;
}
static int usage(int rc)
{
fprintf(stderr, "Usage: %s [OPTIONS]\n\n"
int sd = !strcmp(prognm, "shutdown");
fprintf(stderr, "Usage: %s [OPTIONS]%s\n\n"
"Options:\n"
" -?, --help This help text\n"
" -c Cancel a scheduled shutdown\n"
" -f, --force Force unsafe %s now, do not contact the init system.\n"
" --halt Halt system, regardless of how the command is called.\n"
" -h Halt or power off after shutdown.\n"
" -P, --poweroff Power-off system, regardless of how the command is called.\n"
" -r, --reboot Reboot system, regardless of how the command is called.\n"
" -t, --timeout=SEC Force reboot/shutdown after a given timeout\n"
"\n", prognm, msg);
"\n", prognm, sd ? " [TIME [MESSAGE]]" : "", msg);
if (sd)
fprintf(stderr, "TIME is now, +MIN, or HH:MM. Logged-in users are sent MESSAGE as a\n"
"warning, and from five minutes before, new logins are refused.\n\n");
return rc;
}
@@ -128,17 +197,21 @@ int reboot_main(int argc, char *argv[])
{"timeout", 1, NULL, 't'},
{NULL, 0, NULL, 0}
};
int c, force = 0;
int c, force = 0, cancel = 0;
int tmp;
/* Initial command taken from program name */
transform(prognm);
while ((c = getopt_long(argc, argv, "h?fHPprt:", long_options, NULL)) != EOF) {
while ((c = getopt_long(argc, argv, "h?cfHPprt:", long_options, NULL)) != EOF) {
switch(c) {
case '?':
return usage(0);
case 'c':
cancel = 1;
break;
case 'f':
force = 1;
break;
@@ -176,6 +249,31 @@ int reboot_main(int argc, char *argv[])
if (getenv("SULOGIN"))
force = 1;
if (cancel)
return do_shutdown_cancel();
/* shutdown [opts] [TIME [MESSAGE]], see issue #149 */
if (!strcmp(prognm, "shutdown") && optind < argc) {
char text[256] = "";
int sec, i;
sec = parse_time(argv[optind]);
if (sec < 0)
errx(1, "Invalid time '%s', use now, +MIN, or HH:MM.", argv[optind]);
for (i = optind + 1; i < argc; i++) {
if (text[0])
strlcat(text, " ", sizeof(text));
strlcat(text, argv[i], sizeof(text));
}
if (sec > 0 || text[0]) {
if (force)
errx(1, "Cannot schedule a forced %s.", msg);
return schedule(sec, text);
}
}
if (force) {
sync();