]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
execute: Do not pass destroy as a boolean argument to unref()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 27 Mar 2023 12:32:58 +0000 (14:32 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 27 Mar 2023 12:32:58 +0000 (14:32 +0200)
Let's mimick what we do for DynamicUser and have two separate functions
for unreffing and destroying a ExecSharedRuntime object.

src/core/execute.c
src/core/execute.h
src/core/mount.c
src/core/service.c
src/core/socket.c
src/core/swap.c

index b5cf140f72dfe9e6d107a3c4ada48f861666c167..3011f15dfb7fcb4f10ec47f591e752a68b6d5a18 100644 (file)
@@ -6956,18 +6956,37 @@ static void *remove_tmpdir_thread(void *p) {
         return NULL;
 }
 
-static ExecSharedRuntime* exec_shared_runtime_free(ExecSharedRuntime *rt, bool destroy) {
-        int r;
-
+static ExecSharedRuntime* exec_shared_runtime_free(ExecSharedRuntime *rt) {
         if (!rt)
                 return NULL;
 
         if (rt->manager)
                 (void) hashmap_remove(rt->manager->exec_shared_runtime_by_id, rt->id);
 
-        /* When destroy is true, then rm_rf tmp_dir and var_tmp_dir. */
+        rt->id = mfree(rt->id);
+        rt->tmp_dir = mfree(rt->tmp_dir);
+        rt->var_tmp_dir = mfree(rt->var_tmp_dir);
+        safe_close_pair(rt->netns_storage_socket);
+        safe_close_pair(rt->ipcns_storage_socket);
+        return mfree(rt);
+}
+
+DEFINE_TRIVIAL_UNREF_FUNC(ExecSharedRuntime, exec_shared_runtime, exec_shared_runtime_free);
+DEFINE_TRIVIAL_CLEANUP_FUNC(ExecSharedRuntime*, exec_shared_runtime_free);
+
+ExecSharedRuntime* exec_shared_runtime_destroy(ExecSharedRuntime *rt) {
+        int r;
+
+        if (!rt)
+                return NULL;
+
+        assert(rt->n_ref > 0);
+        rt->n_ref--;
+
+        if (rt->n_ref > 0)
+                return NULL;
 
-        if (destroy && rt->tmp_dir && !streq(rt->tmp_dir, RUN_SYSTEMD_EMPTY)) {
+        if (rt->tmp_dir && !streq(rt->tmp_dir, RUN_SYSTEMD_EMPTY)) {
                 log_debug("Spawning thread to nuke %s", rt->tmp_dir);
 
                 r = asynchronous_job(remove_tmpdir_thread, rt->tmp_dir);
@@ -6977,7 +6996,7 @@ static ExecSharedRuntime* exec_shared_runtime_free(ExecSharedRuntime *rt, bool d
                         rt->tmp_dir = NULL;
         }
 
-        if (destroy && rt->var_tmp_dir && !streq(rt->var_tmp_dir, RUN_SYSTEMD_EMPTY)) {
+        if (rt->var_tmp_dir && !streq(rt->var_tmp_dir, RUN_SYSTEMD_EMPTY)) {
                 log_debug("Spawning thread to nuke %s", rt->var_tmp_dir);
 
                 r = asynchronous_job(remove_tmpdir_thread, rt->var_tmp_dir);
@@ -6987,16 +7006,7 @@ static ExecSharedRuntime* exec_shared_runtime_free(ExecSharedRuntime *rt, bool d
                         rt->var_tmp_dir = NULL;
         }
 
-        rt->id = mfree(rt->id);
-        rt->tmp_dir = mfree(rt->tmp_dir);
-        rt->var_tmp_dir = mfree(rt->var_tmp_dir);
-        safe_close_pair(rt->netns_storage_socket);
-        safe_close_pair(rt->ipcns_storage_socket);
-        return mfree(rt);
-}
-
-static void exec_shared_runtime_freep(ExecSharedRuntime **rt) {
-        (void) exec_shared_runtime_free(*rt, false);
+        return exec_shared_runtime_free(rt);
 }
 
 static int exec_shared_runtime_allocate(ExecSharedRuntime **ret, const char *id) {
@@ -7152,19 +7162,6 @@ ref:
         return 1;
 }
 
-ExecSharedRuntime *exec_shared_runtime_unref(ExecSharedRuntime *rt, bool destroy) {
-        if (!rt)
-                return NULL;
-
-        assert(rt->n_ref > 0);
-
-        rt->n_ref--;
-        if (rt->n_ref > 0)
-                return NULL;
-
-        return exec_shared_runtime_free(rt, destroy);
-}
-
 int exec_shared_runtime_serialize(const Manager *m, FILE *f, FDSet *fds) {
         ExecSharedRuntime *rt;
 
@@ -7438,7 +7435,7 @@ void exec_shared_runtime_vacuum(Manager *m) {
                 if (rt->n_ref > 0)
                         continue;
 
-                (void) exec_shared_runtime_free(rt, false);
+                (void) exec_shared_runtime_free(rt);
         }
 }
 
index 95d5de8fb80973ad97cbe5832b39b1b30ba735cc..10cd2c60465d586ae798b283763d43d4a626cf4c 100644 (file)
@@ -484,7 +484,8 @@ void exec_status_dump(const ExecStatus *s, FILE *f, const char *prefix);
 void exec_status_reset(ExecStatus *s);
 
 int exec_shared_runtime_acquire(Manager *m, const ExecContext *c, const char *name, bool create, ExecSharedRuntime **ret);
-ExecSharedRuntime *exec_shared_runtime_unref(ExecSharedRuntime *r, bool destroy);
+ExecSharedRuntime *exec_shared_runtime_destroy(ExecSharedRuntime *r);
+ExecSharedRuntime *exec_shared_runtime_unref(ExecSharedRuntime *r);
 
 int exec_shared_runtime_serialize(const Manager *m, FILE *f, FDSet *fds);
 int exec_shared_runtime_deserialize_compat(Unit *u, const char *key, const char *value, FDSet *fds);
index 366986f31211b6bbdf4b3e975a795cb8e6d276a0..e755a55964efb8040e69ddeff560a2ecf754cce8 100644 (file)
@@ -254,7 +254,7 @@ static void mount_done(Unit *u) {
         mount_parameters_done(&m->parameters_proc_self_mountinfo);
         mount_parameters_done(&m->parameters_fragment);
 
-        m->exec_runtime = exec_shared_runtime_unref(m->exec_runtime, false);
+        m->exec_runtime = exec_shared_runtime_unref(m->exec_runtime);
         exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
         m->control_command = NULL;
 
@@ -948,7 +948,7 @@ static void mount_enter_dead(Mount *m, MountResult f) {
 
         mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
 
-        m->exec_runtime = exec_shared_runtime_unref(m->exec_runtime, true);
+        m->exec_runtime = exec_shared_runtime_destroy(m->exec_runtime);
 
         unit_destroy_runtime_data(UNIT(m), &m->exec_context);
 
index bf04237130366e80f2f352964052fc107aa1f377..4894d94511269900c0230edb841e16de1f2352b9 100644 (file)
@@ -383,7 +383,7 @@ static void service_done(Unit *u) {
         s->pid_file = mfree(s->pid_file);
         s->status_text = mfree(s->status_text);
 
-        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime, false);
+        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime);
         exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
         s->control_command = NULL;
         s->main_command = NULL;
@@ -1925,7 +1925,7 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
         s->notify_access_override = _NOTIFY_ACCESS_INVALID;
 
         /* We want fresh tmpdirs in case service is started again immediately */
-        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime, true);
+        s->exec_runtime = exec_shared_runtime_destroy(s->exec_runtime);
 
         /* Also, remove the runtime directory */
         unit_destroy_runtime_data(UNIT(s), &s->exec_context);
index fddf5d680f4f829f88fad1630c0e4a8053360772..0e33c41defae5dd771069baf3827972de484c00d 100644 (file)
@@ -150,7 +150,7 @@ static void socket_done(Unit *u) {
 
         s->peers_by_address = set_free(s->peers_by_address);
 
-        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime, false);
+        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime);
         exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
         s->control_command = NULL;
 
@@ -2049,7 +2049,7 @@ static void socket_enter_dead(Socket *s, SocketResult f) {
 
         socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD);
 
-        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime, true);
+        s->exec_runtime = exec_shared_runtime_destroy(s->exec_runtime);
 
         unit_destroy_runtime_data(UNIT(s), &s->exec_context);
 
index 629ed52cf523b054e9c32b1b9aab005cff1814f4..7b33060ab8a70baf8c2665127982fd0a54845417 100644 (file)
@@ -170,7 +170,7 @@ static void swap_done(Unit *u) {
         s->parameters_fragment.what = mfree(s->parameters_fragment.what);
         s->parameters_fragment.options = mfree(s->parameters_fragment.options);
 
-        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime, false);
+        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime);
         exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
         s->control_command = NULL;
 
@@ -719,7 +719,7 @@ static void swap_enter_dead(Swap *s, SwapResult f) {
         unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop);
         swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
 
-        s->exec_runtime = exec_shared_runtime_unref(s->exec_runtime, true);
+        s->exec_runtime = exec_shared_runtime_destroy(s->exec_runtime);
 
         unit_destroy_runtime_data(UNIT(s), &s->exec_context);