device.c revision e1ce2c2782015579f042d4d6963ed039333fb8c2
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
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"
[DEVICE_DEAD] = UNIT_INACTIVE,
};
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);
if (r >= 0 && make_id)
unit_choose_id(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;
char *w, *state;
size_t l;
bool delete;
assert(m);
return -ENOMEM;
/* Check whether this entry is even relevant for us. */
/* We allow exactly one alias to be configured a this time and
* it must be a path */
}
if ((r = device_find_escape_name(m, sysfs, &u)) < 0)
return r;
if (r == 0 && dn)
if ((r = device_find_escape_name(m, dn, &u)) < 0)
return r;
if (r == 0) {
return r;
if (r > 0)
break;
}
}
if (r == 0 && alias)
if ((r = device_find_escape_name(m, alias, &u)) < 0)
return r;
/* FIXME: this needs proper merging */
assert((r > 0) == !!u);
/* If this is a different unit, then let's not merge things */
u = NULL;
if (!u) {
delete = true;
if (!(u = unit_new(m)))
return -ENOMEM;
if ((r = device_add_escaped_name(u, sysfs, true)) < 0)
goto fail;
} else
delete = false;
r = -ENOMEM;
goto fail;
}
if (alias)
if ((r = device_add_escaped_name(u, alias, true)) < 0)
goto fail;
if (dn)
if ((r = device_add_escaped_name(u, dn, true)) < 0)
goto fail;
goto fail;
if ((r = unit_set_description(u, model)) < 0)
goto fail;
} else if (dn) {
if ((r = unit_set_description(u, dn)) < 0)
goto fail;
} else
if ((r = unit_set_description(u, sysfs)) < 0)
goto fail;
if (wants) {
char *e;
if (!(e = strndup(w, l))) {
r = -ENOMEM;
goto fail;
}
free(e);
if (r < 0)
goto fail;
}
}
if (update_state) {
}
return 0;
fail:
if (delete && u)
unit_free(u);
return r;
}
int r;
struct udev_device *dev;
assert(m);
return -ENOMEM;
}
return r;
}
const char *sysfs;
char *e;
Unit *u;
Device *d;
assert(m);
return -ENOMEM;
return -ENOMEM;
u = manager_get_unit(m, e);
free(e);
if (!u)
return 0;
d = DEVICE(u);
return 0;
}
static void device_shutdown(Manager *m) {
assert(m);
if (m->udev_monitor) {
m->udev_monitor = NULL;
}
if (m->udev) {
udev_unref(m->udev);
}
}
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;
}
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;
const char *action;
assert(m);
log_error("Failed to receive device.");
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_AVAILABLE] = "available"
};
const UnitVTable device_vtable = {
.suffix = ".device",
.no_requires = true,
.no_instances = true,
.no_snapshots = true,
.no_isolate = true,
.done = device_done,
.dump = device_dump,
};