udev: introduce udev_queue_is_empty() and udev_queue_init()

This commit is contained in:
Yu Watanabe 2020-12-14 18:20:18 +09:00
parent 0746a5ee4c
commit bee33d0527
3 changed files with 34 additions and 19 deletions

View 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;
r = udev_queue_init();
if (r < 0)
return r;
if (inotify_add_watch(fd, "/run/udev" , IN_DELETE) < 0)
return -errno;
udev_queue->fd = TAKE_FD(fd);
return udev_queue->fd;
return udev_queue->fd = r;
}
/**

View 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);
}

View 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);