chiark / gitweb /
device: properly handle devices that are referenced before they show up
[elogind.git] / src / device.c
index 526e7143785b8d684e2b53faec475dcbc6459f80..a196a982a02c153582938ac78f3881329d8786b8 100644 (file)
@@ -40,22 +40,21 @@ static void device_unset_sysfs(Device *d) {
 
         assert(d);
 
-        if (d->sysfs) {
-                /* Remove this unit from the chain of devices which share the
-                 * same sysfs path. */
-                first = hashmap_get(d->meta.manager->devices_by_sysfs, d->sysfs);
-                LIST_REMOVE(Device, same_sysfs, first, d);
-
-                if (first)
-                        hashmap_remove_and_replace(d->meta.manager->devices_by_sysfs, d->sysfs, first->sysfs, first);
-                else
-                        hashmap_remove(d->meta.manager->devices_by_sysfs, d->sysfs);
-
-                free(d->sysfs);
-                d->sysfs = NULL;
-        }
+        if (!d->sysfs)
+                return;
+
+        /* Remove this unit from the chain of devices which share the
+         * same sysfs path. */
+        first = hashmap_get(d->meta.manager->devices_by_sysfs, d->sysfs);
+        LIST_REMOVE(Device, same_sysfs, first, d);
 
-        d->meta.following = NULL;
+        if (first)
+                hashmap_remove_and_replace(d->meta.manager->devices_by_sysfs, d->sysfs, first->sysfs, first);
+        else
+                hashmap_remove(d->meta.manager->devices_by_sysfs, d->sysfs);
+
+        free(d->sysfs);
+        d->sysfs = NULL;
 }
 
 static void device_init(Unit *u) {
@@ -196,7 +195,6 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
         }
 
         if (!u) {
-                Device *first;
                 delete = true;
 
                 if (!(u = unit_new(m)))
@@ -205,13 +203,22 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
                 if ((r = device_add_escaped_name(u, path)) < 0)
                         goto fail;
 
+                unit_add_to_load_queue(u);
+        } else
+                delete = false;
+
+        /* If this was created via some dependency and has not
+         * actually been seen yet ->sysfs will not be
+         * initialized. Hence initialize it if necessary. */
+
+        if (!DEVICE(u)->sysfs) {
+                Device *first;
+
                 if (!(DEVICE(u)->sysfs = strdup(sysfs))) {
                         r = -ENOMEM;
                         goto fail;
                 }
 
-                unit_add_to_load_queue(u);
-
                 if (!m->devices_by_sysfs)
                         if (!(m->devices_by_sysfs = hashmap_new(string_hash_func, string_compare_func))) {
                                 r = -ENOMEM;
@@ -223,9 +230,7 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
 
                 if ((r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first)) < 0)
                         goto fail;
-
-        } else
-                delete = false;
+        }
 
         if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
             (model = udev_device_get_property_value(dev, "ID_MODEL"))) {
@@ -268,10 +273,7 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p
                                         goto fail;
                         }
                 }
-
-                u->meta.following = NULL;
-        } else
-                device_find_escape_name(m, sysfs, &u->meta.following);
+        }
 
         unit_add_to_dbus_queue(u);
         return 0;
@@ -365,6 +367,30 @@ static int device_process_removed_device(Manager *m, struct udev_device *dev) {
         return 0;
 }
 
+static Unit *device_following(Unit *u) {
+        Device *d = DEVICE(u);
+        Device *other, *first = NULL;
+
+        assert(d);
+
+        if (startswith(u->meta.id, "sys-"))
+                return NULL;
+
+        /* Make everybody follow the unit that's named after the sysfs path */
+        for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
+                if (startswith(other->meta.id, "sys-"))
+                        return UNIT(other);
+
+        for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
+                if (startswith(other->meta.id, "sys-"))
+                        return UNIT(other);
+
+                first = other;
+        }
+
+        return UNIT(first);
+}
+
 static void device_shutdown(Manager *m) {
         assert(m);
 
@@ -512,6 +538,8 @@ const UnitVTable device_vtable = {
 
         .bus_message_handler = bus_device_message_handler,
 
+        .following = device_following,
+
         .enumerate = device_enumerate,
         .shutdown = device_shutdown
 };