libudev-queue.c revision a267bebf464cbb339d3411370f2e984dfc063567
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * libudev - interface to udev device information
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Copyright (C) 2009 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * This library is free software; you can redistribute it and/or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * modify it under the terms of the GNU Lesser General Public
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * License as published by the Free Software Foundation; either
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * version 2.1 of the License, or (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdio.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdlib.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stddef.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <unistd.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <errno.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <string.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <dirent.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <fcntl.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <limits.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <sys/stat.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "libudev.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "libudev-private.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering * SECTION:libudev-queue
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering * @short_description: access to currently active events
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering *
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering * The udev daemon processes events asynchronously. All events which do not have
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * interdependencies run in parallel. This exports the current state of the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * event processing queue, and the current event sequence numbers from the kernel
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering * and the udev daemon.
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering */
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering * udev_queue:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Opaque object representing the current event queue in the udev daemon.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstruct udev_queue {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev *udev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int refcount;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering struct udev_list queue_list;
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering};
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering/**
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering * udev_queue_new:
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering * @udev: udev library context
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * The initial refcount is 1, and needs to be decremented to
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * release the resources of the udev queue context.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Returns: the udev queue context, or #NULL on error.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ struct udev_queue *udev_queue_new(struct udev *udev)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering struct udev_queue *udev_queue;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk udev_queue = calloc(1, sizeof(struct udev_queue));
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering if (udev_queue == NULL)
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering return NULL;
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz udev_queue->refcount = 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_queue->udev = udev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_list_init(udev, &udev_queue->queue_list, false);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return udev_queue;
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * udev_queue_ref:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Take a reference of a udev queue context.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Returns: the same udev queue context.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack_public_ struct udev_queue *udev_queue_ref(struct udev_queue *udev_queue)
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack{
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev_queue == NULL)
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_queue->refcount++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return udev_queue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek * udev_queue_unref:
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Drop a reference of a udev queue context. If the refcount reaches zero,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * the resources of the queue context will be released.
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Returns: the passed queue context if it has still an active reference, or #NULL otherwise.
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek{
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev_queue == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_queue->refcount--;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev_queue->refcount > 0)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return udev_queue;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek udev_list_cleanup(&udev_queue->queue_list);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(udev_queue);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * udev_queue_get_udev:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Retrieve the udev library context the queue context was created with.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Returns: the udev library context.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ struct udev *udev_queue_get_udev(struct udev_queue *udev_queue)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev_queue == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return udev_queue->udev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekunsigned long long int udev_get_kernel_seqnum(struct udev *udev)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned long long int seqnum;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char buf[32];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ssize_t len;
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = open("/sys/kernel/uevent_seqnum", O_RDONLY|O_CLOEXEC);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek len = read(fd, buf, sizeof(buf));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close(fd);
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer if (len <= 2)
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer return 0;
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer buf[len-1] = '\0';
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer seqnum = strtoull(buf, NULL, 10);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return seqnum;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * udev_queue_get_kernel_seqnum:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Get the current kernel event sequence number.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Returns: the sequence number.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ unsigned long long int udev_queue_get_kernel_seqnum(struct udev_queue *udev_queue)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned long long int seqnum;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev_queue == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek seqnum = udev_get_kernel_seqnum(udev_queue->udev);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek return seqnum;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek}
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmekint udev_queue_read_seqnum(FILE *queue_file, unsigned long long int *seqnum)
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek{
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek if (fread(seqnum, sizeof(unsigned long long int), 1, queue_file) != 1)
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek return -1;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek return 0;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek}
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekssize_t udev_queue_skip_devpath(FILE *queue_file)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek unsigned short int len;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (fread(&len, sizeof(unsigned short int), 1, queue_file) == 1) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek char *devpath = alloca(len);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering /* use fread to skip, fseek might drop buffered data */
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering if (fread(devpath, 1, len, queue_file) == len)
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering return len;
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering }
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return -1;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek}
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekssize_t udev_queue_read_devpath(FILE *queue_file, char *devpath, size_t size)
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek{
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek unsigned short int read_bytes = 0;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek unsigned short int len;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fread(&len, sizeof(unsigned short int), 1, queue_file) != 1)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek read_bytes = (len < size - 1) ? len : size - 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fread(devpath, 1, read_bytes, queue_file) != read_bytes)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek devpath[read_bytes] = '\0';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* if devpath was too long, skip unread characters */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (read_bytes != len) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned short int skip_bytes = len - read_bytes;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *buf = alloca(skip_bytes);
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fread(buf, 1, skip_bytes, queue_file) != skip_bytes)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt return -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering return read_bytes;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic FILE *open_queue_file(struct udev_queue *udev_queue, unsigned long long int *seqnum_start)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt FILE *queue_file;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek queue_file = fopen("/run/udev/queue.bin", "re");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (queue_file == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev_queue_read_seqnum(queue_file, seqnum_start) < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_err(udev_queue->udev, "corrupt queue file\n");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fclose(queue_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return queue_file;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig)/**
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) * udev_queue_get_udev_seqnum:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) *
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) * Get the last known udev event sequence number.
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Returns: the sequence number.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ unsigned long long int udev_queue_get_udev_seqnum(struct udev_queue *udev_queue)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt unsigned long long int seqnum_udev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek FILE *queue_file;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek queue_file = open_queue_file(udev_queue, &seqnum_udev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (queue_file == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (;;) {
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek unsigned long long int seqnum;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ssize_t devpath_len;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (udev_queue_read_seqnum(queue_file, &seqnum) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek devpath_len = udev_queue_skip_devpath(queue_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (devpath_len < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (devpath_len > 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek seqnum_udev = seqnum;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fclose(queue_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return seqnum_udev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * udev_queue_get_udev_is_active:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Check if udev is active on the system.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt * Returns: a flag indicating if udev is active.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int udev_queue_get_udev_is_active(struct udev_queue *udev_queue)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek unsigned long long int seqnum_start;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek FILE *queue_file;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek queue_file = open_queue_file(udev_queue, &seqnum_start);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (queue_file == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fclose(queue_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek/**
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * udev_queue_get_queue_is_empty:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Check if udev is currently processing any events.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt * Returns: a flag indicating if udev is currently handling events.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int udev_queue_get_queue_is_empty(struct udev_queue *udev_queue)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek{
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned long long int seqnum_kernel;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned long long int seqnum_udev = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int queued = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int is_empty = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek FILE *queue_file;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (udev_queue == NULL)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return -EINVAL;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering queue_file = open_queue_file(udev_queue, &seqnum_udev);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (queue_file == NULL)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return 1;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek for (;;) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek unsigned long long int seqnum;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek ssize_t devpath_len;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (udev_queue_read_seqnum(queue_file, &seqnum) < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek break;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek devpath_len = udev_queue_skip_devpath(queue_file);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (devpath_len < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek break;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (devpath_len > 0) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering queued++;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek seqnum_udev = seqnum;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek } else {
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering queued--;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (queued > 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek seqnum_kernel = udev_queue_get_kernel_seqnum(udev_queue);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (seqnum_udev < seqnum_kernel)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek is_empty = 1;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekout:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fclose(queue_file);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt return is_empty;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek}
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt/**
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek * udev_queue_get_seqnum_sequence_is_finished:
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt * @start: first event sequence number
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @end: last event sequence number
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Check if udev is currently processing any events in a given sequence number range.
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk *
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * Returns: a flag indicating if any of the sequence numbers in the given range is currently active.
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk **/
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk_public_ int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_queue,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk unsigned long long int start, unsigned long long int end)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk{
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk unsigned long long int seqnum;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk ssize_t devpath_len;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk int unfinished;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt FILE *queue_file;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (udev_queue == NULL)
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt return -EINVAL;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk queue_file = open_queue_file(udev_queue, &seqnum);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (queue_file == NULL)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt return 1;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (start < seqnum)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk start = seqnum;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (start > end) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering fclose(queue_file);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return 1;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (end - start > INT_MAX - 1) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk fclose(queue_file);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return -EOVERFLOW;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering /*
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering * we might start with 0, and handle the initial seqnum
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering * only when we find an entry in the queue file
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering **/
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering unfinished = end - start;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering do {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (udev_queue_read_seqnum(queue_file, &seqnum) < 0)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek break;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek devpath_len = udev_queue_skip_devpath(queue_file);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (devpath_len < 0)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek break;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering /*
dbd2a83fbf051fc51bdca3aa7536c78479488c5bLennart Poettering * we might start with an empty or re-build queue file, where
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * the initial seqnum is not recorded as finished
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt */
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (start == seqnum && devpath_len > 0)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek unfinished++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (devpath_len == 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (seqnum >= start && seqnum <= end)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unfinished--;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } while (unfinished > 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fclose(queue_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return (unfinished == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/**
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * udev_queue_get_seqnum_is_finished:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @udev_queue: udev queue context
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * @seqnum: sequence number
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek *
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * Check if udev is currently processing a given sequence number.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Returns: a flag indicating if the given sequence number is currently active.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek **/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_public_ int udev_queue_get_seqnum_is_finished(struct udev_queue *udev_queue, unsigned long long int seqnum)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering{
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!udev_queue_get_seqnum_sequence_is_finished(udev_queue, seqnum, seqnum))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 1;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering/**
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * udev_queue_get_queued_list_entry:
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * @udev_queue: udev queue context
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering *
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * Get the first entry of the list of queued events.
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering *
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * Returns: a udev_list_entry.
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering **/
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering_public_ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev_queue)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering{
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering unsigned long long int seqnum;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering FILE *queue_file;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (udev_queue == NULL)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering udev_list_cleanup(&udev_queue->queue_list);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering queue_file = open_queue_file(udev_queue, &seqnum);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (queue_file == NULL)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering for (;;) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char syspath[UTIL_PATH_SIZE];
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char *s;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering size_t l;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering ssize_t len;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char seqnum_str[32];
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering struct udev_list_entry *list_entry;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (udev_queue_read_seqnum(queue_file, &seqnum) < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering break;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering snprintf(seqnum_str, sizeof(seqnum_str), "%llu", seqnum);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering s = syspath;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering l = util_strpcpy(&s, sizeof(syspath), "/sys");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering len = udev_queue_read_devpath(queue_file, s, l);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (len < 0)
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (len > 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_list_entry_add(&udev_queue->queue_list, syspath, seqnum_str);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_queue->queue_list)) {
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering if (strcmp(seqnum_str, udev_list_entry_get_value(list_entry)) == 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_list_entry_delete(list_entry);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering }
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering fclose(queue_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return udev_list_get_entry(&udev_queue->queue_list);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek