aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorнаб <[email protected]>2021-04-11 19:27:43 +0200
committerBrian Behlendorf <[email protected]>2021-04-19 09:06:31 -0700
commit0f4d83117a6baad3743a1ada469f296a3e7ae3d0 (patch)
treec7b86886bfaefa419d89adb8feb23236fc0d8df1 /lib
parentfef8bd41fc178d7212957b611c9bc81fe10cb63e (diff)
libshare: nfs: don't leak nfs_lock_fd when lock fails
Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: George Wilson <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes #11886
Diffstat (limited to 'lib')
-rw-r--r--lib/libshare/os/freebsd/nfs.c13
-rw-r--r--lib/libshare/os/linux/nfs.c13
2 files changed, 18 insertions, 8 deletions
diff --git a/lib/libshare/os/freebsd/nfs.c b/lib/libshare/os/freebsd/nfs.c
index f11e77b84..b0394dfab 100644
--- a/lib/libshare/os/freebsd/nfs.c
+++ b/lib/libshare/os/freebsd/nfs.c
@@ -65,17 +65,22 @@ static int nfs_lock_fd = -1;
static int
nfs_exports_lock(void)
{
+ int err;
+
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (nfs_lock_fd == -1) {
+ err = errno;
fprintf(stderr, "failed to lock %s: %s\n",
- ZFS_EXPORTS_LOCK, strerror(errno));
- return (errno);
+ ZFS_EXPORTS_LOCK, strerror(err));
+ return (err);
}
if (flock(nfs_lock_fd, LOCK_EX) != 0) {
+ err = errno;
fprintf(stderr, "failed to lock %s: %s\n",
- ZFS_EXPORTS_LOCK, strerror(errno));
- return (errno);
+ ZFS_EXPORTS_LOCK, strerror(err));
+ (void) close(nfs_lock_fd);
+ return (err);
}
return (0);
}
diff --git a/lib/libshare/os/linux/nfs.c b/lib/libshare/os/linux/nfs.c
index 21d52953f..a7bcbd138 100644
--- a/lib/libshare/os/linux/nfs.c
+++ b/lib/libshare/os/linux/nfs.c
@@ -65,17 +65,22 @@ static int nfs_lock_fd = -1;
static int
nfs_exports_lock(void)
{
+ int err;
+
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (nfs_lock_fd == -1) {
+ err = errno;
fprintf(stderr, "failed to lock %s: %s\n",
- ZFS_EXPORTS_LOCK, strerror(errno));
- return (errno);
+ ZFS_EXPORTS_LOCK, strerror(err));
+ return (err);
}
if (flock(nfs_lock_fd, LOCK_EX) != 0) {
+ err = errno;
fprintf(stderr, "failed to lock %s: %s\n",
- ZFS_EXPORTS_LOCK, strerror(errno));
- return (errno);
+ ZFS_EXPORTS_LOCK, strerror(err));
+ (void) close(nfs_lock_fd);
+ return (err);
}
return (0);
}