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

6006dc
diff --git a/.gitignore b/.gitignore
6006dc
index 3d9640d..d26f4be 100644
6006dc
--- a/.gitignore
6006dc
+++ b/.gitignore
6006dc
@@ -70,6 +70,8 @@ tests/nsm_client/nlm_sm_inter_svc.c
6006dc
 tests/nsm_client/nlm_sm_inter_xdr.c
6006dc
 utils/nfsidmap/nfsidmap
6006dc
 systemd/nfs-server-generator
6006dc
+systemd/rpc-pipefs-generator
6006dc
+systemd/rpc-gssd.service
6006dc
 # cscope database files
6006dc
 cscope.*
6006dc
 # generic editor backup et al
6006dc
diff --git a/configure.ac b/configure.ac
6006dc
index 3bce774..fce0e15 100644
6006dc
--- a/configure.ac
6006dc
+++ b/configure.ac
6006dc
@@ -498,8 +498,14 @@ AC_SUBST([AM_CFLAGS], ["$my_am_cflags"])
6006dc
 # Make sure that $ACLOCAL_FLAGS are used during a rebuild
6006dc
 AC_SUBST([ACLOCAL_AMFLAGS], ["-I $ac_macro_dir \$(ACLOCAL_FLAGS)"])
6006dc
 
6006dc
+# make _sysconfdir available for substituion in config files
6006dc
+# 2 "evals" needed late to expand variable names.
6006dc
+AC_SUBST([_sysconfdir])
6006dc
+AC_CONFIG_COMMANDS_PRE([eval eval _sysconfdir=$sysconfdir])
6006dc
+
6006dc
 AC_CONFIG_FILES([
6006dc
 	Makefile
6006dc
+	systemd/rpc-gssd.service
6006dc
 	linux-nfs/Makefile
6006dc
 	support/Makefile
6006dc
 	support/export/Makefile
6006dc
diff --git a/nfs.conf b/nfs.conf
6006dc
index c907de6..89cf386 100644
6006dc
--- a/nfs.conf
6006dc
+++ b/nfs.conf
6006dc
@@ -1,7 +1,10 @@
6006dc
 #
6006dc
-# This is a general conifguration for the 
6006dc
+# This is a general configuration for the
6006dc
 # NFS daemons and tools
6006dc
 #
6006dc
+#[general]
6006dc
+# pipefs-directory=/var/lib/nfs/rpc_pipefs
6006dc
+#
6006dc
 #[exportfs]
6006dc
 # debug=0
6006dc
 #
6006dc
@@ -12,7 +15,6 @@
6006dc
 # limit-to-legacy-enctypes=0
6006dc
 # context-timeout=0
6006dc
 # rpc-timeout=5
6006dc
-# pipefs-directory=/var/lib/nfs/rpc_pipefs
6006dc
 # keytab-file=/etc/krb5.keytab
6006dc
 # cred-cache-directory=
6006dc
 # preferred-realm=
6006dc
diff --git a/support/include/xcommon.h b/support/include/xcommon.h
6006dc
index d1a4b18..23c9a13 100644
6006dc
--- a/support/include/xcommon.h
6006dc
+++ b/support/include/xcommon.h
6006dc
@@ -17,6 +17,12 @@
6006dc
 #include <stdlib.h>
6006dc
 #include <string.h>
6006dc
 
6006dc
+#ifdef MAJOR_IN_MKDEV
6006dc
+#include <sys/mkdev.h>
6006dc
+#elif defined(MAJOR_IN_SYSMACROS)
6006dc
+#include <sys/sysmacros.h>
6006dc
+#endif
6006dc
+
6006dc
 #define streq(s, t)	(strcmp ((s), (t)) == 0)
6006dc
 
6006dc
 /* Functions in sundries.c that are used in mount.c and umount.c  */ 
6006dc
diff --git a/support/nfs/nfsexport.c b/support/nfs/nfsexport.c
6006dc
index f129fd2..c1c736e 100644
6006dc
--- a/support/nfs/nfsexport.c
6006dc
+++ b/support/nfs/nfsexport.c
6006dc
@@ -18,6 +18,7 @@
6006dc
 #include <fcntl.h>
6006dc
 
6006dc
 #include "nfslib.h"
6006dc
+#include "xcommon.h"
6006dc
 
6006dc
 	/* if /proc/net/rpc/... exists, then 
6006dc
 	 * write to it, as that interface is more stable.
6006dc
diff --git a/systemd/Makefile.am b/systemd/Makefile.am
6006dc
index b789916..54a3b64 100644
6006dc
--- a/systemd/Makefile.am
6006dc
+++ b/systemd/Makefile.am
6006dc
@@ -8,6 +8,7 @@ unit_files =  \
6006dc
     nfs-blkmap.service \
6006dc
     nfs-config.service \
6006dc
     nfs-idmapd.service \
6006dc
+    rpc_pipefs.target \
6006dc
     nfs-mountd.service \
6006dc
     nfs-server.service \
6006dc
     nfs-utils.service \
6006dc
@@ -24,14 +25,23 @@ EXTRA_DIST = $(unit_files) $(man5_MANS) $(man7_MANS)
6006dc
 unit_dir = /usr/lib/systemd/system
6006dc
 generator_dir = /usr/lib/systemd/system-generators
6006dc
 
6006dc
-EXTRA_PROGRAMS	= nfs-server-generator
6006dc
+EXTRA_PROGRAMS	= nfs-server-generator rpc-pipefs-generator
6006dc
 genexecdir = $(generator_dir)
6006dc
+
6006dc
+COMMON_SRCS = systemd.c systemd.h
6006dc
+
6006dc
+nfs_server_generator_SOURCES = $(COMMON_SRCS) nfs-server-generator.c
6006dc
+
6006dc
+rpc_pipefs_generator_SOURCES = $(COMMON_SRCS) rpc-pipefs-generator.c
6006dc
+
6006dc
 nfs_server_generator_LDADD = ../support/export/libexport.a \
6006dc
 			     ../support/nfs/libnfs.a \
6006dc
 			     ../support/misc/libmisc.a
6006dc
 
6006dc
+rpc_pipefs_generator_LDADD = ../support/nfs/libnfs.a
6006dc
+
6006dc
 if INSTALL_SYSTEMD
6006dc
-genexec_PROGRAMS = nfs-server-generator
6006dc
+genexec_PROGRAMS = nfs-server-generator rpc-pipefs-generator
6006dc
 install-data-hook: $(unit_files)
6006dc
 	mkdir -p $(DESTDIR)/$(unitdir)
6006dc
 	cp $(unit_files) $(DESTDIR)/$(unitdir)
6006dc
diff --git a/systemd/nfs-blkmap.service b/systemd/nfs-blkmap.service
6006dc
index ddbf4e9..c844e2d 100644
6006dc
--- a/systemd/nfs-blkmap.service
6006dc
+++ b/systemd/nfs-blkmap.service
6006dc
@@ -2,8 +2,8 @@
6006dc
 Description=pNFS block layout mapping daemon
6006dc
 DefaultDependencies=no
6006dc
 Conflicts=umount.target
6006dc
-After=var-lib-nfs-rpc_pipefs.mount
6006dc
-Requires=var-lib-nfs-rpc_pipefs.mount
6006dc
+After=rpc_pipefs.target
6006dc
+Requires=rpc_pipefs.target
6006dc
 
6006dc
 PartOf=nfs-utils.service
6006dc
 
6006dc
diff --git a/systemd/nfs-idmapd.service b/systemd/nfs-idmapd.service
6006dc
index df3dd9d..38685b6 100644
6006dc
--- a/systemd/nfs-idmapd.service
6006dc
+++ b/systemd/nfs-idmapd.service
6006dc
@@ -1,8 +1,8 @@
6006dc
 [Unit]
6006dc
 Description=NFSv4 ID-name mapping service
6006dc
 DefaultDependencies=no
6006dc
-Requires=var-lib-nfs-rpc_pipefs.mount
6006dc
-After=var-lib-nfs-rpc_pipefs.mount local-fs.target
6006dc
+Requires=rpc_pipefs.target
6006dc
+After=rpc_pipefs.target local-fs.target
6006dc
 
6006dc
 BindsTo=nfs-server.service
6006dc
 
6006dc
diff --git a/systemd/nfs-server-generator.c b/systemd/nfs-server-generator.c
6006dc
index 4aa6509..737f109 100644
6006dc
--- a/systemd/nfs-server-generator.c
6006dc
+++ b/systemd/nfs-server-generator.c
6006dc
@@ -29,6 +29,7 @@
6006dc
 #include "misc.h"
6006dc
 #include "nfslib.h"
6006dc
 #include "exportfs.h"
6006dc
+#include "systemd.h"
6006dc
 
6006dc
 /* A simple "set of strings" to remove duplicates
6006dc
  * found in /etc/exports
6006dc
@@ -55,35 +56,6 @@ static int is_unique(struct list **lp, char *path)
6006dc
 	return 1;
6006dc
 }
6006dc
 
6006dc
-/* We need to convert a path name to a systemd unit
6006dc
- * name.  This requires some translation ('/' -> '-')
6006dc
- * and some escaping.
6006dc
- */
6006dc
-static void systemd_escape(FILE *f, char *path)
6006dc
-{
6006dc
-	while (*path == '/')
6006dc
-		path++;
6006dc
-	if (!*path) {
6006dc
-		/* "/" becomes "-", otherwise leading "/" is ignored */
6006dc
-		fputs("-", f);
6006dc
-		return;
6006dc
-	}
6006dc
-	while (*path) {
6006dc
-		char c = *path++;
6006dc
-
6006dc
-		if (c == '/') {
6006dc
-			/* multiple non-trailing slashes become '-' */
6006dc
-			while (*path == '/')
6006dc
-				path++;
6006dc
-			if (*path)
6006dc
-				fputs("-", f);
6006dc
-		} else if (isalnum(c) || c == ':' || c == '.')
6006dc
-			fputc(c, f);
6006dc
-		else
6006dc
-			fprintf(f, "\\x%02x", c & 0xff);
6006dc
-	}
6006dc
-}
6006dc
-
6006dc
 static int has_noauto_flag(char *path)
6006dc
 {
6006dc
 	FILE		*fstab;
6006dc
@@ -108,7 +80,7 @@ static int has_noauto_flag(char *path)
6006dc
 
6006dc
 int main(int argc, char *argv[])
6006dc
 {
6006dc
-	char		*path;
6006dc
+	char		*path, *spath;
6006dc
 	char		dirbase[] = "/nfs-server.service.d";
6006dc
 	char		filebase[] = "/order-with-mounts.conf";
6006dc
 	nfs_export	*exp;
6006dc
@@ -167,9 +139,15 @@ int main(int argc, char *argv[])
6006dc
 		if (strcmp(mnt->mnt_type, "nfs") != 0 &&
6006dc
 		    strcmp(mnt->mnt_type, "nfs4") != 0)
6006dc
 			continue;
6006dc
-		fprintf(f, "Before= ");
6006dc
-		systemd_escape(f, mnt->mnt_dir);
6006dc
-		fprintf(f, ".mount\n");
6006dc
+
6006dc
+		spath = systemd_escape(mnt->mnt_dir, ".mount");
6006dc
+		if (!spath) {
6006dc
+			fprintf(stderr, 
6006dc
+				"nfs-server-generator: convert path failed: %s\n",
6006dc
+				mnt->mnt_dir);
6006dc
+			continue;
6006dc
+		}
6006dc
+		fprintf(f, "Before=%s\n", spath);
6006dc
 	}
6006dc
 
6006dc
 	fclose(fstab);
6006dc
diff --git a/systemd/nfs.conf.man b/systemd/nfs.conf.man
6006dc
index 938b970..9700586 100644
6006dc
--- a/systemd/nfs.conf.man
6006dc
+++ b/systemd/nfs.conf.man
6006dc
@@ -96,6 +96,18 @@ value, which can be one or more from the list
6006dc
 .BR all .
6006dc
 When a list is given, the members should be comma-separated.
6006dc
 .TP
6006dc
+.B general
6006dc
+Recognized values:
6006dc
+.BR pipefs-directory .
6006dc
+
6006dc
+See
6006dc
+.BR blkmapd (8),
6006dc
+.BR rpc.idmapd (8),
6006dc
+and
6006dc
+.BR rpc.gssd (8)
6006dc
+for details.
6006dc
+
6006dc
+.TP
6006dc
 .B nfsdcltrack
6006dc
 Recognized values:
6006dc
 .BR storagedir .
6006dc
@@ -198,7 +210,6 @@ Recognized values:
6006dc
 .BR limit-to-legacy-enctypes ,
6006dc
 .BR context-timeout ,
6006dc
 .BR rpc-timeout ,
6006dc
-.BR pipefs-directory ,
6006dc
 .BR keytab-file ,
6006dc
 .BR cred-cache-directory ,
6006dc
 .BR preferred-realm .
6006dc
diff --git a/systemd/rpc-gssd.service b/systemd/rpc-gssd.service
6006dc
deleted file mode 100644
6006dc
index 5d6d09f..0000000
6006dc
--- a/systemd/rpc-gssd.service
6006dc
+++ /dev/null
6006dc
@@ -1,19 +0,0 @@
6006dc
-[Unit]
6006dc
-Description=RPC security service for NFS client and server
6006dc
-DefaultDependencies=no
6006dc
-Conflicts=umount.target
6006dc
-Requires=var-lib-nfs-rpc_pipefs.mount
6006dc
-After=var-lib-nfs-rpc_pipefs.mount gssproxy.service
6006dc
-
6006dc
-ConditionPathExists=/etc/krb5.keytab
6006dc
-
6006dc
-PartOf=nfs-utils.service
6006dc
-
6006dc
-Wants=nfs-config.service
6006dc
-After=nfs-config.service
6006dc
-
6006dc
-[Service]
6006dc
-EnvironmentFile=-/run/sysconfig/nfs-utils
6006dc
-
6006dc
-Type=forking
6006dc
-ExecStart=/usr/sbin/rpc.gssd $GSSDARGS
6006dc
diff --git a/systemd/rpc-gssd.service.in b/systemd/rpc-gssd.service.in
6006dc
new file mode 100644
6006dc
index 0000000..c75ccbd
6006dc
--- /dev/null
6006dc
+++ b/systemd/rpc-gssd.service.in
6006dc
@@ -0,0 +1,19 @@
6006dc
+[Unit]
6006dc
+Description=RPC security service for NFS client and server
6006dc
+DefaultDependencies=no
6006dc
+Conflicts=umount.target
6006dc
+Requires=rpc_pipefs.target
6006dc
+After=rpc_pipefs.target gssproxy.service
6006dc
+
6006dc
+ConditionPathExists=@_sysconfdir@/krb5.keytab
6006dc
+
6006dc
+PartOf=nfs-utils.service
6006dc
+
6006dc
+Wants=nfs-config.service
6006dc
+After=nfs-config.service
6006dc
+
6006dc
+[Service]
6006dc
+EnvironmentFile=-/run/sysconfig/nfs-utils
6006dc
+
6006dc
+Type=forking
6006dc
+ExecStart=/usr/sbin/rpc.gssd $GSSDARGS
6006dc
diff --git a/systemd/rpc-pipefs-generator.c b/systemd/rpc-pipefs-generator.c
6006dc
new file mode 100644
6006dc
index 0000000..66addb9
6006dc
--- /dev/null
6006dc
+++ b/systemd/rpc-pipefs-generator.c
6006dc
@@ -0,0 +1,138 @@
6006dc
+/*
6006dc
+ * rpc-pipefs-generator:
6006dc
+ *   systemd generator to create ordering dependencies between
6006dc
+ *   nfs services and the rpc_pipefs mountpoint
6006dc
+ */
6006dc
+
6006dc
+#ifdef HAVE_CONFIG_H
6006dc
+#include <config.h>
6006dc
+#endif
6006dc
+
6006dc
+#include <sys/stat.h>
6006dc
+#include <sys/types.h>
6006dc
+#include <unistd.h>
6006dc
+#include <stdlib.h>
6006dc
+#include <string.h>
6006dc
+#include <ctype.h>
6006dc
+#include <stdio.h>
6006dc
+#include <mntent.h>
6006dc
+
6006dc
+#include "nfslib.h"
6006dc
+#include "conffile.h"
6006dc
+#include "systemd.h"
6006dc
+
6006dc
+#define RPC_PIPEFS_DEFAULT "/var/lib/nfs/rpc_pipefs"
6006dc
+char *conf_path = NFS_CONFFILE;
6006dc
+
6006dc
+static int generate_mount_unit(const char *pipefs_path, const char *pipefs_unit,
6006dc
+			       const char *dirname)
6006dc
+{
6006dc
+	char	*path;
6006dc
+	FILE	*f;
6006dc
+
6006dc
+	path = malloc(strlen(dirname) + 1 + strlen(pipefs_unit));
6006dc
+	if (!path)
6006dc
+		return 1;
6006dc
+	sprintf(path, "%s/%s", dirname, pipefs_unit);
6006dc
+	f = fopen(path, "w");
6006dc
+	if (!f)
6006dc
+		return 1;
6006dc
+
6006dc
+	fprintf(f, "# Automatically generated by rpc-pipefs-generator\n\n[Unit]\n");
6006dc
+	fprintf(f, "Description=RPC Pipe File System\n");
6006dc
+	fprintf(f, "DefaultDependencies=no\n");
6006dc
+	fprintf(f, "After=systemd-tmpfiles-setup.service\n");
6006dc
+	fprintf(f, "Conflicts=umount.target\n");
6006dc
+	fprintf(f, "\n[Mount]\n");
6006dc
+	fprintf(f, "What=sunrpc\n");
6006dc
+	fprintf(f, "Where=%s\n", pipefs_path);
6006dc
+	fprintf(f, "Type=rpc_pipefs\n");
6006dc
+
6006dc
+	fclose(f);
6006dc
+	return 0;
6006dc
+}
6006dc
+
6006dc
+static
6006dc
+int generate_target(char *pipefs_path, const char *dirname)
6006dc
+{
6006dc
+	char	*path;
6006dc
+	char	filebase[] = "/rpc_pipefs.target";
6006dc
+	char	*pipefs_unit;
6006dc
+	FILE	*f;
6006dc
+	int 	ret = 0;
6006dc
+
6006dc
+	pipefs_unit = systemd_escape(pipefs_path, ".mount");
6006dc
+	if (!pipefs_unit)
6006dc
+		return 1;
6006dc
+
6006dc
+	ret = generate_mount_unit(pipefs_path, pipefs_unit, dirname);
6006dc
+	if (ret)
6006dc
+		return ret;
6006dc
+
6006dc
+	path = malloc(strlen(dirname) + 1 + sizeof(filebase));
6006dc
+	if (!path)
6006dc
+		return 2;
6006dc
+	sprintf(path, "%s", dirname);
6006dc
+	mkdir(path, 0755);
6006dc
+	strcat(path, filebase);
6006dc
+	f = fopen(path, "w");
6006dc
+	if (!f)
6006dc
+		return 1;
6006dc
+
6006dc
+	fprintf(f, "# Automatically generated by rpc-pipefs-generator\n\n[Unit]\n");
6006dc
+	fprintf(f, "Requires=%s\n", pipefs_unit);
6006dc
+	fprintf(f, "After=%s\n", pipefs_unit);
6006dc
+	fclose(f);
6006dc
+
6006dc
+	return 0;
6006dc
+}
6006dc
+
6006dc
+static int is_non_pipefs_mountpoint(char *path)
6006dc
+{
6006dc
+	FILE		*mtab;
6006dc
+	struct mntent	*mnt;
6006dc
+
6006dc
+	mtab = setmntent("/etc/mtab", "r");
6006dc
+	if (!mtab)
6006dc
+		return 0;
6006dc
+
6006dc
+	while ((mnt = getmntent(mtab)) != NULL) {
6006dc
+		if (strlen(mnt->mnt_dir) != strlen(path))
6006dc
+			continue;
6006dc
+		if (strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir)))
6006dc
+			continue;
6006dc
+		if (strncmp(mnt->mnt_type, "rpc_pipefs", strlen(mnt->mnt_type)))
6006dc
+			break;
6006dc
+	}
6006dc
+	fclose(mtab);
6006dc
+	return mnt != NULL;
6006dc
+}
6006dc
+
6006dc
+int main(int argc, char *argv[])
6006dc
+{
6006dc
+	int 	ret;
6006dc
+	char	*s;
6006dc
+
6006dc
+	/* Avoid using any external services */
6006dc
+	xlog_syslog(0);
6006dc
+
6006dc
+	if (argc != 4 || argv[1][0] != '/') {
6006dc
+		fprintf(stderr, "rpc-pipefs-generator: create systemd dependencies for nfs services\n");
6006dc
+		fprintf(stderr, "Usage: normal-dir early-dir late-dir\n");
6006dc
+		exit(1);
6006dc
+	}
6006dc
+
6006dc
+	conf_init();
6006dc
+	s = conf_get_str("general", "pipefs-directory");
6006dc
+	if (!s)
6006dc
+		exit(0);
6006dc
+	if (strlen(s) == strlen(RPC_PIPEFS_DEFAULT) &&
6006dc
+			strcmp(s, RPC_PIPEFS_DEFAULT) == 0)
6006dc
+		exit(0);
6006dc
+
6006dc
+	if (is_non_pipefs_mountpoint(s))
6006dc
+		exit(1);
6006dc
+
6006dc
+	ret = generate_target(s, argv[1]);
6006dc
+	exit(ret);
6006dc
+}
6006dc
diff --git a/systemd/rpc-svcgssd.service b/systemd/rpc-svcgssd.service
6006dc
index 41177b6..a3a555c 100644
6006dc
--- a/systemd/rpc-svcgssd.service
6006dc
+++ b/systemd/rpc-svcgssd.service
6006dc
@@ -1,8 +1,7 @@
6006dc
 [Unit]
6006dc
 Description=RPC security service for NFS server
6006dc
 DefaultDependencies=no
6006dc
-Requires=var-lib-nfs-rpc_pipefs.mount
6006dc
-After=var-lib-nfs-rpc_pipefs.mount local-fs.target
6006dc
+After=local-fs.target
6006dc
 PartOf=nfs-server.service
6006dc
 PartOf=nfs-utils.service
6006dc
 
6006dc
diff --git a/systemd/rpc_pipefs.target b/systemd/rpc_pipefs.target
6006dc
new file mode 100644
6006dc
index 0000000..01d4d27
6006dc
--- /dev/null
6006dc
+++ b/systemd/rpc_pipefs.target
6006dc
@@ -0,0 +1,3 @@
6006dc
+[Unit]
6006dc
+Requires=var-lib-nfs-rpc_pipefs.mount
6006dc
+After=var-lib-nfs-rpc_pipefs.mount
6006dc
diff --git a/systemd/systemd.c b/systemd/systemd.c
6006dc
new file mode 100644
6006dc
index 0000000..17820d4
6006dc
--- /dev/null
6006dc
+++ b/systemd/systemd.c
6006dc
@@ -0,0 +1,133 @@
6006dc
+/*
6006dc
+ * Helper functions for systemd generators in nfs-utils.
6006dc
+ *
6006dc
+ * Currently just systemd_escape().
6006dc
+ */
6006dc
+
6006dc
+#include <stdio.h>
6006dc
+#include <stdlib.h>
6006dc
+#include <ctype.h>
6006dc
+#include <string.h>
6006dc
+
6006dc
+static const char hex[16] =
6006dc
+{
6006dc
+  '0', '1', '2', '3', '4', '5', '6', '7',
6006dc
+  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
6006dc
+};
6006dc
+
6006dc
+/*
6006dc
+ * determine length of the string that systemd_escape() needs to allocate
6006dc
+ */
6006dc
+static int systemd_len(char *path)
6006dc
+{
6006dc
+	char *p;
6006dc
+	int len = 0;
6006dc
+
6006dc
+	p = path;
6006dc
+	while (*p == '/')
6006dc
+		/* multiple leading "/" are ignored */
6006dc
+		p++;
6006dc
+
6006dc
+	if (!*p)
6006dc
+		/* root directory "/" becomes is encoded as a single "-" */
6006dc
+		return 1;
6006dc
+
6006dc
+	if (*p == '.')
6006dc
+		/*
6006dc
+		 * replace "." with "\x2d" escape sequence if
6006dc
+		 * it's the first character in escaped path
6006dc
+		 * */
6006dc
+		len += 4;
6006dc
+
6006dc
+	while (*p) {
6006dc
+		unsigned char c = *p++;
6006dc
+
6006dc
+		if (c == '/') {
6006dc
+			/* multiple non-trailing slashes become '-' */
6006dc
+			while (*p == '/')
6006dc
+				p++;
6006dc
+			if (*p)
6006dc
+				len++;
6006dc
+		} else if (isalnum(c) || c == ':' || c == '.' || c == '_')
6006dc
+			/* these characters are not replaced */
6006dc
+			len++;
6006dc
+		else
6006dc
+			/* replace with "\x2d" escape sequence */
6006dc
+			len += 4;
6006dc
+	}
6006dc
+
6006dc
+	return len;
6006dc
+}
6006dc
+
6006dc
+/*
6006dc
+ * convert c to "\x2d" escape sequence and append to string
6006dc
+ * at position p, advancing p
6006dc
+ */
6006dc
+static char *hexify(unsigned char c, char *p)
6006dc
+{
6006dc
+	*p++ = '\\';
6006dc
+	*p++ = 'x';
6006dc
+	*p++ = hex[c >> 4];
6006dc
+	*p++ = hex[c & 0xf];
6006dc
+	return p;
6006dc
+}
6006dc
+
6006dc
+/*
6006dc
+ * convert a path to a unit name according to the logic in systemd.unit(5):
6006dc
+ *
6006dc
+ *     Basically, given a path, "/" is replaced by "-", and all other
6006dc
+ *     characters which are not ASCII alphanumerics are replaced by C-style
6006dc
+ *     "\x2d" escapes (except that "_" is never replaced and "." is only
6006dc
+ *     replaced when it would be the first character in the escaped path).
6006dc
+ *     The root directory "/" is encoded as single dash, while otherwise the
6006dc
+ *     initial and ending "/" are removed from all paths during
6006dc
+ *     transformation.
6006dc
+ *
6006dc
+ * NB: Although the systemd.unit(5) doesn't mention it, the ':' character
6006dc
+ * is not escaped.
6006dc
+ */
6006dc
+char *systemd_escape(char *path, char *suffix)
6006dc
+{
6006dc
+	char *result;
6006dc
+	char *p;
6006dc
+	int len;
6006dc
+
6006dc
+	len = systemd_len(path);
6006dc
+	result = malloc(len + strlen(suffix) + 1);
6006dc
+	p = result;
6006dc
+	while (*path == '/')
6006dc
+		/* multiple leading "/" are ignored */
6006dc
+		path++;
6006dc
+	if (!*path) {
6006dc
+		/* root directory "/" becomes is encoded as a single "-" */
6006dc
+		*p++ = '-';
6006dc
+		goto out;
6006dc
+	}
6006dc
+	if (*path == '.')
6006dc
+		/*
6006dc
+		 * replace "." with "\x2d" escape sequence if
6006dc
+		 * it's the first character in escaped path
6006dc
+		 * */
6006dc
+		p = hexify(*path++, p);
6006dc
+
6006dc
+	while (*path) {
6006dc
+		unsigned char c = *path++;
6006dc
+
6006dc
+		if (c == '/') {
6006dc
+			/* multiple non-trailing slashes become '-' */
6006dc
+			while (*path == '/')
6006dc
+				path++;
6006dc
+			if (*path)
6006dc
+				*p++ = '-';
6006dc
+		} else if (isalnum(c) || c == ':' || c == '.' || c == '_')
6006dc
+			/* these characters are not replaced */
6006dc
+			*p++ = c;
6006dc
+		else
6006dc
+			/* replace with "\x2d" escape sequence */
6006dc
+			p = hexify(c, p);
6006dc
+	}
6006dc
+
6006dc
+out:
6006dc
+	sprintf(p, "%s", suffix);
6006dc
+	return result;
6006dc
+}
6006dc
diff --git a/systemd/systemd.h b/systemd/systemd.h
6006dc
new file mode 100644
6006dc
index 0000000..25235ec
6006dc
--- /dev/null
6006dc
+++ b/systemd/systemd.h
6006dc
@@ -0,0 +1,6 @@
6006dc
+#ifndef SYSTEMD_H
6006dc
+#define SYSTEMD_H
6006dc
+
6006dc
+char *systemd_escape(char *path, char *suffix);
6006dc
+
6006dc
+#endif /* SYSTEMD_H */
6006dc
diff --git a/utils/blkmapd/blkmapd.man b/utils/blkmapd/blkmapd.man
6006dc
index 914b80f..4b3d3f0 100644
6006dc
--- a/utils/blkmapd/blkmapd.man
6006dc
+++ b/utils/blkmapd/blkmapd.man
6006dc
@@ -43,9 +43,24 @@ Performs device discovery only then exits.
6006dc
 Runs
6006dc
 .B blkmapd
6006dc
 in the foreground and sends output to stderr (as opposed to syslogd)
6006dc
+.SH CONFIGURATION FILE
6006dc
+The
6006dc
+.B blkmapd
6006dc
+daemon recognizes the following value from the
6006dc
+.B [general]
6006dc
+section of the
6006dc
+.I /etc/nfs.conf
6006dc
+configuration file:
6006dc
+.TP
6006dc
+.B pipefs-directory
6006dc
+Tells
6006dc
+.B blkmapd
6006dc
+where to look for the rpc_pipefs filesystem.  The default value is
6006dc
+.IR /var/lib/nfs/rpc_pipefs .
6006dc
 .SH SEE ALSO
6006dc
 .BR nfs (5),
6006dc
-.BR dmsetup (8)
6006dc
+.BR dmsetup (8),
6006dc
+.BR nfs.conf (5)
6006dc
 .sp
6006dc
 RFC 5661 for the NFS version 4.1 specification.
6006dc
 .br
6006dc
diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c
6006dc
index b010628..a419947 100644
6006dc
--- a/utils/blkmapd/device-discovery.c
6006dc
+++ b/utils/blkmapd/device-discovery.c
6006dc
@@ -50,19 +50,36 @@
6006dc
 #include <errno.h>
6006dc
 #include <libdevmapper.h>
6006dc
 
6006dc
+#ifdef HAVE_CONFIG_H
6006dc
+#include "config.h"
6006dc
+#endif /* HAVE_CONFIG_H */
6006dc
+
6006dc
 #include "device-discovery.h"
6006dc
+#include "xcommon.h"
6006dc
+#include "nfslib.h"
6006dc
+#include "conffile.h"
6006dc
 
6006dc
 #define EVENT_SIZE (sizeof(struct inotify_event))
6006dc
 #define EVENT_BUFSIZE (1024 * EVENT_SIZE)
6006dc
 
6006dc
-#define BL_PIPE_FILE	"/var/lib/nfs/rpc_pipefs/nfs/blocklayout"
6006dc
-#define NFSPIPE_DIR	"/var/lib/nfs/rpc_pipefs/nfs"
6006dc
 #define RPCPIPE_DIR	"/var/lib/nfs/rpc_pipefs"
6006dc
 #define PID_FILE	"/var/run/blkmapd.pid"
6006dc
 
6006dc
+#define CONF_SAVE(w, f) do {			\
6006dc
+	char *p = f;				\
6006dc
+	if (p != NULL)				\
6006dc
+		(w) = p;			\
6006dc
+} while (0)
6006dc
+
6006dc
+static char bl_pipe_file[PATH_MAX];
6006dc
+static char nfspipe_dir[PATH_MAX];
6006dc
+static char rpcpipe_dir[PATH_MAX];
6006dc
+
6006dc
 struct bl_disk *visible_disk_list;
6006dc
 int    bl_watch_fd, bl_pipe_fd, nfs_pipedir_wfd, rpc_pipedir_wfd;
6006dc
 int    pidfd = -1;
6006dc
+char   *conf_path = NULL;
6006dc
+
6006dc
 
6006dc
 struct bl_disk_path *bl_get_path(const char *filepath,
6006dc
 				 struct bl_disk_path *paths)
6006dc
@@ -357,8 +374,8 @@ static void bl_rpcpipe_cb(void)
6006dc
 				continue;
6006dc
 			if (event->mask & IN_CREATE) {
6006dc
 				BL_LOG_WARNING("nfs pipe dir created\n");
6006dc
-				bl_watch_dir(NFSPIPE_DIR, &nfs_pipedir_wfd);
6006dc
-				bl_pipe_fd = open(BL_PIPE_FILE, O_RDWR);
6006dc
+				bl_watch_dir(nfspipe_dir, &nfs_pipedir_wfd);
6006dc
+				bl_pipe_fd = open(bl_pipe_file, O_RDWR);
6006dc
 			} else if (event->mask & IN_DELETE) {
6006dc
 				BL_LOG_WARNING("nfs pipe dir deleted\n");
6006dc
 				inotify_rm_watch(bl_watch_fd, nfs_pipedir_wfd);
6006dc
@@ -371,7 +388,7 @@ static void bl_rpcpipe_cb(void)
6006dc
 				continue;
6006dc
 			if (event->mask & IN_CREATE) {
6006dc
 				BL_LOG_WARNING("blocklayout pipe file created\n");
6006dc
-				bl_pipe_fd = open(BL_PIPE_FILE, O_RDWR);
6006dc
+				bl_pipe_fd = open(bl_pipe_file, O_RDWR);
6006dc
 				if (bl_pipe_fd < 0)
6006dc
 					BL_LOG_ERR("open %s failed: %s\n",
6006dc
 						event->name, strerror(errno));
6006dc
@@ -437,6 +454,19 @@ int main(int argc, char **argv)
6006dc
 	int opt, dflag = 0, fg = 0, ret = 1;
6006dc
 	struct stat statbuf;
6006dc
 	char pidbuf[64];
6006dc
+	char *xrpcpipe_dir = NULL;
6006dc
+
6006dc
+	strncpy(rpcpipe_dir, RPCPIPE_DIR, sizeof(rpcpipe_dir));
6006dc
+	conf_path = NFS_CONFFILE;
6006dc
+	conf_init();
6006dc
+	CONF_SAVE(xrpcpipe_dir, conf_get_str("general", "pipefs-directory"));
6006dc
+	if (xrpcpipe_dir != NULL)
6006dc
+		strlcpy(rpcpipe_dir, xrpcpipe_dir, sizeof(rpcpipe_dir));
6006dc
+
6006dc
+	strncpy(nfspipe_dir, rpcpipe_dir, sizeof(nfspipe_dir));
6006dc
+	strlcat(nfspipe_dir, "/nfs", sizeof(nfspipe_dir));
6006dc
+	strncpy(bl_pipe_file, rpcpipe_dir, sizeof(bl_pipe_file));
6006dc
+	strlcat(bl_pipe_file, "/nfs/blocklayout", sizeof(bl_pipe_file));
6006dc
 
6006dc
 	while ((opt = getopt(argc, argv, "hdf")) != -1) {
6006dc
 		switch (opt) {
6006dc
@@ -501,12 +531,12 @@ int main(int argc, char **argv)
6006dc
 	}
6006dc
 
6006dc
 	/* open pipe file */
6006dc
-	bl_watch_dir(RPCPIPE_DIR, &rpc_pipedir_wfd);
6006dc
-	bl_watch_dir(NFSPIPE_DIR, &nfs_pipedir_wfd);
6006dc
+	bl_watch_dir(rpcpipe_dir, &rpc_pipedir_wfd);
6006dc
+	bl_watch_dir(nfspipe_dir, &nfs_pipedir_wfd);
6006dc
 
6006dc
-	bl_pipe_fd = open(BL_PIPE_FILE, O_RDWR);
6006dc
+	bl_pipe_fd = open(bl_pipe_file, O_RDWR);
6006dc
 	if (bl_pipe_fd < 0)
6006dc
-		BL_LOG_ERR("open pipe file %s failed: %s\n", BL_PIPE_FILE, strerror(errno));
6006dc
+		BL_LOG_ERR("open pipe file %s failed: %s\n", bl_pipe_file, strerror(errno));
6006dc
 
6006dc
 	while (1) {
6006dc
 		/* discover device when needed */
6006dc
diff --git a/utils/gssd/gssd.c b/utils/gssd/gssd.c
6006dc
index c5c03fb..10d50d3 100644
6006dc
--- a/utils/gssd/gssd.c
6006dc
+++ b/utils/gssd/gssd.c
6006dc
@@ -857,6 +857,10 @@ read_gss_conf(void)
6006dc
 	s = conf_get_str("gssd", "pipefs-directory");
6006dc
 	if (!s)
6006dc
 		s = conf_get_str("general", "pipefs-directory");
6006dc
+	else
6006dc
+		printerr(0, "WARNING: Specifying pipefs-directory in the [gssd] "
6006dc
+			 "section of %s is deprecated.  Use the [general] "
6006dc
+			 "section instead.", NFS_CONFFILE);
6006dc
 	if (s)
6006dc
 		pipefs_path = s;
6006dc
 	s = conf_get_str("gssd", "keytab-file");
6006dc
diff --git a/utils/gssd/gssd.man b/utils/gssd/gssd.man
6006dc
index 87eef02..e620f0d 100644
6006dc
--- a/utils/gssd/gssd.man
6006dc
+++ b/utils/gssd/gssd.man
6006dc
@@ -335,10 +335,6 @@ Equivalent to
6006dc
 Equivalent to
6006dc
 .BR -t .
6006dc
 .TP
6006dc
-.B pipefs-directory
6006dc
-Equivalent to
6006dc
-.BR -p .
6006dc
-.TP
6006dc
 .B keytab-file
6006dc
 Equivalent to
6006dc
 .BR -k .
6006dc
@@ -350,6 +346,14 @@ Equivalent to
6006dc
 .B preferred-realm
6006dc
 Equivalent to
6006dc
 .BR -R .
6006dc
+.P
6006dc
+In addtion, the following value is recognized from the
6006dc
+.B [general]
6006dc
+section:
6006dc
+.TP
6006dc
+.B pipefs-directory
6006dc
+Equivalent to
6006dc
+.BR -p .
6006dc
 
6006dc
 .SH SEE ALSO
6006dc
 .BR rpc.svcgssd (8),
6006dc
diff --git a/utils/idmapd/idmapd.c b/utils/idmapd/idmapd.c
6006dc
index f55d2e1..03efbea 100644
6006dc
--- a/utils/idmapd/idmapd.c
6006dc
+++ b/utils/idmapd/idmapd.c
6006dc
@@ -166,7 +166,7 @@ static uid_t nobodyuid;
6006dc
 static gid_t nobodygid;
6006dc
 
6006dc
 /* Used by conffile.c in libnfs.a */
6006dc
-char *conf_path;
6006dc
+char *conf_path = NULL;
6006dc
 
6006dc
 static int
6006dc
 flush_nfsd_cache(char *path, time_t now)
6006dc
@@ -220,7 +220,6 @@ main(int argc, char **argv)
6006dc
 	int ret;
6006dc
 	char *progname;
6006dc
 
6006dc
-	conf_path = _PATH_IDMAPDCONF;
6006dc
 	nobodyuser = NFS4NOBODY_USER;
6006dc
 	nobodygroup = NFS4NOBODY_GROUP;
6006dc
 	strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
6006dc
@@ -234,8 +233,11 @@ main(int argc, char **argv)
6006dc
 #define GETOPTSTR "hvfd:p:U:G:c:CS"
6006dc
 	opterr=0; /* Turn off error messages */
6006dc
 	while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
6006dc
-		if (opt == 'c')
6006dc
+		if (opt == 'c') {
6006dc
+			warnx("-c is deprecated and may be removed in the "
6006dc
+			      "future.  See idmapd(8).");
6006dc
 			conf_path = optarg;
6006dc
+		}
6006dc
 		if (opt == '?') {
6006dc
 			if (strchr(GETOPTSTR, optopt))
6006dc
 				warnx("'-%c' option requires an argument.", optopt);
6006dc
@@ -247,17 +249,33 @@ main(int argc, char **argv)
6006dc
 	}
6006dc
 	optind = 1;
6006dc
 
6006dc
-	if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
6006dc
-		warn("Skipping configuration file \"%s\"", conf_path);
6006dc
-		conf_path = NULL;
6006dc
+	if (conf_path) { /* deprecated -c option was specified */
6006dc
+		if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
6006dc
+			warn("Skipping configuration file \"%s\"", conf_path);
6006dc
+			conf_path = NULL;
6006dc
+		} else {
6006dc
+			conf_init();
6006dc
+			verbose = conf_get_num("General", "Verbosity", 0);
6006dc
+			cache_entry_expiration = conf_get_num("General",
6006dc
+					"Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
6006dc
+			CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
6006dc
+			if (xpipefsdir != NULL)
6006dc
+				strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
6006dc
+			CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
6006dc
+			CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
6006dc
+		}
6006dc
 	} else {
6006dc
+		conf_path = NFS_CONFFILE;
6006dc
 		conf_init();
6006dc
-		verbose = conf_get_num("General", "Verbosity", 0);
6006dc
-		cache_entry_expiration = conf_get_num("General",
6006dc
-				"Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
6006dc
 		CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
6006dc
 		if (xpipefsdir != NULL)
6006dc
 			strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
6006dc
+
6006dc
+		conf_path = _PATH_IDMAPDCONF;
6006dc
+		conf_init();
6006dc
+		verbose = conf_get_num("General", "Verbosity", 0);
6006dc
+		cache_entry_expiration = conf_get_num("General",
6006dc
+				"cache-expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
6006dc
 		CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
6006dc
 		CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
6006dc
 	}
6006dc
diff --git a/utils/idmapd/idmapd.man b/utils/idmapd/idmapd.man
6006dc
index b9200c7..fb5fc1e 100644
6006dc
--- a/utils/idmapd/idmapd.man
6006dc
+++ b/utils/idmapd/idmapd.man
6006dc
@@ -50,11 +50,28 @@ The default value is \&"/var/lib/nfs/rpc_pipefs\&".
6006dc
 .It Fl c Ar path
6006dc
 Use configuration file
6006dc
 .Ar path .
6006dc
+This option is deprecated.
6006dc
 .It Fl C
6006dc
 Client-only: perform no idmapping for any NFS server, even if one is detected.
6006dc
 .It Fl S
6006dc
 Server-only: perform no idmapping for any NFS client, even if one is detected.
6006dc
 .El
6006dc
+.Sh CONFIGURATION FILES
6006dc
+.Nm
6006dc
+recognizes the following value from the
6006dc
+.Sy [general]
6006dc
+section of the
6006dc
+.Pa /etc/nfs.conf
6006dc
+configuration file:
6006dc
+.Bl -tag -width Ds_imagedir
6006dc
+.It Sy pipefs-directory
6006dc
+Equivalent to
6006dc
+.Sy -p .
6006dc
+.El
6006dc
+.Pp
6006dc
+All other settings related to id mapping are found in the
6006dc
+.Pa /etc/idmapd.conf
6006dc
+configuration file.
6006dc
 .Sh EXAMPLES
6006dc
 .Cm rpc.idmapd -f -vvv
6006dc
 .Pp
6006dc
@@ -71,9 +88,11 @@ messages to console, and with a verbosity level of 3.
6006dc
 .\" This next request is for sections 1, 6, 7 & 8 only.
6006dc
 .\" .Sh ENVIRONMENT
6006dc
 .Sh FILES
6006dc
-.Pa /etc/idmapd.conf
6006dc
+.Pa /etc/idmapd.conf ,
6006dc
+.Pa /etc/nfs.conf
6006dc
 .Sh SEE ALSO
6006dc
 .Xr idmapd.conf 5 ,
6006dc
+.Xr nfs.conf 5 ,
6006dc
 .Xr nfsidmap 8
6006dc
 .\".Sh SEE ALSO
6006dc
 .\".Xr nylon.conf 4
6006dc
diff --git a/utils/mountd/cache.c b/utils/mountd/cache.c
6006dc
index 3bc13ca..f23fb5a 100644
6006dc
--- a/utils/mountd/cache.c
6006dc
+++ b/utils/mountd/cache.c
6006dc
@@ -31,6 +31,7 @@
6006dc
 #include "mountd.h"
6006dc
 #include "fsloc.h"
6006dc
 #include "pseudoflavors.h"
6006dc
+#include "xcommon.h"
6006dc
 
6006dc
 #ifdef USE_BLKID
6006dc
 #include "blkid/blkid.h"