[systemd-commits] 7 commits - Makefile.am TODO src/core src/notify src/run src/shared src/systemctl

Lennart Poettering lennart at kemper.freedesktop.org
Sun Jun 30 15:18:15 PDT 2013


 Makefile.am               |    4 
 TODO                      |    2 
 src/core/cgroup.c         |   38 ++-
 src/core/cgroup.h         |    2 
 src/core/dbus-cgroup.h    |    2 
 src/core/dbus-mount.c     |    1 
 src/core/dbus-scope.c     |  162 +++++++++++++++
 src/core/dbus-scope.h     |   33 +++
 src/core/dbus-service.c   |   11 -
 src/core/dbus-slice.c     |    1 
 src/core/dbus-socket.c    |    1 
 src/core/dbus-swap.c      |    1 
 src/core/scope.c          |  466 ++++++++++++++++++++++++++++++++++++++++++++++
 src/core/scope.h          |   69 ++++++
 src/core/service.c        |    5 
 src/core/slice.c          |    5 
 src/core/snapshot.c       |   30 +-
 src/core/snapshot.h       |    1 
 src/core/unit.c           |    3 
 src/core/unit.h           |    2 
 src/notify/notify.c       |    3 
 src/run/run.c             |  194 ++++++++++++++++---
 src/shared/cgroup-util.c  |   17 +
 src/shared/cgroup-util.h  |    1 
 src/shared/unit-name.c    |    3 
 src/shared/unit-name.h    |    1 
 src/systemctl/systemctl.c |   52 +----
 27 files changed, 1013 insertions(+), 97 deletions(-)

New commits:
commit 6c12b52e19640747e96f89d85422941a23dc6b29
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Jul 1 00:03:57 2013 +0200

    core: add new "scope" unit type for making a unit of pre-existing processes
    
    "Scope" units are very much like service units, however with the
    difference that they are created from pre-existing processes, rather
    than processes that systemd itself forks off. This means they are
    generated programmatically via the bus API as transient units rather
    than from static configuration read from disk. Also, they do not provide
    execution-time parameters, as at the time systemd adds the processes to
    the scope unit they already exist and the parameters cannot be applied
    anymore.
    
    The primary benefit of this new unit type is to create arbitrary cgroups
    for worker-processes forked off an existing service.
    
    This commit also adds a a new mode to "systemd-run" to run the specified
    processes in a scope rather then a transient service.

diff --git a/Makefile.am b/Makefile.am
index ea18640..4593245 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -850,6 +850,8 @@ libsystemd_core_la_SOURCES = \
 	src/core/path.h \
 	src/core/slice.c \
 	src/core/slice.h \
+	src/core/scope.c \
+	src/core/scope.h \
 	src/core/load-dropin.c \
 	src/core/load-dropin.h \
 	src/core/execute.c \
@@ -886,6 +888,8 @@ libsystemd_core_la_SOURCES = \
 	src/core/dbus-path.h \
 	src/core/dbus-slice.c \
 	src/core/dbus-slice.h \
+	src/core/dbus-scope.c \
+	src/core/dbus-scope.h \
 	src/core/dbus-execute.c \
 	src/core/dbus-execute.h \
 	src/core/dbus-kill.c \
diff --git a/TODO b/TODO
index 8a3df60..279446e 100644
--- a/TODO
+++ b/TODO
@@ -28,6 +28,8 @@ Fedora 19:
 
 Features:
 
+* service_coldplug() appears to reinstall the wrong stop timeout watch?
+
 * transient units: allow creating auxiliary units with the same call
 
 * make BlockIODeviceWeight=, BlockIODeviceBandwidth= runtime settable
diff --git a/src/core/dbus-scope.c b/src/core/dbus-scope.c
new file mode 100644
index 0000000..1497b76
--- /dev/null
+++ b/src/core/dbus-scope.c
@@ -0,0 +1,162 @@
+/*-*- 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 <errno.h>
+
+#include "dbus-unit.h"
+#include "dbus-common.h"
+#include "dbus-cgroup.h"
+#include "dbus-kill.h"
+#include "selinux-access.h"
+#include "dbus-scope.h"
+
+#define BUS_SCOPE_INTERFACE                                             \
+        " <interface name=\"org.freedesktop.systemd1.Scope\">\n"        \
+        "  <property name=\"TimeoutStopUSec\" type=\"t\" access=\"read\"/>\n" \
+        BUS_KILL_CONTEXT_INTERFACE                                      \
+        BUS_CGROUP_CONTEXT_INTERFACE                                    \
+        "  <property name=\"Result\" type=\"s\" access=\"read\"/>\n"    \
+        " </interface>\n"
+
+#define INTROSPECTION                                                   \
+        DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
+        "<node>\n"                                                      \
+        BUS_UNIT_INTERFACE                                              \
+        BUS_SCOPE_INTERFACE                                             \
+        BUS_PROPERTIES_INTERFACE                                        \
+        BUS_PEER_INTERFACE                                              \
+        BUS_INTROSPECTABLE_INTERFACE                                    \
+        "</node>\n"
+
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Scope\0"
+
+const char bus_scope_interface[] _introspect_("Scope") = BUS_SCOPE_INTERFACE;
+
+static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_scope_append_scope_result, scope_result, ScopeResult);
+
+static const BusProperty bus_scope_properties[] = {
+        { "TimeoutStopUSec",        bus_property_append_usec,      "t", offsetof(Scope, timeout_stop_usec) },
+        { "Result",                 bus_scope_append_scope_result, "s", offsetof(Scope, result)            },
+        {}
+};
+
+DBusHandlerResult bus_scope_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
+        Scope *s = SCOPE(u);
+
+        const BusBoundProperties bps[] = {
+                { "org.freedesktop.systemd1.Unit",  bus_unit_properties,           u },
+                { "org.freedesktop.systemd1.Scope", bus_scope_properties,          s },
+                { "org.freedesktop.systemd1.Scope", bus_cgroup_context_properties, &s->cgroup_context },
+                { "org.freedesktop.systemd1.Scope", bus_kill_context_properties,   &s->kill_context   },
+                {}
+        };
+
+        SELINUX_UNIT_ACCESS_CHECK(u, c, message, "status");
+
+        return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, bps);
+}
+
+static int bus_scope_set_transient_properties(
+                Scope *s,
+                const char *name,
+                DBusMessageIter *i,
+                UnitSetPropertiesMode mode,
+                DBusError *error) {
+
+        int r;
+
+        assert(name);
+        assert(s);
+        assert(i);
+
+        if (streq(name, "PIDs")) {
+                DBusMessageIter sub;
+
+                if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_ARRAY ||
+                    dbus_message_iter_get_element_type(i) != DBUS_TYPE_UINT32)
+                        return -EINVAL;
+
+                r = set_ensure_allocated(&s->pids, trivial_hash_func, trivial_compare_func);
+                if (r < 0)
+                        return r;
+
+                dbus_message_iter_recurse(i, &sub);
+                while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32) {
+                        uint32_t pid;
+
+                        dbus_message_iter_get_basic(&sub, &pid);
+
+                        if (pid <= 1)
+                                return -EINVAL;
+
+                        r = set_put(s->pids, LONG_TO_PTR(pid));
+                        if (r < 0 && r != -EEXIST)
+                                return r;
+
+                        dbus_message_iter_next(&sub);
+                }
+
+                if (set_size(s->pids) <= 0)
+                        return -EINVAL;
+
+                return 1;
+        }
+
+        return 0;
+}
+
+int bus_scope_set_property(
+                Unit *u,
+                const char *name,
+                DBusMessageIter *i,
+                UnitSetPropertiesMode mode,
+                DBusError *error) {
+
+        Scope *s = SCOPE(u);
+        int r;
+
+        assert(name);
+        assert(u);
+        assert(i);
+
+        r = bus_cgroup_set_property(u, &s->cgroup_context, name, i, mode, error);
+        if (r != 0)
+                return r;
+
+        if (u->load_state == UNIT_STUB) {
+                /* While we are created we still accept PIDs */
+
+                r = bus_scope_set_transient_properties(s, name, i, mode, error);
+                if (r != 0)
+                        return r;
+        }
+
+        return 0;
+}
+
+int bus_scope_commit_properties(Unit *u) {
+        assert(u);
+
+        unit_realize_cgroup(u);
+        return 0;
+}
diff --git a/src/core/dbus-scope.h b/src/core/dbus-scope.h
new file mode 100644
index 0000000..e6836f1
--- /dev/null
+++ b/src/core/dbus-scope.h
@@ -0,0 +1,33 @@
+/*-*- 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 <dbus/dbus.h>
+
+#include "unit.h"
+
+DBusHandlerResult bus_scope_message_handler(Unit *u, DBusConnection *c, DBusMessage *message);
+
+int bus_scope_set_property(Unit *u, const char *name, DBusMessageIter *i, UnitSetPropertiesMode mode, DBusError *error);
+int bus_scope_commit_properties(Unit *u);
+
+extern const char bus_scope_interface[];
diff --git a/src/core/scope.c b/src/core/scope.c
new file mode 100644
index 0000000..f88adda
--- /dev/null
+++ b/src/core/scope.c
@@ -0,0 +1,466 @@
+/*-*- 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 <errno.h>
+#include <signal.h>
+#include <unistd.h>
+
+#include "unit.h"
+#include "scope.h"
+#include "load-fragment.h"
+#include "log.h"
+#include "dbus-scope.h"
+#include "special.h"
+#include "unit-name.h"
+#include "load-dropin.h"
+
+static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
+        [SCOPE_DEAD] = UNIT_INACTIVE,
+        [SCOPE_RUNNING] = UNIT_ACTIVE,
+        [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
+        [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
+        [SCOPE_FAILED] = UNIT_FAILED
+};
+
+static void scope_init(Unit *u) {
+        Scope *s = SCOPE(u);
+
+        assert(u);
+        assert(u->load_state == UNIT_STUB);
+
+        s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
+
+        watch_init(&s->timer_watch);
+
+        cgroup_context_init(&s->cgroup_context);
+        kill_context_init(&s->kill_context);
+
+        UNIT(s)->ignore_on_isolate = true;
+        UNIT(s)->ignore_on_snapshot = true;
+}
+
+static void scope_done(Unit *u) {
+        Scope *s = SCOPE(u);
+
+        assert(u);
+
+        cgroup_context_done(&s->cgroup_context);
+
+        set_free(s->pids);
+        s->pids = NULL;
+
+        unit_unwatch_timer(u, &s->timer_watch);
+}
+
+static void scope_set_state(Scope *s, ScopeState state) {
+        ScopeState old_state;
+        assert(s);
+
+        old_state = s->state;
+        s->state = state;
+
+        if (state != SCOPE_STOP_SIGTERM &&
+            state != SCOPE_STOP_SIGKILL)
+                unit_unwatch_timer(UNIT(s), &s->timer_watch);
+
+        if (state != old_state)
+                log_debug("%s changed %s -> %s",
+                          UNIT(s)->id,
+                          scope_state_to_string(old_state),
+                          scope_state_to_string(state));
+
+        unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
+}
+
+static int scope_add_default_dependencies(Scope *s) {
+        int r;
+
+        assert(s);
+
+        /* Make sure scopes are unloaded on shutdown */
+        r = unit_add_two_dependencies_by_name(
+                        UNIT(s),
+                        UNIT_BEFORE, UNIT_CONFLICTS,
+                        SPECIAL_SHUTDOWN_TARGET, NULL, true);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+static int scope_verify(Scope *s) {
+        assert(s);
+
+        if (UNIT(s)->load_state != UNIT_LOADED)
+                return 0;
+
+        if (set_size(s->pids) <= 0) {
+                log_error_unit(UNIT(s)->id, "Scope %s has no PIDs. Refusing.", UNIT(s)->id);
+                return -EINVAL;
+        }
+
+        return 0;
+}
+
+static int scope_load(Unit *u) {
+        Scope *s = SCOPE(u);
+        int r;
+
+        assert(s);
+        assert(u->load_state == UNIT_STUB);
+
+        if (!u->transient && UNIT(s)->manager->n_reloading <= 0)
+                return -ENOENT;
+
+        u->load_state = UNIT_LOADED;
+
+        r = unit_load_dropin(u);
+        if (r < 0)
+                return r;
+
+        r = unit_add_default_slice(u);
+        if (r < 0)
+                return r;
+
+        if (u->default_dependencies) {
+                r = scope_add_default_dependencies(s);
+                if (r < 0)
+                        return r;
+        }
+
+        return scope_verify(s);
+}
+
+static int scope_coldplug(Unit *u) {
+        Scope *s = SCOPE(u);
+        int r;
+
+        assert(s);
+        assert(s->state == SCOPE_DEAD);
+
+        if (s->deserialized_state != s->state) {
+
+                if ((s->deserialized_state == SCOPE_STOP_SIGKILL || s->deserialized_state == SCOPE_STOP_SIGTERM)
+                    && s->timeout_stop_usec > 0) {
+                        r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
+                        if (r < 0)
+
+                                return r;
+                }
+
+                scope_set_state(s, s->deserialized_state);
+        }
+
+        return 0;
+}
+
+static void scope_dump(Unit *u, FILE *f, const char *prefix) {
+        Scope *s = SCOPE(u);
+
+        assert(s);
+        assert(f);
+
+        fprintf(f,
+                "%sScope State: %s\n"
+                "%sResult: %s\n",
+                prefix, scope_state_to_string(s->state),
+                prefix, scope_result_to_string(s->result));
+
+        cgroup_context_dump(&s->cgroup_context, f, prefix);
+        kill_context_dump(&s->kill_context, f, prefix);
+}
+
+static void scope_enter_dead(Scope *s, ScopeResult f) {
+        assert(s);
+
+        if (f != SCOPE_SUCCESS)
+                s->result = f;
+
+        scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
+}
+
+static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
+        int r;
+
+        assert(s);
+
+        if (f != SCOPE_SUCCESS)
+                s->result = f;
+
+        r = unit_kill_context(
+                        UNIT(s),
+                        &s->kill_context,
+                        state != SCOPE_STOP_SIGTERM,
+                        -1, -1, false);
+        if (r < 0)
+                goto fail;
+
+        if (r > 0) {
+                if (s->timeout_stop_usec > 0) {
+                        r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
+                        if (r < 0)
+                                goto fail;
+                }
+
+                scope_set_state(s, state);
+        } else
+                scope_enter_dead(s, SCOPE_SUCCESS);
+
+        return;
+
+fail:
+        log_warning_unit(UNIT(s)->id,
+                         "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
+
+        scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
+}
+
+static int scope_start(Unit *u) {
+        Scope *s = SCOPE(u);
+        int r;
+
+        assert(s);
+
+        if (s->state == SCOPE_STOP_SIGTERM ||
+            s->state == SCOPE_STOP_SIGKILL)
+                return -EAGAIN;
+
+        assert(s->state == SCOPE_DEAD);
+
+        if (!u->transient && UNIT(s)->manager->n_reloading <= 0)
+                return -ENOENT;
+
+        r = unit_realize_cgroup(u);
+        if (r < 0) {
+                log_error("Failed to realize cgroup: %s", strerror(-r));
+                return r;
+        }
+
+        r = cg_attach_many_with_mask(u->cgroup_mask, u->cgroup_path, s->pids);
+        if (r < 0)
+                return r;
+
+        set_free(s->pids);
+        s->pids = NULL;
+
+        s->result = SCOPE_SUCCESS;
+
+        scope_set_state(s, SCOPE_RUNNING);
+        return 0;
+}
+
+static int scope_stop(Unit *u) {
+        Scope *s = SCOPE(u);
+
+        assert(s);
+        assert(s->state == SCOPE_RUNNING);
+
+        if (s->state == SCOPE_STOP_SIGTERM ||
+            s->state == SCOPE_STOP_SIGKILL)
+                return 0;
+
+        assert(s->state == SCOPE_RUNNING);
+
+        scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
+        return 0;
+}
+
+static int scope_kill(Unit *u, KillWho who, int signo, DBusError *error) {
+        return unit_kill_common(u, who, signo, -1, -1, error);
+}
+
+static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
+        Scope *s = SCOPE(u);
+
+        assert(s);
+        assert(f);
+        assert(fds);
+
+        unit_serialize_item(u, f, "state", scope_state_to_string(s->state));
+        return 0;
+}
+
+static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
+        Scope *s = SCOPE(u);
+
+        assert(u);
+        assert(key);
+        assert(value);
+        assert(fds);
+
+        if (streq(key, "state")) {
+                ScopeState state;
+
+                state = scope_state_from_string(value);
+                if (state < 0)
+                        log_debug("Failed to parse state value %s", value);
+                else
+                        s->deserialized_state = state;
+
+        } else
+                log_debug("Unknown serialization key '%s'", key);
+
+        return 0;
+}
+
+static bool scope_check_gc(Unit *u) {
+        Scope *s = SCOPE(u);
+        int r;
+
+        assert(s);
+
+        /* Never clean up scopes that still have a process around,
+         * even if the scope is formally dead. */
+
+        if (UNIT(s)->cgroup_path) {
+                r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
+                if (r <= 0)
+                        return true;
+        }
+
+        return false;
+}
+
+static void scope_timer_event(Unit *u, uint64_t elapsed, Watch*w) {
+        Scope *s = SCOPE(u);
+
+        assert(s);
+        assert(elapsed == 1);
+        assert(w == &s->timer_watch);
+
+        switch (s->state) {
+
+        case SCOPE_STOP_SIGTERM:
+                if (s->kill_context.send_sigkill) {
+                        log_warning_unit(u->id, "%s stopping timed out. Killing.", u->id);
+                        scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
+                } else {
+                        log_warning_unit(u->id, "%s stopping timed out. Skipping SIGKILL.", u->id);
+                        scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
+                }
+
+                break;
+
+        case SCOPE_STOP_SIGKILL:
+                log_warning_unit(u->id, "%s still around after SIGKILL. Ignoring.", u->id);
+                scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
+                break;
+
+        default:
+                assert_not_reached("Timeout at wrong time.");
+        }
+}
+
+static void scope_notify_cgroup_empty_event(Unit *u) {
+        Scope *s = SCOPE(u);
+        assert(u);
+
+        log_debug_unit(u->id, "%s: cgroup is empty", u->id);
+
+        switch (s->state) {
+
+        case SCOPE_RUNNING:
+        case SCOPE_STOP_SIGTERM:
+        case SCOPE_STOP_SIGKILL:
+                scope_enter_dead(s, SCOPE_SUCCESS);
+
+                break;
+
+        default:
+                ;
+        }
+}
+
+_pure_ static UnitActiveState scope_active_state(Unit *u) {
+        assert(u);
+
+        return state_translation_table[SCOPE(u)->state];
+}
+
+_pure_ static const char *scope_sub_state_to_string(Unit *u) {
+        assert(u);
+
+        return scope_state_to_string(SCOPE(u)->state);
+}
+
+static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
+        [SCOPE_DEAD] = "dead",
+        [SCOPE_RUNNING] = "active",
+        [SCOPE_STOP_SIGTERM] = "stop-sigterm",
+        [SCOPE_STOP_SIGKILL] = "stop-sigkill",
+        [SCOPE_FAILED] = "failed",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(scope_state, ScopeState);
+
+static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
+        [SCOPE_SUCCESS] = "success",
+        [SCOPE_FAILURE_RESOURCES] = "resources",
+        [SCOPE_FAILURE_TIMEOUT] = "timeout",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
+
+const UnitVTable scope_vtable = {
+        .object_size = sizeof(Scope),
+        .sections =
+                "Unit\0"
+                "Scope\0"
+                "Install\0",
+
+        .private_section = "Scope",
+        .cgroup_context_offset = offsetof(Scope, cgroup_context),
+
+        .no_alias = true,
+        .no_instances = true,
+
+        .init = scope_init,
+        .load = scope_load,
+        .done = scope_done,
+
+        .coldplug = scope_coldplug,
+
+        .dump = scope_dump,
+
+        .start = scope_start,
+        .stop = scope_stop,
+
+        .kill = scope_kill,
+
+        .serialize = scope_serialize,
+        .deserialize_item = scope_deserialize_item,
+
+        .active_state = scope_active_state,
+        .sub_state_to_string = scope_sub_state_to_string,
+
+        .check_gc = scope_check_gc,
+
+        .timer_event = scope_timer_event,
+
+        .notify_cgroup_empty = scope_notify_cgroup_empty_event,
+
+        .bus_interface = "org.freedesktop.systemd1.Scope",
+        .bus_message_handler = bus_scope_message_handler,
+        .bus_set_property = bus_scope_set_property,
+        .bus_commit_properties = bus_scope_commit_properties,
+
+        .can_transient = true
+};
diff --git a/src/core/scope.h b/src/core/scope.h
new file mode 100644
index 0000000..2a3dcb7
--- /dev/null
+++ b/src/core/scope.h
@@ -0,0 +1,69 @@
+/*-*- 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 Scope Scope;
+
+#include "unit.h"
+#include "kill.h"
+
+typedef enum ScopeState {
+        SCOPE_DEAD,
+        SCOPE_RUNNING,
+        SCOPE_STOP_SIGTERM,
+        SCOPE_STOP_SIGKILL,
+        SCOPE_FAILED,
+        _SCOPE_STATE_MAX,
+        _SCOPE_STATE_INVALID = -1
+} ScopeState;
+
+typedef enum ScopeResult {
+        SCOPE_SUCCESS,
+        SCOPE_FAILURE_RESOURCES,
+        SCOPE_FAILURE_TIMEOUT,
+        _SCOPE_RESULT_MAX,
+        _SCOPE_RESULT_INVALID = -1
+} ScopeResult;
+
+struct Scope {
+        Unit meta;
+
+        CGroupContext cgroup_context;
+        KillContext kill_context;
+
+        ScopeState state, deserialized_state;
+        ScopeResult result;
+
+        usec_t timeout_stop_usec;
+
+        Set *pids;
+
+        Watch timer_watch;
+};
+
+extern const UnitVTable scope_vtable;
+
+const char* scope_state_to_string(ScopeState i) _const_;
+ScopeState scope_state_from_string(const char *s) _pure_;
+
+const char* scope_result_to_string(ScopeResult i) _const_;
+ScopeResult scope_result_from_string(const char *s) _pure_;
diff --git a/src/core/service.c b/src/core/service.c
index 6f18cbf..2bc0dc5 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -1617,6 +1617,7 @@ static int service_coldplug(Unit *u) {
                     s->deserialized_state == SERVICE_FINAL_SIGTERM ||
                     s->deserialized_state == SERVICE_FINAL_SIGKILL ||
                     s->deserialized_state == SERVICE_AUTO_RESTART) {
+
                         if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
                                 usec_t k;
 
@@ -2144,7 +2145,6 @@ static void service_kill_control_processes(Service *s) {
                 return;
 
         p = strappenda(UNIT(s)->cgroup_path, "/control");
-
         cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
 }
 
@@ -3322,8 +3322,7 @@ static void service_notify_cgroup_empty_event(Unit *u) {
 
         assert(u);
 
-        log_debug_unit(u->id,
-                       "%s: cgroup is empty", u->id);
+        log_debug_unit(u->id, "%s: cgroup is empty", u->id);
 
         switch (s->state) {
 
diff --git a/src/core/slice.c b/src/core/slice.c
index 557f829..40d416e 100644
--- a/src/core/slice.c
+++ b/src/core/slice.c
@@ -103,7 +103,10 @@ static int slice_add_default_dependencies(Slice *s) {
         assert(s);
 
         /* Make sure slices are unloaded on shutdown */
-        r = unit_add_dependency_by_name(UNIT(s), UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
+        r = unit_add_two_dependencies_by_name(
+                        UNIT(s),
+                        UNIT_BEFORE, UNIT_CONFLICTS,
+                        SPECIAL_SHUTDOWN_TARGET, NULL, true);
         if (r < 0)
                 return r;
 
diff --git a/src/core/unit.c b/src/core/unit.c
index c6c9c18..991111a 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -60,7 +60,8 @@ const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
         [UNIT_SNAPSHOT] = &snapshot_vtable,
         [UNIT_SWAP] = &swap_vtable,
         [UNIT_PATH] = &path_vtable,
-        [UNIT_SLICE] = &slice_vtable
+        [UNIT_SLICE] = &slice_vtable,
+        [UNIT_SCOPE] = &scope_vtable
 };
 
 Unit *unit_new(Manager *m, size_t size) {
diff --git a/src/core/unit.h b/src/core/unit.h
index 8a62787..ed4df18 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -280,6 +280,7 @@ typedef enum UnitSetPropertiesMode {
 #include "swap.h"
 #include "path.h"
 #include "slice.h"
+#include "scope.h"
 
 struct UnitVTable {
         /* How much memory does an object of this unit type need */
@@ -462,6 +463,7 @@ DEFINE_CAST(SNAPSHOT, Snapshot);
 DEFINE_CAST(SWAP, Swap);
 DEFINE_CAST(PATH, Path);
 DEFINE_CAST(SLICE, Slice);
+DEFINE_CAST(SCOPE, Scope);
 
 Unit *unit_new(Manager *m, size_t size);
 void unit_free(Unit *u);
diff --git a/src/notify/notify.c b/src/notify/notify.c
index 1e9766f..a688a9f 100644
--- a/src/notify/notify.c
+++ b/src/notify/notify.c
@@ -157,7 +157,8 @@ int main(int argc, char* argv[]) {
         log_parse_environment();
         log_open();
 
-        if ((r = parse_argv(argc, argv)) <= 0) {
+        r = parse_argv(argc, argv);
+        if (r <= 0) {
                 retval = r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
                 goto finish;
         }
diff --git a/src/run/run.c b/src/run/run.c
index 9f8bda4..b451168 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -20,22 +20,104 @@
 ***/
 
 #include <stdio.h>
+#include <getopt.h>
 
 #include "sd-bus.h"
 #include "bus-internal.h"
 #include "bus-message.h"
 #include "strv.h"
+#include "build.h"
+#include "unit-name.h"
 
-static int start_transient_service(
-                sd_bus *bus,
-                const char *name,
-                char **argv,
-                sd_bus_error *error) {
+static bool arg_scope = false;
+static bool arg_user = false;
+static const char *arg_unit = NULL;
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
-        char **i;
+static int help(void) {
+
+        printf("%s [OPTIONS...] [COMMAND LINE...]\n\n"
+               "Notify the init system about service status updates.\n\n"
+               "  -h --help             Show this help\n"
+               "     --version          Show package version\n"
+               "     --user             Run as user unit\n"
+               "     --scope            Run this as scope rather than service\n"
+               "     --unit=UNIT        Run under the specified unit name\n",
+               program_invocation_short_name);
+
+        return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+        enum {
+                ARG_VERSION = 0x100,
+                ARG_USER,
+                ARG_SCOPE,
+                ARG_UNIT
+        };
+
+        static const struct option options[] = {
+                { "help",      no_argument,       NULL, 'h'           },
+                { "version",   no_argument,       NULL, ARG_VERSION   },
+                { "user",      no_argument,       NULL, ARG_USER      },
+                { "scope",     no_argument,       NULL, ARG_SCOPE     },
+                { "unit",      required_argument, NULL, ARG_UNIT      },
+                { NULL,        0,                 NULL, 0             }
+        };
+
+        int c;
+
+        assert(argc >= 0);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0) {
+
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                case ARG_VERSION:
+                        puts(PACKAGE_STRING);
+                        puts(SYSTEMD_FEATURES);
+                        return 0;
+
+                case ARG_USER:
+                        arg_user = true;
+                        break;
+
+                case ARG_SCOPE:
+                        arg_scope = true;
+                        break;
+
+                case ARG_UNIT:
+                        arg_unit = optarg;
+                        break;
+
+                case '?':
+                        return -EINVAL;
+
+                default:
+                        log_error("Unknown option code %c", c);
+                        return -EINVAL;
+                }
+        }
+
+        if (optind >= argc) {
+                log_error("Command line to execute required.");
+                return -EINVAL;
+        }
+
+        return 1;
+}
+
+static int message_start_transient_unit_new(sd_bus *bus, const char *name, sd_bus_message **ret) {
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r;
 
+        log_info("Running as unit %s.", name);
+
         r = sd_bus_message_new_method_call(
                         bus,
                         "org.freedesktop.systemd1",
@@ -57,6 +139,47 @@ static int start_transient_service(
         if (r < 0)
                 return r;
 
+        *ret = m;
+        m = NULL;
+
+        return 0;
+}
+
+static int message_start_transient_unit_send(sd_bus *bus, sd_bus_message *m, sd_bus_error *error, sd_bus_message **reply) {
+        int r;
+
+        r = sd_bus_message_close_container(m);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_close_container(m);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
+}
+
+static int start_transient_service(
+                sd_bus *bus,
+                char **argv,
+                sd_bus_error *error) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+        _cleanup_free_ char *name = NULL;
+        char **i;
+        int r;
+
+        if (arg_unit)
+                name = unit_name_mangle_with_suffix(arg_unit, ".service");
+        else
+                asprintf(&name, "run-%lu.service", (unsigned long) getpid());
+        if (!name)
+                return -ENOMEM;
+
+        r = message_start_transient_unit_new(bus, name, &m);
+        if (r < 0)
+                return r;
+
         r = sd_bus_message_append(m, "s", "ExecStart");
         if (r < 0)
                 return r;
@@ -107,46 +230,69 @@ static int start_transient_service(
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_close_container(m);
+        return  message_start_transient_unit_send(bus, m, error, &reply);
+}
+
+static int start_transient_scope(
+                sd_bus *bus,
+                char **argv,
+                sd_bus_error *error) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+        _cleanup_free_ char *name = NULL;
+        int r;
+
+        if (arg_unit)
+                name = unit_name_mangle_with_suffix(arg_unit, ".scope");
+        else
+                asprintf(&name, "run-%lu.scope", (unsigned long) getpid());
+        if (!name)
+                return -ENOMEM;
+
+        r = message_start_transient_unit_new(bus, name, &m);
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_close_container(m);
+        r = sd_bus_message_append(m, "sv", "PIDs", "au", 1, (uint32_t) getpid());
+        if (r < 0)
+                return r;
+
+        r = message_start_transient_unit_send(bus, m, error, &reply);
         if (r < 0)
                 return r;
 
-        return sd_bus_send_with_reply_and_block(bus, m, 0, error, &reply);
+        execvp(argv[0], argv);
+        log_error("Failed to execute: %m");
+        return -errno;
 }
 
 int main(int argc, char* argv[]) {
         sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_bus_unref_ sd_bus *bus = NULL;
-        _cleanup_free_ char *name = NULL;
         int r;
 
         log_parse_environment();
         log_open();
 
-        if (argc < 2) {
-                log_error("Missing command line.");
-                r = -EINVAL;
+        r = parse_argv(argc, argv);
+        if (r <= 0)
                 goto fail;
-        }
 
-        r = sd_bus_open_user(&bus);
+        if (arg_user)
+                r = sd_bus_open_user(&bus);
+        else
+                r = sd_bus_open_system(&bus);
         if (r < 0) {
-                log_error("Failed to create new bus: %s", strerror(-r));
-                goto fail;
-        }
-
-        if (asprintf(&name, "run-%lu.service", (unsigned long) getpid()) < 0) {
-                r = log_oom();
+                log_error("Failed to create new bus connection: %s", strerror(-r));
                 goto fail;
         }
 
-        r = start_transient_service(bus, name, argv + 1, &error);
+        if (arg_scope)
+                r = start_transient_scope(bus, argv + optind, &error);
+        else
+                r = start_transient_service(bus, argv + optind, &error);
         if (r < 0) {
-                log_error("Failed start transient service: %s", error.message);
+                log_error("Failed start transient unit: %s", error.message);
                 sd_bus_error_free(&error);
                 goto fail;
         }
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index 5816b7d..0e5da23 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -1679,6 +1679,23 @@ int cg_attach_with_mask(CGroupControllerMask mask, const char *path, pid_t pid)
         return r;
 }
 
+int cg_attach_many_with_mask(CGroupControllerMask mask, const char *path, Set* pids) {
+        Iterator i;
+        void *pidp;
+        int r = 0;
+
+        SET_FOREACH(pidp, pids, i) {
+                pid_t pid = PTR_TO_LONG(pidp);
+                int k;
+
+                k = cg_attach_with_mask(mask, path, pid);
+                if (k < 0)
+                        r = k;
+        }
+
+        return r;
+}
+
 int cg_migrate_with_mask(CGroupControllerMask mask, const char *from, const char *to) {
         CGroupControllerMask bit = 1;
         const char *n;
diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h
index 9883d94..c781aab 100644
--- a/src/shared/cgroup-util.h
+++ b/src/shared/cgroup-util.h
@@ -127,6 +127,7 @@ int cg_slice_to_path(const char *unit, char **ret);
 
 int cg_create_with_mask(CGroupControllerMask mask, const char *path);
 int cg_attach_with_mask(CGroupControllerMask mask, const char *path, pid_t pid);
+int cg_attach_many_with_mask(CGroupControllerMask mask, const char *path, Set* pids);
 int cg_migrate_with_mask(CGroupControllerMask mask, const char *from, const char *to);
 int cg_trim_with_mask(CGroupControllerMask mask, const char *path, bool delete_root);
 
diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c
index bf1ab79..f2c30a6 100644
--- a/src/shared/unit-name.c
+++ b/src/shared/unit-name.c
@@ -44,7 +44,8 @@ static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
         [UNIT_TIMER] = "timer",
         [UNIT_SWAP] = "swap",
         [UNIT_PATH] = "path",
-        [UNIT_SLICE] = "slice"
+        [UNIT_SLICE] = "slice",
+        [UNIT_SCOPE] = "scope"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
diff --git a/src/shared/unit-name.h b/src/shared/unit-name.h
index 922d752..88f2b83 100644
--- a/src/shared/unit-name.h
+++ b/src/shared/unit-name.h
@@ -42,6 +42,7 @@ enum UnitType {
         UNIT_SWAP,
         UNIT_PATH,
         UNIT_SLICE,
+        UNIT_SCOPE,
         _UNIT_TYPE_MAX,
         _UNIT_TYPE_INVALID = -1
 };

commit a00963a2e4e98c0e4ef477b63b70c5e71d65fdc1
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Jul 1 00:02:41 2013 +0200

    systemctl: show per-unit cgroup tree correctly following the new property

diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index ef1a079..fde3f43 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -435,8 +435,12 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
         }
 }
 
-static int get_unit_list(DBusConnection *bus, DBusMessage **reply,
-                         struct unit_info **unit_infos, unsigned *c) {
+static int get_unit_list(
+                DBusConnection *bus,
+                DBusMessage **reply,
+                struct unit_info **unit_infos,
+                unsigned *c) {
+
         DBusMessageIter iter, sub;
         size_t size = 0;
         int r;
@@ -498,9 +502,11 @@ static int list_units(DBusConnection *bus, char **args) {
         return 0;
 }
 
-static int get_triggered_units(DBusConnection *bus, const char* unit_path,
-                               char*** triggered)
-{
+static int get_triggered_units(
+                DBusConnection *bus,
+                const char* unit_path,
+                char*** triggered) {
+
         const char *interface = "org.freedesktop.systemd1.Unit",
                    *triggers_property = "Triggers";
         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
@@ -2779,7 +2785,7 @@ static void print_status_info(UnitStatusInfo *i) {
                 printf("   Status: \"%s\"\n", i->status_text);
 
         if (i->control_group &&
-            (i->main_pid > 0 || i->control_pid > 0 || cg_is_empty_by_spec(i->control_group, false) == 0)) {
+            (i->main_pid > 0 || i->control_pid > 0 || cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0)) {
                 unsigned c;
 
                 printf("   CGroup: %s\n", i->control_group);
@@ -2801,7 +2807,7 @@ static void print_status_info(UnitStatusInfo *i) {
                         if (i->control_pid > 0)
                                 extra[k++] = i->control_pid;
 
-                        show_cgroup_and_extra_by_spec(i->control_group, prefix,
+                        show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix,
                                                       c, false, extra, k, flags);
                 }
         }
@@ -2911,8 +2917,12 @@ static int status_property(const char *name, DBusMessageIter *iter, UnitStatusIn
                         else if (streq(name, "SourcePath"))
                                 i->source_path = s;
 #ifndef LEGACY
-                        else if (streq(name, "DefaultControlGroup"))
-                                i->control_group = s;
+                        else if (streq(name, "DefaultControlGroup")) {
+                                const char *e;
+                                e = startswith(s, SYSTEMD_CGROUP_CONTROLLER ":");
+                                if (e)
+                                        i->control_group = e;
+                        }
 #endif
                         else if (streq(name, "ControlGroup"))
                                 i->control_group = s;
@@ -3259,30 +3269,6 @@ static int print_property(const char *name, DBusMessageIter *iter) {
 
                         return 0;
 
-                } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
-                        DBusMessageIter sub, sub2;
-
-                        dbus_message_iter_recurse(iter, &sub);
-                        while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
-                                const char *controller, *attr, *value;
-
-                                dbus_message_iter_recurse(&sub, &sub2);
-
-                                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
-                                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
-                                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
-
-                                        printf("ControlGroupAttributes={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
-                                               controller,
-                                               attr,
-                                               value);
-                                }
-
-                                dbus_message_iter_next(&sub);
-                        }
-
-                        return 0;
-
                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
                         DBusMessageIter sub;
 

commit a190eeb884840e1a431e22eee2c3b93c96ca7fa3
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Jul 1 00:02:03 2013 +0200

    snapshot: snapshots are just a special kind of transient units now

diff --git a/src/core/snapshot.c b/src/core/snapshot.c
index a63eccd..1423854 100644
--- a/src/core/snapshot.c
+++ b/src/core/snapshot.c
@@ -40,6 +40,7 @@ static void snapshot_init(Unit *u) {
 
         UNIT(s)->ignore_on_isolate = true;
         UNIT(s)->ignore_on_snapshot = true;
+        UNIT(s)->allow_isolate = true;
 }
 
 static void snapshot_set_state(Snapshot *s, SnapshotState state) {
@@ -66,7 +67,7 @@ static int snapshot_load(Unit *u) {
 
         /* Make sure that only snapshots created via snapshot_create()
          * can be loaded */
-        if (!s->by_snapshot_create && UNIT(s)->manager->n_reloading <= 0)
+        if (!u->transient && UNIT(s)->manager->n_reloading <= 0)
                 return -ENOENT;
 
         u->load_state = UNIT_LOADED;
@@ -151,21 +152,24 @@ static int snapshot_deserialize_item(Unit *u, const char *key, const char *value
         if (streq(key, "state")) {
                 SnapshotState state;
 
-                if ((state = snapshot_state_from_string(value)) < 0)
+                state = snapshot_state_from_string(value);
+                if (state < 0)
                         log_debug("Failed to parse state value %s", value);
                 else
                         s->deserialized_state = state;
 
         } else if (streq(key, "cleanup")) {
 
-                if ((r = parse_boolean(value)) < 0)
+                r = parse_boolean(value);
+                if (r < 0)
                         log_debug("Failed to parse cleanup value %s", value);
                 else
                         s->cleanup = r;
 
         } else if (streq(key, "wants")) {
 
-                if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, value, NULL, true)) < 0)
+                r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, value, NULL, true);
+                if (r < 0)
                         return r;
         } else
                 log_debug("Unknown serialization key '%s'", key);
@@ -186,9 +190,9 @@ _pure_ static const char *snapshot_sub_state_to_string(Unit *u) {
 }
 
 int snapshot_create(Manager *m, const char *name, bool cleanup, DBusError *e, Snapshot **_s) {
-        Iterator i;
+        _cleanup_free_ char *n = NULL;
         Unit *other, *u = NULL;
-        char *n = NULL;
+        Iterator i;
         int r;
         const char *k;
 
@@ -221,24 +225,22 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, DBusError *e, Sn
                                 break;
 
                         free(n);
+                        n = NULL;
                 }
-
-                name = n;
         }
 
         r = manager_load_unit_prepare(m, name, NULL, e, &u);
-        free(n);
-
         if (r < 0)
                 goto fail;
 
-        SNAPSHOT(u)->by_snapshot_create = true;
+        u->transient = true;
         manager_dispatch_load_queue(m);
         assert(u->load_state == UNIT_LOADED);
 
         HASHMAP_FOREACH_KEY(other, k, m->units, i) {
 
-                if (other->ignore_on_snapshot)
+                if (other->ignore_on_snapshot ||
+                    other->transient)
                         continue;
 
                 if (k != other->id)
@@ -251,12 +253,12 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, DBusError *e, Sn
                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                         continue;
 
-                if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_WANTS, other, true)) < 0)
+                r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_WANTS, other, true);
+                if (r < 0)
                         goto fail;
         }
 
         SNAPSHOT(u)->cleanup = cleanup;
-        u->allow_isolate = true;
         *_s = SNAPSHOT(u);
 
         return 0;
diff --git a/src/core/snapshot.h b/src/core/snapshot.h
index 56f87cf..2675b1b 100644
--- a/src/core/snapshot.h
+++ b/src/core/snapshot.h
@@ -38,7 +38,6 @@ struct Snapshot {
         SnapshotState state, deserialized_state;
 
         bool cleanup;
-        bool by_snapshot_create:1;
 };
 
 extern const UnitVTable snapshot_vtable;

commit 6a95dff87daf899a96135c7ab87aa1b1a67f6afc
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Jul 1 00:01:29 2013 +0200

    dbus: expose cgroup properties in introspection everywhere

diff --git a/src/core/dbus-cgroup.h b/src/core/dbus-cgroup.h
index c5908dd..4ce1e7e 100644
--- a/src/core/dbus-cgroup.h
+++ b/src/core/dbus-cgroup.h
@@ -27,7 +27,7 @@
 #include "dbus-common.h"
 #include "cgroup.h"
 
-#define BUS_CGROUP_CONTEXT_INTERFACE \
+#define BUS_CGROUP_CONTEXT_INTERFACE                                    \
         "  <property name=\"CPUAccounting\" type=\"b\" access=\"read\"/>\n" \
         "  <property name=\"CPUShares\" type=\"t\" access=\"read\"/>\n" \
         "  <property name=\"BlockIOAccounting\" type=\"b\" access=\"read\"/>\n" \
diff --git a/src/core/dbus-mount.c b/src/core/dbus-mount.c
index ae04ab8..ef55fcb 100644
--- a/src/core/dbus-mount.c
+++ b/src/core/dbus-mount.c
@@ -41,6 +41,7 @@
         BUS_EXEC_COMMAND_INTERFACE("ExecRemount")                       \
         BUS_EXEC_CONTEXT_INTERFACE                                      \
         BUS_KILL_CONTEXT_INTERFACE                                      \
+        BUS_CGROUP_CONTEXT_INTERFACE                                    \
         "  <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
         "  <property name=\"DirectoryMode\" type=\"u\" access=\"read\"/>\n" \
         "  <property name=\"Result\" type=\"s\" access=\"read\"/>\n"    \
diff --git a/src/core/dbus-service.c b/src/core/dbus-service.c
index e93ac55..e5e95a1 100644
--- a/src/core/dbus-service.c
+++ b/src/core/dbus-service.c
@@ -54,6 +54,7 @@
         BUS_EXEC_COMMAND_INTERFACE("ExecStopPost")                      \
         BUS_EXEC_CONTEXT_INTERFACE                                      \
         BUS_KILL_CONTEXT_INTERFACE                                      \
+        BUS_CGROUP_CONTEXT_INTERFACE                                    \
         "  <property name=\"PermissionsStartOnly\" type=\"b\" access=\"read\"/>\n" \
         "  <property name=\"RootDirectoryStartOnly\" type=\"b\" access=\"read\"/>\n" \
         "  <property name=\"RemainAfterExit\" type=\"b\" access=\"read\"/>\n" \
diff --git a/src/core/dbus-slice.c b/src/core/dbus-slice.c
index db356ad..3b67779 100644
--- a/src/core/dbus-slice.c
+++ b/src/core/dbus-slice.c
@@ -29,6 +29,7 @@
 
 #define BUS_SLICE_INTERFACE                                             \
         " <interface name=\"org.freedesktop.systemd1.Slice\">\n"        \
+        BUS_CGROUP_CONTEXT_INTERFACE                                    \
         " </interface>\n"
 
 #define INTROSPECTION                                                   \
diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c
index 7ef55ec..a431fa1 100644
--- a/src/core/dbus-socket.c
+++ b/src/core/dbus-socket.c
@@ -40,6 +40,7 @@
         BUS_EXEC_COMMAND_INTERFACE("ExecStopPost")                      \
         BUS_EXEC_CONTEXT_INTERFACE                                      \
         BUS_KILL_CONTEXT_INTERFACE                                      \
+        BUS_CGROUP_CONTEXT_INTERFACE                                    \
         "  <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
         "  <property name=\"BindToDevice\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"DirectoryMode\" type=\"u\" access=\"read\"/>\n" \
diff --git a/src/core/dbus-swap.c b/src/core/dbus-swap.c
index 0248b4b..d854e0f 100644
--- a/src/core/dbus-swap.c
+++ b/src/core/dbus-swap.c
@@ -39,6 +39,7 @@
         BUS_EXEC_COMMAND_INTERFACE("ExecDeactivate")                    \
         BUS_EXEC_CONTEXT_INTERFACE                                      \
         BUS_KILL_CONTEXT_INTERFACE                                      \
+        BUS_CGROUP_CONTEXT_INTERFACE                                    \
         "  <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
         "  <property name=\"Result\" type=\"s\" access=\"read\"/>\n"    \
         " </interface>\n"

commit 64fad95a324fde85aeb2dd6f3291a55374ee0113
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun Jun 30 23:56:59 2013 +0200

    service: correct service bus introspection for timeouts

diff --git a/src/core/dbus-service.c b/src/core/dbus-service.c
index c2031c3..e93ac55 100644
--- a/src/core/dbus-service.c
+++ b/src/core/dbus-service.c
@@ -38,7 +38,8 @@
         "  <property name=\"PIDFile\" type=\"s\" access=\"read\"/>\n"   \
         "  <property name=\"NotifyAccess\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"RestartUSec\" type=\"t\" access=\"read\"/>\n" \
-        "  <property name=\"TimeoutUSec\" type=\"t\" access=\"read\"/>\n" \
+        "  <property name=\"TimeoutStartUSec\" type=\"t\" access=\"read\"/>\n" \
+        "  <property name=\"TimeoutStopUSec\" type=\"t\" access=\"read\"/>\n" \
         "  <property name=\"WatchdogUSec\" type=\"t\" access=\"read\"/>\n" \
         "  <property name=\"WatchdogTimestamp\" type=\"t\" access=\"read\"/>\n" \
         "  <property name=\"WatchdogTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
@@ -110,7 +111,7 @@ static const BusProperty bus_exec_main_status_properties[] = {
         { "ExecMainPID",                    bus_property_append_pid,  "u", offsetof(ExecStatus, pid)                       },
         { "ExecMainCode",                   bus_property_append_int,  "i", offsetof(ExecStatus, code)                      },
         { "ExecMainStatus",                 bus_property_append_int,  "i", offsetof(ExecStatus, status)                    },
-        { NULL, }
+        {}
 };
 
 static const BusProperty bus_service_properties[] = {
@@ -119,7 +120,6 @@ static const BusProperty bus_service_properties[] = {
         { "PIDFile",                bus_property_append_string,       "s", offsetof(Service, pid_file),               true },
         { "NotifyAccess",           bus_service_append_notify_access, "s", offsetof(Service, notify_access)                },
         { "RestartUSec",            bus_property_append_usec,         "t", offsetof(Service, restart_usec)                 },
-        { "TimeoutUSec",            bus_property_append_usec,         "t", offsetof(Service, timeout_start_usec)           },
         { "TimeoutStartUSec",       bus_property_append_usec,         "t", offsetof(Service, timeout_start_usec)           },
         { "TimeoutStopUSec",        bus_property_append_usec,         "t", offsetof(Service, timeout_stop_usec)            },
         { "WatchdogUSec",           bus_property_append_usec,         "t", offsetof(Service, watchdog_usec)                },
@@ -143,7 +143,7 @@ static const BusProperty bus_service_properties[] = {
         { "BusName",                bus_property_append_string,       "s", offsetof(Service, bus_name),               true },
         { "StatusText",             bus_property_append_string,       "s", offsetof(Service, status_text),            true },
         { "Result",                 bus_service_append_service_result,"s", offsetof(Service, result)                       },
-        { NULL, }
+        {}
 };
 
 DBusHandlerResult bus_service_message_handler(Unit *u, DBusConnection *connection, DBusMessage *message) {
@@ -156,7 +156,7 @@ DBusHandlerResult bus_service_message_handler(Unit *u, DBusConnection *connectio
                 { "org.freedesktop.systemd1.Service", bus_kill_context_properties,     &s->kill_context },
                 { "org.freedesktop.systemd1.Service", bus_cgroup_context_properties,   &s->cgroup_context },
                 { "org.freedesktop.systemd1.Service", bus_exec_main_status_properties, &s->main_exec_status },
-                { NULL, }
+                {}
         };
 
         SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "status");

commit b56c28c31adc101df82c1c3c30740b47cbd4f782
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun Jun 30 23:56:11 2013 +0200

    cgroup: implicitly add units to GC queue when their cgroups run empty

diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 1c818ab..cdccf3f 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -733,8 +733,12 @@ int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
                 return 0;
 
         u = manager_get_unit_by_cgroup(m, cgroup);
-        if (u && UNIT_VTABLE(u)->notify_cgroup_empty)
-                UNIT_VTABLE(u)->notify_cgroup_empty(u);
+        if (u) {
+                if (UNIT_VTABLE(u)->notify_cgroup_empty)
+                        UNIT_VTABLE(u)->notify_cgroup_empty(u);
+
+                unit_add_to_gc_queue(u);
+        }
 
         return 0;
 }

commit 0a1eb06d9aacc1f007be04c4133031e0acc91cdd
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun Jun 30 23:55:36 2013 +0200

    cgroup: readd proper cgroup empty tracking

diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 79467a8..1c818ab 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -389,6 +389,10 @@ static int unit_create_cgroups(Unit *u, CGroupControllerMask mask) {
         if (!path)
                 return -ENOMEM;
 
+        r = hashmap_put(u->manager->cgroup_unit, path, u);
+        if (r < 0)
+                return r;
+
         /* First, create our own group */
         r = cg_create_with_mask(mask, path);
         if (r < 0)
@@ -410,7 +414,7 @@ static int unit_create_cgroups(Unit *u, CGroupControllerMask mask) {
         return 0;
 }
 
-static void unit_realize_cgroup_now(Unit *u) {
+static int unit_realize_cgroup_now(Unit *u) {
         CGroupControllerMask mask;
 
         assert(u);
@@ -425,14 +429,14 @@ static void unit_realize_cgroup_now(Unit *u) {
 
         if (u->cgroup_realized &&
             u->cgroup_mask == mask)
-                return;
+                return 0;
 
         /* First, realize parents */
         if (UNIT_ISSET(u->slice))
                 unit_realize_cgroup_now(UNIT_DEREF(u->slice));
 
         /* And then do the real work */
-        unit_create_cgroups(u, mask);
+        return unit_create_cgroups(u, mask);
 }
 
 static void unit_add_to_cgroup_queue(Unit *u) {
@@ -451,8 +455,9 @@ unsigned manager_dispatch_cgroup_queue(Manager *m) {
         while ((i = m->cgroup_queue)) {
                 assert(i->in_cgroup_queue);
 
-                unit_realize_cgroup_now(i);
-                cgroup_context_apply(unit_get_cgroup_context(i), i->cgroup_mask, i->cgroup_path);
+                if (unit_realize_cgroup_now(i) >= 0)
+                        cgroup_context_apply(unit_get_cgroup_context(i), i->cgroup_mask, i->cgroup_path);
+
                 n++;
         }
 
@@ -484,14 +489,15 @@ static void unit_queue_siblings(Unit *u) {
         }
 }
 
-void unit_realize_cgroup(Unit *u) {
+int unit_realize_cgroup(Unit *u) {
         CGroupContext *c;
+        int r;
 
         assert(u);
 
         c = unit_get_cgroup_context(u);
         if (!c)
-                return;
+                return 0;
 
         /* So, here's the deal: when realizing the cgroups for this
          * unit, we need to first create all parents, but there's more
@@ -508,10 +514,13 @@ void unit_realize_cgroup(Unit *u) {
         unit_queue_siblings(u);
 
         /* And realize this one now */
-        unit_realize_cgroup_now(u);
+        r = unit_realize_cgroup_now(u);
 
         /* And apply the values */
-        cgroup_context_apply(c, u->cgroup_mask, u->cgroup_path);
+        if (r >= 0)
+                cgroup_context_apply(c, u->cgroup_mask, u->cgroup_path);
+
+        return r;
 }
 
 void unit_destroy_cgroup(Unit *u) {
@@ -526,10 +535,13 @@ void unit_destroy_cgroup(Unit *u) {
         if (r < 0)
                 log_error("Failed to destroy cgroup %s: %s", u->cgroup_path, strerror(-r));
 
+        hashmap_remove(u->manager->cgroup_unit, u->cgroup_path);
+
         free(u->cgroup_path);
         u->cgroup_path = NULL;
         u->cgroup_realized = false;
         u->cgroup_mask = 0;
+
 }
 
 pid_t unit_search_main_pid(Unit *u) {
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
index 96f1d9f..786bd71 100644
--- a/src/core/cgroup.h
+++ b/src/core/cgroup.h
@@ -97,7 +97,7 @@ void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
 
-void unit_realize_cgroup(Unit *u);
+int unit_realize_cgroup(Unit *u);
 void unit_destroy_cgroup(Unit *u);
 
 int manager_setup_cgroup(Manager *m);



More information about the systemd-commits mailing list