[dm-devel] [PATCH 2/3] kpartx: use opened loop device immediately
Martin Wilck
mwilck at suse.com
Tue Nov 16 21:34:02 UTC 2021
On Mon, 2021-11-15 at 21:22 +0100, mwilck at suse.com wrote:
> From: Martin Wilck <mwilck at suse.com>
>
> The code in find_unused_loop_device() goes through circles to
> get an unused device, but it takes no care not to race with a
> different
> process opening the same loop device. Use the once-opened
> loop device for setup immediately instead of closing and re-opening
> it.
>
> While at it, simplify the code somewhat.
>
> Signed-off-by: Martin Wilck <mwilck at suse.com>
> ---
> kpartx/kpartx.c | 4 +--
> kpartx/lopart.c | 72 +++++++++++++++++++----------------------------
> --
> kpartx/lopart.h | 3 +--
> 3 files changed, 29 insertions(+), 50 deletions(-)
>
> diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c
> index 7bc6454..3c49999 100644
> --- a/kpartx/kpartx.c
> +++ b/kpartx/kpartx.c
>
> @@ -266,11 +250,9 @@ int set_loop(const char *device, const char
> *file, int offset, int *loopro)
> }
> }
>
> - if ((fd = open (device, mode)) < 0) {
> - close(ffd);
> - perror (device);
> + *device = find_unused_loop_device(mode, &fd);
> + if (!*device)
> return 1;
This leaks the file descriptor ffd. I'll re-post.
Regards
Martin
More information about the dm-devel
mailing list