]> granicus.if.org Git - sudo/blob - lib/util/event_select.c
a7336331d0a83c46456aedc863177577ad87f0bb
[sudo] / lib / util / event_select.c
1 /*
2  * Copyright (c) 2013-2015 Todd C. Miller <Todd.Miller@sudo.ws>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /*
18  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
19  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
20  */
21
22 #include <config.h>
23
24 #include <sys/param.h>          /* for howmany() on Linux */
25 #include <sys/time.h>
26 #ifdef HAVE_SYS_SYSMACROS_H
27 # include <sys/sysmacros.h>     /* for howmany() on Solaris */
28 #endif
29 #ifdef HAVE_SYS_SELECT_H
30 # include <sys/select.h>
31 #endif /* HAVE_SYS_SELECT_H */
32 #include <stdio.h>
33 #include <stdlib.h>
34 #ifdef HAVE_STDBOOL_H
35 # include <stdbool.h>
36 #else
37 # include "compat/stdbool.h"
38 #endif /* HAVE_STDBOOL_H */
39 #ifdef HAVE_STRING_H
40 # include <string.h>
41 #endif /* HAVE_STRING_H */
42 #ifdef HAVE_STRINGS_H
43 # include <strings.h>
44 #endif /* HAVE_STRINGS_H */
45 #include <time.h>
46 #include <unistd.h>
47 #include <errno.h>
48
49 #include "sudo_compat.h"
50 #include "sudo_util.h"
51 #include "sudo_fatal.h"
52 #include "sudo_debug.h"
53 #include "sudo_event.h"
54
55 int
56 sudo_ev_base_alloc_impl(struct sudo_event_base *base)
57 {
58     debug_decl(sudo_ev_base_alloc_impl, SUDO_DEBUG_EVENT)
59
60     base->maxfd = NFDBITS - 1;
61     base->readfds_in = calloc(1, sizeof(fd_mask));
62     base->writefds_in = calloc(1, sizeof(fd_mask));
63     base->readfds_out = calloc(1, sizeof(fd_mask));
64     base->writefds_out = calloc(1, sizeof(fd_mask));
65
66     if (base->readfds_in == NULL || base->writefds_in == NULL ||
67         base->readfds_out == NULL || base->writefds_out == NULL) {
68         sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
69             "%s: unable to calloc(1, %zu)", __func__, sizeof(fd_mask));
70         sudo_ev_base_free_impl(base);
71         debug_return_int(-1);
72     }
73     debug_return_int(0);
74 }
75
76 void
77 sudo_ev_base_free_impl(struct sudo_event_base *base)
78 {
79     debug_decl(sudo_ev_base_free_impl, SUDO_DEBUG_EVENT)
80     free(base->readfds_in);
81     free(base->writefds_in);
82     free(base->readfds_out);
83     free(base->writefds_out);
84     debug_return;
85 }
86
87 int
88 sudo_ev_add_impl(struct sudo_event_base *base, struct sudo_event *ev)
89 {
90     debug_decl(sudo_ev_add_impl, SUDO_DEBUG_EVENT)
91
92     /* If out of space in fd sets, realloc. */
93     if (ev->fd > base->maxfd) {
94         const int o = (base->maxfd + 1) / NFDBITS;
95         const int n = howmany(ev->fd + 1, NFDBITS);
96         const size_t used_bytes = o * sizeof(fd_mask);
97         const size_t new_bytes = (n - o) * sizeof(fd_mask);
98         fd_set *rfds_in, *wfds_in, *rfds_out, *wfds_out;
99
100         rfds_in = reallocarray(base->readfds_in, n, sizeof(fd_mask));
101         wfds_in = reallocarray(base->writefds_in, n, sizeof(fd_mask));
102         rfds_out = reallocarray(base->readfds_out, n, sizeof(fd_mask));
103         wfds_out = reallocarray(base->writefds_out, n, sizeof(fd_mask));
104         if (rfds_in == NULL || wfds_in == NULL ||
105             rfds_out == NULL || wfds_out == NULL) {
106             sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
107                 "%s: unable to reallocarray(%d, %zu)",
108                 __func__, n, sizeof(fd_mask));
109             free(rfds_in);
110             free(wfds_in);
111             free(rfds_out);
112             free(wfds_out);
113             debug_return_int(-1);
114         }
115
116         /* Clear newly allocated space. */
117         memset((char *)rfds_in + used_bytes, 0, new_bytes);
118         memset((char *)wfds_in + used_bytes, 0, new_bytes);
119         memset((char *)rfds_out + used_bytes, 0, new_bytes);
120         memset((char *)wfds_out + used_bytes, 0, new_bytes);
121
122         /* Update base. */
123         base->readfds_in = rfds_in;
124         base->writefds_in = wfds_in;
125         base->readfds_out = rfds_out;
126         base->writefds_out = wfds_out;
127         base->maxfd = (n * NFDBITS) - 1;
128     }
129
130     /* Set events and adjust high fd as needed. */
131     if (ISSET(ev->events, SUDO_EV_READ)) {
132         sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: added fd %d to readfs",
133             __func__, ev->fd);
134         FD_SET(ev->fd, base->readfds_in);
135     }
136     if (ISSET(ev->events, SUDO_EV_WRITE)) {
137         sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: added fd %d to writefds",
138             __func__, ev->fd);
139         FD_SET(ev->fd, base->writefds_in);
140     }
141     if (ev->fd > base->highfd)
142         base->highfd = ev->fd;
143
144     debug_return_int(0);
145 }
146
147 int
148 sudo_ev_del_impl(struct sudo_event_base *base, struct sudo_event *ev)
149 {
150     debug_decl(sudo_ev_del_impl, SUDO_DEBUG_EVENT)
151
152     /* Remove from readfds and writefds and adjust high fd. */
153     if (ISSET(ev->events, SUDO_EV_READ)) {
154         sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: removed fd %d from readfds",
155             __func__, ev->fd);
156         FD_CLR(ev->fd, base->readfds_in);
157     }
158     if (ISSET(ev->events, SUDO_EV_WRITE)) {
159         sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: removed fd %d from writefds",
160             __func__, ev->fd);
161         FD_CLR(ev->fd, base->writefds_in);
162     }
163     if (base->highfd == ev->fd) {
164         for (;;) {
165             if (FD_ISSET(base->highfd, base->readfds_in) ||
166                 FD_ISSET(base->highfd, base->writefds_in))
167                 break;
168             if (--base->highfd < 0)
169                 break;
170         }
171     }
172
173     debug_return_int(0);
174 }
175
176 #ifdef HAVE_PSELECT
177 static int
178 sudo_ev_select(int nfds, fd_set *readfds, fd_set *writefds,
179     fd_set *exceptfds, const struct timespec *timeout)
180 {
181     return pselect(nfds, readfds, writefds, exceptfds, timeout, NULL);
182 }
183 #else
184 static int
185 sudo_ev_select(int nfds, fd_set *readfds, fd_set *writefds,
186     fd_set *exceptfds, const struct timespec *timeout)
187 {
188     struct timeval tvbuf, *tv = NULL;
189
190     if (timeout != NULL) {
191         TIMESPEC_TO_TIMEVAL(&tvbuf, timeout);
192         tv = &tvbuf;
193     }
194     return select(nfds, readfds, writefds, exceptfds, tv);
195 }
196 #endif /* HAVE_PSELECT */
197
198 int
199 sudo_ev_scan_impl(struct sudo_event_base *base, int flags)
200 {
201     struct timespec now, ts, *timeout;
202     struct sudo_event *ev;
203     size_t setsize;
204     int nready;
205     debug_decl(sudo_ev_loop, SUDO_DEBUG_EVENT)
206
207     if ((ev = TAILQ_FIRST(&base->timeouts)) != NULL) {
208         sudo_gettime_mono(&now);
209         sudo_timespecsub(&ev->timeout, &now, &ts);
210         if (ts.tv_sec < 0)
211             sudo_timespecclear(&ts);
212         timeout = &ts;
213     } else {
214         if (ISSET(flags, SUDO_EVLOOP_NONBLOCK)) {
215             sudo_timespecclear(&ts);
216             timeout = &ts;
217         } else {
218             timeout = NULL;
219         }
220     }
221
222     /* select() overwrites readfds/writefds so make a copy. */
223     setsize = howmany(base->highfd + 1, NFDBITS) * sizeof(fd_mask);
224     memcpy(base->readfds_out, base->readfds_in, setsize);
225     memcpy(base->writefds_out, base->writefds_in, setsize);
226
227     sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: select high fd %d",
228         __func__, base->highfd);
229     nready = sudo_ev_select(base->highfd + 1, base->readfds_out,
230         base->writefds_out, NULL, timeout);
231     sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %d fds ready", __func__, nready);
232     switch (nready) {
233     case -1:
234         /* Error or interrupted by signal. */
235         debug_return_int(-1);
236     case 0:
237         /* Front end will activate timeout events. */
238         break;
239     default:
240         /* Activate each I/O event that fired. */
241         TAILQ_FOREACH(ev, &base->events, entries) {
242             if (ev->fd >= 0) {
243                 int what = 0;
244                 if (FD_ISSET(ev->fd, base->readfds_out))
245                     what |= (ev->events & SUDO_EV_READ);
246                 if (FD_ISSET(ev->fd, base->writefds_out))
247                     what |= (ev->events & SUDO_EV_WRITE);
248                 if (what != 0) {
249                     /* Make event active. */
250                     sudo_debug_printf(SUDO_DEBUG_DEBUG,
251                         "%s: selected fd %d, events %d, activating %p",
252                         __func__, ev->fd, what, ev);
253                     ev->revents = what;
254                     sudo_ev_activate(base, ev);
255                 }
256             }
257         }
258         break;
259     }
260     debug_return_int(nready);
261 }