X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=device.c;h=34d760a48a55930982958aa3a129cf8c3de86e31;hp=6d7e2c6fa3d51654c391c2b99ab89b79a764b60c;hb=f50e0a012340fa8dfe6ec7f0cd869f5f3a052d7a;hpb=b08d03ffe58332f590aae5c78a85e4fc0b8588ce diff --git a/device.c b/device.c index 6d7e2c6fa..34d760a48 100644 --- a/device.c +++ b/device.c @@ -8,6 +8,16 @@ #include "strv.h" #include "log.h" +static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = { + [DEVICE_DEAD] = UNIT_INACTIVE, + [DEVICE_AVAILABLE] = UNIT_ACTIVE +}; + +static const char* const state_string_table[_DEVICE_STATE_MAX] = { + [DEVICE_DEAD] = "dead", + [DEVICE_AVAILABLE] = "available" +}; + static void device_done(Unit *u) { Device *d = DEVICE(u); @@ -15,13 +25,31 @@ static void device_done(Unit *u) { free(d->sysfs); } -static void device_dump(Unit *u, FILE *f, const char *prefix) { +static void device_set_state(Device *d, DeviceState state) { + DeviceState old_state; + assert(d); - static const char* const state_table[_DEVICE_STATE_MAX] = { - [DEVICE_DEAD] = "dead", - [DEVICE_AVAILABLE] = "available" - }; + 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]); + + unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state]); +} + +static int device_coldplug(Unit *u) { + Device *d = DEVICE(u); + + assert(d); + assert(d->state == DEVICE_DEAD); + + if (d->sysfs) + device_set_state(d, DEVICE_AVAILABLE); + + return 0; +} + +static void device_dump(Unit *u, FILE *f, const char *prefix) { Device *d = DEVICE(u); assert(d); @@ -29,8 +57,14 @@ static void device_dump(Unit *u, FILE *f, const char *prefix) { fprintf(f, "%sDevice State: %s\n" "%sSysfs Path: %s\n", - prefix, state_table[d->state], - prefix, d->sysfs); + prefix, state_string_table[d->state], + prefix, strna(d->sysfs)); +} + +static UnitActiveState device_active_state(Unit *u) { + assert(u); + + return state_translation_table[DEVICE(u)->state]; } static int device_add_escaped_name(Unit *u, const char *prefix, const char *dn, bool make_id) { @@ -108,11 +142,9 @@ static int device_process_device(Manager *m, struct udev_device *dev) { } if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) || - (model = udev_device_get_property_value(dev, "ID_MODEL"))) - if (!(u->meta.description = strdup(model))) { - r = -ENOMEM; + (model = udev_device_get_property_value(dev, "ID_MODEL"))) + if ((r = unit_set_description(u, model)) < 0) goto fail; - } } else { delete = false; @@ -229,19 +261,17 @@ fail: return r; } -static UnitActiveState device_active_state(Unit *u) { - return DEVICE(u)->state == DEVICE_DEAD ? UNIT_INACTIVE : UNIT_ACTIVE; -} - const UnitVTable device_vtable = { .suffix = ".device", .init = unit_load_fragment_and_dropin, .done = device_done, + .coldplug = device_coldplug, + .dump = device_dump, - .enumerate = device_enumerate, - .shutdown = device_shutdown, + .active_state = device_active_state, - .active_state = device_active_state + .enumerate = device_enumerate, + .shutdown = device_shutdown };