From: Nikita Shubin Date: Mon, 9 Dec 2024 12:25:40 +0000 (+0300) Subject: core: use chip fd if zero returned X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=4b31970da381e1202a8c93549b6fa4eab0af9088;p=qemu-gpiodev%2Flibgpiod.git core: use chip fd if zero returned In case we are using CUSE faked device, it can't provide dedicated file descriptor. Use chip fd instead. Signed-off-by: Nikita Shubin --- diff --git a/lib/chip.c b/lib/chip.c index 611eb32..0459b0c 100644 --- a/lib/chip.c +++ b/lib/chip.c @@ -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; } diff --git a/tools/gpiomon.c b/tools/gpiomon.c index 88b5ccf..3a02b80 100644 --- a/tools/gpiomon.c +++ b/tools/gpiomon.c @@ -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]);