Add explicit device_close routine.

This commit is contained in:
Ondrej Kozina
2019-05-24 16:10:53 +02:00
parent 0e4757e0fb
commit e92e320956
2 changed files with 22 additions and 9 deletions

View File

@@ -109,6 +109,7 @@ const char *crypt_get_cipher_spec(struct crypt_device *cd);
struct device; struct device;
int device_alloc(struct crypt_device *cd, struct device **device, const char *path); int device_alloc(struct crypt_device *cd, struct device **device, const char *path);
int device_alloc_no_check(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); void device_free(struct crypt_device *cd, struct device *device);
const char *device_path(const struct device *device); const char *device_path(const struct device *device);
const char *device_dm_name(const struct device *device); const char *device_dm_name(const struct device *device);

View File

@@ -420,15 +420,7 @@ void device_free(struct crypt_device *cd, struct device *device)
if (!device) if (!device)
return; return;
if (device->ro_dev_fd != -1) { device_close(cd, device);
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);
}
if (device->dev_fd_excl != -1) { if (device->dev_fd_excl != -1) {
log_dbg(cd, "Closed exclusive fd for %s.", device_path(device)); 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; 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;
}
}