From 99620f457ed0886852ba18c9093b59767299121c Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 4 Apr 2023 12:17:16 +0200
Subject: [PATCH] service: close fdstore asynchronously

The file descriptors we keep in the fdstore might be basically anything,
let's clean it up with our asynchronous closing feature, to not
deadlock on close().

(Let's also do the same for stdin/stdout/stderr fds, since they might
point to network services these days.)

Conflict:fd adaptation, the actual code function does not change.
Reference:https://github.com/systemd/systemd/commit/99620f457ed0886852ba18c9093b59767299121c

---
 TODO               |  2 --
 src/core/service.c | 15 ++++++++-------
 2 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/TODO b/TODO
index 75f885c..27b85ea 100644
--- a/TODO
+++ b/TODO
@@ -531,8 +531,6 @@ Features:
 * maybe rework get_user_creds() to query the user database if $SHELL is used
   for root, but only then.
 
-* be stricter with fds we receive for the fdstore: close them asynchronously
-
 * calenderspec: add support for week numbers and day numbers within a
   year. This would allow us to define "bi-weekly" triggers safely.
 
diff --git a/src/core/service.c b/src/core/service.c
index d52ffe7..1b18a83 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -334,7 +334,7 @@ static void service_fd_store_unlink(ServiceFDStore *fs) {
         sd_event_source_disable_unref(fs->event_source);
 
         free(fs->fdname);
-        safe_close(fs->fd);
+        asynchronous_close(fs->fd);
         free(fs);
 }
 
@@ -360,9 +360,9 @@ static void service_release_stdio_fd(Service *s) {
 
         log_unit_debug(UNIT(s), "Releasing stdin/stdout/stderr file descriptors.");
 
-        s->stdin_fd = safe_close(s->stdin_fd);
-        s->stdout_fd = safe_close(s->stdout_fd);
-        s->stderr_fd = safe_close(s->stderr_fd);
+        s->stdin_fd = asynchronous_close(s->stdin_fd);
+        s->stdout_fd = asynchronous_close(s->stdout_fd);
+        s->stderr_fd = asynchronous_close(s->stderr_fd);
 }
 static void service_done(Unit *u) {
         Service *s = SERVICE(u);
@@ -448,7 +448,7 @@ static int service_add_fd_store(Service *s, int fd, const char *name, bool do_po
                 if (r < 0)
                         return r;
                 if (r > 0) {
-                        safe_close(fd);
+                        asynchronous_close(fd);
                         return 0; /* fd already included */
                 }
         }
@@ -491,7 +491,7 @@ static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name, bo
         assert(s);
 
         while (fdset_size(fds) > 0) {
-                _cleanup_close_ int fd = -1;
+                _cleanup_(asynchronous_closep) int fd = -EBADF;
 
                 fd = fdset_steal_first(fds);
                 if (fd < 0)
@@ -506,7 +506,8 @@ static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name, bo
                         return log_unit_error_errno(UNIT(s), r, "Failed to add fd to store: %m");
                 if (r > 0)
                         log_unit_debug(UNIT(s), "Added fd %u (%s) to fd store.", fd, strna(name));
-                fd = -1;
+
+                TAKE_FD(fd);
         }
 
         return 0;
-- 
2.33.0

