Merge pull request #9904 from yuwata/replace-udev-device

tree-wide: drop udev_device struct and use sd_device instead
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-08-23 09:01:44 +02:00 committed by GitHub
commit 60118b21c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
70 changed files with 916 additions and 1122 deletions

View file

@ -1,42 +1,45 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "libudev.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "def.h"
#include "device-enumerator-private.h"
#include "escape.h"
#include "fileio.h"
#include "mkdir.h"
#include "parse-util.h"
#include "proc-cmdline.h"
#include "string-util.h"
#include "udev-util.h"
#include "strv.h"
#include "util.h"
static struct udev_device *find_pci_or_platform_parent(struct udev_device *device) {
struct udev_device *parent;
const char *subsystem, *sysname;
static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
const char *subsystem, *sysname, *value;
sd_device *parent;
int r;
assert(device);
assert(ret);
parent = udev_device_get_parent(device);
if (!parent)
return NULL;
r = sd_device_get_parent(device, &parent);
if (r < 0)
return r;
subsystem = udev_device_get_subsystem(parent);
if (!subsystem)
return NULL;
r = sd_device_get_subsystem(parent, &subsystem);
if (r < 0)
return r;
sysname = udev_device_get_sysname(parent);
if (!sysname)
return NULL;
r = sd_device_get_sysname(parent, &sysname);
if (r < 0)
return r;
if (streq(subsystem, "drm")) {
const char *c;
c = startswith(sysname, "card");
if (!c)
return NULL;
return -ENODATA;
c += strspn(c, DIGITS);
if (*c == '-') {
@ -44,54 +47,68 @@ static struct udev_device *find_pci_or_platform_parent(struct udev_device *devic
if (!startswith(c, "-LVDS-") &&
!startswith(c, "-Embedded DisplayPort-"))
return NULL;
return -EOPNOTSUPP;
}
} else if (streq(subsystem, "pci")) {
const char *value;
} else if (streq(subsystem, "pci") &&
sd_device_get_sysattr_value(parent, "class", &value) >= 0) {
unsigned long class = 0;
value = udev_device_get_sysattr_value(parent, "class");
if (value) {
unsigned long class = 0;
r = safe_atolu(value, &class);
if (r < 0)
return log_warning_errno(r, "Cannot parse PCI class '%s' of device %s:%s: %m",
value, subsystem, sysname);
if (safe_atolu(value, &class) < 0) {
log_warning("Cannot parse PCI class %s of device %s:%s.",
value, subsystem, sysname);
return NULL;
}
/* Graphics card */
if (class == 0x30000)
return parent;
/* Graphics card */
if (class == 0x30000) {
*ret = TAKE_PTR(parent);
return 0;
}
} else if (streq(subsystem, "platform"))
return parent;
} else if (streq(subsystem, "platform")) {
*ret = TAKE_PTR(parent);
return 0;
}
return find_pci_or_platform_parent(parent);
return find_pci_or_platform_parent(parent, ret);
}
static bool same_device(struct udev_device *a, struct udev_device *b) {
static int same_device(sd_device *a, sd_device *b) {
const char *a_val, *b_val;
int r;
assert(a);
assert(b);
if (!streq_ptr(udev_device_get_subsystem(a), udev_device_get_subsystem(b)))
r = sd_device_get_subsystem(a, &a_val);
if (r < 0)
return r;
r = sd_device_get_subsystem(b, &b_val);
if (r < 0)
return r;
if (!streq_ptr(a_val, b_val))
return false;
if (!streq_ptr(udev_device_get_sysname(a), udev_device_get_sysname(b)))
return false;
r = sd_device_get_sysname(a, &a_val);
if (r < 0)
return r;
return true;
r = sd_device_get_sysname(b, &b_val);
if (r < 0)
return r;
return streq_ptr(a_val, b_val);
}
static bool validate_device(struct udev *udev, struct udev_device *device) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *enumerate = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
struct udev_device *parent;
static int validate_device(sd_device *device) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerate = NULL;
_cleanup_(sd_device_unrefp) sd_device *parent = NULL;
const char *v, *subsystem;
sd_device *other;
int r;
assert(udev);
assert(device);
/* Verify whether we should actually care for a specific
@ -108,76 +125,85 @@ static bool validate_device(struct udev *udev, struct udev_device *device) {
* that we use "raw" only if no "firmware" or "platform"
* device for the same device exists. */
subsystem = udev_device_get_subsystem(device);
if (!streq_ptr(subsystem, "backlight"))
return true;
v = udev_device_get_sysattr_value(device, "type");
if (!streq_ptr(v, "raw"))
return true;
parent = find_pci_or_platform_parent(device);
if (!parent)
return true;
subsystem = udev_device_get_subsystem(parent);
if (!subsystem)
return true;
enumerate = udev_enumerate_new(udev);
if (!enumerate)
return true;
r = udev_enumerate_add_match_subsystem(enumerate, "backlight");
r = sd_device_get_subsystem(device, &subsystem);
if (r < 0)
return r;
if (!streq(subsystem, "backlight"))
return true;
r = udev_enumerate_scan_devices(enumerate);
r = sd_device_get_sysattr_value(device, "type", &v);
if (r < 0)
return r;
if (!streq(v, "raw"))
return true;
first = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *other;
struct udev_device *other_parent;
r = find_pci_or_platform_parent(device, &parent);
if (r < 0)
return r;
r = sd_device_get_subsystem(parent, &subsystem);
if (r < 0)
return r;
r = sd_device_enumerator_new(&enumerate);
if (r < 0)
return r;
r = sd_device_enumerator_allow_uninitialized(enumerate);
if (r < 0)
return r;
r = sd_device_enumerator_add_match_subsystem(enumerate, "backlight", true);
if (r < 0)
return r;
r = device_enumerator_scan_devices(enumerate);
if (r < 0)
return r;
FOREACH_DEVICE_AND_SUBSYSTEM(enumerate, other) {
_cleanup_(sd_device_unrefp) sd_device *other_parent = NULL;
const char *other_subsystem;
other = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!other)
return true;
if (same_device(device, other))
if (same_device(device, other) > 0)
continue;
v = udev_device_get_sysattr_value(other, "type");
if (!STRPTR_IN_SET(v, "platform", "firmware"))
if (sd_device_get_sysattr_value(other, "type", &v) < 0 ||
!STR_IN_SET(v, "platform", "firmware"))
continue;
/* OK, so there's another backlight device, and it's a
* platform or firmware device, so, let's see if we
* can verify it belongs to the same device as
* ours. */
other_parent = find_pci_or_platform_parent(other);
if (!other_parent)
* can verify it belongs to the same device as ours. */
if (find_pci_or_platform_parent(other, &other_parent) < 0)
continue;
if (same_device(parent, other_parent)) {
/* Both have the same PCI parent, that means
* we are out. */
const char *device_sysname = NULL, *other_sysname = NULL;
/* Both have the same PCI parent, that means we are out. */
(void) sd_device_get_sysname(device, &device_sysname);
(void) sd_device_get_sysname(other, &other_sysname);
log_debug("Skipping backlight device %s, since device %s is on same PCI device and takes precedence.",
udev_device_get_sysname(device),
udev_device_get_sysname(other));
device_sysname, other_sysname);
return false;
}
other_subsystem = udev_device_get_subsystem(other_parent);
if (streq_ptr(other_subsystem, "platform") && streq_ptr(subsystem, "pci")) {
/* The other is connected to the platform bus
* and we are a PCI device, that also means we
* are out. */
if (sd_device_get_subsystem(other_parent, &other_subsystem) < 0)
continue;
if (streq(other_subsystem, "platform") && streq(subsystem, "pci")) {
const char *device_sysname = NULL, *other_sysname = NULL;
/* The other is connected to the platform bus and we are a PCI device, that also means we are out. */
(void) sd_device_get_sysname(device, &device_sysname);
(void) sd_device_get_sysname(other, &other_sysname);
log_debug("Skipping backlight device %s, since device %s is a platform device and takes precedence.",
udev_device_get_sysname(device),
udev_device_get_sysname(other));
device_sysname, other_sysname);
return false;
}
}
@ -185,29 +211,29 @@ static bool validate_device(struct udev *udev, struct udev_device *device) {
return true;
}
static unsigned get_max_brightness(struct udev_device *device) {
int r;
static int get_max_brightness(sd_device *device, unsigned *ret) {
const char *max_brightness_str;
unsigned max_brightness;
int r;
max_brightness_str = udev_device_get_sysattr_value(device, "max_brightness");
if (!max_brightness_str) {
log_warning("Failed to read 'max_brightness' attribute.");
return 0;
}
assert(device);
assert(ret);
r = sd_device_get_sysattr_value(device, "max_brightness", &max_brightness_str);
if (r < 0)
return log_warning_errno(r, "Failed to read 'max_brightness' attribute: %m");
r = safe_atou(max_brightness_str, &max_brightness);
if (r < 0) {
log_warning_errno(r, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str);
return 0;
}
if (r < 0)
return log_warning_errno(r, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str);
if (max_brightness <= 0) {
log_warning("Maximum brightness is 0, ignoring device.");
return 0;
return -EINVAL;
}
return max_brightness;
*ret = max_brightness;
return 0;
}
/* Some systems turn the backlight all the way off at the lowest levels.
@ -215,50 +241,54 @@ static unsigned get_max_brightness(struct udev_device *device) {
* max_brightness in case of 'backlight' subsystem. This avoids preserving
* an unreadably dim screen, which would otherwise force the user to
* disable state restoration. */
static void clamp_brightness(struct udev_device *device, char **value, unsigned max_brightness) {
static int clamp_brightness(sd_device *device, char **value, unsigned max_brightness) {
unsigned brightness, new_brightness, min_brightness;
const char *subsystem;
int r;
r = safe_atou(*value, &brightness);
if (r < 0) {
log_warning_errno(r, "Failed to parse brightness \"%s\": %m", *value);
return;
}
assert(value);
assert(*value);
subsystem = udev_device_get_subsystem(device);
if (streq_ptr(subsystem, "backlight"))
r = safe_atou(*value, &brightness);
if (r < 0)
return log_warning_errno(r, "Failed to parse brightness \"%s\": %m", *value);
r = sd_device_get_subsystem(device, &subsystem);
if (r < 0)
return log_warning_errno(r, "Failed to get device subsystem: %m");
if (streq(subsystem, "backlight"))
min_brightness = MAX(1U, max_brightness/20);
else
min_brightness = 0;
new_brightness = CLAMP(brightness, min_brightness, max_brightness);
if (new_brightness != brightness) {
char *old_value = *value;
char *new_value;
r = asprintf(value, "%u", new_brightness);
if (r < 0) {
log_oom();
return;
}
r = asprintf(&new_value, "%u", new_brightness);
if (r < 0)
return log_oom();
log_info("Saved brightness %s %s to %s.", old_value,
log_info("Saved brightness %s %s to %s.", *value,
new_brightness > brightness ?
"too low; increasing" : "too high; decreasing",
*value);
new_value);
free(old_value);
free_and_replace(*value, new_value);
}
return 0;
}
static bool shall_clamp(struct udev_device *d) {
static bool shall_clamp(sd_device *d) {
const char *s;
int r;
assert(d);
s = udev_device_get_property_value(d, "ID_BACKLIGHT_CLAMP");
if (!s)
r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &s);
if (r < 0)
return true;
r = parse_boolean(s);
@ -271,8 +301,7 @@ static bool shall_clamp(struct udev_device *d) {
}
int main(int argc, char *argv[]) {
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
_cleanup_free_ char *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
const char *sysname, *path_id, *ss, *saved;
unsigned max_brightness;
@ -295,12 +324,6 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
udev = udev_new();
if (!udev) {
log_oom();
return EXIT_FAILURE;
}
sysname = strchr(argv[2], ':');
if (!sysname) {
log_error("Requires a subsystem and sysname pair specifying a backlight device.");
@ -316,23 +339,17 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
errno = 0;
device = udev_device_new_from_subsystem_sysname(udev, ss, sysname);
if (!device) {
if (errno > 0)
log_error_errno(errno, "Failed to get backlight or LED device '%s:%s': %m", ss, sysname);
else
log_oom();
r = sd_device_new_from_subsystem_sysname(&device, ss, sysname);
if (r < 0) {
log_error_errno(r, "Failed to get backlight or LED device '%s:%s': %m", ss, sysname);
return EXIT_FAILURE;
}
/* If max_brightness is 0, then there is no actual backlight
* device. This happens on desktops with Asus mainboards
* that load the eeepc-wmi module.
*/
max_brightness = get_max_brightness(device);
if (max_brightness == 0)
* that load the eeepc-wmi module. */
r = get_max_brightness(device, &max_brightness);
if (r < 0)
return EXIT_SUCCESS;
escaped_ss = cescape(ss);
@ -347,8 +364,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
path_id = udev_device_get_property_value(device, "ID_PATH");
if (path_id) {
if (sd_device_get_property_value(device, "ID_PATH", &path_id) >= 0) {
escaped_path_id = cescape(path_id);
if (!escaped_path_id) {
log_oom();
@ -375,7 +391,7 @@ int main(int argc, char *argv[]) {
if (shall_restore_state() == 0)
return EXIT_SUCCESS;
if (!validate_device(udev, device))
if (validate_device(device) == 0)
return EXIT_SUCCESS;
clamp = shall_clamp(device);
@ -389,9 +405,9 @@ int main(int argc, char *argv[]) {
if (!clamp)
return EXIT_SUCCESS;
curval = udev_device_get_sysattr_value(device, "brightness");
if (!curval) {
log_warning("Failed to read 'brightness' attribute.");
r = sd_device_get_sysattr_value(device, "brightness", &curval);
if (r < 0) {
log_warning_errno(r, "Failed to read 'brightness' attribute: %m");
return EXIT_FAILURE;
}
@ -406,9 +422,9 @@ int main(int argc, char *argv[]) {
}
if (clamp)
clamp_brightness(device, &value, max_brightness);
(void) clamp_brightness(device, &value, max_brightness);
r = udev_device_set_sysattr_value(device, "brightness", value);
r = sd_device_set_sysattr_value(device, "brightness", value);
if (r < 0) {
log_error_errno(r, "Failed to write system 'brightness' attribute: %m");
return EXIT_FAILURE;
@ -417,14 +433,14 @@ int main(int argc, char *argv[]) {
} else if (streq(argv[1], "save")) {
const char *value;
if (!validate_device(udev, device)) {
if (validate_device(device) == 0) {
unlink(saved);
return EXIT_SUCCESS;
}
value = udev_device_get_sysattr_value(device, "brightness");
if (!value) {
log_error("Failed to read system 'brightness' attribute");
r = sd_device_get_sysattr_value(device, "brightness", &value);
if (r < 0) {
log_error_errno(r, "Failed to read system 'brightness' attribute: %m");
return EXIT_FAILURE;
}

View file

@ -3,19 +3,20 @@
#include <errno.h>
#include <sys/epoll.h>
#include "libudev.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "dbus-device.h"
#include "device-private.h"
#include "device-enumerator-private.h"
#include "device-util.h"
#include "device.h"
#include "libudev-private.h"
#include "log.h"
#include "parse-util.h"
#include "path-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "swap.h"
#include "udev-util.h"
#include "unit-name.h"
#include "unit.h"
@ -300,29 +301,22 @@ _pure_ static const char *device_sub_state_to_string(Unit *u) {
return device_state_to_string(DEVICE(u)->state);
}
static int device_update_description(Unit *u, struct udev_device *dev, const char *path) {
const char *model;
static int device_update_description(Unit *u, sd_device *dev, const char *path) {
const char *model, *label;
int r;
assert(u);
assert(dev);
assert(path);
model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
if (!model)
model = udev_device_get_property_value(dev, "ID_MODEL");
if (model) {
const char *label;
if (sd_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE", &model) >= 0 ||
sd_device_get_property_value(dev, "ID_MODEL", &model) >= 0) {
/* Try to concatenate the device model string with a label, if there is one */
label = udev_device_get_property_value(dev, "ID_FS_LABEL");
if (!label)
label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME");
if (!label)
label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER");
if (sd_device_get_property_value(dev, "ID_FS_LABEL", &label) >= 0 ||
sd_device_get_property_value(dev, "ID_PART_ENTRY_NAME", &label) >= 0 ||
sd_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER", &label) >= 0) {
if (label) {
_cleanup_free_ char *j;
j = strjoin(model, " ", label);
@ -340,7 +334,7 @@ static int device_update_description(Unit *u, struct udev_device *dev, const cha
return 0;
}
static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
static int device_add_udev_wants(Unit *u, sd_device *dev) {
_cleanup_strv_free_ char **added = NULL;
const char *wants, *property;
Device *d = DEVICE(u);
@ -351,8 +345,8 @@ static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
wants = udev_device_get_property_value(dev, property);
if (!wants)
r = sd_device_get_property_value(dev, property, &wants);
if (r < 0)
return 0;
for (;;) {
@ -429,15 +423,14 @@ static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
return 0;
}
static bool device_is_bound_by_mounts(Device *d, struct udev_device *dev) {
static bool device_is_bound_by_mounts(Device *d, sd_device *dev) {
const char *bound_by;
int r;
assert(d);
assert(dev);
bound_by = udev_device_get_property_value(dev, "SYSTEMD_MOUNT_DEVICE_BOUND");
if (bound_by) {
if (sd_device_get_property_value(dev, "SYSTEMD_MOUNT_DEVICE_BOUND", &bound_by) >= 0) {
r = parse_boolean(bound_by);
if (r < 0)
log_warning_errno(r, "Failed to parse SYSTEMD_MOUNT_DEVICE_BOUND='%s' udev property of %s, ignoring: %m", bound_by, strna(d->sysfs));
@ -467,7 +460,7 @@ static void device_upgrade_mount_deps(Unit *u) {
}
}
static int device_setup_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
static int device_setup_unit(Manager *m, sd_device *dev, const char *path, bool main) {
_cleanup_free_ char *e = NULL;
const char *sysfs = NULL;
Unit *u = NULL;
@ -478,9 +471,9 @@ static int device_setup_unit(Manager *m, struct udev_device *dev, const char *pa
assert(path);
if (dev) {
sysfs = udev_device_get_syspath(dev);
if (!sysfs) {
log_debug("Couldn't get syspath from udev device, ignoring.");
r = sd_device_get_syspath(dev, &sysfs);
if (r < 0) {
log_debug_errno(r, "Couldn't get syspath from udev device, ignoring: %m");
return 0;
}
}
@ -559,15 +552,14 @@ fail:
return r;
}
static int device_process_new(Manager *m, struct udev_device *dev) {
static int device_process_new(Manager *m, sd_device *dev) {
const char *sysfs, *dn, *alias;
struct udev_list_entry *item = NULL, *first = NULL;
dev_t devnum;
int r;
assert(m);
sysfs = udev_device_get_syspath(dev);
if (!sysfs)
if (sd_device_get_syspath(dev, &sysfs) < 0)
return 0;
/* Add the main unit named after the sysfs path */
@ -576,40 +568,39 @@ static int device_process_new(Manager *m, struct udev_device *dev) {
return r;
/* Add an additional unit for the device node */
dn = udev_device_get_devnode(dev);
if (dn)
if (sd_device_get_devname(dev, &dn) >= 0)
(void) device_setup_unit(m, dev, dn, false);
/* Add additional units for all symlinks */
first = udev_device_get_devlinks_list_entry(dev);
udev_list_entry_foreach(item, first) {
if (sd_device_get_devnum(dev, &devnum) >= 0) {
const char *p;
struct stat st;
/* Don't bother with the /dev/block links */
p = udev_list_entry_get_name(item);
FOREACH_DEVICE_DEVLINK(dev, p) {
struct stat st;
if (PATH_STARTSWITH_SET(p, "/dev/block/", "/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
* same /dev/disk/by-label/xxx link because they have
* the same label. We want to make sure that the same
* device that won the symlink wins in systemd, so we
* check the device node major/minor */
if (stat(p, &st) >= 0)
if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
st.st_rdev != udev_device_get_devnum(dev))
if (PATH_STARTSWITH_SET(p, "/dev/block/", "/dev/char/"))
continue;
(void) device_setup_unit(m, dev, p, false);
/* 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
* same /dev/disk/by-label/xxx link because they have
* the same label. We want to make sure that the same
* device that won the symlink wins in systemd, so we
* check the device node major/minor */
if (stat(p, &st) >= 0 &&
((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
st.st_rdev != devnum))
continue;
(void) device_setup_unit(m, dev, p, false);
}
}
/* Add additional units for all explicitly configured
* aliases */
alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
/* Add additional units for all explicitly configured aliases */
if (sd_device_get_property_value(dev, "SYSTEMD_ALIAS", &alias) < 0)
return 0;
for (;;) {
_cleanup_free_ char *word = NULL;
@ -712,13 +703,12 @@ static int device_update_found_by_name(Manager *m, const char *path, DeviceFound
return 0;
}
static bool device_is_ready(struct udev_device *dev) {
static bool device_is_ready(sd_device *dev) {
const char *ready;
assert(dev);
ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
if (!ready)
if (sd_device_get_property_value(dev, "SYSTEMD_READY", &ready) < 0)
return true;
return parse_boolean(ready) != 0;
@ -790,14 +780,14 @@ static void device_shutdown(Manager *m) {
}
static void device_enumerate(Manager *m) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *dev;
int r;
assert(m);
if (!m->udev_monitor) {
m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
m->udev_monitor = udev_monitor_new_from_netlink(NULL, "udev");
if (!m->udev_monitor) {
log_error_errno(errno, "Failed to allocate udev monitor: %m");
goto fail;
@ -829,53 +819,35 @@ static void device_enumerate(Manager *m) {
(void) sd_event_source_set_description(m->udev_event_source, "device");
}
e = udev_enumerate_new(m->udev);
if (!e) {
log_error_errno(errno, "Failed to alloacte udev enumerator: %m");
goto fail;
}
r = udev_enumerate_add_match_tag(e, "systemd");
r = sd_device_enumerator_new(&e);
if (r < 0) {
log_error_errno(r, "Failed to create udev tag enumeration: %m");
log_error_errno(r, "Failed to alloacte device enumerator: %m");
goto fail;
}
r = udev_enumerate_add_match_is_initialized(e);
r = sd_device_enumerator_add_match_tag(e, "systemd");
if (r < 0) {
log_error_errno(r, "Failed to install initialization match into enumeration: %m");
log_error_errno(r, "Failed to set tag for device enumeration: %m");
goto fail;
}
r = udev_enumerate_scan_devices(e);
r = device_enumerator_scan_devices(e);
if (r < 0) {
log_error_errno(r, "Failed to enumerate devices: %m");
goto fail;
}
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
FOREACH_DEVICE_AND_SUBSYSTEM(e, dev) {
const char *sysfs;
sysfs = udev_list_entry_get_name(item);
dev = udev_device_new_from_syspath(m->udev, sysfs);
if (!dev) {
if (errno == ENOMEM) {
log_oom();
goto fail;
}
/* If we can't create a device, don't bother, it probably just disappeared. */
log_debug_errno(errno, "Failed to create udev device object for %s: %m", sysfs);
continue;
}
if (!device_is_ready(dev))
continue;
(void) device_process_new(m, dev);
if (sd_device_get_syspath(dev, &sysfs) < 0)
continue;
device_update_found_by_sysfs(m, sysfs, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV);
}
@ -904,7 +876,7 @@ static void device_propagate_reload_by_sysfs(Manager *m, const char *sysfs) {
}
static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
Manager *m = userdata;
const char *action, *sysfs;
int r;
@ -924,19 +896,19 @@ static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
* libudev might filter-out devices which pass the bloom
* filter, so getting NULL here is not necessarily an error.
*/
dev = udev_monitor_receive_device(m->udev_monitor);
if (!dev)
r = udev_monitor_receive_sd_device(m->udev_monitor, &dev);
if (r < 0)
return 0;
sysfs = udev_device_get_syspath(dev);
if (!sysfs) {
log_error("Failed to get udev sys path.");
r = sd_device_get_syspath(dev, &sysfs);
if (r < 0) {
log_error_errno(r, "Failed to get device sys path: %m");
return 0;
}
action = udev_device_get_action(dev);
if (!action) {
log_error("Failed to get udev action string.");
r = sd_device_get_property_value(dev, "ACTION", &action);
if (r < 0) {
log_error_errno(r, "Failed to get udev action string: %m");
return 0;
}
@ -992,7 +964,7 @@ static bool device_supported(void) {
return read_only <= 0;
}
static int validate_node(Manager *m, const char *node, struct udev_device **ret) {
static int validate_node(Manager *m, const char *node, sd_device **ret) {
struct stat st;
int r;
@ -1016,9 +988,9 @@ static int validate_node(Manager *m, const char *node, struct udev_device **ret)
return 1; /* good! (though missing) */
} else {
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
r = udev_device_new_from_stat_rdev(m->udev, &st, &dev);
r = device_new_from_stat_rdev(&dev, &st);
if (r == -ENOENT) {
*ret = NULL;
return 1; /* good! (though missing) */
@ -1054,7 +1026,7 @@ void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFo
* and unset individual bits in a single call, while merging partially with previous state. */
if ((found & mask) != 0) {
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
/* If the device is known in the kernel and newly appeared, then we'll create a device unit for it,
* under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if

View file

@ -798,10 +798,6 @@ int manager_new(UnitFileScope scope, unsigned test_run_flags, Manager **_m) {
if (r < 0)
return r;
m->udev = udev_new();
if (!m->udev)
return -ENOMEM;
r = sd_event_default(&m->event);
if (r < 0)
return r;
@ -1334,7 +1330,6 @@ Manager* manager_free(Manager *m) {
manager_close_idle_pipe(m);
udev_unref(m->udev);
sd_event_unref(m->event);
free(m->notify_socket);

View file

@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdio.h>
#include "libudev.h"
#include "sd-bus.h"
#include "sd-event.h"
@ -215,8 +216,6 @@ struct Manager {
dual_timestamp timestamps[_MANAGER_TIMESTAMP_MAX];
struct udev* udev;
/* Data specific to the device subsystem */
struct udev_monitor* udev_monitor;
sd_event_source *udev_event_source;

View file

@ -5,10 +5,12 @@
#include <sys/stat.h>
#include <unistd.h>
#include "libudev.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "dbus-swap.h"
#include "device-private.h"
#include "device-util.h"
#include "device.h"
#include "escape.h"
#include "exit-status.h"
@ -22,7 +24,6 @@
#include "string-table.h"
#include "string-util.h"
#include "swap.h"
#include "udev-util.h"
#include "unit-name.h"
#include "unit.h"
#include "virt.h"
@ -247,7 +248,7 @@ static int swap_verify(Swap *s) {
}
static int swap_load_devnode(Swap *s) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
struct stat st;
const char *p;
int r;
@ -257,15 +258,14 @@ static int swap_load_devnode(Swap *s) {
if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
return 0;
r = udev_device_new_from_stat_rdev(UNIT(s)->manager->udev, &st, &d);
r = device_new_from_stat_rdev(&d, &st);
if (r < 0) {
log_unit_full(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
"Failed to allocate udev device for swap %s: %m", s->what);
"Failed to allocate device for swap %s: %m", s->what);
return 0;
}
p = udev_device_get_devnode(d);
if (!p)
if (sd_device_get_devname(d, &p) < 0)
return 0;
return swap_set_devnode(s, p);
@ -425,10 +425,9 @@ fail:
}
static int swap_process_new(Manager *m, const char *device, int prio, bool set_flags) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
const char *dn;
struct stat st;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
const char *dn, *devlink;
struct stat st, st_link;
int r;
assert(m);
@ -442,38 +441,33 @@ static int swap_process_new(Manager *m, const char *device, int prio, bool set_f
if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
return 0;
r = udev_device_new_from_stat_rdev(m->udev, &st, &d);
r = device_new_from_stat_rdev(&d, &st);
if (r < 0) {
log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
"Failed to allocate udev device for swap %s: %m", device);
"Failed to allocate device for swap %s: %m", device);
return 0;
}
/* Add the main device node */
dn = udev_device_get_devnode(d);
if (dn && !streq(dn, device))
if (sd_device_get_devname(d, &dn) >= 0 && !streq(dn, device))
swap_setup_unit(m, dn, device, prio, set_flags);
/* Add additional units for all symlinks */
first = udev_device_get_devlinks_list_entry(d);
udev_list_entry_foreach(item, first) {
const char *p;
FOREACH_DEVICE_DEVLINK(d, devlink) {
/* Don't bother with the /dev/block links */
p = udev_list_entry_get_name(item);
if (streq(p, device))
if (streq(devlink, device))
continue;
if (path_startswith(p, "/dev/block/"))
if (path_startswith(devlink, "/dev/block/"))
continue;
if (stat(p, &st) >= 0)
if (!S_ISBLK(st.st_mode) ||
st.st_rdev != udev_device_get_devnum(d))
continue;
if (stat(devlink, &st_link) >= 0 &&
(!S_ISBLK(st_link.st_mode) ||
st_link.st_rdev != st.st_rdev))
continue;
swap_setup_unit(m, p, device, prio, set_flags);
swap_setup_unit(m, devlink, device, prio, set_flags);
}
return r;
@ -1322,18 +1316,17 @@ fail:
swap_shutdown(m);
}
int swap_process_device_new(Manager *m, struct udev_device *dev) {
struct udev_list_entry *item = NULL, *first = NULL;
int swap_process_device_new(Manager *m, sd_device *dev) {
_cleanup_free_ char *e = NULL;
const char *dn;
const char *dn, *devlink;
Unit *u;
int r = 0;
assert(m);
assert(dev);
dn = udev_device_get_devnode(dev);
if (!dn)
r = sd_device_get_devname(dev, &dn);
if (r < 0)
return 0;
r = unit_name_from_path(dn, ".swap", &e);
@ -1344,12 +1337,11 @@ int swap_process_device_new(Manager *m, struct udev_device *dev) {
if (u)
r = swap_set_devnode(SWAP(u), dn);
first = udev_device_get_devlinks_list_entry(dev);
udev_list_entry_foreach(item, first) {
FOREACH_DEVICE_DEVLINK(dev, devlink) {
_cleanup_free_ char *n = NULL;
int q;
q = unit_name_from_path(udev_list_entry_get_name(item), ".swap", &n);
q = unit_name_from_path(devlink, ".swap", &n);
if (q < 0)
return q;
@ -1364,13 +1356,13 @@ int swap_process_device_new(Manager *m, struct udev_device *dev) {
return r;
}
int swap_process_device_remove(Manager *m, struct udev_device *dev) {
int swap_process_device_remove(Manager *m, sd_device *dev) {
const char *dn;
int r = 0;
Swap *s;
dn = udev_device_get_devnode(dev);
if (!dn)
r = sd_device_get_devname(dev, &dn);
if (r < 0)
return 0;
while ((s = hashmap_get(m->swaps_by_devnode, dn))) {

View file

@ -5,7 +5,7 @@
Copyright © 2010 Maarten Lankhorst
***/
#include "libudev.h"
#include "sd-device.h"
#include "unit.h"
typedef struct Swap Swap;
@ -85,8 +85,8 @@ struct Swap {
extern const UnitVTable swap_vtable;
int swap_process_device_new(Manager *m, struct udev_device *dev);
int swap_process_device_remove(Manager *m, struct udev_device *dev);
int swap_process_device_new(Manager *m, sd_device *dev);
int swap_process_device_remove(Manager *m, sd_device *dev);
const char* swap_exec_command_to_string(SwapExecCommand i) _const_;
SwapExecCommand swap_exec_command_from_string(const char *s) _pure_;

View file

@ -13,11 +13,12 @@
/* This needs to be after sys/mount.h :( */
#include <libmount.h>
#include "libudev.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "blockdev-util.h"
#include "def.h"
#include "device-enumerator-private.h"
#include "escape.h"
#include "fd-util.h"
#include "fstab-util.h"
@ -28,7 +29,6 @@
#include "process-util.h"
#include "signal-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "umount.h"
#include "util.h"
#include "virt.h"
@ -229,49 +229,41 @@ int swap_list_get(const char *swaps, MountPoint **head) {
}
static int loopback_list_get(MountPoint **head) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *d;
int r;
assert(head);
udev = udev_new();
if (!udev)
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "block");
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = udev_enumerate_add_match_sysname(e, "loop*");
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
r = udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL);
r = sd_device_enumerator_add_match_subsystem(e, "block", true);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
r = sd_device_enumerator_add_match_sysname(e, "loop*");
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d;
const char *dn;
r = sd_device_enumerator_add_match_sysattr(e, "loop/backing_file", NULL, true);
if (r < 0)
return r;
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
_cleanup_free_ MountPoint *lb = NULL;
const char *dn;
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
dn = udev_device_get_devnode(d);
if (!dn)
if (sd_device_get_devname(d, &dn) < 0)
continue;
lb = new0(MountPoint, 1);
@ -290,47 +282,41 @@ static int loopback_list_get(MountPoint **head) {
}
static int dm_list_get(MountPoint **head) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *d;
int r;
assert(head);
udev = udev_new();
if (!udev)
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "block");
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = udev_enumerate_add_match_sysname(e, "dm-*");
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
r = sd_device_enumerator_add_match_subsystem(e, "block", true);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d;
dev_t devnum;
const char *dn;
r = sd_device_enumerator_add_match_sysname(e, "dm-*");
if (r < 0)
return r;
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
_cleanup_free_ MountPoint *m = NULL;
const char *dn;
dev_t devnum;
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
if (sd_device_get_devnum(d, &devnum) < 0)
continue;
devnum = udev_device_get_devnum(d);
dn = udev_device_get_devnode(d);
if (major(devnum) == 0 || !dn)
if (sd_device_get_devname(d, &dn) < 0)
continue;
m = new0(MountPoint, 1);

View file

@ -5,7 +5,7 @@
#include <sys/statfs.h>
#include <unistd.h>
#include "libudev.h"
#include "sd-device.h"
#include "sd-id128.h"
#include "alloc-util.h"
@ -30,7 +30,7 @@
#include "specifier.h"
#include "stat-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "strv.h"
#include "unit-name.h"
#include "util.h"
#include "virt.h"
@ -446,41 +446,36 @@ static int add_esp(DissectedPartition *p) {
#endif
static int open_parent(dev_t devnum, int *ret) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
const char *name, *devtype, *node;
struct udev_device *parent;
sd_device *parent;
dev_t pn;
int fd;
int fd, r;
assert(ret);
udev = udev_new();
if (!udev)
return log_oom();
r = sd_device_new_from_devnum(&d, 'b', devnum);
if (r < 0)
return log_debug_errno(r, "Failed to open device: %m");
d = udev_device_new_from_devnum(udev, 'b', devnum);
if (!d)
return log_oom();
name = udev_device_get_devnode(d);
if (!name)
name = udev_device_get_syspath(d);
if (!name) {
log_debug("Device %u:%u does not have a name, ignoring.", major(devnum), minor(devnum));
goto not_found;
if (sd_device_get_devname(d, &name) < 0) {
r = sd_device_get_syspath(d, &name);
if (r < 0) {
log_debug_errno(r, "Device %u:%u does not have a name, ignoring: %m", major(devnum), minor(devnum));
goto not_found;
}
}
parent = udev_device_get_parent(d);
if (!parent) {
log_debug("%s: not a partitioned device, ignoring.", name);
r = sd_device_get_parent(d, &parent);
if (r < 0) {
log_debug_errno(r, "%s: not a partitioned device, ignoring: %m", name);
goto not_found;
}
/* Does it have a devtype? */
devtype = udev_device_get_devtype(parent);
if (!devtype) {
log_debug("%s: parent doesn't have a device type, ignoring.", name);
r = sd_device_get_devtype(parent, &devtype);
if (r < 0) {
log_debug_errno(r, "%s: parent doesn't have a device type, ignoring: %m", name);
goto not_found;
}
@ -491,17 +486,17 @@ static int open_parent(dev_t devnum, int *ret) {
}
/* Does it have a device node? */
node = udev_device_get_devnode(parent);
if (!node) {
log_debug("%s: parent device does not have device node, ignoring.", name);
r = sd_device_get_devname(parent, &node);
if (r < 0) {
log_debug_errno(r, "%s: parent device does not have device node, ignoring: %m", name);
goto not_found;
}
log_debug("%s: root device %s.", name, node);
pn = udev_device_get_devnum(parent);
if (major(pn) == 0) {
log_debug("%s: parent device is not a proper block device, ignoring.", name);
r = sd_device_get_devnum(parent, &pn);
if (r < 0) {
log_debug_errno(r, "%s: parent device is not a proper block device, ignoring: %m", name);
goto not_found;
}

View file

@ -22,6 +22,7 @@
#endif
#include "sd-bus.h"
#include "sd-device.h"
#include "sd-journal.h"
#include "acl-util.h"
@ -30,6 +31,7 @@
#include "bus-util.h"
#include "catalog.h"
#include "chattr-util.h"
#include "device-private.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
@ -57,8 +59,6 @@
#include "strv.h"
#include "syslog-util.h"
#include "terminal-util.h"
#include "udev-util.h"
#include "udev.h"
#include "unit-name.h"
#include "user-util.h"
@ -177,9 +177,8 @@ typedef struct BootId {
} BootId;
static int add_matches_for_device(sd_journal *j, const char *devpath) {
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
sd_device *d = NULL;
struct stat st;
int r;
@ -191,33 +190,25 @@ static int add_matches_for_device(sd_journal *j, const char *devpath) {
return -EINVAL;
}
udev = udev_new();
if (!udev)
return log_oom();
if (stat(devpath, &st) < 0)
return log_error_errno(errno, "Couldn't stat file: %m");
r = udev_device_new_from_stat_rdev(udev, &st, &device);
r = device_new_from_stat_rdev(&device, &st);
if (r < 0)
return log_error_errno(r, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
return log_error_errno(r, "Failed to get device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
d = device;
while (d) {
for (d = device; d; ) {
_cleanup_free_ char *match = NULL;
const char *subsys, *sysname, *devnode;
sd_device *parent;
subsys = udev_device_get_subsystem(d);
if (!subsys) {
d = udev_device_get_parent(d);
continue;
}
r = sd_device_get_subsystem(d, &subsys);
if (r < 0)
goto get_parent;
sysname = udev_device_get_sysname(d);
if (!sysname) {
d = udev_device_get_parent(d);
continue;
}
r = sd_device_get_sysname(d, &sysname);
if (r < 0)
goto get_parent;
match = strjoin("_KERNEL_DEVICE=+", subsys, ":", sysname);
if (!match)
@ -227,8 +218,7 @@ static int add_matches_for_device(sd_journal *j, const char *devpath) {
if (r < 0)
return log_error_errno(r, "Failed to add match: %m");
devnode = udev_device_get_devnode(d);
if (devnode) {
if (sd_device_get_devname(d, &devnode) >= 0) {
_cleanup_free_ char *match1 = NULL;
r = stat(devnode, &st);
@ -244,7 +234,11 @@ static int add_matches_for_device(sd_journal *j, const char *devpath) {
return log_error_errno(r, "Failed to add match: %m");
}
d = udev_device_get_parent(d);
get_parent:
if (sd_device_get_parent(d, &parent) < 0)
break;
d = parent;
}
r = add_match_this_boot(j, arg_machine);

View file

@ -6,10 +6,11 @@
#include <sys/socket.h>
#include <unistd.h>
#include "libudev.h"
#include "sd-device.h"
#include "sd-messages.h"
#include "alloc-util.h"
#include "device-util.h"
#include "escape.h"
#include "fd-util.h"
#include "format-util.h"
@ -209,16 +210,13 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) {
}
if (kernel_device) {
struct udev_device *ud;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
ud = udev_device_new_from_device_id(s->udev, kernel_device);
if (ud) {
if (sd_device_new_from_device_id(&d, kernel_device) >= 0) {
const char *g;
struct udev_list_entry *ll;
char *b;
g = udev_device_get_devnode(ud);
if (g) {
if (sd_device_get_devname(d, &g) >= 0) {
b = strappend("_UDEV_DEVNODE=", g);
if (b) {
iovec[n++] = IOVEC_MAKE_STRING(b);
@ -226,8 +224,7 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) {
}
}
g = udev_device_get_sysname(ud);
if (g) {
if (sd_device_get_sysname(d, &g) >= 0) {
b = strappend("_UDEV_SYSNAME=", g);
if (b) {
iovec[n++] = IOVEC_MAKE_STRING(b);
@ -236,25 +233,19 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) {
}
j = 0;
ll = udev_device_get_devlinks_list_entry(ud);
udev_list_entry_foreach(ll, ll) {
FOREACH_DEVICE_DEVLINK(d, g) {
if (j > N_IOVEC_UDEV_FIELDS)
break;
g = udev_list_entry_get_name(ll);
if (g) {
b = strappend("_UDEV_DEVLINK=", g);
if (b) {
iovec[n++] = IOVEC_MAKE_STRING(b);
z++;
}
b = strappend("_UDEV_DEVLINK=", g);
if (b) {
iovec[n++] = IOVEC_MAKE_STRING(b);
z++;
}
j++;
}
udev_device_unref(ud);
}
}

View file

@ -9,7 +9,6 @@
#include <sys/statvfs.h>
#include <linux/sockios.h>
#include "libudev.h"
#include "sd-daemon.h"
#include "sd-journal.h"
#include "sd-messages.h"
@ -1845,10 +1844,6 @@ int server_init(Server *s) {
if (r < 0)
return r;
s->udev = udev_new();
if (!s->udev)
return -ENOMEM;
s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
if (!s->rate_limit)
return -ENOMEM;
@ -1949,8 +1944,6 @@ void server_done(Server *s) {
if (s->mmap)
mmap_cache_unref(s->mmap);
udev_unref(s->udev);
}
static const char* const storage_table[_STORAGE_MAX] = {

View file

@ -137,8 +137,6 @@ struct Server {
Set *deferred_closes;
struct udev *udev;
uint64_t *kernel_seqnum;
bool dev_kmsg_readable:1;

View file

@ -1,6 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "libudev.h"
#include "sd-device.h"
#include "sd-id128.h"
#include "dhcp-identifier.h"
@ -8,7 +8,6 @@
#include "network-internal.h"
#include "siphash24.h"
#include "sparse-endian.h"
#include "udev-util.h"
#include "virt.h"
#define SYSTEMD_PEN 43793
@ -71,25 +70,23 @@ int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len) {
}
int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_id) {
/* name is a pointer to memory in the udev_device struct, so must
have the same scope */
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
/* name is a pointer to memory in the sd_device struct, so must
* have the same scope */
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
const char *name = NULL;
uint64_t id;
if (detect_container() <= 0) {
/* not in a container, udev will be around */
_cleanup_(udev_unrefp) struct udev *udev;
char ifindex_str[2 + DECIMAL_STR_MAX(int)];
udev = udev_new();
if (!udev)
return -ENOMEM;
int initialized, r;
sprintf(ifindex_str, "n%d", ifindex);
device = udev_device_new_from_device_id(udev, ifindex_str);
if (device) {
if (udev_device_get_is_initialized(device) <= 0)
if (sd_device_new_from_device_id(&device, ifindex_str) >= 0) {
r = sd_device_get_is_initialized(device, &initialized);
if (r < 0)
return r;
if (!initialized)
/* not yet ready */
return -EBUSY;

View file

@ -23,24 +23,22 @@
#include "utf8.h"
#include "util.h"
const char *net_get_name(struct udev_device *device) {
const char *net_get_name(sd_device *device) {
const char *name, *field;
assert(device);
/* fetch some persistent data unique (on this machine) to this device */
FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
name = udev_device_get_property_value(device, field);
if (name)
FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
if (sd_device_get_property_value(device, field, &name) >= 0)
return name;
}
return NULL;
}
#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result) {
int net_get_unique_predictable_data(sd_device *device, uint64_t *result) {
size_t l, sz = 0;
const char *name = NULL;
int r;

View file

@ -3,12 +3,12 @@
#include <stdbool.h>
#include "sd-device.h"
#include "sd-dhcp-lease.h"
#include "condition.h"
#include "conf-parser.h"
#include "set.h"
#include "udev.h"
#define LINK_BRIDGE_PORT_PRIORITY_INVALID 128
#define LINK_BRIDGE_PORT_PRIORITY_MAX 63
@ -38,8 +38,8 @@ CONFIG_PARSER_PROTOTYPE(config_parse_ifalias);
CONFIG_PARSER_PROTOTYPE(config_parse_iaid);
CONFIG_PARSER_PROTOTYPE(config_parse_bridge_port_priority);
int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result);
const char *net_get_name(struct udev_device *device);
int net_get_unique_predictable_data(sd_device *device, uint64_t *result);
const char *net_get_name(sd_device *device);
void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size);
int deserialize_in_addrs(struct in_addr **addresses, const char *string);

View file

@ -25,6 +25,7 @@
#include "parse-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "unaligned.h"
int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {

View file

@ -843,6 +843,22 @@ int device_new_from_synthetic_event(sd_device **new_device, const char *syspath,
return 0;
}
int device_new_from_stat_rdev(sd_device **ret, const struct stat *st) {
char type;
assert(ret);
assert(st);
if (S_ISBLK(st->st_mode))
type = 'b';
else if (S_ISCHR(st->st_mode))
type = 'c';
else
return -ENOTTY;
return sd_device_new_from_devnum(ret, type, st->st_rdev);
}
int device_copy_properties(sd_device *device_dst, sd_device *device_src) {
const char *property, *value;
int r;

View file

@ -3,12 +3,14 @@
#include <inttypes.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "sd-device.h"
int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len);
int device_new_from_strv(sd_device **ret, char **strv);
int device_new_from_stat_rdev(sd_device **ret, const struct stat *st);
int device_get_id_filename(sd_device *device, const char **ret);

View file

@ -196,8 +196,6 @@ _public_ const char *udev_device_get_property_value(struct udev_device *udev_dev
struct udev_device *udev_device_new(struct udev *udev) {
struct udev_device *udev_device;
assert_return_errno(udev, NULL, EINVAL);
udev_device = new0(struct udev_device, 1);
if (!udev_device) {
errno = ENOMEM;

View file

@ -52,8 +52,6 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
_cleanup_free_ struct udev_enumerate *udev_enumerate = NULL;
int r;
assert_return_errno(udev, NULL, EINVAL);
udev_enumerate = new0(struct udev_enumerate, 1);
if (!udev_enumerate) {
errno = ENOMEM;

View file

@ -40,8 +40,6 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
struct udev_hwdb *hwdb;
int r;
assert_return_errno(udev, NULL, EINVAL);
r = sd_hwdb_new(&hwdb_internal);
if (r < 0) {
errno = -r;

View file

@ -18,6 +18,7 @@
#include "fileio.h"
#include "format-util.h"
#include "libudev-private.h"
#include "libudev-device-internal.h"
#include "missing.h"
#include "mount-util.h"
#include "socket-util.h"
@ -153,11 +154,6 @@ struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const c
struct udev_monitor *udev_monitor;
unsigned int group;
if (udev == NULL) {
errno = EINVAL;
return NULL;
}
if (name == NULL)
group = UDEV_MONITOR_NONE;
else if (streq(name, "udev")) {
@ -707,6 +703,19 @@ retry:
return udev_device;
}
int udev_monitor_receive_sd_device(struct udev_monitor *udev_monitor, sd_device **ret) {
_cleanup_(udev_device_unrefp) struct udev_device *udev_device = NULL;
assert(ret);
udev_device = udev_monitor_receive_device(udev_monitor);
if (!udev_device)
return -errno;
*ret = sd_device_ref(udev_device->device);
return 0;
}
int udev_monitor_send_device(struct udev_monitor *udev_monitor,
struct udev_monitor *destination, struct udev_device *udev_device)
{

View file

@ -6,6 +6,7 @@
#include <stdint.h>
#include "libudev.h"
#include "sd-device.h"
#include "macro.h"
#include "mkdir.h"
@ -62,6 +63,7 @@ int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct
int udev_monitor_send_device(struct udev_monitor *udev_monitor,
struct udev_monitor *destination, struct udev_device *udev_device);
struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd);
int udev_monitor_receive_sd_device(struct udev_monitor *udev_monitor, sd_device **ret);
/* libudev-list.c */
struct udev_list_node {
@ -126,3 +128,11 @@ uint64_t util_string_bloom64(const char *str);
/* libudev-util-private.c */
int util_resolve_subsys_kernel(struct udev *udev, const char *string, char *result, size_t maxsize, int read_value);
/* Cleanup functions */
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev*, udev_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_device*, udev_device_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_enumerate*, udev_enumerate_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_hwdb*, udev_hwdb_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_queue*, udev_queue_unref);

View file

@ -45,11 +45,6 @@ _public_ struct udev_queue *udev_queue_new(struct udev *udev)
{
struct udev_queue *udev_queue;
if (udev == NULL) {
errno = EINVAL;
return NULL;
}
udev_queue = new0(struct udev_queue, 1);
if (udev_queue == NULL) {
errno = ENOMEM;

View file

@ -3,8 +3,11 @@
#include <errno.h>
#include <string.h>
#include "sd-device.h"
#include "acl-util.h"
#include "alloc-util.h"
#include "device-enumerator-private.h"
#include "dirent-util.h"
#include "escape.h"
#include "fd-util.h"
@ -12,7 +15,6 @@
#include "logind-acl.h"
#include "set.h"
#include "string-util.h"
#include "udev-util.h"
#include "util.h"
static int flush_acl(acl_t acl) {
@ -157,30 +159,27 @@ finish:
return r;
}
int devnode_acl_all(struct udev *udev,
const char *seat,
int devnode_acl_all(const char *seat,
bool flush,
bool del, uid_t old_uid,
bool add, uid_t new_uid) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
_cleanup_set_free_free_ Set *nodes = NULL;
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *dent;
sd_device *d;
Iterator i;
char *n;
int r;
assert(udev);
nodes = set_new(&path_hash_ops);
if (!nodes)
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
if (isempty(seat))
seat = "seat0";
@ -190,37 +189,25 @@ int devnode_acl_all(struct udev *udev,
* could add the seat name as second match tag, but this would
* be hardly optimizable in libudev, and hence checking the
* second tag manually in our loop is a good solution. */
r = udev_enumerate_add_match_tag(e, "uaccess");
r = sd_device_enumerator_add_match_tag(e, "uaccess");
if (r < 0)
return r;
r = udev_enumerate_add_match_is_initialized(e);
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
const char *node, *sn;
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
sn = udev_device_get_property_value(d, "ID_SEAT");
if (isempty(sn))
if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn))
sn = "seat0";
if (!streq(seat, sn))
continue;
node = udev_device_get_devnode(d);
/* In case people mistag devices with nodes, we need to ignore this */
if (!node)
if (sd_device_get_devname(d, &node) < 0)
continue;
n = strdup(node);

View file

@ -4,8 +4,6 @@
#include <stdbool.h>
#include <sys/types.h>
#include "libudev.h"
#if HAVE_ACL
int devnode_acl(const char *path,
@ -13,8 +11,7 @@ int devnode_acl(const char *path,
bool del, uid_t old_uid,
bool add, uid_t new_uid);
int devnode_acl_all(struct udev *udev,
const char *seat,
int devnode_acl_all(const char *seat,
bool flush,
bool del, uid_t old_uid,
bool add, uid_t new_uid);
@ -27,8 +24,7 @@ static inline int devnode_acl(const char *path,
return 0;
}
static inline int devnode_acl_all(struct udev *udev,
const char *seat,
static inline int devnode_acl_all(const char *seat,
bool flush,
bool del, uid_t old_uid,
bool add, uid_t new_uid) {

View file

@ -6,18 +6,20 @@
#include <sys/types.h>
#include <linux/vt.h>
#include "sd-device.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "conf-parser.h"
#include "device-enumerator-private.h"
#include "fd-util.h"
#include "logind.h"
#include "parse-util.h"
#include "process-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "udev-util.h"
#include "user-util.h"
void manager_reset_config(Manager *m) {
@ -215,15 +217,22 @@ int manager_add_button(Manager *m, const char *name, Button **_button) {
return 0;
}
int manager_process_seat_device(Manager *m, struct udev_device *d) {
int manager_process_seat_device(Manager *m, sd_device *d) {
const char *action = NULL;
Device *device;
int r;
assert(m);
if (streq_ptr(udev_device_get_action(d), "remove")) {
(void) sd_device_get_property_value(d, "ACTION", &action);
if (streq_ptr(action, "remove")) {
const char *syspath;
device = hashmap_get(m->devices, udev_device_get_syspath(d));
r = sd_device_get_syspath(d, &syspath);
if (r < 0)
return 0;
device = hashmap_get(m->devices, syspath);
if (!device)
return 0;
@ -231,12 +240,11 @@ int manager_process_seat_device(Manager *m, struct udev_device *d) {
device_free(device);
} else {
const char *sn;
Seat *seat = NULL;
const char *sn, *syspath;
bool master;
Seat *seat;
sn = udev_device_get_property_value(d, "ID_SEAT");
if (isempty(sn))
if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn))
sn = "seat0";
if (!seat_name_is_valid(sn)) {
@ -245,13 +253,17 @@ int manager_process_seat_device(Manager *m, struct udev_device *d) {
}
seat = hashmap_get(m->seats, sn);
master = udev_device_has_tag(d, "master-of-seat");
master = sd_device_has_tag(d, "master-of-seat") > 0;
/* Ignore non-master devices for unknown seats */
if (!master && !seat)
return 0;
r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
r = sd_device_get_syspath(d, &syspath);
if (r < 0)
return r;
r = manager_add_device(m, syspath, master, &device);
if (r < 0)
return r;
@ -272,30 +284,34 @@ int manager_process_seat_device(Manager *m, struct udev_device *d) {
return 0;
}
int manager_process_button_device(Manager *m, struct udev_device *d) {
int manager_process_button_device(Manager *m, sd_device *d) {
const char *action = NULL, *sysname;
Button *b;
int r;
assert(m);
if (streq_ptr(udev_device_get_action(d), "remove")) {
r = sd_device_get_sysname(d, &sysname);
if (r < 0)
return r;
b = hashmap_get(m->buttons, udev_device_get_sysname(d));
(void) sd_device_get_property_value(d, "ACTION", &action);
if (streq_ptr(action, "remove")) {
b = hashmap_get(m->buttons, sysname);
if (!b)
return 0;
button_free(b);
} else {
const char *sn;
const char *sn = NULL;
r = manager_add_button(m, udev_device_get_sysname(d), &b);
r = manager_add_button(m, sysname, &b);
if (r < 0)
return r;
sn = udev_device_get_property_value(d, "ID_SEAT");
if (isempty(sn))
if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn))
sn = "seat0";
button_set_seat(b, sn);
@ -539,46 +555,41 @@ static bool manager_is_docked(Manager *m) {
}
static int manager_count_external_displays(Manager *m) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
int r;
int n = 0;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *d;
int r, n = 0;
e = udev_enumerate_new(m->udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "drm");
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
struct udev_device *p;
const char *status, *enabled, *dash, *nn, *i;
r = sd_device_enumerator_add_match_subsystem(e, "drm", true);
if (r < 0)
return r;
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
sd_device *p;
const char *status, *enabled, *dash, *nn, *i, *subsys;
bool external = false;
d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
p = udev_device_get_parent(d);
if (!p)
if (sd_device_get_parent(d, &p) < 0)
continue;
/* If the parent shares the same subsystem as the
* device we are looking at then it is a connector,
* which is what we are interested in. */
if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
if (sd_device_get_subsystem(p, &subsys) < 0 || !streq(subsys, "drm"))
continue;
nn = udev_device_get_sysname(d);
if (!nn)
if (sd_device_get_sysname(d, &nn) < 0)
continue;
/* Ignore internal displays: the type is encoded in
@ -605,16 +616,12 @@ static int manager_count_external_displays(Manager *m) {
continue;
/* Ignore ports that are not enabled */
enabled = udev_device_get_sysattr_value(d, "enabled");
if (!enabled)
continue;
if (!streq_ptr(enabled, "enabled"))
if (sd_device_get_sysattr_value(d, "enabled", &enabled) < 0 || !streq(enabled, "enabled"))
continue;
/* We count any connector which is not explicitly
* "disconnected" as connected. */
status = udev_device_get_sysattr_value(d, "status");
if (!streq_ptr(status, "disconnected"))
if (sd_device_get_sysattr_value(d, "status", &status) < 0 || !streq(status, "disconnected"))
n++;
}

View file

@ -5,6 +5,7 @@
#include <string.h>
#include <unistd.h>
#include "sd-device.h"
#include "sd-messages.h"
#include "alloc-util.h"
@ -13,6 +14,7 @@
#include "bus-error.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "device-enumerator-private.h"
#include "dirent-util.h"
#include "efivars.h"
#include "escape.h"
@ -30,7 +32,6 @@
#include "special.h"
#include "strv.h"
#include "terminal-util.h"
#include "udev-util.h"
#include "unit-name.h"
#include "user-util.h"
#include "utmp-wtmp.h"
@ -1189,33 +1190,37 @@ static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bu
return sd_bus_reply_method_return(message, NULL);
}
static int trigger_device(Manager *m, struct udev_device *d) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *first, *item;
static int trigger_device(Manager *m, sd_device *d) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
int r;
assert(m);
e = udev_enumerate_new(m->udev);
if (!e)
return -ENOMEM;
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
if (d) {
r = udev_enumerate_add_match_parent(e, d);
r = sd_device_enumerator_add_match_parent(e, d);
if (r < 0)
return r;
}
r = udev_enumerate_scan_devices(e);
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
_cleanup_free_ char *t = NULL;
const char *p;
p = udev_list_entry_get_name(item);
r = sd_device_get_syspath(d, &p);
if (r < 0)
return r;
t = strappend(p, "/uevent");
if (!t)
@ -1228,7 +1233,7 @@ static int trigger_device(Manager *m, struct udev_device *d) {
}
static int attach_device(Manager *m, const char *seat, const char *sysfs) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_free_ char *rule = NULL, *file = NULL;
const char *id_for_seat;
int r;
@ -1237,15 +1242,14 @@ static int attach_device(Manager *m, const char *seat, const char *sysfs) {
assert(seat);
assert(sysfs);
d = udev_device_new_from_syspath(m->udev, sysfs);
if (!d)
r = sd_device_new_from_syspath(&d, sysfs);
if (r < 0)
return r;
if (sd_device_has_tag(d, "seat") <= 0)
return -ENODEV;
if (!udev_device_has_tag(d, "seat"))
return -ENODEV;
id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
if (!id_for_seat)
if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0)
return -ENODEV;
if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
@ -1254,7 +1258,7 @@ static int attach_device(Manager *m, const char *seat, const char *sysfs) {
if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
return -ENOMEM;
mkdir_p_label("/etc/udev/rules.d", 0755);
(void) mkdir_p_label("/etc/udev/rules.d", 0755);
r = write_string_file_atomic_label(file, rule);
if (r < 0)
return r;

View file

@ -203,8 +203,7 @@ int seat_apply_acls(Seat *s, Session *old_active) {
assert(s);
r = devnode_acl_all(s->manager->udev,
s->id,
r = devnode_acl_all(s->id,
false,
!!old_active, old_active ? old_active->user->uid : 0,
!!s->active, s->active ? s->active->user->uid : 0);

View file

@ -6,7 +6,7 @@
#include <sys/ioctl.h>
#include <sys/types.h>
#include "libudev.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "bus-util.h"
@ -247,13 +247,13 @@ static void session_device_stop(SessionDevice *sd) {
sd->active = false;
}
static DeviceType detect_device_type(struct udev_device *dev) {
static DeviceType detect_device_type(sd_device *dev) {
const char *sysname, *subsystem;
DeviceType type;
DeviceType type = DEVICE_TYPE_UNKNOWN;
sysname = udev_device_get_sysname(dev);
subsystem = udev_device_get_subsystem(dev);
type = DEVICE_TYPE_UNKNOWN;
if (sd_device_get_sysname(dev, &sysname) < 0 ||
sd_device_get_subsystem(dev, &subsystem) < 0)
return type;
if (streq_ptr(subsystem, "drm")) {
if (startswith(sysname, "card"))
@ -267,42 +267,37 @@ static DeviceType detect_device_type(struct udev_device *dev) {
}
static int session_device_verify(SessionDevice *sd) {
struct udev_device *dev, *p = NULL;
const char *sp, *node;
_cleanup_(sd_device_unrefp) sd_device *p = NULL;
sd_device *dev;
const char *sp = NULL, *node;
int r;
dev = udev_device_new_from_devnum(sd->session->manager->udev, 'c', sd->dev);
if (!dev)
if (sd_device_new_from_devnum(&p, 'c', sd->dev) < 0)
return -ENODEV;
sp = udev_device_get_syspath(dev);
node = udev_device_get_devnode(dev);
if (!node) {
r = -EINVAL;
goto err_dev;
}
dev = p;
(void) sd_device_get_syspath(dev, &sp);
if (sd_device_get_devname(dev, &node) < 0)
return -EINVAL;
/* detect device type so we can find the correct sysfs parent */
sd->type = detect_device_type(dev);
if (sd->type == DEVICE_TYPE_UNKNOWN) {
r = -ENODEV;
goto err_dev;
} else if (sd->type == DEVICE_TYPE_EVDEV) {
if (sd->type == DEVICE_TYPE_UNKNOWN)
return -ENODEV;
else if (sd->type == DEVICE_TYPE_EVDEV) {
/* for evdev devices we need the parent node as device */
p = dev;
dev = udev_device_get_parent_with_subsystem_devtype(p, "input", NULL);
if (!dev) {
r = -ENODEV;
goto err_dev;
}
sp = udev_device_get_syspath(dev);
} else if (sd->type != DEVICE_TYPE_DRM) {
if (sd_device_get_parent_with_subsystem_devtype(p, "input", NULL, &dev) < 0)
return -ENODEV;
if (sd_device_get_syspath(dev, &sp) < 0)
return -ENODEV;
} else if (sd->type != DEVICE_TYPE_DRM)
/* Prevent opening unsupported devices. Especially devices of
* subsystem "input" must be opened via the evdev node as
* we require EVIOCREVOKE. */
r = -ENODEV;
goto err_dev;
}
return -ENODEV;
/* search for an existing seat device and return it if available */
sd->device = hashmap_get(sd->session->manager->devices, sp);
@ -312,31 +307,22 @@ static int session_device_verify(SessionDevice *sd) {
* logind-manager handle the new device. */
r = manager_process_seat_device(sd->session->manager, dev);
if (r < 0)
goto err_dev;
return r;
/* if it's still not available, then the device is invalid */
sd->device = hashmap_get(sd->session->manager->devices, sp);
if (!sd->device) {
r = -ENODEV;
goto err_dev;
}
if (!sd->device)
return -ENODEV;
}
if (sd->device->seat != sd->session->seat) {
r = -EPERM;
goto err_dev;
}
if (sd->device->seat != sd->session->seat)
return -EPERM;
sd->node = strdup(node);
if (!sd->node) {
r = -ENOMEM;
goto err_dev;
}
if (!sd->node)
return -ENOMEM;
r = 0;
err_dev:
udev_device_unref(p ? : dev);
return r;
return 0;
}
int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) {

View file

@ -5,25 +5,26 @@
#include <string.h>
#include <unistd.h>
#include "libudev.h"
#include "sd-daemon.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "def.h"
#include "device-enumerator-private.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "fs-util.h"
#include "libudev-private.h"
#include "logind.h"
#include "parse-util.h"
#include "process-util.h"
#include "selinux-util.h"
#include "signal-util.h"
#include "strv.h"
#include "udev-util.h"
static Manager* manager_unref(Manager *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
@ -56,10 +57,6 @@ static int manager_new(Manager **ret) {
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
return -ENOMEM;
m->udev = udev_new();
if (!m->udev)
return -errno;
r = sd_event_default(&m->event);
if (r < 0)
return r;
@ -139,8 +136,6 @@ static Manager* manager_unref(Manager *m) {
udev_monitor_unref(m->udev_vcsa_monitor);
udev_monitor_unref(m->udev_button_monitor);
udev_unref(m->udev);
if (m->unlink_nologin)
(void) unlink_or_warn("/run/nologin");
@ -163,8 +158,8 @@ static Manager* manager_unref(Manager *m) {
}
static int manager_enumerate_devices(Manager *m) {
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *d;
int r;
assert(m);
@ -172,31 +167,21 @@ static int manager_enumerate_devices(Manager *m) {
/* Loads devices from udev and creates seats for them as
* necessary */
e = udev_enumerate_new(m->udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_tag(e, "master-of-seat");
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = udev_enumerate_add_match_is_initialized(e);
r = sd_device_enumerator_add_match_tag(e, "master-of-seat");
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
int k;
d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
k = manager_process_seat_device(m, d);
if (k < 0)
r = k;
@ -206,8 +191,8 @@ static int manager_enumerate_devices(Manager *m) {
}
static int manager_enumerate_buttons(Manager *m) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *d;
int r;
assert(m);
@ -217,35 +202,25 @@ static int manager_enumerate_buttons(Manager *m) {
if (manager_all_buttons_ignored(m))
return 0;
e = udev_enumerate_new(m->udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "input");
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = udev_enumerate_add_match_tag(e, "power-switch");
r = sd_device_enumerator_add_match_subsystem(e, "input", true);
if (r < 0)
return r;
r = udev_enumerate_add_match_is_initialized(e);
r = sd_device_enumerator_add_match_tag(e, "power-switch");
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
int k;
d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
k = manager_process_button_device(m, d);
if (k < 0)
r = k;
@ -566,64 +541,69 @@ static int manager_enumerate_inhibitors(Manager *m) {
}
static int manager_dispatch_seat_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
Manager *m = userdata;
int r;
assert(m);
d = udev_monitor_receive_device(m->udev_seat_monitor);
if (!d)
return -ENOMEM;
r = udev_monitor_receive_sd_device(m->udev_seat_monitor, &d);
if (r < 0)
return r;
manager_process_seat_device(m, d);
return 0;
}
static int manager_dispatch_device_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
Manager *m = userdata;
int r;
assert(m);
d = udev_monitor_receive_device(m->udev_device_monitor);
if (!d)
return -ENOMEM;
r = udev_monitor_receive_sd_device(m->udev_device_monitor, &d);
if (r < 0)
return r;
manager_process_seat_device(m, d);
return 0;
}
static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
Manager *m = userdata;
const char *name;
const char *name, *action;
int r;
assert(m);
d = udev_monitor_receive_device(m->udev_vcsa_monitor);
if (!d)
return -ENOMEM;
name = udev_device_get_sysname(d);
r = udev_monitor_receive_sd_device(m->udev_vcsa_monitor, &d);
if (r < 0)
return r;
/* Whenever a VCSA device is removed try to reallocate our
* VTs, to make sure our auto VTs never go away. */
if (name && startswith(name, "vcsa") && streq_ptr(udev_device_get_action(d), "remove"))
if (sd_device_get_sysname(d, &name) >= 0 &&
startswith(name, "vcsa") &&
sd_device_get_property_value(d, "ACTION", &action) >= 0 &&
streq_ptr(action, "remove"))
seat_preallocate_vts(m->seat0);
return 0;
}
static int manager_dispatch_button_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
Manager *m = userdata;
int r;
assert(m);
d = udev_monitor_receive_device(m->udev_button_monitor);
if (!d)
return -ENOMEM;
r = udev_monitor_receive_sd_device(m->udev_button_monitor, &d);
if (r < 0)
return r;
manager_process_button_device(m, d);
return 0;
@ -869,7 +849,7 @@ static int manager_connect_udev(Manager *m) {
assert(!m->udev_vcsa_monitor);
assert(!m->udev_button_monitor);
m->udev_seat_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
m->udev_seat_monitor = udev_monitor_new_from_netlink(NULL, "udev");
if (!m->udev_seat_monitor)
return -ENOMEM;
@ -885,7 +865,7 @@ static int manager_connect_udev(Manager *m) {
if (r < 0)
return r;
m->udev_device_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
m->udev_device_monitor = udev_monitor_new_from_netlink(NULL, "udev");
if (!m->udev_device_monitor)
return -ENOMEM;
@ -911,7 +891,7 @@ static int manager_connect_udev(Manager *m) {
/* Don't watch keys if nobody cares */
if (!manager_all_buttons_ignored(m)) {
m->udev_button_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
m->udev_button_monitor = udev_monitor_new_from_netlink(NULL, "udev");
if (!m->udev_button_monitor)
return -ENOMEM;
@ -935,7 +915,7 @@ static int manager_connect_udev(Manager *m) {
/* Don't bother watching VCSA devices, if nobody cares */
if (m->n_autovts > 0 && m->console_active_fd >= 0) {
m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
m->udev_vcsa_monitor = udev_monitor_new_from_netlink(NULL, "udev");
if (!m->udev_vcsa_monitor)
return -ENOMEM;

View file

@ -5,6 +5,7 @@
#include "libudev.h"
#include "sd-bus.h"
#include "sd-device.h"
#include "sd-event.h"
#include "conf-parser.h"
@ -34,7 +35,6 @@ struct Manager {
LIST_HEAD(Session, session_gc_queue);
LIST_HEAD(User, user_gc_queue);
struct udev *udev;
struct udev_monitor *udev_seat_monitor, *udev_device_monitor, *udev_vcsa_monitor, *udev_button_monitor;
sd_event_source *console_active_event_source;
@ -133,8 +133,8 @@ int manager_add_user_by_name(Manager *m, const char *name, User **_user);
int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user);
int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor);
int manager_process_seat_device(Manager *m, struct udev_device *d);
int manager_process_button_device(Manager *m, struct udev_device *d);
int manager_process_seat_device(Manager *m, sd_device *d);
int manager_process_button_device(Manager *m, sd_device *d);
int manager_spawn_autovt(Manager *m, unsigned int vtnr);

View file

@ -3,31 +3,33 @@
#include <errno.h>
#include <string.h>
#include "libudev.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "device-enumerator-private.h"
#include "locale-util.h"
#include "path-util.h"
#include "string-util.h"
#include "sysfs-show.h"
#include "terminal-util.h"
#include "udev-util.h"
#include "util.h"
static int show_sysfs_one(
struct udev *udev,
const char *seat,
struct udev_list_entry **item,
sd_device **dev_list,
size_t *i_dev,
size_t n_dev,
const char *sub,
const char *prefix,
unsigned n_columns,
OutputFlags flags) {
size_t max_width;
int r;
assert(udev);
assert(seat);
assert(item);
assert(dev_list);
assert(i_dev);
assert(prefix);
if (flags & OUTPUT_FULL_WIDTH)
@ -37,73 +39,58 @@ static int show_sysfs_one(
else
max_width = n_columns;
while (*item) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
struct udev_list_entry *next, *lookahead;
const char *sn, *name, *sysfs, *subsystem, *sysname;
while (*i_dev < n_dev) {
const char *sysfs, *sn, *name = NULL, *subsystem = NULL, *sysname = NULL;
_cleanup_free_ char *k = NULL, *l = NULL;
size_t lookahead;
bool is_master;
sysfs = udev_list_entry_get_name(*item);
if (!path_startswith(sysfs, sub))
if (sd_device_get_syspath(dev_list[*i_dev], &sysfs) < 0 ||
!path_startswith(sysfs, sub))
return 0;
d = udev_device_new_from_syspath(udev, sysfs);
if (!d) {
*item = udev_list_entry_get_next(*item);
continue;
}
sn = udev_device_get_property_value(d, "ID_SEAT");
if (isempty(sn))
if (sd_device_get_property_value(dev_list[*i_dev], "ID_SEAT", &sn) < 0 || isempty(sn))
sn = "seat0";
/* Explicitly also check for tag 'seat' here */
if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
*item = udev_list_entry_get_next(*item);
if (!streq(seat, sn) || sd_device_has_tag(dev_list[*i_dev], "seat") <= 0) {
(*i_dev)++;
continue;
}
is_master = udev_device_has_tag(d, "master-of-seat");
is_master = sd_device_has_tag(dev_list[*i_dev], "master-of-seat") > 0;
name = udev_device_get_sysattr_value(d, "name");
if (!name)
name = udev_device_get_sysattr_value(d, "id");
subsystem = udev_device_get_subsystem(d);
sysname = udev_device_get_sysname(d);
if (sd_device_get_sysattr_value(dev_list[*i_dev], "name", &name) < 0)
(void) sd_device_get_sysattr_value(dev_list[*i_dev], "id", &name);
(void) sd_device_get_subsystem(dev_list[*i_dev], &subsystem);
(void) sd_device_get_sysname(dev_list[*i_dev], &sysname);
/* Look if there's more coming after this */
lookahead = next = udev_list_entry_get_next(*item);
while (lookahead) {
for (lookahead = *i_dev + 1; lookahead < n_dev; lookahead++) {
const char *lookahead_sysfs;
lookahead_sysfs = udev_list_entry_get_name(lookahead);
if (sd_device_get_syspath(dev_list[lookahead], &lookahead_sysfs) < 0)
continue;
if (path_startswith(lookahead_sysfs, sub) &&
!path_startswith(lookahead_sysfs, sysfs)) {
_cleanup_(udev_device_unrefp) struct udev_device *lookahead_d = NULL;
const char *lookahead_sn;
lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
if (lookahead_d) {
const char *lookahead_sn;
if (sd_device_get_property_value(dev_list[lookahead], "ID_SEAT", &lookahead_sn) < 0 ||
isempty(lookahead_sn))
lookahead_sn = "seat0";
lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
if (isempty(lookahead_sn))
lookahead_sn = "seat0";
if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
break;
}
if (streq(seat, lookahead_sn) && sd_device_has_tag(dev_list[lookahead], "seat") > 0)
break;
}
lookahead = udev_list_entry_get_next(lookahead);
}
k = ellipsize(sysfs, max_width, 20);
if (!k)
return -ENOMEM;
printf("%s%s%s\n", prefix, special_glyph(lookahead ? TREE_BRANCH : TREE_RIGHT), k);
printf("%s%s%s\n", prefix, special_glyph(lookahead < n_dev ? TREE_BRANCH : TREE_RIGHT), k);
if (asprintf(&l,
"%s%s:%s%s%s%s",
@ -117,29 +104,31 @@ static int show_sysfs_one(
if (!k)
return -ENOMEM;
printf("%s%s%s\n", prefix, lookahead ? special_glyph(TREE_VERTICAL) : " ", k);
printf("%s%s%s\n", prefix, lookahead < n_dev ? special_glyph(TREE_VERTICAL) : " ", k);
*item = next;
if (*item) {
if (++(*i_dev) < n_dev) {
_cleanup_free_ char *p = NULL;
p = strappend(prefix, lookahead ? special_glyph(TREE_VERTICAL) : " ");
p = strappend(prefix, lookahead < n_dev ? special_glyph(TREE_VERTICAL) : " ");
if (!p)
return -ENOMEM;
show_sysfs_one(udev, seat, item, sysfs, p,
n_columns == (unsigned) -1 || n_columns < 2 ? n_columns : n_columns - 2,
flags);
r = show_sysfs_one(seat, dev_list, i_dev, n_dev, sysfs, p,
n_columns == (unsigned) -1 || n_columns < 2 ? n_columns : n_columns - 2,
flags);
if (r < 0)
return r;
}
}
return 0;
}
int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputFlags flags) {
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
struct udev_list_entry *first = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
size_t n_dev = 0, n_allocated = 0, i = 0;
sd_device *d, **dev_list = NULL;
int r;
if (n_columns <= 0)
@ -150,34 +139,43 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputF
if (isempty(seat))
seat = "seat0";
udev = udev_new();
if (!udev)
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
if (!streq(seat, "seat0"))
r = udev_enumerate_add_match_tag(e, seat);
else
r = udev_enumerate_add_match_tag(e, "seat");
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = udev_enumerate_add_match_is_initialized(e);
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
r = sd_device_enumerator_add_match_tag(e, streq(seat, "seat0") ? "seat" : seat);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
if (first)
show_sysfs_one(udev, seat, &first, "/", prefix, n_columns, flags);
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
const char *syspath;
if (sd_device_get_syspath(d, &syspath) < 0)
continue;
if (!GREEDY_REALLOC(dev_list, n_allocated, n_dev + 2))
return -ENOMEM;
dev_list[n_dev++] = sd_device_ref(d);
dev_list[n_dev] = NULL;
}
if (n_dev > 0)
show_sysfs_one(seat, dev_list, &i, n_dev, "/", prefix, n_columns, flags);
else
printf("%s%s%s\n", prefix, special_glyph(TREE_RIGHT), "(none)");
return r;
for (i = 0; i < n_dev; i++)
sd_device_unref(dev_list[i]);
free(dev_list);
return 0;
}

View file

@ -2,9 +2,10 @@
#include <getopt.h>
#include "libudev.h"
#include "sd-bus.h"
#include "sd-device.h"
#include "device-enumerator-private.h"
#include "bus-error.h"
#include "bus-unit-util.h"
#include "bus-util.h"
@ -21,7 +22,6 @@
#include "spawn-polkit-agent.h"
#include "stat-util.h"
#include "strv.h"
#include "udev-util.h"
#include "unit-def.h"
#include "unit-name.h"
#include "user-util.h"
@ -921,8 +921,7 @@ static int stop_mounts(
}
static int umount_by_device(sd_bus *bus, const char *what) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_strv_free_ char **list = NULL;
struct stat st;
const char *v;
@ -939,22 +938,20 @@ static int umount_by_device(sd_bus *bus, const char *what) {
return -ENOTBLK;
}
udev = udev_new();
if (!udev)
return log_oom();
r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
if (r < 0)
return log_error_errno(r, "Failed to get device from device number: %m");
d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
if (!d)
return log_oom();
r = sd_device_get_property_value(d, "ID_FS_USAGE", &v);
if (r < 0)
return log_error_errno(r, "Failed to get device property: %m");
v = udev_device_get_property_value(d, "ID_FS_USAGE");
if (!streq_ptr(v, "filesystem")) {
if (!streq(v, "filesystem")) {
log_error("%s does not contain a known file system.", what);
return -EINVAL;
}
v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE");
if (!isempty(v))
if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
r2 = stop_mounts(bus, v);
r = find_mount_points(what, &list);
@ -1042,7 +1039,7 @@ static int action_umount(
return r2;
}
static int acquire_mount_type(struct udev_device *d) {
static int acquire_mount_type(sd_device *d) {
const char *v;
assert(d);
@ -1050,8 +1047,7 @@ static int acquire_mount_type(struct udev_device *d) {
if (arg_mount_type)
return 0;
v = udev_device_get_property_value(d, "ID_FS_TYPE");
if (isempty(v))
if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
return 0;
arg_mount_type = strdup(v);
@ -1062,14 +1058,15 @@ static int acquire_mount_type(struct udev_device *d) {
return 1;
}
static int acquire_mount_options(struct udev_device *d) {
static int acquire_mount_options(sd_device *d) {
const char *v;
assert(d);
if (arg_mount_options)
return 0;
v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS");
if (isempty(v))
if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
return 0;
arg_mount_options = strdup(v);
@ -1080,38 +1077,41 @@ static int acquire_mount_options(struct udev_device *d) {
return 1;
}
static const char *get_model(struct udev_device *d) {
static const char *get_model(sd_device *d) {
const char *model;
assert(d);
model = udev_device_get_property_value(d, "ID_MODEL_FROM_DATABASE");
if (model)
if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0)
return model;
return udev_device_get_property_value(d, "ID_MODEL");
if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0)
return model;
return NULL;
}
static const char* get_label(struct udev_device *d) {
static const char* get_label(sd_device *d) {
const char *label;
assert(d);
label = udev_device_get_property_value(d, "ID_FS_LABEL");
if (label)
if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0)
return label;
return udev_device_get_property_value(d, "ID_PART_ENTRY_NAME");
if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0)
return label;
return NULL;
}
static int acquire_mount_where(struct udev_device *d) {
static int acquire_mount_where(sd_device *d) {
const char *v;
if (arg_mount_where)
return 0;
v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE");
if (isempty(v)) {
if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) {
_cleanup_free_ char *escaped = NULL;
const char *name;
@ -1121,8 +1121,7 @@ static int acquire_mount_where(struct udev_device *d) {
if (!name) {
const char *dn;
dn = udev_device_get_devnode(d);
if (!dn)
if (sd_device_get_devname(d, &dn) < 0)
return 0;
name = basename(dn);
@ -1171,7 +1170,7 @@ static int acquire_mount_where_for_loop_dev(const char *loop_dev) {
return 1;
}
static int acquire_description(struct udev_device *d) {
static int acquire_description(sd_device *d) {
const char *model, *label;
if (arg_description)
@ -1181,7 +1180,7 @@ static int acquire_description(struct udev_device *d) {
label = get_label(d);
if (!label)
label = udev_device_get_property_value(d, "ID_PART_ENTRY_NUMBER");
(void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
if (model && label)
arg_description = strjoin(model, " ", label);
@ -1199,7 +1198,7 @@ static int acquire_description(struct udev_device *d) {
return 1;
}
static int acquire_removable(struct udev_device *d) {
static int acquire_removable(sd_device *d) {
const char *v;
/* Shortcut this if there's no reason to check it */
@ -1207,15 +1206,13 @@ static int acquire_removable(struct udev_device *d) {
return 0;
for (;;) {
v = udev_device_get_sysattr_value(d, "removable");
if (v)
if (sd_device_get_sysattr_value(d, "removable", &v) > 0)
break;
d = udev_device_get_parent(d);
if (!d)
if (sd_device_get_parent(d, &d) < 0)
return 0;
if (!streq_ptr(udev_device_get_subsystem(d), "block"))
if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block"))
return 0;
}
@ -1243,8 +1240,7 @@ static int acquire_removable(struct udev_device *d) {
}
static int discover_loop_backing_file(void) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_free_ char *loop_dev = NULL;
struct stat st;
const char *v;
@ -1284,16 +1280,11 @@ static int discover_loop_backing_file(void) {
return -EINVAL;
}
udev = udev_new();
if (!udev)
return log_oom();
r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
if (r < 0)
return log_error_errno(r, "Failed to get device from device number: %m");
d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
if (!d)
return log_oom();
v = udev_device_get_property_value(d, "ID_FS_USAGE");
if (!streq_ptr(v, "filesystem")) {
if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
log_error("%s does not contain a known file system.", arg_mount_what);
return -EINVAL;
}
@ -1318,8 +1309,7 @@ static int discover_loop_backing_file(void) {
}
static int discover_device(void) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
struct stat st;
const char *v;
int r;
@ -1335,16 +1325,11 @@ static int discover_device(void) {
return -EINVAL;
}
udev = udev_new();
if (!udev)
return log_oom();
r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
if (r < 0)
return log_error_errno(r, "Failed to get device from device number: %m");
d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
if (!d)
return log_oom();
v = udev_device_get_property_value(d, "ID_FS_USAGE");
if (!streq_ptr(v, "filesystem")) {
if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
log_error("%s does not contain a known file system.", arg_mount_what);
return -EINVAL;
}
@ -1412,49 +1397,36 @@ static int list_devices(void) {
[COLUMN_UUID] = "UUID"
};
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
size_t n_allocated = 0, n = 0, i;
size_t column_width[_COLUMN_MAX];
struct item *items = NULL;
sd_device *d;
unsigned c;
int r;
for (c = 0; c < _COLUMN_MAX; c++)
column_width[c] = strlen(titles[c]);
udev = udev_new();
if (!udev)
r = sd_device_enumerator_new(&e);
if (r < 0)
return log_oom();
e = udev_enumerate_new(udev);
if (!e)
return log_oom();
r = udev_enumerate_add_match_subsystem(e, "block");
r = sd_device_enumerator_add_match_subsystem(e, "block", true);
if (r < 0)
return log_error_errno(r, "Failed to add block match: %m");
r = udev_enumerate_add_match_property(e, "ID_FS_USAGE", "filesystem");
r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
if (r < 0)
return log_error_errno(r, "Failed to add property match: %m");
r = udev_enumerate_scan_devices(e);
r = device_enumerator_scan_devices(e);
if (r < 0)
return log_error_errno(r, "Failed to scan devices: %m");
return log_error_errno(r, "Failed to enumerate devices: %m");
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
struct item *j;
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d) {
r = log_oom();
goto finish;
}
if (!GREEDY_REALLOC0(items, n_allocated, n+1)) {
r = log_oom();
goto finish;
@ -1469,11 +1441,11 @@ static int list_devices(void) {
switch (c) {
case COLUMN_NODE:
x = udev_device_get_devnode(d);
(void) sd_device_get_devname(d, &x);
break;
case COLUMN_PATH:
x = udev_device_get_property_value(d, "ID_PATH");
(void) sd_device_get_property_value(d, "ID_PATH", &x);
break;
case COLUMN_MODEL:
@ -1481,11 +1453,11 @@ static int list_devices(void) {
break;
case COLUMN_WWN:
x = udev_device_get_property_value(d, "ID_WWN");
(void) sd_device_get_property_value(d, "ID_WWN", &x);
break;
case COLUMN_FSTYPE:
x = udev_device_get_property_value(d, "ID_FS_TYPE");
(void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
break;
case COLUMN_LABEL:
@ -1493,7 +1465,7 @@ static int list_devices(void) {
break;
case COLUMN_UUID:
x = udev_device_get_property_value(d, "ID_FS_UUID");
(void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
break;
}

View file

@ -16,6 +16,7 @@
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "netdev/bridge.h"
#include "netdev/bond.h"

View file

@ -12,6 +12,7 @@
#include "set.h"
#include "socket-util.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
#include "util.h"

View file

@ -202,13 +202,11 @@ int ipv4ll_configure(Link *link) {
return r;
}
if (link->udev_device) {
r = net_get_unique_predictable_data(link->udev_device, &seed);
if (r >= 0) {
r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed);
if (r < 0)
return r;
}
if (link->sd_device &&
net_get_unique_predictable_data(link->sd_device, &seed) >= 0) {
r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed);
if (r < 0)
return r;
}
r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);

View file

@ -23,7 +23,7 @@
#include "socket-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "udev-util.h"
#include "strv.h"
#include "util.h"
#include "virt.h"
@ -549,7 +549,7 @@ static void link_free(Link *link) {
(void) unlink(link->state_file);
free(link->state_file);
udev_device_unref(link->udev_device);
sd_device_unref(link->sd_device);
HASHMAP_FOREACH (carrier, link->bound_to_links, i)
hashmap_remove(link->bound_to_links, INT_TO_PTR(carrier->ifindex));
@ -2908,7 +2908,7 @@ static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m,
return r;
if (!link->network) {
r = network_get(link->manager, link->udev_device, link->ifname,
r = network_get(link->manager, link->sd_device, link->ifname,
&link->mac, &network);
if (r == -ENOENT) {
link_enter_unmanaged(link);
@ -2946,7 +2946,7 @@ static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m,
return 1;
}
int link_initialized(Link *link, struct udev_device *device) {
int link_initialized(Link *link, sd_device *device) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
int r;
@ -2958,12 +2958,12 @@ int link_initialized(Link *link, struct udev_device *device) {
if (link->state != LINK_STATE_PENDING)
return 0;
if (link->udev_device)
if (link->sd_device)
return 0;
log_link_debug(link, "udev initialized link");
link->udev_device = udev_device_ref(device);
link->sd_device = sd_device_ref(device);
/* udev has initialized the link, but we don't know if we have yet
* processed the NEWLINK messages with the latest state. Do a GETLINK,
@ -3176,10 +3176,10 @@ ipv4ll_address_fail:
}
int link_add(Manager *m, sd_netlink_message *message, Link **ret) {
Link *link;
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
char ifindex_str[2 + DECIMAL_STR_MAX(int)];
int r;
int initialized, r;
Link *link;
assert(m);
assert(m->rtnl);
@ -3201,13 +3201,18 @@ int link_add(Manager *m, sd_netlink_message *message, Link **ret) {
if (detect_container() <= 0) {
/* not in a container, udev will be around */
sprintf(ifindex_str, "n%d", link->ifindex);
device = udev_device_new_from_device_id(m->udev, ifindex_str);
if (!device) {
r = log_link_warning_errno(link, errno, "Could not find udev device: %m");
r = sd_device_new_from_device_id(&device, ifindex_str);
if (r < 0) {
log_link_warning_errno(link, r, "Could not find device: %m");
goto failed;
}
if (udev_device_get_is_initialized(device) <= 0) {
r = sd_device_get_is_initialized(device, &initialized);
if (r < 0) {
log_link_warning_errno(link, r, "Could not determine whether the device is initialized or not: %m");
goto failed;
}
if (!initialized) {
/* not yet ready */
log_link_debug(link, "link pending udev initialization...");
return 0;

View file

@ -4,6 +4,7 @@
#include <endian.h>
#include "sd-bus.h"
#include "sd-device.h"
#include "sd-dhcp-client.h"
#include "sd-dhcp-server.h"
#include "sd-dhcp6-client.h"
@ -57,7 +58,7 @@ typedef struct Link {
struct ether_addr mac;
struct in6_addr ipv6ll_address;
uint32_t mtu;
struct udev_device *udev_device;
sd_device *sd_device;
unsigned flags;
uint8_t kernel_operstate;
@ -136,7 +137,7 @@ int link_address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *u
int link_route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata);
void link_enter_failed(Link *link);
int link_initialized(Link *link, struct udev_device *device);
int link_initialized(Link *link, sd_device *device);
void link_check_ready(Link *link);

View file

@ -3,6 +3,7 @@
#include "alloc-util.h"
#include "bus-util.h"
#include "networkd-manager.h"
#include "strv.h"
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_operational_state, link_operstate, LinkOperationalState);

View file

@ -22,7 +22,6 @@
#include "ordered-set.h"
#include "path-util.h"
#include "set.h"
#include "udev-util.h"
#include "virt.h"
/* use 8 MB for receive socket kernel queue. */
@ -183,18 +182,20 @@ int manager_connect_bus(Manager *m) {
return 0;
}
static int manager_udev_process_link(Manager *m, struct udev_device *device) {
static int manager_udev_process_link(Manager *m, sd_device *device) {
const char *action;
Link *link = NULL;
int r, ifindex;
assert(m);
assert(device);
if (!streq_ptr(udev_device_get_action(device), "add"))
r = sd_device_get_property_value(device, "ACTION", &action);
if (r < 0 || !streq_ptr(action, "add"))
return 0;
ifindex = udev_device_get_ifindex(device);
if (ifindex <= 0) {
r = sd_device_get_ifindex(device, &ifindex);
if (r < 0 || ifindex <= 0) {
log_debug("Ignoring udev ADD event for device with invalid ifindex");
return 0;
}
@ -215,11 +216,12 @@ static int manager_udev_process_link(Manager *m, struct udev_device *device) {
static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
Manager *m = userdata;
struct udev_monitor *monitor = m->udev_monitor;
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
int r;
device = udev_monitor_receive_device(monitor);
if (!device)
return -ENOMEM;
r = udev_monitor_receive_sd_device(monitor, &device);
if (r < 0)
return r;
(void) manager_udev_process_link(m, device);
@ -235,11 +237,7 @@ static int manager_connect_udev(Manager *m) {
if (detect_container() > 0)
return 0;
m->udev = udev_new();
if (!m->udev)
return -ENOMEM;
m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
m->udev_monitor = udev_monitor_new_from_netlink(NULL, "udev");
if (!m->udev_monitor)
return -ENOMEM;
@ -1481,7 +1479,6 @@ void manager_free(Manager *m) {
sd_event_source_unref(m->udev_event_source);
udev_monitor_unref(m->udev_monitor);
udev_unref(m->udev);
sd_bus_unref(m->bus);

View file

@ -7,7 +7,7 @@
#include "sd-event.h"
#include "sd-netlink.h"
#include "sd-resolve.h"
#include "udev.h"
#include "libudev.h"
#include "dhcp-identifier.h"
#include "hashmap.h"
@ -26,7 +26,6 @@ struct Manager {
sd_event *event;
sd_resolve *resolve;
sd_bus *bus;
struct udev *udev;
struct udev_monitor *udev_monitor;
sd_event_source *udev_event_source;

View file

@ -10,6 +10,7 @@
#include "networkd-ndisc.h"
#include "networkd-route.h"
#include "strv.h"
#define NDISC_DNSSL_MAX 64U
#define NDISC_RDNSS_MAX 64U

View file

@ -450,26 +450,25 @@ int network_get_by_name(Manager *manager, const char *name, Network **ret) {
return 0;
}
int network_get(Manager *manager, struct udev_device *device,
int network_get(Manager *manager, sd_device *device,
const char *ifname, const struct ether_addr *address,
Network **ret) {
Network *network;
struct udev_device *parent;
const char *path = NULL, *parent_driver = NULL, *driver = NULL, *devtype = NULL;
sd_device *parent;
Network *network;
assert(manager);
assert(ret);
if (device) {
path = udev_device_get_property_value(device, "ID_PATH");
(void) sd_device_get_property_value(device, "ID_PATH", &path);
parent = udev_device_get_parent(device);
if (parent)
parent_driver = udev_device_get_driver(parent);
if (sd_device_get_parent(device, &parent) >= 0)
(void) sd_device_get_driver(parent, &parent_driver);
driver = udev_device_get_property_value(device, "ID_NET_DRIVER");
(void) sd_device_get_property_value(device, "ID_NET_DRIVER", &driver);
devtype = udev_device_get_devtype(device);
(void) sd_device_get_devtype(device, &devtype);
}
LIST_FOREACH(networks, network, manager->networks) {
@ -484,8 +483,7 @@ int network_get(Manager *manager, struct udev_device *device,
const char *attr;
uint8_t name_assign_type = NET_NAME_UNKNOWN;
attr = udev_device_get_sysattr_value(device, "name_assign_type");
if (attr)
if (sd_device_get_sysattr_value(device, "name_assign_type", &attr) >= 0)
(void) safe_atou8(attr, &name_assign_type);
if (name_assign_type == NET_NAME_ENUM)

View file

@ -2,7 +2,7 @@
#pragma once
#include "sd-bus.h"
#include "udev.h"
#include "sd-device.h"
#include "condition.h"
#include "conf-parser.h"
@ -268,7 +268,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
int network_load(Manager *manager);
int network_get_by_name(Manager *manager, const char *name, Network **ret);
int network_get(Manager *manager, struct udev_device *device, const char *ifname, const struct ether_addr *mac, Network **ret);
int network_get(Manager *manager, sd_device *device, const char *ifname, const struct ether_addr *mac, Network **ret);
int network_apply(Network *network, Link *link);
void network_apply_anonymize_if_set(Network *network);

View file

@ -12,6 +12,7 @@
#include "parse-util.h"
#include "sd-radv.h"
#include "string-util.h"
#include "strv.h"
int config_parse_router_prefix_delegation(
const char *unit,

View file

@ -12,6 +12,7 @@
#include "parse-util.h"
#include "socket-util.h"
#include "string-util.h"
#include "strv.h"
int routing_policy_rule_new(RoutingPolicyRule **ret) {
RoutingPolicyRule *rule;

View file

@ -4,6 +4,7 @@
#include "sd-event.h"
#include "capability-util.h"
#include "mkdir.h"
#include "networkd-conf.h"
#include "networkd-manager.h"
#include "signal-util.h"

View file

@ -2,13 +2,14 @@
#include <sys/param.h>
#include "sd-device.h"
#include "alloc-util.h"
#include "dhcp-lease-internal.h"
#include "hostname-util.h"
#include "network-internal.h"
#include "networkd-manager.h"
#include "string-util.h"
#include "udev-util.h"
static void test_deserialize_in_addr(void) {
_cleanup_free_ struct in_addr *addresses = NULL;
@ -117,7 +118,7 @@ static int test_load_config(Manager *manager) {
return 0;
}
static void test_network_get(Manager *manager, struct udev_device *loopback) {
static void test_network_get(Manager *manager, sd_device *loopback) {
Network *network;
const struct ether_addr mac = {};
@ -219,9 +220,8 @@ static void test_dhcp_hostname_shorten_overlong(void) {
int main(void) {
_cleanup_(manager_freep) Manager *manager = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *loopback = NULL;
int r;
_cleanup_(sd_device_unrefp) sd_device *loopback = NULL;
int ifindex, r;
test_deserialize_in_addr();
test_deserialize_dhcp_routes();
@ -234,12 +234,10 @@ int main(void) {
if (r == -EPERM)
return EXIT_TEST_SKIP;
udev = udev_new();
assert_se(udev);
loopback = udev_device_new_from_syspath(udev, "/sys/class/net/lo");
assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0);
assert_se(loopback);
assert_se(udev_device_get_ifindex(loopback) == 1);
assert_se(sd_device_get_ifindex(loopback, &ifindex) >= 0);
assert_se(ifindex == 1);
test_network_get(manager, loopback);

View file

@ -9,6 +9,7 @@
#include "manager.h"
#include "netlink-util.h"
#include "network-internal.h"
#include "strv.h"
#include "time-util.h"
#include "util.h"

View file

@ -4,7 +4,7 @@
#include <net/if.h>
#include <sys/file.h>
#include "libudev.h"
#include "sd-device.h"
#include "sd-id128.h"
#include "sd-netlink.h"
@ -17,7 +17,7 @@
#include "socket-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "strv.h"
#include "util.h"
#define HOST_HASH_KEY SD_ID128_MAKE(1a,37,6f,c7,46,ec,45,0b,ad,a3,d5,31,06,60,5d,b1)
@ -392,21 +392,25 @@ int remove_bridge(const char *bridge_name) {
return remove_one_link(rtnl, bridge_name);
}
static int parse_interface(struct udev *udev, const char *name) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
static int parse_interface(const char *name) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
char ifi_str[2 + DECIMAL_STR_MAX(int)];
int ifi;
int ifi, initialized, r;
ifi = (int) if_nametoindex(name);
if (ifi <= 0)
return log_error_errno(errno, "Failed to resolve interface %s: %m", name);
sprintf(ifi_str, "n%i", ifi);
d = udev_device_new_from_device_id(udev, ifi_str);
if (!d)
return log_error_errno(errno, "Failed to get udev device for interface %s: %m", name);
r = sd_device_new_from_device_id(&d, ifi_str);
if (r < 0)
return log_error_errno(r, "Failed to get device for interface %s: %m", name);
if (udev_device_get_is_initialized(d) <= 0) {
r = sd_device_get_is_initialized(d, &initialized);
if (r < 0)
return log_error_errno(r, "Failed to determine whether interface %s is initialized or not: %m", name);
if (!initialized) {
log_error("Network interface %s is not initialized yet.", name);
return -EBUSY;
}
@ -415,7 +419,6 @@ static int parse_interface(struct udev *udev, const char *name) {
}
int move_network_interfaces(pid_t pid, char **ifaces) {
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
char **i;
int r;
@ -427,17 +430,11 @@ int move_network_interfaces(pid_t pid, char **ifaces) {
if (r < 0)
return log_error_errno(r, "Failed to connect to netlink: %m");
udev = udev_new();
if (!udev) {
log_error("Failed to connect to udev.");
return -ENOMEM;
}
STRV_FOREACH(i, ifaces) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
int ifi;
ifi = parse_interface(udev, *i);
ifi = parse_interface(*i);
if (ifi < 0)
return ifi;
@ -458,7 +455,6 @@ int move_network_interfaces(pid_t pid, char **ifaces) {
}
int setup_macvlan(const char *machine_name, pid_t pid, char **ifaces) {
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
unsigned idx = 0;
char **i;
@ -471,19 +467,13 @@ int setup_macvlan(const char *machine_name, pid_t pid, char **ifaces) {
if (r < 0)
return log_error_errno(r, "Failed to connect to netlink: %m");
udev = udev_new();
if (!udev) {
log_error("Failed to connect to udev.");
return -ENOMEM;
}
STRV_FOREACH(i, ifaces) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
_cleanup_free_ char *n = NULL;
struct ether_addr mac;
int ifi;
ifi = parse_interface(udev, *i);
ifi = parse_interface(*i);
if (ifi < 0)
return ifi;
@ -546,7 +536,6 @@ int setup_macvlan(const char *machine_name, pid_t pid, char **ifaces) {
}
int setup_ipvlan(const char *machine_name, pid_t pid, char **ifaces) {
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
char **i;
int r;
@ -558,18 +547,12 @@ int setup_ipvlan(const char *machine_name, pid_t pid, char **ifaces) {
if (r < 0)
return log_error_errno(r, "Failed to connect to netlink: %m");
udev = udev_new();
if (!udev) {
log_error("Failed to connect to udev.");
return -ENOMEM;
}
STRV_FOREACH(i, ifaces) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
_cleanup_free_ char *n = NULL;
int ifi;
ifi = parse_interface(udev, *i);
ifi = parse_interface(*i);
if (ifi < 0)
return ifi;

View file

@ -91,7 +91,6 @@
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "udev-util.h"
#include "umask-util.h"
#include "user-util.h"
#include "util.h"

View file

@ -3,20 +3,20 @@
#include <linux/rfkill.h>
#include <poll.h>
#include "libudev.h"
#include "sd-daemon.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "libudev-private.h"
#include "mkdir.h"
#include "parse-util.h"
#include "proc-cmdline.h"
#include "string-table.h"
#include "string-util.h"
#include "udev-util.h"
#include "util.h"
#include "list.h"
@ -54,64 +54,57 @@ static const char* const rfkill_type_table[NUM_RFKILL_TYPES] = {
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(rfkill_type, int);
static int find_device(
struct udev *udev,
const struct rfkill_event *event,
struct udev_device **ret) {
sd_device **ret) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
_cleanup_free_ char *sysname = NULL;
struct udev_device *device;
const char *name;
int r;
assert(udev);
assert(event);
assert(ret);
if (asprintf(&sysname, "rfkill%i", event->idx) < 0)
return log_oom();
device = udev_device_new_from_subsystem_sysname(udev, "rfkill", sysname);
if (!device)
return log_full_errno(IN_SET(errno, ENOENT, ENXIO, ENODEV) ? LOG_DEBUG : LOG_ERR, errno,
r = sd_device_new_from_subsystem_sysname(&device, "rfkill", sysname);
if (r < 0)
return log_full_errno(IN_SET(r, -ENOENT, -ENXIO, -ENODEV) ? LOG_DEBUG : LOG_ERR, r,
"Failed to open device '%s': %m", sysname);
name = udev_device_get_sysattr_value(device, "name");
if (!name) {
log_debug("Device has no name, ignoring.");
udev_device_unref(device);
return -ENOENT;
}
r = sd_device_get_sysattr_value(device, "name", &name);
if (r < 0)
return log_debug_errno(r, "Device has no name, ignoring: %m");
log_debug("Operating on rfkill device '%s'.", name);
*ret = device;
*ret = TAKE_PTR(device);
return 0;
}
static int wait_for_initialized(
struct udev *udev,
struct udev_device *device,
struct udev_device **ret) {
sd_device *device,
sd_device **ret) {
_cleanup_(udev_monitor_unrefp) struct udev_monitor *monitor = NULL;
struct udev_device *d;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
int initialized, watch_fd, r;
const char *sysname;
int watch_fd, r;
assert(udev);
assert(device);
assert(ret);
if (udev_device_get_is_initialized(device) != 0) {
*ret = udev_device_ref(device);
if (sd_device_get_is_initialized(device, &initialized) >= 0 && initialized) {
*ret = sd_device_ref(device);
return 0;
}
assert_se(sysname = udev_device_get_sysname(device));
assert_se(sd_device_get_sysname(device, &sysname) >= 0);
/* Wait until the device is initialized, so that we can get
* access to the ID_PATH property */
monitor = udev_monitor_new_from_netlink(udev, "udev");
monitor = udev_monitor_new_from_netlink(NULL, "udev");
if (!monitor)
return log_error_errno(errno, "Failed to acquire monitor: %m");
@ -128,18 +121,19 @@ static int wait_for_initialized(
return log_error_errno(watch_fd, "Failed to get watch fd: %m");
/* Check again, maybe things changed */
d = udev_device_new_from_subsystem_sysname(udev, "rfkill", sysname);
if (!d)
return log_full_errno(IN_SET(errno, ENOENT, ENXIO, ENODEV) ? LOG_DEBUG : LOG_ERR, errno,
r = sd_device_new_from_subsystem_sysname(&d, "rfkill", sysname);
if (r < 0)
return log_full_errno(IN_SET(r, -ENOENT, -ENXIO, -ENODEV) ? LOG_DEBUG : LOG_ERR, r,
"Failed to open device '%s': %m", sysname);
if (udev_device_get_is_initialized(d) != 0) {
*ret = d;
if (sd_device_get_is_initialized(d, &initialized) >= 0 && initialized) {
*ret = TAKE_PTR(d);
return 0;
}
for (;;) {
_cleanup_(udev_device_unrefp) struct udev_device *t = NULL;
_cleanup_(sd_device_unrefp) sd_device *t = NULL;
const char *name;
r = fd_wait_for_event(watch_fd, POLLIN, EXIT_USEC);
if (r == -EINTR)
@ -151,24 +145,22 @@ static int wait_for_initialized(
return -ETIMEDOUT;
}
t = udev_monitor_receive_device(monitor);
if (!t)
r = udev_monitor_receive_sd_device(monitor, &t);
if (r < 0)
continue;
if (streq_ptr(udev_device_get_sysname(t), sysname)) {
*ret = udev_device_ref(t);
if (sd_device_get_sysname(t, &name) >= 0 && streq_ptr(name, sysname)) {
*ret = TAKE_PTR(t);
return 0;
}
}
}
static int determine_state_file(
struct udev *udev,
const struct rfkill_event *event,
char **ret) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL, *device = NULL;
const char *path_id, *type;
char *state_file;
int r;
@ -176,18 +168,17 @@ static int determine_state_file(
assert(event);
assert(ret);
r = find_device(udev, event, &d);
r = find_device(event, &d);
if (r < 0)
return r;
r = wait_for_initialized(udev, d, &device);
r = wait_for_initialized(d, &device);
if (r < 0)
return r;
assert_se(type = rfkill_type_to_string(event->type));
path_id = udev_device_get_property_value(device, "ID_PATH");
if (path_id) {
if (sd_device_get_property_value(device, "ID_PATH", &path_id) >= 0) {
_cleanup_free_ char *escaped_path_id = NULL;
escaped_path_id = cescape(path_id);
@ -207,7 +198,6 @@ static int determine_state_file(
static int load_state(
int rfkill_fd,
struct udev *udev,
const struct rfkill_event *event) {
_cleanup_free_ char *state_file = NULL, *value = NULL;
@ -216,13 +206,12 @@ static int load_state(
int b, r;
assert(rfkill_fd >= 0);
assert(udev);
assert(event);
if (shall_restore_state() == 0)
return 0;
r = determine_state_file(udev, event, &state_file);
r = determine_state_file(event, &state_file);
if (r < 0)
return r;
@ -281,7 +270,6 @@ static void save_state_queue_remove(
static int save_state_queue(
struct write_queue_item **write_queue,
int rfkill_fd,
struct udev *udev,
const struct rfkill_event *event) {
_cleanup_free_ char *state_file = NULL;
@ -289,10 +277,9 @@ static int save_state_queue(
int r;
assert(rfkill_fd >= 0);
assert(udev);
assert(event);
r = determine_state_file(udev, event, &state_file);
r = determine_state_file(event, &state_file);
if (r < 0)
return r;
@ -314,17 +301,15 @@ static int save_state_queue(
static int save_state_cancel(
struct write_queue_item **write_queue,
int rfkill_fd,
struct udev *udev,
const struct rfkill_event *event) {
_cleanup_free_ char *state_file = NULL;
int r;
assert(rfkill_fd >= 0);
assert(udev);
assert(event);
r = determine_state_file(udev, event, &state_file);
r = determine_state_file(event, &state_file);
save_state_queue_remove(write_queue, event->idx, state_file);
if (r < 0)
return r;
@ -358,7 +343,6 @@ static int save_state_write(struct write_queue_item **write_queue) {
int main(int argc, char *argv[]) {
LIST_HEAD(write_queue_item, write_queue);
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_close_ int rfkill_fd = -1;
bool ready = false;
int r, n;
@ -376,12 +360,6 @@ int main(int argc, char *argv[]) {
umask(0022);
udev = udev_new();
if (!udev) {
r = log_oom();
goto finish;
}
r = mkdir_p("/var/lib/systemd/rfkill", 0755);
if (r < 0) {
log_error_errno(r, "Failed to create rfkill directory: %m");
@ -474,17 +452,17 @@ int main(int argc, char *argv[]) {
case RFKILL_OP_ADD:
log_debug("A new rfkill device has been added with index %i and type %s.", event.idx, type);
(void) load_state(rfkill_fd, udev, &event);
(void) load_state(rfkill_fd, &event);
break;
case RFKILL_OP_DEL:
log_debug("An rfkill device has been removed with index %i and type %s", event.idx, type);
(void) save_state_cancel(&write_queue, rfkill_fd, udev, &event);
(void) save_state_cancel(&write_queue, rfkill_fd, &event);
break;
case RFKILL_OP_CHANGE:
log_debug("An rfkill device has changed state with index %i and type %s", event.idx, type);
(void) save_state_queue(&write_queue, rfkill_fd, udev, &event);
(void) save_state_queue(&write_queue, rfkill_fd, &event);
break;
default:

View file

@ -4,6 +4,7 @@
#include <sys/prctl.h>
#include <sys/wait.h>
#include "sd-device.h"
#include "sd-id128.h"
#include "architecture.h"
@ -13,6 +14,7 @@
#include "copy.h"
#include "crypt-util.h"
#include "def.h"
#include "device-enumerator-private.h"
#include "device-nodes.h"
#include "dissect-image.h"
#include "fd-util.h"
@ -35,7 +37,6 @@
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "udev-util.h"
#include "user-util.h"
#include "xattr-util.h"
@ -94,19 +95,20 @@ not_found:
#if HAVE_BLKID
/* Detect RPMB and Boot partitions, which are not listed by blkid.
* See https://github.com/systemd/systemd/issues/5806. */
static bool device_is_mmc_special_partition(struct udev_device *d) {
static bool device_is_mmc_special_partition(sd_device *d) {
const char *sysname;
sysname = udev_device_get_sysname(d);
return sysname && startswith(sysname, "mmcblk") &&
if (sd_device_get_sysname(d, &sysname) < 0)
return false;
return startswith(sysname, "mmcblk") &&
(endswith(sysname, "rpmb") || endswith(sysname, "boot0") || endswith(sysname, "boot1"));
}
static bool device_is_block(struct udev_device *d) {
static bool device_is_block(sd_device *d) {
const char *ss;
ss = udev_device_get_subsystem(d);
if (!ss)
if (sd_device_get_subsystem(d, &ss) < 0)
return false;
return streq(ss, "block");
@ -122,19 +124,18 @@ int dissect_image(
#if HAVE_BLKID
sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL;
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
bool is_gpt, is_mbr, generic_rw, multiple_generic = false;
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
_cleanup_(blkid_free_probep) blkid_probe b = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_free_ char *generic_node = NULL;
sd_id128_t generic_uuid = SD_ID128_NULL;
const char *pttype = NULL;
struct udev_list_entry *first, *item;
blkid_partlist pl;
int r, generic_nr;
struct stat st;
sd_device *q;
unsigned i;
assert(fd >= 0);
@ -253,13 +254,9 @@ int dissect_image(
if (!pl)
return -errno ?: -ENOMEM;
udev = udev_new();
if (!udev)
return -errno;
d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
if (!d)
return -ENOMEM;
r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
if (r < 0)
return r;
for (i = 0;; i++) {
int n, z;
@ -269,31 +266,28 @@ int dissect_image(
return -ENXIO;
}
e = udev_enumerate_new(udev);
if (!e)
return -errno;
r = udev_enumerate_add_match_parent(e, d);
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
r = sd_device_enumerator_add_match_parent(e, d);
if (r < 0)
return r;
r = device_enumerator_scan_devices(e);
if (r < 0)
return r;
/* Count the partitions enumerated by the kernel */
n = 0;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *q;
FOREACH_DEVICE_AND_SUBSYSTEM(e, q) {
dev_t qn;
q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!q)
return -errno;
qn = udev_device_get_devnum(q);
if (major(qn) == 0)
if (sd_device_get_devnum(q, &qn) < 0)
continue;
if (!device_is_block(q))
@ -353,24 +347,18 @@ int dissect_image(
}
}
e = udev_enumerate_unref(e);
e = sd_device_enumerator_unref(e);
}
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *q;
FOREACH_DEVICE_AND_SUBSYSTEM(e, q) {
unsigned long long pflags;
blkid_partition pp;
const char *node;
dev_t qn;
int nr;
q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!q)
return -errno;
qn = udev_device_get_devnum(q);
if (major(qn) == 0)
r = sd_device_get_devnum(q, &qn);
if (r < 0)
continue;
if (st.st_rdev == qn)
@ -382,8 +370,8 @@ int dissect_image(
if (device_is_mmc_special_partition(q))
continue;
node = udev_device_get_devnode(q);
if (!node)
r = sd_device_get_devname(q, &node);
if (r < 0)
continue;
pp = blkid_partlist_devno_to_partition(pl, qn);

View file

@ -1,7 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <string.h>
#include "alloc-util.h"
#include "fileio.h"
#include "log.h"
#include "string-util.h"
@ -37,26 +39,3 @@ int udev_parse_config(void) {
return 0;
}
int udev_device_new_from_stat_rdev(struct udev *udev, const struct stat *st, struct udev_device **ret) {
struct udev_device *nd;
char type;
assert(udev);
assert(st);
assert(ret);
if (S_ISBLK(st->st_mode))
type = 'b';
else if (S_ISCHR(st->st_mode))
type = 'c';
else
return -ENOTTY;
nd = udev_device_new_from_devnum(udev, type, st->st_rdev);
if (!nd)
return -errno;
*ret = nd;
return 0;
}

View file

@ -1,19 +1,4 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include "udev.h"
#include "util.h"
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev*, udev_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_device*, udev_device_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_enumerate*, udev_enumerate_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_rules*, udev_rules_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl*, udev_ctrl_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_connection*, udev_ctrl_connection_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_msg*, udev_ctrl_msg_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref);
int udev_parse_config(void);
int udev_device_new_from_stat_rdev(struct udev *udev, const struct stat *st, struct udev_device **ret);

View file

@ -5,14 +5,11 @@
#include <sys/epoll.h>
#include <unistd.h>
#include "libudev.h"
#include "fd-util.h"
#include "libudev-private.h"
#include "log.h"
#include "stdio-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "util.h"
static void print_device(struct udev_device *device) {
const char *str;

View file

@ -17,7 +17,6 @@
#include "selinux-util.h"
#include "signal-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "udev.h"
static int fake_filesystems(void) {

View file

@ -9,6 +9,7 @@
#include "conf-parser.h"
#include "ethtool-util.h"
#include "fd-util.h"
#include "libudev-device-internal.h"
#include "libudev-private.h"
#include "link-config.h"
#include "log.h"
@ -332,7 +333,7 @@ static int get_mac(struct udev_device *device, bool want_random,
else {
uint64_t result;
r = net_get_unique_predictable_data(device, &result);
r = net_get_unique_predictable_data(device->device, &result);
if (r < 0)
return r;

View file

@ -19,6 +19,7 @@
#include "libudev.h"
#include "alloc-util.h"
#include "fd-util.h"
#include "libudev-private.h"
#include "scsi_id.h"

View file

@ -11,7 +11,6 @@
#include "hwdb-util.h"
#include "parse-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "udev.h"
static sd_hwdb *hwdb;

View file

@ -102,7 +102,6 @@
#include "stdio-util.h"
#include "string-util.h"
#include "udev.h"
#include "udev-util.h"
#define ONBOARD_INDEX_MAX (16*1024-1)

View file

@ -21,7 +21,6 @@
#include "string-util.h"
#include "sysexits.h"
#include "udev.h"
#include "udev-util.h"
_printf_(2,3)
static void path_prepend(char **path, const char *fmt, ...) {

View file

@ -188,6 +188,13 @@ int udev_builtin_add_property(struct udev_device *dev, bool test, const char *ke
int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *prefix, const char *modalias,
const char *filter, bool test);
/* Cleanup functions */
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_rules*, udev_rules_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl*, udev_ctrl_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_connection*, udev_ctrl_connection_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_msg*, udev_ctrl_msg_unref);
/* udevadm commands */
struct udevadm_cmd {
const char *name;

View file

@ -21,7 +21,6 @@
#include "process-util.h"
#include "time-util.h"
#include "udev-util.h"
#include "udev.h"
#include "udevadm-util.h"

View file

@ -13,7 +13,6 @@
#include "dirent-util.h"
#include "fd-util.h"
#include "string-util.h"
#include "udev-util.h"
#include "udev.h"
#include "udevadm-util.h"

View file

@ -12,7 +12,6 @@
#include "fd-util.h"
#include "format-util.h"
#include "udev-util.h"
#include "udev.h"
#include "udevadm-util.h"

View file

@ -13,7 +13,6 @@
#include <unistd.h>
#include "string-util.h"
#include "udev-util.h"
#include "udev.h"
#include "udevadm-util.h"

View file

@ -11,7 +11,6 @@
#include "fd-util.h"
#include "set.h"
#include "string-util.h"
#include "udev-util.h"
#include "udev.h"
#include "udevadm-util.h"
#include "util.h"

View file

@ -8,7 +8,6 @@ struct udev_device *find_device(struct udev *udev,
const char *id,
const char *prefix) {
assert(udev);
assert(id);
if (prefix && !startswith(id, prefix))