From: Takashi Sakamoto Date: Tue, 29 Mar 2022 05:13:16 +0000 (+0900) Subject: utils: use libsystemd instead of libudev X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=7e71591fdaee9c04483dd4b2b05c967a754dde00;p=alsa-gobject.git utils: use libsystemd instead of libudev --- diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1e479ec..4d94540 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -46,7 +46,7 @@ jobs: DEBIAN_FRONTEND=noninteractive apt-get install -y git build-essential DEBIAN_FRONTEND=noninteractive apt-get install -y meson ninja-build libglib2.0-dev gobject-introspection libgirepository1.0-dev DEBIAN_FRONTEND=noninteractive apt-get install -y gi-docgen python3-gi - DEBIAN_FRONTEND=noninteractive apt-get install -y libudev-dev + DEBIAN_FRONTEND=noninteractive apt-get install -y libsystemd-dev - name: Initialization for build. run: | meson --prefix=/tmp. -Ddoc=true -Dwarning_level=3 . build diff --git a/README.rst b/README.rst index c131fb8..c7e2c38 100644 --- a/README.rst +++ b/README.rst @@ -62,7 +62,7 @@ Dependencies * GLib ``_ * GObject introspection ``_ -* libudev1 ``_ +* libsystemd * Linux kernel version 4.5 or later Requirements to build @@ -112,7 +112,7 @@ Design note * These libraries are independent of alsa-lib, to focus on interaction to kernel land directly by system calls without alsa-lib's configuration space and plugin framework. -* The way to enumerate any device is based on sysfs, programmed with libudev1. +* The way to enumerate any device is based on sysfs, programmed with libsystemd. * GObject-drived object is used for structures in UAPI of Linux sound subsystem with reserved space. * Boxed object is used for structures in UAPI of Linux sound subsystem without diff --git a/src/utils/meson.build b/src/utils/meson.build index b23d8df..8fcb4b6 100644 --- a/src/utils/meson.build +++ b/src/utils/meson.build @@ -9,7 +9,7 @@ sources = [ ] dependencies = [ - dependency('libudev'), + dependency('libsystemd'), ] static_library = static_library('utils', diff --git a/src/utils/sysfs.c b/src/utils/sysfs.c index c8e5b26..b725f35 100644 --- a/src/utils/sysfs.c +++ b/src/utils/sysfs.c @@ -9,69 +9,42 @@ #define SOUND_SUBSYSTEM "sound" int lookup_and_allocate_string_by_sysname(char **name, const char *sysname, - const char *(*func)(struct udev_device *)) + int (*func)(sd_device *, const char **)) { - struct udev *ctx; - struct udev_device *device; + sd_device *device; const char *n; - int err = 0; + int err; if (name == NULL || sysname == NULL || func == NULL) return -EINVAL; - ctx = udev_new(); - if (ctx == NULL) - return -errno; - - device = udev_device_new_from_subsystem_sysname(ctx, SOUND_SUBSYSTEM, sysname); - if (device == NULL) { - err = -errno; - goto err_ctx; - } + err = sd_device_new_from_subsystem_sysname(&device, SOUND_SUBSYSTEM, sysname); + if (err < 0) + return err; - n = func(device); - if (n == NULL) { - err = -errno; - goto err_dev; - } + err = func(device, &n); + if (err < 0) + goto end; *name = strdup(n); if (*name == NULL) err = -ENOMEM; -err_dev: - udev_device_unref(device); -err_ctx: - udev_unref(ctx); +end: + sd_device_unref(device); return err; } -static int detect_device(struct udev_device **device, struct udev *ctx, - struct udev_list_entry *entry, const char *prefix) +static int detect_device(sd_device *device, const char *prefix) { - const char *syspath; - struct udev_device *dev; const char *sysname; + int err; - syspath = udev_list_entry_get_name(entry); - if (syspath == NULL) - return -errno; - - dev = udev_device_new_from_syspath(ctx, syspath); - if (dev == NULL) - return -errno; - - sysname = udev_device_get_sysname(dev); - if (sysname == NULL) { - udev_device_unref(dev); - return -errno; - } + err = sd_device_get_sysname(device, &sysname); + if (err < 0) + return err; - if (strstr(sysname, prefix) != sysname) { - udev_device_unref(dev); + if (strstr(sysname, prefix) != sysname) return -ENODEV; - } - - *device = dev; return 0; } @@ -87,88 +60,70 @@ static int compare_u32(const void *l, const void *r) int generate_sysnum_list_by_sysname_prefix(unsigned int **entries, unsigned long *entry_count, const char *prefix) { - struct udev *ctx; - struct udev_enumerate *enumerator; + sd_device_enumerator *enumerator; unsigned int count; - struct udev_list_entry *entry, *entry_list; + sd_device *device; unsigned int index; int err; - ctx = udev_new(); - if (ctx == NULL) - return -errno; - - enumerator = udev_enumerate_new(ctx); - if (enumerator == NULL) { - err = -errno; - goto err_ctx; - } - - err = udev_enumerate_add_match_subsystem(enumerator, SOUND_SUBSYSTEM); - if (err < 0) { - goto err_enum; - } + err = sd_device_enumerator_new(&enumerator); + if (err < 0) + return err; - err = udev_enumerate_scan_devices(enumerator); + err = sd_device_enumerator_add_match_subsystem(enumerator, SOUND_SUBSYSTEM, 1); if (err < 0) - goto err_enum; + return err; count = 0; - entry_list = udev_enumerate_get_list_entry(enumerator); - udev_list_entry_foreach(entry, entry_list) { - struct udev_device *dev; + device = sd_device_enumerator_get_device_first(enumerator); + do { int err; - err = detect_device(&dev, ctx, entry, prefix); - if (err < 0) - continue; - - ++count; - udev_device_unref(dev); - } + err = detect_device(device, prefix); + if (err >= 0) + ++count; + } while ((device = sd_device_enumerator_get_device_next(enumerator))); // Nothing available. if (count == 0) - goto err_enum; + goto end; *entries = calloc(count, sizeof(**entries)); if (*entries == NULL) { err = -ENOMEM; - goto err_enum; + goto end; } index = 0; - udev_list_entry_foreach(entry, entry_list) { - struct udev_device *dev; + device = sd_device_enumerator_get_device_first(enumerator); + do { const char *sysnum; - long val; int err; - err = detect_device(&dev, ctx, entry, prefix); + err = detect_device(device, prefix); if (err < 0) continue; - sysnum = udev_device_get_sysnum(dev); - if (sysnum != NULL && !long_from_string(sysnum, &val)) { - (*entries)[index] = (unsigned int)val; - ++index; - } + err = sd_device_get_sysnum(device, &sysnum); + if (err >= 0) { + long val; - udev_device_unref(dev); - } + if (!long_from_string(sysnum, &val)) { + (*entries)[index] = (unsigned int)val; + ++index; + } + } + } while ((device = sd_device_enumerator_get_device_next(enumerator))); if (index != count) { err = -ENODATA; - goto err_enum; + goto end; } *entry_count = count; qsort(*entries, count, sizeof(unsigned int), compare_u32); -err_enum: - udev_enumerate_unref(enumerator); -err_ctx: - udev_unref(ctx); - +end: + sd_device_enumerator_unref(enumerator); return err; } diff --git a/src/utils/utils.h b/src/utils/utils.h index 2c6bca1..a527cf4 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -4,7 +4,7 @@ #include #include -#include +#include #define generate_file_error(exception, errno, ...) \ g_set_error(exception, G_FILE_ERROR, g_file_error_from_errno(errno), __VA_ARGS__) @@ -26,7 +26,7 @@ long long_from_string(const char *literal, long *number); int allocate_string(char **dst, const char *template, va_list ap); int lookup_and_allocate_string_by_sysname(char **name, const char *sysname, - const char *(*func)(struct udev_device *)); + int (*func)(sd_device *, const char **)); int generate_sysnum_list_by_sysname_prefix(unsigned int **entries, unsigned long *entry_count, const char *prefix); @@ -35,7 +35,7 @@ int request_ctl_ioctl_opened(int *fd, unsigned int card_id, long request, void * int request_ctl_ioctl(unsigned int card_id, long request, void *data); static inline int lookup_and_allocate_name_by_sysname(char **name, - const char *(*func)(struct udev_device *), + int (*func)(sd_device *, const char **), const char *fmt, va_list ap) { char *sysname; @@ -54,7 +54,7 @@ static inline int lookup_and_allocate_sysname_by_sysname(char **sysname, const c int err; va_start(ap, fmt); - err = lookup_and_allocate_name_by_sysname(sysname, udev_device_get_sysname, fmt, ap); + err = lookup_and_allocate_name_by_sysname(sysname, sd_device_get_sysname, fmt, ap); va_end(ap); return err; @@ -66,7 +66,7 @@ static inline int lookup_and_allocate_devname_by_sysname(char **devname, const c int err; va_start(ap, fmt); - err = lookup_and_allocate_name_by_sysname(devname, udev_device_get_devnode, fmt, ap); + err = lookup_and_allocate_name_by_sysname(devname, sd_device_get_devname, fmt, ap); va_end(ap); return err;