From d6ef125c4be83de1d94727bf6be74cd7c0bf424c Mon Sep 17 00:00:00 2001
From: Pavel Raiskup <praiskup@redhat.com>
Date: Wed, 3 Dec 2014 17:10:11 -0500
Subject: [PATCH] exportfs: warn when really nothing is exported

Throw 'No file systems exported!' iff no volume is exported rather
then if some exports file is empty.  Typically this can happen if
the default /etc/exports file is empty and admin installed
configuration into /etc/exports.d directory.

This is follow-up for e725def62c73b4 commit.

Signed-off-by: Pavel Raiskup <praiskup@redhat.com>
Signed-off-by: Steve Dickson <steved@redhat.com>
---
 support/export/export.c        |   34 ++++++++++++++--------------------
 support/include/exportfs.h     |    4 ++--
 systemd/nfs-server-generator.c |    4 ++--
 utils/exportfs/exportfs.c      |    7 +++++--
 4 files changed, 23 insertions(+), 26 deletions(-)

--- a/support/export/export.c
+++ b/support/export/export.c
@@ -79,55 +79,49 @@ export_avoid_host_lookup(int i)
  * export_read - read entries from /etc/exports
  * @fname: name of file to read from
  *
+ * Returns number of read entries.
  */
-void
-export_read(char *fname, int verbose)
+int
+export_read(char *fname)
 {
 	struct exportent	*eep;
 	nfs_export		*exp;
 
 	int volumes = 0;
-	int bad_entry = 0;
 
 	setexportent(fname, "r");
 	while ((eep = getexportent(0,1)) != NULL) {
 		exp = export_lookup(eep->e_hostname, eep->e_path, assume_canonical);
 		if (!exp) {
-			exp = export_create(eep, assume_canonical);
-			if (exp)
+			if (export_create(eep, assume_canonical))
+				/*possible complaints already logged */
 				volumes++;
-			else
-				bad_entry++;
 		}
 		else
 			warn_duplicated_exports(exp, eep);
 	}
 	endexportent();
-	if (volumes == 0) {
-		if (bad_entry > 0)
-			xlog(L_WARNING, "No file systems exported!");
-		else if (verbose)
-			xlog(L_WARNING, "No file systems exported!");
-	}
+
+	return volumes;
 }
 
 /* Based on mnt_table_parse_dir() in
    util-linux-ng/shlibs/mount/src/tab_parse.c */
-void
-export_d_read(const char *dname, int verbose)
+int
+export_d_read(const char *dname)
 {
 	int n = 0, i;
 	struct dirent **namelist = NULL;
-
+	int volumes = 0;
 
 	n = scandir(dname, &namelist, NULL, versionsort);
 	if (n < 0) {
 		if (errno == ENOENT)
 			/* Silently return */
-			return;
+			return volumes;
 		xlog(L_NOTICE, "scandir %s: %s", dname, strerror(errno));
 	} else if (n == 0)
-		return;
+		return volumes;
 
 	for (i = 0; i < n; i++) {
 		struct dirent *d = namelist[i];
@@ -157,14 +151,14 @@ export_d_read(const char *dname, int ver
 			continue;
 		}
 
-		export_read(fname, verbose);
+		volumes += export_read(fname);
 	}
 
 	for (i = 0; i < n; i++)
 		free(namelist[i]);
 	free(namelist);
 
-	return;
+	return volumes;
 }
 
 /**
--- a/support/include/exportfs.h
+++ b/support/include/exportfs.h
@@ -134,8 +134,8 @@ int 				client_member(const char *client
 						const char *name);
 
 void				export_avoid_host_lookup(int i);
-void				export_read(char *fname, int verbose);
-void				export_d_read(const char *dname, int verbose);
+int				export_read(char *fname);
+int				export_d_read(const char *dname);
 void				export_reset(nfs_export *);
 nfs_export *			export_lookup(char *hname, char *path, int caconical);
 nfs_export *			export_find(const struct addrinfo *ai,
--- a/systemd/nfs-server-generator.c
+++ b/systemd/nfs-server-generator.c
@@ -128,8 +128,8 @@ int main(int argc, char *argv[])
 	path = malloc(strlen(argv[1]) + sizeof(dirbase) + sizeof(filebase));
 	if (!path)
 		exit(2);
-	export_read(_PATH_EXPORTS, 0);
-	export_d_read(_PATH_EXPORTS_D, 0);
+	export_read(_PATH_EXPORTS);
+	export_d_read(_PATH_EXPORTS_D);
 
 	strcat(strcpy(path, argv[1]), dirbase);
 	mkdir(path, 0755);
--- a/utils/exportfs/exportfs.c
+++ b/utils/exportfs/exportfs.c
@@ -187,8 +187,11 @@ main(int argc, char **argv)
 			 * to be reliably run before the network is fully up.
 			 */
 			export_avoid_host_lookup(1);
-		export_read(_PATH_EXPORTS, f_verbose);
-		export_d_read(_PATH_EXPORTS_D, f_verbose);
+		if (! (export_read(_PATH_EXPORTS) +
+		       export_d_read(_PATH_EXPORTS_D))) {
+			if (f_verbose)
+				xlog(L_WARNING, "No file systems exported!");
+		}
 	}
 	if (f_export) {
 		if (f_all)
