xref: /minix3/external/bsd/tmux/dist/signal.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /* Id */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5  * Copyright (c) 2010 Romain Francoise <rfrancoise@debian.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <string.h>
21 #include <signal.h>
22 
23 #include "tmux.h"
24 
25 struct event	ev_sighup;
26 struct event	ev_sigchld;
27 struct event	ev_sigcont;
28 struct event	ev_sigterm;
29 struct event	ev_sigusr1;
30 struct event	ev_sigwinch;
31 
32 void
set_signals(void (* handler)(int,short,unused void *))33 set_signals(void(*handler)(int, short, unused void *))
34 {
35 	struct sigaction	sigact;
36 
37 	memset(&sigact, 0, sizeof sigact);
38 	sigemptyset(&sigact.sa_mask);
39 	sigact.sa_flags = SA_RESTART;
40 	sigact.sa_handler = SIG_IGN;
41 	if (sigaction(SIGINT, &sigact, NULL) != 0)
42 		fatal("sigaction failed");
43 	if (sigaction(SIGPIPE, &sigact, NULL) != 0)
44 		fatal("sigaction failed");
45 	if (sigaction(SIGUSR2, &sigact, NULL) != 0)
46 		fatal("sigaction failed");
47 	if (sigaction(SIGTSTP, &sigact, NULL) != 0)
48 		fatal("sigaction failed");
49 
50 	signal_set(&ev_sighup, SIGHUP, handler, NULL);
51 	signal_add(&ev_sighup, NULL);
52 	signal_set(&ev_sigchld, SIGCHLD, handler, NULL);
53 	signal_add(&ev_sigchld, NULL);
54 	signal_set(&ev_sigcont, SIGCONT, handler, NULL);
55 	signal_add(&ev_sigcont, NULL);
56 	signal_set(&ev_sigterm, SIGTERM, handler, NULL);
57 	signal_add(&ev_sigterm, NULL);
58 	signal_set(&ev_sigusr1, SIGUSR1, handler, NULL);
59 	signal_add(&ev_sigusr1, NULL);
60 	signal_set(&ev_sigwinch, SIGWINCH, handler, NULL);
61 	signal_add(&ev_sigwinch, NULL);
62 }
63 
64 void
clear_signals(int after_fork)65 clear_signals(int after_fork)
66 {
67 	struct sigaction	sigact;
68 
69 	memset(&sigact, 0, sizeof sigact);
70 	sigemptyset(&sigact.sa_mask);
71 	sigact.sa_flags = SA_RESTART;
72 	sigact.sa_handler = SIG_DFL;
73 	if (sigaction(SIGINT, &sigact, NULL) != 0)
74 		fatal("sigaction failed");
75 	if (sigaction(SIGPIPE, &sigact, NULL) != 0)
76 		fatal("sigaction failed");
77 	if (sigaction(SIGUSR2, &sigact, NULL) != 0)
78 		fatal("sigaction failed");
79 	if (sigaction(SIGTSTP, &sigact, NULL) != 0)
80 		fatal("sigaction failed");
81 
82 	if (after_fork) {
83 		if (sigaction(SIGHUP, &sigact, NULL) != 0)
84 			fatal("sigaction failed");
85 		if (sigaction(SIGCHLD, &sigact, NULL) != 0)
86 			fatal("sigaction failed");
87 		if (sigaction(SIGCONT, &sigact, NULL) != 0)
88 			fatal("sigaction failed");
89 		if (sigaction(SIGTERM, &sigact, NULL) != 0)
90 			fatal("sigaction failed");
91 		if (sigaction(SIGUSR1, &sigact, NULL) != 0)
92 			fatal("sigaction failed");
93 		if (sigaction(SIGWINCH, &sigact, NULL) != 0)
94 			fatal("sigaction failed");
95 	} else {
96 		event_del(&ev_sighup);
97 		event_del(&ev_sigchld);
98 		event_del(&ev_sigcont);
99 		event_del(&ev_sigterm);
100 		event_del(&ev_sigusr1);
101 		event_del(&ev_sigwinch);
102 	}
103 }
104