chiark / gitweb /
add REMOTE_ADDR and REMOTE_PORT for Accept=yes
[elogind.git] / src / core / service.c
index 72e8f71e5f9b6be9ec818548fe982f46baa9a353..db1f1e78efcfd8b74cec30dcb12f43dc6ed48b86 100644 (file)
@@ -21,7 +21,6 @@
 
 #include <errno.h>
 #include <signal.h>
-#include <dirent.h>
 #include <unistd.h>
 
 #include "async.h"
@@ -209,7 +208,7 @@ static void service_start_watchdog(Service *s) {
         if (s->watchdog_event_source) {
                 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
                 if (r < 0) {
-                        log_unit_warning(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r));
+                        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to reset watchdog timer: %m", UNIT(s)->id);
                         return;
                 }
 
@@ -222,7 +221,7 @@ static void service_start_watchdog(Service *s) {
                                 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
                                 service_dispatch_watchdog, s);
                 if (r < 0) {
-                        log_unit_warning(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
+                        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to add watchdog timer: %m", UNIT(s)->id);
                         return;
                 }
 
@@ -232,7 +231,7 @@ static void service_start_watchdog(Service *s) {
         }
 
         if (r < 0)
-                log_unit_warning(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
+                log_unit_warning_errno(UNIT(s)->id, r, "%s failed to install watchdog timer: %m", UNIT(s)->id);
 }
 
 static void service_reset_watchdog(Service *s) {
@@ -242,6 +241,42 @@ static void service_reset_watchdog(Service *s) {
         service_start_watchdog(s);
 }
 
+static void service_fd_store_unlink(ServiceFDStore *fs) {
+
+        if (!fs)
+                return;
+
+        if (fs->service) {
+                assert(fs->service->n_fd_store > 0);
+                LIST_REMOVE(fd_store, fs->service->fd_store, fs);
+                fs->service->n_fd_store--;
+        }
+
+        if (fs->event_source) {
+                sd_event_source_set_enabled(fs->event_source, SD_EVENT_OFF);
+                sd_event_source_unref(fs->event_source);
+        }
+
+        safe_close(fs->fd);
+        free(fs);
+}
+
+static void service_release_resources(Unit *u) {
+        Service *s = SERVICE(u);
+
+        assert(s);
+
+        if (!s->fd_store)
+                return;
+
+        log_debug("Releasing all resources for %s", u->id);
+
+        while (s->fd_store)
+                service_fd_store_unlink(s->fd_store);
+
+        assert(s->n_fd_store == 0);
+}
+
 static void service_done(Unit *u) {
         Service *s = SERVICE(u);
 
@@ -286,6 +321,90 @@ static void service_done(Unit *u) {
         service_stop_watchdog(s);
 
         s->timer_event_source = sd_event_source_unref(s->timer_event_source);
+
+        service_release_resources(u);
+}
+
+static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
+        ServiceFDStore *fs = userdata;
+
+        assert(e);
+        assert(fs);
+
+        /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
+        service_fd_store_unlink(fs);
+        return 0;
+}
+
+static int service_add_fd_store(Service *s, int fd) {
+        ServiceFDStore *fs;
+        int r;
+
+        assert(s);
+        assert(fd >= 0);
+
+        if (s->n_fd_store >= s->n_fd_store_max)
+                return 0;
+
+        LIST_FOREACH(fd_store, fs, s->fd_store) {
+                r = same_fd(fs->fd, fd);
+                if (r < 0)
+                        return r;
+                if (r > 0) {
+                        /* Already included */
+                        safe_close(fd);
+                        return 1;
+                }
+        }
+
+        fs = new0(ServiceFDStore, 1);
+        if (!fs)
+                return -ENOMEM;
+
+        fs->fd = fd;
+        fs->service = s;
+
+        r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs);
+        if (r < 0) {
+                free(fs);
+                return r;
+        }
+
+        LIST_PREPEND(fd_store, s->fd_store, fs);
+        s->n_fd_store++;
+
+        return 1;
+}
+
+static int service_add_fd_store_set(Service *s, FDSet *fds) {
+        int r;
+
+        assert(s);
+
+        if (fdset_size(fds) <= 0)
+                return 0;
+
+        while (s->n_fd_store < s->n_fd_store_max) {
+                _cleanup_close_ int fd = -1;
+
+                fd = fdset_steal_first(fds);
+                if (fd < 0)
+                        break;
+
+                r = service_add_fd_store(s, fd);
+                if (r < 0)
+                        return log_unit_error_errno(UNIT(s)->id, r, "%s: Couldn't add fd to fd store: %m", UNIT(s)->id);
+
+                if (r > 0) {
+                        log_unit_debug(UNIT(s)->id, "%s: added fd to fd store.", UNIT(s)->id);
+                        fd = -1;
+                }
+        }
+
+        if (fdset_size(fds) > 0)
+                log_unit_warning(UNIT(s)->id, "%s: tried to store more fds than FDStoreMax=%u allows, closing remaining.", UNIT(s)->id, s->n_fd_store_max);
+
+        return 0;
 }
 
 static int service_arm_timer(Service *s, usec_t usec) {
@@ -375,8 +494,7 @@ static int service_add_default_dependencies(Service *s) {
                 return r;
 
         /* Second, activate normal shutdown */
-        r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
-        return r;
+        return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
 }
 
 static void service_fix_output(Service *s) {
@@ -397,6 +515,67 @@ static void service_fix_output(Service *s) {
                 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
 }
 
+static int service_add_extras(Service *s) {
+        int r;
+
+        assert(s);
+
+        if (s->type == _SERVICE_TYPE_INVALID) {
+                /* Figure out a type automatically */
+                if (s->bus_name)
+                        s->type = SERVICE_DBUS;
+                else if (s->exec_command[SERVICE_EXEC_START])
+                        s->type = SERVICE_SIMPLE;
+                else
+                        s->type = SERVICE_ONESHOT;
+        }
+
+        /* Oneshot services have disabled start timeout by default */
+        if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
+                s->timeout_start_usec = 0;
+
+        service_fix_output(s);
+
+        r = unit_patch_contexts(UNIT(s));
+        if (r < 0)
+                return r;
+
+        r = unit_add_exec_dependencies(UNIT(s), &s->exec_context);
+        if (r < 0)
+                return r;
+
+        r = unit_add_default_slice(UNIT(s), &s->cgroup_context);
+        if (r < 0)
+                return r;
+
+        if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
+                s->notify_access = NOTIFY_MAIN;
+
+        if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
+                s->notify_access = NOTIFY_MAIN;
+
+        if (s->bus_name) {
+                const char *n;
+
+                r = unit_watch_bus_name(UNIT(s), s->bus_name);
+                if (r < 0)
+                        return r;
+
+                n = strjoina(s->bus_name, ".busname");
+                r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, n, NULL, true);
+                if (r < 0)
+                        return r;
+        }
+
+        if (UNIT(s)->default_dependencies) {
+                r = service_add_default_dependencies(s);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 static int service_load(Unit *u) {
         Service *s = SERVICE(u);
         int r;
@@ -421,52 +600,11 @@ static int service_load(Unit *u) {
                 if (r < 0)
                         return r;
 
-                if (s->type == _SERVICE_TYPE_INVALID) {
-                        /* Figure out a type automatically */
-                        if (s->bus_name)
-                                s->type = SERVICE_DBUS;
-                        else if (s->exec_command[SERVICE_EXEC_START])
-                                s->type = SERVICE_SIMPLE;
-                        else
-                                s->type = SERVICE_ONESHOT;
-                }
-
-                /* Oneshot services have disabled start timeout by default */
-                if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
-                        s->timeout_start_usec = 0;
-
-                service_fix_output(s);
-
-                r = unit_patch_contexts(u);
-                if (r < 0)
-                        return r;
-
-                r = unit_add_exec_dependencies(u, &s->exec_context);
+                /* This is a new unit? Then let's add in some
+                 * extras */
+                r = service_add_extras(s);
                 if (r < 0)
                         return r;
-
-                r = unit_add_default_slice(u, &s->cgroup_context);
-                if (r < 0)
-                        return r;
-
-                if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
-                        s->notify_access = NOTIFY_MAIN;
-
-                if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
-                        s->notify_access = NOTIFY_MAIN;
-
-                if (s->bus_name) {
-                        r = unit_watch_bus_name(u, s->bus_name);
-                        if (r < 0)
-                                return r;
-                }
-
-                if (u->default_dependencies) {
-                        r = service_add_default_dependencies(s);
-                        if (r < 0)
-
-                                return r;
-                }
         }
 
         return service_verify(s);
@@ -480,7 +618,7 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
         assert(s);
 
         prefix = strempty(prefix);
-        prefix2 = strappenda(prefix, "\t");
+        prefix2 = strjoina(prefix, "\t");
 
         fprintf(f,
                 "%sService State: %s\n"
@@ -546,16 +684,17 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
                 exec_command_dump_list(s->exec_command[c], f, prefix2);
         }
 
-#ifdef HAVE_SYSV_COMPAT
-        if (s->sysv_start_priority >= 0)
-                fprintf(f,
-                        "%sSysVStartPriority: %i\n",
-                        prefix, s->sysv_start_priority);
-#endif
-
         if (s->status_text)
                 fprintf(f, "%sStatus Text: %s\n",
                         prefix, s->status_text);
+
+        if (s->n_fd_store_max > 0) {
+                fprintf(f,
+                        "%sFile Descriptor Store Max: %u\n"
+                        "%sFile Descriptor Store Current: %u\n",
+                        prefix, s->n_fd_store_max,
+                        prefix, s->n_fd_store);
+        }
 }
 
 static int service_load_pid_file(Service *s, bool may_warn) {
@@ -578,7 +717,7 @@ static int service_load_pid_file(Service *s, bool may_warn) {
         r = parse_pid(k, &pid);
         if (r < 0) {
                 if (may_warn)
-                        log_unit_info(UNIT(s)->id, "Failed to read PID from file %s: %s", s->pid_file, strerror(-r));
+                        log_unit_info_errno(UNIT(s)->id, r, "Failed to read PID from file %s: %m", s->pid_file);
                 return r;
         }
 
@@ -710,7 +849,7 @@ static void service_set_state(Service *s, ServiceState state) {
         /* For the inactive states unit_notify() will trim the cgroup,
          * but for exit we have to do that ourselves... */
         if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
-                unit_destroy_cgroup(UNIT(s));
+                unit_destroy_cgroup_if_empty(UNIT(s));
 
         /* For remain_after_exit services, let's see if we can "release" the
          * hold on the console, since unit_notify() only does that in case of
@@ -739,7 +878,7 @@ static void service_set_state(Service *s, ServiceState state) {
         s->reload_result = SERVICE_SUCCESS;
 }
 
-static int service_coldplug(Unit *u) {
+static int service_coldplug(Unit *u, Hashmap *deferred_work) {
         Service *s = SERVICE(u);
         int r;
 
@@ -813,10 +952,10 @@ static int service_coldplug(Unit *u) {
 }
 
 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
+        _cleanup_free_ int *rfds = NULL;
+        unsigned rn_fds = 0;
         Iterator i;
         int r;
-        int *rfds = NULL;
-        unsigned rn_fds = 0;
         Unit *u;
 
         assert(s);
@@ -838,10 +977,12 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
 
                 r = socket_collect_fds(sock, &cfds, &cn_fds);
                 if (r < 0)
-                        goto fail;
+                        return r;
 
-                if (!cfds)
+                if (cn_fds <= 0) {
+                        free(cfds);
                         continue;
+                }
 
                 if (!rfds) {
                         rfds = cfds;
@@ -849,32 +990,39 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
                 } else {
                         int *t;
 
-                        t = new(int, rn_fds+cn_fds);
+                        t = realloc(rfds, (rn_fds + cn_fds) * sizeof(int));
                         if (!t) {
                                 free(cfds);
-                                r = -ENOMEM;
-                                goto fail;
+                                return -ENOMEM;
                         }
 
-                        memcpy(t, rfds, rn_fds * sizeof(int));
-                        memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
-                        free(rfds);
+                        memcpy(t + rn_fds, cfds, cn_fds * sizeof(int));
+                        rfds = t;
+                        rn_fds += cn_fds;
+
                         free(cfds);
 
-                        rfds = t;
-                        rn_fds = rn_fds+cn_fds;
                 }
         }
 
+        if (s->n_fd_store > 0) {
+                ServiceFDStore *fs;
+                int *t;
+
+                t = realloc(rfds, (rn_fds + s->n_fd_store) * sizeof(int));
+                if (!t)
+                        return -ENOMEM;
+
+                rfds = t;
+                LIST_FOREACH(fd_store, fs, s->fd_store)
+                        rfds[rn_fds++] = fs->fd;
+        }
+
         *fds = rfds;
         *n_fds = rn_fds;
 
+        rfds = NULL;
         return 0;
-
-fail:
-        free(rfds);
-
-        return r;
 }
 
 static int service_spawn(
@@ -909,7 +1057,11 @@ static int service_spawn(
         assert(c);
         assert(_pid);
 
-        unit_realize_cgroup(UNIT(s));
+        (void) unit_realize_cgroup(UNIT(s));
+        if (s->reset_cpu_usage) {
+                (void) unit_reset_cpu_usage(UNIT(s));
+                s->reset_cpu_usage = false;
+        }
 
         r = unit_setup_exec_runtime(UNIT(s));
         if (r < 0)
@@ -943,7 +1095,7 @@ static int service_spawn(
         if (r < 0)
                 goto fail;
 
-        our_env = new0(char*, 4);
+        our_env = new0(char*, 6);
         if (!our_env) {
                 r = -ENOMEM;
                 goto fail;
@@ -967,6 +1119,46 @@ static int service_spawn(
                         goto fail;
                 }
 
+        if (UNIT_DEREF(s->accept_socket)) {
+                union sockaddr_union sa;
+                socklen_t salen = sizeof(sa);
+
+                r = getpeername(s->socket_fd, &sa.sa, &salen);
+                if (r < 0) {
+                        r = -errno;
+                        goto fail;
+                }
+
+                if (IN_SET(sa.sa.sa_family, AF_INET, AF_INET6)) {
+                        _cleanup_free_ char *addr = NULL;
+                        char *t;
+                        int port;
+
+                        r = sockaddr_pretty(&sa.sa, salen, true, false, &addr);
+                        if (r < 0)
+                                goto fail;
+
+                        t = strappend("REMOTE_ADDR=", addr);
+                        if (!t) {
+                                r = -ENOMEM;
+                                goto fail;
+                        }
+                        our_env[n_env++] = t;
+
+                        port = sockaddr_port(&sa.sa);
+                        if (port < 0) {
+                                r = port;
+                                goto fail;
+                        }
+
+                        if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) {
+                                r = -ENOMEM;
+                                goto fail;
+                        }
+                        our_env[n_env++] = t;
+                }
+        }
+
         final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
         if (!final_env) {
                 r = -ENOMEM;
@@ -974,7 +1166,7 @@ static int service_spawn(
         }
 
         if (is_control && UNIT(s)->cgroup_path) {
-                path = strappenda(UNIT(s)->cgroup_path, "/control");
+                path = strjoina(UNIT(s)->cgroup_path, "/control");
                 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
         } else
                 path = UNIT(s)->cgroup_path;
@@ -1130,7 +1322,7 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run install restart timer: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run install restart timer: %m", UNIT(s)->id);
         service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
 }
 
@@ -1167,7 +1359,7 @@ static void service_enter_stop_post(Service *s, ServiceResult f) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run 'stop-post' task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop-post' task: %m", UNIT(s)->id);
         service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
 }
 
@@ -1213,7 +1405,7 @@ static void service_enter_signal(Service *s, ServiceState state, ServiceResult f
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to kill processes: %m", UNIT(s)->id);
 
         if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL ||
             state == SERVICE_STOP_SIGABRT)
@@ -1268,7 +1460,7 @@ static void service_enter_stop(Service *s, ServiceResult f) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop' task: %m", UNIT(s)->id);
         service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
 }
 
@@ -1330,7 +1522,7 @@ static void service_enter_start_post(Service *s) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-post' task: %m", UNIT(s)->id);
         service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
 }
 
@@ -1340,7 +1532,7 @@ static void service_kill_control_processes(Service *s) {
         if (!UNIT(s)->cgroup_path)
                 return;
 
-        p = strappenda(UNIT(s)->cgroup_path, "/control");
+        p = strjoina(UNIT(s)->cgroup_path, "/control");
         cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
 }
 
@@ -1423,7 +1615,7 @@ static void service_enter_start(Service *s) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start' task: %m", UNIT(s)->id);
         service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
 }
 
@@ -1461,7 +1653,7 @@ static void service_enter_start_pre(Service *s) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-pre' task: %m", UNIT(s)->id);
         service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
 }
 
@@ -1541,7 +1733,7 @@ static void service_enter_reload(Service *s) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run 'reload' task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'reload' task: %m", UNIT(s)->id);
         s->reload_result = SERVICE_FAILURE_RESOURCES;
         service_enter_running(s, SERVICE_SUCCESS);
 }
@@ -1574,7 +1766,7 @@ static void service_run_next_control(Service *s) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run next control task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next control task: %m", UNIT(s)->id);
 
         if (s->state == SERVICE_START_PRE)
                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
@@ -1618,7 +1810,7 @@ static void service_run_next_main(Service *s) {
         return;
 
 fail:
-        log_unit_warning(UNIT(s)->id, "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
+        log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next main task: %m", UNIT(s)->id);
         service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
 }
 
@@ -1680,6 +1872,7 @@ static int service_start(Unit *u) {
         s->main_pid_known = false;
         s->main_pid_alien = false;
         s->forbid_restart = false;
+        s->reset_cpu_usage = true;
 
         free(s->status_text);
         s->status_text = NULL;
@@ -1688,7 +1881,7 @@ static int service_start(Unit *u) {
         s->notify_state = NOTIFY_UNKNOWN;
 
         service_enter_start_pre(s);
-        return 0;
+        return 1;
 }
 
 static int service_stop(Unit *u) {
@@ -1729,7 +1922,7 @@ static int service_stop(Unit *u) {
                s->state == SERVICE_EXITED);
 
         service_enter_stop(s, SERVICE_SUCCESS);
-        return 0;
+        return 1;
 }
 
 static int service_reload(Unit *u) {
@@ -1753,6 +1946,7 @@ _pure_ static bool service_can_reload(Unit *u) {
 
 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
         Service *s = SERVICE(u);
+        ServiceFDStore *fs;
 
         assert(u);
         assert(f);
@@ -1784,7 +1978,8 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
         if (s->socket_fd >= 0) {
                 int copy;
 
-                if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
+                copy = fdset_put_dup(fds, s->socket_fd);
+                if (copy < 0)
                         return copy;
 
                 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
@@ -1793,12 +1988,23 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
         if (s->bus_endpoint_fd >= 0) {
                 int copy;
 
-                if ((copy = fdset_put_dup(fds, s->bus_endpoint_fd)) < 0)
+                copy = fdset_put_dup(fds, s->bus_endpoint_fd);
+                if (copy < 0)
                         return copy;
 
                 unit_serialize_item_format(u, f, "endpoint-fd", "%i", copy);
         }
 
+        LIST_FOREACH(fd_store, fs, s->fd_store) {
+                int copy;
+
+                copy = fdset_put_dup(fds, fs->fd);
+                if (copy < 0)
+                        return copy;
+
+                unit_serialize_item_format(u, f, "fd-store-fd", "%i", copy);
+        }
+
         if (s->main_exec_status.pid > 0) {
                 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
                                            s->main_exec_status.pid);
@@ -1825,6 +2031,7 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
 
 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
         Service *s = SERVICE(u);
+        int r;
 
         assert(u);
         assert(key);
@@ -1920,6 +2127,19 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value,
                         safe_close(s->bus_endpoint_fd);
                         s->bus_endpoint_fd = fdset_remove(fds, fd);
                 }
+        } else if (streq(key, "fd-store-fd")) {
+                int fd;
+
+                if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
+                        log_unit_debug(u->id, "Failed to parse fd-store-fd value %s", value);
+                else {
+                        r = service_add_fd_store(s, fd);
+                        if (r < 0)
+                                log_unit_error_errno(u->id, r, "Failed to add fd to store: %m");
+                        else if (r > 0)
+                                fdset_remove(fds, fd);
+                }
+
         } else if (streq(key, "main-exec-status-pid")) {
                 pid_t pid;
 
@@ -2031,7 +2251,7 @@ static int service_watch_pid_file(Service *s) {
 
         return 0;
 fail:
-        log_unit_error(UNIT(s)->id, "Failed to set a watch for %s's PID file %s: %s", UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
+        log_unit_error_errno(UNIT(s)->id, r, "Failed to set a watch for %s's PID file %s: %m", UNIT(s)->id, s->pid_file_pathspec->path);
         service_unwatch_pid_file(s);
         return r;
 }
@@ -2205,16 +2425,16 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                                 f = SERVICE_SUCCESS;
                 }
 
-                log_unit_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
-                           u->id,
-                           "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
-                                  u->id, sigchld_code_to_string(code), status,
-                                  strna(code == CLD_EXITED
-                                        ? exit_status_to_string(status, EXIT_STATUS_FULL)
-                                        : signal_to_string(status)),
-                           "EXIT_CODE=%s", sigchld_code_to_string(code),
-                           "EXIT_STATUS=%i", status,
-                           NULL);
+                log_unit_struct(u->id,
+                                f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
+                                LOG_MESSAGE("%s: main process exited, code=%s, status=%i/%s",
+                                            u->id, sigchld_code_to_string(code), status,
+                                            strna(code == CLD_EXITED
+                                                  ? exit_status_to_string(status, EXIT_STATUS_FULL)
+                                                  : signal_to_string(status))),
+                                "EXIT_CODE=%s", sigchld_code_to_string(code),
+                                "EXIT_STATUS=%i", status,
+                                NULL);
 
                 if (f != SERVICE_SUCCESS)
                         s->result = f;
@@ -2550,7 +2770,7 @@ static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void
         return 0;
 }
 
-static void service_notify_message(Unit *u, pid_t pid, char **tags) {
+static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) {
         Service *s = SERVICE(u);
         _cleanup_free_ char *cc = NULL;
         bool notify_dbus = false;
@@ -2682,6 +2902,12 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
                 service_reset_watchdog(s);
         }
 
+        /* Add the passed fds to the fd store */
+        if (strv_find(tags, "FDSTORE=1")) {
+                log_unit_debug(u->id, "%s: got FDSTORE=1", u->id);
+                service_add_fd_store_set(s, fds);
+        }
+
         /* Notify clients about changed status or main pid */
         if (notify_dbus)
                 unit_add_to_dbus_queue(u);
@@ -2924,6 +3150,7 @@ const UnitVTable service_vtable = {
         .init = service_init,
         .done = service_done,
         .load = service_load,
+        .release_resources = service_release_resources,
 
         .coldplug = service_coldplug,