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

+1
View File
@@ -54,6 +54,7 @@ finit_SOURCES = api.c cgroup.c cgroup.h \
legacy.c legacy.h \
devmon.c devmon.h \
exec.c finit.c finit.h \
shutdown.c shutdown.h \
stty.c \
helpers.c helpers.h \
initramfs.c \
+15 -37
View File
@@ -48,6 +48,7 @@
#include "schedule.h"
#include "service.h"
#include "sm.h"
#include "shutdown.h"
#include "sig.h"
#include "util.h"
@@ -241,43 +242,9 @@ static int do_reboot(int cmd, int timeout, char *buf, size_t len)
switch (cmd) {
case INIT_CMD_REBOOT:
halt = SHUT_REBOOT;
break;
case INIT_CMD_HALT:
halt = SHUT_HALT;
break;
case INIT_CMD_POWEROFF:
halt = SHUT_OFF;
break;
case INIT_CMD_SUSPEND:
break;
default:
return 255;
}
shutdown_bypass(timeout);
switch (cmd) {
case INIT_CMD_REBOOT:
dbg("reboot");
halt = SHUT_REBOOT;
sm_runlevel(6);
break;
case INIT_CMD_HALT:
dbg("halt");
halt = SHUT_HALT;
sm_runlevel(0);
break;
case INIT_CMD_POWEROFF:
dbg("poweroff");
halt = SHUT_OFF;
sm_runlevel(0);
shutdown_now(cmd, timeout);
break;
case INIT_CMD_SUSPEND:
@@ -293,8 +260,7 @@ static int do_reboot(int cmd, int timeout, char *buf, size_t len)
break;
default:
/* Handled previously */
break;
return 255;
}
return rc;
@@ -384,6 +350,7 @@ static void api_cb(uev_t *w, void *arg, int events)
case INIT_CMD_HALT:
case INIT_CMD_POWEROFF:
case INIT_CMD_SUSPEND:
case INIT_CMD_SHUTDOWN:
if (IS_RESERVED_RUNLEVEL(runlevel)) {
strterm(rq.data, sizeof(rq.data));
warnx("Unsupported command (cmd: %d, data: %s) in runlevel S and 6/0.",
@@ -484,6 +451,17 @@ static void api_cb(uev_t *w, void *arg, int events)
result = do_reboot(rq.cmd, rq.sleeptime, rq.data, sizeof(rq.data));
break;
case INIT_CMD_SHUTDOWN:
strterm(rq.data, sizeof(rq.data));
result = shutdown_schedule(rq.runlevel, rq.sleeptime, rq.data);
break;
case INIT_CMD_SHUTDOWN_CANCEL:
result = shutdown_cancel();
if (result)
strlcpy(rq.data, "No scheduled shutdown to cancel.", sizeof(rq.data));
break;
case INIT_CMD_SWITCH_ROOT:
result = do_switch_root_api(sd, &rq);
if (result)
+6 -7
View File
@@ -50,6 +50,7 @@
#include "schedule.h"
#include "service.h"
#include "sig.h"
#include "shutdown.h"
#include "sm.h"
#include "svc.h"
#include "util.h"
@@ -438,7 +439,7 @@ static int manager_set_runlevel(link_call_t *call, void *userdata)
return 0;
}
static int dbus_shutdown(link_call_t *call, shutop_t target, int level)
static int dbus_shutdown(link_call_t *call, int cmd)
{
uint32_t timeout;
@@ -450,16 +451,14 @@ static int dbus_shutdown(link_call_t *call, shutop_t target, int level)
return link_call_reply_error(call,
"org.finit.Error.WrongRunlevel",
"Already in shutdown");
halt = target;
shutdown_bypass((int)timeout);
sm_runlevel(level);
shutdown_now(cmd, (int)timeout);
(void)link_call_reply(call);
return 0;
}
static int manager_reboot (link_call_t *c, void *u) { (void)u; return dbus_shutdown(c, SHUT_REBOOT, 6); }
static int manager_poweroff(link_call_t *c, void *u) { (void)u; return dbus_shutdown(c, SHUT_OFF, 0); }
static int manager_halt (link_call_t *c, void *u) { (void)u; return dbus_shutdown(c, SHUT_HALT, 0); }
static int manager_reboot (link_call_t *c, void *u) { (void)u; return dbus_shutdown(c, INIT_CMD_REBOOT); }
static int manager_poweroff(link_call_t *c, void *u) { (void)u; return dbus_shutdown(c, INIT_CMD_POWEROFF); }
static int manager_halt (link_call_t *c, void *u) { (void)u; return dbus_shutdown(c, INIT_CMD_HALT); }
static int manager_set_debug(link_call_t *call, void *u)
{
+2
View File
@@ -100,6 +100,8 @@
#define INIT_CMD_POWEROFF 22
#define INIT_CMD_SUSPEND 23
#define INIT_CMD_SWITCH_ROOT 24 /* Switch to new root filesystem */
#define INIT_CMD_SHUTDOWN 25 /* runlevel: REBOOT/HALT/POWEROFF, sleeptime: sec, data: msg */
#define INIT_CMD_SHUTDOWN_CANCEL 26
#define INIT_CMD_WDOG_HELLO 128 /* Watchdog register and hello */
#define INIT_CMD_SVC_ITER 129
#define INIT_CMD_SVC_QUERY 130
+30
View File
@@ -1090,6 +1090,36 @@ static int do_reboot_dbus(const char *method)
}
#endif
/* Scheduled shutdown, see issue #149, @cmd is INIT_CMD_REBOOT, _HALT or _POWEROFF */
int do_shutdown_at(int cmd, int sec, const char *msg)
{
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = INIT_CMD_SHUTDOWN,
.runlevel = cmd,
.sleeptime = sec,
};
strlcpy(rq.data, msg ? msg : "", sizeof(rq.data));
return client_send(&rq, sizeof(rq));
}
int do_shutdown_cancel(void)
{
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = INIT_CMD_SHUTDOWN_CANCEL,
};
if (client_send(&rq, sizeof(rq))) {
if (rq.cmd == INIT_CMD_NACK)
warnx("%s", rq.data);
return 1;
}
return 0;
}
int do_reboot (char *arg)
{
#ifdef HAVE_DBUS
+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();
+264
View File
@@ -0,0 +1,264 @@
/* Scheduled shutdown with wall messages and nologin, see issue #149
*
* Copyright (c) 2026 Joachim Wiberg <troglobit@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utmp.h>
#include "conf.h"
#include "finit.h"
#include "log.h"
#include "private.h"
#include "schedule.h"
#include "shutdown.h"
#include "sig.h"
#include "sm.h"
#include "util.h"
#include "utmp-api.h"
/* Minutes left at each warning, 0 is the deadline */
static const int warn_at[] = { 60, 30, 15, 10, 5, 4, 3, 2, 1, 0 };
#define NOLOGIN_MIN 5
#define MAX_WAIT 3600 /* sec, keeps the msec timer delay an int */
#define MAX_DELAY (365 * 24 * 3600)
static struct {
int cmd; /* INIT_CMD_REBOOT, _HALT, _POWEROFF, 0: none */
long when; /* Deadline, jiffies() */
int next; /* Minutes left at next warning */
int nologin; /* We created FINIT_NOLOGIN_PATH */
char msg[256];
struct wq work;
} sched;
static const char *action(int cmd)
{
switch (cmd) {
case INIT_CMD_REBOOT:
return "reboot";
case INIT_CMD_HALT:
return "halt";
default:
return "power-off";
}
}
/*
* Write @text to the terminal of every logged-in user. Done in a
* child, reading utmp may wait for its lock and closing a serial
* tty for its output to drain.
*/
static void wall(const char *text)
{
char host[64] = "", tm[32], hdr[160];
struct utmp *ut;
time_t t;
if (!has_utmp() || fork())
return;
t = time(NULL);
gethostname(host, sizeof(host) - 1);
strftime(tm, sizeof(tm), "%a %b %e %H:%M:%S %Y", localtime(&t));
snprintf(hdr, sizeof(hdr), "\r\nBroadcast message from root@%s (%s):\r\n\r\n", host, tm);
setutent();
while ((ut = getutent())) {
char tty[sizeof(ut->ut_line) + 6];
int fd;
if (ut->ut_type != USER_PROCESS || !ut->ut_line[0])
continue;
snprintf(tty, sizeof(tty), "/dev/%.*s", (int)sizeof(ut->ut_line), ut->ut_line);
if (strstr(tty, ".."))
continue;
/* utmp is not ours alone to write, make sure it is a tty */
fd = open(tty, O_WRONLY | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0)
continue;
if (isatty(fd) && write(fd, hdr, strlen(hdr)) > 0)
(void)!write(fd, text, strlen(text));
close(fd);
}
endutent();
_exit(0);
}
static void announce(int min)
{
char line[80], text[sizeof(sched.msg) + sizeof(line) + 8];
if (min > 0)
snprintf(line, sizeof(line), "The system is going DOWN for %s in %d minute%s!",
action(sched.cmd), min, min > 1 ? "s" : "");
else
snprintf(line, sizeof(line), "The system is going DOWN for %s NOW!", action(sched.cmd));
logit(LOG_CONSOLE | LOG_NOTICE, "%s", line);
snprintf(text, sizeof(text), "%s%s%s\r\n", sched.msg, sched.msg[0] ? "\r\n" : "", line);
wall(text);
}
/* Never touch an admin's nologin, only create and remove our own */
static void nologin(void)
{
FILE *fp;
if (fexist(FINIT_NOLOGIN_PATH))
return;
fp = fopen(FINIT_NOLOGIN_PATH, "w");
if (!fp) {
dbg("Failed creating %s: %s", FINIT_NOLOGIN_PATH, strerror(errno));
return;
}
fprintf(fp, "%s%sThe system is going down for %s.\n", sched.msg,
sched.msg[0] ? "\n" : "", action(sched.cmd));
fclose(fp);
sched.nologin = 1;
}
static void tick(void *arg)
{
long left, wait;
size_t i;
int min;
if (!sched.cmd || IS_RESERVED_RUNLEVEL(runlevel))
return;
left = sched.when - jiffies();
if (left <= 0) {
announce(0);
shutdown_now(sched.cmd, 0);
return;
}
min = (left + 59) / 60;
if (min <= NOLOGIN_MIN)
nologin();
if (min == sched.next)
announce(min);
for (i = 0; warn_at[i] >= min; i++)
;
sched.next = warn_at[i];
wait = left - sched.next * 60;
if (wait > MAX_WAIT)
wait = MAX_WAIT;
sched.work.delay = wait * 1000;
schedule_work(&sched.work);
}
/*
* Reboot, halt, or power off now, replacing any scheduled shutdown.
* A @timeout in seconds arms the emergency shutdown bypass, 0: none.
*/
int shutdown_now(int cmd, int timeout)
{
switch (cmd) {
case INIT_CMD_REBOOT:
halt = SHUT_REBOOT;
break;
case INIT_CMD_HALT:
halt = SHUT_HALT;
break;
case INIT_CMD_POWEROFF:
halt = SHUT_OFF;
break;
default:
return 1;
}
if (sched.cmd) {
sched.cmd = 0;
uev_timer_stop(&sched.work.watcher);
}
shutdown_bypass(timeout);
dbg("%s", action(cmd));
sm_runlevel(cmd == INIT_CMD_REBOOT ? 6 : 0);
return 0;
}
/* Replaces any already scheduled shutdown */
int shutdown_schedule(int cmd, int sec, const char *msg)
{
if (cmd != INIT_CMD_REBOOT && cmd != INIT_CMD_HALT && cmd != INIT_CMD_POWEROFF)
return 1;
if (sec < 0 || sec > MAX_DELAY)
return 1;
/* Our nologin belongs to the shutdown being replaced */
if (sched.nologin) {
erase(FINIT_NOLOGIN_PATH);
sched.nologin = 0;
}
sched.cmd = cmd;
sched.when = jiffies() + sec;
sched.next = (sec + 59) / 60; /* First tick warns */
strlcpy(sched.msg, msg ? msg : "", sizeof(sched.msg));
/* First tick right away, a zero delay would disarm the timer */
sched.work.cb = tick;
sched.work.delay = 1;
return schedule_work(&sched.work);
}
int shutdown_cancel(void)
{
if (!sched.cmd)
return 1;
sched.cmd = 0;
uev_timer_stop(&sched.work.watcher);
if (sched.nologin) {
erase(FINIT_NOLOGIN_PATH);
sched.nologin = 0;
}
logit(LOG_CONSOLE | LOG_NOTICE, "The system shutdown has been cancelled.");
wall("The system shutdown has been cancelled.\r\n");
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+31
View File
@@ -0,0 +1,31 @@
/* Scheduled shutdown, see issue #149
*
* Copyright (c) 2026 Joachim Wiberg <troglobit@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef FINIT_SHUTDOWN_H_
#define FINIT_SHUTDOWN_H_
int shutdown_now (int cmd, int timeout);
int shutdown_schedule(int cmd, int sec, const char *msg);
int shutdown_cancel (void);
#endif /* FINIT_SHUTDOWN_H_ */