]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: introduce udev_queue_is_empty() and udev_queue_init()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 14 Dec 2020 09:20:18 +0000 (18:20 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 15 Dec 2020 17:28:08 +0000 (02:28 +0900)
src/libudev/libudev-queue.c
src/shared/udev-util.c
src/shared/udev-util.h

index 01b237fde42d1b7e7c46aa9c923536a151fb3f8c..7ca17fa6c39c0a76b4f3f004bcc22fe5b2d8c18c 100644 (file)
@@ -4,9 +4,6 @@
 ***/
 
 #include <errno.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <sys/inotify.h>
 #include <unistd.h>
 
 #include "libudev.h"
@@ -14,6 +11,7 @@
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "io-util.h"
+#include "udev-util.h"
 
 /**
  * SECTION:libudev-queue
@@ -144,7 +142,7 @@ _public_ int udev_queue_get_udev_is_active(struct udev_queue *udev_queue) {
  * Returns: a flag indicating if udev is currently handling events.
  **/
 _public_ int udev_queue_get_queue_is_empty(struct udev_queue *udev_queue) {
-        return access("/run/udev/queue", F_OK) < 0;
+        return udev_queue_is_empty() > 0;
 }
 
 /**
@@ -153,14 +151,13 @@ _public_ int udev_queue_get_queue_is_empty(struct udev_queue *udev_queue) {
  * @start: first event sequence number
  * @end: last event sequence number
  *
- * This function is deprecated, it just returns the result of
- * udev_queue_get_queue_is_empty().
+ * This function is deprecated, and equivalent to udev_queue_get_queue_is_empty().
  *
  * Returns: a flag indicating if udev is currently handling events.
  **/
 _public_ int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_queue,
                                                         unsigned long long int start, unsigned long long int end) {
-        return udev_queue_get_queue_is_empty(udev_queue);
+        return udev_queue_is_empty() > 0;
 }
 
 /**
@@ -168,13 +165,12 @@ _public_ int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_
  * @udev_queue: udev queue context
  * @seqnum: sequence number
  *
- * This function is deprecated, it just returns the result of
- * udev_queue_get_queue_is_empty().
+ * This function is deprecated, and equivalent to udev_queue_get_queue_is_empty().
  *
  * Returns: a flag indicating if udev is currently handling events.
  **/
 _public_ int udev_queue_get_seqnum_is_finished(struct udev_queue *udev_queue, unsigned long long int seqnum) {
-        return udev_queue_get_queue_is_empty(udev_queue);
+        return udev_queue_is_empty() > 0;
 }
 
 /**
@@ -196,22 +192,18 @@ _public_ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_qu
  * Returns: a file descriptor to watch for a queue to become empty.
  */
 _public_ int udev_queue_get_fd(struct udev_queue *udev_queue) {
-        _cleanup_close_ int fd = -1;
+        int r;
 
         assert_return(udev_queue, -EINVAL);
 
         if (udev_queue->fd >= 0)
                 return udev_queue->fd;
 
-        fd = inotify_init1(IN_CLOEXEC);
-        if (fd < 0)
-                return -errno;
-
-        if (inotify_add_watch(fd, "/run/udev" , IN_DELETE) < 0)
-                return -errno;
+        r = udev_queue_init();
+        if (r < 0)
+                return r;
 
-        udev_queue->fd = TAKE_FD(fd);
-        return udev_queue->fd;
+        return udev_queue->fd = r;
 }
 
 /**
index 5a506887e7a2fdf935d446f015a2c3a87a0ae21a..df08e3a40e663c6eeb42d123ffe21eac187d33ad 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <sys/inotify.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
@@ -9,6 +10,7 @@
 #include "device-util.h"
 #include "env-file.h"
 #include "escape.h"
+#include "fd-util.h"
 #include "log.h"
 #include "macro.h"
 #include "parse-util.h"
@@ -536,3 +538,21 @@ int udev_resolve_subsys_kernel(const char *string, char *result, size_t maxsize,
         }
         return 0;
 }
+
+int udev_queue_is_empty(void) {
+        return access("/run/udev/queue", F_OK) < 0 ?
+                (errno == ENOENT ? true : -errno) : false;
+}
+
+int udev_queue_init(void) {
+        _cleanup_close_ int fd = -1;
+
+        fd = inotify_init1(IN_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        if (inotify_add_watch(fd, "/run/udev" , IN_DELETE) < 0)
+                return -errno;
+
+        return TAKE_FD(fd);
+}
index 61ce4eaac98b4d17acc70ed8997f1a62ecb1a9cc..4e6e903ddde03ed3f43c26940bd571747e858e45 100644 (file)
@@ -43,3 +43,6 @@ int udev_rule_parse_value(char *str, char **ret_value, char **ret_endpos);
 size_t udev_replace_whitespace(const char *str, char *to, size_t len);
 size_t udev_replace_chars(char *str, const char *allow);
 int udev_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value);
+
+int udev_queue_is_empty(void);
+int udev_queue_init(void);