core: use chip fd if zero returned nshubin/cuse bb/nshubin/cuse
authorNikita Shubin <nikita.shubin@maquefel.me>
Mon, 9 Dec 2024 12:25:40 +0000 (15:25 +0300)
committerNikita Shubin <nikita.shubin@maquefel.me>
Mon, 9 Dec 2024 12:25:40 +0000 (15:25 +0300)
In case we are using CUSE faked device, it can't provide dedicated file
descriptor. Use chip fd instead.

Signed-off-by: Nikita Shubin <nikita.shubin@maquefel.me>
lib/chip.c
tools/gpiomon.c

index 611eb32858a03cb993d84fe4acd615a5167b9cab..0459b0c231bdf979676564d9ff279d72f73277f3 100644 (file)
@@ -215,6 +215,7 @@ gpiod_chip_request_lines(struct gpiod_chip *chip,
        struct gpio_v2_line_request uapi_req;
        struct gpiod_line_request *request;
        struct gpiochip_info info;
+       bool close_uapi_fd = true;
        int ret;
 
        assert(chip);
@@ -241,9 +242,16 @@ gpiod_chip_request_lines(struct gpiod_chip *chip,
        if (ret)
                return NULL;
 
+       if (uapi_req.fd == 0) {
+               close_uapi_fd = true;
+               uapi_req.fd = chip->fd;
+       }
+
        request = gpiod_line_request_from_uapi(&uapi_req, info.name);
        if (!request) {
-               close(uapi_req.fd);
+               if (close_uapi_fd)
+                       close(uapi_req.fd);
+
                return NULL;
        }
 
index 88b5ccf67e3ee17ac0ee0541ea0d0ab72ae9953e..3a02b80ea20249f944e9956d611f9be25de63093 100644 (file)
@@ -447,7 +447,6 @@ int main(int argc, char **argv)
 
                pollfds[i].fd = gpiod_line_request_get_fd(requests[i]);
                pollfds[i].events = POLLIN;
-               gpiod_chip_close(chip);
        }
 
        gpiod_request_config_free(req_cfg);
@@ -501,6 +500,8 @@ int main(int argc, char **argv)
        }
 
 done:
+       gpiod_chip_close(chip);
+
        for (i = 0; i < resolver->num_chips; i++)
                gpiod_line_request_release(requests[i]);