libshare: nfs: don't leak nfs_lock_fd when lock fails

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #11886
This commit is contained in:
наб 2021-04-11 19:27:43 +02:00 committed by Brian Behlendorf
parent fef8bd41fc
commit 0f4d83117a
2 changed files with 18 additions and 8 deletions

View File

@ -65,17 +65,22 @@ static int nfs_lock_fd = -1;
static int static int
nfs_exports_lock(void) nfs_exports_lock(void)
{ {
int err;
nfs_lock_fd = open(ZFS_EXPORTS_LOCK, nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
O_RDWR | O_CREAT | O_CLOEXEC, 0600); O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (nfs_lock_fd == -1) { if (nfs_lock_fd == -1) {
err = errno;
fprintf(stderr, "failed to lock %s: %s\n", fprintf(stderr, "failed to lock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(errno)); ZFS_EXPORTS_LOCK, strerror(err));
return (errno); return (err);
} }
if (flock(nfs_lock_fd, LOCK_EX) != 0) { if (flock(nfs_lock_fd, LOCK_EX) != 0) {
err = errno;
fprintf(stderr, "failed to lock %s: %s\n", fprintf(stderr, "failed to lock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(errno)); ZFS_EXPORTS_LOCK, strerror(err));
return (errno); (void) close(nfs_lock_fd);
return (err);
} }
return (0); return (0);
} }

View File

@ -65,17 +65,22 @@ static int nfs_lock_fd = -1;
static int static int
nfs_exports_lock(void) nfs_exports_lock(void)
{ {
int err;
nfs_lock_fd = open(ZFS_EXPORTS_LOCK, nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
O_RDWR | O_CREAT | O_CLOEXEC, 0600); O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (nfs_lock_fd == -1) { if (nfs_lock_fd == -1) {
err = errno;
fprintf(stderr, "failed to lock %s: %s\n", fprintf(stderr, "failed to lock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(errno)); ZFS_EXPORTS_LOCK, strerror(err));
return (errno); return (err);
} }
if (flock(nfs_lock_fd, LOCK_EX) != 0) { if (flock(nfs_lock_fd, LOCK_EX) != 0) {
err = errno;
fprintf(stderr, "failed to lock %s: %s\n", fprintf(stderr, "failed to lock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(errno)); ZFS_EXPORTS_LOCK, strerror(err));
return (errno); (void) close(nfs_lock_fd);
return (err);
} }
return (0); return (0);
} }