1/* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ 2 3/* 4 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 33#include <sys/types.h> 34#ifdef HAVE_SYS_TIME_H 35#include <sys/time.h> 36#else 37#include <sys/_time.h> 38#endif 39#include <sys/queue.h> 40#include <sys/socket.h> 41#include <signal.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46#include <errno.h> 47#ifdef HAVE_FCNTL_H 48#include <fcntl.h> 49#endif 50 51#include "event.h" 52#include "evsignal.h" 53#include "log.h" 54 55extern struct event_list signalqueue; 56 57static sig_atomic_t evsigcaught[NSIG]; 58volatile sig_atomic_t evsignal_caught = 0; 59 60static struct event ev_signal; 61static int ev_signal_pair[2]; 62static int ev_signal_added; 63 64static void evsignal_handler(int sig); 65 66/* Callback for when the signal handler write a byte to our signaling socket */ 67static void 68evsignal_cb(int fd, short what, void *arg) 69{ 70 static char signals[100]; 71 struct event *ev = arg; 72 ssize_t n; 73 74 n = read(fd, signals, sizeof(signals)); 75 if (n == -1) 76 event_err(1, "%s: read", __func__); 77 event_add(ev, NULL); 78} 79 80#ifdef HAVE_SETFD 81#define FD_CLOSEONEXEC(x) do { \ 82 if (fcntl(x, F_SETFD, 1) == -1) \ 83 event_warn("fcntl(%d, F_SETFD)", x); \ 84} while (0) 85#else 86#define FD_CLOSEONEXEC(x) 87#endif 88 89void 90evsignal_init(void) 91{ 92 /* 93 * Our signal handler is going to write to one end of the socket 94 * pair to wake up our event loop. The event loop then scans for 95 * signals that got delivered. 96 */ 97 if (socketpair(AF_UNIX, SOCK_STREAM, 0, ev_signal_pair) == -1) 98 event_err(1, "%s: socketpair", __func__); 99 100 FD_CLOSEONEXEC(ev_signal_pair[0]); 101 FD_CLOSEONEXEC(ev_signal_pair[1]); 102 103 fcntl(ev_signal_pair[0], F_SETFL, O_NONBLOCK); 104 105 event_set(&ev_signal, ev_signal_pair[1], EV_READ, 106 evsignal_cb, &ev_signal); 107 ev_signal.ev_flags |= EVLIST_INTERNAL; 108} 109 110int 111evsignal_add(struct event *ev) 112{ 113 int evsignal; 114 struct sigaction sa; 115 116 if (ev->ev_events & (EV_READ|EV_WRITE)) 117 event_errx(1, "%s: EV_SIGNAL incompatible use", __func__); 118 evsignal = EVENT_SIGNAL(ev); 119 120 memset(&sa, 0, sizeof(sa)); 121 sa.sa_handler = evsignal_handler; 122 sigfillset(&sa.sa_mask); 123 sa.sa_flags |= SA_RESTART; 124 125 if (sigaction(evsignal, &sa, NULL) == -1) 126 return (-1); 127 128 if (!ev_signal_added) { 129 ev_signal_added = 1; 130 event_add(&ev_signal, NULL); 131 } 132 133 return (0); 134} 135 136/* 137 * Nothing to be done here. 138 */ 139 140int 141evsignal_del(struct event *ev) 142{ 143 int evsignal; 144 145 evsignal = EVENT_SIGNAL(ev); 146 147 return (sigaction(EVENT_SIGNAL(ev),(struct sigaction *)SIG_DFL, NULL)); 148} 149 150static void 151evsignal_handler(int sig) 152{ 153 int save_errno = errno; 154 155 evsigcaught[sig]++; 156 evsignal_caught = 1; 157 158 /* Wake up our notification mechanism */ 159 write(ev_signal_pair[0], "a", 1); 160 errno = save_errno; 161} 162 163void 164evsignal_process(void) 165{ 166 struct event *ev; 167 sig_atomic_t ncalls; 168 169 evsignal_caught = 0; 170 TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) { 171 ncalls = evsigcaught[EVENT_SIGNAL(ev)]; 172 if (ncalls) { 173 if (!(ev->ev_events & EV_PERSIST)) 174 event_del(ev); 175 event_active(ev, EV_SIGNAL, ncalls); 176 evsigcaught[EVENT_SIGNAL(ev)] = 0; 177 } 178 } 179} 180 181