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

View File

@ -24,6 +24,8 @@
* Copyright (c) 2011 Gunnar Beutner
* Copyright (c) 2019, 2020 by Delphix. All rights reserved.
*/
#ifndef _LIBSPL_LIBSHARE_IMPL_H
#define _LIBSPL_LIBSHARE_IMPL_H
typedef struct sa_share_fsinfo {
char *shareopts;
@ -59,3 +61,5 @@ typedef struct sa_fstype {
} sa_fstype_t;
sa_fstype_t *register_fstype(const char *name, const sa_share_ops_t *ops);
#endif /* _LIBSPL_LIBSHARE_IMPL_H */

View File

@ -22,8 +22,11 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <libshare.h>
#include "nfs.h"
@ -71,3 +74,77 @@ nfs_exports_unlock(const char *name)
(void) close(nfs_lock_fd);
nfs_lock_fd = -1;
}
__attribute__((visibility("hidden"))) char *
nfs_init_tmpfile(const char *prefix, const char *mdir)
{
char *tmpfile = NULL;
struct stat sb;
if (mdir != NULL &&
stat(mdir, &sb) < 0 &&
mkdir(mdir, 0755) < 0) {
fprintf(stderr, "failed to create %s: %s\n",
mdir, strerror(errno));
return (NULL);
}
if (asprintf(&tmpfile, "%s.XXXXXXXX", prefix) == -1) {
fprintf(stderr, "Unable to allocate temporary file\n");
return (NULL);
}
int fd = mkostemp(tmpfile, O_CLOEXEC);
if (fd == -1) {
fprintf(stderr, "Unable to create temporary file: %s",
strerror(errno));
free(tmpfile);
return (NULL);
}
close(fd);
return (tmpfile);
}
__attribute__((visibility("hidden"))) int
nfs_fini_tmpfile(const char *exports, char *tmpfile)
{
if (rename(tmpfile, exports) == -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);
}
__attribute__((visibility("hidden"))) int
nfs_disable_share_impl(const char *lockfile, const char *exports,
const char *expdir, sa_share_impl_t impl_share)
{
int error;
char *filename;
if ((filename = nfs_init_tmpfile(exports, expdir)) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock(lockfile);
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(lockfile);
return (error);
}
error = nfs_fini_tmpfile(exports, filename);
nfs_exports_unlock(lockfile);
return (error);
}

View File

@ -24,9 +24,18 @@
* Copyright (c) 2011 Gunnar Beutner
*/
#include "libshare_impl.h"
#define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
void libshare_nfs_init(void);
int nfs_exports_lock(const char *name);
void nfs_exports_unlock(const char *name);
char *nfs_init_tmpfile(const char *prefix, const char *mdir);
int nfs_fini_tmpfile(const char *exports, char *tmpfile);
int nfs_copy_entries(char *filename, const char *mountpoint);
int nfs_disable_share_impl(const char *lockfile, const char *exports,
const char *expdir, sa_share_impl_t impl_share);

View File

@ -143,47 +143,11 @@ translate_opts(const char *shareopts)
return (newopts);
}
static char *
nfs_init_tmpfile(void)
{
char *tmpfile = NULL;
if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
fprintf(stderr, "Unable to allocate buffer for temporary "
"file name\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 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)
{
int error = SA_OK;
@ -234,7 +198,7 @@ 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, NULL)) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock(ZFS_EXPORTS_LOCK);
@ -283,7 +247,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (SA_SYSTEM_ERR);
}
error = nfs_fini_tmpfile(filename);
error = nfs_fini_tmpfile(ZFS_EXPORTS_FILE, filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
@ -291,30 +255,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, NULL, impl_share));
}
/*

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