device.c revision b30e2f4c18ad81b04e4314fd191a5d458553773c
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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"
[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);
log_debug("%s changed %s -> %s",
}
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);
}
static const char *device_sub_state_to_string(Unit *u) {
assert(u);
}
char *e;
int r;
assert(u);
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);
return -ENOMEM;
u = manager_get_unit(m, e);
free(e);
if (u) {
*_u = u;
return 1;
}
return 0;
}
int r;
bool delete;
assert(m);
return -ENOMEM;
if ((r = device_find_escape_name(m, path, &u)) < 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;
}
goto fail;
}
if ((r = unit_set_description(u, model)) < 0)
goto fail;
} else
if ((r = unit_set_description(u, path)) < 0)
goto fail;
if (main) {
/* The additional systemd udev properties we only
* interpret for the main object */
else {
if ((r = device_add_escaped_name(u, alias)) < 0)
goto fail;
}
}
char *state, *w;
size_t l;
char *e;
if (!(e = strndup(w, l))) {
r = -ENOMEM;
goto fail;
}
free(e);
if (r < 0)
goto fail;
}
}
}
return 0;
fail:
if (delete && u)
unit_free(u);
return r;
}
assert(m);
return -ENOMEM;
/* Add the main unit named after the sysfs path */
/* 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) {
struct epoll_event ev;
int r;
struct udev_enumerate *e = NULL;
assert(m);
if (!m->udev) {
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 = {
.suffix = ".device",
.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",
};