]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/service: add missing serialization for extra fds 34720/head
authorMike Yuan <me@yhndnzj.com>
Tue, 8 Oct 2024 13:48:49 +0000 (15:48 +0200)
committerMike Yuan <me@yhndnzj.com>
Fri, 11 Oct 2024 16:22:24 +0000 (18:22 +0200)
src/core/service.c

index eddec763e1654907b651f4e6a99f064b89f77e40..8f4cd636700578308237d186251977d4892cb2ab 100644 (file)
@@ -3144,6 +3144,21 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
                 (void) serialize_item_format(f, "fd-store-fd", "%i \"%s\" %s", copy, c, one_zero(fs->do_poll));
         }
 
+        FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) {
+                _cleanup_free_ char *c = NULL;
+                int copy;
+
+                copy = fdset_put_dup(fds, i->fd);
+                if (copy < 0)
+                        return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m");
+
+                c = cescape(i->fdname);
+                if (!c)
+                        return log_oom();
+
+                (void) serialize_item_format(f, "extra-fd", "%i \"%s\"", copy, c);
+        }
+
         if (s->main_exec_status.pid > 0) {
                 (void) serialize_item_format(f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid);
                 (void) serialize_dual_timestamp(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
@@ -3394,6 +3409,29 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value,
                 }
 
                 TAKE_FD(fd);
+        } else if (streq(key, "extra-fd")) {
+                _cleanup_free_ char *fdv = NULL, *fdn = NULL;
+                _cleanup_close_ int fd = -EBADF;
+
+                r = extract_many_words(&value, " ", EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE, &fdv, &fdn);
+                if (r != 2) {
+                        log_unit_debug(u, "Failed to deserialize extra-fd, ignoring: %s", value);
+                        return 0;
+                }
+
+                fd = deserialize_fd(fds, fdv);
+                if (fd < 0)
+                        return 0;
+
+                if (!GREEDY_REALLOC(s->extra_fds, s->n_extra_fds + 1)) {
+                        log_oom_debug();
+                        return 0;
+                }
+
+                s->extra_fds[s->n_extra_fds++] = (ServiceExtraFD) {
+                        .fd = TAKE_FD(fd),
+                        .fdname = TAKE_PTR(fdn),
+                };
         } else if (streq(key, "main-exec-status-pid")) {
                 pid_t pid;