autofs-5.1.9 - remove mount point directory at umount if we created it

From: Ian Kent <raven@themaw.net>

If the (indirect) mount point directory was created by the daemon at
mount time then remove it at umount instead of at mount handler thread
cleanup.

If this is a submount then the umount itself will have discarded any
subdirectories so rm_unwanted() is not needed.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG          |    1 +
 daemon/automount.c |   21 ++-------------------
 daemon/indirect.c  |   11 +++++++++--
 lib/mounts.c       |   13 ++++++++++++-
 4 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 5400851b6..50aa7d480 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -96,6 +96,7 @@
 - quiet unset environment variable message on start.
 - improve local mount localion path resolution.
 - make open_mutex usage more consistent at process spawn.
+- remove mount point directory at umount if we created it.
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/automount.c b/daemon/automount.c
index a01d176ae..05fe8ac33 100644
--- a/daemon/automount.c
+++ b/daemon/automount.c
@@ -1778,20 +1778,11 @@ static void handle_mounts_cleanup(void *arg)
 {
 	struct master *master = master_get_master();
 	struct autofs_point *ap;
-	char buf[MAX_ERR_BUF];
-	unsigned int clean = 0, submount, logopt;
 	unsigned int pending = 0;
 
 	ap = (struct autofs_point *) arg;
 
-	logopt = ap->logopt;
-	submount = ap->submount;
-
-	if (!submount && strcmp(ap->path, "/-") &&
-	    ap->flags & MOUNT_FLAG_DIR_CREATED)
-		clean = 1;
-
-	if (submount) {
+	if (ap->submount) {
 		struct mnt_list *mnt;
 
 		/* Submount at ap->path belongs to parent submount list. */
@@ -1809,15 +1800,7 @@ static void handle_mounts_cleanup(void *arg)
 	if (!list_empty(&master->completed))
 		pending = 1;
 
-	info(logopt, "shut down path %s", ap->path);
-
-	if (clean) {
-		if (rmdir(ap->path) == -1) {
-			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
-			warn(logopt, "failed to remove dir %s: %s",
-			     ap->path, estr);
-		}
-	}
+	info(ap->logopt, "shut down path %s", ap->path);
 
 	master_remove_mapent(ap->entry);
 	master_source_unlock(ap->entry);
diff --git a/daemon/indirect.c b/daemon/indirect.c
index bb6377344..f8abaaa27 100644
--- a/daemon/indirect.c
+++ b/daemon/indirect.c
@@ -317,8 +317,15 @@ force_umount:
 		rv = umount2(ap->path, MNT_DETACH);
 	} else {
 		info(ap->logopt, "umounting indirect mount %s succeeded", ap->path);
-		if (ap->submount)
-			rm_unwanted(ap, ap->path, 1);
+		if (ap->flags & MOUNT_FLAG_DIR_CREATED) {
+			unsigned int browse = ap->flags & MOUNT_FLAG_GHOST;
+
+	                if (!browse && (rmdir(ap->path) == -1)) {
+				char buf[MAX_ERR_BUF];
+				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
+				warn(ap->logopt, "failed to remove dir %s: %s", ap->path, estr);
+			}
+		}
 	}
 
 	return rv;
diff --git a/lib/mounts.c b/lib/mounts.c
index f17bb12db..736f65f3d 100644
--- a/lib/mounts.c
+++ b/lib/mounts.c
@@ -3358,8 +3358,19 @@ int umount_ent(struct autofs_point *ap, const char *path)
 	}
 
 	/* If mount is gone remove it from mounted mounts list. */
-	if (!mounted)
+	if (!mounted) {
 		mnts_remove_mount(path, MNTS_MOUNTED);
+		if (ap->flags & MOUNT_FLAG_DIR_CREATED) {
+			unsigned int browse = ap->flags & MOUNT_FLAG_GHOST;
+
+			if (!browse && (rmdir(path) == -1)) {
+				char buf[MAX_ERR_BUF];
+				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
+				warn(ap->logopt,
+				     "failed to remove dir %s: %s", path, estr);
+			}
+		}
+	}
 
 	return mounted;
 }
