]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
daemon-util: introduce several helper functions for fd store
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 15 Aug 2022 11:05:21 +0000 (20:05 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 16 Aug 2022 12:49:00 +0000 (21:49 +0900)
src/shared/daemon-util.c [new file with mode: 0644]
src/shared/daemon-util.h
src/shared/meson.build

diff --git a/src/shared/daemon-util.c b/src/shared/daemon-util.c
new file mode 100644 (file)
index 0000000..32180a1
--- /dev/null
@@ -0,0 +1,76 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "daemon-util.h"
+#include "fd-util.h"
+#include "log.h"
+#include "string-util.h"
+
+static int notify_remove_fd_warn(const char *name) {
+        int r;
+
+        assert(name);
+
+        r = sd_notifyf(/* unset_environment = */ false,
+                       "FDSTOREREMOVE=1\n"
+                       "FDNAME=%s", name);
+        if (r < 0)
+                return log_warning_errno(r,
+                                         "Failed to remove file descriptor \"%s\" from the store, ignoring: %m",
+                                         name);
+
+        return 0;
+}
+
+int notify_remove_fd_warnf(const char *format, ...) {
+        _cleanup_free_ char *p = NULL;
+        va_list ap;
+        int r;
+
+        assert(format);
+
+        va_start(ap, format);
+        r = vasprintf(&p, format, ap);
+        va_end(ap);
+        if (r < 0)
+                return log_oom();
+
+        return notify_remove_fd_warn(p);
+}
+
+int close_and_notify_warn(int fd, const char *name) {
+        if (name)
+                (void) notify_remove_fd_warn(name);
+
+        return safe_close(fd);
+}
+
+static int notify_push_fd(int fd, const char *name) {
+        _cleanup_free_ char *state = NULL;
+
+        assert(fd >= 0);
+        assert(name);
+
+        state = strjoin("FDSTORE=1\n"
+                        "FDNAME=", name);
+        if (!state)
+                return -ENOMEM;
+
+        return sd_pid_notify_with_fds(0, /* unset_environment = */ false, state, &fd, 1);
+}
+
+int notify_push_fdf(int fd, const char *format, ...) {
+        _cleanup_free_ char *name = NULL;
+        va_list ap;
+        int r;
+
+        assert(fd >= 0);
+        assert(format);
+
+        va_start(ap, format);
+        r = vasprintf(&name, format, ap);
+        va_end(ap);
+        if (r < 0)
+                return -ENOMEM;
+
+        return notify_push_fd(fd, name);
+}
index 585e4894a01365459792e54e2d41a45280e5f41f..711885bba462a89df59e61693b6f9973c069009d 100644 (file)
@@ -5,6 +5,8 @@
 
 #include "sd-daemon.h"
 
+#include "macro.h"
+
 #define NOTIFY_READY "READY=1\n" "STATUS=Processing requests..."
 #define NOTIFY_STOPPING "STOPPING=1\n" "STATUS=Shutting down..."
 
@@ -20,3 +22,7 @@ static inline void notify_on_cleanup(const char **p) {
         if (*p)
                 (void) sd_notify(false, *p);
 }
+
+int notify_remove_fd_warnf(const char *format, ...) _printf_(1, 2);
+int close_and_notify_warn(int fd, const char *name);
+int notify_push_fdf(int fd, const char *format, ...) _printf_(2, 3);
index 598a64593b533fe122442bfd373d47085ef91cc5..426a87b70cffe9844164612cdef9c4a900e53f18 100644 (file)
@@ -85,6 +85,7 @@ shared_sources = files(
         'creds-util.h',
         'cryptsetup-util.c',
         'cryptsetup-util.h',
+        'daemon-util.c',
         'daemon-util.h',
         'data-fd-util.c',
         'data-fd-util.h',