libshare: nfs: commonify nfs_{init,fini}_tmpfile(), nfs_disable_share()

Also open the temp file cloexec

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:42:07 +02:00
committed by Brian Behlendorf
parent a3d387b31d
commit 8357bbff1f
5 changed files with 101 additions and 132 deletions
+6 -69
View File
@@ -350,49 +350,6 @@ get_linux_shareopts(const char *shareopts, char **plinux_opts)
return (error);
}
static char *
nfs_init_tmpfile(void)
{
char *tmpfile = NULL;
struct stat sb;
if (stat(ZFS_EXPORTS_DIR, &sb) < 0 &&
mkdir(ZFS_EXPORTS_DIR, 0755) < 0) {
fprintf(stderr, "failed to create %s: %s\n",
ZFS_EXPORTS_DIR, strerror(errno));
return (NULL);
}
if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
fprintf(stderr, "Unable to allocate temporary file\n");
return (NULL);
}
int fd = mkstemp(tmpfile);
if (fd == -1) {
fprintf(stderr, "Unable to create temporary file: %s",
strerror(errno));
free(tmpfile);
return (NULL);
}
close(fd);
return (tmpfile);
}
static int
nfs_fini_tmpfile(char *tmpfile)
{
if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
strerror(errno));
unlink(tmpfile);
free(tmpfile);
return (SA_SYSTEM_ERR);
}
free(tmpfile);
return (SA_OK);
}
/*
* This function populates an entry into /etc/exports.d/zfs.exports.
* This file is consumed by the linux nfs server so that zfs shares are
@@ -443,7 +400,7 @@ nfs_add_entry(const char *filename, const char *sharepath,
* This function copies all entries from the exports file to "filename",
* omitting any entries for the specified mountpoint.
*/
static int
__attribute__((visibility("hidden"))) int
nfs_copy_entries(char *filename, const char *mountpoint)
{
char *buf = NULL;
@@ -516,7 +473,8 @@ nfs_enable_share(sa_share_impl_t impl_share)
char *filename = NULL;
int error;
if ((filename = nfs_init_tmpfile()) == NULL)
if ((filename =
nfs_init_tmpfile(ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR)) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock(ZFS_EXPORTS_LOCK);
@@ -547,7 +505,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
linux_opts);
free(linux_opts);
if (error == 0) {
error = nfs_fini_tmpfile(filename);
error = nfs_fini_tmpfile(ZFS_EXPORTS_FILE, filename);
} else {
unlink(filename);
free(filename);
@@ -562,29 +520,8 @@ nfs_enable_share(sa_share_impl_t impl_share)
static int
nfs_disable_share(sa_share_impl_t impl_share)
{
int error;
char *filename = NULL;
if ((filename = nfs_init_tmpfile()) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock(ZFS_EXPORTS_LOCK);
if (error != 0) {
unlink(filename);
free(filename);
return (error);
}
error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
if (error != SA_OK) {
unlink(filename);
free(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
error = nfs_fini_tmpfile(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
return (nfs_disable_share_impl(
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share));
}
static boolean_t