1 /* $OpenBSD: sig.c,v 1.13 2011/06/03 23:34:56 deraadt Exp $ */ 2 /* $NetBSD: sig.c,v 1.15 2009/02/19 15:20:22 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Christos Zoulas of Cornell University. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 22 * 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 33 * SUCH DAMAGE. 34 */ 35 36 #include "config.h" 37 38 /* 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. 42 */ 43 #include "el.h" 44 #include <stdlib.h> 45 46 private EditLine *sel = NULL; 47 48 private const int sighdl[] = { 49 #define _DO(a) (a), 50 ALLSIGS 51 #undef _DO 52 - 1 53 }; 54 55 private void sig_handler(int); 56 57 /* sig_handler(): 58 * This is the handler called for all signals 59 * XXX: we cannot pass any data so we just store the old editline 60 * state in a private variable 61 */ 62 private void 63 sig_handler(int signo) 64 { 65 int i, save_errno = errno; 66 sigset_t nset, oset; 67 68 (void) sigemptyset(&nset); 69 (void) sigaddset(&nset, signo); 70 (void) sigprocmask(SIG_BLOCK, &nset, &oset); 71 72 sel->el_signal->sig_no = signo; 73 74 switch (signo) { 75 case SIGCONT: 76 tty_rawmode(sel); 77 if (ed_redisplay(sel, 0) == CC_REFRESH) 78 re_refresh(sel); 79 term__flush(sel); 80 break; 81 82 case SIGWINCH: 83 el_resize(sel); 84 break; 85 86 default: 87 tty_cookedmode(sel); 88 break; 89 } 90 91 for (i = 0; sighdl[i] != -1; i++) 92 if (signo == sighdl[i]) 93 break; 94 95 (void) sigaction(signo, &sel->el_signal->sig_action[i], NULL); 96 sel->el_signal->sig_action[i].sa_handler = SIG_ERR; 97 sel->el_signal->sig_action[i].sa_flags = 0; 98 sigemptyset(&sel->el_signal->sig_action[i].sa_mask); 99 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 100 (void) kill(0, signo); 101 errno = save_errno; 102 } 103 104 105 /* sig_init(): 106 * Initialize all signal stuff 107 */ 108 protected int 109 sig_init(EditLine *el) 110 { 111 size_t i; 112 sigset_t *nset, oset; 113 114 el->el_signal = el_malloc(sizeof(*el->el_signal)); 115 if (el->el_signal == NULL) 116 return -1; 117 118 nset = &el->el_signal->sig_set; 119 (void) sigemptyset(nset); 120 #define _DO(a) (void) sigaddset(nset, a); 121 ALLSIGS 122 #undef _DO 123 (void) sigprocmask(SIG_BLOCK, nset, &oset); 124 125 for (i = 0; sighdl[i] != -1; i++) { 126 el->el_signal->sig_action[i].sa_handler = SIG_ERR; 127 el->el_signal->sig_action[i].sa_flags = 0; 128 sigemptyset(&el->el_signal->sig_action[i].sa_mask); 129 } 130 131 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 132 133 return 0; 134 } 135 136 137 /* sig_end(): 138 * Clear all signal stuff 139 */ 140 protected void 141 sig_end(EditLine *el) 142 { 143 144 el_free((ptr_t) el->el_signal); 145 el->el_signal = NULL; 146 } 147 148 149 /* sig_set(): 150 * set all the signal handlers 151 */ 152 protected void 153 sig_set(EditLine *el) 154 { 155 size_t i; 156 sigset_t oset; 157 struct sigaction osa, nsa; 158 159 nsa.sa_handler = sig_handler; 160 nsa.sa_flags = 0; 161 sigemptyset(&nsa.sa_mask); 162 163 (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset); 164 165 for (i = 0; sighdl[i] != -1; i++) { 166 /* This could happen if we get interrupted */ 167 if (sigaction(sighdl[i], &nsa, &osa) != -1 && 168 osa.sa_handler != sig_handler) 169 el->el_signal->sig_action[i] = osa; 170 } 171 sel = el; 172 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 173 } 174 175 176 /* sig_clr(): 177 * clear all the signal handlers 178 */ 179 protected void 180 sig_clr(EditLine *el) 181 { 182 size_t i; 183 sigset_t oset; 184 185 (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset); 186 187 for (i = 0; sighdl[i] != -1; i++) 188 if (el->el_signal->sig_action[i].sa_handler != SIG_ERR) 189 (void)sigaction(sighdl[i], 190 &el->el_signal->sig_action[i], NULL); 191 192 sel = NULL; /* we are going to die if the handler is 193 * called */ 194 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 195 } 196