autofs-5.1.8 - be more careful with cmd pipe at exit

From: Ian Kent <raven@themaw.net>

It shouldn't be needed, as everthing should be shutdown by the time
the cmd pipe is destroyed, but be more careful at exit anyway.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG          |    1 +
 daemon/automount.c |   11 +++++++----
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 23efc053..a87f16eb 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -102,6 +102,7 @@
 - fix ldap_parse_page_control() check.
 - fix typo in create_cmd_pipe_fifo().
 - add null check in master_kill().
+- be more careful with cmd pipe at exit.
 
 19/10/2021 autofs-5.1.8
 - add xdr_exports().
diff --git a/daemon/automount.c b/daemon/automount.c
index c125e625..6cb3b1be 100644
--- a/daemon/automount.c
+++ b/daemon/automount.c
@@ -1622,19 +1622,18 @@ static void *cmd_pipe_handler(void *arg)
 
 	while (1) {
 		cmd_pipe_mutex_lock();
-		if (done) {
-			cmd_pipe_mutex_unlock();
+		if (done)
 			break;
-		}
 		cmd_pipe_mutex_unlock();
 
 		errno = 0;
 		if (ppoll(fds, pollfds, NULL, &signalset) == -1) {
 			if (errno == EINTR)
 				continue;
+			cmd_pipe_mutex_lock();
 			estr = strerror_r(errno, buf, MAX_ERR_BUF);
 			logerr("poll failed: %s", estr);
-			return NULL;
+			break;
 		}
 
 		if (fds[0].revents & POLLIN) {
@@ -1643,6 +1642,7 @@ static void *cmd_pipe_handler(void *arg)
 		}
 	}
 	destroy_cmd_pipe_fifo();
+	cmd_pipe_mutex_unlock();
 	return NULL;
 }
 
@@ -1673,8 +1673,11 @@ int start_cmd_pipe_handler(void)
 void finish_cmd_pipe_handler(void)
 {
 	cmd_pipe_mutex_lock();
+	if (cmd_pipe_thid == -1 || done)
+	       goto exit;
 	done = 1;
 	pthread_kill(cmd_pipe_thid, SIGPIPE);
+exit:
 	cmd_pipe_mutex_unlock();
 }