Coverage Report

Created: 2025-07-11 06:57

/src/libevent/poll.c
Line
Count
Source (jump to first uncovered line)
1
/*  $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
2
3
/*
4
 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5
 * Copyright 2007-2012 Niels Provos and Nick Mathewson
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
#include "event2/event-config.h"
30
#include "evconfig-private.h"
31
32
#ifdef EVENT__HAVE_POLL
33
34
#include <sys/types.h>
35
#ifdef EVENT__HAVE_SYS_TIME_H
36
#include <sys/time.h>
37
#endif
38
#include <sys/queue.h>
39
#include <poll.h>
40
#include <signal.h>
41
#include <limits.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <unistd.h>
46
#include <errno.h>
47
48
#include "event-internal.h"
49
#include "evsignal-internal.h"
50
#include "log-internal.h"
51
#include "evmap-internal.h"
52
#include "event2/thread.h"
53
#include "evthread-internal.h"
54
#include "time-internal.h"
55
56
/* Since Linux 2.6.17, poll is able to report about peer half-closed connection
57
   using special POLLRDHUP flag on a read event.
58
*/
59
#if !defined(POLLRDHUP)
60
#define POLLRDHUP 0
61
#define EARLY_CLOSE_IF_HAVE_RDHUP 0
62
#else
63
#define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
64
#endif
65
66
67
struct pollidx {
68
  int idxplus1;
69
};
70
71
struct pollop {
72
  int event_count;    /* Highest number alloc */
73
  int nfds;     /* Highest number used */
74
  int realloc_copy;   /* True iff we must realloc
75
           * event_set_copy */
76
  struct pollfd *event_set;
77
  struct pollfd *event_set_copy;
78
};
79
80
static void *poll_init(struct event_base *);
81
static int poll_add(struct event_base *, int, short old, short events, void *idx);
82
static int poll_del(struct event_base *, int, short old, short events, void *idx);
83
static int poll_dispatch(struct event_base *, struct timeval *);
84
static void poll_dealloc(struct event_base *);
85
86
const struct eventop pollops = {
87
  "poll",
88
  poll_init,
89
  poll_add,
90
  poll_del,
91
  poll_dispatch,
92
  poll_dealloc,
93
  1, /* need_reinit */
94
  EV_FEATURE_FDS|EARLY_CLOSE_IF_HAVE_RDHUP,
95
  sizeof(struct pollidx),
96
};
97
98
static void *
99
poll_init(struct event_base *base)
100
0
{
101
0
  struct pollop *pollop;
102
103
0
  if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
104
0
    return (NULL);
105
106
0
  if (sigfd_init_(base) < 0)
107
0
    evsig_init_(base);
108
109
0
  evutil_weakrand_seed_(&base->weakrand_seed, 0);
110
111
0
  return (pollop);
112
0
}
113
114
#ifdef CHECK_INVARIANTS
115
static void
116
poll_check_ok(struct pollop *pop)
117
{
118
  int i, idx;
119
  struct event *ev;
120
121
  for (i = 0; i < pop->fd_count; ++i) {
122
    idx = pop->idxplus1_by_fd[i]-1;
123
    if (idx < 0)
124
      continue;
125
    EVUTIL_ASSERT(pop->event_set[idx].fd == i);
126
  }
127
  for (i = 0; i < pop->nfds; ++i) {
128
    struct pollfd *pfd = &pop->event_set[i];
129
    EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1);
130
  }
131
}
132
#else
133
#define poll_check_ok(pop)
134
#endif
135
136
static int
137
poll_dispatch(struct event_base *base, struct timeval *tv)
138
0
{
139
0
  int res, i, j, nfds;
140
0
  long msec = -1;
141
0
  struct pollop *pop = base->evbase;
142
0
  struct pollfd *event_set;
143
144
0
  poll_check_ok(pop);
145
146
0
  nfds = pop->nfds;
147
148
0
#ifndef EVENT__DISABLE_THREAD_SUPPORT
149
0
  if (base->th_base_lock) {
150
    /* If we're using this backend in a multithreaded setting,
151
     * then we need to work on a copy of event_set, so that we can
152
     * let other threads modify the main event_set while we're
153
     * polling. If we're not multithreaded, then we'll skip the
154
     * copy step here to save memory and time. */
155
0
    if (pop->realloc_copy) {
156
0
      struct pollfd *tmp = mm_realloc(pop->event_set_copy,
157
0
          pop->event_count * sizeof(struct pollfd));
158
0
      if (tmp == NULL) {
159
0
        event_warn("realloc");
160
0
        return -1;
161
0
      }
162
0
      pop->event_set_copy = tmp;
163
0
      pop->realloc_copy = 0;
164
0
    }
165
0
    memcpy(pop->event_set_copy, pop->event_set,
166
0
        sizeof(struct pollfd)*nfds);
167
0
    event_set = pop->event_set_copy;
168
0
  } else {
169
0
    event_set = pop->event_set;
170
0
  }
171
#else
172
  event_set = pop->event_set;
173
#endif
174
175
0
  if (tv != NULL) {
176
0
    msec = evutil_tv_to_msec_(tv);
177
0
    if (msec < 0 || msec > INT_MAX)
178
0
      msec = INT_MAX;
179
0
  }
180
181
0
  EVBASE_RELEASE_LOCK(base, th_base_lock);
182
183
0
  res = poll(event_set, nfds, msec);
184
185
0
  EVBASE_ACQUIRE_LOCK(base, th_base_lock);
186
187
0
  if (res == -1) {
188
0
    if (errno != EINTR) {
189
0
      event_warn("poll");
190
0
      return (-1);
191
0
    }
192
193
0
    return (0);
194
0
  }
195
196
0
  event_debug(("%s: poll reports %d", __func__, res));
197
198
0
  if (res == 0 || nfds == 0)
199
0
    return (0);
200
201
0
  i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
202
0
  for (j = 0; j < nfds; j++) {
203
0
    int what;
204
0
    if (++i == nfds)
205
0
      i = 0;
206
0
    what = event_set[i].revents;
207
0
    if (!what)
208
0
      continue;
209
210
0
    res = 0;
211
212
    /* If the file gets closed notify */
213
0
    if (what & (POLLHUP|POLLERR|POLLNVAL))
214
0
      what |= POLLIN|POLLOUT;
215
0
    if (what & POLLIN)
216
0
      res |= EV_READ;
217
0
    if (what & POLLOUT)
218
0
      res |= EV_WRITE;
219
0
    if (what & POLLRDHUP)
220
0
      res |= EV_CLOSED;
221
0
    if (res == 0)
222
0
      continue;
223
224
0
    evmap_io_active_(base, event_set[i].fd, res);
225
0
  }
226
227
0
  return (0);
228
0
}
229
230
static int
231
poll_add(struct event_base *base, int fd, short old, short events, void *idx_)
232
0
{
233
0
  struct pollop *pop = base->evbase;
234
0
  struct pollfd *pfd = NULL;
235
0
  struct pollidx *idx = idx_;
236
0
  int i;
237
238
0
  EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
239
0
  if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
240
0
    return (0);
241
242
0
  poll_check_ok(pop);
243
0
  if (pop->nfds + 1 >= pop->event_count) {
244
0
    struct pollfd *tmp_event_set;
245
0
    int tmp_event_count;
246
247
0
    if (pop->event_count < 32)
248
0
      tmp_event_count = 32;
249
0
    else
250
0
      tmp_event_count = pop->event_count * 2;
251
252
    /* We need more file descriptors */
253
0
    tmp_event_set = mm_realloc(pop->event_set,
254
0
         tmp_event_count * sizeof(struct pollfd));
255
0
    if (tmp_event_set == NULL) {
256
0
      event_warn("realloc");
257
0
      return (-1);
258
0
    }
259
0
    pop->event_set = tmp_event_set;
260
261
0
    pop->event_count = tmp_event_count;
262
0
    pop->realloc_copy = 1;
263
0
  }
264
265
0
  i = idx->idxplus1 - 1;
266
267
0
  if (i >= 0) {
268
0
    pfd = &pop->event_set[i];
269
0
  } else {
270
0
    i = pop->nfds++;
271
0
    pfd = &pop->event_set[i];
272
0
    pfd->events = 0;
273
0
    pfd->fd = fd;
274
0
    idx->idxplus1 = i + 1;
275
0
  }
276
277
0
  pfd->revents = 0;
278
0
  if (events & EV_WRITE)
279
0
    pfd->events |= POLLOUT;
280
0
  if (events & EV_READ)
281
0
    pfd->events |= POLLIN;
282
0
  if (events & EV_CLOSED)
283
0
    pfd->events |= POLLRDHUP;
284
0
  poll_check_ok(pop);
285
286
0
  return (0);
287
0
}
288
289
/*
290
 * Nothing to be done here.
291
 */
292
293
static int
294
poll_del(struct event_base *base, int fd, short old, short events, void *idx_)
295
0
{
296
0
  struct pollop *pop = base->evbase;
297
0
  struct pollfd *pfd = NULL;
298
0
  struct pollidx *idx = idx_;
299
0
  int i;
300
301
0
  EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
302
0
  if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
303
0
    return (0);
304
305
0
  poll_check_ok(pop);
306
0
  i = idx->idxplus1 - 1;
307
0
  if (i < 0)
308
0
    return (-1);
309
310
  /* Do we still want to read or write? */
311
0
  pfd = &pop->event_set[i];
312
0
  if (events & EV_READ)
313
0
    pfd->events &= ~POLLIN;
314
0
  if (events & EV_WRITE)
315
0
    pfd->events &= ~POLLOUT;
316
0
  if (events & EV_CLOSED)
317
0
    pfd->events &= ~POLLRDHUP;
318
0
  poll_check_ok(pop);
319
0
  if (pfd->events)
320
    /* Another event cares about that fd. */
321
0
    return (0);
322
323
  /* Okay, so we aren't interested in that fd anymore. */
324
0
  idx->idxplus1 = 0;
325
326
0
  --pop->nfds;
327
0
  if (i != pop->nfds) {
328
    /*
329
     * Shift the last pollfd down into the now-unoccupied
330
     * position.
331
     */
332
0
    memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
333
0
           sizeof(struct pollfd));
334
0
    idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd);
335
0
    EVUTIL_ASSERT(idx);
336
0
    EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
337
0
    idx->idxplus1 = i + 1;
338
0
  }
339
340
0
  poll_check_ok(pop);
341
0
  return (0);
342
0
}
343
344
static void
345
poll_dealloc(struct event_base *base)
346
0
{
347
0
  struct pollop *pop = base->evbase;
348
349
0
  evsig_dealloc_(base);
350
0
  if (pop->event_set)
351
0
    mm_free(pop->event_set);
352
0
  if (pop->event_set_copy)
353
0
    mm_free(pop->event_set_copy);
354
355
0
  memset(pop, 0, sizeof(struct pollop));
356
0
  mm_free(pop);
357
0
}
358
359
#endif /* EVENT__HAVE_POLL */