Le 13/07/2025 à 02:13, aitor a écrit :
> Hi,
> After updating the iso images with the lastest version of vdev, I've decided to try to solve another issue existent in vdev. I'll
> try to explain in the best way.
>
> When vdev starts running, the first thing it will do is to find all the devices shown by the kernel in the userspace by searching
> all the uevent files in the sysfs filesystem, ending up in a poll() system call that will wait for new events, triggered every time
> a hotpluggable device is added/removed.
>
> The problem is that processing all the coldplugged devices requires a considerable time and, if a new device arises in the meantime,
> it won't be functional for several seconds. In the worst of the cases, it may be possible that vdev misses it depending on the followed
> runs of search within the sysfs's directory hierarchy.
>
> On the other hand, I haven't found a way to wait on the file descriptor -i.e. the poll() system call- in a non-blocking mode while all
> this is going on. We could think about forking a child process in order to carry out the coldplugged device processing and wait for new
> events in the parent process. But, in doing so, there is a high risk of breaking the eventfs filesystem (monitored by libudev clients),
> as it is receiving synthetic events from different sources at the same time.
>
> None the less, I'm considering to use the async.h library:
>
> https://github.com/naasking/async.h
>
> The idea is to define two asynchronous tasks during the coldplug phase. The first task will process the coldplugged devices in batches.
> During the batch processing, a second task will interrumpt the first task in order to detect possible new devices (by searching again in
> the sysfs), devices that will be consumed in the latter task before going ahead with the next batch of devices in the earlier.
>
> This asynchronous diagram can be controled by semaphores, as you can see in the example bellow:
>
> https://github.com/naasking/async.h/blob/master/async/example-buffer.c
>
> Today I've developed a simple example and it seems to work. The idea behind this scheme is to detect arising devices by searching in the
> sysfs filesystem as well, while cannot otherwise be done. The list of devices processed in the first task remains constant, it's not updated
> by the second task. From batch to batch, the latter gives first priority to new arising devices, say a keyboard or a mouse or a usb stick...
> And so on until the last batch of devices is processed in the first task, ending up again in the poll() system call, and waiting forever.
AFAIU, your issue is that the sysfs tree may change while you scan
it and before you start polling for new events. I don't know if this
problem is real and if it has a solution.
However, if your issue is to poll() new events in the same time you
scan sysfs, I could imagine to do these two things with two threads. The
thread which scans sysfs would feed a pipe with the uevents it finds and
the thread which polls() new events might poll() not only the uevent
netlink but also your pipe, in the same time, hence reading two sources
of uevents.
Might be simpler than using async.
-- Didier