chiark / gitweb /
socket: optionally call accept() for incoming connections and spawn one service insta...
[elogind.git] / device.c
index 500cd23e2718c7e46ac3b3555330665ebc1f26c0..1a4edd29a79fda8757c6e79b89314d7b1f5e5419 100644 (file)
--- a/device.c
+++ b/device.c
@@ -27,6 +27,7 @@
 #include "device.h"
 #include "strv.h"
 #include "log.h"
+#include "unit-name.h"
 
 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
         [DEVICE_DEAD] = UNIT_INACTIVE,
@@ -42,20 +43,29 @@ static void device_done(Unit *u) {
         Device *d = DEVICE(u);
 
         assert(d);
+
         free(d->sysfs);
+        d->sysfs = NULL;
+}
+
+static void device_init(Unit *u) {
+        Device *d = DEVICE(u);
+
+        assert(d);
+        assert(u->meta.load_state == UNIT_STUB);
+
+        d->state = 0;
 }
 
 static void device_set_state(Device *d, DeviceState state) {
         DeviceState old_state;
         assert(d);
 
-        if (state == d->state)
-                return;
-
         old_state = d->state;
         d->state = state;
 
-        log_debug("%s changed %s → %s", unit_id(UNIT(d)), state_string_table[old_state], state_string_table[state]);
+        if (state != old_state)
+                log_debug("%s changed %s → %s", UNIT(d)->meta.id, state_string_table[old_state], state_string_table[state]);
 
         unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state]);
 }
@@ -90,6 +100,12 @@ static UnitActiveState device_active_state(Unit *u) {
         return state_translation_table[DEVICE(u)->state];
 }
 
+static const char *device_sub_state_to_string(Unit *u) {
+        assert(u);
+
+        return state_string_table[DEVICE(u)->state];
+}
+
 static int device_add_escaped_name(Unit *u, const char *dn, bool make_id) {
         char *e;
         int r;
@@ -98,7 +114,7 @@ static int device_add_escaped_name(Unit *u, const char *dn, bool make_id) {
         assert(dn);
         assert(dn[0] == '/');
 
-        if (!(e = unit_name_escape_path(dn+1, ".device")))
+        if (!(e = unit_name_build_escape(dn+1, NULL, ".device")))
                 return -ENOMEM;
 
         r = unit_add_name(u, e);
@@ -123,7 +139,7 @@ static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
         assert(dn[0] == '/');
         assert(_u);
 
-        if (!(e = unit_name_escape_path(dn+1, ".device")))
+        if (!(e = unit_name_build_escape(dn+1, NULL, ".device")))
                 return -ENOMEM;
 
         u = manager_get_unit(m, e);
@@ -137,6 +153,28 @@ static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
         return 0;
 }
 
+static bool devnode_is_api(const char *node) {
+        unsigned i;
+
+        static const char * const table[] = {
+                "/dev/null",
+                "/dev/zero",
+                "/dev/urandom",
+                "/dev/random",
+                "/dev/port",
+                "/dev/oldmem",
+                "/dev/full",
+                "/dev/kmsg",
+                "/dev/mem"
+        };
+
+        for (i = 0; i < ELEMENTSOF(table); i++)
+                if (streq(table[i], node))
+                        return true;
+
+        return false;
+}
+
 static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
         const char *dn, *names, *wants, *sysfs, *expose, *model;
         Unit *u = NULL;
@@ -168,7 +206,7 @@ static int device_process_new_device(Manager *m, struct udev_device *dev, bool u
                 if (!b)
                         return 0;
         } else
-                if (!dn && !names && !wants)
+                if ((!dn || devnode_is_api(dn)) && !names && !wants)
                         return 0;
 
         /* Ok, seems kinda interesting. Now, let's see if this one
@@ -209,15 +247,16 @@ static int device_process_new_device(Manager *m, struct udev_device *dev, bool u
                 if ((r = device_add_escaped_name(u, sysfs, true)) < 0)
                         goto fail;
 
+                unit_add_to_load_queue(u);
+        } else
+                delete = false;
+
+        if (!(DEVICE(u)->sysfs))
                 if (!(DEVICE(u)->sysfs = strdup(sysfs))) {
                         r = -ENOMEM;
                         goto fail;
                 }
 
-                unit_add_to_load_queue(u);
-        } else
-                delete = false;
-
         if (dn)
                 if ((r = device_add_escaped_name(u, dn, true)) < 0)
                         goto fail;
@@ -265,7 +304,7 @@ static int device_process_new_device(Manager *m, struct udev_device *dev, bool u
                                 goto fail;
                         }
 
-                        r = unit_add_dependency_by_name(u, UNIT_WANTS, e);
+                        r = unit_add_dependency_by_name(u, UNIT_WANTS, NULL, e);
                         free(e);
 
                         if (r < 0)
@@ -318,7 +357,7 @@ static int device_process_removed_device(Manager *m, struct udev_device *dev) {
                 return -ENOMEM;
 
         assert(sysfs[0] == '/');
-        if (!(e = unit_name_escape_path(sysfs+1, ".device")))
+        if (!(e = unit_name_build_escape(sysfs+1, NULL, ".device")))
                 return -ENOMEM;
 
         u = manager_get_unit(m, e);
@@ -438,13 +477,18 @@ fail:
 const UnitVTable device_vtable = {
         .suffix = ".device",
 
-        .init = unit_load_fragment_and_dropin_optional,
+        .no_requires = true,
+        .no_instances = true,
+
+        .init = device_init,
+        .load = unit_load_fragment_and_dropin_optional,
         .done = device_done,
         .coldplug = device_coldplug,
 
         .dump = device_dump,
 
         .active_state = device_active_state,
+        .sub_state_to_string = device_sub_state_to_string,
 
         .enumerate = device_enumerate,
         .shutdown = device_shutdown