]> git.alsa-project.org Git - alsa-gobject.git/commitdiff
utils: use libsystemd instead of libudev topic/libsystemd
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Tue, 29 Mar 2022 05:13:16 +0000 (14:13 +0900)
committerTakashi Sakamoto <o-takashi@sakamocchi.jp>
Mon, 12 Dec 2022 08:10:02 +0000 (17:10 +0900)
.github/workflows/build.yml
README.rst
src/utils/meson.build
src/utils/sysfs.c
src/utils/utils.h

index 1e479ecb77eba07e5c3acb94f8352c7208cfec93..4d945402fb2e1ee920baedc343d7be1aae7b8eda 100644 (file)
@@ -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
index c131fb8aab505c35fb9c3385a4606f2537c1e431..c7e2c3880919e865e5926a55da04ed018e4bd2fe 100644 (file)
@@ -62,7 +62,7 @@ Dependencies
 
 * GLib `<https://gitlab.gnome.org/GNOME/glib>`_
 * GObject introspection `<https://gi.readthedocs.io/>`_
-* libudev1 `<https://www.freedesktop.org/wiki/Software/systemd/>`_
+* libsystemd <https://www.freedesktop.org/wiki/Software/systemd/>
 * 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
index b23d8dfbbd9fb78361311dfca9d65f7c87a32ec7..8fcb4b6165805a88d8b02d8eb27bb9c0bbcfce11 100644 (file)
@@ -9,7 +9,7 @@ sources = [
 ]
 
 dependencies = [
-  dependency('libudev'),
+  dependency('libsystemd'),
 ]
 
 static_library = static_library('utils',
index c8e5b263e9738f07b406008ac6ea69d00c35e9cb..b725f35e5510176e35210b614eb980a3fc76f80d 100644 (file)
@@ -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;
 }
index 2c6bca1b7036dc626396a1d207d73175e1934314..a527cf4939c28be5ae09bb653382e6f03fa6ffc1 100644 (file)
@@ -4,7 +4,7 @@
 
 #include <stdlib.h>
 #include <stdarg.h>
-#include <libudev.h>
+#include <systemd/sd-device.h>
 
 #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;