device.c revision 07630cea1f3a845c09309f197ac7c4f11edd3b62
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include "libudev.h"
#include "dbus-device.h"
#include "log.h"
#include "path-util.h"
#include "string-util.h"
#include "swap.h"
#include "udev-util.h"
#include "unit-name.h"
#include "unit.h"
#include "device.h"
[DEVICE_DEAD] = UNIT_INACTIVE,
};
static void device_unset_sysfs(Device *d) {
assert(d);
if (!d->sysfs)
return;
/* Remove this unit from the chain of devices which share the
* same sysfs path. */
if (first)
else
}
char *copy;
int r;
assert(d);
return 0;
if (r < 0)
return r;
if (!copy)
return -ENOMEM;
if (r < 0) {
return r;
}
return 0;
}
static void device_init(Unit *u) {
assert(d);
/* In contrast to all other unit types we timeout jobs waiting
* for devices by default. This is because they otherwise wait
* indefinitely for plugged in devices, something which cannot
* happen for the other units since their operations time out
* anyway. */
u->ignore_on_isolate = true;
u->ignore_on_snapshot = true;
}
static void device_done(Unit *u) {
assert(d);
}
assert(d);
log_unit_debug(UNIT(d), "Changed %s -> %s", device_state_to_string(old_state), device_state_to_string(state));
}
static int device_coldplug(Unit *u) {
assert(d);
if (d->found & DEVICE_FOUND_UDEV)
/* If udev says the device is around, it's around */
* it's "tentatively" around. */
return 0;
}
assert(u);
assert(f);
return 0;
}
assert(u);
if (state < 0)
else
d->deserialized_state = state;
} else
return 0;
}
assert(d);
fprintf(f,
"%sDevice State: %s\n"
"%sSysfs Path: %s\n",
}
assert(u);
}
assert(u);
}
const char *model;
int r;
assert(u);
if (!model)
if (model) {
const char *label;
/* Try to concatenate the device model string with a label, if there is one */
if (!label)
if (!label)
if (label) {
_cleanup_free_ char *j;
if (j)
r = unit_set_description(u, j);
else
r = -ENOMEM;
} else
r = unit_set_description(u, model);
} else
r = unit_set_description(u, path);
if (r < 0)
log_unit_error_errno(u, r, "Failed to set device description: %m");
return r;
}
const char *wants;
size_t l;
int r;
const char *property;
assert(u);
if (!wants)
return 0;
_cleanup_free_ char *n = NULL;
char e[l+1];
e[l] = 0;
r = unit_name_mangle(e, UNIT_NAME_NOGLOB, &n);
if (r < 0)
return log_unit_error_errno(u, r, "Failed to mangle unit name: %m");
if (r < 0)
return log_unit_error_errno(u, r, "Failed to add wants dependency: %m");
}
log_unit_warning(u, "Property %s on %s has trailing garbage, ignoring.", property, strna(udev_device_get_syspath(dev)));
return 0;
}
_cleanup_free_ char *e = NULL;
bool delete;
int r;
assert(m);
if (dev) {
if (!sysfs)
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to generate unit name from device path: %m");
u = manager_get_unit(m, e);
if (u &&
sysfs &&
log_unit_debug(u, "Device %s appeared twice with different sysfs paths %s and %s", e, DEVICE(u)->sysfs, sysfs);
return -EEXIST;
}
if (!u) {
delete = true;
if (!u)
return log_oom();
r = unit_add_name(u, e);
if (r < 0)
goto fail;
} 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 (sysfs) {
if (r < 0)
goto fail;
/* The additional systemd udev properties we only interpret
* for the main object */
if (main)
(void) device_add_udev_wants(u, dev);
}
/* Note that this won't dispatch the load queue, the caller
* has to do that if needed and appropriate */
return 0;
fail:
log_unit_warning_errno(u, r, "Failed to set up device unit: %m");
if (delete)
unit_free(u);
return r;
}
int r;
assert(m);
if (!sysfs)
return 0;
/* Add the main unit named after the sysfs path */
if (r < 0)
return r;
/* Add an additional unit for the device node */
if (dn)
/* Add additional units for all symlinks */
const char *p;
p = udev_list_entry_get_name(item);
if (path_startswith(p, "/dev/block/") ||
path_startswith(p, "/dev/char/"))
continue;
/* Verify that the symlink in the FS actually belongs
* to this device. This is useful to deal with
* conflicting devices, e.g. when two disks want the
* the same label. We want to make sure that the same
* device that won the symlink wins in systemd, so we
continue;
(void) device_setup_unit(m, dev, p, false);
}
/* Add additional units for all explicitly configured
* aliases */
if (alias) {
size_t l;
char e[l+1];
e[l] = 0;
if (path_is_absolute(e))
(void) device_setup_unit(m, dev, e, false);
else
}
}
return 0;
}
DeviceFound n, previous;
assert(d);
if (n == d->found)
return;
d->found = n;
if (!now)
return;
if (d->found & DEVICE_FOUND_UDEV)
/* When the device is known to udev we consider it
* plugged. */
/* If the device has not been seen by udev yet, but is
* now referenced by the kernel, then we assume the
* kernel knows it now, and udev might soon too. */
else
/* If nobody sees the device, or if the device was
* previously seen by udev and now is only referenced
* from the kernel, then we consider the device is
* gone, the kernel just hasn't noticed it yet. */
}
static int device_update_found_by_sysfs(Manager *m, const char *sysfs, bool add, DeviceFound found, bool now) {
Device *d, *l;
assert(m);
if (found == DEVICE_NOT_FOUND)
return 0;
LIST_FOREACH(same_sysfs, d, l)
return 0;
}
static int device_update_found_by_name(Manager *m, const char *path, bool add, DeviceFound found, bool now) {
_cleanup_free_ char *e = NULL;
Unit *u;
int r;
assert(m);
if (found == DEVICE_NOT_FOUND)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to generate unit name from device path: %m");
u = manager_get_unit(m, e);
if (!u)
return 0;
return 0;
}
const char *ready;
if (!ready)
return true;
return parse_boolean(ready) != 0;
}
assert(d);
return NULL;
/* Make everybody follow the unit that's named after the sysfs path */
}
}
int r;
assert(d);
if (LIST_JUST_US(same_sysfs, d)) {
return 0;
}
if (!set)
return -ENOMEM;
if (r < 0)
goto fail;
}
if (r < 0)
goto fail;
}
return 1;
fail:
return r;
}
static void device_shutdown(Manager *m) {
assert(m);
if (m->udev_monitor) {
m->udev_monitor = NULL;
}
}
static int device_enumerate(Manager *m) {
int r;
assert(m);
if (!m->udev_monitor) {
if (!m->udev_monitor) {
r = -ENOMEM;
goto fail;
}
/* This will fail if we are unprivileged, but that
* should not matter much, as user instances won't run
* during boot. */
if (r < 0)
goto fail;
if (r < 0)
goto fail;
r = sd_event_add_io(m->event, &m->udev_event_source, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m);
if (r < 0)
goto fail;
}
e = udev_enumerate_new(m->udev);
if (!e) {
r = -ENOMEM;
goto fail;
}
r = udev_enumerate_add_match_tag(e, "systemd");
if (r < 0)
goto fail;
if (r < 0)
goto fail;
r = udev_enumerate_scan_devices(e);
if (r < 0)
goto fail;
const char *sysfs;
if (!dev) {
log_oom();
continue;
}
if (!device_is_ready(dev))
continue;
(void) device_process_new(m, dev);
}
return 0;
fail:
log_error_errno(r, "Failed to enumerate devices: %m");
device_shutdown(m);
return r;
}
int r;
assert(m);
if (!ratelimit_test(&limit))
return 0;
}
/*
* libudev might filter-out devices which pass the bloom
* filter, so getting NULL here is not necessarily an error.
*/
if (!dev)
return 0;
if (!sysfs) {
log_error("Failed to get udev sys path.");
return 0;
}
if (!action) {
log_error("Failed to get udev action string.");
return 0;
}
r = swap_process_device_remove(m, dev);
if (r < 0)
log_error_errno(r, "Failed to process swap device remove event: %m");
/* If we get notified that a device was removed by
* udev, then it's completely gone, hence unset all
* found bits */
device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP, true);
} else if (device_is_ready(dev)) {
(void) device_process_new(m, dev);
r = swap_process_device_new(m, dev);
if (r < 0)
log_error_errno(r, "Failed to process swap device new event: %m");
/* The device is found now, set the udev found bit */
} else {
/* The device is nominally around, but not ready for
* us. Hence unset the udev bit, but leave the rest
* around. */
}
return 0;
}
static bool device_supported(void) {
static int read_only = -1;
/* If /sys is read-only we don't support device units, and any
* attempts to start one should fail immediately. */
if (read_only < 0)
return read_only <= 0;
}
assert(m);
if (!device_supported())
return 0;
/* This is called whenever we find a device referenced in
* probing it yet, and we thus haven't learned about it
* yet. In this case we will set the device unit to
* "tentative" state. */
if (add) {
return 0;
/* We make an extra check here, if the device node
* actually exists. If it's missing, then this is an
* indication that device was unplugged but is still
* really cover all cases where a device might be gone
* away, since drives that can have a medium inserted
* will still have a device node even when the medium
* is not there... */
return 0;
return log_error_errno(errno, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
/* If the device is known in the kernel and newly
* appeared, then we'll create a device unit for it,
}
/* Update the device unit's state, should it exist */
}
const UnitVTable device_vtable = {
.object_size = sizeof(Device),
.sections =
"Unit\0"
"Device\0"
"Install\0",
.no_instances = true,
.init = device_init,
.done = device_done,
.dump = device_dump,
.starting_stopping = {
[0] = "Expecting device %s...",
},
.finished_start_job = {
[JOB_DONE] = "Found device %s.",
[JOB_TIMEOUT] = "Timed out waiting for device %s.",
},
},
};