1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

Merge pull request #10395 from yuwata/udev-cleanup-9

udev-builtin: replace udev_device by sd_device
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-10-23 13:43:33 +02:00 committed by GitHub
commit f402ce827d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 792 additions and 685 deletions

View File

@ -30,3 +30,30 @@
for (device = sd_device_enumerator_get_subsystem_first(enumerator); \ for (device = sd_device_enumerator_get_subsystem_first(enumerator); \
device; \ device; \
device = sd_device_enumerator_get_subsystem_next(enumerator)) device = sd_device_enumerator_get_subsystem_next(enumerator))
#define log_device_full(device, level, error, ...) \
({ \
const char *_sysname = NULL, *_subsystem = NULL; \
sd_device *_d = (device); \
int _level = (level), _error = (error); \
\
if (_d && _unlikely_(log_get_max_level() >= _level)) { \
(void) sd_device_get_sysname(_d, &_sysname); \
(void) sd_device_get_subsystem(_d, &_subsystem); \
} \
log_object_internal(_level, _error, __FILE__, __LINE__, __func__, \
_sysname ? "DEVICE=" : NULL, _sysname, \
_subsystem ? "SUBSYSTEM=" : NULL, _subsystem, ##__VA_ARGS__); \
})
#define log_device_debug(link, ...) log_device_full(link, LOG_DEBUG, 0, ##__VA_ARGS__)
#define log_device_info(link, ...) log_device_full(link, LOG_INFO, 0, ##__VA_ARGS__)
#define log_device_notice(link, ...) log_device_full(link, LOG_NOTICE, 0, ##__VA_ARGS__)
#define log_device_warning(link, ...) log_device_full(link, LOG_WARNING, 0, ##__VA_ARGS__)
#define log_device_error(link, ...) log_device_full(link, LOG_ERR, 0, ##__VA_ARGS__)
#define log_device_debug_errno(link, error, ...) log_device_full(link, LOG_DEBUG, error, ##__VA_ARGS__)
#define log_device_info_errno(link, error, ...) log_device_full(link, LOG_INFO, error, ##__VA_ARGS__)
#define log_device_notice_errno(link, error, ...) log_device_full(link, LOG_NOTICE, error, ##__VA_ARGS__)
#define log_device_warning_errno(link, error, ...) log_device_full(link, LOG_WARNING, error, ##__VA_ARGS__)
#define log_device_error_errno(link, error, ...) log_device_full(link, LOG_ERR, error, ##__VA_ARGS__)

View File

@ -26,7 +26,7 @@
#include "strxcpyx.h" #include "strxcpyx.h"
#include "udev-builtin.h" #include "udev-builtin.h"
static void print_property(struct udev_device *dev, bool test, const char *name, const char *value) { static void print_property(sd_device *dev, bool test, const char *name, const char *value) {
char s[256]; char s[256];
s[0] = '\0'; s[0] = '\0';
@ -94,7 +94,7 @@ static void print_property(struct udev_device *dev, bool test, const char *name,
} }
} }
static int find_gpt_root(struct udev_device *dev, blkid_probe pr, bool test) { static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) {
#if defined(GPT_ROOT_NATIVE) && ENABLE_EFI #if defined(GPT_ROOT_NATIVE) && ENABLE_EFI
@ -182,6 +182,8 @@ static int probe_superblocks(blkid_probe pr) {
struct stat st; struct stat st;
int rc; int rc;
/* TODO: Return negative errno. */
if (fstat(blkid_probe_get_fd(pr), &st)) if (fstat(blkid_probe_get_fd(pr), &st))
return -errno; return -errno;
@ -210,18 +212,13 @@ static int probe_superblocks(blkid_probe pr) {
return blkid_do_safeprobe(pr); return blkid_do_safeprobe(pr);
} }
static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_blkid(sd_device *dev, int argc, char *argv[], bool test) {
const char *root_partition; const char *devnode, *root_partition = NULL, *data, *name;
int64_t offset = 0;
bool noraid = false;
_cleanup_close_ int fd = -1;
_cleanup_(blkid_free_probep) blkid_probe pr = NULL; _cleanup_(blkid_free_probep) blkid_probe pr = NULL;
const char *data; bool noraid = false, is_gpt = false;
const char *name; _cleanup_close_ int fd = -1;
int nvals; int64_t offset = 0;
int i; int nvals, i, r;
int err = 0;
bool is_gpt = false;
static const struct option options[] = { static const struct option options[] = {
{ "offset", required_argument, NULL, 'o' }, { "offset", required_argument, NULL, 'o' },
@ -238,13 +235,11 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t
switch (option) { switch (option) {
case 'o': case 'o':
err = safe_atoi64(optarg, &offset); r = safe_atoi64(optarg, &offset);
if (err < 0) if (r < 0)
goto out; return r;
if (offset < 0) { if (offset < 0)
err = -ERANGE; return -ERANGE;
goto out;
}
break; break;
case 'R': case 'R':
noraid = true; noraid = true;
@ -252,9 +247,10 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t
} }
} }
errno = 0;
pr = blkid_new_probe(); pr = blkid_new_probe();
if (!pr) if (!pr)
return EXIT_FAILURE; return errno > 0 ? -errno : -ENOMEM;
blkid_probe_set_superblocks_flags(pr, blkid_probe_set_superblocks_flags(pr,
BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
@ -264,27 +260,30 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t
if (noraid) if (noraid)
blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID); blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC); r = sd_device_get_devname(dev, &devnode);
if (fd < 0) { if (r < 0)
err = log_debug_errno(errno, "Failure opening block device %s: %m", udev_device_get_devnode(dev)); return r;
goto out;
}
err = blkid_probe_set_device(pr, fd, offset, 0); fd = open(devnode, O_RDONLY|O_CLOEXEC);
if (err < 0) if (fd < 0)
goto out; return log_debug_errno(errno, "Failure opening block device %s: %m", devnode);
errno = 0;
r = blkid_probe_set_device(pr, fd, offset, 0);
if (r < 0)
return errno > 0 ? -errno : -ENOMEM;
log_debug("probe %s %sraid offset=%"PRIi64, log_debug("probe %s %sraid offset=%"PRIi64,
udev_device_get_devnode(dev), devnode,
noraid ? "no" : "", offset); noraid ? "no" : "", offset);
err = probe_superblocks(pr); r = probe_superblocks(pr);
if (err < 0) if (r < 0)
goto out; return r;
/* If we are a partition then our parent passed on the root /* If we are a partition then our parent passed on the root
* partition UUID to us */ * partition UUID to us */
root_partition = udev_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID"); (void) sd_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition);
nvals = blkid_probe_numof_values(pr); nvals = blkid_probe_numof_values(pr);
for (i = 0; i < nvals; i++) { for (i = 0; i < nvals; i++) {
@ -306,11 +305,7 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t
if (is_gpt) if (is_gpt)
find_gpt_root(dev, pr, test); find_gpt_root(dev, pr, test);
out: return 0;
if (err < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
} }
const struct udev_builtin udev_builtin_blkid = { const struct udev_builtin udev_builtin_blkid = {

View File

@ -15,25 +15,25 @@
#include "udev-builtin.h" #include "udev-builtin.h"
#include "util.h" #include "util.h"
static int builtin_btrfs(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_btrfs(sd_device *dev, int argc, char *argv[], bool test) {
struct btrfs_ioctl_vol_args args = {}; struct btrfs_ioctl_vol_args args = {};
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
int err; int r;
if (argc != 3 || !streq(argv[1], "ready")) if (argc != 3 || !streq(argv[1], "ready"))
return EXIT_FAILURE; return -EINVAL;
fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC); fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC);
if (fd < 0) if (fd < 0)
return EXIT_FAILURE; return -errno;
strscpy(args.name, sizeof(args.name), argv[2]); strscpy(args.name, sizeof(args.name), argv[2]);
err = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args); r = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
if (err < 0) if (r < 0)
return EXIT_FAILURE; return -errno;
udev_builtin_add_property(dev, test, "ID_BTRFS_READY", one_zero(err == 0)); udev_builtin_add_property(dev, test, "ID_BTRFS_READY", one_zero(r == 0));
return EXIT_SUCCESS; return 0;
} }
const struct udev_builtin udev_builtin_btrfs = { const struct udev_builtin udev_builtin_btrfs = {

View File

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */ /* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <fnmatch.h> #include <fnmatch.h>
#include <getopt.h> #include <getopt.h>
#include <stdio.h> #include <stdio.h>
@ -9,19 +10,18 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "hwdb-util.h" #include "hwdb-util.h"
#include "libudev-private.h"
#include "parse-util.h" #include "parse-util.h"
#include "string-util.h" #include "string-util.h"
#include "udev-builtin.h" #include "udev-builtin.h"
static sd_hwdb *hwdb; static sd_hwdb *hwdb;
int udev_builtin_hwdb_lookup(struct udev_device *dev, int udev_builtin_hwdb_lookup(sd_device *dev,
const char *prefix, const char *modalias, const char *prefix, const char *modalias,
const char *filter, bool test) { const char *filter, bool test) {
_cleanup_free_ char *lookup = NULL; _cleanup_free_ char *lookup = NULL;
const char *key, *value; const char *key, *value;
int n = 0; int n = 0, r;
if (!hwdb) if (!hwdb)
return -ENOENT; return -ENOENT;
@ -37,22 +37,21 @@ int udev_builtin_hwdb_lookup(struct udev_device *dev,
if (filter && fnmatch(filter, key, FNM_NOESCAPE) != 0) if (filter && fnmatch(filter, key, FNM_NOESCAPE) != 0)
continue; continue;
if (udev_builtin_add_property(dev, test, key, value) < 0) r = udev_builtin_add_property(dev, test, key, value);
return -ENOMEM; if (r < 0)
return r;
n++; n++;
} }
return n; return n;
} }
static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) { static const char *modalias_usb(sd_device *dev, char *s, size_t size) {
const char *v, *p; const char *v, *p;
uint16_t vn, pn; uint16_t vn, pn;
v = udev_device_get_sysattr_value(dev, "idVendor"); if (sd_device_get_sysattr_value(dev, "idVendor", &v) < 0)
if (!v)
return NULL; return NULL;
p = udev_device_get_sysattr_value(dev, "idProduct"); if (sd_device_get_sysattr_value(dev, "idProduct", &p) < 0)
if (!p)
return NULL; return NULL;
if (safe_atoux16(v, &vn) < 0) if (safe_atoux16(v, &vn) < 0)
return NULL; return NULL;
@ -62,10 +61,10 @@ static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) {
return s; return s;
} }
static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev, static int udev_builtin_hwdb_search(sd_device *dev, sd_device *srcdev,
const char *subsystem, const char *prefix, const char *subsystem, const char *prefix,
const char *filter, bool test) { const char *filter, bool test) {
struct udev_device *d; sd_device *d;
char s[16]; char s[16];
bool last = false; bool last = false;
int r = 0; int r = 0;
@ -75,21 +74,21 @@ static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device
if (!srcdev) if (!srcdev)
srcdev = dev; srcdev = dev;
for (d = srcdev; d && !last; d = udev_device_get_parent(d)) { for (d = srcdev; d; ) {
const char *dsubsys; const char *dsubsys, *devtype, *modalias = NULL;
const char *modalias = NULL;
dsubsys = udev_device_get_subsystem(d); if (sd_device_get_subsystem(d, &dsubsys) < 0)
if (!dsubsys) goto next;
continue;
/* look only at devices of a specific subsystem */ /* look only at devices of a specific subsystem */
if (subsystem && !streq(dsubsys, subsystem)) if (subsystem && !streq(dsubsys, subsystem))
continue; goto next;
modalias = udev_device_get_property_value(d, "MODALIAS"); (void) sd_device_get_property_value(d, "MODALIAS", &modalias);
if (streq(dsubsys, "usb") && streq_ptr(udev_device_get_devtype(d), "usb_device")) { if (streq(dsubsys, "usb") &&
sd_device_get_devtype(d, &devtype) >= 0 &&
streq(devtype, "usb_device")) {
/* if the usb_device does not have a modalias, compose one */ /* if the usb_device does not have a modalias, compose one */
if (!modalias) if (!modalias)
modalias = modalias_usb(d, s, sizeof(s)); modalias = modalias_usb(d, s, sizeof(s));
@ -99,17 +98,23 @@ static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device
} }
if (!modalias) if (!modalias)
continue; goto next;
r = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test); r = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test);
if (r > 0) if (r > 0)
break; break;
if (last)
break;
next:
if (sd_device_get_parent(d, &d) < 0)
break;
} }
return r; return r;
} }
static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_hwdb(sd_device *dev, int argc, char *argv[], bool test) {
static const struct option options[] = { static const struct option options[] = {
{ "filter", required_argument, NULL, 'f' }, { "filter", required_argument, NULL, 'f' },
{ "device", required_argument, NULL, 'd' }, { "device", required_argument, NULL, 'd' },
@ -121,10 +126,11 @@ static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool te
const char *device = NULL; const char *device = NULL;
const char *subsystem = NULL; const char *subsystem = NULL;
const char *prefix = NULL; const char *prefix = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *srcdev = NULL; _cleanup_(sd_device_unrefp) sd_device *srcdev = NULL;
int r;
if (!hwdb) if (!hwdb)
return EXIT_FAILURE; return -EINVAL;
for (;;) { for (;;) {
int option; int option;
@ -153,22 +159,17 @@ static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool te
} }
/* query a specific key given as argument */ /* query a specific key given as argument */
if (argv[optind]) { if (argv[optind])
if (udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test) > 0) return udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test);
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
/* read data from another device than the device we will store the data */ /* read data from another device than the device we will store the data */
if (device) { if (device) {
srcdev = udev_device_new_from_device_id(NULL, device); r = sd_device_new_from_device_id(&srcdev, device);
if (!srcdev) if (r < 0)
return EXIT_FAILURE; return r;
} }
if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0) return udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test);
return EXIT_SUCCESS;
return EXIT_FAILURE;
} }
/* called at udev startup and reload */ /* called at udev startup and reload */

View File

@ -46,7 +46,7 @@ static inline int abs_size_mm(const struct input_absinfo *absinfo) {
return (absinfo->maximum - absinfo->minimum) / absinfo->resolution; return (absinfo->maximum - absinfo->minimum) / absinfo->resolution;
} }
static void extract_info(struct udev_device *dev, const char *devpath, bool test) { static void extract_info(sd_device *dev, const char *devpath, bool test) {
char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)]; char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)];
struct input_absinfo xabsinfo = {}, yabsinfo = {}; struct input_absinfo xabsinfo = {}, yabsinfo = {};
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
@ -71,12 +71,12 @@ static void extract_info(struct udev_device *dev, const char *devpath, bool test
/* /*
* Read a capability attribute and return bitmask. * Read a capability attribute and return bitmask.
* @param dev udev_device * @param dev sd_device
* @param attr sysfs attribute name (e. g. "capabilities/key") * @param attr sysfs attribute name (e. g. "capabilities/key")
* @param bitmask: Output array which has a sizeof of bitmask_size * @param bitmask: Output array which has a sizeof of bitmask_size
*/ */
static void get_cap_mask(struct udev_device *dev, static void get_cap_mask(sd_device *dev,
struct udev_device *pdev, const char* attr, sd_device *pdev, const char* attr,
unsigned long *bitmask, size_t bitmask_size, unsigned long *bitmask, size_t bitmask_size,
bool test) { bool test) {
const char *v; const char *v;
@ -85,8 +85,8 @@ static void get_cap_mask(struct udev_device *dev,
char* word; char* word;
unsigned long val; unsigned long val;
v = udev_device_get_sysattr_value(pdev, attr); if (sd_device_get_sysattr_value(pdev, attr, &v) < 0)
v = strempty(v); v = "";
xsprintf(text, "%s", v); xsprintf(text, "%s", v);
log_debug("%s raw kernel attribute: %s", attr, text); log_debug("%s raw kernel attribute: %s", attr, text);
@ -126,7 +126,7 @@ static void get_cap_mask(struct udev_device *dev,
} }
/* pointer devices */ /* pointer devices */
static bool test_pointers(struct udev_device *dev, static bool test_pointers(sd_device *dev,
const unsigned long* bitmask_ev, const unsigned long* bitmask_ev,
const unsigned long* bitmask_abs, const unsigned long* bitmask_abs,
const unsigned long* bitmask_key, const unsigned long* bitmask_key,
@ -248,7 +248,7 @@ static bool test_pointers(struct udev_device *dev,
} }
/* key like devices */ /* key like devices */
static bool test_key(struct udev_device *dev, static bool test_key(sd_device *dev,
const unsigned long* bitmask_ev, const unsigned long* bitmask_ev,
const unsigned long* bitmask_key, const unsigned long* bitmask_key,
bool test) { bool test) {
@ -299,8 +299,8 @@ static bool test_key(struct udev_device *dev,
return ret; return ret;
} }
static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_input_id(sd_device *dev, int argc, char *argv[], bool test) {
struct udev_device *pdev; sd_device *pdev;
unsigned long bitmask_ev[NBITS(EV_MAX)]; unsigned long bitmask_ev[NBITS(EV_MAX)];
unsigned long bitmask_abs[NBITS(ABS_MAX)]; unsigned long bitmask_abs[NBITS(ABS_MAX)];
unsigned long bitmask_key[NBITS(KEY_MAX)]; unsigned long bitmask_key[NBITS(KEY_MAX)];
@ -314,9 +314,18 @@ static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], boo
/* walk up the parental chain until we find the real input device; the /* walk up the parental chain until we find the real input device; the
* argument is very likely a subdevice of this, like eventN */ * argument is very likely a subdevice of this, like eventN */
pdev = dev; for (pdev = dev; pdev; ) {
while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL) const char *s;
pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL);
if (sd_device_get_sysattr_value(pdev, "capabilities/ev", &s) >= 0)
break;
if (sd_device_get_parent_with_subsystem_devtype(pdev, "input", NULL, &pdev) >= 0)
continue;
pdev = NULL;
break;
}
if (pdev) { if (pdev) {
/* Use this as a flag that input devices were detected, so that this /* Use this as a flag that input devices were detected, so that this
@ -340,12 +349,12 @@ static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], boo
} }
devnode = udev_device_get_devnode(dev); if (sd_device_get_devname(dev, &devnode) >= 0 &&
sysname = udev_device_get_sysname(dev); sd_device_get_sysname(dev, &sysname) >= 0 &&
if (devnode && sysname && startswith(sysname, "event")) startswith(sysname, "event"))
extract_info(dev, devnode, test); extract_info(dev, devnode, test);
return EXIT_SUCCESS; return 0;
} }
const struct udev_builtin udev_builtin_input_id = { const struct udev_builtin udev_builtin_input_id = {

View File

@ -8,6 +8,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <linux/input.h> #include <linux/input.h>
#include "device-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "parse-util.h" #include "parse-util.h"
#include "stdio-util.h" #include "stdio-util.h"
@ -18,25 +19,25 @@
static const struct key_name *keyboard_lookup_key(const char *str, GPERF_LEN_TYPE len); static const struct key_name *keyboard_lookup_key(const char *str, GPERF_LEN_TYPE len);
#include "keyboard-keys-from-name.h" #include "keyboard-keys-from-name.h"
static int install_force_release(struct udev_device *dev, const unsigned *release, unsigned release_count) { static int install_force_release(sd_device *dev, const unsigned *release, unsigned release_count) {
struct udev_device *atkbd; sd_device *atkbd;
const char *cur; const char *cur;
char codes[4096]; char codes[4096];
char *s; char *s;
size_t l; size_t l;
unsigned i; unsigned i;
int ret; int r;
assert(dev); assert(dev);
assert(release); assert(release);
atkbd = udev_device_get_parent_with_subsystem_devtype(dev, "serio", NULL); r = sd_device_get_parent_with_subsystem_devtype(dev, "serio", NULL, &atkbd);
if (!atkbd) if (r < 0)
return -ENODEV; return r;
cur = udev_device_get_sysattr_value(atkbd, "force_release"); r = sd_device_get_sysattr_value(atkbd, "force_release", &cur);
if (!cur) if (r < 0)
return -ENODEV; return r;
s = codes; s = codes;
l = sizeof(codes); l = sizeof(codes);
@ -49,14 +50,14 @@ static int install_force_release(struct udev_device *dev, const unsigned *releas
l = strpcpyf(&s, l, ",%u", release[i]); l = strpcpyf(&s, l, ",%u", release[i]);
log_debug("keyboard: updating force-release list with '%s'", codes); log_debug("keyboard: updating force-release list with '%s'", codes);
ret = udev_device_set_sysattr_value(atkbd, "force_release", codes); r = sd_device_set_sysattr_value(atkbd, "force_release", codes);
if (ret < 0) if (r < 0)
log_error_errno(ret, "Error writing force-release attribute: %m"); return log_error_errno(r, "Error writing force-release attribute: %m");
return ret;
return 0;
} }
static void map_keycode(int fd, const char *devnode, int scancode, const char *keycode) static void map_keycode(int fd, const char *devnode, int scancode, const char *keycode) {
{
struct { struct {
unsigned scan; unsigned scan;
unsigned key; unsigned key;
@ -139,36 +140,37 @@ static void override_abs(int fd, const char *devnode,
log_error_errno(errno, "Unable to EVIOCSABS device \"%s\"", devnode); log_error_errno(errno, "Unable to EVIOCSABS device \"%s\"", devnode);
} }
static void set_trackpoint_sensitivity(struct udev_device *dev, const char *value) static int set_trackpoint_sensitivity(sd_device *dev, const char *value) {
{ sd_device *pdev;
struct udev_device *pdev;
char val_s[DECIMAL_STR_MAX(int)]; char val_s[DECIMAL_STR_MAX(int)];
const char *devnode;
int r, val_i; int r, val_i;
assert(dev); assert(dev);
assert(value); assert(value);
r = sd_device_get_devname(dev, &devnode);
if (r < 0)
return log_error_errno(r, "Failed to get devname: %m");
/* The sensitivity sysfs attr belongs to the serio parent device */ /* The sensitivity sysfs attr belongs to the serio parent device */
pdev = udev_device_get_parent_with_subsystem_devtype(dev, "serio", NULL); r = sd_device_get_parent_with_subsystem_devtype(dev, "serio", NULL, &pdev);
if (!pdev) { if (r < 0)
log_warning("Failed to get serio parent for '%s'", udev_device_get_devnode(dev)); return log_warning_errno(r, "Failed to get serio parent for '%s': %m", devnode);
return;
}
r = safe_atoi(value, &val_i); r = safe_atoi(value, &val_i);
if (r < 0) { if (r < 0)
log_error("Unable to parse POINTINGSTICK_SENSITIVITY '%s' for '%s'", value, udev_device_get_devnode(dev)); return log_error_errno(r, "Unable to parse POINTINGSTICK_SENSITIVITY '%s' for '%s': %m", value, devnode);
return; else if (val_i < 0 || val_i > 255)
} else if (val_i < 0 || val_i > 255) { return log_error_errno(ERANGE, "POINTINGSTICK_SENSITIVITY %d outside range [0..255] for '%s'", val_i, devnode);
log_error("POINTINGSTICK_SENSITIVITY %d outside range [0..255] for '%s' ", val_i, udev_device_get_devnode(dev));
return;
}
xsprintf(val_s, "%d", val_i); xsprintf(val_s, "%d", val_i);
r = udev_device_set_sysattr_value(pdev, "sensitivity", val_s); r = sd_device_set_sysattr_value(pdev, "sensitivity", val_s);
if (r < 0) if (r < 0)
log_error_errno(r, "Failed to write 'sensitivity' attribute for '%s': %m", udev_device_get_devnode(pdev)); return log_error_errno(r, "Failed to write 'sensitivity' attribute for '%s': %m", devnode);
return 0;
} }
static int open_device(const char *devnode) { static int open_device(const char *devnode) {
@ -181,27 +183,26 @@ static int open_device(const char *devnode) {
return fd; return fd;
} }
static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_keyboard(sd_device *dev, int argc, char *argv[], bool test) {
struct udev_list_entry *entry;
unsigned release[1024]; unsigned release[1024];
unsigned release_count = 0; unsigned release_count = 0;
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
const char *node; const char *node, *key, *value;
int has_abs = -1; int has_abs = -1, r;
node = udev_device_get_devnode(dev); r = sd_device_get_devname(dev, &node);
if (!node) { if (r < 0) {
log_error("No device node for \"%s\"", udev_device_get_syspath(dev)); const char *s = NULL;
return EXIT_FAILURE;
(void) sd_device_get_syspath(dev, &s);
return log_error_errno(r, "No device node for \"%s\": %m", strnull(s));
} }
udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) { FOREACH_DEVICE_PROPERTY(dev, key, value) {
const char *key;
char *endptr; char *endptr;
key = udev_list_entry_get_name(entry);
if (startswith(key, "KEYBOARD_KEY_")) { if (startswith(key, "KEYBOARD_KEY_")) {
const char *keycode; const char *keycode = value;
unsigned scancode; unsigned scancode;
/* KEYBOARD_KEY_<hex scan code>=<key identifier string> */ /* KEYBOARD_KEY_<hex scan code>=<key identifier string> */
@ -211,8 +212,6 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
continue; continue;
} }
keycode = udev_list_entry_get_value(entry);
/* a leading '!' needs a force-release entry */ /* a leading '!' needs a force-release entry */
if (keycode[0] == '!') { if (keycode[0] == '!') {
keycode++; keycode++;
@ -228,7 +227,7 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
if (fd == -1) { if (fd == -1) {
fd = open_device(node); fd = open_device(node);
if (fd < 0) if (fd < 0)
return EXIT_FAILURE; return fd;
} }
map_keycode(fd, node, scancode, keycode); map_keycode(fd, node, scancode, keycode);
@ -245,7 +244,7 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
if (fd == -1) { if (fd == -1) {
fd = open_device(node); fd = open_device(node);
if (fd < 0) if (fd < 0)
return EXIT_FAILURE; return fd;
} }
if (has_abs == -1) { if (has_abs == -1) {
@ -253,10 +252,8 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
int rc; int rc;
rc = ioctl(fd, EVIOCGBIT(0, sizeof(bits)), &bits); rc = ioctl(fd, EVIOCGBIT(0, sizeof(bits)), &bits);
if (rc < 0) { if (rc < 0)
log_error_errno(errno, "Unable to EVIOCGBIT device \"%s\"", node); return log_error_errno(errno, "Unable to EVIOCGBIT device \"%s\"", node);
return EXIT_FAILURE;
}
has_abs = !!(bits & (1 << EV_ABS)); has_abs = !!(bits & (1 << EV_ABS));
if (!has_abs) if (!has_abs)
@ -266,16 +263,16 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
if (!has_abs) if (!has_abs)
continue; continue;
override_abs(fd, node, evcode, udev_list_entry_get_value(entry)); override_abs(fd, node, evcode, value);
} else if (streq(key, "POINTINGSTICK_SENSITIVITY")) } else if (streq(key, "POINTINGSTICK_SENSITIVITY"))
set_trackpoint_sensitivity(dev, udev_list_entry_get_value(entry)); set_trackpoint_sensitivity(dev, value);
} }
/* install list of force-release codes */ /* install list of force-release codes */
if (release_count > 0) if (release_count > 0)
install_force_release(dev, release, release_count); install_force_release(dev, release, release_count);
return EXIT_SUCCESS; return 0;
} }
const struct udev_builtin udev_builtin_keyboard = { const struct udev_builtin udev_builtin_keyboard = {

View File

@ -21,7 +21,7 @@ _printf_(6,0) static void udev_kmod_log(void *data, int priority, const char *fi
log_internalv(priority, 0, file, line, fn, format, args); log_internalv(priority, 0, file, line, fn, format, args);
} }
static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_kmod(sd_device *dev, int argc, char *argv[], bool test) {
int i; int i;
if (!ctx) if (!ctx)
@ -29,13 +29,13 @@ static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool te
if (argc < 3 || !streq(argv[1], "load")) { if (argc < 3 || !streq(argv[1], "load")) {
log_error("%s: expected: load <module>", argv[0]); log_error("%s: expected: load <module>", argv[0]);
return EXIT_FAILURE; return -EINVAL;
} }
for (i = 2; argv[i]; i++) for (i = 2; argv[i]; i++)
(void) module_load_and_warn(ctx, argv[i], false); (void) module_load_and_warn(ctx, argv[i], false);
return EXIT_SUCCESS; return 0;
} }
/* called at udev startup and reload */ /* called at udev startup and reload */

View File

@ -106,7 +106,6 @@
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "fs-util.h" #include "fs-util.h"
#include "libudev-private.h"
#include "parse-util.h" #include "parse-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "string-util.h" #include "string-util.h"
@ -133,7 +132,7 @@ struct netnames {
uint8_t mac[6]; uint8_t mac[6];
bool mac_valid; bool mac_valid;
struct udev_device *pcidev; sd_device *pcidev;
char pci_slot[IFNAMSIZ]; char pci_slot[IFNAMSIZ];
char pci_path[IFNAMSIZ]; char pci_path[IFNAMSIZ];
char pci_onboard[IFNAMSIZ]; char pci_onboard[IFNAMSIZ];
@ -147,94 +146,111 @@ struct netnames {
}; };
struct virtfn_info { struct virtfn_info {
struct udev_device *physfn_pcidev; sd_device *physfn_pcidev;
char suffix[IFNAMSIZ]; char suffix[IFNAMSIZ];
}; };
/* skip intermediate virtio devices */ /* skip intermediate virtio devices */
static struct udev_device *skip_virtio(struct udev_device *dev) { static sd_device *skip_virtio(sd_device *dev) {
struct udev_device *parent = dev; sd_device *parent;
/* there can only ever be one virtio bus per parent device, so we can /* there can only ever be one virtio bus per parent device, so we can
safely ignore any virtio buses. see * safely ignore any virtio buses. see
<http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html> */ * http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
while (parent && streq_ptr("virtio", udev_device_get_subsystem(parent))) for (parent = dev; parent; ) {
parent = udev_device_get_parent(parent); const char *subsystem;
if (sd_device_get_subsystem(parent, &subsystem) < 0)
break;
if (!streq(subsystem, "virtio"))
break;
if (sd_device_get_parent(parent, &parent) < 0)
return NULL;
}
return parent; return parent;
} }
static int get_virtfn_info(struct udev_device *dev, struct netnames *names, struct virtfn_info *vf_info) { static int get_virtfn_info(sd_device *dev, struct netnames *names, struct virtfn_info *ret) {
const char *physfn_link_file; _cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL;
const char *physfn_link_file, *syspath;
_cleanup_free_ char *physfn_pci_syspath = NULL; _cleanup_free_ char *physfn_pci_syspath = NULL;
_cleanup_free_ char *virtfn_pci_syspath = NULL; _cleanup_free_ char *virtfn_pci_syspath = NULL;
struct dirent *dent; struct dirent *dent;
_cleanup_closedir_ DIR *dir = NULL; _cleanup_closedir_ DIR *dir = NULL;
struct virtfn_info vf_info_local = {}; char suffix[IFNAMSIZ];
int r; int r;
assert(dev);
assert(names);
assert(ret);
r = sd_device_get_syspath(names->pcidev, &syspath);
if (r < 0)
return r;
/* Check if this is a virtual function. */ /* Check if this is a virtual function. */
physfn_link_file = strjoina(udev_device_get_syspath(names->pcidev), "/physfn"); physfn_link_file = strjoina(syspath, "/physfn");
r = chase_symlinks(physfn_link_file, NULL, 0, &physfn_pci_syspath); r = chase_symlinks(physfn_link_file, NULL, 0, &physfn_pci_syspath);
if (r < 0) if (r < 0)
return r; return r;
/* Get physical function's pci device. */ /* Get physical function's pci device. */
vf_info_local.physfn_pcidev = udev_device_new_from_syspath(NULL, physfn_pci_syspath); r = sd_device_new_from_syspath(&physfn_pcidev, physfn_pci_syspath);
if (!vf_info_local.physfn_pcidev) if (r < 0)
return -ENOENT; return r;
/* Find the virtual function number by finding the right virtfn link. */ /* Find the virtual function number by finding the right virtfn link. */
dir = opendir(physfn_pci_syspath); dir = opendir(physfn_pci_syspath);
if (!dir) { if (!dir)
r = -errno; return -errno;
goto out_unref;
}
FOREACH_DIRENT_ALL(dent, dir, break) { FOREACH_DIRENT_ALL(dent, dir, break) {
_cleanup_free_ char *virtfn_link_file = NULL; _cleanup_free_ char *virtfn_link_file = NULL;
if (!startswith(dent->d_name, "virtfn")) if (!startswith(dent->d_name, "virtfn"))
continue; continue;
virtfn_link_file = strjoin(physfn_pci_syspath, "/", dent->d_name); virtfn_link_file = strjoin(physfn_pci_syspath, "/", dent->d_name);
if (!virtfn_link_file) { if (!virtfn_link_file)
r = -ENOMEM; return -ENOMEM;
goto out_unref;
}
if (chase_symlinks(virtfn_link_file, NULL, 0, &virtfn_pci_syspath) < 0) if (chase_symlinks(virtfn_link_file, NULL, 0, &virtfn_pci_syspath) < 0)
continue; continue;
if (streq(udev_device_get_syspath(names->pcidev), virtfn_pci_syspath)) {
if (!snprintf_ok(vf_info_local.suffix, sizeof(vf_info_local.suffix), "v%s", &dent->d_name[6])) { if (streq(syspath, virtfn_pci_syspath)) {
r = -ENOENT; if (!snprintf_ok(suffix, sizeof(suffix), "v%s", &dent->d_name[6]))
goto out_unref; return -ENOENT;
}
break; break;
} }
} }
if (isempty(vf_info_local.suffix)) { if (isempty(suffix))
r = -ENOENT; return -ENOENT;
goto out_unref;
}
*vf_info = vf_info_local;
return 0;
out_unref: ret->physfn_pcidev = TAKE_PTR(physfn_pcidev);
udev_device_unref(vf_info_local.physfn_pcidev); strncpy(ret->suffix, suffix, sizeof(ret->suffix));
return r;
return 0;
} }
/* retrieve on-board index number and label from firmware */ /* retrieve on-board index number and label from firmware */
static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) { static int dev_pci_onboard(sd_device *dev, struct netnames *names) {
unsigned long idx, dev_port = 0; unsigned long idx, dev_port = 0;
const char *attr, *port_name; const char *attr, *port_name = NULL;
size_t l; size_t l;
char *s; char *s;
int r; int r;
/* ACPI _DSM — device specific method for naming a PCI or PCI Express device */ /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
attr = udev_device_get_sysattr_value(names->pcidev, "acpi_index"); if (sd_device_get_sysattr_value(names->pcidev, "acpi_index", &attr) < 0) {
/* SMBIOS type 41 — Onboard Devices Extended Information */ /* SMBIOS type 41 — Onboard Devices Extended Information */
if (!attr) r = sd_device_get_sysattr_value(names->pcidev, "index", &attr);
attr = udev_device_get_sysattr_value(names->pcidev, "index"); if (r < 0)
if (!attr) return r;
return -ENOENT; }
r = safe_atolu(attr, &idx); r = safe_atolu(attr, &idx);
if (r < 0) if (r < 0)
@ -248,12 +264,11 @@ static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
return -ENOENT; return -ENOENT;
/* kernel provided port index for multiple ports on a single PCI function */ /* kernel provided port index for multiple ports on a single PCI function */
attr = udev_device_get_sysattr_value(dev, "dev_port"); if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0)
if (attr)
dev_port = strtoul(attr, NULL, 10); dev_port = strtoul(attr, NULL, 10);
/* kernel provided front panel port name for multiple port PCI device */ /* kernel provided front panel port name for multiple port PCI device */
port_name = udev_device_get_sysattr_value(dev, "phys_port_name"); (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
s = names->pci_onboard; s = names->pci_onboard;
l = sizeof(names->pci_onboard); l = sizeof(names->pci_onboard);
@ -265,18 +280,22 @@ static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
if (l == 0) if (l == 0)
names->pci_onboard[0] = '\0'; names->pci_onboard[0] = '\0';
names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label"); if (sd_device_get_sysattr_value(names->pcidev, "label", &names->pci_onboard_label) < 0)
names->pci_onboard_label = NULL;
return 0; return 0;
} }
/* read the 256 bytes PCI configuration space to check the multi-function bit */ /* read the 256 bytes PCI configuration space to check the multi-function bit */
static bool is_pci_multifunction(struct udev_device *dev) { static bool is_pci_multifunction(sd_device *dev) {
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
const char *filename; const char *filename, *syspath;
uint8_t config[64]; uint8_t config[64];
filename = strjoina(udev_device_get_syspath(dev), "/config"); if (sd_device_get_syspath(dev, &syspath) < 0)
return false;
filename = strjoina(syspath, "/config");
fd = open(filename, O_RDONLY | O_CLOEXEC); fd = open(filename, O_RDONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
return false; return false;
@ -284,30 +303,38 @@ static bool is_pci_multifunction(struct udev_device *dev) {
return false; return false;
/* bit 0-6 header type, bit 7 multi/single function device */ /* bit 0-6 header type, bit 7 multi/single function device */
if ((config[PCI_HEADER_TYPE] & 0x80) != 0) return config[PCI_HEADER_TYPE] & 0x80;
return true;
return false;
} }
static bool is_pci_ari_enabled(struct udev_device *dev) { static bool is_pci_ari_enabled(sd_device *dev) {
return streq_ptr(udev_device_get_sysattr_value(dev, "ari_enabled"), "1"); const char *a;
if (sd_device_get_sysattr_value(dev, "ari_enabled", &a) < 0)
return false;
return streq(a, "1");
} }
static int dev_pci_slot(struct udev_device *dev, struct netnames *names) { static int dev_pci_slot(sd_device *dev, struct netnames *names) {
unsigned long dev_port = 0; unsigned long dev_port = 0;
unsigned domain, bus, slot, func, hotplug_slot = 0; unsigned domain, bus, slot, func, hotplug_slot = 0;
size_t l; size_t l;
char *s; char *s;
const char *attr, *port_name; const char *sysname, *attr, *port_name = NULL, *syspath;
_cleanup_(udev_device_unrefp) struct udev_device *pci = NULL; _cleanup_(sd_device_unrefp) sd_device *pci = NULL;
struct udev_device *hotplug_slot_dev; sd_device *hotplug_slot_dev;
char slots[PATH_MAX]; char slots[PATH_MAX];
_cleanup_closedir_ DIR *dir = NULL; _cleanup_closedir_ DIR *dir = NULL;
struct dirent *dent; struct dirent *dent;
int r;
if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4) r = sd_device_get_sysname(names->pcidev, &sysname);
if (r < 0)
return r;
if (sscanf(sysname, "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
return -ENOENT; return -ENOENT;
if (is_pci_ari_enabled(names->pcidev)) if (is_pci_ari_enabled(names->pcidev))
/* ARI devices support up to 256 functions on a single device ("slot"), and interpret the /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
* traditional 5-bit slot and 3-bit function number as a single 8-bit function number, * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
@ -315,27 +342,24 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
func += slot * 8; func += slot * 8;
/* kernel provided port index for multiple ports on a single PCI function */ /* kernel provided port index for multiple ports on a single PCI function */
attr = udev_device_get_sysattr_value(dev, "dev_port"); if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0) {
if (attr) {
dev_port = strtoul(attr, NULL, 10); dev_port = strtoul(attr, NULL, 10);
/* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
* provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port', * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
* which thus stays initialized as 0. */ * which thus stays initialized as 0. */
if (dev_port == 0) { if (dev_port == 0 &&
attr = udev_device_get_sysattr_value(dev, "type"); sd_device_get_sysattr_value(dev, "type", &attr) >= 0) {
if (attr) { unsigned long type;
unsigned long type = strtoul(attr, NULL, 10);
if (type == ARPHRD_INFINIBAND) { type = strtoul(attr, NULL, 10);
attr = udev_device_get_sysattr_value(dev, "dev_id"); if (type == ARPHRD_INFINIBAND &&
if (attr) sd_device_get_sysattr_value(dev, "dev_id", &attr) >= 0)
dev_port = strtoul(attr, NULL, 16); dev_port = strtoul(attr, NULL, 16);
}
}
} }
} }
/* kernel provided front panel port name for multiple port PCI device */ /* kernel provided front panel port name for multiple port PCI device */
port_name = udev_device_get_sysattr_value(dev, "phys_port_name"); (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
/* compose a name based on the raw kernel's PCI bus, slot numbers */ /* compose a name based on the raw kernel's PCI bus, slot numbers */
s = names->pci_path; s = names->pci_path;
@ -353,11 +377,14 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
names->pci_path[0] = '\0'; names->pci_path[0] = '\0';
/* ACPI _SUN — slot user number */ /* ACPI _SUN — slot user number */
pci = udev_device_new_from_subsystem_sysname(NULL, "subsystem", "pci"); r = sd_device_new_from_subsystem_sysname(&pci, "subsystem", "pci");
if (!pci) if (r < 0)
return -ENOENT; return r;
if (!snprintf_ok(slots, sizeof slots, "%s/slots", udev_device_get_syspath(pci))) r = sd_device_get_syspath(pci, &syspath);
if (r < 0)
return r;
if (!snprintf_ok(slots, sizeof slots, "%s/slots", syspath))
return -ENAMETOOLONG; return -ENAMETOOLONG;
dir = opendir(slots); dir = opendir(slots);
@ -366,31 +393,33 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
hotplug_slot_dev = names->pcidev; hotplug_slot_dev = names->pcidev;
while (hotplug_slot_dev) { while (hotplug_slot_dev) {
if (sd_device_get_sysname(hotplug_slot_dev, &sysname) < 0)
continue;
FOREACH_DIRENT_ALL(dent, dir, break) { FOREACH_DIRENT_ALL(dent, dir, break) {
unsigned i; unsigned i;
int r;
char str[PATH_MAX]; char str[PATH_MAX];
_cleanup_free_ char *address = NULL; _cleanup_free_ char *address = NULL;
if (dent->d_name[0] == '.') if (dent->d_name[0] == '.')
continue; continue;
r = safe_atou_full(dent->d_name, 10, &i); r = safe_atou_full(dent->d_name, 10, &i);
if (i < 1 || r < 0) if (r < 0 || i <= 0)
continue; continue;
/* match slot address with device by stripping the function */
if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, dent->d_name) && if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, dent->d_name) &&
read_one_line_file(str, &address) >= 0) read_one_line_file(str, &address) >= 0 &&
/* match slot address with device by stripping the function */ startswith(sysname, address)) {
if (startswith(udev_device_get_sysname(hotplug_slot_dev), address)) hotplug_slot = i;
hotplug_slot = i;
if (hotplug_slot > 0)
break; break;
}
} }
if (hotplug_slot > 0) if (hotplug_slot > 0)
break; break;
if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev, "pci", NULL, &hotplug_slot_dev) < 0)
break;
rewinddir(dir); rewinddir(dir);
hotplug_slot_dev = udev_device_get_parent_with_subsystem_devtype(hotplug_slot_dev, "pci", NULL);
} }
if (hotplug_slot > 0) { if (hotplug_slot > 0) {
@ -412,24 +441,30 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
return 0; return 0;
} }
static int names_vio(struct udev_device *dev, struct netnames *names) { static int names_vio(sd_device *dev, struct netnames *names) {
struct udev_device *parent; sd_device *parent;
unsigned busid, slotid, ethid; unsigned busid, slotid, ethid;
const char *syspath; const char *syspath, *subsystem;
int r;
/* check if our direct parent is a VIO device with no other bus in-between */ /* check if our direct parent is a VIO device with no other bus in-between */
parent = udev_device_get_parent(dev); r = sd_device_get_parent(dev, &parent);
if (!parent) if (r < 0)
return -ENOENT; return r;
if (!streq_ptr("vio", udev_device_get_subsystem(parent))) r = sd_device_get_subsystem(parent, &subsystem);
return -ENOENT; if (r < 0)
return r;
if (!streq("vio", subsystem))
return -ENOENT;
/* The devices' $DEVPATH number is tied to (virtual) hardware (slot id /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
* selected in the HMC), thus this provides a reliable naming (e.g. * selected in the HMC), thus this provides a reliable naming (e.g.
* "/devices/vio/30000002/net/eth1"); we ignore the bus number, as * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
* there should only ever be one bus, and then remove leading zeros. */ * there should only ever be one bus, and then remove leading zeros. */
syspath = udev_device_get_syspath(dev); r = sd_device_get_syspath(dev, &syspath);
if (r < 0)
return r;
if (sscanf(syspath, "/sys/devices/vio/%4x%4x/net/eth%u", &busid, &slotid, &ethid) != 3) if (sscanf(syspath, "/sys/devices/vio/%4x%4x/net/eth%u", &busid, &slotid, &ethid) != 3)
return -EINVAL; return -EINVAL;
@ -443,21 +478,28 @@ static int names_vio(struct udev_device *dev, struct netnames *names) {
#define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u" #define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
#define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u" #define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
static int names_platform(struct udev_device *dev, struct netnames *names, bool test) { static int names_platform(sd_device *dev, struct netnames *names, bool test) {
struct udev_device *parent; sd_device *parent;
char vendor[5]; char vendor[5];
unsigned model, instance, ethid; unsigned model, instance, ethid;
const char *syspath, *pattern, *validchars; const char *syspath, *pattern, *validchars, *subsystem;
int r;
/* check if our direct parent is a platform device with no other bus in-between */ /* check if our direct parent is a platform device with no other bus in-between */
parent = udev_device_get_parent(dev); r = sd_device_get_parent(dev, &parent);
if (!parent) if (r < 0)
return -ENOENT; return r;
if (!streq_ptr("platform", udev_device_get_subsystem(parent))) r = sd_device_get_subsystem(parent, &subsystem);
if (r < 0)
return r;
if (!streq("platform", subsystem))
return -ENOENT; return -ENOENT;
syspath = udev_device_get_syspath(dev); r = sd_device_get_syspath(dev, &syspath);
if (r < 0)
return r;
/* syspath is too short, to have a valid ACPI instance */ /* syspath is too short, to have a valid ACPI instance */
if (strlen(syspath) < sizeof _PLATFORM_TEST) if (strlen(syspath) < sizeof _PLATFORM_TEST)
@ -493,15 +535,19 @@ static int names_platform(struct udev_device *dev, struct netnames *names, bool
return 0; return 0;
} }
static int names_pci(struct udev_device *dev, struct netnames *names) { static int names_pci(sd_device *dev, struct netnames *names) {
struct udev_device *parent; sd_device *parent;
struct netnames vf_names = {}; struct netnames vf_names = {};
struct virtfn_info vf_info = {}; struct virtfn_info vf_info = {};
const char *subsystem;
int r;
assert(dev); assert(dev);
assert(names); assert(names);
parent = udev_device_get_parent(dev); r = sd_device_get_parent(dev, &parent);
if (r < 0)
return r;
/* skip virtio subsystem if present */ /* skip virtio subsystem if present */
parent = skip_virtio(parent); parent = skip_virtio(parent);
@ -509,13 +555,14 @@ static int names_pci(struct udev_device *dev, struct netnames *names) {
return -ENOENT; return -ENOENT;
/* check if our direct parent is a PCI device with no other bus in-between */ /* check if our direct parent is a PCI device with no other bus in-between */
if (streq_ptr("pci", udev_device_get_subsystem(parent))) { if (sd_device_get_subsystem(parent, &subsystem) >= 0 &&
streq("pci", subsystem)) {
names->type = NET_PCI; names->type = NET_PCI;
names->pcidev = parent; names->pcidev = parent;
} else { } else {
names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL); r = sd_device_get_parent_with_subsystem_devtype(dev, "pci", NULL, &names->pcidev);
if (!names->pcidev) if (r < 0)
return -ENOENT; return r;
} }
if (get_virtfn_info(dev, names, &vf_info) >= 0) { if (get_virtfn_info(dev, names, &vf_info) >= 0) {
@ -535,32 +582,35 @@ static int names_pci(struct udev_device *dev, struct netnames *names) {
if (strlen(vf_names.pci_path) + strlen(vf_info.suffix) < sizeof(names->pci_path)) if (strlen(vf_names.pci_path) + strlen(vf_info.suffix) < sizeof(names->pci_path))
strscpyl(names->pci_path, sizeof(names->pci_path), strscpyl(names->pci_path, sizeof(names->pci_path),
vf_names.pci_path, vf_info.suffix, NULL); vf_names.pci_path, vf_info.suffix, NULL);
udev_device_unref(vf_info.physfn_pcidev); sd_device_unref(vf_info.physfn_pcidev);
} else { } else {
dev_pci_onboard(dev, names); dev_pci_onboard(dev, names);
dev_pci_slot(dev, names); dev_pci_slot(dev, names);
} }
return 0; return 0;
} }
static int names_usb(struct udev_device *dev, struct netnames *names) { static int names_usb(sd_device *dev, struct netnames *names) {
struct udev_device *usbdev; sd_device *usbdev;
char name[256]; char name[256], *ports, *config, *interf, *s;
char *ports; const char *sysname;
char *config;
char *interf;
size_t l; size_t l;
char *s; int r;
assert(dev); assert(dev);
assert(names); assert(names);
usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface"); r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &usbdev);
if (!usbdev) if (r < 0)
return -ENOENT; return r;
r = sd_device_get_sysname(usbdev, &sysname);
if (r < 0)
return r;
/* get USB port number chain, configuration, interface */ /* get USB port number chain, configuration, interface */
strscpy(name, sizeof(name), udev_device_get_sysname(usbdev)); strscpy(name, sizeof(name), sysname);
s = strchr(name, '-'); s = strchr(name, '-');
if (!s) if (!s)
return -EINVAL; return -EINVAL;
@ -599,19 +649,25 @@ static int names_usb(struct udev_device *dev, struct netnames *names) {
return 0; return 0;
} }
static int names_bcma(struct udev_device *dev, struct netnames *names) { static int names_bcma(sd_device *dev, struct netnames *names) {
struct udev_device *bcmadev; sd_device *bcmadev;
unsigned core; unsigned core;
const char *sysname;
int r;
assert(dev); assert(dev);
assert(names); assert(names);
bcmadev = udev_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL); r = sd_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL, &bcmadev);
if (!bcmadev) if (r < 0)
return -ENOENT; return r;
r = sd_device_get_sysname(bcmadev, &sysname);
if (r < 0)
return r;
/* bus num:core num */ /* bus num:core num */
if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*u:%u", &core) != 1) if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
return -EINVAL; return -EINVAL;
/* suppress the common core == 0 */ /* suppress the common core == 0 */
if (core > 0) if (core > 0)
@ -621,34 +677,41 @@ static int names_bcma(struct udev_device *dev, struct netnames *names) {
return 0; return 0;
} }
static int names_ccw(struct udev_device *dev, struct netnames *names) { static int names_ccw(sd_device *dev, struct netnames *names) {
struct udev_device *cdev; sd_device *cdev;
const char *bus_id, *subsys; const char *bus_id, *subsys;
size_t bus_id_len; size_t bus_id_len;
size_t bus_id_start; size_t bus_id_start;
int r;
assert(dev); assert(dev);
assert(names); assert(names);
/* Retrieve the associated CCW device */ /* Retrieve the associated CCW device */
cdev = udev_device_get_parent(dev); r = sd_device_get_parent(dev, &cdev);
if (r < 0)
return r;
/* skip virtio subsystem if present */ /* skip virtio subsystem if present */
cdev = skip_virtio(cdev); cdev = skip_virtio(cdev);
if (!cdev) if (!cdev)
return -ENOENT; return -ENOENT;
r = sd_device_get_subsystem(cdev, &subsys);
if (r < 0)
return r;
/* Network devices are either single or grouped CCW devices */ /* Network devices are either single or grouped CCW devices */
subsys = udev_device_get_subsystem(cdev); if (!STR_IN_SET(subsys, "ccwgroup", "ccw"))
if (!STRPTR_IN_SET(subsys, "ccwgroup", "ccw"))
return -ENOENT; return -ENOENT;
/* Retrieve bus-ID of the CCW device. The bus-ID uniquely /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
* identifies the network device on the Linux on System z channel * identifies the network device on the Linux on System z channel
* subsystem. Note that the bus-ID contains lowercase characters. * subsystem. Note that the bus-ID contains lowercase characters.
*/ */
bus_id = udev_device_get_sysname(cdev); r = sd_device_get_sysname(cdev, &bus_id);
if (!bus_id) if (r < 0)
return -ENOENT; return r;
/* Check the length of the bus-ID. Rely on that the kernel provides /* Check the length of the bus-ID. Rely on that the kernel provides
* a correct bus-ID; alternatively, improve this check and parse and * a correct bus-ID; alternatively, improve this check and parse and
@ -673,17 +736,19 @@ static int names_ccw(struct udev_device *dev, struct netnames *names) {
return 0; return 0;
} }
static int names_mac(struct udev_device *dev, struct netnames *names) { static int names_mac(sd_device *dev, struct netnames *names) {
const char *s; const char *s;
unsigned long i; unsigned long i;
unsigned a1, a2, a3, a4, a5, a6; unsigned a1, a2, a3, a4, a5, a6;
int r;
/* Some kinds of devices tend to have hardware addresses /* Some kinds of devices tend to have hardware addresses
* that are impossible to use in an iface name. * that are impossible to use in an iface name.
*/ */
s = udev_device_get_sysattr_value(dev, "type"); r = sd_device_get_sysattr_value(dev, "type", &s);
if (!s) if (r < 0)
return EXIT_FAILURE; return r;
i = strtoul(s, NULL, 0); i = strtoul(s, NULL, 0);
switch (i) { switch (i) {
/* The persistent part of a hardware address of an InfiniBand NIC /* The persistent part of a hardware address of an InfiniBand NIC
@ -696,16 +761,16 @@ static int names_mac(struct udev_device *dev, struct netnames *names) {
} }
/* check for NET_ADDR_PERM, skip random MAC addresses */ /* check for NET_ADDR_PERM, skip random MAC addresses */
s = udev_device_get_sysattr_value(dev, "addr_assign_type"); r = sd_device_get_sysattr_value(dev, "addr_assign_type", &s);
if (!s) if (r < 0)
return EXIT_FAILURE; return r;
i = strtoul(s, NULL, 0); i = strtoul(s, NULL, 0);
if (i != 0) if (i != 0)
return 0; return 0;
s = udev_device_get_sysattr_value(dev, "address"); r = sd_device_get_sysattr_value(dev, "address", &s);
if (!s) if (r < 0)
return -ENOENT; return r;
if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6) if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
return -EINVAL; return -EINVAL;
@ -724,7 +789,7 @@ static int names_mac(struct udev_device *dev, struct netnames *names) {
} }
/* IEEE Organizationally Unique Identifier vendor string */ /* IEEE Organizationally Unique Identifier vendor string */
static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) { static int ieee_oui(sd_device *dev, struct netnames *names, bool test) {
char str[32]; char str[32];
if (!names->mac_valid) if (!names->mac_valid)
@ -739,21 +804,17 @@ static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test)
return 0; return 0;
} }
static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
const char *s; const char *s, *p, *devtype, *prefix = "en";
const char *p;
unsigned long i;
const char *devtype;
const char *prefix = "en";
struct netnames names = {}; struct netnames names = {};
int err; unsigned long i;
int r;
/* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
r = sd_device_get_sysattr_value(dev, "type", &s);
if (r < 0)
return r;
/* handle only ARPHRD_ETHER, ARPHRD_SLIP
* and ARPHRD_INFINIBAND devices
*/
s = udev_device_get_sysattr_value(dev, "type");
if (!s)
return EXIT_FAILURE;
i = strtoul(s, NULL, 0); i = strtoul(s, NULL, 0);
switch (i) { switch (i) {
case ARPHRD_ETHER: case ARPHRD_ETHER:
@ -770,25 +831,24 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
} }
/* skip stacked devices, like VLANs, ... */ /* skip stacked devices, like VLANs, ... */
s = udev_device_get_sysattr_value(dev, "ifindex"); r = sd_device_get_sysattr_value(dev, "ifindex", &s);
if (!s) if (r < 0)
return EXIT_FAILURE; return r;
p = udev_device_get_sysattr_value(dev, "iflink"); r = sd_device_get_sysattr_value(dev, "iflink", &p);
if (!p) if (r < 0)
return EXIT_FAILURE; return r;
if (!streq(s, p)) if (!streq(s, p))
return 0; return 0;
devtype = udev_device_get_devtype(dev); if (sd_device_get_devtype(dev, &devtype) >= 0) {
if (devtype) {
if (streq("wlan", devtype)) if (streq("wlan", devtype))
prefix = "wl"; prefix = "wl";
else if (streq("wwan", devtype)) else if (streq("wwan", devtype))
prefix = "ww"; prefix = "ww";
} }
err = names_mac(dev, &names); r = names_mac(dev, &names);
if (err >= 0 && names.mac_valid) { if (r >= 0 && names.mac_valid) {
char str[IFNAMSIZ]; char str[IFNAMSIZ];
xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix, xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix,
@ -800,39 +860,35 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
} }
/* get path names for Linux on System z network devices */ /* get path names for Linux on System z network devices */
err = names_ccw(dev, &names); if (names_ccw(dev, &names) >= 0 && names.type == NET_CCW) {
if (err >= 0 && names.type == NET_CCW) {
char str[IFNAMSIZ]; char str[IFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.ccw_busid)) if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.ccw_busid))
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
goto out; return 0;
} }
/* get ibmveth/ibmvnic slot-based names. */ /* get ibmveth/ibmvnic slot-based names. */
err = names_vio(dev, &names); if (names_vio(dev, &names) >= 0 && names.type == NET_VIO) {
if (err >= 0 && names.type == NET_VIO) {
char str[IFNAMSIZ]; char str[IFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.vio_slot)) if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.vio_slot))
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
goto out; return 0;
} }
/* get ACPI path names for ARM64 platform devices */ /* get ACPI path names for ARM64 platform devices */
err = names_platform(dev, &names, test); if (names_platform(dev, &names, test) >= 0 && names.type == NET_PLATFORM) {
if (err >= 0 && names.type == NET_PLATFORM) {
char str[IFNAMSIZ]; char str[IFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.platform_path)) if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.platform_path))
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
goto out; return 0;
} }
/* get PCI based path names, we compose only PCI based paths */ /* get PCI based path names, we compose only PCI based paths */
err = names_pci(dev, &names); if (names_pci(dev, &names) < 0)
if (err < 0) return 0;
goto out;
/* plain PCI device */ /* plain PCI device */
if (names.type == NET_PCI) { if (names.type == NET_PCI) {
@ -853,12 +909,11 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
if (names.pci_slot[0] && if (names.pci_slot[0] &&
snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_slot)) snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_slot))
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
goto out; return 0;
} }
/* USB device */ /* USB device */
err = names_usb(dev, &names); if (names_usb(dev, &names) >= 0 && names.type == NET_USB) {
if (err >= 0 && names.type == NET_USB) {
char str[IFNAMSIZ]; char str[IFNAMSIZ];
if (names.pci_path[0] && if (names.pci_path[0] &&
@ -868,12 +923,11 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
if (names.pci_slot[0] && if (names.pci_slot[0] &&
snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.usb_ports)) snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.usb_ports))
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
goto out; return 0;
} }
/* Broadcom bus */ /* Broadcom bus */
err = names_bcma(dev, &names); if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) {
if (err >= 0 && names.type == NET_BCMA) {
char str[IFNAMSIZ]; char str[IFNAMSIZ];
if (names.pci_path[0] && if (names.pci_path[0] &&
@ -883,10 +937,10 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
if (names.pci_slot[0] && if (names.pci_slot[0] &&
snprintf(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.bcma_core)) snprintf(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.bcma_core))
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
goto out; return 0;
} }
out:
return EXIT_SUCCESS; return 0;
} }
const struct udev_builtin udev_builtin_net_id = { const struct udev_builtin udev_builtin_net_id = {

View File

@ -1,14 +1,14 @@
/* SPDX-License-Identifier: LGPL-2.1+ */ /* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h" #include "alloc-util.h"
#include "libudev-device-internal.h"
#include "link-config.h" #include "link-config.h"
#include "log.h" #include "log.h"
#include "string-util.h"
#include "udev-builtin.h" #include "udev-builtin.h"
static link_config_ctx *ctx = NULL; static link_config_ctx *ctx = NULL;
static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) { static int builtin_net_setup_link(sd_device *dev, int argc, char **argv, bool test) {
_cleanup_free_ char *driver = NULL; _cleanup_free_ char *driver = NULL;
const char *name = NULL; const char *name = NULL;
link_config *link; link_config *link;
@ -16,34 +16,35 @@ static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv
if (argc > 1) { if (argc > 1) {
log_error("This program takes no arguments."); log_error("This program takes no arguments.");
return EXIT_FAILURE; return -EINVAL;
} }
r = link_get_driver(ctx, dev->device, &driver); r = link_get_driver(ctx, dev, &driver);
if (r >= 0) if (r >= 0)
udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver); udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
r = link_config_get(ctx, dev->device, &link); r = link_config_get(ctx, dev, &link);
if (r < 0) { if (r < 0) {
if (r == -ENOENT) { if (r == -ENOENT)
log_debug("No matching link configuration found."); return log_debug_errno(r, "No matching link configuration found.");
return EXIT_SUCCESS;
} else { return log_error_errno(r, "Could not get link config: %m");
log_error_errno(r, "Could not get link config: %m");
return EXIT_FAILURE;
}
} }
r = link_config_apply(ctx, link, dev->device, &name); r = link_config_apply(ctx, link, dev, &name);
if (r < 0) if (r < 0) {
log_warning_errno(r, "Could not apply link config to %s, ignoring: %m", udev_device_get_sysname(dev)); const char *sysname = NULL;
(void) sd_device_get_sysname(dev, &sysname);
log_warning_errno(r, "Could not apply link config to %s, ignoring: %m", strnull(sysname));
}
udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename); udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
if (name) if (name)
udev_builtin_add_property(dev, test, "ID_NET_NAME", name); udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
return EXIT_SUCCESS; return 0;
} }
static int builtin_net_setup_link_init(void) { static int builtin_net_setup_link_init(void) {

View File

@ -56,57 +56,68 @@ static void path_prepend(char **path, const char *fmt, ...) {
** Linux only supports 32 bit luns. ** Linux only supports 32 bit luns.
** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details. ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
*/ */
static void format_lun_number(struct udev_device *dev, char **path) { static int format_lun_number(sd_device *dev, char **path) {
unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10); const char *sysnum;
unsigned long lun;
int r;
r = sd_device_get_sysnum(dev, &sysnum);
if (r < 0)
return r;
if (!sysnum)
return -ENOENT;
lun = strtoul(sysnum, NULL, 10);
if (lun < 256) if (lun < 256)
/* address method 0, peripheral device addressing with bus id of zero */ /* address method 0, peripheral device addressing with bus id of zero */
path_prepend(path, "lun-%lu", lun); path_prepend(path, "lun-%lu", lun);
else else
/* handle all other lun addressing methods by using a variant of the original lun format */ /* handle all other lun addressing methods by using a variant of the original lun format */
path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff); path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
return 0;
} }
static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys) { static sd_device *skip_subsystem(sd_device *dev, const char *subsys) {
struct udev_device *parent = dev; sd_device *parent;
assert(dev); assert(dev);
assert(subsys); assert(subsys);
while (parent) { for (parent = dev; ; ) {
const char *subsystem; const char *subsystem;
subsystem = udev_device_get_subsystem(parent); if (sd_device_get_subsystem(parent, &subsystem) < 0)
if (!streq_ptr(subsystem, subsys)) break;
if (!streq(subsystem, subsys))
break; break;
dev = parent; dev = parent;
parent = udev_device_get_parent(parent); if (sd_device_get_parent(dev, &parent) < 0)
break;
} }
return dev; return dev;
} }
static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path) { static sd_device *handle_scsi_fibre_channel(sd_device *parent, char **path) {
struct udev_device *targetdev; sd_device *targetdev;
_cleanup_(udev_device_unrefp) struct udev_device *fcdev = NULL; _cleanup_(sd_device_unrefp) sd_device *fcdev = NULL;
const char *port; const char *port, *sysname;
_cleanup_free_ char *lun = NULL; _cleanup_free_ char *lun = NULL;
assert(parent); assert(parent);
assert(path); assert(path);
targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target"); if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
if (!targetdev)
return NULL; return NULL;
if (sd_device_get_sysname(targetdev, &sysname) < 0)
fcdev = udev_device_new_from_subsystem_sysname(NULL, "fc_transport", udev_device_get_sysname(targetdev));
if (!fcdev)
return NULL; return NULL;
if (sd_device_new_from_subsystem_sysname(&fcdev, "fc_transport", sysname) < 0)
port = udev_device_get_sysattr_value(fcdev, "port_name"); return NULL;
if (!port) if (sd_device_get_sysattr_value(fcdev, "port_name", &port) < 0)
return NULL; return NULL;
format_lun_number(parent, &lun); format_lun_number(parent, &lun);
@ -114,31 +125,24 @@ static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent,
return parent; return parent;
} }
static struct udev_device *handle_scsi_sas_wide_port(struct udev_device *parent, char **path) { static sd_device *handle_scsi_sas_wide_port(sd_device *parent, char **path) {
struct udev_device *targetdev, *target_parent; sd_device *targetdev, *target_parent;
_cleanup_(udev_device_unrefp) struct udev_device *sasdev = NULL; _cleanup_(sd_device_unrefp) sd_device *sasdev = NULL;
const char *sas_address; const char *sas_address, *sysname;
_cleanup_free_ char *lun = NULL; _cleanup_free_ char *lun = NULL;
assert(parent); assert(parent);
assert(path); assert(path);
if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
if (!targetdev)
return NULL; return NULL;
if (sd_device_get_parent(targetdev, &target_parent) < 0)
target_parent = udev_device_get_parent(targetdev);
if (!target_parent)
return NULL; return NULL;
if (sd_device_get_sysname(target_parent, &sysname) < 0)
sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_device",
udev_device_get_sysname(target_parent));
if (!sasdev)
return NULL; return NULL;
if (sd_device_new_from_subsystem_sysname(&sasdev, "sas_device", sysname) < 0)
sas_address = udev_device_get_sysattr_value(sasdev, "sas_address"); return NULL;
if (!sas_address) if (sd_device_get_sysattr_value(sasdev, "sas_address", &sas_address) < 0)
return NULL; return NULL;
format_lun_number(parent, &lun); format_lun_number(parent, &lun);
@ -146,43 +150,35 @@ static struct udev_device *handle_scsi_sas_wide_port(struct udev_device *parent,
return parent; return parent;
} }
static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path) static sd_device *handle_scsi_sas(sd_device *parent, char **path) {
{ sd_device *targetdev, *target_parent, *port, *expander;
struct udev_device *targetdev, *target_parent, *port, *expander; _cleanup_(sd_device_unrefp) sd_device *target_sasdev = NULL, *expander_sasdev = NULL, *port_sasdev = NULL;
_cleanup_(udev_device_unrefp) struct udev_device
*target_sasdev = NULL, *expander_sasdev = NULL, *port_sasdev = NULL;
const char *sas_address = NULL; const char *sas_address = NULL;
const char *phy_id; const char *phy_id;
const char *phy_count; const char *phy_count, *sysname;
_cleanup_free_ char *lun = NULL; _cleanup_free_ char *lun = NULL;
assert(parent); assert(parent);
assert(path); assert(path);
if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
if (!targetdev)
return NULL; return NULL;
if (sd_device_get_parent(targetdev, &target_parent) < 0)
target_parent = udev_device_get_parent(targetdev); return NULL;
if (!target_parent) if (sd_device_get_sysname(target_parent, &sysname) < 0)
return NULL; return NULL;
/* Get sas device */ /* Get sas device */
target_sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_device", udev_device_get_sysname(target_parent)); if (sd_device_new_from_subsystem_sysname(&target_sasdev, "sas_device", sysname) < 0)
if (!target_sasdev)
return NULL; return NULL;
/* The next parent is sas port */ /* The next parent is sas port */
port = udev_device_get_parent(target_parent); if (sd_device_get_parent(target_parent, &port) < 0)
if (!port) return NULL;
if (sd_device_get_sysname(port, &sysname) < 0)
return NULL; return NULL;
/* Get port device */ /* Get port device */
port_sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_port", udev_device_get_sysname(port)); if (sd_device_new_from_subsystem_sysname(&port_sasdev, "sas_port", sysname) < 0)
return NULL;
phy_count = udev_device_get_sysattr_value(port_sasdev, "num_phys"); if (sd_device_get_sysattr_value(port_sasdev, "num_phys", &phy_count) < 0)
if (!phy_count)
return NULL; return NULL;
/* Check if we are simple disk */ /* Check if we are simple disk */
@ -190,22 +186,20 @@ static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **pa
return handle_scsi_sas_wide_port(parent, path); return handle_scsi_sas_wide_port(parent, path);
/* Get connected phy */ /* Get connected phy */
phy_id = udev_device_get_sysattr_value(target_sasdev, "phy_identifier"); if (sd_device_get_sysattr_value(target_sasdev, "phy_identifier", &phy_id) < 0)
if (!phy_id)
return NULL; return NULL;
/* The port's parent is either hba or expander */ /* The port's parent is either hba or expander */
expander = udev_device_get_parent(port); if (sd_device_get_parent(port, &expander) < 0)
if (!expander)
return NULL; return NULL;
if (sd_device_get_sysname(expander, &sysname) < 0)
return NULL;
/* Get expander device */ /* Get expander device */
expander_sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_device", udev_device_get_sysname(expander)); if (sd_device_new_from_subsystem_sysname(&expander_sasdev, "sas_device", sysname) >= 0) {
if (expander_sasdev) { /* Get expander's address */
/* Get expander's address */ if (sd_device_get_sysattr_value(expander_sasdev, "sas_address", &sas_address) < 0)
sas_address = udev_device_get_sysattr_value(expander_sasdev, "sas_address"); return NULL;
if (!sas_address)
return NULL;
} }
format_lun_number(parent, &lun); format_lun_number(parent, &lun);
@ -217,44 +211,43 @@ static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **pa
return parent; return parent;
} }
static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) { static sd_device *handle_scsi_iscsi(sd_device *parent, char **path) {
struct udev_device *transportdev; sd_device *transportdev;
_cleanup_(udev_device_unrefp) struct udev_device _cleanup_(sd_device_unrefp) sd_device *sessiondev = NULL, *conndev = NULL;
*sessiondev = NULL, *conndev = NULL;
const char *target, *connname, *addr, *port; const char *target, *connname, *addr, *port;
_cleanup_free_ char *lun = NULL; _cleanup_free_ char *lun = NULL;
const char *sysname, *sysnum;
assert(parent); assert(parent);
assert(path); assert(path);
/* find iscsi session */ /* find iscsi session */
transportdev = parent; for (transportdev = parent; ; ) {
for (;;) {
transportdev = udev_device_get_parent(transportdev); if (sd_device_get_parent(transportdev, &transportdev) < 0)
if (!transportdev)
return NULL; return NULL;
if (startswith(udev_device_get_sysname(transportdev), "session")) if (sd_device_get_sysname(transportdev, &sysname) < 0)
return NULL;
if (startswith(sysname, "session"))
break; break;
} }
/* find iscsi session device */ /* find iscsi session device */
sessiondev = udev_device_new_from_subsystem_sysname(NULL, "iscsi_session", udev_device_get_sysname(transportdev)); if (sd_device_new_from_subsystem_sysname(&sessiondev, "iscsi_session", sysname) < 0)
if (!sessiondev)
return NULL; return NULL;
target = udev_device_get_sysattr_value(sessiondev, "targetname"); if (sd_device_get_sysattr_value(sessiondev, "targetname", &target) < 0)
if (!target)
return NULL; return NULL;
connname = strjoina("connection", udev_device_get_sysnum(transportdev), ":0"); if (sd_device_get_sysnum(transportdev, &sysnum) < 0 || !sysnum)
conndev = udev_device_new_from_subsystem_sysname(NULL, "iscsi_connection", connname); return NULL;
if (!conndev) connname = strjoina("connection", sysnum, ":0");
if (sd_device_new_from_subsystem_sysname(&conndev, "iscsi_connection", connname) < 0)
return NULL; return NULL;
addr = udev_device_get_sysattr_value(conndev, "persistent_address"); if (sd_device_get_sysattr_value(conndev, "persistent_address", &addr) < 0)
port = udev_device_get_sysattr_value(conndev, "persistent_port"); return NULL;
if (!addr || !port) if (sd_device_get_sysattr_value(conndev, "persistent_port", &port) < 0)
return NULL; return NULL;
format_lun_number(parent, &lun); format_lun_number(parent, &lun);
@ -262,37 +255,34 @@ static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **
return parent; return parent;
} }
static struct udev_device *handle_scsi_ata(struct udev_device *parent, char **path) { static sd_device *handle_scsi_ata(sd_device *parent, char **path) {
struct udev_device *targetdev, *target_parent; sd_device *targetdev, *target_parent;
_cleanup_(udev_device_unrefp) struct udev_device *atadev = NULL; _cleanup_(sd_device_unrefp) sd_device *atadev = NULL;
const char *port_no; const char *port_no, *sysname;
assert(parent); assert(parent);
assert(path); assert(path);
if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &targetdev) < 0)
targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
if (!targetdev)
return NULL; return NULL;
target_parent = udev_device_get_parent(targetdev); if (sd_device_get_parent(targetdev, &target_parent) < 0)
if (!target_parent)
return NULL; return NULL;
atadev = udev_device_new_from_subsystem_sysname(NULL, "ata_port", udev_device_get_sysname(target_parent)); if (sd_device_get_sysname(target_parent, &sysname) < 0)
if (!atadev) return NULL;
if (sd_device_new_from_subsystem_sysname(&atadev, "ata_port", sysname) < 0)
return NULL; return NULL;
port_no = udev_device_get_sysattr_value(atadev, "port_no"); if (sd_device_get_sysattr_value(atadev, "port_no", &port_no) < 0)
if (!port_no)
return NULL; return NULL;
path_prepend(path, "ata-%s", port_no); path_prepend(path, "ata-%s", port_no);
return parent; return parent;
} }
static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path) { static sd_device *handle_scsi_default(sd_device *parent, char **path) {
struct udev_device *hostdev; sd_device *hostdev;
int host, bus, target, lun; int host, bus, target, lun;
const char *name, *base, *pos; const char *name, *base, *pos;
_cleanup_closedir_ DIR *dir = NULL; _cleanup_closedir_ DIR *dir = NULL;
@ -302,11 +292,11 @@ static struct udev_device *handle_scsi_default(struct udev_device *parent, char
assert(parent); assert(parent);
assert(path); assert(path);
hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host"); if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &hostdev) < 0)
if (!hostdev)
return NULL; return NULL;
name = udev_device_get_sysname(parent); if (sd_device_get_sysname(parent, &name) < 0)
return NULL;
if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
return NULL; return NULL;
@ -328,7 +318,8 @@ static struct udev_device *handle_scsi_default(struct udev_device *parent, char
* get into the way of this "I hope it works" logic. * get into the way of this "I hope it works" logic.
*/ */
base = udev_device_get_syspath(hostdev); if (sd_device_get_syspath(hostdev, &base) < 0)
return NULL;
pos = strrchr(base, '/'); pos = strrchr(base, '/');
if (!pos) if (!pos)
return NULL; return NULL;
@ -367,9 +358,9 @@ static struct udev_device *handle_scsi_default(struct udev_device *parent, char
return hostdev; return hostdev;
} }
static struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path, size_t guid_str_len) { static sd_device *handle_scsi_hyperv(sd_device *parent, char **path, size_t guid_str_len) {
struct udev_device *hostdev; sd_device *hostdev;
struct udev_device *vmbusdev; sd_device *vmbusdev;
const char *guid_str; const char *guid_str;
_cleanup_free_ char *lun = NULL; _cleanup_free_ char *lun = NULL;
char guid[39]; char guid[39];
@ -379,16 +370,13 @@ static struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char *
assert(path); assert(path);
assert(guid_str_len < sizeof(guid)); assert(guid_str_len < sizeof(guid));
hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host"); if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &hostdev) < 0)
if (!hostdev)
return NULL; return NULL;
vmbusdev = udev_device_get_parent(hostdev); if (sd_device_get_parent(hostdev, &vmbusdev) < 0)
if (!vmbusdev)
return NULL; return NULL;
guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id"); if (sd_device_get_sysattr_value(vmbusdev, "device_id", &guid_str) < 0)
if (!guid_str)
return NULL; return NULL;
if (strlen(guid_str) < guid_str_len || guid_str[0] != '{' || guid_str[guid_str_len-1] != '}') if (strlen(guid_str) < guid_str_len || guid_str[0] != '{' || guid_str[guid_str_len-1] != '}')
@ -406,23 +394,23 @@ static struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char *
return parent; return parent;
} }
static struct udev_device *handle_scsi(struct udev_device *parent, char **path, bool *supported_parent) { static sd_device *handle_scsi(sd_device *parent, char **path, bool *supported_parent) {
const char *devtype, *id, *name; const char *devtype, *id, *name;
devtype = udev_device_get_devtype(parent); if (sd_device_get_devtype(parent, &devtype) < 0 ||
if (!streq_ptr(devtype, "scsi_device")) !streq(devtype, "scsi_device"))
return parent; return parent;
/* firewire */ /* firewire */
id = udev_device_get_sysattr_value(parent, "ieee1394_id"); if (sd_device_get_sysattr_value(parent, "ieee1394_id", &id) >= 0) {
if (id) {
path_prepend(path, "ieee1394-0x%s", id); path_prepend(path, "ieee1394-0x%s", id);
*supported_parent = true; *supported_parent = true;
return skip_subsystem(parent, "scsi"); return skip_subsystem(parent, "scsi");
} }
/* scsi sysfs does not have a "subsystem" for the transport */ /* scsi sysfs does not have a "subsystem" for the transport */
name = udev_device_get_syspath(parent); if (sd_device_get_syspath(parent, &name) < 0)
return NULL;
if (strstr(name, "/rport-")) { if (strstr(name, "/rport-")) {
*supported_parent = true; *supported_parent = true;
@ -450,11 +438,12 @@ static struct udev_device *handle_scsi(struct udev_device *parent, char **path,
return handle_scsi_default(parent, path); return handle_scsi_default(parent, path);
} }
static struct udev_device *handle_cciss(struct udev_device *parent, char **path) { static sd_device *handle_cciss(sd_device *parent, char **path) {
const char *str; const char *str;
unsigned controller, disk; unsigned controller, disk;
str = udev_device_get_sysname(parent); if (sd_device_get_sysname(parent, &str) < 0)
return NULL;
if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2) if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
return NULL; return NULL;
@ -462,30 +451,32 @@ static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
return skip_subsystem(parent, "cciss"); return skip_subsystem(parent, "cciss");
} }
static void handle_scsi_tape(struct udev_device *dev, char **path) { static void handle_scsi_tape(sd_device *dev, char **path) {
const char *name; const char *name;
/* must be the last device in the syspath */ /* must be the last device in the syspath */
if (*path) if (*path)
return; return;
name = udev_device_get_sysname(dev); if (sd_device_get_sysname(dev, &name) < 0)
return;
if (startswith(name, "nst") && strchr("lma", name[3])) if (startswith(name, "nst") && strchr("lma", name[3]))
path_prepend(path, "nst%c", name[3]); path_prepend(path, "nst%c", name[3]);
else if (startswith(name, "st") && strchr("lma", name[2])) else if (startswith(name, "st") && strchr("lma", name[2]))
path_prepend(path, "st%c", name[2]); path_prepend(path, "st%c", name[2]);
} }
static struct udev_device *handle_usb(struct udev_device *parent, char **path) { static sd_device *handle_usb(sd_device *parent, char **path) {
const char *devtype, *str, *port; const char *devtype, *str, *port;
devtype = udev_device_get_devtype(parent); if (sd_device_get_devtype(parent, &devtype) < 0)
if (!devtype)
return parent; return parent;
if (!STR_IN_SET(devtype, "usb_interface", "usb_device")) if (!STR_IN_SET(devtype, "usb_interface", "usb_device"))
return parent; return parent;
str = udev_device_get_sysname(parent); if (sd_device_get_sysname(parent, &str) < 0)
return parent;
port = strchr(str, '-'); port = strchr(str, '-');
if (!port) if (!port)
return parent; return parent;
@ -495,11 +486,12 @@ static struct udev_device *handle_usb(struct udev_device *parent, char **path) {
return skip_subsystem(parent, "usb"); return skip_subsystem(parent, "usb");
} }
static struct udev_device *handle_bcma(struct udev_device *parent, char **path) { static sd_device *handle_bcma(sd_device *parent, char **path) {
const char *sysname; const char *sysname;
unsigned core; unsigned core;
sysname = udev_device_get_sysname(parent); if (sd_device_get_sysname(parent, &sysname) < 0)
return NULL;
if (sscanf(sysname, "bcma%*u:%u", &core) != 1) if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
return NULL; return NULL;
@ -508,38 +500,41 @@ static struct udev_device *handle_bcma(struct udev_device *parent, char **path)
} }
/* Handle devices of AP bus in System z platform. */ /* Handle devices of AP bus in System z platform. */
static struct udev_device *handle_ap(struct udev_device *parent, char **path) { static sd_device *handle_ap(sd_device *parent, char **path) {
const char *type, *func; const char *type, *func;
assert(parent); assert(parent);
assert(path); assert(path);
type = udev_device_get_sysattr_value(parent, "type"); if (sd_device_get_sysattr_value(parent, "type", &type) >= 0 &&
func = udev_device_get_sysattr_value(parent, "ap_functions"); sd_device_get_sysattr_value(parent, "ap_functions", &func) >= 0)
if (type && func)
path_prepend(path, "ap-%s-%s", type, func); path_prepend(path, "ap-%s-%s", type, func);
else else {
path_prepend(path, "ap-%s", udev_device_get_sysname(parent)); const char *sysname;
if (sd_device_get_sysname(parent, &sysname) >= 0)
path_prepend(path, "ap-%s", sysname);
}
return skip_subsystem(parent, "ap"); return skip_subsystem(parent, "ap");
} }
static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_path_id(sd_device *dev, int argc, char *argv[], bool test) {
struct udev_device *parent; sd_device *parent;
_cleanup_free_ char *path = NULL; _cleanup_free_ char *path = NULL;
bool supported_transport = false; bool supported_transport = false;
bool supported_parent = false; bool supported_parent = false;
const char *subsystem;
assert(dev); assert(dev);
/* walk up the chain of devices and compose path */ /* walk up the chain of devices and compose path */
parent = dev; parent = dev;
while (parent) { while (parent) {
const char *subsys; const char *subsys, *sysname;
subsys = udev_device_get_subsystem(parent); if (sd_device_get_subsystem(parent, &subsys) < 0 ||
if (!subsys) { sd_device_get_sysname(parent, &sysname) < 0) {
; ;
} else if (streq(subsys, "scsi_tape")) { } else if (streq(subsys, "scsi_tape")) {
handle_scsi_tape(parent, &path); handle_scsi_tape(parent, &path);
@ -556,40 +551,44 @@ static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool
parent = handle_bcma(parent, &path); parent = handle_bcma(parent, &path);
supported_transport = true; supported_transport = true;
} else if (streq(subsys, "serio")) { } else if (streq(subsys, "serio")) {
path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent)); const char *sysnum;
parent = skip_subsystem(parent, "serio");
if (sd_device_get_sysnum(parent, &sysnum) >= 0 && sysnum) {
path_prepend(&path, "serio-%s", sysnum);
parent = skip_subsystem(parent, "serio");
}
} else if (streq(subsys, "pci")) { } else if (streq(subsys, "pci")) {
path_prepend(&path, "pci-%s", udev_device_get_sysname(parent)); path_prepend(&path, "pci-%s", sysname);
parent = skip_subsystem(parent, "pci"); parent = skip_subsystem(parent, "pci");
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "platform")) { } else if (streq(subsys, "platform")) {
path_prepend(&path, "platform-%s", udev_device_get_sysname(parent)); path_prepend(&path, "platform-%s", sysname);
parent = skip_subsystem(parent, "platform"); parent = skip_subsystem(parent, "platform");
supported_transport = true; supported_transport = true;
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "acpi")) { } else if (streq(subsys, "acpi")) {
path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent)); path_prepend(&path, "acpi-%s", sysname);
parent = skip_subsystem(parent, "acpi"); parent = skip_subsystem(parent, "acpi");
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "xen")) { } else if (streq(subsys, "xen")) {
path_prepend(&path, "xen-%s", udev_device_get_sysname(parent)); path_prepend(&path, "xen-%s", sysname);
parent = skip_subsystem(parent, "xen"); parent = skip_subsystem(parent, "xen");
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "virtio")) { } else if (streq(subsys, "virtio")) {
parent = skip_subsystem(parent, "virtio"); parent = skip_subsystem(parent, "virtio");
supported_transport = true; supported_transport = true;
} else if (streq(subsys, "scm")) { } else if (streq(subsys, "scm")) {
path_prepend(&path, "scm-%s", udev_device_get_sysname(parent)); path_prepend(&path, "scm-%s", sysname);
parent = skip_subsystem(parent, "scm"); parent = skip_subsystem(parent, "scm");
supported_transport = true; supported_transport = true;
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "ccw")) { } else if (streq(subsys, "ccw")) {
path_prepend(&path, "ccw-%s", udev_device_get_sysname(parent)); path_prepend(&path, "ccw-%s", sysname);
parent = skip_subsystem(parent, "ccw"); parent = skip_subsystem(parent, "ccw");
supported_transport = true; supported_transport = true;
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "ccwgroup")) { } else if (streq(subsys, "ccwgroup")) {
path_prepend(&path, "ccwgroup-%s", udev_device_get_sysname(parent)); path_prepend(&path, "ccwgroup-%s", sysname);
parent = skip_subsystem(parent, "ccwgroup"); parent = skip_subsystem(parent, "ccwgroup");
supported_transport = true; supported_transport = true;
supported_parent = true; supported_parent = true;
@ -598,14 +597,14 @@ static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool
supported_transport = true; supported_transport = true;
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "iucv")) { } else if (streq(subsys, "iucv")) {
path_prepend(&path, "iucv-%s", udev_device_get_sysname(parent)); path_prepend(&path, "iucv-%s", sysname);
parent = skip_subsystem(parent, "iucv"); parent = skip_subsystem(parent, "iucv");
supported_transport = true; supported_transport = true;
supported_parent = true; supported_parent = true;
} else if (streq(subsys, "nvme")) { } else if (streq(subsys, "nvme")) {
const char *nsid = udev_device_get_sysattr_value(dev, "nsid"); const char *nsid;
if (nsid) { if (sd_device_get_sysattr_value(dev, "nsid", &nsid) >= 0) {
path_prepend(&path, "nvme-%s", nsid); path_prepend(&path, "nvme-%s", nsid);
parent = skip_subsystem(parent, "nvme"); parent = skip_subsystem(parent, "nvme");
supported_parent = true; supported_parent = true;
@ -613,12 +612,14 @@ static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool
} }
} }
if (parent) if (!parent)
parent = udev_device_get_parent(parent); break;
if (sd_device_get_parent(parent, &parent) < 0)
break;
} }
if (!path) if (!path)
return EXIT_FAILURE; return -ENOENT;
/* /*
* Do not return devices with an unknown parent device type. They * Do not return devices with an unknown parent device type. They
@ -626,15 +627,17 @@ static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool
* unique and predictable name. * unique and predictable name.
*/ */
if (!supported_parent) if (!supported_parent)
return EXIT_FAILURE; return -ENOENT;
/* /*
* Do not return block devices without a well-known transport. Some * Do not return block devices without a well-known transport. Some
* devices do not expose their buses and do not provide a unique * devices do not expose their buses and do not provide a unique
* and predictable name that way. * and predictable name that way.
*/ */
if (streq_ptr(udev_device_get_subsystem(dev), "block") && !supported_transport) if (sd_device_get_subsystem(dev, &subsystem) >= 0 &&
return EXIT_FAILURE; streq(subsystem, "block") &&
!supported_transport)
return -ENOENT;
{ {
char tag[UTIL_NAME_SIZE]; char tag[UTIL_NAME_SIZE];
@ -670,7 +673,7 @@ static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool
udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag); udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
} }
return EXIT_SUCCESS; return 0;
} }
const struct udev_builtin udev_builtin_path_id = { const struct udev_builtin udev_builtin_path_id = {

View File

@ -15,7 +15,7 @@
#include "log.h" #include "log.h"
#include "udev-builtin.h" #include "udev-builtin.h"
static int builtin_uaccess(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_uaccess(sd_device *dev, int argc, char *argv[], bool test) {
int r; int r;
const char *path = NULL, *seat; const char *path = NULL, *seat;
bool changed_acl = false; bool changed_acl = false;
@ -27,18 +27,21 @@ static int builtin_uaccess(struct udev_device *dev, int argc, char *argv[], bool
if (!logind_running()) if (!logind_running())
return 0; return 0;
path = udev_device_get_devnode(dev); r = sd_device_get_devname(dev, &path);
seat = udev_device_get_property_value(dev, "ID_SEAT"); if (r < 0)
if (!seat) goto finish;
if (sd_device_get_property_value(dev, "ID_SEAT", &seat) < 0)
seat = "seat0"; seat = "seat0";
r = sd_seat_get_active(seat, NULL, &uid); r = sd_seat_get_active(seat, NULL, &uid);
if (IN_SET(r, -ENXIO, -ENODATA)) { if (r < 0) {
/* No active session on this seat */ if (IN_SET(r, -ENXIO, -ENODATA))
r = 0; /* No active session on this seat */
goto finish; r = 0;
} else if (r < 0) { else
log_error("Failed to determine active user on seat %s.", seat); log_error_errno(r, "Failed to determine active user on seat %s: %m", seat);
goto finish; goto finish;
} }
@ -64,7 +67,7 @@ finish:
} }
} }
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; return r;
} }
const struct udev_builtin udev_builtin_uaccess = { const struct udev_builtin udev_builtin_uaccess = {

View File

@ -135,13 +135,15 @@ static void set_scsi_type(char *to, const char *from, size_t len) {
#define USB_DT_DEVICE 0x01 #define USB_DT_DEVICE 0x01
#define USB_DT_INTERFACE 0x04 #define USB_DT_INTERFACE 0x04
static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len) { static int dev_if_packed_info(sd_device *dev, char *ifs_str, size_t len) {
_cleanup_free_ char *filename = NULL; _cleanup_free_ char *filename = NULL;
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
ssize_t size; ssize_t size;
unsigned char buf[18 + 65535]; unsigned char buf[18 + 65535];
size_t pos = 0; size_t pos = 0;
unsigned strpos = 0; unsigned strpos = 0;
const char *syspath;
int r;
struct usb_interface_descriptor { struct usb_interface_descriptor {
uint8_t bLength; uint8_t bLength;
uint8_t bDescriptorType; uint8_t bDescriptorType;
@ -154,7 +156,10 @@ static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len
uint8_t iInterface; uint8_t iInterface;
} _packed_; } _packed_;
if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) r = sd_device_get_syspath(dev, &syspath);
if (r < 0)
return r;
if (asprintf(&filename, "%s/descriptors", syspath) < 0)
return log_oom(); return log_oom();
fd = open(filename, O_RDONLY|O_CLOEXEC); fd = open(filename, O_RDONLY|O_CLOEXEC);
@ -218,7 +223,7 @@ static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len
* 6.) If the device supplies a serial number, this number * 6.) If the device supplies a serial number, this number
* is concatenated with the identification with an underscore '_'. * is concatenated with the identification with an underscore '_'.
*/ */
static int builtin_usb_id(struct udev_device *dev, int argc, char *argv[], bool test) { static int builtin_usb_id(sd_device *dev, int argc, char *argv[], bool test) {
char vendor_str[64] = ""; char vendor_str[64] = "";
char vendor_str_enc[256]; char vendor_str_enc[256];
const char *vendor_id; const char *vendor_id;
@ -234,116 +239,114 @@ static int builtin_usb_id(struct udev_device *dev, int argc, char *argv[], bool
const char *driver = NULL; const char *driver = NULL;
char serial[256]; char serial[256];
struct udev_device *dev_interface = NULL; sd_device *dev_interface, *dev_usb;
struct udev_device *dev_usb = NULL;
const char *if_class, *if_subclass; const char *if_class, *if_subclass;
int if_class_num; int if_class_num;
int protocol = 0; int protocol = 0;
size_t l; size_t l;
char *s; char *s;
const char *syspath, *sysname, *devtype, *interface_syspath;
int r;
assert(dev); assert(dev);
r = sd_device_get_syspath(dev, &syspath);
if (r < 0)
return r;
r = sd_device_get_sysname(dev, &sysname);
if (r < 0)
return r;
/* shortcut, if we are called directly for a "usb_device" type */ /* shortcut, if we are called directly for a "usb_device" type */
if (udev_device_get_devtype(dev) != NULL && streq(udev_device_get_devtype(dev), "usb_device")) { if (sd_device_get_devtype(dev, &devtype) >= 0 && streq(devtype, "usb_device")) {
dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str)); dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
dev_usb = dev; dev_usb = dev;
goto fallback; goto fallback;
} }
/* usb interface directory */ /* usb interface directory */
dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface"); r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &dev_interface);
if (dev_interface == NULL) { if (r < 0)
log_debug("unable to access usb_interface device of '%s'", return log_debug_errno(r, "Failed to access usb_interface device of '%s': %m", syspath);
udev_device_get_syspath(dev));
return EXIT_FAILURE;
}
ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber"); r = sd_device_get_syspath(dev_interface, &interface_syspath);
driver = udev_device_get_sysattr_value(dev_interface, "driver"); if (r < 0)
return r;
(void) sd_device_get_sysattr_value(dev_interface, "bInterfaceNumber", &ifnum);
(void) sd_device_get_sysattr_value(dev_interface, "driver", &driver);
if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass"); r = sd_device_get_sysattr_value(dev_interface, "bInterfaceClass", &if_class);
if (!if_class) { if (r < 0)
log_debug("%s: cannot get bInterfaceClass attribute", return log_debug_errno(r, "Failed to get bInterfaceClass attribute of '%s': %m", sysname);
udev_device_get_sysname(dev));
return EXIT_FAILURE;
}
if_class_num = strtoul(if_class, NULL, 16); if_class_num = strtoul(if_class, NULL, 16);
if (if_class_num == 8) { if (if_class_num == 8) {
/* mass storage */ /* mass storage */
if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass"); if (sd_device_get_sysattr_value(dev_interface, "bInterfaceSubClass", &if_subclass) >= 0)
if (if_subclass != NULL)
protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1); protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
} else { } else
set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1); set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
}
log_debug("%s: if_class %d protocol %d", log_debug("%s: if_class %d protocol %d", interface_syspath, if_class_num, protocol);
udev_device_get_syspath(dev_interface), if_class_num, protocol);
/* usb device directory */ /* usb device directory */
dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device"); r = sd_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device", &dev_usb);
if (!dev_usb) { if (r < 0)
log_debug("unable to find parent 'usb' device of '%s'", return log_debug_errno(r, "Failed to find parent 'usb' device of '%s'", syspath);
udev_device_get_syspath(dev));
return EXIT_FAILURE;
}
/* all interfaces of the device in a single string */ /* all interfaces of the device in a single string */
dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str)); dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
/* mass storage : SCSI or ATAPI */ /* mass storage : SCSI or ATAPI */
if (IN_SET(protocol, 6, 2)) { if (IN_SET(protocol, 6, 2)) {
struct udev_device *dev_scsi; sd_device *dev_scsi;
const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev; const char *scsi_sysname, *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
int host, bus, target, lun; int host, bus, target, lun;
/* get scsi device */ /* get scsi device */
dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device"); r = sd_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device", &dev_scsi);
if (dev_scsi == NULL) { if (r < 0) {
log_debug("unable to find parent 'scsi' device of '%s'", log_debug_errno(r, "Unable to find parent 'scsi' device of '%s'", syspath);
udev_device_get_syspath(dev));
goto fallback; goto fallback;
} }
if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) { if (sd_device_get_sysname(dev_scsi, &scsi_sysname) < 0)
log_debug("invalid scsi device '%s'", udev_device_get_sysname(dev_scsi)); goto fallback;
if (sscanf(scsi_sysname, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
log_debug("invalid scsi device '%s'", scsi_sysname);
goto fallback; goto fallback;
} }
/* Generic SPC-2 device */ /* Generic SPC-2 device */
scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor"); r = sd_device_get_sysattr_value(dev_scsi, "vendor", &scsi_vendor);
if (!scsi_vendor) { if (r < 0) {
log_debug("%s: cannot get SCSI vendor attribute", log_debug_errno(r, "%s: cannot get SCSI vendor attribute: %m", scsi_sysname);
udev_device_get_sysname(dev_scsi));
goto fallback; goto fallback;
} }
udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc)); udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1); util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
util_replace_chars(vendor_str, NULL); util_replace_chars(vendor_str, NULL);
scsi_model = udev_device_get_sysattr_value(dev_scsi, "model"); r = sd_device_get_sysattr_value(dev_scsi, "model", &scsi_model);
if (!scsi_model) { if (r < 0) {
log_debug("%s: cannot get SCSI model attribute", log_debug_errno(r, "%s: cannot get SCSI model attribute: %m", scsi_sysname);
udev_device_get_sysname(dev_scsi));
goto fallback; goto fallback;
} }
udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc)); udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1); util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
util_replace_chars(model_str, NULL); util_replace_chars(model_str, NULL);
scsi_type = udev_device_get_sysattr_value(dev_scsi, "type"); r = sd_device_get_sysattr_value(dev_scsi, "type", &scsi_type);
if (!scsi_type) { if (r < 0) {
log_debug("%s: cannot get SCSI type attribute", log_debug_errno(r, "%s: cannot get SCSI type attribute", scsi_sysname);
udev_device_get_sysname(dev_scsi));
goto fallback; goto fallback;
} }
set_scsi_type(type_str, scsi_type, sizeof(type_str)-1); set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev"); r = sd_device_get_sysattr_value(dev_scsi, "rev", &scsi_rev);
if (!scsi_rev) { if (r < 0) {
log_debug("%s: cannot get SCSI revision attribute", log_debug_errno(r, "%s: cannot get SCSI revision attribute: %m", scsi_sysname);
udev_device_get_sysname(dev_scsi));
goto fallback; goto fallback;
} }
util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1); util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
@ -357,33 +360,30 @@ static int builtin_usb_id(struct udev_device *dev, int argc, char *argv[], bool
} }
fallback: fallback:
vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor"); r = sd_device_get_sysattr_value(dev_usb, "idVendor", &vendor_id);
product_id = udev_device_get_sysattr_value(dev_usb, "idProduct"); if (r < 0)
return r;
r = sd_device_get_sysattr_value(dev_usb, "idProduct", &product_id);
if (r < 0)
return r;
/* fallback to USB vendor & device */ /* fallback to USB vendor & device */
if (vendor_str[0] == '\0') { if (vendor_str[0] == '\0') {
const char *usb_vendor = NULL; const char *usb_vendor;
usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer"); if (sd_device_get_sysattr_value(dev_usb, "manufacturer", &usb_vendor) < 0)
if (!usb_vendor)
usb_vendor = vendor_id; usb_vendor = vendor_id;
if (!usb_vendor) {
log_debug("No USB vendor information available");
return EXIT_FAILURE;
}
udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc)); udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1); util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
util_replace_chars(vendor_str, NULL); util_replace_chars(vendor_str, NULL);
} }
if (model_str[0] == '\0') { if (model_str[0] == '\0') {
const char *usb_model = NULL; const char *usb_model;
usb_model = udev_device_get_sysattr_value(dev_usb, "product"); if (sd_device_get_sysattr_value(dev_usb, "product", &usb_model) < 0)
if (!usb_model)
usb_model = product_id; usb_model = product_id;
if (!usb_model)
return EXIT_FAILURE;
udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc)); udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1); util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
util_replace_chars(model_str, NULL); util_replace_chars(model_str, NULL);
@ -392,8 +392,7 @@ fallback:
if (revision_str[0] == '\0') { if (revision_str[0] == '\0') {
const char *usb_rev; const char *usb_rev;
usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice"); if (sd_device_get_sysattr_value(dev_usb, "bcdDevice", &usb_rev) >= 0) {
if (usb_rev) {
util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1); util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
util_replace_chars(revision_str, NULL); util_replace_chars(revision_str, NULL);
} }
@ -402,8 +401,7 @@ fallback:
if (serial_str[0] == '\0') { if (serial_str[0] == '\0') {
const char *usb_serial; const char *usb_serial;
usb_serial = udev_device_get_sysattr_value(dev_usb, "serial"); if (sd_device_get_sysattr_value(dev_usb, "serial", &usb_serial) >= 0) {
if (usb_serial) {
const unsigned char *p; const unsigned char *p;
/* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */ /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
@ -412,11 +410,11 @@ fallback:
usb_serial = NULL; usb_serial = NULL;
break; break;
} }
}
if (usb_serial) { if (usb_serial) {
util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1); util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
util_replace_chars(serial_str, NULL); util_replace_chars(serial_str, NULL);
}
} }
} }
@ -445,11 +443,11 @@ fallback:
udev_builtin_add_property(dev, test, "ID_BUS", "usb"); udev_builtin_add_property(dev, test, "ID_BUS", "usb");
if (!isempty(packed_if_str)) if (!isempty(packed_if_str))
udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str); udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str);
if (ifnum != NULL) if (ifnum)
udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum); udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum);
if (driver != NULL) if (driver)
udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver); udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver);
return EXIT_SUCCESS; return 0;
} }
const struct udev_builtin udev_builtin_usb_id = { const struct udev_builtin udev_builtin_usb_id = {

View File

@ -4,13 +4,15 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "device-private.h"
#include "device-util.h"
#include "string-util.h" #include "string-util.h"
#include "strv.h"
#include "udev-builtin.h" #include "udev-builtin.h"
#include "udev.h"
static bool initialized; static bool initialized;
static const struct udev_builtin *builtins[] = { static const struct udev_builtin *builtins[_UDEV_BUILTIN_MAX] = {
#if HAVE_BLKID #if HAVE_BLKID
[UDEV_BUILTIN_BLKID] = &udev_builtin_blkid, [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid,
#endif #endif
@ -36,7 +38,7 @@ void udev_builtin_init(void) {
if (initialized) if (initialized)
return; return;
for (i = 0; i < ELEMENTSOF(builtins); i++) for (i = 0; i < _UDEV_BUILTIN_MAX; i++)
if (builtins[i] && builtins[i]->init) if (builtins[i] && builtins[i]->init)
builtins[i]->init(); builtins[i]->init();
@ -49,7 +51,7 @@ void udev_builtin_exit(void) {
if (!initialized) if (!initialized)
return; return;
for (i = 0; i < ELEMENTSOF(builtins); i++) for (i = 0; i < _UDEV_BUILTIN_MAX; i++)
if (builtins[i] && builtins[i]->exit) if (builtins[i] && builtins[i]->exit)
builtins[i]->exit(); builtins[i]->exit();
@ -59,7 +61,7 @@ void udev_builtin_exit(void) {
bool udev_builtin_validate(void) { bool udev_builtin_validate(void) {
unsigned i; unsigned i;
for (i = 0; i < ELEMENTSOF(builtins); i++) for (i = 0; i < _UDEV_BUILTIN_MAX; i++)
if (builtins[i] && builtins[i]->validate && builtins[i]->validate()) if (builtins[i] && builtins[i]->validate && builtins[i]->validate())
return true; return true;
return false; return false;
@ -68,12 +70,14 @@ bool udev_builtin_validate(void) {
void udev_builtin_list(void) { void udev_builtin_list(void) {
unsigned i; unsigned i;
for (i = 0; i < ELEMENTSOF(builtins); i++) for (i = 0; i < _UDEV_BUILTIN_MAX; i++)
if (builtins[i]) if (builtins[i])
fprintf(stderr, " %-14s %s\n", builtins[i]->name, builtins[i]->help); fprintf(stderr, " %-14s %s\n", builtins[i]->name, builtins[i]->help);
} }
const char *udev_builtin_name(enum udev_builtin_cmd cmd) { const char *udev_builtin_name(enum udev_builtin_cmd cmd) {
assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
if (!builtins[cmd]) if (!builtins[cmd])
return NULL; return NULL;
@ -81,6 +85,8 @@ const char *udev_builtin_name(enum udev_builtin_cmd cmd) {
} }
bool udev_builtin_run_once(enum udev_builtin_cmd cmd) { bool udev_builtin_run_once(enum udev_builtin_cmd cmd) {
assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
if (!builtins[cmd]) if (!builtins[cmd])
return false; return false;
@ -88,23 +94,27 @@ bool udev_builtin_run_once(enum udev_builtin_cmd cmd) {
} }
enum udev_builtin_cmd udev_builtin_lookup(const char *command) { enum udev_builtin_cmd udev_builtin_lookup(const char *command) {
char name[UTIL_PATH_SIZE];
enum udev_builtin_cmd i; enum udev_builtin_cmd i;
char *pos; size_t n;
strscpy(name, sizeof(name), command); assert(command);
pos = strchr(name, ' ');
if (pos) command += strspn(command, WHITESPACE);
pos[0] = '\0'; n = strcspn(command, WHITESPACE);
for (i = 0; i < ELEMENTSOF(builtins); i++) for (i = 0; i < _UDEV_BUILTIN_MAX; i++)
if (builtins[i] && streq(builtins[i]->name, name)) if (builtins[i] && strneq(builtins[i]->name, command, n))
return i; return i;
return UDEV_BUILTIN_MAX;
return _UDEV_BUILTIN_INVALID;
} }
int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test) { int udev_builtin_run(sd_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test) {
_cleanup_strv_free_ char **argv = NULL; _cleanup_strv_free_ char **argv = NULL;
assert(dev);
assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
assert(command);
if (!builtins[cmd]) if (!builtins[cmd])
return -EOPNOTSUPP; return -EOPNOTSUPP;
@ -117,10 +127,19 @@ int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const c
return builtins[cmd]->cmd(dev, strv_length(argv), argv, test); return builtins[cmd]->cmd(dev, strv_length(argv), argv, test);
} }
int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val) { int udev_builtin_add_property(sd_device *dev, bool test, const char *key, const char *val) {
udev_device_add_property(dev, key, val); int r;
assert(dev);
assert(key);
r = device_add_property(dev, key, val);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to add property '%s%s%s'",
key, val ? "=" : "", strempty(val));
if (test) if (test)
printf("%s=%s\n", key, val); printf("%s=%s\n", key, val);
return 0; return 0;
} }

View File

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "libudev.h" #include "sd-device.h"
enum udev_builtin_cmd { enum udev_builtin_cmd {
#if HAVE_BLKID #if HAVE_BLKID
@ -23,12 +23,13 @@ enum udev_builtin_cmd {
#if HAVE_ACL #if HAVE_ACL
UDEV_BUILTIN_UACCESS, UDEV_BUILTIN_UACCESS,
#endif #endif
UDEV_BUILTIN_MAX _UDEV_BUILTIN_MAX,
_UDEV_BUILTIN_INVALID = -1,
}; };
struct udev_builtin { struct udev_builtin {
const char *name; const char *name;
int (*cmd)(struct udev_device *dev, int argc, char *argv[], bool test); int (*cmd)(sd_device *dev, int argc, char *argv[], bool test);
const char *help; const char *help;
int (*init)(void); int (*init)(void);
void (*exit)(void); void (*exit)(void);
@ -59,9 +60,9 @@ void udev_builtin_exit(void);
enum udev_builtin_cmd udev_builtin_lookup(const char *command); enum udev_builtin_cmd udev_builtin_lookup(const char *command);
const char *udev_builtin_name(enum udev_builtin_cmd cmd); const char *udev_builtin_name(enum udev_builtin_cmd cmd);
bool udev_builtin_run_once(enum udev_builtin_cmd cmd); bool udev_builtin_run_once(enum udev_builtin_cmd cmd);
int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test); int udev_builtin_run(sd_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test);
void udev_builtin_list(void); void udev_builtin_list(void);
bool udev_builtin_validate(void); bool udev_builtin_validate(void);
int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val); int udev_builtin_add_property(sd_device *dev, bool test, const char *key, const char *val);
int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *prefix, const char *modalias, int udev_builtin_hwdb_lookup(sd_device *dev, const char *prefix, const char *modalias,
const char *filter, bool test); const char *filter, bool test);

View File

@ -900,8 +900,8 @@ void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec, usec_
udev_event_apply_format(event, cmd, command, sizeof(command), false); udev_event_apply_format(event, cmd, command, sizeof(command), false);
if (builtin_cmd < UDEV_BUILTIN_MAX) if (builtin_cmd >= 0 && builtin_cmd < _UDEV_BUILTIN_MAX)
udev_builtin_run(event->dev, builtin_cmd, command, false); udev_builtin_run(event->dev->device, builtin_cmd, command, false);
else { else {
if (event->exec_delay > 0) { if (event->exec_delay > 0) {
log_debug("delay execution of '%s'", command); log_debug("delay execution of '%s'", command);

View File

@ -22,6 +22,7 @@
#include "fileio.h" #include "fileio.h"
#include "fs-util.h" #include "fs-util.h"
#include "glob-util.h" #include "glob-util.h"
#include "libudev-device-internal.h"
#include "path-util.h" #include "path-util.h"
#include "proc-cmdline.h" #include "proc-cmdline.h"
#include "stat-util.h" #include "stat-util.h"
@ -1227,7 +1228,7 @@ static void add_rule(struct udev_rules *rules, char *line,
if (value[0] != '/') { if (value[0] != '/') {
const enum udev_builtin_cmd cmd = udev_builtin_lookup(value); const enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
if (cmd < UDEV_BUILTIN_MAX) { if (cmd >= 0) {
LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value); LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value);
rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd); rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
continue; continue;
@ -1237,7 +1238,7 @@ static void add_rule(struct udev_rules *rules, char *line,
} else if (streq(attr, "builtin")) { } else if (streq(attr, "builtin")) {
const enum udev_builtin_cmd cmd = udev_builtin_lookup(value); const enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
if (cmd >= UDEV_BUILTIN_MAX) if (cmd < 0)
LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value); LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value);
else else
rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd); rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
@ -1275,12 +1276,12 @@ static void add_rule(struct udev_rules *rules, char *line,
if (streq(attr, "builtin")) { if (streq(attr, "builtin")) {
const enum udev_builtin_cmd cmd = udev_builtin_lookup(value); const enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
if (cmd < UDEV_BUILTIN_MAX) if (cmd < 0)
rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
else
LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value); LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value);
else
rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
} else if (streq(attr, "program")) { } else if (streq(attr, "program")) {
const enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX; const enum udev_builtin_cmd cmd = _UDEV_BUILTIN_MAX;
rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd); rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
} else } else
@ -2014,10 +2015,11 @@ int udev_rules_apply_to_event(
rules_str(rules, rule->rule.filename_off), rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line); rule->rule.filename_line);
if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) { r = udev_builtin_run(event->dev->device, cur->key.builtin_cmd, command, false);
if (r < 0) {
/* remember failure */ /* remember failure */
log_debug("IMPORT builtin '%s' returned non-zero", log_debug_errno(r, "IMPORT builtin '%s' fails: %m",
udev_builtin_name(cur->key.builtin_cmd)); udev_builtin_name(cur->key.builtin_cmd));
event->builtin_ret |= (1 << cur->key.builtin_cmd); event->builtin_ret |= (1 << cur->key.builtin_cmd);
if (cur->key.op != OP_NOMATCH) if (cur->key.op != OP_NOMATCH)
goto nomatch; goto nomatch;

View File

@ -6,15 +6,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "libudev-private.h" #include "log.h"
#include "path-util.h"
#include "string-util.h"
#include "strxcpyx.h"
#include "udev-builtin.h" #include "udev-builtin.h"
#include "udevadm.h" #include "udevadm.h"
#include "udevadm-util.h"
static const char *arg_command = NULL; static const char *arg_command = NULL;
static char arg_syspath[UTIL_PATH_SIZE] = {}; static const char *arg_syspath = NULL;
static int help(void) { static int help(void) {
printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n" printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
@ -36,7 +34,6 @@ static int parse_argv(int argc, char *argv[]) {
{} {}
}; };
const char *s;
int c; int c;
while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0) while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
@ -57,23 +54,17 @@ static int parse_argv(int argc, char *argv[]) {
return -EINVAL; return -EINVAL;
} }
s = argv[optind++]; arg_syspath = argv[optind++];
if (!s) { if (!arg_syspath) {
log_error("syspath missing."); log_error("syspath missing.");
return -EINVAL; return -EINVAL;
} }
/* add /sys if needed */
if (!path_startswith(s, "/sys"))
strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", s, NULL);
else
strscpy(arg_syspath, sizeof(arg_syspath), s);
return 1; return 1;
} }
int builtin_main(int argc, char *argv[], void *userdata) { int builtin_main(int argc, char *argv[], void *userdata) {
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL; _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
enum udev_builtin_cmd cmd; enum udev_builtin_cmd cmd;
int r; int r;
@ -86,21 +77,21 @@ int builtin_main(int argc, char *argv[], void *userdata) {
udev_builtin_init(); udev_builtin_init();
cmd = udev_builtin_lookup(arg_command); cmd = udev_builtin_lookup(arg_command);
if (cmd >= UDEV_BUILTIN_MAX) { if (cmd < 0) {
log_error("Unknown command '%s'", arg_command); log_error("Unknown command '%s'", arg_command);
r = -EINVAL; r = -EINVAL;
goto finish; goto finish;
} }
dev = udev_device_new_from_syspath(NULL, arg_syspath); r = find_device(arg_syspath, "/sys", &dev);
if (!dev) { if (r < 0) {
r = log_error_errno(errno, "Failed to open device '%s'", arg_syspath); log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
goto finish; goto finish;
} }
r = udev_builtin_run(dev, cmd, arg_command, true); r = udev_builtin_run(dev, cmd, arg_command, true);
if (r < 0) if (r < 0)
log_debug("error executing '%s', exit code %i", arg_command, r); log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
finish: finish:
udev_builtin_exit(); udev_builtin_exit();

View File

@ -2,17 +2,23 @@
#include <errno.h> #include <errno.h>
#include "alloc-util.h"
#include "device-private.h" #include "device-private.h"
#include "path-util.h" #include "path-util.h"
#include "string-util.h"
#include "udevadm-util.h" #include "udevadm-util.h"
int find_device(const char *id, const char *prefix, sd_device **ret) { int find_device(const char *id, const char *prefix, sd_device **ret) {
_cleanup_free_ char *buf = NULL;
assert(id); assert(id);
assert(ret); assert(ret);
if (prefix && !startswith(id, prefix)) if (prefix && !path_startswith(id, prefix)) {
id = strjoina(prefix, id); buf = path_join(NULL, prefix, id);
if (!buf)
return -ENOMEM;
id = buf;
}
if (path_startswith(id, "/sys/")) if (path_startswith(id, "/sys/"))
return sd_device_new_from_syspath(ret, id); return sd_device_new_from_syspath(ret, id);