1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
37 #include <sys/_time.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
55 extern struct event_list signalqueue;
57 static sig_atomic_t evsigcaught[NSIG];
58 volatile sig_atomic_t evsignal_caught = 0;
60 static struct event ev_signal;
61 static int ev_signal_pair[2];
62 static int ev_signal_added;
64 static void evsignal_handler(int sig);
66 /* Callback for when the signal handler write a byte to our signaling socket */
68 evsignal_cb(int fd, short what, void *arg)
70 static char signals[100];
71 struct event *ev = arg;
74 n = read(fd, signals, sizeof(signals));
76 event_err(1, "%s: read", __func__);
81 #define FD_CLOSEONEXEC(x) do { \
82 if (fcntl(x, F_SETFD, 1) == -1) \
83 event_warn("fcntl(%d, F_SETFD)", x); \
86 #define FD_CLOSEONEXEC(x)
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.
97 if (socketpair(AF_UNIX, SOCK_STREAM, 0, ev_signal_pair) == -1)
98 event_err(1, "%s: socketpair", __func__);
100 FD_CLOSEONEXEC(ev_signal_pair[0]);
101 FD_CLOSEONEXEC(ev_signal_pair[1]);
103 fcntl(ev_signal_pair[0], F_SETFL, O_NONBLOCK);
105 event_set(&ev_signal, ev_signal_pair[1], EV_READ,
106 evsignal_cb, &ev_signal);
107 ev_signal.ev_flags |= EVLIST_INTERNAL;
111 evsignal_add(struct event *ev)
116 if (ev->ev_events & (EV_READ|EV_WRITE))
117 event_errx(1, "%s: EV_SIGNAL incompatible use", __func__);
118 evsignal = EVENT_SIGNAL(ev);
120 memset(&sa, 0, sizeof(sa));
121 sa.sa_handler = evsignal_handler;
122 sigfillset(&sa.sa_mask);
123 sa.sa_flags |= SA_RESTART;
125 if (sigaction(evsignal, &sa, NULL) == -1)
128 if (!ev_signal_added) {
130 event_add(&ev_signal, NULL);
137 * Nothing to be done here.
141 evsignal_del(struct event *ev)
145 evsignal = EVENT_SIGNAL(ev);
147 return (sigaction(EVENT_SIGNAL(ev),(struct sigaction *)SIG_DFL, NULL));
151 evsignal_handler(int sig)
153 int save_errno = errno;
158 /* Wake up our notification mechanism */
159 write(ev_signal_pair[0], "a", 1);
164 evsignal_process(void)
170 TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) {
171 ncalls = evsigcaught[EVENT_SIGNAL(ev)];
173 if (!(ev->ev_events & EV_PERSIST))
175 event_active(ev, EV_SIGNAL, ncalls);
176 evsigcaught[EVENT_SIGNAL(ev)] = 0;