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

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