From: Bartosz Golaszewski Date: Fri, 18 Mar 2022 13:15:07 +0000 (+0100) Subject: gpiosim: fix file descriptor leak X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=38f57b1084dcf92b24590ce1a7c4db32255277fc;p=qemu-gpiodev%2Flibgpiod.git gpiosim: fix file descriptor leak If a bank gets released before the device is disabled, the sysfs file descriptor of that bank is leaked. close() it when releasing the bank. While at it: improve two other details - unlink the configfs directory after closing the file descriptor associated with it and remove an unnecessary check when closing the sysfs descriptor when disabling the device. Signed-off-by: Bartosz Golaszewski --- diff --git a/tests/gpiosim/gpiosim.c b/tests/gpiosim/gpiosim.c index 3e13ff9..6e36dfd 100644 --- a/tests/gpiosim/gpiosim.c +++ b/tests/gpiosim/gpiosim.c @@ -659,10 +659,6 @@ static void dev_close_sysfs_dirs(struct gpiosim_dev *dev) free(bank->chip_name); free(bank->dev_path); bank->chip_name = bank->dev_path = NULL; - - if (bank->sysfs_dir_fd < 0) - break; - close(bank->sysfs_dir_fd); bank->sysfs_dir_fd = -1; } @@ -744,9 +740,12 @@ static void bank_release(struct refcount *ref) } list_del(&bank->siblings); + close(bank->cfs_dir_fd); unlinkat(dev->cfs_dir_fd, bank->item_name, AT_REMOVEDIR); gpiosim_dev_unref(dev); - close(bank->cfs_dir_fd); + if (bank->sysfs_dir_fd >= 0) + /* If the device wasn't disabled yet, this fd is still open. */ + close(bank->sysfs_dir_fd); free(bank->item_name); free(bank->chip_name); free(bank->dev_path);