質問

I'm trying to port an application that was running before on an Ubuntu system to the Raspberry Pi, using Raspbian. The application should detect new USB flash drives. This is how the udev monitoring is done:

/* Create the udev object */
udev = udev_new();
if (!udev) {
    wprinterr("Can't create udev\n");
    exit(EXIT_FAILURE);
}
mon = udev_monitor_new_from_netlink(udev, "udev");
if(mon==NULL) {
    wprinterr("Could not create udev monitor!\n");
    exit(EXIT_FAILURE);
}
if(udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL) != 0) {
    wprinterr("Could not add subsystem match to udev monitor\n");
    exit(EXIT_FAILURE);
}
if(udev_monitor_enable_receiving(mon) != 0) {
    wprinterr("Could not enable udev monitor receiving\n");
    exit(EXIT_FAILURE);
}
while (1) {
/*
* this will block until there is a monitor event
*/
    dev = udev_monitor_receive_device(mon);
    if (dev) {

This works fine on the Ubuntu machine. But on the Raspberry Pi udev_monitor_receive_device(mon) returns immediately with a NULL pointer. The documentation (Docu) says that this happens in case of error condition. Errno is set to 11 (Resource temporarily unavailable). But I can't find out, what is going wrong. The udev daemon is running btw. Has somebody some ideas?

I wrote a similar question on Raspberry forum some time ago but didn't got an answer and could not find a solution myself. So I posted it here again. Link to Raspberry forum post

役に立ちましたか?

解決

I did not find a solution, but I found a workaround. Select can be used to block until there is a monitor event. After this udev_monitor_receie_device(mon) will return with a prober pointer to a device.

I can be done like this:

    mon = udev_monitor_new_from_netlink(udev, "udev");
    if(mon==NULL) {
        wprinterr("Could not create udev monitor!\n");
        exit(EXIT_FAILURE);
    }
    if(udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL) != 0) {
        wprinterr("Could not add subsystem match to udev monitor\n");
        exit(EXIT_FAILURE);
    }
    if(udev_monitor_enable_receiving(mon) != 0) {
        wprinterr("Could not enable udev monitor receiving\n");
        exit(EXIT_FAILURE);
    }    
    fd = udev_monitor_get_fd(mon);
    while (1) {
        /*
         * this will block until there is a monitor event
         */
        fd_set fds;
        int ret;
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
        ret = select(fd+1, &fds, NULL, NULL, NULL);

        /* Check if our file descriptor has received data. */
        if (ret > 0 && FD_ISSET(fd, &fds)) {
            dev = udev_monitor_receive_device(mon);
            if (dev) {
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top