From 441ec80468d17a548c8c85e39a0b0e74b75f2650 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 21 May 2019 18:04:04 +0200 Subject: [PATCH] loop-util: add api for locking the block device with flock() --- src/shared/loop-util.c | 13 +++++++++++++ src/shared/loop-util.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index b1d07fe708..f9be08bf35 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "alloc-util.h" @@ -233,3 +234,15 @@ int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) { return 0; } + +int loop_device_flock(LoopDevice *d, int operation) { + assert(d); + + if (d->fd < 0) + return -EBADF; + + if (flock(d->fd, operation) < 0) + return -errno; + + return 0; +} diff --git a/src/shared/loop-util.h b/src/shared/loop-util.h index e5a0ae75b8..5156b46ad6 100644 --- a/src/shared/loop-util.h +++ b/src/shared/loop-util.h @@ -28,3 +28,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(LoopDevice*, loop_device_unref); void loop_device_relinquish(LoopDevice *d); int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size); + +int loop_device_flock(LoopDevice *d, int operation);