From d35fe8c0afaa55441608cb7bbfa4af908e1ea8e3 Mon Sep 17 00:00:00 2001
From: Franck Bui <fbui@suse.com>
Date: Thu, 5 May 2022 08:49:56 +0200
Subject: [PATCH] core: introduce MANAGER_IS_SWITCHING_ROOT() helper function

Will be used by the following commit.
---
 src/core/main.c    | 3 +++
 src/core/manager.c | 6 ++++++
 src/core/manager.h | 6 ++++++
 3 files changed, 15 insertions(+)

diff --git a/src/core/main.c b/src/core/main.c
index 1213ad6..df4fb9d 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1981,6 +1981,8 @@ static int invoke_main_loop(
                         return 0;
 
                 case MANAGER_SWITCH_ROOT:
+                        manager_set_switching_root(m, true);
+
                         if (!m->switch_root_init) {
                                 r = prepare_reexecute(m, &arg_serialization, ret_fds, true);
                                 if (r < 0) {
@@ -2899,6 +2901,7 @@ int main(int argc, char *argv[]) {
         set_manager_defaults(m);
         set_manager_settings(m);
         manager_set_first_boot(m, first_boot);
+        manager_set_switching_root(m, arg_switched_root);
 
         /* Remember whether we should queue the default job */
         queue_default_job = !arg_serialization || arg_switched_root;
diff --git a/src/core/manager.c b/src/core/manager.c
index abc63a7..d3b7fc5 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -756,6 +756,10 @@ static int manager_setup_sigchld_event_source(Manager *m) {
         return 0;
 }
 
+void manager_set_switching_root(Manager *m, bool switching_root) {
+        m->switching_root = MANAGER_IS_SYSTEM(m) && switching_root;
+}
+
 int manager_new(UnitFileScope scope, ManagerTestRunFlags test_run_flags, Manager **_m) {
         _cleanup_(manager_freep) Manager *m = NULL;
         const char *e;
@@ -1799,6 +1803,8 @@ int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
 
         manager_ready(m);
 
+        manager_set_switching_root(m, false);
+
         return 0;
 }
 
diff --git a/src/core/manager.h b/src/core/manager.h
index 14a80b3..453706c 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -400,6 +400,9 @@ struct Manager {
         char *switch_root;
         char *switch_root_init;
 
+        /* This is true before and after switching root. */
+        bool switching_root;
+
         /* This maps all possible path prefixes to the units needing
          * them. It's a hashmap with a path string as key and a Set as
          * value where Unit objects are contained. */
@@ -461,6 +464,8 @@ static inline usec_t manager_default_timeout_abort_usec(Manager *m) {
 /* The objective is set to OK as soon as we enter the main loop, and set otherwise as soon as we are done with it */
 #define MANAGER_IS_RUNNING(m) ((m)->objective == MANAGER_OK)
 
+#define MANAGER_IS_SWITCHING_ROOT(m) ((m)->switching_root)
+
 #define MANAGER_IS_TEST_RUN(m) ((m)->test_run_flags != 0)
 
 int manager_new(UnitFileScope scope, ManagerTestRunFlags test_run_flags, Manager **m);
@@ -525,6 +530,7 @@ void manager_set_show_status(Manager *m, ShowStatus mode, const char *reason);
 void manager_override_show_status(Manager *m, ShowStatus mode, const char *reason);
 
 void manager_set_first_boot(Manager *m, bool b);
+void manager_set_switching_root(Manager *m, bool switching_root);
 
 void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) _printf_(4,5);
 
-- 
2.33.0

