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 40abc63c40
commit cc33149e5a
2 changed files with 18 additions and 8 deletions

View File

@ -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);
}

View File

@ -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);
}