Blame SOURCES/nfs-utils-1.3.0-server-generator.patch

fc3648
diff -up nfs-utils-1.3.0/configure.ac.orig nfs-utils-1.3.0/configure.ac
fc3648
--- nfs-utils-1.3.0/configure.ac.orig	2017-03-31 15:55:05.544831618 -0400
fc3648
+++ nfs-utils-1.3.0/configure.ac	2017-03-31 15:58:38.833955546 -0400
fc3648
@@ -64,8 +64,14 @@ unitdir=/usr/lib/systemd/system
fc3648
 AC_ARG_WITH(systemd,
fc3648
 	[AC_HELP_STRING([--with-systemd@<:@=unit-dir-path@:>@],
fc3648
 			[install systemd unit files @<:@Default: no, and path defaults to /usr/lib/systemd/system if not given@:>@])],
fc3648
-	test "$withval" = "no" && use_systemd=0 || unitdir=$withval use_systemd=1
fc3648
-	use_systemd=0
fc3648
+	if test "$withval" != "no" ; then 
fc3648
+		use_systemd=1
fc3648
+		if test "$withval" != "yes" ; then 
fc3648
+			unitdir=$withval
fc3648
+		fi
fc3648
+	else
fc3648
+		use_systemd=0
fc3648
+	fi
fc3648
 	)
fc3648
 	AM_CONDITIONAL(INSTALL_SYSTEMD, [test "$use_systemd" = 1])
fc3648
 	AC_SUBST(unitdir)
fc3648
diff -up nfs-utils-1.3.0/.gitignore.orig nfs-utils-1.3.0/.gitignore
fc3648
--- nfs-utils-1.3.0/.gitignore.orig	2014-03-25 11:12:07.000000000 -0400
fc3648
+++ nfs-utils-1.3.0/.gitignore	2017-03-31 15:55:47.123245655 -0400
fc3648
@@ -69,6 +69,7 @@ tests/nsm_client/nlm_sm_inter_clnt.c
fc3648
 tests/nsm_client/nlm_sm_inter_svc.c
fc3648
 tests/nsm_client/nlm_sm_inter_xdr.c
fc3648
 utils/nfsidmap/nfsidmap
fc3648
+systemd/nfs-server-generator
fc3648
 # cscope database files
fc3648
 cscope.*
fc3648
 # generic editor backup et al
fc3648
diff -up nfs-utils-1.3.0/support/export/export.c.orig nfs-utils-1.3.0/support/export/export.c
fc3648
--- nfs-utils-1.3.0/support/export/export.c.orig	2017-03-31 15:55:05.528831459 -0400
fc3648
+++ nfs-utils-1.3.0/support/export/export.c	2017-03-31 15:55:47.124245665 -0400
fc3648
@@ -15,6 +15,8 @@
fc3648
 #include <sys/param.h>
fc3648
 #include <netinet/in.h>
fc3648
 #include <stdlib.h>
fc3648
+#include <dirent.h>
fc3648
+#include <errno.h>
fc3648
 #include "xmalloc.h"
fc3648
 #include "nfslib.h"
fc3648
 #include "exportfs.h"
fc3648
@@ -68,11 +70,15 @@ static void warn_duplicated_exports(nfs_
fc3648
 /**
fc3648
  * export_read - read entries from /etc/exports
fc3648
  * @fname: name of file to read from
fc3648
+ * @ignore_hosts: don't check validity of host names
fc3648
  *
fc3648
  * Returns number of read entries.
fc3648
+ * @ignore_hosts can be set when the host names won't be used
fc3648
+ * and when getting delays or errors due to problems with
fc3648
+ * hostname looking is not acceptable.
fc3648
  */
fc3648
 int
fc3648
-export_read(char *fname)
fc3648
+export_read(char *fname, int ignore_hosts)
fc3648
 {
fc3648
 	struct exportent	*eep;
fc3648
 	nfs_export		*exp;
fc3648
@@ -81,7 +87,7 @@ export_read(char *fname)
fc3648
 
fc3648
 	setexportent(fname, "r");
fc3648
 	while ((eep = getexportent(0,1)) != NULL) {
fc3648
-		exp = export_lookup(eep->e_hostname, eep->e_path, 0);
fc3648
+		exp = export_lookup(eep->e_hostname, eep->e_path, ignore_hosts);
fc3648
 		if (!exp) {
fc3648
 			if (export_create(eep, 0))
fc3648
 				/* possible complaints already logged */
fc3648
@@ -94,6 +100,70 @@ export_read(char *fname)
fc3648
 
fc3648
 	return volumes;
fc3648
 }
fc3648
+
fc3648
+/**
fc3648
+ * export_d_read - read entries from /etc/exports.
fc3648
+ * @fname: name of directory to read from
fc3648
+ * @ignore_hosts: don't check validity of host names
fc3648
+ *
fc3648
+ * Returns number of read entries.
fc3648
+ * Based on mnt_table_parse_dir() in
fc3648
+ *  util-linux-ng/shlibs/mount/src/tab_parse.c
fc3648
+ */
fc3648
+int
fc3648
+export_d_read(const char *dname, int ignore_hosts)
fc3648
+{
fc3648
+	int n = 0, i;
fc3648
+	struct dirent **namelist = NULL;
fc3648
+	int volumes = 0;
fc3648
+
fc3648
+
fc3648
+	n = scandir(dname, &namelist, NULL, versionsort);
fc3648
+	if (n < 0) {
fc3648
+		if (errno == ENOENT)
fc3648
+			/* Silently return */
fc3648
+			return volumes;
fc3648
+		xlog(L_NOTICE, "scandir %s: %s", dname, strerror(errno));
fc3648
+	} else if (n == 0)
fc3648
+		return volumes;
fc3648
+
fc3648
+	for (i = 0; i < n; i++) {
fc3648
+		struct dirent *d = namelist[i];
fc3648
+		size_t namesz;
fc3648
+		char fname[PATH_MAX + 1];
fc3648
+		int fname_len;
fc3648
+
fc3648
+
fc3648
+		if (d->d_type != DT_UNKNOWN
fc3648
+		    && d->d_type != DT_REG
fc3648
+		    && d->d_type != DT_LNK)
fc3648
+			continue;
fc3648
+		if (*d->d_name == '.')
fc3648
+			continue;
fc3648
+
fc3648
+#define _EXT_EXPORT_SIZ   (sizeof(_EXT_EXPORT) - 1)
fc3648
+		namesz = strlen(d->d_name);
fc3648
+		if (!namesz
fc3648
+		    || namesz < _EXT_EXPORT_SIZ + 1
fc3648
+		    || strcmp(d->d_name + (namesz - _EXT_EXPORT_SIZ),
fc3648
+			      _EXT_EXPORT))
fc3648
+			continue;
fc3648
+
fc3648
+		fname_len = snprintf(fname, PATH_MAX +1, "%s/%s", dname, d->d_name);
fc3648
+		if (fname_len > PATH_MAX) {
fc3648
+			xlog(L_WARNING, "Too long file name: %s in %s", d->d_name, dname);
fc3648
+			continue;
fc3648
+		}
fc3648
+
fc3648
+		volumes += export_read(fname, ignore_hosts);
fc3648
+	}
fc3648
+
fc3648
+	for (i = 0; i < n; i++)
fc3648
+		free(namelist[i]);
fc3648
+	free(namelist);
fc3648
+
fc3648
+	return volumes;
fc3648
+}
fc3648
 
fc3648
 /**
fc3648
  * export_create - create an in-core nfs_export record from an export entry
fc3648
diff -up nfs-utils-1.3.0/support/include/exportfs.h.orig nfs-utils-1.3.0/support/include/exportfs.h
fc3648
--- nfs-utils-1.3.0/support/include/exportfs.h.orig	2017-03-31 15:55:05.528831459 -0400
fc3648
+++ nfs-utils-1.3.0/support/include/exportfs.h	2017-03-31 15:55:47.124245665 -0400
fc3648
@@ -133,7 +133,8 @@ struct addrinfo *		client_resolve(const
fc3648
 int 				client_member(const char *client,
fc3648
 						const char *name);
fc3648
 
fc3648
-int				export_read(char *fname);
fc3648
+int				export_read(char *fname, int ignore_hosts);
fc3648
+int				export_d_read(const char *dname, int ignore_hosts);
fc3648
 void				export_reset(nfs_export *);
fc3648
 nfs_export *			export_lookup(char *hname, char *path, int caconical);
fc3648
 nfs_export *			export_find(const struct addrinfo *ai,
fc3648
diff -up nfs-utils-1.3.0/systemd/Makefile.am.orig nfs-utils-1.3.0/systemd/Makefile.am
fc3648
--- nfs-utils-1.3.0/systemd/Makefile.am.orig	2017-03-31 15:55:05.545831628 -0400
fc3648
+++ nfs-utils-1.3.0/systemd/Makefile.am	2017-03-31 15:55:47.124245665 -0400
fc3648
@@ -4,7 +4,6 @@ MAINTAINERCLEANFILES = Makefile.in
fc3648
 
fc3648
 unit_files =  \
fc3648
     nfs-client.target \
fc3648
-    \
fc3648
     auth-rpcgss-module.service \
fc3648
     nfs-blkmap.service \
fc3648
     nfs-config.service \
fc3648
@@ -15,8 +14,6 @@ unit_files =  \
fc3648
     rpc-gssd.service \
fc3648
     rpc-statd-notify.service \
fc3648
     rpc-statd.service \
fc3648
-    rpc-svcgssd.service \
fc3648
-    \
fc3648
     proc-fs-nfsd.mount \
fc3648
     var-lib-nfs-rpc_pipefs.mount
fc3648
 
fc3648
@@ -25,8 +22,16 @@ man7_MANS	= nfs.systemd.man
fc3648
 EXTRA_DIST = $(unit_files) $(man5_MANS) $(man7_MANS)
fc3648
 
fc3648
 unit_dir = /usr/lib/systemd/system
fc3648
+generator_dir = /usr/lib/systemd/system-generators
fc3648
+
fc3648
+EXTRA_PROGRAMS	= nfs-server-generator
fc3648
+genexecdir = $(generator_dir)
fc3648
+nfs_server_generator_LDADD = ../support/export/libexport.a \
fc3648
+			     ../support/nfs/libnfs.a \
fc3648
+			     ../support/misc/libmisc.a
fc3648
 
fc3648
 if INSTALL_SYSTEMD
fc3648
+genexec_PROGRAMS = nfs-server-generator
fc3648
 install-data-hook: $(unit_files)
fc3648
 	mkdir -p $(DESTDIR)/$(unitdir)
fc3648
 	cp $(unit_files) $(DESTDIR)/$(unitdir)
fc3648
diff -up nfs-utils-1.3.0/systemd/nfs-server-generator.c.orig nfs-utils-1.3.0/systemd/nfs-server-generator.c
fc3648
--- nfs-utils-1.3.0/systemd/nfs-server-generator.c.orig	2017-03-31 15:55:47.124245665 -0400
fc3648
+++ nfs-utils-1.3.0/systemd/nfs-server-generator.c	2017-03-31 15:55:47.124245665 -0400
fc3648
@@ -0,0 +1,179 @@
fc3648
+/*
fc3648
+ * nfs-server-generator:
fc3648
+ *   systemd generator to create ordering dependencies between
fc3648
+ *   nfs-server and various filesystem mounts
fc3648
+ *
fc3648
+ * 1/ nfs-server should start Before any 'nfs' mountpoints are
fc3648
+ *    mounted, in case they are loop-back mounts.  This ordering is particularly
fc3648
+ *    important for the shutdown side, so the nfs-server is stopped
fc3648
+ *    after the filesystems are unmounted.
fc3648
+ * 2/ nfs-server should start After all exported filesystems are mounted
fc3648
+ *    so there is no risk of exporting the underlying directory.
fc3648
+ *    This is particularly important for _net mounts which
fc3648
+ *    are not caught by "local-fs.target".
fc3648
+ */
fc3648
+
fc3648
+#ifdef HAVE_CONFIG_H
fc3648
+#include <config.h>
fc3648
+#endif
fc3648
+
fc3648
+#include <sys/stat.h>
fc3648
+#include <sys/types.h>
fc3648
+#include <unistd.h>
fc3648
+#include <stdlib.h>
fc3648
+#include <string.h>
fc3648
+#include <ctype.h>
fc3648
+#include <stdio.h>
fc3648
+#include <mntent.h>
fc3648
+
fc3648
+#include "misc.h"
fc3648
+#include "nfslib.h"
fc3648
+#include "exportfs.h"
fc3648
+
fc3648
+/* A simple "set of strings" to remove duplicates
fc3648
+ * found in /etc/exports
fc3648
+ */
fc3648
+struct list {
fc3648
+	struct list *next;
fc3648
+	char *name;
fc3648
+};
fc3648
+static int is_unique(struct list **lp, char *path)
fc3648
+{
fc3648
+	struct list *l = *lp;
fc3648
+
fc3648
+	while (l) {
fc3648
+		if (strcmp(l->name, path) == 0)
fc3648
+			return 0;
fc3648
+		l = l->next;
fc3648
+	}
fc3648
+	l = malloc(sizeof(*l));
fc3648
+	if (l == NULL)
fc3648
+		return 0;
fc3648
+	l->name = path;
fc3648
+	l->next = *lp;
fc3648
+	*lp = l;
fc3648
+	return 1;
fc3648
+}
fc3648
+
fc3648
+/* We need to convert a path name to a systemd unit
fc3648
+ * name.  This requires some translation ('/' -> '-')
fc3648
+ * and some escaping.
fc3648
+ */
fc3648
+static void systemd_escape(FILE *f, char *path)
fc3648
+{
fc3648
+	while (*path == '/')
fc3648
+		path++;
fc3648
+	if (!*path) {
fc3648
+		/* "/" becomes "-", otherwise leading "/" is ignored */
fc3648
+		fputs("-", f);
fc3648
+		return;
fc3648
+	}
fc3648
+	while (*path) {
fc3648
+		char c = *path++;
fc3648
+
fc3648
+		if (c == '/') {
fc3648
+			/* multiple non-trailing slashes become '-' */
fc3648
+			while (*path == '/')
fc3648
+				path++;
fc3648
+			if (*path)
fc3648
+				fputs("-", f);
fc3648
+		} else if (isalnum(c) || c == ':' || c == '.')
fc3648
+			fputc(c, f);
fc3648
+		else
fc3648
+			fprintf(f, "\\x%02x", c & 0xff);
fc3648
+	}
fc3648
+}
fc3648
+
fc3648
+static int has_noauto_flag(char *path)
fc3648
+{
fc3648
+	FILE		*fstab;
fc3648
+	struct mntent	*mnt;
fc3648
+
fc3648
+	fstab = setmntent("/etc/fstab", "r");
fc3648
+	if (!fstab)
fc3648
+		return 0;
fc3648
+
fc3648
+	while ((mnt = getmntent(fstab)) != NULL) {
fc3648
+		int l = strlen(mnt->mnt_dir);
fc3648
+		if (strncmp(mnt->mnt_dir, path, l) != 0)
fc3648
+			continue;
fc3648
+		if (path[l] && path[l] != '/')
fc3648
+			continue;
fc3648
+		if (hasmntopt(mnt, "noauto"))
fc3648
+			break;
fc3648
+	}
fc3648
+	fclose(fstab);
fc3648
+	return mnt != NULL;
fc3648
+}
fc3648
+
fc3648
+int main(int argc, char *argv[])
fc3648
+{
fc3648
+	char		*path;
fc3648
+	char		dirbase[] = "/nfs-server.service.d";
fc3648
+	char		filebase[] = "/order-with-mounts.conf";
fc3648
+	nfs_export	*exp;
fc3648
+	int		i;
fc3648
+	struct list	*list = NULL;
fc3648
+	FILE		*f, *fstab;
fc3648
+	struct mntent	*mnt;
fc3648
+
fc3648
+	/* Avoid using any external services */
fc3648
+	xlog_syslog(0);
fc3648
+
fc3648
+	if (argc != 4 || argv[1][0] != '/') {
fc3648
+		fprintf(stderr, "nfs-server-generator: create systemd dependencies for nfs-server\n");
fc3648
+		fprintf(stderr, "Usage: normal-dir early-dir late-dir\n");
fc3648
+		exit(1);
fc3648
+	}
fc3648
+
fc3648
+	path = malloc(strlen(argv[1]) + sizeof(dirbase) + sizeof(filebase));
fc3648
+	if (!path)
fc3648
+		exit(2);
fc3648
+	if (export_read(_PATH_EXPORTS, 1) +
fc3648
+	    export_d_read(_PATH_EXPORTS_D, 1) == 0)
fc3648
+		/* Nothing is exported, so nothing to do */
fc3648
+		exit(0);
fc3648
+
fc3648
+	strcat(strcpy(path, argv[1]), dirbase);
fc3648
+	mkdir(path, 0755);
fc3648
+	strcat(path, filebase);
fc3648
+	f = fopen(path, "w");
fc3648
+	if (!f)
fc3648
+		exit(1);
fc3648
+	fprintf(f, "# Automatically generated by nfs-server-generator\n\n[Unit]\n");
fc3648
+
fc3648
+	for (i = 0; i < MCL_MAXTYPES; i++) {
fc3648
+		for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
fc3648
+			if (!is_unique(&list, exp->m_export.e_path))
fc3648
+				continue;
fc3648
+			if (exp->m_export.e_mountpoint)
fc3648
+				continue;
fc3648
+			if (has_noauto_flag(exp->m_export.e_path))
fc3648
+				continue;
fc3648
+			if (strchr(exp->m_export.e_path, ' '))
fc3648
+				fprintf(f, "RequiresMountsFor=\"%s\"\n",
fc3648
+					exp->m_export.e_path);
fc3648
+			else
fc3648
+				fprintf(f, "RequiresMountsFor=%s\n",
fc3648
+					exp->m_export.e_path);
fc3648
+		}
fc3648
+	}
fc3648
+
fc3648
+	fstab = setmntent("/etc/fstab", "r");
fc3648
+	if (!fstab)
fc3648
+		exit(1);
fc3648
+
fc3648
+	while ((mnt = getmntent(fstab)) != NULL) {
fc3648
+		if (strcmp(mnt->mnt_type, "nfs") != 0 &&
fc3648
+		    strcmp(mnt->mnt_type, "nfs4") != 0)
fc3648
+			continue;
fc3648
+		fprintf(f, "Before= ");
fc3648
+		systemd_escape(f, mnt->mnt_dir);
fc3648
+		fprintf(f, ".mount\n");
fc3648
+	}
fc3648
+
fc3648
+	fclose(fstab);
fc3648
+	fclose(f);
fc3648
+
fc3648
+	exit(0);
fc3648
+}
fc3648
diff -up nfs-utils-1.3.0/utils/exportfs/exportfs.c.orig nfs-utils-1.3.0/utils/exportfs/exportfs.c
fc3648
--- nfs-utils-1.3.0/utils/exportfs/exportfs.c.orig	2017-03-31 15:55:05.553831708 -0400
fc3648
+++ nfs-utils-1.3.0/utils/exportfs/exportfs.c	2017-03-31 15:55:47.125245675 -0400
fc3648
@@ -26,7 +26,6 @@
fc3648
 #include <fcntl.h>
fc3648
 #include <netdb.h>
fc3648
 #include <errno.h>
fc3648
-#include <dirent.h>
fc3648
 #include <limits.h>
fc3648
 #include <time.h>
fc3648
 
fc3648
@@ -48,7 +47,6 @@ static void	error(nfs_export *exp, int e
fc3648
 static void	usage(const char *progname, int n);
fc3648
 static void	validate_export(nfs_export *exp);
fc3648
 static int	matchhostname(const char *hostname1, const char *hostname2);
fc3648
-static int	export_d_read(const char *dname);
fc3648
 static void grab_lockfile(void);
fc3648
 static void release_lockfile(void);
fc3648
 
fc3648
@@ -190,8 +188,8 @@ main(int argc, char **argv)
fc3648
 	atexit(release_lockfile);
fc3648
 
fc3648
 	if (f_export && ! f_ignore) {
fc3648
-		if (! (export_read(_PATH_EXPORTS) +
fc3648
-		       export_d_read(_PATH_EXPORTS_D))) {
fc3648
+		if (! (export_read(_PATH_EXPORTS, 0) +
fc3648
+		       export_d_read(_PATH_EXPORTS_D, 0))) {
fc3648
 			if (f_verbose)
fc3648
 				xlog(L_WARNING, "No file systems exported!");
fc3648
 		}
fc3648
@@ -705,63 +703,6 @@ out:
fc3648
 	return result;
fc3648
 }
fc3648
 
fc3648
-/* Based on mnt_table_parse_dir() in
fc3648
-   util-linux-ng/shlibs/mount/src/tab_parse.c */
fc3648
-static int
fc3648
-export_d_read(const char *dname)
fc3648
-{
fc3648
-	int n = 0, i;
fc3648
-	struct dirent **namelist = NULL;
fc3648
-	int volumes = 0;
fc3648
-
fc3648
-
fc3648
-	n = scandir(dname, &namelist, NULL, versionsort);
fc3648
-	if (n < 0) {
fc3648
-		if (errno == ENOENT)
fc3648
-			/* Silently return */
fc3648
-			return volumes;
fc3648
-		xlog(L_NOTICE, "scandir %s: %s", dname, strerror(errno));
fc3648
-	} else if (n == 0)
fc3648
-		return volumes;
fc3648
-
fc3648
-	for (i = 0; i < n; i++) {
fc3648
-		struct dirent *d = namelist[i];
fc3648
-		size_t namesz;
fc3648
-		char fname[PATH_MAX + 1];
fc3648
-		int fname_len;
fc3648
-
fc3648
-
fc3648
-		if (d->d_type != DT_UNKNOWN
fc3648
-		    && d->d_type != DT_REG
fc3648
-		    && d->d_type != DT_LNK)
fc3648
-			continue;
fc3648
-		if (*d->d_name == '.')
fc3648
-			continue;
fc3648
-
fc3648
-#define _EXT_EXPORT_SIZ   (sizeof(_EXT_EXPORT) - 1)
fc3648
-		namesz = strlen(d->d_name);
fc3648
-		if (!namesz
fc3648
-		    || namesz < _EXT_EXPORT_SIZ + 1
fc3648
-		    || strcmp(d->d_name + (namesz - _EXT_EXPORT_SIZ),
fc3648
-			      _EXT_EXPORT))
fc3648
-			continue;
fc3648
-
fc3648
-		fname_len = snprintf(fname, PATH_MAX +1, "%s/%s", dname, d->d_name);
fc3648
-		if (fname_len > PATH_MAX) {
fc3648
-			xlog(L_WARNING, "Too long file name: %s in %s", d->d_name, dname);
fc3648
-			continue;
fc3648
-		}
fc3648
-
fc3648
-		volumes += export_read(fname);
fc3648
-	}
fc3648
-
fc3648
-	for (i = 0; i < n; i++)
fc3648
-		free(namelist[i]);
fc3648
-	free(namelist);
fc3648
-
fc3648
-	return volumes;
fc3648
-}
fc3648
-
fc3648
 static char
fc3648
 dumpopt(char c, char *fmt, ...)
fc3648
 {