From: Lennart Poettering Date: Wed, 18 Oct 2023 11:37:45 +0000 (+0200) Subject: manager: move idle_pipe allocation to manager.c and make it atomic X-Git-Tag: v255-rc1~211 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=06044356de405456c69d684c5b0981c7c30daf41;p=thirdparty%2Fsystemd.git manager: move idle_pipe allocation to manager.c and make it atomic Let's make sure it either fails or suceeds, but never fails half-way leaving a half-initialized array around. --- diff --git a/src/core/manager.c b/src/core/manager.c index 9307a13a79f..55321d1638a 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -4952,6 +4952,35 @@ ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) { return s; } +int manager_allocate_idle_pipe(Manager *m) { + int r; + + assert(m); + + if (m->idle_pipe[0] >= 0) { + assert(m->idle_pipe[1] >= 0); + assert(m->idle_pipe[2] >= 0); + assert(m->idle_pipe[3] >= 0); + return 0; + } + + assert(m->idle_pipe[1] < 0); + assert(m->idle_pipe[2] < 0); + assert(m->idle_pipe[3] < 0); + + r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC)); + if (r < 0) + return r; + + r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC)); + if (r < 0) { + safe_close_pair(m->idle_pipe + 0); + return r; + } + + return 1; +} + void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) { assert(defaults); assert(scope >= 0); diff --git a/src/core/manager.h b/src/core/manager.h index 6321a353a5a..3b3920078e6 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -635,6 +635,8 @@ int manager_override_watchdog_pretimeout_governor(Manager *m, const char *govern LogTarget manager_get_executor_log_target(Manager *m); +int manager_allocate_idle_pipe(Manager *m); + const char* oom_policy_to_string(OOMPolicy i) _const_; OOMPolicy oom_policy_from_string(const char *s) _pure_; diff --git a/src/core/transaction.c b/src/core/transaction.c index 85f673f447d..a81c40fb062 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -781,19 +781,11 @@ int transaction_activate( assert(hashmap_isempty(tr->jobs)); - if (!hashmap_isempty(m->jobs)) { - /* Are there any jobs now? Then make sure we have the - * idle pipe around. We don't really care too much - * whether this works or not, as the idle pipe is a - * feature for cosmetics, not actually useful for - * anything beyond that. */ - - if (m->idle_pipe[0] < 0 && m->idle_pipe[1] < 0 && - m->idle_pipe[2] < 0 && m->idle_pipe[3] < 0) { - (void) pipe2(m->idle_pipe, O_NONBLOCK|O_CLOEXEC); - (void) pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC); - } - } + /* Are there any jobs now? Then make sure we have the idle pipe around. We don't really care too much + * whether this works or not, as the idle pipe is a feature for cosmetics, not actually useful for + * anything beyond that. */ + if (!hashmap_isempty(m->jobs)) + (void) manager_allocate_idle_pipe(m); return 0; }