From d30e1dc85834cb3f7985da0c50f5fda5c7c5f0d9 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Fri, 19 Jan 2024 05:46:10 +0900 Subject: [PATCH] LU-17444 utils: fix fd leak after conversion to llapi_root_path_open Conversions to llapi_root_path_open missed a few close() calls, leading to fd leaks. These should be obvious enough to regroup in a single commit. Fixes: 7154244354e3 ("LU-16786 utils: Replace open call to WANT_FD") Change-Id: I3af25ef2981367bfaea7f5280972f84bee09a5c2 Signed-off-by: Dominique Martinet Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/53736 Reviewed-by: Andreas Dilger Reviewed-by: Arshad Hussain Reviewed-by: Stephane Thiell Reviewed-by: Etienne AUJAMES Reviewed-by: Oleg Drokin Tested-by: jenkins Tested-by: Maloo --- lustre/utils/lfs.c | 6 ++++-- lustre/utils/liblustreapi_fid.c | 5 +---- lustre/utils/liblustreapi_util.c | 1 + 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index 4566994..20e42c0 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -10102,10 +10102,11 @@ static int lfs_rmfid(int argc, char **argv) fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1])); if (!fa) { + rc = -errno ?: -ENOMEM; fprintf(stderr, "%s rmfid: error allocating %zd bytes: %s\n", progname, offsetof(struct fid_array, fa_fids[nr + 1]), - strerror(errno)); - return -ENOMEM; + strerror(-rc)); + goto out_close; } fa->fa_nr = 0; @@ -10149,6 +10150,7 @@ static int lfs_rmfid(int argc, char **argv) fa = NULL; } +out_close: close(rootfd); return rc; } diff --git a/lustre/utils/liblustreapi_fid.c b/lustre/utils/liblustreapi_fid.c index b3bf411..bf326bc 100644 --- a/lustre/utils/liblustreapi_fid.c +++ b/lustre/utils/liblustreapi_fid.c @@ -472,15 +472,12 @@ int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid, { int mnt_fd, rc; - /* this will return a cached FD if available, so only one open needed. - * WANT_FD doesn't modify lustre_dir so casting away "const" is OK */ - rc = llapi_root_path_open(lustre_dir, &mnt_fd); if (rc) goto out; - /* "mnt_fd" is cached internally for reuse, no need to close it */ rc = llapi_open_by_fid_at(mnt_fd, fid, flags); + close(mnt_fd); out: return rc; } diff --git a/lustre/utils/liblustreapi_util.c b/lustre/utils/liblustreapi_util.c index 9309aac..ceab7a4 100644 --- a/lustre/utils/liblustreapi_util.c +++ b/lustre/utils/liblustreapi_util.c @@ -342,6 +342,7 @@ int llapi_rmfid(const char *path, struct fid_array *fa) } rc = llapi_rmfid_at(rootfd, fa); + close(rootfd); if (rc < 0) { fprintf(stderr, "lfs rmfid: cannot remove FIDs: %s\n", strerror(-rc)); -- 1.8.3.1