summaryrefslogtreecommitdiffstats
path: root/0042-UP-fix-signal-handling.patch
blob: 4b4b40bf33ffa0370a418f325a7a3715ad53a00d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
---
 libmultipath/file.c        |    4 +-
 libmultipath/lock.c        |    9 ----
 libmultipath/lock.h        |    1 
 libmultipath/log_pthread.c |   22 -----------
 libmultipath/waiter.c      |    2 -
 multipathd/cli_handlers.c  |    4 +-
 multipathd/main.c          |   90 ++++++++++++++++++++-------------------------
 multipathd/main.h          |    3 +
 multipathd/uxlsnr.c        |   21 +++++++---
 multipathd/uxlsnr.h        |    3 +
 10 files changed, 65 insertions(+), 94 deletions(-)

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