[systemd-commits] 5 commits - .gitignore Makefile.am TODO src/core src/dbus1-generator src/gpt-auto-generator src/libsystemd-bus src/shared src/test

Lennart Poettering lennart at kemper.freedesktop.org
Mon Dec 2 16:14:36 PST 2013


 .gitignore                                  |    1 
 Makefile.am                                 |   48 +-
 TODO                                        |    2 
 src/core/busname.c                          |  600 ++++++++++++++++++++++++++++
 src/core/busname.h                          |   65 +++
 src/core/dbus-busname.c                     |   40 +
 src/core/dbus-busname.h                     |   28 +
 src/core/load-fragment-gperf.gperf.m4       |    3 
 src/core/load-fragment.c                    |   45 ++
 src/core/load-fragment.h                    |    1 
 src/core/manager.c                          |    2 
 src/core/mount.c                            |   36 -
 src/core/socket.c                           |  399 ++++++++----------
 src/core/unit.c                             |    5 
 src/core/unit.h                             |   12 
 src/dbus1-generator/Makefile                |    1 
 src/dbus1-generator/dbus1-generator.c       |  279 +++++++++++++
 src/gpt-auto-generator/gpt-auto-generator.c |    1 
 src/libsystemd-bus/bus-kernel.c             |   64 ++
 src/libsystemd-bus/bus-kernel.h             |    1 
 src/shared/cgroup-util.c                    |   25 -
 src/shared/macro.h                          |   13 
 src/shared/special.h                        |    1 
 src/shared/unit-name.c                      |   12 
 src/shared/unit-name.h                      |    5 
 src/test/test-util.c                        |   11 
 26 files changed, 1414 insertions(+), 286 deletions(-)

New commits:
commit 674eb68520107d771e3458287025a73387f938c4
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Dec 3 01:13:03 2013 +0100

    bus: add generator that turns old dbus1 activation files into .busname + .service units

diff --git a/.gitignore b/.gitignore
index 19bbceb..db2641f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,6 +40,7 @@
 /systemd-coredumpctl
 /systemd-cryptsetup
 /systemd-cryptsetup-generator
+/systemd-dbus1-generator
 /systemd-delta
 /systemd-detect-virt
 /systemd-efi-boot-generator
diff --git a/Makefile.am b/Makefile.am
index ab0a15c..ce59c04 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1753,6 +1753,18 @@ systemd_gpt_auto_generator_CFLAGS = \
 endif
 
 # ------------------------------------------------------------------------------
+systemgenerator_PROGRAMS +=  \
+	systemd-dbus1-generator
+
+systemd_dbus1_generator_SOURCES = \
+	src/dbus1-generator/dbus1-generator.c
+
+systemd_dbus1_generator_LDADD = \
+	libsystemd-label.la \
+	libsystemd-shared.la \
+	libsystemd-bus-internal.la
+
+# ------------------------------------------------------------------------------
 systemd_rc_local_generator_SOURCES = \
 	src/rc-local-generator/rc-local-generator.c
 
diff --git a/src/dbus1-generator/Makefile b/src/dbus1-generator/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/src/dbus1-generator/Makefile
@@ -0,0 +1 @@
+../Makefile
\ No newline at end of file
diff --git a/src/dbus1-generator/dbus1-generator.c b/src/dbus1-generator/dbus1-generator.c
new file mode 100644
index 0000000..93c6392
--- /dev/null
+++ b/src/dbus1-generator/dbus1-generator.c
@@ -0,0 +1,279 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "util.h"
+#include "conf-parser.h"
+#include "special.h"
+#include "mkdir.h"
+#include "bus-util.h"
+#include "bus-internal.h"
+#include "unit-name.h"
+#include "cgroup-util.h"
+
+static const char *arg_dest = "/tmp";
+
+static int create_dbus_files(
+                const char *path,
+                const char *name,
+                const char *service,
+                const char *exec,
+                const char *user,
+                const char *type) {
+
+        _cleanup_free_ char *b = NULL, *s = NULL, *lnk = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+
+        assert(path);
+        assert(name);
+
+        if (!service) {
+                _cleanup_free_ char *a = NULL;
+
+                s = strjoin("dbus-", name, ".service", NULL);
+                if (!s)
+                        return log_oom();
+
+                a = strjoin(arg_dest, "/", s, NULL);
+                if (!a)
+                        return log_oom();
+
+                f = fopen(a, "wxe");
+                if (!f) {
+                        log_error("Failed to create %s: %m", a);
+                        return -errno;
+                }
+
+                fprintf(f,
+                        "# Automatically generated by systemd-dbus1-generator\n\n"
+                        "[Unit]\n"
+                        "Source=%s\n"
+                        "Description=DBUS1: %s\n\n"
+                        "[Service]\n"
+                        "ExecStart=%s\n"
+                        "Type=dbus\n"
+                        "BusName=%s\n",
+                        path,
+                        name,
+                        exec,
+                        name);
+
+                if (user)
+                        fprintf(f, "User=%s\n", user);
+
+
+                if (type) {
+                        fprintf(f, "Environment=DBUS_STARTER_BUS_TYPE=%s\n", type);
+
+                        if (streq(type, "system"))
+                                fprintf(f, "Environment=DBUS_STARTER_ADDRESS=kernel:/dev/kdbus/0-system\n");
+                        else if (streq(type, "session"))
+                                fprintf(f, "Environment=DBUS_STARTER_ADDRESS=kernel:/dev/kdbus/%lu-user\n", (unsigned long) getuid());
+                }
+
+                fflush(f);
+                if (ferror(f)) {
+                        log_error("Failed to write %s: %m", a);
+                        return -errno;
+                }
+
+                service = s;
+        }
+
+        b = strjoin(arg_dest, "/", name, ".busname", NULL);
+        if (!b)
+                return log_oom();
+
+        f = fopen(b, "wxe");
+        if (!f) {
+                log_error("Failed to create %s: %m", b);
+                return -errno;
+        }
+
+        fprintf(f,
+                "# Automatically generated by systemd-dbus1-generator\n\n"
+                "[Unit]\n"
+                "Source=%s\n"
+                "Description=DBUS1: %s\n\n"
+                "[BusName]\n"
+                "Name=%s\n"
+                "Service=%s\n",
+                path,
+                name,
+                name,
+                service);
+
+        fflush(f);
+        if (ferror(f)) {
+                log_error("Failed to write %s: %m", b);
+                return -errno;
+        }
+
+        lnk = strjoin(arg_dest, "/" SPECIAL_BUSNAMES_TARGET ".wants/", name, ".busname", NULL);
+        if (!lnk)
+                return log_oom();
+
+        mkdir_parents_label(lnk, 0755);
+        if (symlink(b, lnk)) {
+                log_error("Failed to create symlinks %s: %m", lnk);
+                return -errno;
+        }
+
+        return 0;
+}
+
+static int add_dbus(const char *path, const char *fname, const char *type) {
+        _cleanup_free_ char *name = NULL, *exec = NULL, *user = NULL, *service = NULL;
+
+        ConfigTableItem table[] = {
+                { "D-BUS Service", "Name", config_parse_string, 0, &name },
+                { "D-BUS Service", "Exec", config_parse_string, 0, &exec },
+                { "D-BUS Service", "User", config_parse_string, 0, &user },
+                { "D-BUS Service", "SystemdService", config_parse_string, 0, &service },
+        };
+
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *p = NULL;
+        int r;
+
+        assert(path);
+        assert(fname);
+
+        p = strjoin(path, "/", fname, NULL);
+        if (!p)
+                return log_oom();
+
+        f = fopen(p, "re");
+        if (!f) {
+                if (errno == -ENOENT)
+                        return 0;
+
+                log_error("Failed to read %s: %m", p);
+                return -errno;
+        }
+
+        r = config_parse(NULL, p, f, "D-BUS Service\0", config_item_table_lookup, table, true, false, NULL);
+        if (r < 0)
+                return r;
+
+        if (!name) {
+                log_warning("Activation file %s lacks name setting, ignoring.", p);
+                return 0;
+        }
+
+        if (!service_name_is_valid(name)) {
+                log_warning("Bus service name %s is not valid, ignoring.", name);
+                return 0;
+        }
+
+        if (service) {
+                if (!unit_name_is_valid(service, false)) {
+                        log_warning("Unit name %s is not valid, ignoring.", service);
+                        return 0;
+                }
+                if (!endswith(service, ".service")) {
+                        log_warning("Bus names can only activate services, ignoring %s.", p);
+                        return 0;
+                }
+        } else {
+                if (!exec) {
+                        log_warning("Neither service name nor binary path specified, ignoring %s.", p);
+                        return 0;
+                }
+
+                if (exec[0] != '/') {
+                        log_warning("Exec= in %s does not start with an absolute path, ignoring.", p);
+                        return 0;
+                }
+        }
+
+        return create_dbus_files(p, name, service, exec, user, type);
+}
+
+static int parse_dbus_fragments(void) {
+        _cleanup_closedir_ DIR *d = NULL;
+        struct dirent *de;
+        const char *p, *type;
+        int r;
+
+        r = cg_pid_get_owner_uid(0, NULL);
+        if (r >= 0) {
+                p = "/usr/share/dbus-1/services";
+                type = "session";
+        } else if (r == -ENOENT) {
+                p = "/usr/share/dbus-1/system-services";
+                type = "systemd";
+        } else if (r < 0) {
+                log_error("Failed to determine whether we are running as user or system instance: %s", strerror(-r));
+                return r;
+        }
+
+        d = opendir(p);
+        if (!d) {
+                if (errno == -ENOENT)
+                        return 0;
+
+                log_error("Failed to enumerate D-Bus activated services: %m");
+                return -errno;
+        }
+
+        r = 0;
+        FOREACH_DIRENT(de, d, goto fail) {
+                int q;
+
+                if (!endswith(de->d_name, ".service"))
+                        continue;
+
+                q = add_dbus(p, de->d_name, type);
+                if (q < 0)
+                        r = q;
+        }
+
+        return r;
+
+fail:
+        log_error("Failed to read D-Bus services directory: %m");
+        return -errno;
+}
+
+int main(int argc, char *argv[]) {
+        int r;
+
+        if (argc > 1 && argc != 4) {
+                log_error("This program takes three or no arguments.");
+                return EXIT_FAILURE;
+        }
+
+        if (argc > 1)
+                arg_dest = argv[3];
+
+        log_set_target(LOG_TARGET_SAFE);
+        log_parse_environment();
+        log_open();
+
+        umask(0022);
+
+        if (access("/dev/kdbus/control", F_OK) < 0)
+                return 0;
+
+        r = parse_dbus_fragments();
+
+        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c
index 25440e7..3cbafa4 100644
--- a/src/gpt-auto-generator/gpt-auto-generator.c
+++ b/src/gpt-auto-generator/gpt-auto-generator.c
@@ -228,7 +228,6 @@ static int add_home(const char *path, const char *fstype) {
         if (!lnk)
                 return log_oom();
 
-
         mkdir_parents_label(lnk, 0755);
         if (symlink(unit, lnk) < 0) {
                 log_error("Failed to create symlink %s: %m", lnk);
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index 4e0211a..88bc33e 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -1337,36 +1337,41 @@ int cg_pid_get_session(pid_t pid, char **session) {
 
 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
         _cleanup_free_ char *slice = NULL;
-        const char *e;
+        const char *start, *end;
         char *s;
+        uid_t u;
         int r;
 
         assert(path);
-        assert(uid);
 
         r = cg_path_get_slice(path, &slice);
         if (r < 0)
                 return r;
 
-        e = startswith(slice, "user-");
-        if (!e)
+        start = startswith(slice, "user-");
+        if (!start)
                 return -ENOENT;
-        if (!endswith(slice, ".slice"))
+        end = endswith(slice, ".slice");
+        if (!end)
                 return -ENOENT;
 
-        s = strndupa(e, strlen(e) - 6);
+        s = strndupa(start, end - start);
         if (!s)
-                return -ENOMEM;
+                return -ENOENT;
 
-        return parse_uid(s, uid);
+        if (parse_uid(s, &u) < 0)
+                return -EIO;
+
+        if (uid)
+                *uid = u;
+
+        return 0;
 }
 
 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
         _cleanup_free_ char *cgroup = NULL;
         int r;
 
-        assert(uid);
-
         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
         if (r < 0)
                 return r;

commit e821075a23fdfa3ca7738fc30bb2d4c430fe10c0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Dec 2 23:30:19 2013 +0100

    bus: add .busname unit type to implement kdbus-style bus activation

diff --git a/Makefile.am b/Makefile.am
index 0de03fe..ab0a15c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -880,20 +880,22 @@ libsystemd_core_la_SOURCES = \
 	src/core/load-fragment.h \
 	src/core/service.c \
 	src/core/service.h \
-	src/core/automount.c \
-	src/core/automount.h \
-	src/core/mount.c \
-	src/core/mount.h \
-	src/core/swap.c \
-	src/core/swap.h \
-	src/core/device.c \
-	src/core/device.h \
+	src/core/socket.c \
+	src/core/socket.h \
+	src/core/busname.c \
+	src/core/busname.h \
 	src/core/target.c \
 	src/core/target.h \
 	src/core/snapshot.c \
 	src/core/snapshot.h \
-	src/core/socket.c \
-	src/core/socket.h \
+	src/core/device.c \
+	src/core/device.h \
+	src/core/mount.c \
+	src/core/mount.h \
+	src/core/automount.c \
+	src/core/automount.h \
+	src/core/swap.c \
+	src/core/swap.h \
 	src/core/timer.c \
 	src/core/timer.h \
 	src/core/path.c \
@@ -920,20 +922,22 @@ libsystemd_core_la_SOURCES = \
 	src/core/dbus-service.h \
 	src/core/dbus-socket.c \
 	src/core/dbus-socket.h \
-	src/core/dbus-timer.c \
-	src/core/dbus-timer.h \
+	src/core/dbus-busname.c \
+	src/core/dbus-busname.h \
 	src/core/dbus-target.c \
 	src/core/dbus-target.h \
+	src/core/dbus-snapshot.c \
+	src/core/dbus-snapshot.h \
+	src/core/dbus-device.c \
+	src/core/dbus-device.h \
 	src/core/dbus-mount.c \
 	src/core/dbus-mount.h \
 	src/core/dbus-automount.c \
 	src/core/dbus-automount.h \
 	src/core/dbus-swap.c \
 	src/core/dbus-swap.h \
-	src/core/dbus-snapshot.c \
-	src/core/dbus-snapshot.h \
-	src/core/dbus-device.c \
-	src/core/dbus-device.h \
+	src/core/dbus-timer.c \
+	src/core/dbus-timer.h \
 	src/core/dbus-path.c \
 	src/core/dbus-path.h \
 	src/core/dbus-slice.c \
diff --git a/TODO b/TODO
index d5b31c2..20a9a07 100644
--- a/TODO
+++ b/TODO
@@ -116,10 +116,8 @@ Features:
 * libsystemd-bus:
   - default policy (allow uid == 0 and our own uid)
   - access policy as vtable flag
-  - enforce alignment of pointers passed in
   - when kdbus doesn't take our message without memfds, try again with memfds
   - implement translator service
-  - implement busname unit type in systemd
   - move to gvariant
   - implement monitor logic
   - properly map matches with well-known names against messages with unique names
diff --git a/src/core/busname.c b/src/core/busname.c
new file mode 100644
index 0000000..c452656
--- /dev/null
+++ b/src/core/busname.c
@@ -0,0 +1,600 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "special.h"
+#include "bus-kernel.h"
+#include "bus-internal.h"
+#include "bus-util.h"
+#include "service.h"
+#include "dbus-busname.h"
+#include "busname.h"
+
+static const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
+        [BUSNAME_DEAD] = UNIT_INACTIVE,
+        [BUSNAME_LISTENING] = UNIT_ACTIVE,
+        [BUSNAME_RUNNING] = UNIT_ACTIVE,
+        [BUSNAME_FAILED] = UNIT_FAILED
+};
+
+static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
+
+static void busname_init(Unit *u) {
+        BusName *n = BUSNAME(u);
+
+        assert(u);
+        assert(u->load_state == UNIT_STUB);
+
+        n->starter_fd = -1;
+}
+
+static void busname_done(Unit *u) {
+        BusName *n = BUSNAME(u);
+
+        assert(u);
+
+        free(n->name);
+        n->name = NULL;
+
+        unit_ref_unset(&n->service);
+
+        n->event_source = sd_event_source_unref(n->event_source);
+
+        if (n->starter_fd >= 0) {
+                close_nointr_nofail(n->starter_fd);
+                n->starter_fd = -1;
+        }
+}
+
+static int busname_add_default_default_dependencies(BusName *n) {
+        int r;
+
+        assert(n);
+
+        r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
+        if (r < 0)
+                return r;
+
+        if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
+                r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
+                if (r < 0)
+                        return r;
+        }
+
+        return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
+}
+
+static int busname_add_extras(BusName *n) {
+        Unit *u = UNIT(n);
+        int r;
+
+        assert(n);
+
+        if (!n->name) {
+                n->name = unit_name_to_prefix(u->id);
+                if (!n->name)
+                        return -ENOMEM;
+        }
+
+        if (!u->description) {
+                r = unit_set_description(u, n->name);
+                if (r < 0)
+                        return r;
+        }
+
+        if (!UNIT_DEREF(n->service)) {
+                Unit *x;
+
+                r = unit_load_related_unit(u, ".service", &x);
+                if (r < 0)
+                        return r;
+
+                unit_ref_set(&n->service, x);
+        }
+
+        r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
+        if (r < 0)
+                return r;
+
+        if (u->default_dependencies) {
+                r = busname_add_default_default_dependencies(n);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
+
+
+static int busname_verify(BusName *n) {
+        char *e;
+
+        assert(n);
+
+        if (UNIT(n)->load_state != UNIT_LOADED)
+                return 0;
+
+        if (!service_name_is_valid(n->name)) {
+                log_error_unit(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
+                return -EINVAL;
+        }
+
+        e = strappenda(n->name, ".busname");
+        if (!unit_has_name(UNIT(n), e)) {
+                log_error_unit(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
+                return -EINVAL;
+        }
+
+        return 0;
+}
+
+static int busname_load(Unit *u) {
+        BusName *n = BUSNAME(u);
+        int r;
+
+        assert(u);
+        assert(u->load_state == UNIT_STUB);
+
+        r = unit_load_fragment_and_dropin(u);
+        if (r < 0)
+                return r;
+
+        if (u->load_state == UNIT_LOADED) {
+                /* This is a new unit? Then let's add in some extras */
+                r = busname_add_extras(n);
+                if (r < 0)
+                        return r;
+        }
+
+        return busname_verify(n);
+}
+
+static void busname_dump(Unit *u, FILE *f, const char *prefix) {
+        BusName *n = BUSNAME(u);
+
+        assert(n);
+        assert(f);
+
+        fprintf(f,
+                "%sBus Name State: %s\n"
+                "%sResult: %s\n"
+                "%sName: %s\n",
+                prefix, busname_state_to_string(n->state),
+                prefix, busname_result_to_string(n->result),
+                prefix, n->name);
+}
+
+static void busname_unwatch_fd(BusName *n) {
+        int r;
+
+        assert(n);
+
+        if (n->event_source) {
+                r = sd_event_source_set_enabled(n->event_source, SD_EVENT_OFF);
+                if (r < 0)
+                        log_debug_unit(UNIT(n)->id, "Failed to disable event source.");
+        }
+}
+
+static void busname_close_fd(BusName *n) {
+        assert(n);
+
+        if (n->starter_fd <= 0)
+                return;
+
+        close_nointr_nofail(n->starter_fd);
+        n->starter_fd = -1;
+}
+
+static int busname_watch_fd(BusName *n) {
+        int r;
+
+        assert(n);
+
+        if (n->starter_fd < 0)
+                return 0;
+
+        if (n->event_source)
+                r = sd_event_source_set_enabled(n->event_source, SD_EVENT_ON);
+        else
+                r = sd_event_add_io(UNIT(n)->manager->event, n->starter_fd, EPOLLIN, busname_dispatch_io, n, &n->event_source);
+        if (r < 0) {
+                log_warning_unit(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
+                busname_unwatch_fd(n);
+                return r;
+        }
+
+        return 0;
+}
+
+static int busname_open_fd(BusName *n) {
+        assert(n);
+
+        if (n->starter_fd >= 0)
+                return 0;
+
+        n->starter_fd = bus_kernel_create_starter(UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user", n->name);
+        if (n->starter_fd < 0) {
+                log_warning_unit(UNIT(n)->id, "Failed to create starter fd: %s", strerror(-n->starter_fd));
+                return n->starter_fd;
+        }
+
+        return 0;
+}
+
+static void busname_set_state(BusName *n, BusNameState state) {
+        BusNameState old_state;
+        assert(n);
+
+        old_state = n->state;
+        n->state = state;
+
+        if (state != BUSNAME_LISTENING)
+                busname_unwatch_fd(n);
+
+        if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_RUNNING))
+                busname_close_fd(n);
+
+        if (state != old_state)
+                log_debug_unit(UNIT(n)->id, "%s changed %s -> %s",
+                               UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
+
+        unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
+}
+
+static int busname_coldplug(Unit *u) {
+        BusName *n = BUSNAME(u);
+        int r;
+
+        assert(n);
+        assert(n->state == BUSNAME_DEAD);
+
+        if (n->deserialized_state == n->state)
+                return 0;
+
+        if (IN_SET(n->deserialized_state, BUSNAME_LISTENING, BUSNAME_RUNNING)) {
+                r = busname_open_fd(n);
+                if (r < 0)
+                        return r;
+        }
+
+        if (n->deserialized_state == BUSNAME_LISTENING) {
+                r = busname_watch_fd(n);
+                if (r < 0)
+                        return r;
+        }
+
+        busname_set_state(n, n->deserialized_state);
+        return 0;
+}
+
+static void busname_enter_dead(BusName *n, BusNameResult f) {
+        assert(n);
+
+        if (f != BUSNAME_SUCCESS)
+                n->result = f;
+
+        busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
+}
+
+static void busname_enter_listening(BusName *n) {
+        int r;
+
+        assert(n);
+
+        r = busname_open_fd(n);
+        if (r < 0) {
+                log_warning_unit(UNIT(n)->id, "%s failed to listen on bus names: %s", UNIT(n)->id, strerror(-r));
+                goto fail;
+        }
+
+        r = busname_watch_fd(n);
+        if (r < 0) {
+                log_warning_unit(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
+                goto fail;
+        }
+
+        busname_set_state(n, BUSNAME_LISTENING);
+        return;
+
+fail:
+        busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+}
+
+static void busname_enter_running(BusName *n) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        bool pending = false;
+        Unit *other;
+        Iterator i;
+        int r;
+
+        assert(n);
+
+        /* We don't take conenctions anymore if we are supposed to
+         * shut down anyway */
+
+        if (unit_stop_pending(UNIT(n))) {
+                log_debug_unit(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
+                return;
+        }
+
+        /* If there's already a start pending don't bother to do
+         * anything */
+        SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
+                if (unit_active_or_pending(other)) {
+                        pending = true;
+                        break;
+                }
+
+        if (!pending) {
+                r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
+                if (r < 0)
+                        goto fail;
+        }
+
+        busname_set_state(n, BUSNAME_RUNNING);
+        return;
+
+fail:
+        log_warning_unit(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
+        busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+}
+
+static int busname_start(Unit *u) {
+        BusName *n = BUSNAME(u);
+
+        assert(n);
+
+        if (UNIT_ISSET(n->service)) {
+                Service *service;
+
+                service = SERVICE(UNIT_DEREF(n->service));
+
+                if (UNIT(service)->load_state != UNIT_LOADED) {
+                        log_error_unit(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
+                        return -ENOENT;
+                }
+        }
+
+        assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
+
+        n->result = BUSNAME_SUCCESS;
+        busname_enter_listening(n);
+
+        return 0;
+}
+
+static int busname_stop(Unit *u) {
+        BusName *n = BUSNAME(u);
+
+        assert(n);
+        assert(n->state == BUSNAME_LISTENING || n->state == BUSNAME_RUNNING);
+
+        busname_enter_dead(n, BUSNAME_SUCCESS);
+        return 0;
+}
+
+static int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
+        BusName *n = BUSNAME(u);
+
+        assert(n);
+        assert(f);
+        assert(fds);
+
+        unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
+        unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
+
+        if (n->starter_fd >= 0) {
+                int copy;
+
+                copy = fdset_put_dup(fds, n->starter_fd);
+                if (copy < 0)
+                        return copy;
+
+                unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
+        }
+
+        return 0;
+}
+
+static int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
+        BusName *n = BUSNAME(u);
+
+        assert(n);
+        assert(key);
+        assert(value);
+
+        if (streq(key, "state")) {
+                BusNameState state;
+
+                state = busname_state_from_string(value);
+                if (state < 0)
+                        log_debug_unit(u->id, "Failed to parse state value %s", value);
+                else
+                        n->deserialized_state = state;
+
+        } else if (streq(key, "result")) {
+                BusNameResult f;
+
+                f = busname_result_from_string(value);
+                if (f < 0)
+                        log_debug_unit(u->id, "Failed to parse result value %s", value);
+                else if (f != BUSNAME_SUCCESS)
+                        n->result = f;
+
+        } else if (streq(key, "starter-fd")) {
+                int fd;
+
+                if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
+                        log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
+                else {
+                        if (n->starter_fd >= 0)
+                                close_nointr_nofail(n->starter_fd);
+                        n->starter_fd = fdset_remove(fds, fd);
+                }
+        } else
+                log_debug_unit(u->id, "Unknown serialization key '%s'", key);
+
+        return 0;
+}
+
+_pure_ static UnitActiveState busname_active_state(Unit *u) {
+        assert(u);
+
+        return state_translation_table[BUSNAME(u)->state];
+}
+
+_pure_ static const char *busname_sub_state_to_string(Unit *u) {
+        assert(u);
+
+        return busname_state_to_string(BUSNAME(u)->state);
+}
+
+static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
+        BusName *n = userdata;
+
+        assert(n);
+        assert(fd >= 0);
+
+        if (n->state != BUSNAME_LISTENING)
+                return 0;
+
+        log_debug_unit(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
+
+        if (revents != EPOLLIN) {
+                log_error_unit(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
+                               UNIT(n)->id, revents);
+                goto fail;
+        }
+
+        busname_enter_running(n);
+        return 0;
+fail:
+
+        busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+        return 0;
+}
+
+static void busname_reset_failed(Unit *u) {
+        BusName *n = BUSNAME(u);
+
+        assert(n);
+
+        if (n->state == BUSNAME_FAILED)
+                busname_set_state(n, BUSNAME_DEAD);
+
+        n->result = BUSNAME_SUCCESS;
+}
+
+static void busname_trigger_notify(Unit *u, Unit *other) {
+        BusName *n = BUSNAME(u);
+        Service *s;
+
+        assert(n);
+        assert(other);
+
+        if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
+                return;
+
+        if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
+                return;
+
+        s = SERVICE(other);
+
+        if (s->state == SERVICE_FAILED) {
+                if (s->result == SERVICE_FAILURE_START_LIMIT)
+                        busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
+                else
+                        busname_enter_listening(n);
+        }
+
+        if (IN_SET(n->state,
+                   SERVICE_DEAD,
+                   SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
+                   SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
+                   SERVICE_AUTO_RESTART))
+                busname_enter_listening(n);
+}
+
+static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
+        [BUSNAME_DEAD] = "dead",
+        [BUSNAME_LISTENING] = "listening",
+        [BUSNAME_RUNNING] = "running",
+        [BUSNAME_FAILED] = "failed"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
+
+static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
+        [BUSNAME_SUCCESS] = "success",
+        [BUSNAME_FAILURE_RESOURCES] = "resources",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
+
+const UnitVTable busname_vtable = {
+        .object_size = sizeof(BusName),
+
+        .sections =
+                "Unit\0"
+                "BusName\0"
+                "Install\0",
+        .private_section = "BusName",
+
+        .init = busname_init,
+        .done = busname_done,
+        .load = busname_load,
+
+        .coldplug = busname_coldplug,
+
+        .dump = busname_dump,
+
+        .start = busname_start,
+        .stop = busname_stop,
+
+        .serialize = busname_serialize,
+        .deserialize_item = busname_deserialize_item,
+
+        .active_state = busname_active_state,
+        .sub_state_to_string = busname_sub_state_to_string,
+
+        .trigger_notify = busname_trigger_notify,
+
+        .reset_failed = busname_reset_failed,
+
+        .bus_interface = "org.freedesktop.systemd1.BusName",
+        .bus_vtable = bus_busname_vtable,
+        .bus_changing_properties = bus_busname_changing_properties,
+
+        .status_message_formats = {
+                .finished_start_job = {
+                        [JOB_DONE]       = "Listening on %s.",
+                        [JOB_FAILED]     = "Failed to listen on %s.",
+                        [JOB_DEPENDENCY] = "Dependency failed for %s.",
+                        [JOB_TIMEOUT]    = "Timed out starting %s.",
+                },
+                .finished_stop_job = {
+                        [JOB_DONE]       = "Closed %s.",
+                        [JOB_FAILED]     = "Failed stopping %s.",
+                        [JOB_TIMEOUT]    = "Timed out stopping %s.",
+                },
+        },
+};
diff --git a/src/core/busname.h b/src/core/busname.h
new file mode 100644
index 0000000..6debd48
--- /dev/null
+++ b/src/core/busname.h
@@ -0,0 +1,65 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+typedef struct BusName BusName;
+
+#include "unit.h"
+
+typedef enum BusNameState {
+        BUSNAME_DEAD,
+        BUSNAME_LISTENING,
+        BUSNAME_RUNNING,
+        BUSNAME_FAILED,
+        _BUSNAME_STATE_MAX,
+        _BUSNAME_STATE_INVALID = -1
+} BusNameState;
+
+typedef enum BusNameResult {
+        BUSNAME_SUCCESS,
+        BUSNAME_FAILURE_RESOURCES,
+        BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT,
+        _BUSNAME_RESULT_MAX,
+        _BUSNAME_RESULT_INVALID = -1
+} BusNameResult;
+
+struct BusName {
+        Unit meta;
+
+        char *name;
+        int starter_fd;
+
+        UnitRef service;
+
+        BusNameState state, deserialized_state;
+        BusNameResult result;
+
+        sd_event_source *event_source;
+};
+
+extern const UnitVTable busname_vtable;
+
+const char* busname_state_to_string(BusNameState i) _const_;
+BusNameState busname_state_from_string(const char *s) _pure_;
+
+const char* busname_result_to_string(BusNameResult i) _const_;
+BusNameResult busname_result_from_string(const char *s) _pure_;
diff --git a/src/core/dbus-busname.c b/src/core/dbus-busname.c
new file mode 100644
index 0000000..f9511a9
--- /dev/null
+++ b/src/core/dbus-busname.c
@@ -0,0 +1,40 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "unit.h"
+#include "busname.h"
+#include "dbus-unit.h"
+#include "dbus-busname.h"
+#include "bus-util.h"
+
+static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, busname_result, BusNameResult);
+
+const sd_bus_vtable bus_busname_vtable[] = {
+        SD_BUS_VTABLE_START(0),
+        SD_BUS_PROPERTY("Name", "s", NULL, offsetof(BusName, name), 0),
+        SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(BusName, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_VTABLE_END
+};
+
+const char* const bus_busname_changing_properties[] = {
+        "Result",
+        NULL
+};
diff --git a/src/core/dbus-busname.h b/src/core/dbus-busname.h
new file mode 100644
index 0000000..3a29317
--- /dev/null
+++ b/src/core/dbus-busname.h
@@ -0,0 +1,28 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "sd-bus.h"
+#include "unit.h"
+
+extern const sd_bus_vtable bus_busname_vtable[];
+extern const char* const bus_busname_changing_properties[];
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index 663deae..a5033b2 100644
--- a/src/core/load-fragment-gperf.gperf.m4
+++ b/src/core/load-fragment-gperf.gperf.m4
@@ -223,6 +223,9 @@ EXEC_CONTEXT_CONFIG_ITEMS(Socket)m4_dnl
 CGROUP_CONTEXT_CONFIG_ITEMS(Socket)m4_dnl
 KILL_CONTEXT_CONFIG_ITEMS(Socket)m4_dnl
 m4_dnl
+BusName.Name,                    config_parse_string,                0,                             offsetof(BusName, name)
+BusName.Service,                 config_parse_busname_service,       0,                             0
+m4_dnl
 Mount.What,                      config_parse_string,                0,                             offsetof(Mount, parameters_fragment.what)
 Mount.Where,                     config_parse_path,                  0,                             offsetof(Mount, where)
 Mount.Options,                   config_parse_string,                0,                             offsetof(Mount, parameters_fragment.options)
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 8a42e73..d43d1b4 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -1457,6 +1457,51 @@ int config_parse_service_timeout(const char *unit,
         return 0;
 }
 
+int config_parse_busname_service(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        BusName *n = data;
+        int r;
+        Unit *x;
+        _cleanup_free_ char *p = NULL;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        r = unit_name_printf(UNIT(n), rvalue, &p);
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, -r, "Failed to resolve specifiers, ignoring: %s", rvalue);
+                return 0;
+        }
+
+        if (!endswith(p, ".service")) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Unit must be of type service, ignoring: %s", rvalue);
+                return 0;
+        }
+
+        r = manager_load_unit(UNIT(n)->manager, p, NULL, &error, &x);
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load unit %s, ignoring: %s", rvalue, bus_error_message(&error, r));
+                return 0;
+        }
+
+        unit_ref_set(&n->service, x);
+
+        return 0;
+}
+
 int config_parse_unit_env_file(const char *unit,
                                const char *filename,
                                unsigned line,
diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h
index 31e30e3..dcd09ad 100644
--- a/src/core/load-fragment.h
+++ b/src/core/load-fragment.h
@@ -63,6 +63,7 @@ int config_parse_trigger_unit(const char *unit, const char *filename, unsigned l
 int config_parse_path_spec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_socket_service(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_service_sockets(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_busname_service(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_env_file(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_ip_tos(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_condition_path(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
diff --git a/src/core/mount.c b/src/core/mount.c
index e46c72a..77493db 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -138,6 +138,8 @@ static void mount_init(Unit *u) {
         m->directory_mode = 0755;
 
         exec_context_init(&m->exec_context);
+        kill_context_init(&m->kill_context);
+        cgroup_context_init(&m->cgroup_context);
 
         if (unit_has_name(u, "-.mount")) {
                 /* Don't allow start/stop for root directory */
@@ -150,9 +152,6 @@ static void mount_init(Unit *u) {
                 m->exec_context.std_error = u->manager->default_std_error;
         }
 
-        kill_context_init(&m->kill_context);
-        cgroup_context_init(&m->cgroup_context);
-
         /* We need to make sure that /bin/mount is always called in
          * the same process group as us, so that the autofs kernel
          * side doesn't send us another mount request while we are
@@ -503,29 +502,22 @@ static int mount_verify(Mount *m) {
 
         b = unit_has_name(UNIT(m), e);
         if (!b) {
-                log_error_unit(UNIT(m)->id,
-                               "%s's Where setting doesn't match unit name. Refusing.",
-                               UNIT(m)->id);
+                log_error_unit(UNIT(m)->id, "%s's Where= setting doesn't match unit name. Refusing.", UNIT(m)->id);
                 return -EINVAL;
         }
 
         if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
-                log_error_unit(UNIT(m)->id,
-                               "Cannot create mount unit for API file system %s. Refusing.",
-                               m->where);
+                log_error_unit(UNIT(m)->id, "Cannot create mount unit for API file system %s. Refusing.", m->where);
                 return -EINVAL;
         }
 
         if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
-                log_error_unit(UNIT(m)->id,
-                               "%s's What setting is missing. Refusing.", UNIT(m)->id);
+                log_error_unit(UNIT(m)->id, "%s's What setting is missing. Refusing.", UNIT(m)->id);
                 return -EBADMSG;
         }
 
         if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
-                log_error_unit(UNIT(m)->id,
-                               "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",
-                               UNIT(m)->id);
+                log_error_unit(UNIT(m)->id, "%s has PAM enabled. Kill mode must be set to control-group'. Refusing.",UNIT(m)->id);
                 return -EINVAL;
         }
 
@@ -536,7 +528,9 @@ static int mount_add_extras(Mount *m) {
         Unit *u = UNIT(m);
         int r;
 
-        if (UNIT(m)->fragment_path)
+        assert(m);
+
+        if (u->fragment_path)
                 m->from_fragment = true;
 
         if (!m->where) {
@@ -551,7 +545,7 @@ static int mount_add_extras(Mount *m) {
         if (r < 0)
                 return r;
 
-        if (!UNIT(m)->description) {
+        if (!u->description) {
                 r = unit_set_description(u, m->where);
                 if (r < 0)
                         return r;
@@ -569,7 +563,7 @@ static int mount_add_extras(Mount *m) {
         if (r < 0)
                 return r;
 
-        if (UNIT(m)->default_dependencies) {
+        if (u->default_dependencies) {
                 r = mount_add_default_dependencies(m);
                 if (r < 0)
                         return r;
@@ -583,6 +577,10 @@ static int mount_add_extras(Mount *m) {
         if (r < 0)
                 return r;
 
+        r = unit_exec_context_defaults(u, &m->exec_context);
+        if (r < 0)
+                return r;
+
         return 0;
 }
 
@@ -606,10 +604,6 @@ static int mount_load(Unit *u) {
                 r = mount_add_extras(m);
                 if (r < 0)
                         return r;
-
-                r = unit_exec_context_defaults(u, &m->exec_context);
-                if (r < 0)
-                        return r;
         }
 
         return mount_verify(m);
diff --git a/src/core/socket.c b/src/core/socket.c
index 9e14ffd..aaaa8d6 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -34,8 +34,6 @@
 #endif
 
 #include "sd-event.h"
-#include "unit.h"
-#include "socket.h"
 #include "log.h"
 #include "load-dropin.h"
 #include "load-fragment.h"
@@ -44,7 +42,6 @@
 #include "path-util.h"
 #include "unit-name.h"
 #include "unit-printf.h"
-#include "dbus-socket.h"
 #include "missing.h"
 #include "special.h"
 #include "label.h"
@@ -53,6 +50,9 @@
 #include "smack-util.h"
 #include "bus-util.h"
 #include "bus-error.h"
+#include "dbus-socket.h"
+#include "unit.h"
+#include "socket.h"
 
 static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
         [SOCKET_DEAD] = UNIT_INACTIVE,
@@ -243,48 +243,6 @@ static bool have_non_accept_socket(Socket *s) {
         return false;
 }
 
-static int socket_verify(Socket *s) {
-        assert(s);
-
-        if (UNIT(s)->load_state != UNIT_LOADED)
-                return 0;
-
-        if (!s->ports) {
-                log_error_unit(UNIT(s)->id,
-                               "%s lacks Listen setting. Refusing.", UNIT(s)->id);
-                return -EINVAL;
-        }
-
-        if (s->accept && have_non_accept_socket(s)) {
-                log_error_unit(UNIT(s)->id,
-                               "%s configured for accepting sockets, but sockets are non-accepting. Refusing.",
-                               UNIT(s)->id);
-                return -EINVAL;
-        }
-
-        if (s->accept && s->max_connections <= 0) {
-                log_error_unit(UNIT(s)->id,
-                               "%s's MaxConnection setting too small. Refusing.", UNIT(s)->id);
-                return -EINVAL;
-        }
-
-        if (s->accept && UNIT_DEREF(s->service)) {
-                log_error_unit(UNIT(s)->id,
-                               "Explicit service configuration for accepting sockets not supported on %s. Refusing.",
-                               UNIT(s)->id);
-                return -EINVAL;
-        }
-
-        if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
-                log_error_unit(UNIT(s)->id,
-                               "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.",
-                               UNIT(s)->id);
-                return -EINVAL;
-        }
-
-        return 0;
-}
-
 static int socket_add_mount_links(Socket *s) {
         SocketPort *p;
         int r;
@@ -312,20 +270,14 @@ static int socket_add_mount_links(Socket *s) {
 
 static int socket_add_device_link(Socket *s) {
         char *t;
-        int r;
 
         assert(s);
 
         if (!s->bind_to_device || streq(s->bind_to_device, "lo"))
                 return 0;
 
-        if (asprintf(&t, "/sys/subsystem/net/devices/%s", s->bind_to_device) < 0)
-                return -ENOMEM;
-
-        r = unit_add_node_link(UNIT(s), t, false);
-        free(t);
-
-        return r;
+        t = strappenda("/sys/subsystem/net/devices/", s->bind_to_device);
+        return unit_add_node_link(UNIT(s), t, false);
 }
 
 static int socket_add_default_dependencies(Socket *s) {
@@ -356,55 +308,109 @@ _pure_ static bool socket_has_exec(Socket *s) {
         return false;
 }
 
-static int socket_load(Unit *u) {
-        Socket *s = SOCKET(u);
+static int socket_add_extras(Socket *s) {
+        Unit *u = UNIT(s);
         int r;
 
-        assert(u);
-        assert(u->load_state == UNIT_STUB);
-
-        if ((r = unit_load_fragment_and_dropin(u)) < 0)
-                return r;
-
-        /* This is a new unit? Then let's add in some extras */
-        if (u->load_state == UNIT_LOADED) {
-
-                if (have_non_accept_socket(s)) {
-
-                        if (!UNIT_DEREF(s->service)) {
-                                Unit *x;
+        assert(s);
 
-                                r = unit_load_related_unit(u, ".service", &x);
-                                if (r < 0)
-                                        return r;
+        if (have_non_accept_socket(s)) {
 
-                                unit_ref_set(&s->service, x);
-                        }
+                if (!UNIT_DEREF(s->service)) {
+                        Unit *x;
 
-                        r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true);
+                        r = unit_load_related_unit(u, ".service", &x);
                         if (r < 0)
                                 return r;
+
+                        unit_ref_set(&s->service, x);
                 }
 
-                if ((r = socket_add_mount_links(s)) < 0)
+                r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true);
+                if (r < 0)
                         return r;
+        }
 
-                if ((r = socket_add_device_link(s)) < 0)
-                        return r;
+        r = socket_add_mount_links(s);
+        if (r < 0)
+                return r;
 
-                if (socket_has_exec(s))
-                        if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
-                                return r;
+        r = socket_add_device_link(s);
+        if (r < 0)
+                return r;
+
+        r = unit_exec_context_defaults(u, &s->exec_context);
+        if (r < 0)
+                return r;
+
+        if (socket_has_exec(s)) {
+                r = unit_add_exec_dependencies(u, &s->exec_context);
+                if (r < 0)
+                        return r;
 
                 r = unit_add_default_slice(u);
                 if (r < 0)
                         return r;
+        }
 
-                if (UNIT(s)->default_dependencies)
-                        if ((r = socket_add_default_dependencies(s)) < 0)
-                                return r;
+        if (u->default_dependencies) {
+                r = socket_add_default_dependencies(s);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
+static int socket_verify(Socket *s) {
+        assert(s);
+
+        if (UNIT(s)->load_state != UNIT_LOADED)
+                return 0;
+
+        if (!s->ports) {
+                log_error_unit(UNIT(s)->id, "%s lacks Listen setting. Refusing.", UNIT(s)->id);
+                return -EINVAL;
+        }
+
+        if (s->accept && have_non_accept_socket(s)) {
+                log_error_unit(UNIT(s)->id, "%s configured for accepting sockets, but sockets are non-accepting. Refusing.",
+                               UNIT(s)->id);
+                return -EINVAL;
+        }
+
+        if (s->accept && s->max_connections <= 0) {
+                log_error_unit(UNIT(s)->id, "%s's MaxConnection setting too small. Refusing.", UNIT(s)->id);
+                return -EINVAL;
+        }
 
-                r = unit_exec_context_defaults(u, &s->exec_context);
+        if (s->accept && UNIT_DEREF(s->service)) {
+                log_error_unit(UNIT(s)->id, "Explicit service configuration for accepting sockets not supported on %s. Refusing.", UNIT(s)->id);
+                return -EINVAL;
+        }
+
+        if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
+                log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
+                return -EINVAL;
+        }
+
+        return 0;
+}
+
+static int socket_load(Unit *u) {
+        Socket *s = SOCKET(u);
+        int r;
+
+        assert(u);
+        assert(u->load_state == UNIT_STUB);
+
+        r = unit_load_fragment_and_dropin(u);
+        if (r < 0)
+                return r;
+
+        if (u->load_state == UNIT_LOADED) {
+                /* This is a new unit? Then let's add in some extras */
+                r = socket_add_extras(s);
                 if (r < 0)
                         return r;
         }
@@ -429,18 +435,15 @@ _const_ static const char* listen_lookup(int family, int type) {
 }
 
 static void socket_dump(Unit *u, FILE *f, const char *prefix) {
-
         SocketExecCommand c;
         Socket *s = SOCKET(u);
         SocketPort *p;
         const char *prefix2;
-        char *p2;
 
         assert(s);
         assert(f);
 
-        p2 = strappend(prefix, "\t");
-        prefix2 = p2 ? p2 : prefix;
+        prefix2 = strappenda(prefix, "\t");
 
         fprintf(f,
                 "%sSocket State: %s\n"
@@ -588,8 +591,6 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
 
                 exec_command_dump_list(s->exec_command[c], f, prefix2);
         }
-
-        free(p2);
 }
 
 static int instance_from_socket(int fd, unsigned nr, char **instance) {
@@ -1160,10 +1161,8 @@ static void socket_set_state(Socket *s, SocketState state) {
                 socket_close_fds(s);
 
         if (state != old_state)
-                log_debug_unit(UNIT(s)->id,
-                               "%s changed %s -> %s", UNIT(s)->id,
-                               socket_state_to_string(old_state),
-                               socket_state_to_string(state));
+                log_debug_unit(UNIT(s)->id, "%s changed %s -> %s",
+                               UNIT(s)->id, socket_state_to_string(old_state), socket_state_to_string(state));
 
         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
 }
@@ -1175,49 +1174,48 @@ static int socket_coldplug(Unit *u) {
         assert(s);
         assert(s->state == SOCKET_DEAD);
 
-        if (s->deserialized_state != s->state) {
-
-                if (s->deserialized_state == SOCKET_START_PRE ||
-                    s->deserialized_state == SOCKET_START_POST ||
-                    s->deserialized_state == SOCKET_STOP_PRE ||
-                    s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
-                    s->deserialized_state == SOCKET_STOP_PRE_SIGKILL ||
-                    s->deserialized_state == SOCKET_STOP_POST ||
-                    s->deserialized_state == SOCKET_FINAL_SIGTERM ||
-                    s->deserialized_state == SOCKET_FINAL_SIGKILL) {
+        if (s->deserialized_state == s->state)
+                return 0;
 
-                        if (s->control_pid <= 0)
-                                return -EBADMSG;
+        if (s->deserialized_state == SOCKET_START_PRE ||
+            s->deserialized_state == SOCKET_START_POST ||
+            s->deserialized_state == SOCKET_STOP_PRE ||
+            s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
+            s->deserialized_state == SOCKET_STOP_PRE_SIGKILL ||
+            s->deserialized_state == SOCKET_STOP_POST ||
+            s->deserialized_state == SOCKET_FINAL_SIGTERM ||
+            s->deserialized_state == SOCKET_FINAL_SIGKILL) {
 
-                        r = unit_watch_pid(UNIT(s), s->control_pid);
-                        if (r < 0)
-                                return r;
+                if (s->control_pid <= 0)
+                        return -EBADMSG;
 
-                        r = socket_arm_timer(s);
-                        if (r < 0)
-                                return r;
-                }
+                r = unit_watch_pid(UNIT(s), s->control_pid);
+                if (r < 0)
+                        return r;
 
-                if (s->deserialized_state == SOCKET_START_POST ||
-                    s->deserialized_state == SOCKET_LISTENING ||
-                    s->deserialized_state == SOCKET_RUNNING ||
-                    s->deserialized_state == SOCKET_STOP_PRE ||
-                    s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
-                    s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) {
-                        r = socket_open_fds(s);
-                        if (r < 0)
-                                return r;
-                }
+                r = socket_arm_timer(s);
+                if (r < 0)
+                        return r;
+        }
 
-                if (s->deserialized_state == SOCKET_LISTENING) {
-                        r = socket_watch_fds(s);
-                        if (r < 0)
-                                return r;
-                }
+        if (s->deserialized_state == SOCKET_START_POST ||
+            s->deserialized_state == SOCKET_LISTENING ||
+            s->deserialized_state == SOCKET_RUNNING ||
+            s->deserialized_state == SOCKET_STOP_PRE ||
+            s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
+            s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) {
+                r = socket_open_fds(s);
+                if (r < 0)
+                        return r;
+        }
 
-                socket_set_state(s, s->deserialized_state);
+        if (s->deserialized_state == SOCKET_LISTENING) {
+                r = socket_watch_fds(s);
+                if (r < 0)
+                        return r;
         }
 
+        socket_set_state(s, s->deserialized_state);
         return 0;
 }
 
@@ -1353,9 +1351,7 @@ static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
         return;
 
 fail:
-        log_warning_unit(UNIT(s)->id,
-                         "%s failed to kill processes: %s",
-                         UNIT(s)->id, strerror(-r));
+        log_warning_unit(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
 
         if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
                 socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
@@ -1385,9 +1381,7 @@ static void socket_enter_stop_pre(Socket *s, SocketResult f) {
         return;
 
 fail:
-        log_warning_unit(UNIT(s)->id,
-                         "%s failed to run 'stop-pre' task: %s",
-                         UNIT(s)->id, strerror(-r));
+        log_warning_unit(UNIT(s)->id, "%s failed to run 'stop-pre' task: %s", UNIT(s)->id, strerror(-r));
         socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
 }
 
@@ -1397,9 +1391,7 @@ static void socket_enter_listening(Socket *s) {
 
         r = socket_watch_fds(s);
         if (r < 0) {
-                log_warning_unit(UNIT(s)->id,
-                                 "%s failed to watch sockets: %s",
-                                 UNIT(s)->id, strerror(-r));
+                log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r));
                 goto fail;
         }
 
@@ -1416,9 +1408,7 @@ static void socket_enter_start_post(Socket *s) {
 
         r = socket_open_fds(s);
         if (r < 0) {
-                log_warning_unit(UNIT(s)->id,
-                                 "%s failed to listen on sockets: %s",
-                                 UNIT(s)->id, strerror(-r));
+                log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r));
                 goto fail;
         }
 
@@ -1429,9 +1419,7 @@ static void socket_enter_start_post(Socket *s) {
         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
                 r = socket_spawn(s, s->control_command, &s->control_pid);
                 if (r < 0) {
-                        log_warning_unit(UNIT(s)->id,
-                                         "%s failed to run 'start-post' task: %s",
-                                         UNIT(s)->id, strerror(-r));
+                        log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
                         goto fail;
                 }
 
@@ -1454,7 +1442,8 @@ static void socket_enter_start_pre(Socket *s) {
         s->control_command_id = SOCKET_EXEC_START_PRE;
 
         if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
-                if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
+                r = socket_spawn(s, s->control_command, &s->control_pid);
+                if (r < 0)
                         goto fail;
 
                 socket_set_state(s, SOCKET_START_PRE);
@@ -1464,9 +1453,7 @@ static void socket_enter_start_pre(Socket *s) {
         return;
 
 fail:
-        log_warning_unit(UNIT(s)->id,
-                         "%s failed to run 'start-pre' task: %s",
-                         UNIT(s)->id, strerror(-r));
+        log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
         socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
 }
 
@@ -1479,9 +1466,8 @@ static void socket_enter_running(Socket *s, int cfd) {
         /* We don't take connections anymore if we are supposed to
          * shut down anyway */
         if (unit_stop_pending(UNIT(s))) {
-                log_debug_unit(UNIT(s)->id,
-                               "Suppressing connection request on %s since unit stop is scheduled.",
-                               UNIT(s)->id);
+
+                log_debug_unit(UNIT(s)->id, "Suppressing connection request on %s since unit stop is scheduled.", UNIT(s)->id);
 
                 if (cfd >= 0)
                         close_nointr_nofail(cfd);
@@ -1491,9 +1477,7 @@ static void socket_enter_running(Socket *s, int cfd) {
 
                         r = socket_watch_fds(s);
                         if (r < 0) {
-                                log_warning_unit(UNIT(s)->id,
-                                                 "%s failed to watch sockets: %s",
-                                                 UNIT(s)->id, strerror(-r));
+                                log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r));
                                 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
                         }
                 }
@@ -1503,13 +1487,13 @@ static void socket_enter_running(Socket *s, int cfd) {
 
         if (cfd < 0) {
                 Iterator i;
-                Unit *u;
+                Unit *other;
                 bool pending = false;
 
                 /* If there's already a start pending don't bother to
                  * do anything */
-                SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERS], i)
-                        if (unit_active_or_pending(u)) {
+                SET_FOREACH(other, UNIT(s)->dependencies[UNIT_TRIGGERS], i)
+                        if (unit_active_or_pending(other)) {
                                 pending = true;
                                 break;
                         }
@@ -1526,9 +1510,7 @@ static void socket_enter_running(Socket *s, int cfd) {
                 Service *service;
 
                 if (s->n_connections >= s->max_connections) {
-                        log_warning_unit(UNIT(s)->id,
-                                         "%s: Too many incoming connections (%u)",
-                                         UNIT(s)->id, s->n_connections);
+                        log_warning_unit(UNIT(s)->id, "%s: Too many incoming connections (%u)", UNIT(s)->id, s->n_connections);
                         close_nointr_nofail(cfd);
                         return;
                 }
@@ -1555,7 +1537,6 @@ static void socket_enter_running(Socket *s, int cfd) {
                 }
 
                 name = unit_name_build(prefix, instance, ".service");
-
                 if (!name) {
                         r = -ENOMEM;
                         goto fail;
@@ -1591,11 +1572,10 @@ static void socket_enter_running(Socket *s, int cfd) {
         return;
 
 fail:
-        log_warning_unit(UNIT(s)->id,
-                         "%s failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
-                         UNIT(s)->id,
-                         cfd >= 0 ? "template" : "non-template",
+        log_warning_unit(UNIT(s)->id, "%s failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
+                         UNIT(s)->id, cfd >= 0 ? "template" : "non-template",
                          bus_error_message(&error, r));
+
         socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
 
         if (cfd >= 0)
@@ -1613,15 +1593,14 @@ static void socket_run_next(Socket *s) {
 
         s->control_command = s->control_command->command_next;
 
-        if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
+        r = socket_spawn(s, s->control_command, &s->control_pid);
+        if (r < 0)
                 goto fail;
 
         return;
 
 fail:
-        log_warning_unit(UNIT(s)->id,
-                         "%s failed to run next task: %s",
-                         UNIT(s)->id, strerror(-r));
+        log_warning_unit(UNIT(s)->id, "%s failed to run next task: %s", UNIT(s)->id, strerror(-r));
 
         if (s->state == SOCKET_START_POST)
                 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
@@ -1657,9 +1636,7 @@ static int socket_start(Unit *u) {
                 service = SERVICE(UNIT_DEREF(s->service));
 
                 if (UNIT(service)->load_state != UNIT_LOADED) {
-                        log_error_unit(u->id,
-                                       "Socket service %s not loaded, refusing.",
-                                       UNIT(service)->id);
+                        log_error_unit(u->id, "Socket service %s not loaded, refusing.", UNIT(service)->id);
                         return -ENOENT;
                 }
 
@@ -1668,9 +1645,7 @@ static int socket_start(Unit *u) {
                 if (service->state != SERVICE_DEAD &&
                     service->state != SERVICE_FAILED &&
                     service->state != SERVICE_AUTO_RESTART) {
-                        log_error_unit(u->id,
-                                       "Socket service %s already active, refusing.",
-                                       UNIT(service)->id);
+                        log_error_unit(u->id, "Socket service %s already active, refusing.", UNIT(service)->id);
                         return -EBUSY;
                 }
 
@@ -1687,6 +1662,7 @@ static int socket_start(Unit *u) {
 
         s->result = SOCKET_SUCCESS;
         socket_enter_start_pre(s);
+
         return 0;
 }
 
@@ -1778,15 +1754,13 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
         assert(u);
         assert(key);
         assert(value);
-        assert(fds);
 
         if (streq(key, "state")) {
                 SocketState state;
 
                 state = socket_state_from_string(value);
                 if (state < 0)
-                        log_debug_unit(u->id,
-                                       "Failed to parse state value %s", value);
+                        log_debug_unit(u->id, "Failed to parse state value %s", value);
                 else
                         s->deserialized_state = state;
         } else if (streq(key, "result")) {
@@ -1794,8 +1768,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 f = socket_result_from_string(value);
                 if (f < 0)
-                        log_debug_unit(u->id,
-                                       "Failed to parse result value %s", value);
+                        log_debug_unit(u->id, "Failed to parse result value %s", value);
                 else if (f != SOCKET_SUCCESS)
                         s->result = f;
 
@@ -1803,16 +1776,14 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 unsigned k;
 
                 if (safe_atou(value, &k) < 0)
-                        log_debug_unit(u->id,
-                                       "Failed to parse n-accepted value %s", value);
+                        log_debug_unit(u->id, "Failed to parse n-accepted value %s", value);
                 else
                         s->n_accepted += k;
         } else if (streq(key, "control-pid")) {
                 pid_t pid;
 
                 if (parse_pid(value, &pid) < 0)
-                        log_debug_unit(u->id,
-                                       "Failed to parse control-pid value %s", value);
+                        log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
                 else
                         s->control_pid = pid;
         } else if (streq(key, "control-command")) {
@@ -1820,8 +1791,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 id = socket_exec_command_from_string(value);
                 if (id < 0)
-                        log_debug_unit(u->id,
-                                       "Failed to parse exec-command value %s", value);
+                        log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
                 else {
                         s->control_command_id = id;
                         s->control_command = s->exec_command[id];
@@ -1831,8 +1801,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 SocketPort *p;
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
-                        log_debug_unit(u->id,
-                                       "Failed to parse fifo value %s", value);
+                        log_debug_unit(u->id, "Failed to parse fifo value %s", value);
                 else {
 
                         LIST_FOREACH(port, p, s->ports)
@@ -1852,8 +1821,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 SocketPort *p;
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
-                        log_debug_unit(u->id,
-                                       "Failed to parse special value %s", value);
+                        log_debug_unit(u->id, "Failed to parse special value %s", value);
                 else {
 
                         LIST_FOREACH(port, p, s->ports)
@@ -1873,8 +1841,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 SocketPort *p;
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
-                        log_debug_unit(u->id,
-                                       "Failed to parse mqueue value %s", value);
+                        log_debug_unit(u->id, "Failed to parse mqueue value %s", value);
                 else {
 
                         LIST_FOREACH(port, p, s->ports)
@@ -1894,8 +1861,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 SocketPort *p;
 
                 if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
-                        log_debug_unit(u->id,
-                                       "Failed to parse socket value %s", value);
+                        log_debug_unit(u->id, "Failed to parse socket value %s", value);
                 else {
 
                         LIST_FOREACH(port, p, s->ports)
@@ -1914,8 +1880,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 SocketPort *p;
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
-                        log_debug_unit(u->id,
-                                       "Failed to parse socket value %s", value);
+                        log_debug_unit(u->id, "Failed to parse socket value %s", value);
                 else {
 
                         LIST_FOREACH(port, p, s->ports)
@@ -1929,8 +1894,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                         }
                 }
         } else
-                log_debug_unit(UNIT(s)->id,
-                               "Unknown serialization key '%s'", key);
+                log_debug_unit(UNIT(s)->id, "Unknown serialization key '%s'", key);
 
         return 0;
 }
@@ -2039,12 +2003,10 @@ static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
         if (revents != EPOLLIN) {
 
                 if (revents & EPOLLHUP)
-                        log_error_unit(UNIT(p->socket)->id,
-                                       "%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.",
+                        log_error_unit(UNIT(p->socket)->id, "%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.",
                                        UNIT(p->socket)->id);
                 else
-                        log_error_unit(UNIT(p->socket)->id,
-                                       "%s: Got unexpected poll event (0x%x) on socket.",
+                        log_error_unit(UNIT(p->socket)->id, "%s: Got unexpected poll event (0x%x) on socket.",
                                        UNIT(p->socket)->id, revents);
 
                 goto fail;
@@ -2291,6 +2253,17 @@ int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) {
         return 0;
 }
 
+static void socket_reset_failed(Unit *u) {
+        Socket *s = SOCKET(u);
+
+        assert(s);
+
+        if (s->state == SOCKET_FAILED)
+                socket_set_state(s, SOCKET_DEAD);
+
+        s->result = SOCKET_SUCCESS;
+}
+
 static void socket_notify_service_dead(Socket *s, bool failed_permanent) {
         assert(s);
 
@@ -2300,9 +2273,7 @@ static void socket_notify_service_dead(Socket *s, bool failed_permanent) {
          * services. */
 
         if (s->state == SOCKET_RUNNING) {
-                log_debug_unit(UNIT(s)->id,
-                               "%s got notified about service death (failed permanently: %s)",
-                               UNIT(s)->id, yes_no(failed_permanent));
+                log_debug_unit(UNIT(s)->id, "%s got notified about service death (failed permanently: %s)", UNIT(s)->id, yes_no(failed_permanent));
                 if (failed_permanent)
                         socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_FAILED_PERMANENT);
                 else
@@ -2321,24 +2292,12 @@ void socket_connection_unref(Socket *s) {
         assert(s->n_connections > 0);
         s->n_connections--;
 
-        log_debug_unit(UNIT(s)->id,
-                       "%s: One connection closed, %u left.", UNIT(s)->id, s->n_connections);
-}
-
-static void socket_reset_failed(Unit *u) {
-        Socket *s = SOCKET(u);
-
-        assert(s);
-
-        if (s->state == SOCKET_FAILED)
-                socket_set_state(s, SOCKET_DEAD);
-
-        s->result = SOCKET_SUCCESS;
+        log_debug_unit(UNIT(s)->id, "%s: One connection closed, %u left.", UNIT(s)->id, s->n_connections);
 }
 
 static void socket_trigger_notify(Unit *u, Unit *other) {
         Socket *s = SOCKET(u);
-        Service *se = SERVICE(other);
+        Service *se;
 
         assert(u);
         assert(other);
@@ -2354,6 +2313,8 @@ static void socket_trigger_notify(Unit *u, Unit *other) {
             other->type != UNIT_SERVICE)
                 return;
 
+        se = SERVICE(other);
+
         if (se->state == SERVICE_FAILED)
                 socket_notify_service_dead(s, se->result == SERVICE_FAILURE_START_LIMIT);
 
diff --git a/src/core/unit.c b/src/core/unit.c
index 31d5f11..50db86c 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -53,14 +53,15 @@
 
 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
         [UNIT_SERVICE] = &service_vtable,
-        [UNIT_TIMER] = &timer_vtable,
         [UNIT_SOCKET] = &socket_vtable,
+        [UNIT_BUSNAME] = &busname_vtable,
         [UNIT_TARGET] = &target_vtable,
+        [UNIT_SNAPSHOT] = &snapshot_vtable,
         [UNIT_DEVICE] = &device_vtable,
         [UNIT_MOUNT] = &mount_vtable,
         [UNIT_AUTOMOUNT] = &automount_vtable,
-        [UNIT_SNAPSHOT] = &snapshot_vtable,
         [UNIT_SWAP] = &swap_vtable,
+        [UNIT_TIMER] = &timer_vtable,
         [UNIT_PATH] = &path_vtable,
         [UNIT_SLICE] = &slice_vtable,
         [UNIT_SCOPE] = &scope_vtable
diff --git a/src/core/unit.h b/src/core/unit.h
index 299ded6..8ceeece 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -276,14 +276,15 @@ typedef enum UnitSetPropertiesMode {
 } UnitSetPropertiesMode;
 
 #include "service.h"
-#include "timer.h"
 #include "socket.h"
+#include "busname.h"
 #include "target.h"
+#include "snapshot.h"
 #include "device.h"
 #include "mount.h"
 #include "automount.h"
-#include "snapshot.h"
 #include "swap.h"
+#include "timer.h"
 #include "path.h"
 #include "slice.h"
 #include "scope.h"
@@ -462,15 +463,16 @@ extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
 
 #define UNIT_TRIGGER(u) ((Unit*) set_first((u)->dependencies[UNIT_TRIGGERS]))
 
-DEFINE_CAST(SOCKET, Socket);
-DEFINE_CAST(TIMER, Timer);
 DEFINE_CAST(SERVICE, Service);
+DEFINE_CAST(SOCKET, Socket);
+DEFINE_CAST(BUSNAME, BusName);
 DEFINE_CAST(TARGET, Target);
+DEFINE_CAST(SNAPSHOT, Snapshot);
 DEFINE_CAST(DEVICE, Device);
 DEFINE_CAST(MOUNT, Mount);
 DEFINE_CAST(AUTOMOUNT, Automount);
-DEFINE_CAST(SNAPSHOT, Snapshot);
 DEFINE_CAST(SWAP, Swap);
+DEFINE_CAST(TIMER, Timer);
 DEFINE_CAST(PATH, Path);
 DEFINE_CAST(SLICE, Slice);
 DEFINE_CAST(SCOPE, Scope);
diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index fb852bd..76a55c7 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -1096,6 +1096,56 @@ int bus_kernel_create_bus(const char *name, char **s) {
         return fd;
 }
 
+int bus_kernel_create_starter(const char *bus, const char *name) {
+        struct kdbus_cmd_hello *hello;
+        struct kdbus_item *n;
+        char *p;
+        int fd;
+
+        assert(bus);
+        assert(name);
+
+        p = alloca(sizeof("/dev/kdbus/") - 1 + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + sizeof("/bus"));
+        sprintf(p, "/dev/kdbus/%lu-%s/bus", (unsigned long) getuid(), bus);
+
+        fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        hello = alloca0(ALIGN8(offsetof(struct kdbus_cmd_hello, items) +
+                               offsetof(struct kdbus_item, str) +
+                               strlen(name) + 1));
+
+        n = hello->items;
+        strcpy(n->str, name);
+        n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
+        n->type = KDBUS_ITEM_STARTER_NAME;
+
+        hello->size = ALIGN8(offsetof(struct kdbus_cmd_hello, items) + n->size);
+        hello->conn_flags = KDBUS_HELLO_STARTER;
+        hello->pool_size = KDBUS_POOL_SIZE;
+
+        if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
+                close_nointr_nofail(fd);
+                return -errno;
+        }
+
+        /* The higher 32bit of both flags fields are considered
+         * 'incompatible flags'. Refuse them all for now. */
+        if (hello->bus_flags > 0xFFFFFFFFULL ||
+            hello->conn_flags > 0xFFFFFFFFULL) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
+        if (hello->bloom_size != BLOOM_SIZE) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
+        return fd;
+}
+
 int bus_kernel_create_namespace(const char *name, char **s) {
         struct kdbus_cmd_ns_make *make;
         struct kdbus_item *n;
diff --git a/src/libsystemd-bus/bus-kernel.h b/src/libsystemd-bus/bus-kernel.h
index 92d3888..a9dddc2 100644
--- a/src/libsystemd-bus/bus-kernel.h
+++ b/src/libsystemd-bus/bus-kernel.h
@@ -62,6 +62,7 @@ int bus_kernel_read_message(sd_bus *bus);
 
 int bus_kernel_create_bus(const char *name, char **s);
 int bus_kernel_create_namespace(const char *name, char **s);
+int bus_kernel_create_starter(const char *bus, const char *name);
 
 int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *size);
 void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t size);
diff --git a/src/shared/special.h b/src/shared/special.h
index 6d252e7..2fe5db5 100644
--- a/src/shared/special.h
+++ b/src/shared/special.h
@@ -46,6 +46,7 @@
 /* Early boot targets */
 #define SPECIAL_SYSINIT_TARGET "sysinit.target"
 #define SPECIAL_SOCKETS_TARGET "sockets.target"
+#define SPECIAL_BUSNAMES_TARGET "busnames.target"
 #define SPECIAL_TIMERS_TARGET "timers.target"
 #define SPECIAL_PATHS_TARGET "paths.target"
 #define SPECIAL_LOCAL_FS_TARGET "local-fs.target"
diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c
index 2335463..178efef 100644
--- a/src/shared/unit-name.c
+++ b/src/shared/unit-name.c
@@ -36,13 +36,14 @@
 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
         [UNIT_SERVICE] = "service",
         [UNIT_SOCKET] = "socket",
+        [UNIT_BUSNAME] = "busname",
         [UNIT_TARGET] = "target",
+        [UNIT_SNAPSHOT] = "snapshot",
         [UNIT_DEVICE] = "device",
         [UNIT_MOUNT] = "mount",
         [UNIT_AUTOMOUNT] = "automount",
-        [UNIT_SNAPSHOT] = "snapshot",
-        [UNIT_TIMER] = "timer",
         [UNIT_SWAP] = "swap",
+        [UNIT_TIMER] = "timer",
         [UNIT_PATH] = "path",
         [UNIT_SLICE] = "slice",
         [UNIT_SCOPE] = "scope"
@@ -441,7 +442,7 @@ char *unit_name_from_path_instance(const char *prefix, const char *path, const c
 }
 
 char *unit_name_to_path(const char *name) {
-        char *w, *e;
+        _cleanup_free_ char *w = NULL;
 
         assert(name);
 
@@ -449,10 +450,7 @@ char *unit_name_to_path(const char *name) {
         if (!w)
                 return NULL;
 
-        e = unit_name_path_unescape(w);
-        free(w);
-
-        return e;
+        return unit_name_path_unescape(w);
 }
 
 char *unit_dbus_path_from_name(const char *name) {
diff --git a/src/shared/unit-name.h b/src/shared/unit-name.h
index 20138df..57719d5 100644
--- a/src/shared/unit-name.h
+++ b/src/shared/unit-name.h
@@ -33,13 +33,14 @@ typedef enum UnitLoadState UnitLoadState;
 enum UnitType {
         UNIT_SERVICE = 0,
         UNIT_SOCKET,
+        UNIT_BUSNAME,
         UNIT_TARGET,
+        UNIT_SNAPSHOT,
         UNIT_DEVICE,
         UNIT_MOUNT,
         UNIT_AUTOMOUNT,
-        UNIT_SNAPSHOT,
-        UNIT_TIMER,
         UNIT_SWAP,
+        UNIT_TIMER,
         UNIT_PATH,
         UNIT_SLICE,
         UNIT_SCOPE,

commit f9638db8de2f915a5c5f6e4b7292494168eb4141
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Dec 2 23:31:41 2013 +0100

    bus: make sure we check for "incompatible" flags negotiated with kernel kdbus

diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 98fc27c..fb852bd 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -1074,6 +1074,13 @@ int bus_kernel_create_bus(const char *name, char **s) {
                 return -errno;
         }
 
+        /* The higher 32bit of the flags field are considered
+         * 'incompatible flags'. Refuse them all for now. */
+        if (make->flags > 0xFFFFFFFFULL) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
         if (s) {
                 char *p;
 
@@ -1118,6 +1125,13 @@ int bus_kernel_create_namespace(const char *name, char **s) {
                 return -errno;
         }
 
+        /* The higher 32bit of the flags field are considered
+         * 'incompatible flags'. Refuse them all for now. */
+        if (make->flags > 0xFFFFFFFFULL) {
+                close_nointr_nofail(fd);
+                return -ENOTSUP;
+        }
+
         if (s) {
                 char *p;
 

commit 8bf9fcf48853021ac713d7675b89143e4e8b1ce5
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Dec 2 23:10:28 2013 +0100

    core: extra paranoia when deserializing kdbus fd

diff --git a/src/core/manager.c b/src/core/manager.c
index badf19e..1949386 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -2172,7 +2172,7 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
                 } else if (startswith(l, "kdbus-fd=")) {
                         int fd;
 
-                        if (safe_atoi(l + 9, &fd) < 0 || !fdset_contains(fds, fd))
+                        if (safe_atoi(l + 9, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                                 log_debug("Failed to parse kdbus fd: %s", l + 9);
                         else {
                                 if (m->kdbus_fd >= 0)

commit cabb78068899232c152f4585f19d023e373aa73d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Dec 2 23:08:25 2013 +0100

    macro: add a macro to test whether a value is in a specified list
    
    Introduce IN_SET() macro to nicely check whether a value a is one of a
    few listed values.
    
    This makes writing this:
    
            if (a == 1 || a == 7 || a == 8 || a == 9)
    
    nicer, by allowing this:
    
            if (IN_SET(a, 1, 7, 8, 9))
    
    This is particularly useful for state machine enums.

diff --git a/src/shared/macro.h b/src/shared/macro.h
index 9f5f51c..54b641b 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -285,4 +285,17 @@ do {                                                                    \
 #define SET_FLAG(v, flag, b) \
         (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
 
+#define IN_SET(x, ...) ({                                               \
+        typeof(x) _x = (x);                                             \
+        unsigned _i;                                                    \
+        bool _found = false;                                            \
+        for (_i = 0; _i < sizeof((typeof(_x)[]) { __VA_ARGS__ })/sizeof(typeof(_x)); _i++) \
+                if (((typeof(_x)[]) { __VA_ARGS__ })[_i] == _x) {       \
+                        _found = true;                                  \
+                        break;                                          \
+                }                                                       \
+        _found;                                                         \
+        })
+
+
 #include "log.h"
diff --git a/src/test/test-util.c b/src/test/test-util.c
index afc3247..05b2294 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -568,6 +568,16 @@ static void test_get_files_in_directory(void) {
         assert_se(get_files_in_directory(".", NULL) >= 0);
 }
 
+static void test_in_set(void) {
+        assert_se(IN_SET(1, 1));
+        assert_se(IN_SET(1, 1, 2, 3, 4));
+        assert_se(IN_SET(2, 1, 2, 3, 4));
+        assert_se(IN_SET(3, 1, 2, 3, 4));
+        assert_se(IN_SET(4, 1, 2, 3, 4));
+        assert_se(!IN_SET(0, 1));
+        assert_se(!IN_SET(0, 1, 2, 3, 4));
+}
+
 int main(int argc, char *argv[]) {
         test_streq_ptr();
         test_first_word();
@@ -604,6 +614,7 @@ int main(int argc, char *argv[]) {
         test_split_pair();
         test_fstab_node_to_udev_node();
         test_get_files_in_directory();
+        test_in_set();
 
         return 0;
 }



More information about the systemd-commits mailing list