[sheepdog] [PATCH] lib/logger: nonblocking waitpid to avoid sheep process cannot exit

Ruoyu liangry at ucweb.com
Tue Jun 10 12:22:11 CEST 2014


Sometimes sheep process cannot exit as we expected. I think the
problem might be waitpid, the system call waiting for process to
change state.

Current log_close function calling waitpid as a void method. It is
better to retrieve the return value and pass the nonblocking flag,
that is WNOHANG, to it.

Signed-off-by: Ruoyu <liangry at ucweb.com>
---
 lib/logger.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/lib/logger.c b/lib/logger.c
index 6829f45..79c60a7 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -711,13 +711,25 @@ int log_init(const char *program_name, enum log_dst_type type, int level,
 
 void log_close(void)
 {
-	if (la) {
-		la->active = false;
-		waitpid(logger_pid, NULL, 0);
+	if (!la)
+		return;
 
-		syslog(LOG_WARNING, "logger pid %d stopped\n", logger_pid);
-		closelog();
-		free_logarea();
+	while (true) {
+		la->active = false;
+		pid_t pid = waitpid(logger_pid, NULL, WNOHANG);
+		if (pid == 0) {
+			usleep(100000);
+			continue;
+		} else if (pid > 0) {
+			syslog(LOG_WARNING, "logger pid %d stopped\n",
+					logger_pid);
+			closelog();
+			free_logarea();
+			break;
+		} else {
+			syslog(LOG_ERR, "waitpid() failure\n");
+			exit(1);
+		}
 	}
 }
 
-- 
1.8.3.2





More information about the sheepdog mailing list