Blame SOURCES/0042-UP-fix-signal-handling.patch

f20720
---
f20720
 libmultipath/file.c        |    4 +-
f20720
 libmultipath/lock.c        |    9 ----
f20720
 libmultipath/lock.h        |    1 
f20720
 libmultipath/log_pthread.c |   22 -----------
f20720
 libmultipath/waiter.c      |    2 -
f20720
 multipathd/cli_handlers.c  |    4 +-
f20720
 multipathd/main.c          |   90 ++++++++++++++++++++-------------------------
f20720
 multipathd/main.h          |    3 +
f20720
 multipathd/uxlsnr.c        |   21 +++++++---
f20720
 multipathd/uxlsnr.h        |    3 +
f20720
 10 files changed, 65 insertions(+), 94 deletions(-)
f20720
f20720
Index: multipath-tools-130222/libmultipath/file.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/libmultipath/file.c
f20720
+++ multipath-tools-130222/libmultipath/file.c
f20720
@@ -98,7 +98,7 @@ lock_file(int fd, char *file_name)
f20720
 	sigaddset(&set, SIGALRM);
f20720
 
f20720
 	sigaction(SIGALRM, &act, &oldact);
f20720
-	sigprocmask(SIG_UNBLOCK, &set, &oldset);
f20720
+	pthread_sigmask(SIG_UNBLOCK, &set, &oldset);
f20720
 
f20720
 	alarm(FILE_TIMEOUT);
f20720
 	err = fcntl(fd, F_SETLKW, &lock);
f20720
@@ -112,7 +112,7 @@ lock_file(int fd, char *file_name)
f20720
 			condlog(0, "%s is locked. Giving up.", file_name);
f20720
 	}
f20720
 
f20720
-	sigprocmask(SIG_SETMASK, &oldset, NULL);
f20720
+	pthread_sigmask(SIG_SETMASK, &oldset, NULL);
f20720
 	sigaction(SIGALRM, &oldact, NULL);
f20720
 	return err;
f20720
 }
f20720
Index: multipath-tools-130222/libmultipath/lock.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/libmultipath/lock.c
f20720
+++ multipath-tools-130222/libmultipath/lock.c
f20720
@@ -1,16 +1,7 @@
f20720
 #include <pthread.h>
f20720
-#include <signal.h>
f20720
 #include "lock.h"
f20720
 #include <stdio.h>
f20720
 
f20720
-void block_signal (int signum, sigset_t *old)
f20720
-{
f20720
-	sigset_t set;
f20720
-	sigemptyset(&set);
f20720
-	sigaddset(&set, signum);
f20720
-	pthread_sigmask(SIG_BLOCK, &set, old);
f20720
-}
f20720
-
f20720
 void cleanup_lock (void * data)
f20720
 {
f20720
 	unlock ((*(struct mutex_lock *)data));
f20720
Index: multipath-tools-130222/libmultipath/lock.h
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/libmultipath/lock.h
f20720
+++ multipath-tools-130222/libmultipath/lock.h
f20720
@@ -29,6 +29,5 @@ struct mutex_lock {
f20720
 #endif
f20720
 
f20720
 void cleanup_lock (void * data);
f20720
-void block_signal(int signum, sigset_t *old);
f20720
 
f20720
 #endif /* _LOCK_H */
f20720
Index: multipath-tools-130222/libmultipath/log_pthread.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/libmultipath/log_pthread.c
f20720
+++ multipath-tools-130222/libmultipath/log_pthread.c
f20720
@@ -22,26 +22,13 @@ pthread_cond_t logev_cond;
f20720
 
f20720
 int logq_running;
f20720
 
f20720
-static void
f20720
-sigusr1 (int sig)
f20720
-{
f20720
-	pthread_mutex_lock(&logq_lock);
f20720
-	log_reset("multipathd");
f20720
-	pthread_mutex_unlock(&logq_lock);
f20720
-}
f20720
-
f20720
 void log_safe (int prio, const char * fmt, va_list ap)
f20720
 {
f20720
-	sigset_t old;
f20720
-
f20720
 	if (log_thr == (pthread_t)0) {
f20720
 		syslog(prio, fmt, ap);
f20720
 		return;
f20720
 	}
f20720
 
f20720
-	block_signal(SIGUSR1, &old;;
f20720
-	block_signal(SIGHUP, NULL);
f20720
-
f20720
 	pthread_mutex_lock(&logq_lock);
f20720
 	log_enqueue(prio, fmt, ap);
f20720
 	pthread_mutex_unlock(&logq_lock);
f20720
@@ -49,8 +36,6 @@ void log_safe (int prio, const char * fm
f20720
 	pthread_mutex_lock(&logev_lock);
f20720
 	pthread_cond_signal(&logev_cond);
f20720
 	pthread_mutex_unlock(&logev_lock);
f20720
-
f20720
-	pthread_sigmask(SIG_SETMASK, &old, NULL);
f20720
 }
f20720
 
f20720
 void log_thread_flush (void)
f20720
@@ -81,15 +66,8 @@ static void flush_logqueue (void)
f20720
 
f20720
 static void * log_thread (void * et)
f20720
 {
f20720
-	struct sigaction sig;
f20720
 	int running;
f20720
 
f20720
-	sig.sa_handler = sigusr1;
f20720
-	sigemptyset(&sig.sa_mask);
f20720
-	sig.sa_flags = 0;
f20720
-	if (sigaction(SIGUSR1, &sig, NULL) < 0)
f20720
-		logdbg(stderr, "Cannot set signal handler");
f20720
-
f20720
 	pthread_mutex_lock(&logev_lock);
f20720
 	logq_running = 1;
f20720
 	pthread_mutex_unlock(&logev_lock);
f20720
Index: multipath-tools-130222/libmultipath/waiter.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/libmultipath/waiter.c
f20720
+++ multipath-tools-130222/libmultipath/waiter.c
f20720
@@ -157,8 +157,6 @@ void *waitevent (void *et)
f20720
 	waiter = (struct event_thread *)et;
f20720
 	pthread_cleanup_push(free_waiter, et);
f20720
 
f20720
-	block_signal(SIGUSR1, NULL);
f20720
-	block_signal(SIGHUP, NULL);
f20720
 	while (1) {
f20720
 		r = waiteventloop(waiter);
f20720
 
f20720
Index: multipath-tools-130222/multipathd/cli_handlers.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/multipathd/cli_handlers.c
f20720
+++ multipath-tools-130222/multipathd/cli_handlers.c
f20720
@@ -939,8 +939,8 @@ int
f20720
 cli_shutdown (void * v, char ** reply, int * len, void * data)
f20720
 {
f20720
 	condlog(3, "shutdown (operator)");
f20720
-
f20720
-	return exit_daemon(0);
f20720
+	exit_daemon();
f20720
+	return 0;
f20720
 }
f20720
 
f20720
 int
f20720
Index: multipath-tools-130222/multipathd/main.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/multipathd/main.c
f20720
+++ multipath-tools-130222/multipathd/main.c
f20720
@@ -17,6 +17,7 @@
f20720
 #include <limits.h>
f20720
 #include <linux/oom.h>
f20720
 #include <libudev.h>
f20720
+#include <semaphore.h>
f20720
 #include <mpath_persist.h>
f20720
 
f20720
 /*
f20720
@@ -52,6 +53,7 @@
f20720
 #include <wwids.h>
f20720
 #include <pgpolicies.h>
f20720
 #include <uevent.h>
f20720
+#include <log.h>
f20720
 
f20720
 #include "main.h"
f20720
 #include "pidfile.h"
f20720
@@ -81,13 +83,11 @@ struct mpath_event_param
f20720
 
f20720
 unsigned int mpath_mx_alloc_len;
f20720
 
f20720
-pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
f20720
-pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
f20720
-
f20720
 int logsink;
f20720
 enum daemon_status running_state;
f20720
 pid_t daemon_pid;
f20720
 
f20720
+static sem_t exit_sem;
f20720
 /*
f20720
  * global copy of vecs for use in sig handlers
f20720
  */
f20720
@@ -838,9 +838,6 @@ out:
f20720
 static void *
f20720
 ueventloop (void * ap)
f20720
 {
f20720
-	block_signal(SIGUSR1, NULL);
f20720
-	block_signal(SIGHUP, NULL);
f20720
-
f20720
 	if (uevent_listen())
f20720
 		condlog(0, "error starting uevent listener");
f20720
 
f20720
@@ -850,9 +847,6 @@ ueventloop (void * ap)
f20720
 static void *
f20720
 uevqloop (void * ap)
f20720
 {
f20720
-	block_signal(SIGUSR1, NULL);
f20720
-	block_signal(SIGHUP, NULL);
f20720
-
f20720
 	if (uevent_dispatch(&uev_trigger, ap))
f20720
 		condlog(0, "error starting uevent dispatcher");
f20720
 
f20720
@@ -861,9 +855,6 @@ uevqloop (void * ap)
f20720
 static void *
f20720
 uxlsnrloop (void * ap)
f20720
 {
f20720
-	block_signal(SIGUSR1, NULL);
f20720
-	block_signal(SIGHUP, NULL);
f20720
-
f20720
 	if (cli_init())
f20720
 		return NULL;
f20720
 
f20720
@@ -913,18 +904,10 @@ uxlsnrloop (void * ap)
f20720
 	return NULL;
f20720
 }
f20720
 
f20720
-int
f20720
-exit_daemon (int status)
f20720
+void
f20720
+exit_daemon (void)
f20720
 {
f20720
-	if (status != 0)
f20720
-		fprintf(stderr, "bad exit status. see daemon.log\n");
f20720
-
f20720
-	if (running_state != DAEMON_SHUTDOWN) {
f20720
-		pthread_mutex_lock(&exit_mutex);
f20720
-		pthread_cond_signal(&exit_cond);
f20720
-		pthread_mutex_unlock(&exit_mutex);
f20720
-	}
f20720
-	return status;
f20720
+	sem_post(&exit_sem);
f20720
 }
f20720
 
f20720
 const char *
f20720
@@ -1287,7 +1270,6 @@ checkerloop (void *ap)
f20720
 	struct path *pp;
f20720
 	int count = 0;
f20720
 	unsigned int i;
f20720
-	sigset_t old;
f20720
 
f20720
 	mlockall(MCL_CURRENT | MCL_FUTURE);
f20720
 	vecs = (struct vectors *)ap;
f20720
@@ -1301,7 +1283,6 @@ checkerloop (void *ap)
f20720
 	}
f20720
 
f20720
 	while (1) {
f20720
-		block_signal(SIGHUP, &old;;
f20720
 		pthread_cleanup_push(cleanup_lock, &vecs->lock);
f20720
 		lock(vecs->lock);
f20720
 		pthread_testcancel();
f20720
@@ -1325,7 +1306,6 @@ checkerloop (void *ap)
f20720
 		}
f20720
 
f20720
 		lock_cleanup_pop(vecs->lock);
f20720
-		pthread_sigmask(SIG_SETMASK, &old, NULL);
f20720
 		sleep(1);
f20720
 	}
f20720
 	return NULL;
f20720
@@ -1485,36 +1465,56 @@ signal_set(int signo, void (*func) (int)
f20720
 		return (osig.sa_handler);
f20720
 }
f20720
 
f20720
+void
f20720
+handle_signals(void)
f20720
+{
f20720
+	if (reconfig_sig && running_state == DAEMON_RUNNING) {
f20720
+		condlog(2, "reconfigure (signal)");
f20720
+		pthread_cleanup_push(cleanup_lock,
f20720
+				&gvecs->lock);
f20720
+		lock(gvecs->lock);
f20720
+		pthread_testcancel();
f20720
+		reconfigure(gvecs);
f20720
+		lock_cleanup_pop(gvecs->lock);
f20720
+	}
f20720
+	if (log_reset_sig) {
f20720
+		condlog(2, "reset log (signal)");
f20720
+		pthread_mutex_lock(&logq_lock);
f20720
+		log_reset("multipathd");
f20720
+		pthread_mutex_unlock(&logq_lock);
f20720
+	}
f20720
+	reconfig_sig = 0;
f20720
+	log_reset_sig = 0;
f20720
+}
f20720
+
f20720
 static void
f20720
 sighup (int sig)
f20720
 {
f20720
-	condlog(2, "reconfigure (SIGHUP)");
f20720
-
f20720
-	if (running_state != DAEMON_RUNNING)
f20720
-		return;
f20720
-
f20720
-	reconfigure(gvecs);
f20720
-
f20720
-#ifdef _DEBUG_
f20720
-	dbg_free_final(NULL);
f20720
-#endif
f20720
+	reconfig_sig = 1;
f20720
 }
f20720
 
f20720
 static void
f20720
 sigend (int sig)
f20720
 {
f20720
-	exit_daemon(0);
f20720
+	exit_daemon();
f20720
 }
f20720
 
f20720
 static void
f20720
 sigusr1 (int sig)
f20720
 {
f20720
-	condlog(3, "SIGUSR1 received");
f20720
+	log_reset_sig = 1;
f20720
 }
f20720
 
f20720
 static void
f20720
 signal_init(void)
f20720
 {
f20720
+	sigset_t set;
f20720
+
f20720
+	sigemptyset(&set);
f20720
+	sigaddset(&set, SIGHUP);
f20720
+	sigaddset(&set, SIGUSR1);
f20720
+	pthread_sigmask(SIG_BLOCK, &set, NULL);
f20720
+
f20720
 	signal_set(SIGHUP, sighup);
f20720
 	signal_set(SIGUSR1, sigusr1);
f20720
 	signal_set(SIGINT, sigend);
f20720
@@ -1587,10 +1587,11 @@ child (void * param)
f20720
 	struct vectors * vecs;
f20720
 	struct multipath * mpp;
f20720
 	int i;
f20720
-	sigset_t set;
f20720
 	int rc, pid_rc;
f20720
 
f20720
 	mlockall(MCL_CURRENT | MCL_FUTURE);
f20720
+	sem_init(&exit_sem, 0, 0);
f20720
+	signal_init();
f20720
 
f20720
 	setup_thread_attr(&misc_attr, 64 * 1024, 1);
f20720
 	setup_thread_attr(&waiter_attr, 32 * 1024, 1);
f20720
@@ -1650,7 +1651,6 @@ child (void * param)
f20720
 	if (!vecs)
f20720
 		exit(1);
f20720
 
f20720
-	signal_init();
f20720
 	setscheduler();
f20720
 	set_oom_adj();
f20720
 
f20720
@@ -1693,25 +1693,17 @@ child (void * param)
f20720
 	}
f20720
 	pthread_attr_destroy(&misc_attr);
f20720
 
f20720
-	pthread_mutex_lock(&exit_mutex);
f20720
 	/* Startup complete, create logfile */
f20720
 	pid_rc = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
f20720
 	/* Ignore errors, we can live without */
f20720
 
f20720
 	running_state = DAEMON_RUNNING;
f20720
-	pthread_cond_wait(&exit_cond, &exit_mutex);
f20720
-	/* Need to block these to avoid deadlocking */
f20720
-	sigemptyset(&set);
f20720
-	sigaddset(&set, SIGTERM);
f20720
-	sigaddset(&set, SIGINT);
f20720
-	pthread_sigmask(SIG_BLOCK, &set, NULL);
f20720
 
f20720
 	/*
f20720
 	 * exit path
f20720
 	 */
f20720
+	while(sem_wait(&exit_sem) != 0); /* Do nothing */
f20720
 	running_state = DAEMON_SHUTDOWN;
f20720
-	pthread_sigmask(SIG_UNBLOCK, &set, NULL);
f20720
-	block_signal(SIGHUP, NULL);
f20720
 	lock(vecs->lock);
f20720
 	if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
f20720
 		vector_foreach_slot(vecs->mpvec, mpp, i)
f20720
Index: multipath-tools-130222/multipathd/main.h
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/multipathd/main.h
f20720
+++ multipath-tools-130222/multipathd/main.h
f20720
@@ -16,7 +16,7 @@ struct prin_resp;
f20720
 
f20720
 extern pid_t daemon_pid;
f20720
 
f20720
-int exit_daemon(int);
f20720
+void exit_daemon(void);
f20720
 const char * daemon_status(void);
f20720
 int reconfigure (struct vectors *);
f20720
 int ev_add_path (struct path *, struct vectors *);
f20720
@@ -35,5 +35,6 @@ int mpath_pr_event_handle(struct path *p
f20720
 void * mpath_pr_event_handler_fn (void * );
f20720
 int update_map_pr(struct multipath *mpp);
f20720
 void * mpath_pr_event_handler_fn (void * pathp );
f20720
+void handle_signals(void);
f20720
 
f20720
 #endif /* MAIN_H */
f20720
Index: multipath-tools-130222/multipathd/uxlsnr.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/multipathd/uxlsnr.c
f20720
+++ multipath-tools-130222/multipathd/uxlsnr.c
f20720
@@ -8,6 +8,7 @@
f20720
 /*
f20720
  * A simple domain socket listener
f20720
  */
f20720
+#define _GNU_SOURCE
f20720
 #include <stdio.h>
f20720
 #include <stdlib.h>
f20720
 #include <unistd.h>
f20720
@@ -19,20 +20,21 @@
f20720
 #include <sys/socket.h>
f20720
 #include <sys/un.h>
f20720
 #include <sys/poll.h>
f20720
-
f20720
+#include <signal.h>
f20720
 #include <checkers.h>
f20720
-
f20720
 #include <memory.h>
f20720
 #include <debug.h>
f20720
 #include <vector.h>
f20720
 #include <structs.h>
f20720
+#include <structs_vec.h>
f20720
 #include <uxsock.h>
f20720
 #include <defaults.h>
f20720
 
f20720
+#include "main.h"
f20720
 #include "cli.h"
f20720
 #include "uxlsnr.h"
f20720
 
f20720
-#define SLEEP_TIME 5000
f20720
+struct timespec sleep_time = {5, 0};
f20720
 
f20720
 struct client {
f20720
 	int fd;
f20720
@@ -42,6 +44,8 @@ struct client {
f20720
 static struct client *clients;
f20720
 static unsigned num_clients;
f20720
 struct pollfd *polls;
f20720
+volatile sig_atomic_t reconfig_sig = 0;
f20720
+volatile sig_atomic_t log_reset_sig = 0;
f20720
 
f20720
 /*
f20720
  * handle a new client joining
f20720
@@ -104,6 +108,7 @@ void * uxsock_listen(int (*uxsock_trigge
f20720
 	int rlen;
f20720
 	char *inbuf;
f20720
 	char *reply;
f20720
+	sigset_t mask;
f20720
 
f20720
 	ux_sock = ux_socket_listen(DEFAULT_SOCKET);
f20720
 
f20720
@@ -115,7 +120,9 @@ void * uxsock_listen(int (*uxsock_trigge
f20720
 	pthread_cleanup_push(uxsock_cleanup, NULL);
f20720
 
f20720
 	polls = (struct pollfd *)MALLOC(0);
f20720
-
f20720
+	pthread_sigmask(SIG_SETMASK, NULL, &mask);
f20720
+	sigdelset(&mask, SIGHUP);
f20720
+	sigdelset(&mask, SIGUSR1);
f20720
 	while (1) {
f20720
 		struct client *c;
f20720
 		int i, poll_count;
f20720
@@ -132,11 +139,13 @@ void * uxsock_listen(int (*uxsock_trigge
f20720
 		}
f20720
 
f20720
 		/* most of our life is spent in this call */
f20720
-		poll_count = poll(polls, i, SLEEP_TIME);
f20720
+		poll_count = ppoll(polls, i, &sleep_time, &mask);
f20720
 
f20720
 		if (poll_count == -1) {
f20720
-			if (errno == EINTR)
f20720
+			if (errno == EINTR) {
f20720
+				handle_signals();
f20720
 				continue;
f20720
+			}
f20720
 
f20720
 			/* something went badly wrong! */
f20720
 			condlog(0, "poll");
f20720
Index: multipath-tools-130222/multipathd/uxlsnr.h
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/multipathd/uxlsnr.h
f20720
+++ multipath-tools-130222/multipathd/uxlsnr.h
f20720
@@ -4,5 +4,8 @@
f20720
 void * uxsock_listen(int (*uxsock_trigger)
f20720
 			(char *, char **, int *, void *),
f20720
 			void * trigger_data);
f20720
+
f20720
+extern volatile sig_atomic_t reconfig_sig;
f20720
+extern volatile sig_atomic_t log_reset_sig;
f20720
 #endif
f20720