chiark / gitweb /
Modernization
[elogind.git] / src / core / device.c
index f2bb656397a4aa85a20cc7b1517466272386523f..25af2cb2d59533c66c63686c6b75295dd8b1308a 100644 (file)
@@ -92,10 +92,10 @@ static void device_set_state(Device *d, DeviceState state) {
         d->state = state;
 
         if (state != old_state)
-                log_debug("%s changed %s -> %s",
-                          UNIT(d)->id,
-                          device_state_to_string(old_state),
-                          device_state_to_string(state));
+                log_debug_unit(UNIT(d)->id,
+                               "%s changed %s -> %s", UNIT(d)->id,
+                               device_state_to_string(old_state),
+                               device_state_to_string(state));
 
         unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true);
 }
@@ -124,13 +124,13 @@ static void device_dump(Unit *u, FILE *f, const char *prefix) {
                 prefix, strna(d->sysfs));
 }
 
-static UnitActiveState device_active_state(Unit *u) {
+_pure_ static UnitActiveState device_active_state(Unit *u) {
         assert(u);
 
         return state_translation_table[DEVICE(u)->state];
 }
 
-static const char *device_sub_state_to_string(Unit *u) {
+_pure_ static const char *device_sub_state_to_string(Unit *u) {
         assert(u);
 
         return device_state_to_string(DEVICE(u)->state);
@@ -144,7 +144,8 @@ static int device_add_escaped_name(Unit *u, const char *dn) {
         assert(dn);
         assert(dn[0] == '/');
 
-        if (!(e = unit_name_from_path(dn, ".device")))
+        e = unit_name_from_path(dn, ".device");
+        if (!e)
                 return -ENOMEM;
 
         r = unit_add_name(u, e);
@@ -165,7 +166,8 @@ static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
         assert(dn[0] == '/');
         assert(_u);
 
-        if (!(e = unit_name_from_path(dn, ".device")))
+        e = unit_name_from_path(dn, ".device");
+        if (!e)
                 return -ENOMEM;
 
         u = manager_get_unit(m, e);
@@ -187,10 +189,12 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
 
         assert(m);
 
-        if (!(sysfs = udev_device_get_syspath(dev)))
+        sysfs = udev_device_get_syspath(dev);
+        if (!sysfs)
                 return -ENOMEM;
 
-        if ((r = device_find_escape_name(m, path, &u)) < 0)
+        r = device_find_escape_name(m, path, &u);
+        if (r < 0)
                 return r;
 
         if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs))
@@ -232,47 +236,69 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
                 first = hashmap_get(m->devices_by_sysfs, sysfs);
                 LIST_PREPEND(Device, same_sysfs, first, DEVICE(u));
 
-                if ((r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first)) < 0)
+                r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first);
+                if (r < 0)
                         goto fail;
         }
 
         if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
             (model = udev_device_get_property_value(dev, "ID_MODEL"))) {
-                if ((r = unit_set_description(u, model)) < 0)
+                r = unit_set_description(u, model);
+                if (r < 0)
                         goto fail;
-        } else
-                if ((r = unit_set_description(u, path)) < 0)
+        } else {
+                r = unit_set_description(u, path);
+                if (r < 0)
                         goto fail;
+        }
 
         if (main) {
                 /* The additional systemd udev properties we only
                  * interpret for the main object */
                 const char *wants, *alias;
 
-                if ((alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS"))) {
-                        if (!is_path(alias))
-                                log_warning("SYSTEMD_ALIAS for %s is not a path, ignoring: %s", sysfs, alias);
-                        else {
-                                if ((r = device_add_escaped_name(u, alias)) < 0)
+                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);
                         }
                 }
 
-                if ((wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS"))) {
+                wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS");
+                if (wants) {
                         char *state, *w;
                         size_t l;
 
                         FOREACH_WORD_QUOTED(w, l, wants, state) {
-                                char *e;
+                                _cleanup_free_ char *e, *n = NULL;
 
-                                if (!(e = strndup(w, l))) {
+                                e = strndup(w, l);
+                                if (!e) {
                                         r = -ENOMEM;
                                         goto fail;
                                 }
 
-                                r = unit_add_dependency_by_name(u, UNIT_WANTS, e, NULL, true);
-                                free(e);
+                                n = unit_name_mangle(e);
+                                if (!n) {
+                                        r = -ENOMEM;
+                                        goto fail;
+                                }
 
+                                r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
                                 if (r < 0)
                                         goto fail;
                         }
@@ -294,6 +320,7 @@ fail:
 static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
         const char *sysfs, *dn;
         struct udev_list_entry *item = NULL, *first = NULL;
+        int r;
 
         assert(m);
 
@@ -301,7 +328,9 @@ static int device_process_new_device(Manager *m, struct udev_device *dev, bool u
                 return -ENOMEM;
 
         /* Add the main unit named after the sysfs path */
-        device_update_unit(m, dev, sysfs, true);
+        r = device_update_unit(m, dev, sysfs, true);
+        if (r < 0)
+                return r;
 
         /* Add an additional unit for the device node */
         if ((dn = udev_device_get_devnode(dev)))
@@ -459,7 +488,6 @@ static void device_shutdown(Manager *m) {
 }
 
 static int device_enumerate(Manager *m) {
-        struct epoll_event ev;
         int r;
         struct udev_enumerate *e = NULL;
         struct udev_list_entry *item = NULL, *first = NULL;
@@ -467,6 +495,8 @@ static int device_enumerate(Manager *m) {
         assert(m);
 
         if (!m->udev) {
+                struct epoll_event ev;
+
                 if (!(m->udev = udev_new()))
                         return -ENOMEM;
 
@@ -585,7 +615,6 @@ static const char* const device_state_table[_DEVICE_STATE_MAX] = {
 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
 
 const UnitVTable device_vtable = {
-        .suffix = ".device",
         .object_size = sizeof(Device),
         .sections =
                 "Unit\0"
@@ -613,5 +642,15 @@ const UnitVTable device_vtable = {
         .following_set = device_following_set,
 
         .enumerate = device_enumerate,
-        .shutdown = device_shutdown
+        .shutdown = device_shutdown,
+
+        .status_message_formats = {
+                .starting_stopping = {
+                        [0] = "Expecting device %s...",
+                },
+                .finished_start_job = {
+                        [JOB_DONE]       = "Found device %s.",
+                        [JOB_TIMEOUT]    = "Timed out waiting for device %s.",
+                },
+        },
 };