[systemd-commits] 9 commits - src/cgroups-agent src/core src/shared src/systemctl

Lennart Poettering lennart at kemper.freedesktop.org
Mon Nov 25 08:41:02 PST 2013


 src/cgroups-agent/cgroups-agent.c |    8 -
 src/core/dbus-execute.c           |   85 ++++++++++----
 src/core/dbus-execute.h           |    4 
 src/core/dbus-service.c           |   12 +-
 src/core/dbus-socket.c            |    8 -
 src/core/dbus-unit.c              |    5 
 src/core/device.c                 |  189 ++++++++++++++++++--------------
 src/core/job.c                    |    3 
 src/core/manager.c                |   37 ++++--
 src/core/manager.h                |    4 
 src/core/mount.c                  |   68 +++++------
 src/core/mount.h                  |   12 +-
 src/core/swap.c                   |  220 ++++++++++++++++++--------------------
 src/core/swap.h                   |   17 +-
 src/shared/macro.h                |    2 
 src/systemctl/systemctl.c         |    4 
 16 files changed, 381 insertions(+), 297 deletions(-)

New commits:
commit d2bd639262a45ba4f686ab4a9555f2e8a0337d34
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 17:34:57 2013 +0100

    systemctl: remove debug log message

diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index d4d46f3..3fcc066 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -3670,8 +3670,6 @@ static int show_all(
                 if (!p)
                         return log_oom();
 
-                printf("%s -> '%s'\n", u->id, p);
-
                 r = show_one(verb, bus, p, show_properties, new_line, ellipsized);
                 if (r != 0)
                         return r;

commit dbed408b8e07602c3169af79393b5a3fbd7036b1
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 17:28:42 2013 +0100

    systemctl: when status is called without arguments show pager

diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index fa64476..d4d46f3 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -3654,6 +3654,8 @@ static int show_all(
         if (r < 0)
                 return r;
 
+        pager_open_if_enabled();
+
         c = (unsigned) r;
 
         qsort_safe(unit_infos, c, sizeof(UnitInfo), compare_unit_info);

commit 2c7e050f61d1bf142389edd4eb83c1dc58c5c641
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 17:18:38 2013 +0100

    core: fix bus serialization of conditions

diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c
index 3c25c36..666c97c 100644
--- a/src/core/dbus-unit.c
+++ b/src/core/dbus-unit.c
@@ -326,7 +326,10 @@ static int property_get_conditions(
                 return r;
 
         LIST_FOREACH(conditions, c, u->conditions) {
-                r = sd_bus_message_append(reply, "sbbsi", condition_type_to_string(c->type), c->trigger, c->negate, c->parameter, c->state);
+                r = sd_bus_message_append(reply, "(sbbsi)",
+                                          condition_type_to_string(c->type),
+                                          c->trigger, c->negate,
+                                          c->parameter, c->state);
                 if (r < 0)
                         return r;
 

commit 4d4c80d073f26368d123f8cebff5b4c36a6beace
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 16:59:51 2013 +0100

    core: fix serialization of exec command structs

diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index 9055627..1252399 100644
--- a/src/core/dbus-execute.c
+++ b/src/core/dbus-execute.c
@@ -428,6 +428,42 @@ const sd_bus_vtable bus_exec_vtable[] = {
         SD_BUS_VTABLE_END
 };
 
+static int append_exec_command(sd_bus_message *reply, ExecCommand *c) {
+        int r;
+
+        assert(reply);
+        assert(c);
+
+        if (!c->path)
+                return 0;
+
+        r = sd_bus_message_open_container(reply, 'r', "sasbttttuii");
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append(reply, "s", c->path);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append_strv(reply, c->argv);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append(reply, "bttttuii",
+                                  c->ignore,
+                                  c->exec_status.start_timestamp.realtime,
+                                  c->exec_status.start_timestamp.monotonic,
+                                  c->exec_status.exit_timestamp.realtime,
+                                  c->exec_status.exit_timestamp.monotonic,
+                                  (uint32_t) c->exec_status.pid,
+                                  (int32_t) c->exec_status.code,
+                                  (int32_t) c->exec_status.status);
+        if (r < 0)
+                return r;
+
+        return sd_bus_message_close_container(reply);
+}
+
 int bus_property_get_exec_command(
                 sd_bus *bus,
                 const char *path,
@@ -437,7 +473,7 @@ int bus_property_get_exec_command(
                 void *userdata,
                 sd_bus_error *ret_error) {
 
-        ExecCommand *c = *(ExecCommand**) userdata;
+        ExecCommand *c = (ExecCommand*) userdata;
         int r;
 
         assert(bus);
@@ -447,35 +483,34 @@ int bus_property_get_exec_command(
         if (r < 0)
                 return r;
 
-        LIST_FOREACH(command, c, c) {
-                if (!c->path)
-                        continue;
+        r = append_exec_command(reply, c);
+        if (r < 0)
+                return r;
 
-                r = sd_bus_message_open_container(reply, 'r', "sasbttttuii");
-                if (r < 0)
-                        return r;
+        return sd_bus_message_close_container(reply);
+}
 
-                r = sd_bus_message_append(reply, "s", c->path);
-                if (r < 0)
-                        return r;
+int bus_property_get_exec_command_list(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *ret_error) {
 
-                r = sd_bus_message_append_strv(reply, c->argv);
-                if (r < 0)
-                        return r;
+        ExecCommand *c = *(ExecCommand**) userdata;
+        int r;
 
-                r = sd_bus_message_append(reply, "bttttuii",
-                                          c->ignore,
-                                          c->exec_status.start_timestamp.realtime,
-                                          c->exec_status.start_timestamp.monotonic,
-                                          c->exec_status.exit_timestamp.realtime,
-                                          c->exec_status.exit_timestamp.monotonic,
-                                          (uint32_t) c->exec_status.pid,
-                                          (int32_t) c->exec_status.code,
-                                          (int32_t) c->exec_status.status);
-                if (r < 0)
-                        return r;
+        assert(bus);
+        assert(reply);
+
+        r = sd_bus_message_open_container(reply, 'a', "(sasbttttuii)");
+        if (r < 0)
+                return r;
 
-                r = sd_bus_message_close_container(reply);
+        LIST_FOREACH(command, c, c) {
+                r = append_exec_command(reply, c);
                 if (r < 0)
                         return r;
         }
diff --git a/src/core/dbus-execute.h b/src/core/dbus-execute.h
index 1574c30..5c8bc30 100644
--- a/src/core/dbus-execute.h
+++ b/src/core/dbus-execute.h
@@ -34,7 +34,11 @@
 #define BUS_EXEC_COMMAND_VTABLE(name, offset, flags)                    \
         SD_BUS_PROPERTY(name, "a(sasbttttuii)", bus_property_get_exec_command, offset, flags)
 
+#define BUS_EXEC_COMMAND_LIST_VTABLE(name, offset, flags)                    \
+        SD_BUS_PROPERTY(name, "a(sasbttttuii)", bus_property_get_exec_command_list, offset, flags)
+
 extern const sd_bus_vtable bus_exec_vtable[];
 
 int bus_property_get_exec_output(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *ret_error);
 int bus_property_get_exec_command(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *ret_error);
+int bus_property_get_exec_command_list(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *ret_error);
diff --git a/src/core/dbus-service.c b/src/core/dbus-service.c
index 3fa2d33..3297173 100644
--- a/src/core/dbus-service.c
+++ b/src/core/dbus-service.c
@@ -60,12 +60,12 @@ const sd_bus_vtable bus_service_vtable[] = {
         SD_BUS_PROPERTY("StatusText", "s", NULL, offsetof(Service, status_text), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Service, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         BUS_EXEC_STATUS_VTABLE("ExecMain", offsetof(Service, main_exec_status), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        BUS_EXEC_COMMAND_VTABLE("ExecStartPre", offsetof(Service, exec_command[SERVICE_EXEC_START_PRE]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStart", offsetof(Service, exec_command[SERVICE_EXEC_START]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStartPost", offsetof(Service, exec_command[SERVICE_EXEC_START_POST]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecReload", offsetof(Service, exec_command[SERVICE_EXEC_RELOAD]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStop", offsetof(Service, exec_command[SERVICE_EXEC_STOP]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStopPost", offsetof(Service, exec_command[SERVICE_EXEC_STOP_POST]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStartPre", offsetof(Service, exec_command[SERVICE_EXEC_START_PRE]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStart", offsetof(Service, exec_command[SERVICE_EXEC_START]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStartPost", offsetof(Service, exec_command[SERVICE_EXEC_START_POST]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecReload", offsetof(Service, exec_command[SERVICE_EXEC_RELOAD]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStop", offsetof(Service, exec_command[SERVICE_EXEC_STOP]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStopPost", offsetof(Service, exec_command[SERVICE_EXEC_STOP_POST]), 0),
         SD_BUS_VTABLE_END
 };
 
diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c
index 19a4484..74217df 100644
--- a/src/core/dbus-socket.c
+++ b/src/core/dbus-socket.c
@@ -119,10 +119,10 @@ const sd_bus_vtable bus_socket_vtable[] = {
         SD_BUS_PROPERTY("SmackLabel", "s", NULL, offsetof(Socket, smack), 0),
         SD_BUS_PROPERTY("SmackLabelIPIn", "s", NULL, offsetof(Socket, smack_ip_in), 0),
         SD_BUS_PROPERTY("SmackLabelIPOut", "s", NULL, offsetof(Socket, smack_ip_out), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStartPre", offsetof(Socket, exec_command[SOCKET_EXEC_START_PRE]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStartPost", offsetof(Socket, exec_command[SOCKET_EXEC_START_POST]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStopPre", offsetof(Socket, exec_command[SOCKET_EXEC_STOP_PRE]), 0),
-        BUS_EXEC_COMMAND_VTABLE("ExecStopPost", offsetof(Socket, exec_command[SOCKET_EXEC_STOP_POST]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStartPre", offsetof(Socket, exec_command[SOCKET_EXEC_START_PRE]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStartPost", offsetof(Socket, exec_command[SOCKET_EXEC_START_POST]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStopPre", offsetof(Socket, exec_command[SOCKET_EXEC_STOP_PRE]), 0),
+        BUS_EXEC_COMMAND_LIST_VTABLE("ExecStopPost", offsetof(Socket, exec_command[SOCKET_EXEC_STOP_POST]), 0),
         SD_BUS_VTABLE_END
 };
 

commit 290837072b136624f1f3d941c7274e4b1b275021
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 15:35:10 2013 +0100

    core: set some event source priorities to enforce dispatching order

diff --git a/src/core/manager.c b/src/core/manager.c
index c424fec..ba4dab3 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -368,6 +368,11 @@ static int manager_setup_signals(Manager *m) {
         if (r < 0)
                 return r;
 
+        /* Process signals a bit earlier than the rest of things */
+        r = sd_event_source_set_priority(m->signal_event_source, -5);
+        if (r < 0)
+                return r;
+
         if (m->running_as == SYSTEMD_SYSTEM)
                 return enable_special_signals(m);
 
diff --git a/src/core/mount.c b/src/core/mount.c
index bf1d433..634dff1 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -1611,6 +1611,13 @@ static int mount_enumerate(Manager *m) {
                 r = sd_event_add_io(m->event, fileno(m->proc_self_mountinfo), EPOLLPRI, mount_dispatch_io, m, &m->mount_event_source);
                 if (r < 0)
                         goto fail;
+
+                /* Dispatch this before we dispatch SIGCHLD, so that
+                 * we always get the events from /proc/self/mountinfo
+                 * before the SIGCHLD of /bin/mount. */
+                r = sd_event_source_set_priority(m->mount_event_source, -10);
+                if (r < 0)
+                        goto fail;
         }
 
         r = mount_load_proc_self_mountinfo(m, false);
diff --git a/src/core/swap.c b/src/core/swap.c
index fff6139..adcf78b 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -1238,6 +1238,13 @@ static int swap_enumerate(Manager *m) {
                 r = sd_event_add_io(m->event, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m, &m->swap_event_source);
                 if (r < 0)
                         goto fail;
+
+                /* Dispatch this before we dispatch SIGCHLD, so that
+                 * we always get the events from /proc/swaps before
+                 * the SIGCHLD of /sbin/swapon. */
+                r = sd_event_source_set_priority(m->swap_event_source, -10);
+                if (r < 0)
+                        goto fail;
         }
 
         r = swap_load_proc_swaps(m, false);

commit 5bcb0f2ba0615897662fcd4f6227d066781c6fc2
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 15:26:30 2013 +0100

    swap: split state machine state ACTIVATING into two
    
    We expect the event on /proc/swaps before we expect the SIGCHILD,
    reflect this in the state machine.

diff --git a/src/core/manager.h b/src/core/manager.h
index a46b09e..d6a6bce 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -152,7 +152,6 @@ struct Manager {
         FILE *proc_swaps;
         sd_event_source *swap_event_source;
         Hashmap *swaps_by_proc_swaps;
-        bool request_reload;
 
         /* Data specific to the D-Bus subsystem */
         sd_bus *api_bus, *system_bus;
diff --git a/src/core/mount.c b/src/core/mount.c
index 99e7ced..bf1d433 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -141,8 +141,8 @@ static void mount_init(Unit *u) {
 
         if (unit_has_name(u, "-.mount")) {
                 /* Don't allow start/stop for root directory */
-                UNIT(m)->refuse_manual_start = true;
-                UNIT(m)->refuse_manual_stop = true;
+                u->refuse_manual_start = true;
+                u->refuse_manual_stop = true;
         } else {
                 /* The stdio/kmsg bridge socket is on /, in order to avoid a
                  * dep loop, don't use kmsg logging for -.mount */
@@ -161,7 +161,7 @@ static void mount_init(Unit *u) {
 
         m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
 
-        UNIT(m)->ignore_on_isolate = true;
+        u->ignore_on_isolate = true;
 }
 
 static int mount_arm_timer(Mount *m) {
@@ -694,34 +694,33 @@ static int mount_coldplug(Unit *u) {
         else if (m->from_proc_self_mountinfo)
                 new_state = MOUNT_MOUNTED;
 
-        if (new_state != m->state) {
-
-                if (new_state == MOUNT_MOUNTING ||
-                    new_state == MOUNT_MOUNTING_DONE ||
-                    new_state == MOUNT_REMOUNTING ||
-                    new_state == MOUNT_UNMOUNTING ||
-                    new_state == MOUNT_MOUNTING_SIGTERM ||
-                    new_state == MOUNT_MOUNTING_SIGKILL ||
-                    new_state == MOUNT_UNMOUNTING_SIGTERM ||
-                    new_state == MOUNT_UNMOUNTING_SIGKILL ||
-                    new_state == MOUNT_REMOUNTING_SIGTERM ||
-                    new_state == MOUNT_REMOUNTING_SIGKILL) {
-
-                        if (m->control_pid <= 0)
-                                return -EBADMSG;
-
-                        r = unit_watch_pid(UNIT(m), m->control_pid);
-                        if (r < 0)
-                                return r;
+        if (new_state == m->state)
+                return 0;
 
-                        r = mount_arm_timer(m);
-                        if (r < 0)
-                                return r;
-                }
+        if (new_state == MOUNT_MOUNTING ||
+            new_state == MOUNT_MOUNTING_DONE ||
+            new_state == MOUNT_REMOUNTING ||
+            new_state == MOUNT_UNMOUNTING ||
+            new_state == MOUNT_MOUNTING_SIGTERM ||
+            new_state == MOUNT_MOUNTING_SIGKILL ||
+            new_state == MOUNT_UNMOUNTING_SIGTERM ||
+            new_state == MOUNT_UNMOUNTING_SIGKILL ||
+            new_state == MOUNT_REMOUNTING_SIGTERM ||
+            new_state == MOUNT_REMOUNTING_SIGKILL) {
+
+                if (m->control_pid <= 0)
+                        return -EBADMSG;
+
+                r = unit_watch_pid(UNIT(m), m->control_pid);
+                if (r < 0)
+                        return r;
 
-                mount_set_state(m, new_state);
+                r = mount_arm_timer(m);
+                if (r < 0)
+                        return r;
         }
 
+        mount_set_state(m, new_state);
         return 0;
 }
 
@@ -966,12 +965,6 @@ fail:
         mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
 }
 
-static void mount_enter_mounting_done(Mount *m) {
-        assert(m);
-
-        mount_set_state(m, MOUNT_MOUNTING_DONE);
-}
-
 static void mount_enter_remounting(Mount *m) {
         int r;
 
@@ -1691,7 +1684,7 @@ static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
                                 break;
 
                         case MOUNT_MOUNTING:
-                                mount_enter_mounting_done(mount);
+                                mount_set_state(mount, MOUNT_MOUNTING_DONE);
                                 break;
 
                         default:
diff --git a/src/core/mount.h b/src/core/mount.h
index 2edb75e..22a14e1 100644
--- a/src/core/mount.h
+++ b/src/core/mount.h
@@ -54,12 +54,6 @@ typedef enum MountExecCommand {
         _MOUNT_EXEC_COMMAND_INVALID = -1
 } MountExecCommand;
 
-typedef struct MountParameters {
-        char *what;
-        char *options;
-        char *fstype;
-} MountParameters;
-
 typedef enum MountResult {
         MOUNT_SUCCESS,
         MOUNT_FAILURE_RESOURCES,
@@ -71,6 +65,12 @@ typedef enum MountResult {
         _MOUNT_RESULT_INVALID = -1
 } MountResult;
 
+typedef struct MountParameters {
+        char *what;
+        char *options;
+        char *fstype;
+} MountParameters;
+
 struct Mount {
         Unit meta;
 
diff --git a/src/core/swap.c b/src/core/swap.c
index a798eca..fff6139 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -40,10 +40,12 @@
 #include "def.h"
 #include "path-util.h"
 #include "virt.h"
+#include "udev-util.h"
 
 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
         [SWAP_DEAD] = UNIT_INACTIVE,
         [SWAP_ACTIVATING] = UNIT_ACTIVATING,
+        [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
         [SWAP_ACTIVE] = UNIT_ACTIVE,
         [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
         [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
@@ -57,8 +59,8 @@ static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userd
 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
 
 static void swap_unset_proc_swaps(Swap *s) {
-        Swap *first;
         Hashmap *swaps;
+        Swap *first;
 
         assert(s);
 
@@ -72,15 +74,14 @@ static void swap_unset_proc_swaps(Swap *s) {
         LIST_REMOVE(same_proc_swaps, first, s);
 
         if (first)
-                hashmap_remove_and_replace(swaps,
-                                           s->parameters_proc_swaps.what,
-                                           first->parameters_proc_swaps.what,
-                                           first);
+                hashmap_remove_and_replace(swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first);
         else
                 hashmap_remove(swaps, s->parameters_proc_swaps.what);
 
         free(s->parameters_proc_swaps.what);
         s->parameters_proc_swaps.what = NULL;
+
+        s->from_proc_swaps = false;
 }
 
 static void swap_init(Unit *u) {
@@ -101,7 +102,7 @@ static void swap_init(Unit *u) {
 
         s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
 
-        UNIT(s)->ignore_on_isolate = true;
+        u->ignore_on_isolate = true;
 }
 
 static void swap_unwatch_control_pid(Swap *s) {
@@ -173,8 +174,7 @@ static int swap_add_device_links(Swap *s) {
                 return 0;
 
         if (is_device_path(s->what))
-                return unit_add_node_link(UNIT(s), s->what, !p->noauto &&
-                                          UNIT(s)->manager->running_as == SYSTEMD_SYSTEM);
+                return unit_add_node_link(UNIT(s), s->what, !p->noauto && UNIT(s)->manager->running_as == SYSTEMD_SYSTEM);
         else
                 /* File based swap devices need to be ordered after
                  * systemd-remount-fs.service, since they might need a
@@ -207,11 +207,9 @@ static int swap_add_default_dependencies(Swap *s) {
 
         if (!noauto) {
                 if (nofail)
-                        r = unit_add_dependency_by_name_inverse(UNIT(s),
-                                UNIT_WANTS, SPECIAL_SWAP_TARGET, NULL, true);
+                        r = unit_add_dependency_by_name_inverse(UNIT(s), UNIT_WANTS, SPECIAL_SWAP_TARGET, NULL, true);
                 else
-                        r = unit_add_two_dependencies_by_name_inverse(UNIT(s),
-                                UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SWAP_TARGET, NULL, true);
+                        r = unit_add_two_dependencies_by_name_inverse(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SWAP_TARGET, NULL, true);
                 if (r < 0)
                         return r;
         }
@@ -224,24 +222,20 @@ static int swap_verify(Swap *s) {
         _cleanup_free_ char *e = NULL;
 
         if (UNIT(s)->load_state != UNIT_LOADED)
-                  return 0;
+                return 0;
 
         e = unit_name_from_path(s->what, ".swap");
-        if (e == NULL)
+        if (!e)
                 return log_oom();
 
         b = unit_has_name(UNIT(s), e);
         if (!b) {
-                log_error_unit(UNIT(s)->id,
-                               "%s: Value of \"What\" and unit name do not match, not loading.",
-                               UNIT(s)->id);
+                log_error_unit(UNIT(s)->id, "%s: Value of \"What\" and unit name do not match, not loading.", UNIT(s)->id);
                 return -EINVAL;
         }
 
         if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
-                log_error_unit(UNIT(s)->id,
-                               "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.",
-                               UNIT(s)->id);
+                log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.", UNIT(s)->id);
                 return -EINVAL;
         }
 
@@ -282,9 +276,11 @@ static int swap_load(Unit *u) {
 
                 path_kill_slashes(s->what);
 
-                if (!UNIT(s)->description)
-                        if ((r = unit_set_description(u, s->what)) < 0)
+                if (!UNIT(s)->description) {
+                        r = unit_set_description(u, s->what);
+                        if (r < 0)
                                 return r;
+                }
 
                 r = unit_require_mounts_for(UNIT(s), s->what);
                 if (r < 0)
@@ -321,10 +317,9 @@ static int swap_add_one(
                 bool nofail,
                 bool set_flags) {
 
-        Unit *u = NULL;
         _cleanup_free_ char *e = NULL;
-        char *wp = NULL;
         bool delete = false;
+        Unit *u = NULL;
         int r;
         SwapParameters *p;
         Swap *first;
@@ -368,27 +363,20 @@ static int swap_add_one(
         p = &SWAP(u)->parameters_proc_swaps;
 
         if (!p->what) {
-                wp = strdup(what_proc_swaps);
-                if (!wp) {
-                        r = log_oom();
+                p->what = strdup(what_proc_swaps);
+                if (!p->what) {
+                        r = -ENOMEM;
                         goto fail;
                 }
 
-                if (!m->swaps_by_proc_swaps) {
-                        m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func);
-                        if (!m->swaps_by_proc_swaps) {
-                                r = log_oom();
-                                goto fail;
-                        }
-                }
-
-                free(p->what);
-                p->what = wp;
+                r = hashmap_ensure_allocated(&m->swaps_by_proc_swaps, string_hash_func, string_compare_func);
+                if (r < 0)
+                        goto fail;
 
-                first = hashmap_get(m->swaps_by_proc_swaps, wp);
+                first = hashmap_get(m->swaps_by_proc_swaps, p->what);
                 LIST_PREPEND(same_proc_swaps, first, SWAP(u));
 
-                r = hashmap_replace(m->swaps_by_proc_swaps, wp, first);
+                r = hashmap_replace(m->swaps_by_proc_swaps, p->what, first);
                 if (r < 0)
                         goto fail;
         }
@@ -411,8 +399,6 @@ static int swap_add_one(
 fail:
         log_warning_unit(e, "Failed to load swap unit: %s", strerror(-r));
 
-        free(wp);
-
         if (delete && u)
                 unit_free(u);
 
@@ -420,56 +406,54 @@ fail:
 }
 
 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
+        _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+        struct udev_list_entry *item = NULL, *first = NULL;
+        const char *dn;
         struct stat st;
-        int r = 0, k;
+        int r;
 
         assert(m);
 
-        if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
-                struct udev_device *d;
-                const char *dn;
-                struct udev_list_entry *item = NULL, *first = NULL;
+        r = swap_add_one(m, device, device, prio, false, false, set_flags);
+        if (r < 0)
+                return r;
 
-                /* So this is a proper swap device. Create swap units
-                 * for all names this swap device is known under */
+        /* If this is a block device, then let's add duplicates for
+         * all other names of this block device */
+        if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
+                return 0;
 
-                d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev);
-                if (!d)
-                        return log_oom();
+        d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev);
+        if (!d)
+                return 0;
 
-                dn = udev_device_get_devnode(d);
-                /* Skip dn==device, since that case will be handled below */
-                if (dn && !streq(dn, device))
-                        r = swap_add_one(m, dn, device, prio, false, false, set_flags);
+        /* Add the main device node */
+        dn = udev_device_get_devnode(d);
+        if (dn && !streq(dn, device))
+                swap_add_one(m, dn, device, prio, false, false, set_flags);
 
-                /* Add additional units for all symlinks */
-                first = udev_device_get_devlinks_list_entry(d);
-                udev_list_entry_foreach(item, first) {
-                        const char *p;
+        /* Add additional units for all symlinks */
+        first = udev_device_get_devlinks_list_entry(d);
+        udev_list_entry_foreach(item, first) {
+                const char *p;
 
-                        /* Don't bother with the /dev/block links */
-                        p = udev_list_entry_get_name(item);
+                /* Don't bother with the /dev/block links */
+                p = udev_list_entry_get_name(item);
 
-                        if (path_startswith(p, "/dev/block/"))
-                                continue;
+                if (streq(p, device))
+                        continue;
 
-                        if (stat(p, &st) >= 0)
-                                if ((!S_ISBLK(st.st_mode)) ||
-                                    st.st_rdev != udev_device_get_devnum(d))
-                                        continue;
+                if (path_startswith(p, "/dev/block/"))
+                        continue;
 
-                        k = swap_add_one(m, p, device, prio, false, false, set_flags);
-                        if (k < 0)
-                                r = k;
-                }
+                if (stat(p, &st) >= 0)
+                        if (!S_ISBLK(st.st_mode) ||
+                            st.st_rdev != udev_device_get_devnum(d))
+                                continue;
 
-                udev_device_unref(d);
+                swap_add_one(m, p, device, prio, false, false, set_flags);
         }
 
-        k = swap_add_one(m, device, device, prio, false, false, set_flags);
-        if (k < 0)
-                r = k;
-
         return r;
 }
 
@@ -484,6 +468,7 @@ static void swap_set_state(Swap *s, SwapState state) {
         if (state != SWAP_ACTIVATING &&
             state != SWAP_ACTIVATING_SIGTERM &&
             state != SWAP_ACTIVATING_SIGKILL &&
+            state != SWAP_ACTIVATING_DONE &&
             state != SWAP_DEACTIVATING &&
             state != SWAP_DEACTIVATING_SIGTERM &&
             state != SWAP_DEACTIVATING_SIGKILL) {
@@ -500,8 +485,7 @@ static void swap_set_state(Swap *s, SwapState state) {
                                swap_state_to_string(old_state),
                                swap_state_to_string(state));
 
-        unit_notify(UNIT(s), state_translation_table[old_state],
-                    state_translation_table[state], true);
+        unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
 }
 
 static int swap_coldplug(Unit *u) {
@@ -517,30 +501,30 @@ static int swap_coldplug(Unit *u) {
         else if (s->from_proc_swaps)
                 new_state = SWAP_ACTIVE;
 
-        if (new_state != s->state) {
-
-                if (new_state == SWAP_ACTIVATING ||
-                    new_state == SWAP_ACTIVATING_SIGTERM ||
-                    new_state == SWAP_ACTIVATING_SIGKILL ||
-                    new_state == SWAP_DEACTIVATING ||
-                    new_state == SWAP_DEACTIVATING_SIGTERM ||
-                    new_state == SWAP_DEACTIVATING_SIGKILL) {
+        if (new_state == s->state)
+                return 0;
 
-                        if (s->control_pid <= 0)
-                                return -EBADMSG;
+        if (new_state == SWAP_ACTIVATING ||
+            new_state == SWAP_ACTIVATING_SIGTERM ||
+            new_state == SWAP_ACTIVATING_SIGKILL ||
+            new_state == SWAP_ACTIVATING_DONE ||
+            new_state == SWAP_DEACTIVATING ||
+            new_state == SWAP_DEACTIVATING_SIGTERM ||
+            new_state == SWAP_DEACTIVATING_SIGKILL) {
 
-                        r = unit_watch_pid(UNIT(s), s->control_pid);
-                        if (r < 0)
-                                return r;
+                if (s->control_pid <= 0)
+                        return -EBADMSG;
 
-                        r = swap_arm_timer(s);
-                        if (r < 0)
-                                return r;
-                }
+                r = unit_watch_pid(UNIT(s), s->control_pid);
+                if (r < 0)
+                        return r;
 
-                swap_set_state(s, new_state);
+                r = swap_arm_timer(s);
+                if (r < 0)
+                        return r;
         }
 
+        swap_set_state(s, new_state);
         return 0;
 }
 
@@ -703,10 +687,9 @@ static void swap_enter_activating(Swap *s) {
                 priority = -1;
 
         if (priority >= 0) {
-                char p[LINE_MAX];
+                char p[DECIMAL_STR_MAX(int)];
 
-                snprintf(p, sizeof(p), "%i", priority);
-                char_array_0(p);
+                sprintf(p, "%i", priority);
 
                 r = exec_command_set(
                                 s->control_command,
@@ -815,6 +798,7 @@ static int swap_stop(Unit *u) {
                 return 0;
 
         assert(s->state == SWAP_ACTIVATING ||
+               s->state == SWAP_ACTIVATING_DONE ||
                s->state == SWAP_ACTIVE);
 
         if (detect_container(NULL) > 0)
@@ -968,6 +952,7 @@ static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         switch (s->state) {
 
         case SWAP_ACTIVATING:
+        case SWAP_ACTIVATING_DONE:
         case SWAP_ACTIVATING_SIGTERM:
         case SWAP_ACTIVATING_SIGKILL:
 
@@ -993,10 +978,6 @@ static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
 
         /* Notify clients about changed exit status */
         unit_add_to_dbus_queue(u);
-
-        /* Request a reload of /proc/swaps, so that following units
-         * can follow our state change */
-        u->manager->request_reload = true;
 }
 
 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
@@ -1008,6 +989,7 @@ static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userd
         switch (s->state) {
 
         case SWAP_ACTIVATING:
+        case SWAP_ACTIVATING_DONE:
                 log_warning_unit(UNIT(s)->id, "%s activation timed out. Stopping.", UNIT(s)->id);
                 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
                 break;
@@ -1121,7 +1103,6 @@ static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, v
                 if (!swap->is_active) {
                         /* This has just been deactivated */
 
-                        swap->from_proc_swaps = false;
                         swap_unset_proc_swaps(swap);
 
                         switch (swap->state) {
@@ -1131,6 +1112,7 @@ static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, v
                                 break;
 
                         default:
+                                /* Fire again */
                                 swap_set_state(swap, swap->state);
                                 break;
                         }
@@ -1146,6 +1128,10 @@ static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, v
                                 swap_enter_active(swap, SWAP_SUCCESS);
                                 break;
 
+                        case SWAP_ACTIVATING:
+                                swap_set_state(swap, SWAP_ACTIVATING_DONE);
+                                break;
+
                         default:
                                 /* Nothing really changed, but let's
                                  * issue an notification call
@@ -1190,8 +1176,7 @@ static Unit *swap_following(Unit *u) {
 }
 
 static int swap_following_set(Unit *u, Set **_set) {
-        Swap *s = SWAP(u);
-        Swap *other;
+        Swap *s = SWAP(u), *other;
         Set *set;
         int r;
 
@@ -1203,16 +1188,21 @@ static int swap_following_set(Unit *u, Set **_set) {
                 return 0;
         }
 
-        if (!(set = set_new(NULL, NULL)))
+        set = set_new(NULL, NULL);
+        if (!set)
                 return -ENOMEM;
 
-        LIST_FOREACH_AFTER(same_proc_swaps, other, s)
-                if ((r = set_put(set, other)) < 0)
+        LIST_FOREACH_AFTER(same_proc_swaps, other, s) {
+                r = set_put(set, other);
+                if (r < 0)
                         goto fail;
+        }
 
-        LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
-                if ((r = set_put(set, other)) < 0)
+        LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
+                r = set_put(set, other);
+                if (r < 0)
                         goto fail;
+        }
 
         *_set = set;
         return 1;
@@ -1279,6 +1269,7 @@ static int swap_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
         [SWAP_DEAD] = "dead",
         [SWAP_ACTIVATING] = "activating",
+        [SWAP_ACTIVATING_DONE] = "activating-done",
         [SWAP_ACTIVE] = "active",
         [SWAP_DEACTIVATING] = "deactivating",
         [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
diff --git a/src/core/swap.h b/src/core/swap.h
index c51c55f..313a195 100644
--- a/src/core/swap.h
+++ b/src/core/swap.h
@@ -28,7 +28,8 @@ typedef struct Swap Swap;
 
 typedef enum SwapState {
         SWAP_DEAD,
-        SWAP_ACTIVATING,
+        SWAP_ACTIVATING,               /* /sbin/swapon is running, but the swap not yet enabled. */
+        SWAP_ACTIVATING_DONE,          /* /sbin/swapon is running, and the swap is done. */
         SWAP_ACTIVE,
         SWAP_DEACTIVATING,
         SWAP_ACTIVATING_SIGTERM,
@@ -47,13 +48,6 @@ typedef enum SwapExecCommand {
         _SWAP_EXEC_COMMAND_INVALID = -1
 } SwapExecCommand;
 
-typedef struct SwapParameters {
-        char *what;
-        int priority;
-        bool noauto:1;
-        bool nofail:1;
-} SwapParameters;
-
 typedef enum SwapResult {
         SWAP_SUCCESS,
         SWAP_FAILURE_RESOURCES,
@@ -65,6 +59,13 @@ typedef enum SwapResult {
         _SWAP_RESULT_INVALID = -1
 } SwapResult;
 
+typedef struct SwapParameters {
+        char *what;
+        int priority;
+        bool noauto:1;
+        bool nofail:1;
+} SwapParameters;
+
 struct Swap {
         Unit meta;
 
diff --git a/src/shared/macro.h b/src/shared/macro.h
index d64e3c3..6caecab 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -277,7 +277,7 @@ do {                                                                    \
  * specified type as a decimal string. Adds in extra space for a
  * negative '-' prefix. */
 #define DECIMAL_STR_MAX(type)                                           \
-        (1+(sizeof(type) <= 1 ? 3 :                                     \
+        (2+(sizeof(type) <= 1 ? 3 :                                     \
             sizeof(type) <= 2 ? 5 :                                     \
             sizeof(type) <= 4 ? 10 :                                    \
             sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))

commit f1421cc67dba47f0fdb1c963cf65115c8a8e72f0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 15:25:01 2013 +0100

    device: modernizations

diff --git a/src/core/device.c b/src/core/device.c
index 91e37e0..4ed06a6 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -41,6 +41,7 @@ static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
 
 static void device_unset_sysfs(Device *d) {
+        Hashmap *devices;
         Device *first;
 
         assert(d);
@@ -50,13 +51,14 @@ static void device_unset_sysfs(Device *d) {
 
         /* Remove this unit from the chain of devices which share the
          * same sysfs path. */
-        first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, d->sysfs);
+        devices = UNIT(d)->manager->devices_by_sysfs;
+        first = hashmap_get(devices, d->sysfs);
         LIST_REMOVE(same_sysfs, first, d);
 
         if (first)
-                hashmap_remove_and_replace(UNIT(d)->manager->devices_by_sysfs, d->sysfs, first->sysfs, first);
+                hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first);
         else
-                hashmap_remove(UNIT(d)->manager->devices_by_sysfs, d->sysfs);
+                hashmap_remove(devices, d->sysfs);
 
         free(d->sysfs);
         d->sysfs = NULL;
@@ -73,10 +75,10 @@ static void device_init(Unit *u) {
          * indefinitely for plugged in devices, something which cannot
          * happen for the other units since their operations time out
          * anyway. */
-        UNIT(d)->job_timeout = u->manager->default_timeout_start_usec;
+        u->job_timeout = u->manager->default_timeout_start_usec;
 
-        UNIT(d)->ignore_on_isolate = true;
-        UNIT(d)->ignore_on_snapshot = true;
+        u->ignore_on_isolate = true;
+        u->ignore_on_snapshot = true;
 }
 
 static void device_done(Unit *u) {
@@ -190,7 +192,7 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
 
         sysfs = udev_device_get_syspath(dev);
         if (!sysfs)
-                return -ENOMEM;
+                return 0;
 
         r = device_find_escape_name(m, path, &u);
         if (r < 0)
@@ -204,7 +206,7 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
 
                 u = unit_new(m, sizeof(Device));
                 if (!u)
-                        return -ENOMEM;
+                        return log_oom();
 
                 r = device_add_escaped_name(u, path);
                 if (r < 0)
@@ -248,30 +250,10 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
                 goto fail;
 
         if (main) {
+                const char *wants;
+
                 /* The additional systemd udev properties we only
                  * interpret for the main object */
-                const char *wants, *alias;
-
-                alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
-                if (alias) {
-                        char *state, *w;
-                        size_t l;
-
-                        FOREACH_WORD_QUOTED(w, l, alias, state) {
-                                _cleanup_free_ char *e;
-
-                                e = strndup(w, l);
-                                if (!e) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
-
-                                if (is_path(e))
-                                        device_update_unit(m, dev, e, false);
-                                else
-                                        log_warning("SYSTEMD_ALIAS for %s is not a path, ignoring: %s", sysfs, e);
-                        }
-                }
 
                 wants = udev_device_get_property_value(dev, m->running_as == SYSTEMD_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS");
                 if (wants) {
@@ -279,13 +261,11 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
                         size_t l;
 
                         FOREACH_WORD_QUOTED(w, l, wants, state) {
-                                _cleanup_free_ char *e, *n = NULL;
+                                _cleanup_free_ char *n = NULL;
+                                char e[l+1];
 
-                                e = strndup(w, l);
-                                if (!e) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
+                                memcpy(e, w, l);
+                                e[l] = 1;
 
                                 n = unit_name_mangle(e);
                                 if (!n) {
@@ -300,6 +280,9 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
                 }
         }
 
+        /* Note that this won't dispatch the load queue, the caller
+         * has to do that if needed and appropriate */
+
         unit_add_to_dbus_queue(u);
         return 0;
 
@@ -312,8 +295,8 @@ fail:
         return r;
 }
 
-static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
-        const char *sysfs, *dn;
+static int device_process_new_device(Manager *m, struct udev_device *dev) {
+        const char *sysfs, *dn, *alias;
         struct udev_list_entry *item = NULL, *first = NULL;
         int r;
 
@@ -321,7 +304,7 @@ static int device_process_new_device(Manager *m, struct udev_device *dev, bool u
 
         sysfs = udev_device_get_syspath(dev);
         if (!sysfs)
-                return -ENOMEM;
+                return 0;
 
         /* Add the main unit named after the sysfs path */
         r = device_update_unit(m, dev, sysfs, true);
@@ -329,7 +312,8 @@ static int device_process_new_device(Manager *m, struct udev_device *dev, bool u
                 return r;
 
         /* Add an additional unit for the device node */
-        if ((dn = udev_device_get_devnode(dev)))
+        dn = udev_device_get_devnode(dev);
+        if (dn)
                 device_update_unit(m, dev, dn, false);
 
         /* Add additional units for all symlinks */
@@ -360,35 +344,43 @@ static int device_process_new_device(Manager *m, struct udev_device *dev, bool u
                 device_update_unit(m, dev, p, false);
         }
 
-        if (update_state) {
-                Device *d, *l;
+        /* Add additional units for all explicitly configured
+         * aliases */
+        alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
+        if (alias) {
+                char *state, *w;
+                size_t l;
 
-                manager_dispatch_load_queue(m);
+                FOREACH_WORD_QUOTED(w, l, alias, state) {
+                        char e[l+1];
 
-                l = hashmap_get(m->devices_by_sysfs, sysfs);
-                LIST_FOREACH(same_sysfs, d, l)
-                        device_set_state(d, DEVICE_PLUGGED);
+                        memcpy(e, w, l);
+                        e[l] = 0;
+
+                        if (path_is_absolute(e))
+                                device_update_unit(m, dev, e, false);
+                        else
+                                log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, e);
+                }
         }
 
         return 0;
 }
 
-static int device_process_path(Manager *m, const char *path, bool update_state) {
-        int r;
-        struct udev_device *dev;
+static void device_set_path_plugged(Manager *m, struct udev_device *dev) {
+        const char *sysfs;
+        Device *d, *l;
 
         assert(m);
-        assert(path);
+        assert(dev);
 
-        dev = udev_device_new_from_syspath(m->udev, path);
-        if (!dev) {
-                log_warning("Failed to get udev device object from udev for path %s.", path);
-                return -ENOMEM;
-        }
+        sysfs = udev_device_get_syspath(dev);
+        if (!sysfs)
+                return;
 
-        r = device_process_new_device(m, dev, update_state);
-        udev_device_unref(dev);
-        return r;
+        l = hashmap_get(m->devices_by_sysfs, sysfs);
+        LIST_FOREACH(same_sysfs, d, l)
+                device_set_state(d, DEVICE_PLUGGED);
 }
 
 static int device_process_removed_device(Manager *m, struct udev_device *dev) {
@@ -398,7 +390,8 @@ static int device_process_removed_device(Manager *m, struct udev_device *dev) {
         assert(m);
         assert(dev);
 
-        if (!(sysfs = udev_device_get_syspath(dev)))
+        sysfs = udev_device_get_syspath(dev);
+        if (!sysfs)
                 return -ENOMEM;
 
         /* Remove all units of this sysfs path */
@@ -410,6 +403,34 @@ static int device_process_removed_device(Manager *m, struct udev_device *dev) {
         return 0;
 }
 
+static bool device_is_ready(struct udev_device *dev) {
+        const char *ready;
+
+        assert(dev);
+
+        ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
+        if (!ready)
+                return true;
+
+        return parse_boolean(ready) != 0;
+}
+
+static int device_process_new_path(Manager *m, const char *path) {
+        _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
+
+        assert(m);
+        assert(path);
+
+        dev = udev_device_new_from_syspath(m->udev, path);
+        if (!dev)
+                return log_oom();
+
+        if (!device_is_ready(dev))
+                return 0;
+
+        return device_process_new_device(m, dev);
+}
+
 static Unit *device_following(Unit *u) {
         Device *d = DEVICE(u);
         Device *other, *first = NULL;
@@ -434,36 +455,40 @@ static Unit *device_following(Unit *u) {
         return UNIT(first);
 }
 
-static int device_following_set(Unit *u, Set **_s) {
-        Device *d = DEVICE(u);
-        Device *other;
-        Set *s;
+static int device_following_set(Unit *u, Set **_set) {
+        Device *d = DEVICE(u), *other;
+        Set *set;
         int r;
 
         assert(d);
-        assert(_s);
+        assert(_set);
 
-        if (!d->same_sysfs_prev && !d->same_sysfs_next) {
-                *_s = NULL;
+        if (LIST_JUST_US(same_sysfs, d)) {
+                *_set = NULL;
                 return 0;
         }
 
-        if (!(s = set_new(NULL, NULL)))
+        set = set_new(NULL, NULL);
+        if (!set)
                 return -ENOMEM;
 
-        for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
-                if ((r = set_put(s, other)) < 0)
+        LIST_FOREACH_AFTER(same_sysfs, other, d) {
+                r = set_put(set, other);
+                if (r < 0)
                         goto fail;
+        }
 
-        for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev)
-                if ((r = set_put(s, other)) < 0)
+        LIST_FOREACH_BEFORE(same_sysfs, other, d) {
+                r = set_put(set, other);
+                if (r < 0)
                         goto fail;
+        }
 
-        *_s = s;
+        *_set = set;
         return 1;
 
 fail:
-        set_free(s);
+        set_free(set);
         return r;
 }
 
@@ -538,7 +563,7 @@ static int device_enumerate(Manager *m) {
 
         first = udev_enumerate_get_list_entry(e);
         udev_list_entry_foreach(item, first)
-                device_process_path(m, udev_list_entry_get_name(item), false);
+                device_process_new_path(m, udev_list_entry_get_name(item));
 
         return 0;
 
@@ -549,8 +574,8 @@ fail:
 
 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
         _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
-        const char *action, *ready;
         Manager *m = userdata;
+        const char *action;
         int r;
 
         assert(m);
@@ -578,16 +603,18 @@ static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
                 return 0;
         }
 
-        ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
-
-        if (streq(action, "remove") || (ready && parse_boolean(ready) == 0)) {
+        if (streq(action, "remove") || !device_is_ready(dev))  {
                 r = device_process_removed_device(m, dev);
                 if (r < 0)
-                        log_error("Failed to process udev device event: %s", strerror(-r));
+                        log_error("Failed to process device remove event: %s", strerror(-r));
         } else {
-                r = device_process_new_device(m, dev, true);
+                r = device_process_new_device(m, dev);
                 if (r < 0)
-                        log_error("Failed to process udev device event: %s", strerror(-r));
+                        log_error("Failed to process device new event: %s", strerror(-r));
+
+                manager_dispatch_load_queue(m);
+
+                device_set_path_plugged(m, dev);
         }
 
         return 0;

commit 752b590500cdfe8e60800c0553bf7ae3ac613ffd
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 15:22:41 2013 +0100

    core: dispatch run queue only if there's nothing else to do
    
    Always read all external events before we decide what we do next.

diff --git a/src/core/job.c b/src/core/job.c
index f791299..9cd6ce2 100644
--- a/src/core/job.c
+++ b/src/core/job.c
@@ -874,6 +874,9 @@ void job_add_to_run_queue(Job *j) {
         if (j->in_run_queue)
                 return;
 
+        if (!j->manager->run_queue)
+                sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT);
+
         LIST_PREPEND(run_queue, j->manager->run_queue, j);
         j->in_run_queue = true;
 }
diff --git a/src/core/manager.c b/src/core/manager.c
index b50182b..c424fec 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -95,6 +95,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
 static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
 static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
 static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
+static int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
 
 static int manager_setup_notify(Manager *m) {
         union {
@@ -456,6 +457,18 @@ int manager_new(SystemdRunningAs running_as, bool reexecuting, Manager **_m) {
         if (r < 0)
                 goto fail;
 
+        r = sd_event_add_defer(m->event, manager_dispatch_run_queue, m, &m->run_queue_event_source);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_source_set_priority(m->run_queue_event_source, SD_EVENT_PRIORITY_IDLE);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF);
+        if (r < 0)
+                goto fail;
+
         r = manager_setup_signals(m);
         if (r < 0)
                 goto fail;
@@ -662,6 +675,7 @@ void manager_free(Manager *m) {
         sd_event_source_unref(m->time_change_event_source);
         sd_event_source_unref(m->jobs_in_progress_event_source);
         sd_event_source_unref(m->idle_pipe_event_source);
+        sd_event_source_unref(m->run_queue_event_source);
 
         if (m->signal_fd >= 0)
                 close_nointr_nofail(m->signal_fd);
@@ -1119,32 +1133,27 @@ void manager_clear_jobs(Manager *m) {
                 job_finish_and_invalidate(j, JOB_CANCELED, false);
 }
 
-static unsigned manager_dispatch_run_queue(Manager *m) {
+static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) {
+        Manager *m = userdata;
         Job *j;
-        unsigned n = 0;
-
-        if (m->dispatching_run_queue)
-                return 0;
 
-        m->dispatching_run_queue = true;
+        assert(source);
+        assert(m);
 
         while ((j = m->run_queue)) {
                 assert(j->installed);
                 assert(j->in_run_queue);
 
                 job_run_and_invalidate(j);
-                n++;
         }
 
-        m->dispatching_run_queue = false;
-
         if (m->n_running_jobs > 0)
                 manager_watch_jobs_in_progress(m);
 
         if (m->n_on_console > 0)
                 manager_watch_idle_pipe(m);
 
-        return n;
+        return 1;
 }
 
 static unsigned manager_dispatch_dbus_queue(Manager *m) {
@@ -1679,9 +1688,6 @@ int manager_loop(Manager *m) {
                 if (manager_dispatch_cgroup_queue(m) > 0)
                         continue;
 
-                if (manager_dispatch_run_queue(m) > 0)
-                        continue;
-
                 if (manager_dispatch_dbus_queue(m) > 0)
                         continue;
 
diff --git a/src/core/manager.h b/src/core/manager.h
index 9eddef2..a46b09e 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -97,6 +97,8 @@ struct Manager {
 
         Hashmap *watch_pids;  /* pid => Unit object n:1 */
 
+        sd_event_source *run_queue_event_source;
+
         char *notify_socket;
         int notify_fd;
         sd_event_source *notify_event_source;
@@ -192,7 +194,6 @@ struct Manager {
         ManagerExitCode exit_code:5;
 
         bool dispatching_load_queue:1;
-        bool dispatching_run_queue:1;
         bool dispatching_dbus_queue:1;
 
         bool taint_usr:1;

commit 844de4355592259eebcffddfc38c63266e451cb6
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Nov 25 15:20:59 2013 +0100

    cgroups-agent: down-grade log level

diff --git a/src/cgroups-agent/cgroups-agent.c b/src/cgroups-agent/cgroups-agent.c
index d8ae55a..d1d843b 100644
--- a/src/cgroups-agent/cgroups-agent.c
+++ b/src/cgroups-agent/cgroups-agent.c
@@ -22,7 +22,6 @@
 #include <stdlib.h>
 
 #include "sd-bus.h"
-
 #include "log.h"
 #include "bus-util.h"
 
@@ -46,7 +45,10 @@ int main(int argc, char *argv[]) {
 
         r = bus_open_system_systemd(&bus);
         if (r < 0) {
-                log_warning("Failed to get D-Bus connection: %s", strerror(-r));
+                /* If we couldn't connect we assume this was triggered
+                 * while systemd got restarted/transitioned from
+                 * initrd to the system, so let's ignore this */
+                log_debug("Failed to get D-Bus connection: %s", strerror(-r));
                 return EXIT_FAILURE;
         }
 
@@ -56,7 +58,7 @@ int main(int argc, char *argv[]) {
                                "Released",
                                "s", argv[1]);
         if (r < 0) {
-                log_error("Failed to send signal message on private connection: %s", strerror(-r));
+                log_debug("Failed to send signal message on private connection: %s", strerror(-r));
                 return EXIT_FAILURE;
         }
 



More information about the systemd-commits mailing list