]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: Don't allocate scope if already running in unit with delegated cgroup 32387/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 21 Apr 2024 16:31:53 +0000 (18:31 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 22 Apr 2024 07:42:29 +0000 (09:42 +0200)
If we're already running in a unit with delegation turned on, let's
skip allocation of a scope unit and cgroup subroot. This allows journald
to correctly attribute the logs of all subprocesses spawned by tests such
as test-execute to the test-execute service when the test is running in a service.

src/basic/log.c
src/basic/log.h
src/shared/tests.c

index 6faa6ad4fcef8de0e3f28211a388ded74f594d46..0d740c675ecf8e8d48961659dca71bdc90c11c15 100644 (file)
@@ -256,7 +256,7 @@ fail:
         return r;
 }
 
-static bool stderr_is_journal(void) {
+bool stderr_is_journal(void) {
         _cleanup_free_ char *w = NULL;
         const char *e;
         uint64_t dev, ino;
index 6df164e06f785a9db0890aedd06b18f8e7997812..726f035cfca3ec09fe70573038568ee52a0cf5ac 100644 (file)
@@ -85,6 +85,7 @@ int log_show_tid_from_string(const char *e);
 assert_cc(STRLEN(__FILE__) > STRLEN(RELATIVE_SOURCE_PATH) + 1);
 #define PROJECT_FILE (&__FILE__[STRLEN(RELATIVE_SOURCE_PATH) + 1])
 
+bool stderr_is_journal(void);
 int log_open(void);
 void log_close(void);
 void log_forget_fds(void);
index a0b1472d8981f2e4de34421256c8b7c3aebc59b2..9169513e09cea2c9a3767d87e61d5f4950700b76 100644 (file)
@@ -189,12 +189,16 @@ static int allocate_scope(void) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
-        _cleanup_free_ char *scope = NULL;
+        _cleanup_free_ char *scope = NULL, *cgroup_root = NULL;
         const char *object;
         int r;
 
         /* Let's try to run this test in a scope of its own, with delegation turned on, so that PID 1 doesn't
          * interfere with our cgroup management. */
+        if (cg_pid_get_path(NULL, 0, &cgroup_root) >= 0 && cg_is_delegated(cgroup_root) && stderr_is_journal()) {
+                log_debug("Already running as a unit with delegated cgroup, not allocating a cgroup subroot.");
+                return 0;
+        }
 
         r = sd_bus_default_system(&bus);
         if (r < 0)