[systemd-commits] 4 commits - fixme Makefile.am src/dbus.c src/dbus.h src/.gitignore src/initctl.c src/load-fragment.c src/set.c src/set.h src/systemctl.c src/systemctl.vala src/unit.c

Lennart Poettering lennart at kemper.freedesktop.org
Mon Jun 14 17:52:17 PDT 2010


 Makefile.am         |   15 
 fixme               |    6 
 src/.gitignore      |    1 
 src/dbus.c          |    9 
 src/dbus.h          |   26 
 src/initctl.c       |    6 
 src/load-fragment.c |    6 
 src/set.c           |    9 
 src/set.h           |    1 
 src/systemctl.c     | 1485 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/systemctl.vala  |  357 ------------
 src/unit.c          |    9 
 12 files changed, 1532 insertions(+), 398 deletions(-)

New commits:
commit 7e4249b94e7f6edb9ffa842c3e790acee298594d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Jun 15 02:51:55 2010 +0200

    systemctl: reimplement systemctl in pure C without Vala/GObject

diff --git a/Makefile.am b/Makefile.am
index 7c0f793..03591be 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -343,6 +343,13 @@ systemd_cgroups_agent_CPPFLAGS = \
 systemd_cgroups_agent_LDADD = \
 	$(DBUS_LIBS)
 
+systemctl_SOURCES = \
+	src/systemctl.c \
+	$(BASIC_SOURCES)
+
+systemctl_CPPFLAGS = $(AM_CPPFLAGS) $(DBUS_CFLAGS)
+systemctl_LDADD = $(DBUS_LIBS)
+
 VALAFLAGS = \
 	-g \
 	--save-temps \
@@ -358,13 +365,6 @@ VALA_CFLAGS = \
 	-Wno-unused-variable \
 	-Wno-unused-function
 
-systemctl_SOURCES = \
-	src/systemctl.vala \
-	src/systemd-interfaces.vala
-
-systemctl_CPPFLAGS = $(AM_CPPFLAGS) $(DBUSGLIB_CFLAGS) $(VALA_CFLAGS)
-systemctl_LDADD = $(DBUSGLIB_LIBS)
-
 systemadm_SOURCES = \
 	src/systemadm.vala \
 	src/systemd-interfaces.vala
@@ -417,7 +417,6 @@ CLEANFILES = \
 if HAVE_VALAC
 CLEANFILES += \
 	src/systemd-interfaces.c \
-	src/systemctl.c \
 	src/systemadm.c
 endif
 
diff --git a/fixme b/fixme
index 514e373..57682f0 100644
--- a/fixme
+++ b/fixme
@@ -54,8 +54,6 @@
    - bluetoothd (/var/run/sdp! @/org/bluez/audio!)
    - distccd
 
-* teach dbus to talk to systemd when autospawning services
-
 * regnerate unit/sysv search paths on daemon reload
 
 * write utmp record a la upstart for processes
@@ -66,6 +64,10 @@
 
 * use setproctitle() when forking, before exec() (waiting for (PR_SET_PROCTITLE_AREA to enter the kernel)
 
+* s/maintainance/maintenance/
+
+* follow property change dbus spec
+
 Regularly:
 
 * look for close() vs. close_nointr() vs. close_nointr_nofail()
diff --git a/src/.gitignore b/src/.gitignore
index 954d7fe..1841bf5 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -1,3 +1,2 @@
-systemctl.c
 systemd-interfaces.c
 systemadm.c
diff --git a/src/systemctl.c b/src/systemctl.c
new file mode 100644
index 0000000..8a7a2de
--- /dev/null
+++ b/src/systemctl.c
@@ -0,0 +1,1485 @@
+/*-*- Mode: C; c-basic-offset: 8 -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 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
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdio.h>
+#include <getopt.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include <dbus/dbus.h>
+
+#include "log.h"
+#include "util.h"
+#include "macro.h"
+#include "set.h"
+
+static const char *arg_type = NULL;
+static bool arg_all = false;
+static bool arg_replace = false;
+static bool arg_session = false;
+static bool arg_block = false;
+
+static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
+
+        if (dbus_message_iter_get_arg_type(iter) != type)
+                return -EIO;
+
+        dbus_message_iter_get_basic(iter, data);
+
+        if (!dbus_message_iter_next(iter) != !next)
+                return -EIO;
+
+        return 0;
+}
+
+static int columns(void) {
+        static int parsed_columns = 0;
+        const char *e;
+
+        if (parsed_columns > 0)
+                return parsed_columns;
+
+        if ((e = getenv("COLUMNS")))
+                parsed_columns = atoi(e);
+
+        if (parsed_columns <= 0) {
+                struct winsize ws;
+                zero(ws);
+
+                if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
+                        parsed_columns = ws.ws_col;
+        }
+
+        if (parsed_columns <= 0)
+                parsed_columns = 80;
+
+        return parsed_columns;
+}
+
+static int list_units(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        DBusMessageIter iter, sub, sub2;
+        unsigned k = 0;
+
+        dbus_error_init(&error);
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "ListUnits"))) {
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_message_iter_init(reply, &iter) ||
+            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
+            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
+                log_error("Failed to parse reply.");
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_message_iter_recurse(&iter, &sub);
+
+        printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
+
+        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+                const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
+                uint32_t job_id;
+
+                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
+                        log_error("Failed to parse reply.");
+                        r = -EIO;
+                        goto finish;
+                }
+
+                dbus_message_iter_recurse(&sub, &sub2);
+
+                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
+                        log_error("Failed to parse reply.");
+                        r = -EIO;
+                        goto finish;
+                }
+
+                if ((!arg_type || ((dot = strrchr(id, '.')) &&
+                                   streq(dot+1, arg_type))) &&
+                    (arg_all || !streq(active_state, "inactive"))) {
+
+                        int a = 0, b = 0;
+
+                        printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
+
+                        if (job_id != 0)
+                                printf(" %-15s%n", job_type, &b);
+                        else
+                                b = 1 + 15;
+
+                        if (a + b + 2 < columns()) {
+                                if (job_id == 0)
+                                        printf("                ");
+
+                                printf("%.*s", columns() - a - b - 2, description);
+                        }
+
+                        fputs("\n", stdout);
+                        k++;
+                }
+
+                dbus_message_iter_next(&sub);
+        }
+
+        if (arg_all)
+                printf("\n%u units listed.\n", k);
+        else
+                printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        DBusMessageIter iter, sub, sub2;
+        unsigned k = 0;
+
+        dbus_error_init(&error);
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "ListJobs"))) {
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_message_iter_init(reply, &iter) ||
+            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
+            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
+                log_error("Failed to parse reply.");
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_message_iter_recurse(&iter, &sub);
+
+        printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
+
+        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+                const char *name, *type, *state, *job_path, *unit_path;
+                uint32_t id;
+
+                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
+                        log_error("Failed to parse reply.");
+                        r = -EIO;
+                        goto finish;
+                }
+
+                dbus_message_iter_recurse(&sub, &sub2);
+
+                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
+                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
+                        log_error("Failed to parse reply.");
+                        r = -EIO;
+                        goto finish;
+                }
+
+                printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
+                k++;
+
+                dbus_message_iter_next(&sub);
+        }
+
+        printf("\n%u jobs listed.\n", k);
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int load_unit(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        unsigned i;
+
+        dbus_error_init(&error);
+
+        for (i = 1; i < n; i++) {
+
+                if (!(m = dbus_message_new_method_call(
+                                      "org.freedesktop.systemd1",
+                                      "/org/freedesktop/systemd1",
+                                      "org.freedesktop.systemd1.Manager",
+                                      "LoadUnit"))) {
+                        log_error("Could not allocate message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if (!dbus_message_append_args(m,
+                                              DBUS_TYPE_STRING, &args[i],
+                                              DBUS_TYPE_INVALID)) {
+                        log_error("Could not append arguments to message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                        log_error("Failed to issue method call: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
+
+                dbus_message_unref(m);
+                dbus_message_unref(reply);
+
+                m = reply = NULL;
+        }
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        unsigned i;
+
+        dbus_error_init(&error);
+
+        for (i = 1; i < n; i++) {
+                unsigned id;
+                const char *path;
+
+                if (!(m = dbus_message_new_method_call(
+                                      "org.freedesktop.systemd1",
+                                      "/org/freedesktop/systemd1",
+                                      "org.freedesktop.systemd1.Manager",
+                                      "GetJob"))) {
+                        log_error("Could not allocate message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if ((r = safe_atou(args[i], &id)) < 0) {
+                        log_error("Failed to parse job id: %s", strerror(-r));
+                        goto finish;
+                }
+
+                assert_cc(sizeof(uint32_t) == sizeof(id));
+                if (!dbus_message_append_args(m,
+                                              DBUS_TYPE_UINT32, &id,
+                                              DBUS_TYPE_INVALID)) {
+                        log_error("Could not append arguments to message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                        log_error("Failed to issue method call: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
+
+                if (!dbus_message_get_args(reply, &error,
+                                           DBUS_TYPE_OBJECT_PATH, &path,
+                                           DBUS_TYPE_INVALID)) {
+                        log_error("Failed to parse reply: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
+
+                dbus_message_unref(m);
+                if (!(m = dbus_message_new_method_call(
+                                      "org.freedesktop.systemd1",
+                                      path,
+                                      "org.freedesktop.systemd1.Job",
+                                      "Cancel"))) {
+                        log_error("Could not allocate message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                dbus_message_unref(reply);
+                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                        log_error("Failed to issue method call: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
+
+                dbus_message_unref(m);
+                dbus_message_unref(reply);
+                m = reply = NULL;
+        }
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
+        DBusError error;
+        Set *s = data;
+
+        assert(connection);
+        assert(message);
+        assert(s);
+
+        dbus_error_init(&error);
+
+        /* log_debug("Got D-Bus request: %s.%s() on %s", */
+        /*           dbus_message_get_interface(message), */
+        /*           dbus_message_get_member(message), */
+        /*           dbus_message_get_path(message)); */
+
+        if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
+                log_error("Warning! D-Bus connection terminated.");
+                dbus_connection_close(connection);
+
+        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
+                uint32_t id;
+                const char *path;
+
+                if (!dbus_message_get_args(message, &error,
+                                           DBUS_TYPE_UINT32, &id,
+                                           DBUS_TYPE_OBJECT_PATH, &path,
+                                           DBUS_TYPE_INVALID))
+                        log_error("Failed to parse message: %s", error.message);
+                else {
+                        char *p;
+
+                        if ((p = set_remove(s, (char*) path)))
+                                free(p);
+                }
+        }
+
+        dbus_error_free(&error);
+        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static int wait_for_jobs(DBusConnection *bus, Set *s) {
+        DBusError error;
+        DBusMessage *m = NULL, *reply = NULL;
+        int r;
+
+        assert(bus);
+        assert(s);
+
+        dbus_error_init(&error);
+
+        dbus_bus_add_match(bus,
+                           "type='signal',"
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.systemd1.Manager',"
+                           "member='JobRemoved',"
+                           "path='/org/freedesktop/systemd1'",
+                           &error);
+
+        if (dbus_error_is_set(&error)) {
+                log_error("Failed to add match: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_connection_add_filter(bus, wait_filter, s, NULL)) {
+                log_error("Failed to add filter.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "Subscribe"))) {
+                log_error("Could not allocate message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        while (!set_isempty(s) &&
+               dbus_connection_read_write_dispatch(bus, -1))
+                ;
+
+        r = 0;
+
+finish:
+        /* This is slightly dirty, since we don't undo the filter or the matches. */
+
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int start_unit(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        unsigned i;
+        const char *method, *mode;
+        char *p = NULL;
+        Set *s = NULL;
+
+        dbus_error_init(&error);
+
+        method =
+                streq(args[0], "start")  ? "StartUnit" :
+                streq(args[0], "stop")   ? "StopUnit" :
+                streq(args[0], "reload") ? "ReloadUnit" :
+                                           "RestartUnit";
+
+        mode = arg_replace ? "replace" : "fail";
+
+        for (i = 1; i < n; i++) {
+
+                if (!(m = dbus_message_new_method_call(
+                                      "org.freedesktop.systemd1",
+                                      "/org/freedesktop/systemd1",
+                                      "org.freedesktop.systemd1.Manager",
+                                      method))) {
+                        log_error("Could not allocate message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if (!dbus_message_append_args(m,
+                                              DBUS_TYPE_STRING, &args[i],
+                                              DBUS_TYPE_STRING, &mode,
+                                              DBUS_TYPE_INVALID)) {
+                        log_error("Could not append arguments to message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                        log_error("Failed to issue method call: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
+
+                if (arg_block) {
+                        const char *path;
+
+                        if (!dbus_message_get_args(reply, &error,
+                                                   DBUS_TYPE_OBJECT_PATH, &path,
+                                                   DBUS_TYPE_INVALID)) {
+                                log_error("Failed to parse reply: %s", error.message);
+                                r = -EIO;
+                                goto finish;
+                        }
+
+                        if (!s)
+                                if (!(s = set_new(string_hash_func, string_compare_func))) {
+                                        log_error("Failed to allocate set.");
+                                        r = -ENOMEM;
+                                        goto finish;
+                                }
+
+                        if (!(p = strdup(path))) {
+                                log_error("Failed to duplicate path.");
+                                r = -ENOMEM;
+                                goto finish;
+                        }
+
+                        if ((r = set_put(s, p)) < 0) {
+                                log_error("Failed to add path to set.");
+                                goto finish;
+                        }
+                        p = NULL;
+                }
+
+                dbus_message_unref(m);
+                dbus_message_unref(reply);
+
+                m = reply = NULL;
+        }
+
+        if (arg_block)
+                r = wait_for_jobs(bus, s);
+        else
+                r = 0;
+
+finish:
+        free(p);
+
+        if (s)
+                set_free_free(s);
+
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int isolate_unit(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        const char *mode = "isolate";
+        char *p = NULL;
+        Set *s = NULL;
+
+        dbus_error_init(&error);
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "StartUnit"))) {
+                log_error("Could not allocate message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!dbus_message_append_args(m,
+                                      DBUS_TYPE_STRING, &args[1],
+                                      DBUS_TYPE_STRING, &mode,
+                                      DBUS_TYPE_INVALID)) {
+                log_error("Could not append arguments to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (arg_block) {
+                const char *path;
+
+                if (!dbus_message_get_args(reply, &error,
+                                           DBUS_TYPE_OBJECT_PATH, &path,
+                                           DBUS_TYPE_INVALID)) {
+                        log_error("Failed to parse reply: %s", error.message);
+                        r = -EIO;
+                        goto finish;
+                }
+
+                if (!(s = set_new(string_hash_func, string_compare_func))) {
+                        log_error("Failed to allocate set.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if (!(p = strdup(path))) {
+                        log_error("Failed to duplicate path.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                if ((r = set_put(s, p)) < 0) {
+                        log_error("Failed to add path to set.");
+                        goto finish;
+                }
+                p = NULL;
+
+                r = wait_for_jobs(bus, s);
+
+        } else
+                r = 0;
+
+finish:
+        free(p);
+
+        if (s)
+                set_free_free(s);
+
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
+        DBusError error;
+        DBusMessage *m = NULL, *reply = NULL;
+
+        assert(connection);
+        assert(message);
+
+        dbus_error_init(&error);
+
+        /* log_debug("Got D-Bus request: %s.%s() on %s", */
+        /*           dbus_message_get_interface(message), */
+        /*           dbus_message_get_member(message), */
+        /*           dbus_message_get_path(message)); */
+
+        if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
+                log_error("Warning! D-Bus connection terminated.");
+                dbus_connection_close(connection);
+
+        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
+                   dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
+                const char *id, *path;
+
+                if (!dbus_message_get_args(message, &error,
+                                           DBUS_TYPE_STRING, &id,
+                                           DBUS_TYPE_OBJECT_PATH, &path,
+                                           DBUS_TYPE_INVALID))
+                        log_error("Failed to parse message: %s", error.message);
+                else if (streq(dbus_message_get_member(message), "UnitNew"))
+                        printf("Unit %s added.\n", id);
+                else
+                        printf("Unit %s removed.\n", id);
+
+        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
+                   dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
+                uint32_t id;
+                const char *path;
+
+                if (!dbus_message_get_args(message, &error,
+                                           DBUS_TYPE_UINT32, &id,
+                                           DBUS_TYPE_OBJECT_PATH, &path,
+                                           DBUS_TYPE_INVALID))
+                        log_error("Failed to parse message: %s", error.message);
+                else if (streq(dbus_message_get_member(message), "JobNew"))
+                        printf("Job %u added.\n", id);
+                else
+                        printf("Job %u removed.\n", id);
+
+
+        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
+                   dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
+
+                const char *path, *interface, *property = "Id";
+                DBusMessageIter iter, sub;
+
+                path = dbus_message_get_path(message);
+                interface = dbus_message_get_interface(message);
+
+                if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              path,
+                              "org.freedesktop.DBus.Properties",
+                              "Get"))) {
+                        log_error("Could not allocate message.");
+                        goto oom;
+                }
+
+                if (!dbus_message_append_args(m,
+                                              DBUS_TYPE_STRING, &interface,
+                                              DBUS_TYPE_STRING, &property,
+                                              DBUS_TYPE_INVALID)) {
+                        log_error("Could not append arguments to message.");
+                        goto finish;
+                }
+
+                if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
+                        log_error("Failed to issue method call: %s", error.message);
+                        goto finish;
+                }
+
+                if (!dbus_message_iter_init(reply, &iter) ||
+                    dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
+                        log_error("Failed to parse reply.");
+                        goto finish;
+                }
+
+                dbus_message_iter_recurse(&iter, &sub);
+
+                if (streq(interface, "org.freedesktop.systemd1.Unit")) {
+                        const char *id;
+
+                        if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
+                                log_error("Failed to parse reply.");
+                                goto finish;
+                        }
+
+                        dbus_message_iter_get_basic(&sub, &id);
+                        printf("Unit %s changed.\n", id);
+                } else {
+                        uint32_t id;
+
+                        if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32)  {
+                                log_error("Failed to parse reply.");
+                                goto finish;
+                        }
+
+                        dbus_message_iter_get_basic(&sub, &id);
+                        printf("Job %u changed.\n", id);
+                }
+        }
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+oom:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+        return DBUS_HANDLER_RESULT_NEED_MEMORY;
+}
+
+static int monitor(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+
+        dbus_error_init(&error);
+
+        dbus_bus_add_match(bus,
+                           "type='signal',"
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.systemd1.Manager',"
+                           "path='/org/freedesktop/systemd1'",
+                           &error);
+
+        if (dbus_error_is_set(&error)) {
+                log_error("Failed to add match: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_bus_add_match(bus,
+                           "type='signal',"
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.systemd1.Unit',"
+                           "member='Changed'",
+                           &error);
+
+        if (dbus_error_is_set(&error)) {
+                log_error("Failed to add match: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_bus_add_match(bus,
+                           "type='signal',"
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.systemd1.Job',"
+                           "member='Changed'",
+                           &error);
+
+        if (dbus_error_is_set(&error)) {
+                log_error("Failed to add match: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
+                log_error("Failed to add filter.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "Subscribe"))) {
+                log_error("Could not allocate message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        while (dbus_connection_read_write_dispatch(bus, -1))
+                ;
+
+        r = 0;
+
+finish:
+
+        /* This is slightly dirty, since we don't undo the filter or the matches. */
+
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int dump(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        const char *text;
+
+        dbus_error_init(&error);
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "Dump"))) {
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_message_get_args(reply, &error,
+                                   DBUS_TYPE_STRING, &text,
+                                   DBUS_TYPE_INVALID)) {
+                log_error("Failed to parse reply: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        fputs(text, stdout);
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int snapshot(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        const char *name = "", *path, *id;
+        dbus_bool_t cleanup = FALSE;
+        DBusMessageIter iter, sub;
+        const char
+                *interface = "org.freedesktop.systemd1.Unit",
+                *property = "Id";
+
+        dbus_error_init(&error);
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "CreateSnapshot"))) {
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        if (n > 1)
+                name = args[1];
+
+        if (!dbus_message_append_args(m,
+                                      DBUS_TYPE_STRING, &name,
+                                      DBUS_TYPE_BOOLEAN, &cleanup,
+                                      DBUS_TYPE_INVALID)) {
+                log_error("Could not append arguments to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_message_get_args(reply, &error,
+                                   DBUS_TYPE_OBJECT_PATH, &path,
+                                   DBUS_TYPE_INVALID)) {
+                log_error("Failed to parse reply: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_message_unref(m);
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              path,
+                              "org.freedesktop.DBus.Properties",
+                              "Get"))) {
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        if (!dbus_message_append_args(m,
+                                      DBUS_TYPE_STRING, &interface,
+                                      DBUS_TYPE_STRING, &property,
+                                      DBUS_TYPE_INVALID)) {
+                log_error("Could not append arguments to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        dbus_message_unref(reply);
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_message_iter_init(reply, &iter) ||
+            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
+                log_error("Failed to parse reply.");
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_message_iter_recurse(&iter, &sub);
+
+        if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
+                log_error("Failed to parse reply.");
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_message_iter_get_basic(&sub, &id);
+        puts(id);
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        const char *method;
+
+        dbus_error_init(&error);
+
+        method =
+                streq(args[0], "clear-jobs")    ? "ClearJobs" :
+                streq(args[0], "daemon-reload") ? "Reload" :
+                streq(args[0], "daemon-reexec") ? "Reexecute" :
+                                                  "Exit";
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              method))) {
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        DBusMessageIter iter, sub, sub2;
+        int r;
+        const char
+                *interface = "org.freedesktop.systemd1.Manager",
+                *property = "Environment";
+
+        dbus_error_init(&error);
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.DBus.Properties",
+                              "Get"))) {
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        if (!dbus_message_append_args(m,
+                                      DBUS_TYPE_STRING, &interface,
+                                      DBUS_TYPE_STRING, &property,
+                                      DBUS_TYPE_INVALID)) {
+                log_error("Could not append arguments to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        if (!dbus_message_iter_init(reply, &iter) ||
+            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
+                log_error("Failed to parse reply.");
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_message_iter_recurse(&iter, &sub);
+
+        if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
+            dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING)  {
+                log_error("Failed to parse reply.");
+                r = -EIO;
+                goto finish;
+        }
+
+        dbus_message_iter_recurse(&sub, &sub2);
+
+        while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
+                const char *text;
+
+                if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
+                        log_error("Failed to parse reply.");
+                        r = -EIO;
+                        goto finish;
+                }
+
+                dbus_message_iter_get_basic(&sub2, &text);
+                printf("%s\n", text);
+
+                dbus_message_iter_next(&sub2);
+        }
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int set_environment(DBusConnection *bus, char **args, unsigned n) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int r;
+        const char *method;
+        DBusMessageIter iter, sub;
+        unsigned i;
+
+        dbus_error_init(&error);
+
+        method = streq(args[0], "set-environment")
+                ? "SetEnvironment"
+                : "UnsetEnvironment";
+
+        if (!(m = dbus_message_new_method_call(
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              method))) {
+
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        dbus_message_iter_init_append(m, &iter);
+
+        if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
+                log_error("Could not append arguments to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        for (i = 1; i < n; i++)
+                if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
+                        log_error("Could not append arguments to message.");
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+        if (!dbus_message_iter_close_container(&iter, &sub)) {
+                log_error("Could not append arguments to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int help(void) {
+
+        printf("%s [options]\n\n"
+               "  -h --help      Show this help\n"
+               "  -t --type=TYPE List only units of a particular type\n"
+               "  -a --all       Show all units, including dead ones\n"
+               "     --replace   When installing a new job, replace existing conflicting ones\n"
+               "     --system    Connect to system bus\n"
+               "     --session   Connect to session bus\n"
+               "     --block     Wait until operation finished\n\n"
+               "Commands:\n"
+               "  list-units                      List units\n"
+               "  list-jobs                       List jobs\n"
+               "  clear-jobs                      Cancel all jobs\n"
+               "  load [NAME...]                  Load one or more units\n"
+               "  cancel [JOB...]                 Cancel one or more jobs\n"
+               "  start [NAME...]                 Start one or more units\n"
+               "  stop [NAME...]                  Stop one or more units\n"
+               "  restart [NAME...]               Restart one or more units\n"
+               "  reload [NAME...]                Reload one or more units\n"
+               "  isolate [NAME]                  Start one unit and stop all others\n"
+               "  monitor                         Monitor unit/job changes\n"
+               "  dump                            Dump server status\n"
+               "  snapshot [NAME]                 Create a snapshot\n"
+               "  daemon-reload                   Reload daemon configuration\n"
+               "  daemon-reexecute                Reexecute daemon\n"
+               "  daemon-exit                     Ask the daemon to quit\n"
+               "  show-environment                Dump environment\n"
+               "  set-environment [NAME=VALUE...] Set one or more environment variables\n"
+               "  unset-environment [NAME...]     Unset one or more environment variables\n",
+               __progname);
+
+        return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+        enum {
+                ARG_REPLACE = 0x100,
+                ARG_SESSION,
+                ARG_SYSTEM,
+                ARG_BLOCK,
+        };
+
+        static const struct option options[] = {
+                { "help",      no_argument,       NULL, 'h'         },
+                { "type",      required_argument, NULL, 't'         },
+                { "all",       no_argument,       NULL, 'a'         },
+                { "replace",   no_argument,       NULL, ARG_REPLACE },
+                { "session",   no_argument,       NULL, ARG_SESSION },
+                { "system",    no_argument,       NULL, ARG_SYSTEM  },
+                { "block",     no_argument,       NULL, ARG_BLOCK   }
+        };
+
+        int c;
+
+        assert(argc >= 1);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "hta", options, NULL)) >= 0) {
+
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                case 't':
+                        arg_type = optarg;
+                        break;
+
+                case 'a':
+                        arg_all = true;
+                        break;
+
+                case ARG_REPLACE:
+                        arg_replace = true;
+                        break;
+
+                case ARG_SESSION:
+                        arg_session = true;
+                        break;
+
+                case ARG_SYSTEM:
+                        arg_session = false;
+                        break;
+
+                case ARG_BLOCK:
+                        arg_block = true;
+                        break;
+
+                case '?':
+                        return -EINVAL;
+
+                default:
+                        log_error("Unknown option code %c", c);
+                        return -EINVAL;
+                }
+        }
+
+        return 1;
+}
+
+int main(int argc, char*argv[]) {
+
+
+        static const struct {
+                const char* verb;
+                const enum {
+                        MORE,
+                        LESS,
+                        EQUAL
+                } argc_cmp;
+                const int argc;
+                int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
+        } verbs[] = {
+                { "list-units",        LESS,  1, list_units      },
+                { "list-jobs",         EQUAL, 1, list_jobs       },
+                { "clear-jobs",        EQUAL, 1, clear_jobs      },
+                { "load",              MORE,  2, load_unit       },
+                { "cancel",            MORE,  2, cancel_job      },
+                { "start",             MORE,  2, start_unit      },
+                { "stop",              MORE,  2, start_unit      },
+                { "reload",            MORE,  2, start_unit      },
+                { "restart",           MORE,  2, start_unit      },
+                { "isolate",           EQUAL, 2, isolate_unit    },
+                { "monitor",           EQUAL, 1, monitor         },
+                { "dump",              EQUAL, 1, dump            },
+                { "snapshot",          LESS,  2, snapshot        },
+                { "daemon-reload",     EQUAL, 1, clear_jobs      },
+                { "daemon-reexec",     EQUAL, 1, clear_jobs      },
+                { "daemon-exit",       EQUAL, 1, clear_jobs      },
+                { "show-environment",  EQUAL, 1, show_enviroment },
+                { "set-environment",   MORE,  2, set_environment },
+                { "unset-environment", MORE,  2, set_environment },
+        };
+
+        int r, retval = 1, left;
+        unsigned i;
+        DBusConnection *bus = NULL;
+        DBusError error;
+
+        dbus_error_init(&error);
+
+        log_set_target(LOG_TARGET_CONSOLE);
+        log_parse_environment();
+
+        if ((r = parse_argv(argc, argv)) < 0)
+                goto finish;
+        else if (r == 0) {
+                retval = 0;
+                goto finish;
+        }
+
+        left = argc - optind;
+
+        if (left <= 0)
+                /* Special rule: no arguments means "list-units" */
+                i = 0;
+        else {
+                for (i = 0; i < ELEMENTSOF(verbs); i++)
+                        if (streq(argv[optind], verbs[i].verb))
+                                break;
+
+                if (i >= ELEMENTSOF(verbs)) {
+                        log_error("Unknown operation %s", argv[optind]);
+                        goto finish;
+                }
+        }
+
+        switch (verbs[i].argc_cmp) {
+
+        case EQUAL:
+                if (left != verbs[i].argc) {
+                        log_error("Invalid number of arguments.");
+                        goto finish;
+                }
+
+                break;
+
+        case MORE:
+                if (left < verbs[i].argc) {
+                        log_error("Too few arguments.");
+                        goto finish;
+                }
+
+                break;
+
+        case LESS:
+                if (left > verbs[i].argc) {
+                        log_error("Too many arguments.");
+                        goto finish;
+                }
+
+                break;
+
+        default:
+                assert_not_reached("Unknown comparison operator.");
+        }
+
+        if (!(bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
+                log_error("Failed to get D-Bus connection: %s", error.message);
+                goto finish;
+        }
+
+        dbus_connection_set_exit_on_disconnect(bus, FALSE);
+
+        retval = verbs[i].dispatch(bus, argv + optind, left) < 0;
+
+finish:
+
+        if (bus)
+                dbus_connection_unref(bus);
+
+        dbus_shutdown();
+
+        return retval;
+}
diff --git a/src/systemctl.vala b/src/systemctl.vala
deleted file mode 100644
index 6cce93b..0000000
--- a/src/systemctl.vala
+++ /dev/null
@@ -1,357 +0,0 @@
-/***
-  This file is part of systemd.
-
-  Copyright 2010 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 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
-  General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-using DBus;
-using GLib;
-
-static string type = null;
-static bool all = false;
-static bool replace = false;
-static bool session = false;
-static bool block = false;
-static Connection? bus = null;
-static List<ObjectPath> jobs = null;
-static MainLoop main_loop = null;
-static int exit_code = 0;
-
-public static int job_info_compare(void* key1, void* key2) {
-        Manager.JobInfo *j1 = (Manager.JobInfo*) key1;
-        Manager.JobInfo *j2 = (Manager.JobInfo*) key2;
-
-        return j1->id < j2->id ? -1 : (j1->id > j2->id ? 1 : 0);
-}
-
-public static int unit_info_compare(void* key1, void* key2) {
-        Manager.UnitInfo *u1 = (Manager.UnitInfo*) key1;
-        Manager.UnitInfo *u2 = (Manager.UnitInfo*) key2;
-
-        int r = Posix.strcmp(Posix.strrchr(u1->id, '.'), Posix.strrchr(u2->id, '.'));
-        if (r != 0)
-                return r;
-
-        return Posix.strcmp(u1->id, u2->id);
-}
-
-public void monitor_on_unit_changed(Unit u) {
-        stdout.printf("Unit %s changed.\n", u.id);
-}
-
-public void monitor_on_unit_new(string id, ObjectPath path) {
-        stdout.printf("Unit %s added.\n", id);
-
-        Unit u = bus.get_object(
-                        "org.freedesktop.systemd1",
-                        path,
-                        "org.freedesktop.systemd1.Unit") as Unit;
-
-        u.changed += monitor_on_unit_changed;
-
-        /* FIXME: We leak memory here */
-        u.ref();
-}
-
-public void monitor_on_job_changed(Job j) {
-        stdout.printf("Job %u changed.\n", j.id);
-}
-
-public void monitor_on_job_new(uint32 id, ObjectPath path) {
-        stdout.printf("Job %u added.\n", id);
-
-        Job j = bus.get_object(
-                        "org.freedesktop.systemd1",
-                        path,
-                        "org.freedesktop.systemd1.Job") as Job;
-
-        j.changed += monitor_on_job_changed;
-
-        /* FIXME: We leak memory here */
-        j.ref();
-}
-
-public void monitor_on_unit_removed(string id, ObjectPath path) {
-        stdout.printf("Unit %s removed.\n", id);
-}
-
-public void monitor_on_job_removed(uint32 id, ObjectPath path, bool success) {
-        stdout.printf("Job %u removed (success=%i).\n", id, (int) success);
-}
-
-public void block_on_job_removed(uint32 id, ObjectPath path, bool success) {
-
-        for (unowned List<ObjectPath> i = jobs; i != null; i = i.next)
-                if (i.data == path) {
-                        jobs.remove_link(i);
-                        break;
-                }
-
-        if (jobs == null) {
-                if (!success)
-                        exit_code = 1;
-
-                main_loop.quit();
-        }
-}
-
-static const OptionEntry entries[] = {
-        { "type",    't', 0,                   OptionArg.STRING, out type,    "List only particular type of units", "TYPE" },
-        { "all",     'a', 0,                   OptionArg.NONE,   out all,     "Show all units, including dead ones", null  },
-        { "replace", 0,   0,                   OptionArg.NONE,   out replace, "When installing a new job, replace existing conflicting ones", null },
-        { "session", 0,   0,                   OptionArg.NONE,   out session, "Connect to session bus", null },
-        { "system",  0,   OptionFlags.REVERSE, OptionArg.NONE,   out session, "Connect to system bus", null },
-        { "block",   0,   0,                   OptionArg.NONE,   out block,   "Wait until the operation finished", null },
-        { null }
-};
-
-int main (string[] args) {
-        OptionContext context = new OptionContext("[COMMAND [ARGUMENT...]]");
-        context.add_main_entries(entries, null);
-        context.set_description(
-                        "Commands:\n" +
-                        "  list-units                      List units\n" +
-                        "  list-jobs                       List jobs\n" +
-                        "  clear-jobs                      Cancel all jobs\n" +
-                        "  load [NAME...]                  Load one or more units\n" +
-                        "  cancel [JOB...]                 Cancel one or more jobs\n" +
-                        "  start [NAME...]                 Start one or more units\n" +
-                        "  stop [NAME...]                  Stop one or more units\n" +
-                        "  restart [NAME...]               Restart one or more units\n" +
-                        "  reload [NAME...]                Reload one or more units\n" +
-                        "  isolate [NAME]                  Start one unit and stop all others\n" +
-                        "  monitor                         Monitor unit/job changes\n" +
-                        "  dump                            Dump server status\n" +
-                        "  snapshot [NAME]                 Create a snapshot\n" +
-                        "  daemon-reload                   Reload daemon configuration\n" +
-                        "  daemon-reexecute                Reexecute daemon\n" +
-                        "  daemon-exit                     Ask the daemon to quit\n" +
-                        "  show-environment                Dump environment\n" +
-                        "  set-environment [NAME=VALUE...] Set one or more environment variables\n" +
-                        "  unset-environment [NAME...]     Unset one or more environment variables\n");
-
-        try {
-                context.parse(ref args);
-        } catch (GLib.OptionError e) {
-                message("Failed to parse command line: %s".printf(e.message));
-        }
-
-        try {
-                bus = Bus.get(session ? BusType.SESSION : BusType.SYSTEM);
-
-                Manager manager = bus.get_object (
-                                "org.freedesktop.systemd1",
-                                "/org/freedesktop/systemd1",
-                                "org.freedesktop.systemd1.Manager") as Manager;
-
-                if (args[1] == "list-units" || args.length <= 1) {
-                        var list = manager.list_units();
-                        uint n = 0;
-                        Posix.qsort(list, list.length, sizeof(Manager.UnitInfo), unit_info_compare);
-
-                        stdout.printf("%-45s %-6s %-12s %-12s %-17s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB");
-
-                        foreach (var i in list) {
-
-                                if (type != null && !i.id.has_suffix(".%s".printf(type)))
-                                        continue;
-
-                                if (!all && i.active_state == "inactive")
-                                        continue;
-
-                                stdout.printf("%-45s %-6s %-12s %-12s", i.id, i.load_state, i.active_state, i.sub_state);
-
-                                if (i.job_id != 0)
-                                        stdout.printf(" -> %-15s", i.job_type);
-
-                                stdout.puts("\n");
-                                n++;
-                        }
-
-                        if (all)
-                                stdout.printf("\n%u units listed.\n", n);
-                        else
-                                stdout.printf("\n%u live units listed. Pass --all to see dead units, too.\n", n);
-
-
-                } else if (args[1] == "list-jobs") {
-                        var list = manager.list_jobs();
-                        Posix.qsort(list, list.length, sizeof(Manager.JobInfo), job_info_compare);
-
-                        stdout.printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
-
-                        foreach (var i in list)
-                                stdout.printf("%4u %-45s → %-15s %-7s\n", i.id, i.name, i.type, i.state);
-
-                        stdout.printf("\n%u jobs listed.\n", list.length);
-
-                } else if (args[1] == "clear-jobs") {
-
-                        manager.clear_jobs();
-
-                } else if (args[1] == "load") {
-
-                        if (args.length < 3) {
-                                stderr.printf("Missing argument.\n");
-                                return 1;
-                        }
-
-                        for (int i = 2; i < args.length; i++)
-                                manager.load_unit(args[i]);
-
-                } else if (args[1] == "cancel") {
-
-                        if (args.length < 3) {
-                                stderr.printf("Missing argument.\n");
-                                return 1;
-                        }
-
-                        for (int i = 2; i < args.length; i++) {
-                                uint32 id;
-
-                                if (args[i].scanf("%u", out id) != 1) {
-                                        stderr.printf("Failed to parse argument.\n");
-                                        return 1;
-                                }
-
-                                ObjectPath p = manager.get_job(id);
-
-                                Job j = bus.get_object (
-                                                "org.freedesktop.systemd1",
-                                                p,
-                                                "org.freedesktop.systemd1.Job") as Job;
-
-                                j.cancel();
-                        }
-
-                } else if (args[1] == "start" ||
-                           args[1] == "stop" ||
-                           args[1] == "reload" ||
-                           args[1] == "restart") {
-
-                        if (args.length < 3) {
-                                stderr.printf("Missing argument.\n");
-                                return 1;
-                        }
-
-                        if (block)
-                                manager.subscribe();
-
-                        for (int i = 2; i < args.length; i++) {
-
-                                string mode = replace ? "replace" : "fail";
-                                ObjectPath j = null;
-
-                                if (args[1] == "start")
-                                        j = manager.start_unit(args[i], mode);
-                                else if (args[1] == "stop")
-                                        j = manager.stop_unit(args[i], mode);
-                                else if (args[1] == "restart")
-                                        j = manager.restart_unit(args[i], mode);
-                                else if (args[1] == "reload")
-                                        j = manager.reload_unit(args[i], mode);
-
-                                if (block)
-                                        jobs.append(j);
-                        }
-
-                } else if (args[1] == "isolate") {
-
-                        if (args.length != 3) {
-                                stderr.printf("Missing argument.\n");
-                                return 1;
-                        }
-
-                        ObjectPath j = manager.start_unit(args[2], "isolate");
-
-                        if (block) {
-                                manager.subscribe();
-                                jobs.append(j);
-                        }
-
-                } else if (args[1] == "monitor") {
-
-                        manager.subscribe();
-
-                        var unit_list = manager.list_units();
-
-                        foreach (var i in unit_list) {
-                                monitor_on_unit_new(i.id, i.unit_path);
-
-                                if (i.job_id != 0)
-                                        monitor_on_job_new(i.job_id, i.job_path);
-                        }
-
-                        manager.unit_new += monitor_on_unit_new;
-                        manager.unit_removed += monitor_on_unit_removed;
-                        manager.job_new += monitor_on_job_new;
-                        manager.job_removed += monitor_on_job_removed;
-
-                        main_loop = new MainLoop();
-                        main_loop.run();
-
-                } else if (args[1] == "dump")
-                        stdout.puts(manager.dump());
-
-                else if (args[1] == "snapshot") {
-
-                        ObjectPath p = manager.create_snapshot(args.length > 2 ? args[2] : "");
-
-                        Unit u = bus.get_object(
-                                        "org.freedesktop.systemd1",
-                                        p,
-                                        "org.freedesktop.systemd1.Unit") as Unit;
-
-                        stdout.printf("%s\n", u.id);
-
-                } else if (args[1] == "daemon-reload")
-                        manager.reload();
-
-                else if (args[1] == "daemon-reexecute" || args[1] == "daemon-reexec")
-                        manager.reexecute();
-
-                else if (args[1] == "daemon-exit")
-                        manager.exit();
-
-                else if (args[1] == "show-environment") {
-                        foreach(var x in manager.environment)
-                                stderr.printf("%s\n", x);
-
-                } else if (args[1] == "set-environment")
-                        manager.set_environment(args[2:args.length]);
-
-                else if (args[1] == "unset-environment")
-                        manager.unset_environment(args[2:args.length]);
-
-                else {
-                        stderr.printf("Unknown command %s.\n", args[1]);
-                        return 1;
-                }
-
-                if (jobs != null && block) {
-                        manager.job_removed += block_on_job_removed;
-
-                        main_loop = new MainLoop();
-                        main_loop.run();
-                }
-
-        } catch (DBus.Error e) {
-                stderr.printf("%s\n".printf(e.message));
-                return 1;
-        }
-
-        return exit_code;
-}
commit fb1af5b0fe8e1530008bc0545b6a2e042e84c15f
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Jun 15 02:46:26 2010 +0200

    initctl: make sure we don't exit due to dbus disconnect

diff --git a/src/initctl.c b/src/initctl.c
index 9872437..34c3883 100644
--- a/src/initctl.c
+++ b/src/initctl.c
@@ -230,8 +230,10 @@ static void server_done(Server *s) {
         if (s->epoll_fd >= 0)
                 close_nointr_nofail(s->epoll_fd);
 
-        if (s->bus)
-                dbus_connection_unref(s->bus);
+        if (s->bus) {
+               dbus_connection_set_exit_on_disconnect(s->bus, FALSE);
+               dbus_connection_unref(s->bus);
+        }
 }
 
 static int server_init(Server *s, unsigned n_sockets) {
commit cf3e247193b49c1907dd85b74f0de1530948016c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Jun 15 02:46:07 2010 +0200

    dbus: tighten match rules a bit

diff --git a/src/dbus.c b/src/dbus.c
index a14dbda..69ed641 100644
--- a/src/dbus.c
+++ b/src/dbus.c
@@ -332,7 +332,7 @@ static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
                 log_error("Failed to rearm timer: %s", strerror(-r));
 }
 
-static DBusHandlerResult api_bus_message_filter(DBusConnection  *connection, DBusMessage  *message, void *data) {
+static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
         Manager *m = data;
         DBusError error;
         DBusMessage *reply = NULL;
@@ -438,7 +438,7 @@ oom:
         return DBUS_HANDLER_RESULT_NEED_MEMORY;
 }
 
-static DBusHandlerResult system_bus_message_filter(DBusConnection  *connection, DBusMessage  *message, void *data) {
+static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
         Manager *m = data;
         DBusError error;
 
@@ -457,7 +457,7 @@ static DBusHandlerResult system_bus_message_filter(DBusConnection  *connection,
                 log_error("Warning! System D-Bus connection terminated.");
                 bus_done_system(m);
 
-        } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
+        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
                 const char *cgroup;
 
                 if (!dbus_message_get_args(message, &error,
@@ -733,6 +733,7 @@ int bus_init_system(Manager *m) {
         dbus_bus_add_match(m->system_bus,
                            "type='signal',"
                            "interface='org.freedesktop.systemd1.Agent',"
+                           "member='Released',"
                            "path='/org/freedesktop/systemd1/agent'",
                            &error);
 
@@ -798,6 +799,7 @@ int bus_init_api(Manager *m) {
                            "type='signal',"
                            "sender='"DBUS_SERVICE_DBUS"',"
                            "interface='"DBUS_INTERFACE_DBUS"',"
+                           "member='NameOwnerChange',"
                            "path='"DBUS_PATH_DBUS"'",
                            &error);
 
@@ -813,6 +815,7 @@ int bus_init_api(Manager *m) {
                            "type='signal',"
                            "sender='"DBUS_SERVICE_DBUS"',"
                            "interface='org.freedesktop.systemd1.Activator',"
+                           "member='ActivationRequest',"
                            "path='"DBUS_PATH_DBUS"'",
                            &error);
 
diff --git a/src/dbus.h b/src/dbus.h
index 3ad299e..264bdff 100644
--- a/src/dbus.h
+++ b/src/dbus.h
@@ -37,23 +37,23 @@ typedef struct BusProperty {
 } BusProperty;
 
 #define BUS_PROPERTIES_INTERFACE                                        \
-        " <interface name=\"org.freedesktop.DBus.Properties\">\n"         \
-        "  <method name=\"Get\">\n"                                       \
-        "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"      \
-        "   <arg name=\"property\" direction=\"in\" type=\"s\"/>\n"       \
-        "   <arg name=\"value\" direction=\"out\" type=\"v\"/>\n"         \
-        "  </method>\n"                                                   \
-        "  <method name=\"GetAll\">\n"                                    \
-        "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"      \
+        " <interface name=\"org.freedesktop.DBus.Properties\">\n"       \
+        "  <method name=\"Get\">\n"                                     \
+        "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"    \
+        "   <arg name=\"property\" direction=\"in\" type=\"s\"/>\n"     \
+        "   <arg name=\"value\" direction=\"out\" type=\"v\"/>\n"       \
+        "  </method>\n"                                                 \
+        "  <method name=\"GetAll\">\n"                                  \
+        "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"    \
         "   <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>\n" \
-        "  </method>\n"                                                   \
+        "  </method>\n"                                                 \
         " </interface>\n"
 
 #define BUS_INTROSPECTABLE_INTERFACE                                    \
-        " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"     \
-        "  <method name=\"Introspect\">\n"                                \
-        "   <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"          \
-        "  </method>\n"                                                   \
+        " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"   \
+        "  <method name=\"Introspect\">\n"                              \
+        "   <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"        \
+        "  </method>\n"                                                 \
         " </interface>\n"
 
 int bus_init_system(Manager *m);
commit 53ec43c61d2f0e19c41640c63ec034b2f8cedb89
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Jun 15 02:45:26 2010 +0200

    set: introduce set_free_free()

diff --git a/src/load-fragment.c b/src/load-fragment.c
index 5f5e373..b679fab 100644
--- a/src/load-fragment.c
+++ b/src/load-fragment.c
@@ -1483,7 +1483,6 @@ static int load_from_path(Unit *u, const char *path) {
 #undef EXEC_CONTEXT_CONFIG_ITEMS
 
         const char *sections[3];
-        char *k;
         int r;
         Set *symlink_names;
         FILE *f = NULL;
@@ -1581,10 +1580,7 @@ static int load_from_path(Unit *u, const char *path) {
         r = 0;
 
 finish:
-        while ((k = set_steal_first(symlink_names)))
-                free(k);
-
-        set_free(symlink_names);
+        set_free_free(symlink_names);
         free(filename);
 
         if (f)
diff --git a/src/set.c b/src/set.c
index efd20db..a1aeb72 100644
--- a/src/set.c
+++ b/src/set.c
@@ -37,6 +37,15 @@ void set_free(Set* s) {
         hashmap_free(MAKE_HASHMAP(s));
 }
 
+void set_free_free(Set *s) {
+        void *p;
+
+        while ((p = set_steal_first(s)))
+                free(p);
+
+        set_free(s);
+}
+
 int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func) {
         return hashmap_ensure_allocated((Hashmap**) s, hash_func, compare_func);
 }
diff --git a/src/set.h b/src/set.h
index dd2e91d..c643a27 100644
--- a/src/set.h
+++ b/src/set.h
@@ -34,6 +34,7 @@ typedef struct Set Set;
 
 Set *set_new(hash_func_t hash_func, compare_func_t compare_func);
 void set_free(Set* s);
+void set_free_free(Set *s);
 Set* set_copy(Set *s);
 int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func);
 
diff --git a/src/unit.c b/src/unit.c
index 3bb41a4..229f44c 100644
--- a/src/unit.c
+++ b/src/unit.c
@@ -354,9 +354,7 @@ void unit_free(Unit *u) {
         free(u->meta.description);
         free(u->meta.fragment_path);
 
-        while ((t = set_steal_first(u->meta.names)))
-                free(t);
-        set_free(u->meta.names);
+        set_free_free(u->meta.names);
 
         free(u->meta.instance);
 
@@ -402,10 +400,7 @@ static void merge_names(Unit *u, Unit *other) {
 
         complete_move(&u->meta.names, &other->meta.names);
 
-        while ((t = set_steal_first(other->meta.names)))
-                free(t);
-
-        set_free(other->meta.names);
+        set_free_free(other->meta.names);
         other->meta.names = NULL;
         other->meta.id = NULL;
 


More information about the systemd-commits mailing list