device.c revision b47d419c25ecc735615a1088060c1ec8bef1e41f
/*-*- 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 "unit.h"
#include "device.h"
#include "strv.h"
#include "log.h"
#include "unit-name.h"
#include "dbus-device.h"
#include "def.h"
#include "path-util.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
}
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. */
UNIT(d)->ignore_on_isolate = true;
UNIT(d)->ignore_on_snapshot = true;
}
static void device_done(Unit *u) {
assert(d);
}
assert(d);
}
static int device_coldplug(Unit *u) {
assert(d);
if (d->sysfs)
return 0;
}
assert(d);
fprintf(f,
"%sDevice State: %s\n"
"%sSysfs Path: %s\n",
}
assert(u);
}
assert(u);
}
char *e;
int r;
assert(u);
if (!e)
return -ENOMEM;
r = unit_add_name(u, e);
free(e);
if (r < 0 && r != -EEXIST)
return r;
return 0;
}
char *e;
Unit *u;
assert(m);
if (!e)
return -ENOMEM;
u = manager_get_unit(m, e);
free(e);
if (u) {
*_u = u;
return 1;
}
return 0;
}
int r;
bool delete;
assert(m);
if (!sysfs)
return -ENOMEM;
r = device_find_escape_name(m, path, &u);
if (r < 0)
return r;
return -EEXIST;
if (!u) {
delete = true;
if (!u)
return -ENOMEM;
r = device_add_escaped_name(u, path);
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. */
r = -ENOMEM;
goto fail;
}
if (!m->devices_by_sysfs)
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
}
r = unit_set_description(u, model);
if (r < 0)
goto fail;
} 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 */
if (alias) {
char *state, *w;
size_t l;
_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
}
}
if (wants) {
char *state, *w;
size_t l;
_cleanup_free_ char *e, *n = NULL;
e = strndup(w, l);
if (!e) {
r = -ENOMEM;
goto fail;
}
n = unit_name_mangle(e);
if (!n) {
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
}
}
}
return 0;
fail:
if (delete && u)
unit_free(u);
return r;
}
int r;
assert(m);
return -ENOMEM;
/* Add the main unit named after the sysfs path */
if (r < 0)
return r;
/* Add an additional unit for the device node */
/* 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;
device_update_unit(m, dev, p, false);
}
if (update_state) {
Device *d, *l;
LIST_FOREACH(same_sysfs, d, l)
}
return 0;
}
int r;
struct udev_device *dev;
assert(m);
return -ENOMEM;
}
return r;
}
const char *sysfs;
Device *d;
assert(m);
return -ENOMEM;
/* Remove all units of this sysfs path */
}
return 0;
}
assert(d);
return NULL;
/* Make everybody follow the unit that's named after the sysfs path */
}
}
Set *s;
int r;
assert(d);
if (!d->same_sysfs_prev && !d->same_sysfs_next) {
return 0;
}
return -ENOMEM;
goto fail;
goto fail;
*_s = s;
return 1;
fail:
set_free(s);
return r;
}
static void device_shutdown(Manager *m) {
assert(m);
if (m->udev_monitor) {
m->udev_monitor = NULL;
}
if (m->udev) {
udev_unref(m->udev);
}
m->devices_by_sysfs = NULL;
}
static int device_enumerate(Manager *m) {
int r;
struct udev_enumerate *e = NULL;
assert(m);
if (!m->udev) {
struct epoll_event ev;
return -ENOMEM;
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. */
r = -ENOMEM;
goto fail;
}
if (udev_monitor_enable_receiving(m->udev_monitor) < 0) {
r = -EIO;
goto fail;
}
return -errno;
}
if (!(e = udev_enumerate_new(m->udev))) {
r = -ENOMEM;
goto fail;
}
if (udev_enumerate_add_match_tag(e, "systemd") < 0) {
r = -EIO;
goto fail;
}
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto fail;
}
return 0;
fail:
if (e)
device_shutdown(m);
return r;
}
struct udev_device *dev;
int r;
assert(m);
if (!ratelimit_test(&limit))
log_error("Failed to get udev event: %m");
return;
}
/*
* libudev might filter-out devices which pass the bloom filter,
* so getting NULL here is not necessarily an error
*/
return;
}
log_error("Failed to get udev action string.");
goto fail;
}
if ((r = device_process_removed_device(m, dev)) < 0) {
goto fail;
}
} else {
if ((r = device_process_new_device(m, dev, true)) < 0) {
goto fail;
}
}
fail:
}
static const char* const device_state_table[_DEVICE_STATE_MAX] = {
[DEVICE_DEAD] = "dead",
[DEVICE_PLUGGED] = "plugged"
};
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,
.bus_interface = "org.freedesktop.systemd1.Device",
.starting_stopping = {
[0] = "Expecting device %s...",
},
.finished_start_job = {
[JOB_DONE] = "Found device %s.",
[JOB_TIMEOUT] = "Timed out waiting for device %s.",
},
},
};