diff --git a/lib/internal.h b/lib/internal.h index b86908b9..2279a22c 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -109,6 +109,7 @@ const char *crypt_get_cipher_spec(struct crypt_device *cd); struct device; int device_alloc(struct crypt_device *cd, struct device **device, const char *path); int device_alloc_no_check(struct device **device, const char *path); +void device_close(struct crypt_device *cd, struct device *device); void device_free(struct crypt_device *cd, struct device *device); const char *device_path(const struct device *device); const char *device_dm_name(const struct device *device); diff --git a/lib/utils_device.c b/lib/utils_device.c index 28a84f4b..b56cc8e6 100644 --- a/lib/utils_device.c +++ b/lib/utils_device.c @@ -420,15 +420,7 @@ void device_free(struct crypt_device *cd, struct device *device) if (!device) return; - if (device->ro_dev_fd != -1) { - log_dbg(cd, "Closed read only fd for %s.", device_path(device)); - close(device->ro_dev_fd); - } - - if (device->dev_fd != -1) { - log_dbg(cd, "Closed read write fd for %s.", device_path(device)); - close(device->dev_fd); - } + device_close(cd, device); if (device->dev_fd_excl != -1) { log_dbg(cd, "Closed exclusive fd for %s.", device_path(device)); @@ -978,3 +970,23 @@ bool device_is_locked(struct device *device) { return device ? device_locked(device->lh) : 0; } + +void device_close(struct crypt_device *cd, struct device *device) +{ + if (!device) + return; + + if (device->ro_dev_fd != -1) { + log_dbg(cd, "Closing read only fd for %s.", device_path(device)); + if (close(device->ro_dev_fd)) + log_dbg(cd, "Failed to close read only fd for %s.", device_path(device)); + device->ro_dev_fd = -1; + } + + if (device->dev_fd != -1) { + log_dbg(cd, "Closing read write fd for %s.", device_path(device)); + if (close(device->dev_fd)) + log_dbg(cd, "Failed to close read write fd for %s.", device_path(device)); + device->dev_fd = -1; + } +}