1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Christos Zoulas of Cornell University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	$NetBSD: sig.c,v 1.15 2009/02/19 15:20:22 christos Exp $
33  */
34 
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)sig.c	8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: stable/10/lib/libedit/sig.c 238378 2012-07-11 22:20:19Z pfg $");
40 
41 /*
42  * sig.c: Signal handling stuff.
43  *	  our policy is to trap all signals, set a good state
44  *	  and pass the ball to our caller.
45  */
46 #include "sys.h"
47 #include "el.h"
48 #include <stdlib.h>
49 
50 private EditLine *sel = NULL;
51 
52 private const int sighdl[] = {
53 #define	_DO(a)	(a),
54 	ALLSIGS
55 #undef	_DO
56 	- 1
57 };
58 
59 private void sig_handler(int);
60 
61 /* sig_handler():
62  *	This is the handler called for all signals
63  *	XXX: we cannot pass any data so we just store the old editline
64  *	state in a private variable
65  */
66 private void
sig_handler(int signo)67 sig_handler(int signo)
68 {
69 	int i;
70 	sigset_t nset, oset;
71 
72 	(void) sigemptyset(&nset);
73 	(void) sigaddset(&nset, signo);
74 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
75 
76 	sel->el_signal->sig_no = signo;
77 
78 	switch (signo) {
79 	case SIGCONT:
80 		tty_rawmode(sel);
81 		if (ed_redisplay(sel, 0) == CC_REFRESH)
82 			re_refresh(sel);
83 		term__flush(sel);
84 		break;
85 
86 	case SIGWINCH:
87 		el_resize(sel);
88 		break;
89 
90 	default:
91 		tty_cookedmode(sel);
92 		break;
93 	}
94 
95 	for (i = 0; sighdl[i] != -1; i++)
96 		if (signo == sighdl[i])
97 			break;
98 
99 	(void) sigaction(signo, &sel->el_signal->sig_action[i], NULL);
100 	sel->el_signal->sig_action[i].sa_handler = SIG_ERR;
101 	sel->el_signal->sig_action[i].sa_flags = 0;
102 	sigemptyset(&sel->el_signal->sig_action[i].sa_mask);
103 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
104 	(void) kill(0, signo);
105 }
106 
107 
108 /* sig_init():
109  *	Initialize all signal stuff
110  */
111 protected int
sig_init(EditLine * el)112 sig_init(EditLine *el)
113 {
114 	size_t i;
115 	sigset_t *nset, oset;
116 
117 	el->el_signal = el_malloc(sizeof(*el->el_signal));
118 	if (el->el_signal == NULL)
119 		return -1;
120 
121 	nset = &el->el_signal->sig_set;
122 	(void) sigemptyset(nset);
123 #define	_DO(a) (void) sigaddset(nset, a);
124 	ALLSIGS
125 #undef	_DO
126 	(void) sigprocmask(SIG_BLOCK, nset, &oset);
127 
128 	for (i = 0; sighdl[i] != -1; i++) {
129 		el->el_signal->sig_action[i].sa_handler = SIG_ERR;
130 		el->el_signal->sig_action[i].sa_flags = 0;
131 		sigemptyset(&el->el_signal->sig_action[i].sa_mask);
132 	}
133 
134 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
135 
136 	return 0;
137 }
138 
139 
140 /* sig_end():
141  *	Clear all signal stuff
142  */
143 protected void
sig_end(EditLine * el)144 sig_end(EditLine *el)
145 {
146 
147 	el_free((ptr_t) el->el_signal);
148 	el->el_signal = NULL;
149 }
150 
151 
152 /* sig_set():
153  *	set all the signal handlers
154  */
155 protected void
sig_set(EditLine * el)156 sig_set(EditLine *el)
157 {
158 	size_t i;
159 	sigset_t oset;
160 	struct sigaction osa, nsa;
161 
162 	nsa.sa_handler = sig_handler;
163 	nsa.sa_flags = 0;
164 	sigemptyset(&nsa.sa_mask);
165 
166 	(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
167 
168 	for (i = 0; sighdl[i] != -1; i++) {
169 		/* This could happen if we get interrupted */
170 		if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
171 		    osa.sa_handler != sig_handler)
172 			el->el_signal->sig_action[i] = osa;
173 	}
174 	sel = el;
175 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
176 }
177 
178 
179 /* sig_clr():
180  *	clear all the signal handlers
181  */
182 protected void
sig_clr(EditLine * el)183 sig_clr(EditLine *el)
184 {
185 	size_t i;
186 	sigset_t oset;
187 
188 	(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
189 
190 	for (i = 0; sighdl[i] != -1; i++)
191 		if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
192 			(void)sigaction(sighdl[i],
193 			    &el->el_signal->sig_action[i], NULL);
194 
195 	sel = NULL;		/* we are going to die if the handler is
196 				 * called */
197 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
198 }
199