From 605e03e51a8baeb90c4194c511e85e3663d4a78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Mon, 17 May 2021 20:25:29 +0200 Subject: [PATCH] libshare: nfs: share nfs_is_shared() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Don Brady Reviewed-by: Brian Behlendorf Reviewed-by: John Kennedy Signed-off-by: Ahelenia ZiemiaƄska Closes #12067 --- lib/libshare/nfs.c | 70 +++++++++++++++++++++++++---------- lib/libshare/nfs.h | 1 + lib/libshare/os/freebsd/nfs.c | 30 +-------------- lib/libshare/os/linux/nfs.c | 26 +------------ 4 files changed, 53 insertions(+), 74 deletions(-) diff --git a/lib/libshare/nfs.c b/lib/libshare/nfs.c index 4bc7e7779e..79b822fbcc 100644 --- a/lib/libshare/nfs.c +++ b/lib/libshare/nfs.c @@ -148,38 +148,29 @@ nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf) return (SA_OK); } -/* - * Copy all entries from the exports file to newfp, - * omitting any entries for the specified mountpoint. - */ static int -nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint) +nfs_process_exports(const char *exports, const char *mountpoint, + boolean_t (*cbk)(void *userdata, char *line, boolean_t found_mountpoint), + void *userdata) { int error = SA_OK; + boolean_t cont = B_TRUE; - fputs(FILE_HEADER, newfp); - - /* - * ZFS_EXPORTS_FILE may not exist yet. - * If that's the case, then just write out the new file. - */ FILE *oldfp = fopen(exports, "re"); if (oldfp != NULL) { char *buf = NULL, *sep; size_t buflen = 0, mplen = strlen(mountpoint); - while (getline(&buf, &buflen, oldfp) != -1) { - + while (cont && getline(&buf, &buflen, oldfp) != -1) { if (buf[0] == '\n' || buf[0] == '#') continue; - if ((sep = strpbrk(buf, "\t \n")) != NULL && + cont = cbk(userdata, buf, + (sep = strpbrk(buf, "\t \n")) != NULL && sep - buf == mplen && - strncmp(buf, mountpoint, mplen) == 0) - continue; - - fputs(buf, newfp); + strncmp(buf, mountpoint, mplen) == 0); } + free(buf); if (ferror(oldfp) != 0) error = ferror(oldfp); @@ -189,10 +180,32 @@ nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint) exports, strerror(errno)); error = error != SA_OK ? error : SA_SYSTEM_ERR; } - - free(buf); } + return (error); +} + +static boolean_t +nfs_copy_entries_cb(void *userdata, char *line, boolean_t found_mountpoint) +{ + FILE *newfp = userdata; + if (!found_mountpoint) + fputs(line, newfp); + return (B_TRUE); +} + +/* + * Copy all entries from the exports file (if it exists) to newfp, + * omitting any entries for the specified mountpoint. + */ +static int +nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint) +{ + fputs(FILE_HEADER, newfp); + + int error = nfs_process_exports( + exports, mountpoint, nfs_copy_entries_cb, newfp); + if (error == SA_OK && ferror(newfp) != 0) error = ferror(newfp); @@ -233,3 +246,20 @@ fullerr: nfs_exports_unlock(lockfile); return (error); } + +static boolean_t +nfs_is_shared_cb(void *userdata, char *line, boolean_t found_mountpoint) +{ + boolean_t *found = userdata; + *found = found_mountpoint; + return (!found_mountpoint); +} + +boolean_t +nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share) +{ + boolean_t found = B_FALSE; + nfs_process_exports(exports, impl_share->sa_mountpoint, + nfs_is_shared_cb, &found); + return (found); +} diff --git a/lib/libshare/nfs.h b/lib/libshare/nfs.h index 63838032ba..cfac274c3d 100644 --- a/lib/libshare/nfs.h +++ b/lib/libshare/nfs.h @@ -30,6 +30,7 @@ void libshare_nfs_init(void); +boolean_t nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share); int nfs_toggle_share(const char *lockfile, const char *exports, const char *expdir, sa_share_impl_t impl_share, int(*cbk)(sa_share_impl_t impl_share, FILE *tmpfile)); diff --git a/lib/libshare/os/freebsd/nfs.c b/lib/libshare/os/freebsd/nfs.c index ba98a64726..b53ff09872 100644 --- a/lib/libshare/os/freebsd/nfs.c +++ b/lib/libshare/os/freebsd/nfs.c @@ -148,35 +148,7 @@ nfs_disable_share(sa_share_impl_t impl_share) static boolean_t nfs_is_shared(sa_share_impl_t impl_share) { - char *s, last, line[MAXLINESIZE]; - size_t len; - char *mntpoint = impl_share->sa_mountpoint; - size_t mntlen = strlen(mntpoint); - - FILE *fp = fopen(ZFS_EXPORTS_FILE, "re"); - if (fp == NULL) - return (B_FALSE); - - for (;;) { - s = fgets(line, sizeof (line), fp); - if (s == NULL) - return (B_FALSE); - /* Skip empty lines and comments. */ - if (line[0] == '\n' || line[0] == '#') - continue; - len = strlen(line); - if (line[len - 1] == '\n') - line[len - 1] = '\0'; - last = line[mntlen]; - /* Skip the given mountpoint. */ - if (strncmp(mntpoint, line, mntlen) == 0 && - (last == '\t' || last == ' ' || last == '\0')) { - fclose(fp); - return (B_TRUE); - } - } - fclose(fp); - return (B_FALSE); + return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share)); } static int diff --git a/lib/libshare/os/linux/nfs.c b/lib/libshare/os/linux/nfs.c index 7f8ebd7f65..d66228336d 100644 --- a/lib/libshare/os/linux/nfs.c +++ b/lib/libshare/os/linux/nfs.c @@ -467,31 +467,7 @@ nfs_disable_share(sa_share_impl_t impl_share) static boolean_t nfs_is_shared(sa_share_impl_t impl_share) { - size_t buflen = 0; - char *buf = NULL; - - FILE *fp = fopen(ZFS_EXPORTS_FILE, "re"); - if (fp == NULL) { - return (B_FALSE); - } - while ((getline(&buf, &buflen, fp)) != -1) { - char *space = NULL; - - if ((space = strchr(buf, ' ')) != NULL) { - int mountpoint_len = strlen(impl_share->sa_mountpoint); - - if (space - buf == mountpoint_len && - strncmp(impl_share->sa_mountpoint, buf, - mountpoint_len) == 0) { - fclose(fp); - free(buf); - return (B_TRUE); - } - } - } - free(buf); - fclose(fp); - return (B_FALSE); + return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share)); } /*