|
| 1 | +/* |
| 2 | + * event-handler.c - generic select() based event handler. Should be system |
| 3 | + * independent. |
| 4 | + * |
| 5 | + * Copyright (c) 1994-2025 Paul Mackerras. 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 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in |
| 16 | + * the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO |
| 20 | + * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 21 | + * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 22 | + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 23 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
| 24 | + * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 25 | + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 26 | + * |
| 27 | + * Derived from sys-linux.c and sys-solaris.c by Jaco Kroon <jaco@uls.co.za>. |
| 28 | + */ |
| 29 | +#include <limits.h> |
| 30 | +#include <stddef.h> |
| 31 | +#include <errno.h> |
| 32 | +#include <sys/select.h> |
| 33 | + |
| 34 | +#include "pppd.h" |
| 35 | +#include "pppd-private.h" |
| 36 | + |
| 37 | +struct event_handler { |
| 38 | + struct event_handler* next; |
| 39 | + int fd; |
| 40 | + event_cb cb; |
| 41 | + void* ctx; |
| 42 | +}; |
| 43 | + |
| 44 | +static fd_set in_fds; /* set of fds that wait_input waits for */ |
| 45 | +static int max_in_fd; /* highest fd set in in_fds */ |
| 46 | +static struct event_handler* handlers; |
| 47 | +static int called_remove; |
| 48 | + |
| 49 | +/******************************************************************** |
| 50 | + * |
| 51 | + * wait_input - wait until there is data available, |
| 52 | + * for the length of time specified by *timo (indefinite |
| 53 | + * if timo is NULL). |
| 54 | + */ |
| 55 | + |
| 56 | +void wait_input(struct timeval *timo) |
| 57 | +{ |
| 58 | + fd_set ready, exc; |
| 59 | + int n; |
| 60 | + struct event_handler* h = handlers, *nh; |
| 61 | + |
| 62 | + called_remove = 0; |
| 63 | + ready = in_fds; |
| 64 | + exc = in_fds; |
| 65 | + n = select(max_in_fd + 1, &ready, NULL, &exc, timo); |
| 66 | + if (n < 0 && errno != EINTR) |
| 67 | + fatal("select: %m"); |
| 68 | + |
| 69 | + while (h) { |
| 70 | + nh = h->next; |
| 71 | + if (FD_ISSET(h->fd, &ready)) { |
| 72 | + FD_CLR(h->fd, &ready); /* clear so that if we need to re-iterate we won't call again */ |
| 73 | + h->cb(h->fd, h->ctx); |
| 74 | + |
| 75 | + if (called_remove) { |
| 76 | + nh = handlers; |
| 77 | + called_remove = 0; |
| 78 | + } |
| 79 | + } |
| 80 | + h = nh; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | + * add_fd - add an fd to the set that wait_input waits for. |
| 86 | + */ |
| 87 | +void add_fd(int fd) |
| 88 | +{ |
| 89 | + if (fd >= FD_SETSIZE) |
| 90 | + fatal("internal error: file descriptor too large (%d)", fd); |
| 91 | + FD_SET(fd, &in_fds); |
| 92 | + if (fd > max_in_fd) |
| 93 | + max_in_fd = fd; |
| 94 | +} |
| 95 | + |
| 96 | +void add_fd_callback(int fd, event_cb cb, void* ctx) |
| 97 | +{ |
| 98 | + struct event_handler *n = malloc(sizeof(*n)); |
| 99 | + n->next = handlers; |
| 100 | + n->fd = fd; |
| 101 | + n->cb = cb; |
| 102 | + n->ctx = ctx; |
| 103 | + handlers = n; |
| 104 | + add_fd(fd); |
| 105 | +} |
| 106 | + |
| 107 | +/* |
| 108 | + * remove_fd - remove an fd from the set that wait_input waits for. |
| 109 | + */ |
| 110 | +void remove_fd(int fd) |
| 111 | +{ |
| 112 | + struct event_handler** h, *t; |
| 113 | + FD_CLR(fd, &in_fds); |
| 114 | + |
| 115 | + for (h = &handlers; *h; h = &(*h)->next) { |
| 116 | + if (fd == (*h)->fd) { |
| 117 | + called_remove = 1; |
| 118 | + t = (*h)->next; |
| 119 | + free(*h); |
| 120 | + *h = t; |
| 121 | + return; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void event_handler_init() |
| 127 | +{ |
| 128 | + FD_ZERO(&in_fds); |
| 129 | + max_in_fd = 0; |
| 130 | +} |
0 commit comments