device.c revision 0faacd470dfbd24f4c6504da6f04213aa05f9d19
/*-*- 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 "strv.h"
#include "log.h"
#include "unit-name.h"
#include "dbus-device.h"
#include "def.h"
#include "path-util.h"
#include "udev-util.h"
#include "unit.h"
#include "swap.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
}
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);
}
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);
}
_cleanup_free_ char *e = NULL;
int r;
assert(u);
if (!e)
return -ENOMEM;
r = unit_add_name(u, e);
if (r < 0 && r != -EEXIST)
return r;
return 0;
}
_cleanup_free_ char *e = NULL;
Unit *u;
assert(m);
if (!e)
return -ENOMEM;
u = manager_get_unit(m, e);
if (u) {
*_u = u;
return 1;
}
return 0;
}
const char *model;
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)
return unit_set_description(u, j);
}
return unit_set_description(u, model);
}
return unit_set_description(u, path);
}
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;
n = unit_name_mangle(e, MANGLE_NOGLOB);
if (!n)
return -ENOMEM;
if (r < 0)
return r;
}
return 0;
}
const char *sysfs;
bool delete;
int r;
assert(m);
if (!sysfs)
return 0;
r = device_find_escape_name(m, path, &u);
if (r < 0)
return r;
return -EEXIST;
if (!u) {
delete = true;
if (!u)
return log_oom();
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 (r < 0)
goto fail;
if (r < 0)
goto fail;
}
if (main) {
/* The additional systemd udev properties we only
* interpret for the main object */
r = device_add_udev_wants(u, dev);
if (r < 0)
goto fail;
}
/* Note that this won't dispatch the load queue, the caller
* has to do that if needed and appropriate */
return 0;
fail:
log_warning_errno(r, "Failed to load device unit: %m");
if (delete && u)
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;
device_update_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))
device_update_unit(m, dev, e, false);
else
}
}
return 0;
}
const char *sysfs;
Device *d, *l;
assert(m);
if (!sysfs)
return;
LIST_FOREACH(same_sysfs, d, l)
}
const char *sysfs;
Device *d;
assert(m);
if (!sysfs)
return -ENOMEM;
/* Remove all units of this sysfs path */
}
return 0;
}
const char *ready;
if (!ready)
return true;
return parse_boolean(ready) != 0;
}
assert(m);
if (!dev)
return log_oom();
if (!device_is_ready(dev))
return 0;
return device_process_new_device(m, dev);
}
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;
}
m->devices_by_sysfs = 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;
return 0;
fail:
device_shutdown(m);
return r;
}
const char *action;
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 (!action) {
log_error("Failed to get udev action string.");
return 0;
}
r = device_process_removed_device(m, dev);
if (r < 0)
log_error_errno(r, "Failed to process device remove event: %m");
r = swap_process_removed_device(m, dev);
if (r < 0)
log_error_errno(r, "Failed to process swap device remove event: %m");
} else {
r = device_process_new_device(m, dev);
if (r < 0)
log_error_errno(r, "Failed to process device new event: %m");
r = swap_process_new_device(m, dev);
if (r < 0)
log_error_errno(r, "Failed to process swap device new event: %m");
}
return 0;
}
static bool device_supported(Manager *m) {
static int read_only = -1;
assert(m);
/* 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;
}
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.",
},
},
};