From bf85c2395e3ed5628b58f86a478e99132288130d Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Fri, 11 Aug 2023 14:42:02 +0100 Subject: [PATCH 1/3] core: copy os-release with COPY_TRUNCATE Otherwise if the os-release file shrinks between updates, there will be a merge of the two. Also remove redundant ENOENT check. Follow-up for 3f37a82545d461ab --- src/core/main.c | 4 ++-- test/units/testsuite-82.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index c929b88db6..1fe93a2f55 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1406,10 +1406,10 @@ static int setup_os_release(RuntimeScope scope) { } r = mkdir_parents_label(os_release_dst, 0755); - if (r < 0 && r != -EEXIST) + if (r < 0) return log_debug_errno(r, "Failed to create parent directory of %s, ignoring: %m", os_release_dst); - r = copy_file(os_release_src, os_release_dst, /* open_flags= */ 0, 0644, COPY_MAC_CREATE); + r = copy_file(os_release_src, os_release_dst, /* open_flags= */ 0, 0644, COPY_MAC_CREATE|COPY_TRUNCATE); if (r < 0) return log_debug_errno(r, "Failed to create %s, ignoring: %m", os_release_dst); diff --git a/test/units/testsuite-82.sh b/test/units/testsuite-82.sh index 9e37d0c06f..7adee341c1 100755 --- a/test/units/testsuite-82.sh +++ b/test/units/testsuite-82.sh @@ -92,9 +92,9 @@ elif [ -f /run/testsuite82.touch ]; then echo miep >/tmp/nextroot-lower/lower # Copy os-release away, so that we can manipulate it and check that it is updated in the propagate - # directory across soft reboots. + # directory across soft reboots. Try to cover corner cases by truncating it. mkdir -p /tmp/nextroot-lower/usr/lib - cp /etc/os-release /tmp/nextroot-lower/usr/lib/os-release + grep ID /etc/os-release >/tmp/nextroot-lower/usr/lib/os-release echo MARKER=1 >>/tmp/nextroot-lower/usr/lib/os-release cmp /etc/os-release /run/systemd/propagate/os-release (! grep -q MARKER=1 /etc/os-release) From 5ee8e9887a7e87174b5e25d61e13a58c08237c37 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Wed, 9 Aug 2023 13:59:22 +0100 Subject: [PATCH 2/3] core: allow to pass EINVAL to unit_add_two_dependencies() Useful to conditionally add two deps at a time --- src/core/unit.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/core/unit.c b/src/core/unit.c index 6792bda9d0..0451a23509 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -3291,17 +3291,22 @@ int unit_add_dependency( } int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference, UnitDependencyMask mask) { - int r, s; + int r = 0, s = 0; assert(u); + assert(d >= 0 || e >= 0); - r = unit_add_dependency(u, d, other, add_reference, mask); - if (r < 0) - return r; + if (d >= 0) { + r = unit_add_dependency(u, d, other, add_reference, mask); + if (r < 0) + return r; + } - s = unit_add_dependency(u, e, other, add_reference, mask); - if (s < 0) - return s; + if (e >= 0) { + s = unit_add_dependency(u, e, other, add_reference, mask); + if (s < 0) + return s; + } return r > 0 || s > 0; } From 05be3e8be8e3ef7ec4326290192c36577d6c6024 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Wed, 9 Aug 2023 14:02:34 +0100 Subject: [PATCH 3/3] core: split manager's process killing on shutdown to separate helper --- src/core/main.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 1fe93a2f55..e6932784d1 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1818,6 +1818,19 @@ static void filter_args( } } +static void finish_remaining_processes(ManagerObjective objective) { + assert(objective >= 0 && objective < _MANAGER_OBJECTIVE_MAX); + + /* Kill all remaining processes from the initrd, but don't wait for them, so that we can handle the + * SIGCHLD for them after deserializing. */ + if (IN_SET(objective, MANAGER_SWITCH_ROOT, MANAGER_SOFT_REBOOT)) + broadcast_signal(SIGTERM, /* wait_for_exit= */ false, /* send_sighup= */ true, arg_default_timeout_stop_usec); + + /* On soft reboot really make sure nothing is left */ + if (objective == MANAGER_SOFT_REBOOT) + broadcast_signal(SIGKILL, /* wait_for_exit= */ false, /* send_sighup= */ false, arg_default_timeout_stop_usec); +} + static int do_reexecute( ManagerObjective objective, int argc, @@ -1868,13 +1881,7 @@ static int do_reexecute( if (saved_rlimit_memlock->rlim_cur != RLIM_INFINITY) (void) setrlimit(RLIMIT_MEMLOCK, saved_rlimit_memlock); - /* Kill all remaining processes from the initrd, but don't wait for them, so that we can handle the - * SIGCHLD for them after deserializing. */ - if (IN_SET(objective, MANAGER_SWITCH_ROOT, MANAGER_SOFT_REBOOT)) - broadcast_signal(SIGTERM, /* wait_for_exit= */ false, /* send_sighup= */ true, arg_default_timeout_stop_usec); - /* On soft reboot really make sure nothing is left */ - if (objective == MANAGER_SOFT_REBOOT) - broadcast_signal(SIGKILL, /* wait_for_exit= */ false, /* send_sighup= */ false, arg_default_timeout_stop_usec); + finish_remaining_processes(objective); if (!switch_root_dir && objective == MANAGER_SOFT_REBOOT) { /* If no switch root dir is specified, then check if /run/nextroot/ qualifies and use that */