[systemd-devel] Is there a way to know inside of systemd if it's in a reboot state?

Tia, Javier javier.tia at hpe.com
Thu Apr 22 20:57:23 UTC 2021


I had to make a little change to systemctl to use a file as a flag when 
the system is rebooting. Likely, this a use case that most people would 
not be interested in. And it might exist a better way to do it. Anyway, 
this is the patch:

diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h
index 1f0bdd95b3..9643763d13 100644
--- a/src/basic/fs-util.h
+++ b/src/basic/fs-util.h
@@ -16,6 +16,8 @@

  #define MODE_INVALID ((mode_t) -1)

+#define REBOOTING_FILE "/run/systemd/is-rebooting"
+
  /* The following macros add 1 when converting things, since 0 is a 
valid mode, while the pointer
   * NULL is special */
  #define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1))
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 5ac40be4a7..652b1af12f 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -3587,6 +3587,10 @@ static int start_special(int argc, char *argv[], 
void *userdata) {
          bool termination_action; /* an action that terminates the manager,
                                    * can be performed also by signal. */

+        r = touch(REBOOTING_FILE);
+        if (r < 0)
+                log_debug_errno(r, "Failed to touch %s, ignoring: %m", 
  REBOOTING_FILE);
+
          assert(argv);

          a = verb_to_action(argv[0]);
@@ -9089,6 +9093,10 @@ static int logind_schedule_shutdown(void) {
  static int halt_main(void) {
          int r;

+        r = touch(REBOOTING_FILE);
+        if (r < 0)
+                log_debug_errno(r, "Failed to touch %s, ignoring: %m", 
REBOOTING_FILE);
+
          r = logind_check_inhibitors(arg_action);
          if (r < 0)
                  return r;

▷ Javier's 🖊

On 4/19/21 2:19 PM, Tia, Javier wrote:
> Hi,
> 
> Is there a way to know inside of systemd if it's in a reboot state?
> 
> Something like the Linux kernel does [1], keeping the system state in a
> variable.
> 
> [1]
> https://elixir.bootlin.com/linux/v4.19.98/source/include/linux/kernel.h#L581
> 
> ▷ Javier's 🖊
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel


More information about the systemd-devel mailing list