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>
35 #ifdef HAVE_SYS_TIME_H
38 #include <sys/_time.h>
40 #include <sys/queue.h>
41 #include <sys/socket.h>
54 #include "event-internal.h"
58 struct event_base
*evsignal_base
= NULL
;
60 static void evsignal_handler(int sig
);
62 /* Callback for when the signal handler write a byte to our signaling socket */
64 evsignal_cb(int fd
, short what
, void *arg
)
66 static char signals
[100];
67 struct event
*ev
= arg
;
70 n
= read(fd
, signals
, sizeof(signals
));
72 event_err(1, "%s: read", __func__
);
77 #define FD_CLOSEONEXEC(x) do { \
78 if (fcntl(x, F_SETFD, 1) == -1) \
79 event_warn("fcntl(%d, F_SETFD)", x); \
82 #define FD_CLOSEONEXEC(x)
86 evsignal_init(struct event_base
*base
)
89 * Our signal handler is going to write to one end of the socket
90 * pair to wake up our event loop. The event loop then scans for
91 * signals that got delivered.
93 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, base
->sig
.ev_signal_pair
) == -1)
94 event_err(1, "%s: socketpair", __func__
);
96 FD_CLOSEONEXEC(base
->sig
.ev_signal_pair
[0]);
97 FD_CLOSEONEXEC(base
->sig
.ev_signal_pair
[1]);
98 base
->sig
.evsignal_caught
= 0;
99 memset(&base
->sig
.evsigcaught
, 0, sizeof(sig_atomic_t)*NSIG
);
101 fcntl(base
->sig
.ev_signal_pair
[0], F_SETFL
, O_NONBLOCK
);
103 event_set(&base
->sig
.ev_signal
, base
->sig
.ev_signal_pair
[1], EV_READ
,
104 evsignal_cb
, &base
->sig
.ev_signal
);
105 base
->sig
.ev_signal
.ev_base
= base
;
106 base
->sig
.ev_signal
.ev_flags
|= EVLIST_INTERNAL
;
110 evsignal_add(struct event
*ev
)
114 struct event_base
*base
= ev
->ev_base
;
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
;
124 /* catch signals if they happen quickly */
125 evsignal_base
= base
;
127 if (sigaction(evsignal
, &sa
, NULL
) == -1)
130 if (!base
->sig
.ev_signal_added
) {
131 base
->sig
.ev_signal_added
= 1;
132 event_add(&base
->sig
.ev_signal
, NULL
);
139 evsignal_del(struct event
*ev
)
141 return (sigaction(EVENT_SIGNAL(ev
),(struct sigaction
*)SIG_DFL
, NULL
));
145 evsignal_handler(int sig
)
147 int save_errno
= errno
;
149 if(evsignal_base
== NULL
) {
151 "%s: received signal %s, but have no base configured",
156 evsignal_base
->sig
.evsigcaught
[sig
]++;
157 evsignal_base
->sig
.evsignal_caught
= 1;
159 /* Wake up our notification mechanism */
160 write(evsignal_base
->sig
.ev_signal_pair
[0], "a", 1);
165 evsignal_process(struct event_base
*base
)
170 base
->sig
.evsignal_caught
= 0;
171 TAILQ_FOREACH(ev
, &base
->sig
.signalqueue
, ev_signal_next
) {
172 ncalls
= base
->sig
.evsigcaught
[EVENT_SIGNAL(ev
)];
174 if (!(ev
->ev_events
& EV_PERSIST
))
176 event_active(ev
, EV_SIGNAL
, ncalls
);
177 base
->sig
.evsigcaught
[EVENT_SIGNAL(ev
)] = 0;
183 evsignal_dealloc(struct event_base
*base
)
185 if(base
->sig
.ev_signal_added
) {
186 event_del(&base
->sig
.ev_signal
);
187 base
->sig
.ev_signal_added
= 0;
189 assert(TAILQ_EMPTY(&base
->sig
.signalqueue
));
191 close(base
->sig
.ev_signal_pair
[0]);
192 base
->sig
.ev_signal_pair
[0] = -1;
193 close(base
->sig
.ev_signal_pair
[1]);
194 base
->sig
.ev_signal_pair
[1] = -1;