1 /* $OpenBSD: sig.c,v 1.19 2016/04/11 21:17:29 schwarze Exp $ */
2 /* $NetBSD: sig.c,v 1.25 2016/04/11 18:56:31 christos Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * sig.c: Signal handling stuff.
40 * our policy is to trap all signals, set a good state
41 * and pass the ball to our caller.
49 static EditLine
*sel
= NULL
;
51 static const int sighdl
[] = {
58 static void sig_handler(int);
61 * This is the handler called for all signals
62 * XXX: we cannot pass any data so we just store the old editline
63 * state in a private variable
66 sig_handler(int signo
)
72 (void) sigemptyset(&nset
);
73 (void) sigaddset(&nset
, signo
);
74 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
76 sel
->el_signal
->sig_no
= signo
;
81 if (ed_redisplay(sel
, 0) == CC_REFRESH
)
95 for (i
= 0; sighdl
[i
] != -1; i
++)
96 if (signo
== sighdl
[i
])
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
);
110 * Initialize all signal stuff
113 sig_init(EditLine
*el
)
116 sigset_t
*nset
, oset
;
118 el
->el_signal
= malloc(sizeof(*el
->el_signal
));
119 if (el
->el_signal
== NULL
)
122 nset
= &el
->el_signal
->sig_set
;
123 (void) sigemptyset(nset
);
124 #define _DO(a) (void) sigaddset(nset, a);
127 (void) sigprocmask(SIG_BLOCK
, nset
, &oset
);
129 for (i
= 0; sighdl
[i
] != -1; i
++) {
130 el
->el_signal
->sig_action
[i
].sa_handler
= SIG_ERR
;
131 el
->el_signal
->sig_action
[i
].sa_flags
= 0;
132 sigemptyset(&el
->el_signal
->sig_action
[i
].sa_mask
);
135 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
142 * Clear all signal stuff
145 sig_end(EditLine
*el
)
149 el
->el_signal
= NULL
;
154 * set all the signal handlers
157 sig_set(EditLine
*el
)
161 struct sigaction osa
, nsa
;
163 nsa
.sa_handler
= sig_handler
;
165 sigemptyset(&nsa
.sa_mask
);
167 (void) sigprocmask(SIG_BLOCK
, &el
->el_signal
->sig_set
, &oset
);
169 for (i
= 0; sighdl
[i
] != -1; i
++) {
170 /* This could happen if we get interrupted */
171 if (sigaction(sighdl
[i
], &nsa
, &osa
) != -1 &&
172 osa
.sa_handler
!= sig_handler
)
173 el
->el_signal
->sig_action
[i
] = osa
;
176 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
181 * clear all the signal handlers
184 sig_clr(EditLine
*el
)
189 (void) sigprocmask(SIG_BLOCK
, &el
->el_signal
->sig_set
, &oset
);
191 for (i
= 0; sighdl
[i
] != -1; i
++)
192 if (el
->el_signal
->sig_action
[i
].sa_handler
!= SIG_ERR
)
193 (void)sigaction(sighdl
[i
],
194 &el
->el_signal
->sig_action
[i
], NULL
);
196 sel
= NULL
; /* we are going to die if the handler is
198 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);