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

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