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 Tony Hutter
parent 30620ad8e1
commit 501da8d433
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, 0600); O_RDWR | O_CREAT, 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, 0600); O_RDWR | O_CREAT, 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);
} }