Annotation of embedaddon/sudo/common/event_poll.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
        !             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: #include <config.h>
        !            18: 
        !            19: #include <sys/types.h>
        !            20: #include <sys/time.h>
        !            21: #include <stdio.h>
        !            22: #ifdef STDC_HEADERS
        !            23: # include <stdlib.h>
        !            24: # include <stddef.h>
        !            25: #else
        !            26: # ifdef HAVE_STDLIB_H
        !            27: #  include <stdlib.h>
        !            28: # endif
        !            29: #endif /* STDC_HEADERS */
        !            30: #ifdef HAVE_STDBOOL_H
        !            31: # include <stdbool.h>
        !            32: #else
        !            33: # include "compat/stdbool.h"
        !            34: #endif /* HAVE_STDBOOL_H */
        !            35: #ifdef HAVE_STRING_H
        !            36: # include <string.h>
        !            37: #endif /* HAVE_STRING_H */
        !            38: #ifdef HAVE_STRINGS_H
        !            39: # include <strings.h>
        !            40: #endif /* HAVE_STRINGS_H */
        !            41: #ifdef HAVE_UNISTD_H
        !            42: # include <unistd.h>
        !            43: #endif /* HAVE_UNISTD_H */
        !            44: #include <errno.h>
        !            45: #include <poll.h>
        !            46: 
        !            47: #include "missing.h"
        !            48: #include "alloc.h"
        !            49: #include "fatal.h"
        !            50: #include "sudo_debug.h"
        !            51: #include "sudo_event.h"
        !            52: 
        !            53: /* XXX - use non-exiting allocators? */
        !            54: 
        !            55: int
        !            56: sudo_ev_base_alloc_impl(struct sudo_event_base *base)
        !            57: {
        !            58:     int i;
        !            59:     debug_decl(sudo_ev_base_alloc_impl, SUDO_DEBUG_EVENT)
        !            60: 
        !            61:     base->pfd_high = -1;
        !            62:     base->pfd_max = 32;
        !            63:     base->pfds = erealloc3(NULL, base->pfd_max, sizeof(struct pollfd));
        !            64:     for (i = 0; i < base->pfd_max; i++) {
        !            65:        base->pfds[i].fd = -1;
        !            66:     }
        !            67: 
        !            68:     debug_return_int(0);
        !            69: }
        !            70: 
        !            71: void
        !            72: sudo_ev_base_free_impl(struct sudo_event_base *base)
        !            73: {
        !            74:     debug_decl(sudo_ev_base_free_impl, SUDO_DEBUG_EVENT)
        !            75:     efree(base->pfds);
        !            76:     debug_return;
        !            77: }
        !            78: 
        !            79: int
        !            80: sudo_ev_add_impl(struct sudo_event_base *base, struct sudo_event *ev)
        !            81: {
        !            82:     struct pollfd *pfd;
        !            83:     debug_decl(sudo_ev_add_impl, SUDO_DEBUG_EVENT)
        !            84: 
        !            85:     /* If out of space in pfds array, realloc. */
        !            86:     if (base->pfd_free == base->pfd_max) {
        !            87:        int i;
        !            88:        base->pfd_max <<= 1;
        !            89:        base->pfds =
        !            90:            erealloc3(base->pfds, base->pfd_max, sizeof(struct pollfd));
        !            91:        for (i = base->pfd_free; i < base->pfd_max; i++) {
        !            92:            base->pfds[i].fd = -1;
        !            93:        }
        !            94:     }
        !            95: 
        !            96:     /* Fill in pfd entry. */
        !            97:     ev->pfd_idx = base->pfd_free;
        !            98:     pfd = &base->pfds[ev->pfd_idx];
        !            99:     pfd->fd = ev->fd;
        !           100:     pfd->events = 0;
        !           101:     if (ISSET(ev->events, SUDO_EV_READ))
        !           102:        pfd->events |= POLLIN;
        !           103:     if (ISSET(ev->events, SUDO_EV_WRITE))
        !           104:        pfd->events |= POLLOUT;
        !           105: 
        !           106:     /* Update pfd_high and pfd_free. */
        !           107:     if (ev->pfd_idx > base->pfd_high)
        !           108:        base->pfd_high = ev->pfd_idx;
        !           109:     for (;;) {
        !           110:        if (++base->pfd_free == base->pfd_max)
        !           111:            break;
        !           112:        if (base->pfds[base->pfd_free].fd == -1)
        !           113:            break;
        !           114:     }
        !           115: 
        !           116:     debug_return_int(0);
        !           117: }
        !           118: 
        !           119: int
        !           120: sudo_ev_del_impl(struct sudo_event_base *base, struct sudo_event *ev)
        !           121: {
        !           122:     debug_decl(sudo_ev_del_impl, SUDO_DEBUG_EVENT)
        !           123: 
        !           124:     /* Mark pfd entry unused, add to free list and adjust high slot. */
        !           125:     base->pfds[ev->pfd_idx].fd = -1;
        !           126:     if (ev->pfd_idx < base->pfd_free)
        !           127:        base->pfd_free = ev->pfd_idx;
        !           128:     while (base->pfd_high >= 0 && base->pfds[base->pfd_high].fd == -1)
        !           129:        base->pfd_high--;
        !           130: 
        !           131:     debug_return_int(0);
        !           132: }
        !           133: 
        !           134: int
        !           135: sudo_ev_scan_impl(struct sudo_event_base *base, int flags)
        !           136: {
        !           137:     struct sudo_event *ev;
        !           138:     int nready, timeout;
        !           139:     struct timeval now;
        !           140:     debug_decl(sudo_ev_scan_impl, SUDO_DEBUG_EVENT)
        !           141: 
        !           142:     if ((ev = TAILQ_FIRST(&base->timeouts)) != NULL) {
        !           143:        struct timeval *timo = &ev->timeout;
        !           144:        gettimeofday(&now, NULL);
        !           145:        timeout = ((timo->tv_sec - now.tv_sec) * 1000) +
        !           146:            ((timo->tv_usec - now.tv_usec) / 1000);
        !           147:        if (timeout <= 0)
        !           148:            timeout = 0;
        !           149:     } else {
        !           150:        timeout = (flags & SUDO_EVLOOP_NONBLOCK) ? 0 : -1;
        !           151:     }
        !           152: 
        !           153:     nready = poll(base->pfds, base->pfd_high + 1, timeout);
        !           154:     sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %d fds ready", __func__, nready);
        !           155:     switch (nready) {
        !           156:     case -1:
        !           157:        /* Error or interrupted by signal. */
        !           158:        debug_return_int(-1);
        !           159:     case 0:
        !           160:        /* Front end will activate timeout events. */
        !           161:        break;
        !           162:     default:
        !           163:        /* Activate each I/O event that fired. */
        !           164:        TAILQ_FOREACH(ev, &base->events, entries) {
        !           165:            if (ev->pfd_idx != -1 && base->pfds[ev->pfd_idx].revents) {
        !           166:                int what = 0;
        !           167:                if (base->pfds[ev->pfd_idx].revents & (POLLIN|POLLHUP|POLLNVAL|POLLERR))
        !           168:                    what |= (ev->events & SUDO_EV_READ);
        !           169:                if (base->pfds[ev->pfd_idx].revents & (POLLOUT|POLLHUP|POLLNVAL|POLLERR))
        !           170:                    what |= (ev->events & SUDO_EV_WRITE);
        !           171:                /* Make event active. */
        !           172:                sudo_debug_printf(SUDO_DEBUG_DEBUG,
        !           173:                    "%s: polled fd %d, events %d, activating %p",
        !           174:                    __func__, ev->fd, what, ev);
        !           175:                ev->revents = what;
        !           176:                TAILQ_INSERT_TAIL(&base->active, ev, active_entries);
        !           177:                SET(ev->flags, SUDO_EVQ_ACTIVE);
        !           178:            }
        !           179:        }
        !           180:        break;
        !           181:     }
        !           182:     debug_return_int(nready);
        !           183: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>