busybox has a -d that delays before proceeding. Annoyingly systemd also
has a -d, but that's to disable writing a wtmp shutdown entry.

Also switch to using FLAG().
---
 toys/other/reboot.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
From 56d9b9662df58c8e112d0059f74974e4276ee6e8 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Fri, 10 Sep 2021 16:28:33 -0700
Subject: [PATCH] reboot/halt/poweroff: add -d.

busybox has a -d that delays before proceeding. Annoyingly systemd also
has a -d, but that's to disable writing a wtmp shutdown entry.

Also switch to using FLAG().
---
 toys/other/reboot.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/toys/other/reboot.c b/toys/other/reboot.c
index c883e340..0326377a 100644
--- a/toys/other/reboot.c
+++ b/toys/other/reboot.c
@@ -2,7 +2,7 @@
  *
  * Copyright 2013 Elie De Brauwer <[email protected]>
 
-USE_REBOOT(NEWTOY(reboot, "fn", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
+USE_REBOOT(NEWTOY(reboot, "d:fn", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
 USE_REBOOT(OLDTOY(halt, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
 USE_REBOOT(OLDTOY(poweroff, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
 
@@ -10,10 +10,14 @@ config REBOOT
   bool "reboot"
   default y
   help
-    usage: reboot/halt/poweroff [-fn]
+    usage: reboot/halt/poweroff [-fn] [-d DURATION]
 
-    Restart, halt or powerdown the system.
+    Restart, halt, or power off the system.
 
+    DURATION can be a decimal fraction. An optional suffix can be "m"
+    (minutes), "h" (hours), "d" (days), or "s" (seconds, the default).
+
+    -d	Delay before proceeding
     -f	Don't signal init
     -n	Don't sync before stopping the system
 */
@@ -22,14 +26,24 @@ config REBOOT
 #include "toys.h"
 #include <sys/reboot.h>
 
+GLOBALS(
+  char *d;
+)
+
 void reboot_main(void)
 {
+  struct timespec tv;
   int types[] = {RB_AUTOBOOT, RB_HALT_SYSTEM, RB_POWER_OFF},
       sigs[] = {SIGTERM, SIGUSR1, SIGUSR2}, idx;
 
-  if (!(toys.optflags & FLAG_n)) sync();
+  if (TT.d) {
+    tv.tv_sec = xparsetime(TT.d, 9, &tv.tv_nsec);
+    nanosleep(&tv, NULL);
+  }
+
+  if (!FLAG(n)) sync();
 
   idx = stridx("hp", *toys.which->name)+1;
-  if (toys.optflags & FLAG_f) toys.exitval = reboot(types[idx]);
+  if (FLAG(f)) toys.exitval = reboot(types[idx]);
   else toys.exitval = kill(1, sigs[idx]);
 }
-- 
2.33.0.309.g3052b89438-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to