LCOV - code coverage report
Current view: directory - ipc/chromium/src/third_party/libevent - select.c (source / functions) Found Hit Coverage
Test: app.info Lines: 130 0 0.0 %
Date: 2012-06-02 Functions: 6 0 0.0 %

       1                 : /*      $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $       */
       2                 : 
       3                 : /*
       4                 :  * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
       5                 :  * 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                 :  * 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                 : #ifdef HAVE_CONFIG_H
      30                 : #include "config.h"
      31                 : #endif
      32                 : 
      33                 : #include <sys/types.h>
      34                 : #ifdef HAVE_SYS_TIME_H
      35                 : #include <sys/time.h>
      36                 : #else
      37                 : #include <sys/_time.h>
      38                 : #endif
      39                 : #ifdef HAVE_SYS_SELECT_H
      40                 : #include <sys/select.h>
      41                 : #endif
      42                 : #include <sys/queue.h>
      43                 : #include <signal.h>
      44                 : #include <stdio.h>
      45                 : #include <stdlib.h>
      46                 : #include <string.h>
      47                 : #include <unistd.h>
      48                 : #include <errno.h>
      49                 : #ifdef CHECK_INVARIANTS
      50                 : #include <assert.h>
      51                 : #endif
      52                 : 
      53                 : #include "event.h"
      54                 : #include "event-internal.h"
      55                 : #include "evsignal.h"
      56                 : #include "log.h"
      57                 : 
      58                 : #ifndef howmany
      59                 : #define        howmany(x, y)   (((x)+((y)-1))/(y))
      60                 : #endif
      61                 : 
      62                 : #ifdef ANDROID
      63                 : typedef unsigned long int fd_mask;
      64                 : #endif
      65                 : 
      66                 : struct selectop {
      67                 :         int event_fds;          /* Highest fd in fd set */
      68                 :         int event_fdsz;
      69                 :         fd_set *event_readset_in;
      70                 :         fd_set *event_writeset_in;
      71                 :         fd_set *event_readset_out;
      72                 :         fd_set *event_writeset_out;
      73                 :         struct event **event_r_by_fd;
      74                 :         struct event **event_w_by_fd;
      75                 : };
      76                 : 
      77                 : static void *select_init        (struct event_base *);
      78                 : static int select_add           (void *, struct event *);
      79                 : static int select_del           (void *, struct event *);
      80                 : static int select_dispatch      (struct event_base *, void *, struct timeval *);
      81                 : static void select_dealloc     (struct event_base *, void *);
      82                 : 
      83                 : const struct eventop selectops = {
      84                 :         "select",
      85                 :         select_init,
      86                 :         select_add,
      87                 :         select_del,
      88                 :         select_dispatch,
      89                 :         select_dealloc,
      90                 :         0
      91                 : };
      92                 : 
      93                 : static int select_resize(struct selectop *sop, int fdsz);
      94                 : 
      95                 : static void *
      96               0 : select_init(struct event_base *base)
      97                 : {
      98                 :         struct selectop *sop;
      99                 : 
     100                 :         /* Disable select when this environment variable is set */
     101               0 :         if (getenv("EVENT_NOSELECT"))
     102               0 :                 return (NULL);
     103                 : 
     104               0 :         if (!(sop = calloc(1, sizeof(struct selectop))))
     105               0 :                 return (NULL);
     106                 : 
     107               0 :         select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
     108                 : 
     109               0 :         evsignal_init(base);
     110                 : 
     111               0 :         return (sop);
     112                 : }
     113                 : 
     114                 : #ifdef CHECK_INVARIANTS
     115                 : static void
     116                 : check_selectop(struct selectop *sop)
     117                 : {
     118                 :         int i;
     119                 :         for (i = 0; i <= sop->event_fds; ++i) {
     120                 :                 if (FD_ISSET(i, sop->event_readset_in)) {
     121                 :                         assert(sop->event_r_by_fd[i]);
     122                 :                         assert(sop->event_r_by_fd[i]->ev_events & EV_READ);
     123                 :                         assert(sop->event_r_by_fd[i]->ev_fd == i);
     124                 :                 } else {
     125                 :                         assert(! sop->event_r_by_fd[i]);
     126                 :                 }
     127                 :                 if (FD_ISSET(i, sop->event_writeset_in)) {
     128                 :                         assert(sop->event_w_by_fd[i]);
     129                 :                         assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE);
     130                 :                         assert(sop->event_w_by_fd[i]->ev_fd == i);
     131                 :                 } else {
     132                 :                         assert(! sop->event_w_by_fd[i]);
     133                 :                 }
     134                 :         }
     135                 : 
     136                 : }
     137                 : #else
     138                 : #define check_selectop(sop) do { (void) sop; } while (0)
     139                 : #endif
     140                 : 
     141                 : static int
     142               0 : select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
     143                 : {
     144                 :         int res, i;
     145               0 :         struct selectop *sop = arg;
     146                 : 
     147                 :         check_selectop(sop);
     148                 : 
     149               0 :         memcpy(sop->event_readset_out, sop->event_readset_in,
     150               0 :                sop->event_fdsz);
     151               0 :         memcpy(sop->event_writeset_out, sop->event_writeset_in,
     152               0 :                sop->event_fdsz);
     153                 : 
     154               0 :         res = select(sop->event_fds + 1, sop->event_readset_out,
     155                 :             sop->event_writeset_out, NULL, tv);
     156                 : 
     157                 :         check_selectop(sop);
     158                 : 
     159               0 :         if (res == -1) {
     160               0 :                 if (errno != EINTR) {
     161               0 :                         event_warn("select");
     162               0 :                         return (-1);
     163                 :                 }
     164                 : 
     165               0 :                 evsignal_process(base);
     166               0 :                 return (0);
     167               0 :         } else if (base->sig.evsignal_caught) {
     168               0 :                 evsignal_process(base);
     169                 :         }
     170                 : 
     171                 :         event_debug(("%s: select reports %d", __func__, res));
     172                 : 
     173                 :         check_selectop(sop);
     174               0 :         for (i = 0; i <= sop->event_fds; ++i) {
     175               0 :                 struct event *r_ev = NULL, *w_ev = NULL;
     176               0 :                 res = 0;
     177               0 :                 if (FD_ISSET(i, sop->event_readset_out)) {
     178               0 :                         r_ev = sop->event_r_by_fd[i];
     179               0 :                         res |= EV_READ;
     180                 :                 }
     181               0 :                 if (FD_ISSET(i, sop->event_writeset_out)) {
     182               0 :                         w_ev = sop->event_w_by_fd[i];
     183               0 :                         res |= EV_WRITE;
     184                 :                 }
     185               0 :                 if (r_ev && (res & r_ev->ev_events)) {
     186               0 :                         event_active(r_ev, res & r_ev->ev_events, 1);
     187                 :                 }
     188               0 :                 if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
     189               0 :                         event_active(w_ev, res & w_ev->ev_events, 1);
     190                 :                 }
     191                 :         }
     192                 :         check_selectop(sop);
     193                 : 
     194               0 :         return (0);
     195                 : }
     196                 : 
     197                 : 
     198                 : static int
     199               0 : select_resize(struct selectop *sop, int fdsz)
     200                 : {
     201                 :         int n_events, n_events_old;
     202                 : 
     203               0 :         fd_set *readset_in = NULL;
     204               0 :         fd_set *writeset_in = NULL;
     205               0 :         fd_set *readset_out = NULL;
     206               0 :         fd_set *writeset_out = NULL;
     207               0 :         struct event **r_by_fd = NULL;
     208               0 :         struct event **w_by_fd = NULL;
     209                 : 
     210               0 :         n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
     211               0 :         n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
     212                 : 
     213               0 :         if (sop->event_readset_in)
     214                 :                 check_selectop(sop);
     215                 : 
     216               0 :         if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
     217               0 :                 goto error;
     218               0 :         sop->event_readset_in = readset_in;
     219               0 :         if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
     220               0 :                 goto error;
     221               0 :         sop->event_readset_out = readset_out;
     222               0 :         if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
     223               0 :                 goto error;
     224               0 :         sop->event_writeset_in = writeset_in;
     225               0 :         if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
     226               0 :                 goto error;
     227               0 :         sop->event_writeset_out = writeset_out;
     228               0 :         if ((r_by_fd = realloc(sop->event_r_by_fd,
     229                 :                  n_events*sizeof(struct event*))) == NULL)
     230               0 :                 goto error;
     231               0 :         sop->event_r_by_fd = r_by_fd;
     232               0 :         if ((w_by_fd = realloc(sop->event_w_by_fd,
     233                 :                  n_events * sizeof(struct event*))) == NULL)
     234               0 :                 goto error;
     235               0 :         sop->event_w_by_fd = w_by_fd;
     236                 : 
     237               0 :         memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
     238               0 :             fdsz - sop->event_fdsz);
     239               0 :         memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
     240               0 :             fdsz - sop->event_fdsz);
     241               0 :         memset(sop->event_r_by_fd + n_events_old, 0,
     242               0 :             (n_events-n_events_old) * sizeof(struct event*));
     243               0 :         memset(sop->event_w_by_fd + n_events_old, 0,
     244               0 :             (n_events-n_events_old) * sizeof(struct event*));
     245                 : 
     246               0 :         sop->event_fdsz = fdsz;
     247                 :         check_selectop(sop);
     248                 : 
     249               0 :         return (0);
     250                 : 
     251                 :  error:
     252               0 :         event_warn("malloc");
     253               0 :         return (-1);
     254                 : }
     255                 : 
     256                 : 
     257                 : static int
     258               0 : select_add(void *arg, struct event *ev)
     259                 : {
     260               0 :         struct selectop *sop = arg;
     261                 : 
     262               0 :         if (ev->ev_events & EV_SIGNAL)
     263               0 :                 return (evsignal_add(ev));
     264                 : 
     265                 :         check_selectop(sop);
     266                 :         /*
     267                 :          * Keep track of the highest fd, so that we can calculate the size
     268                 :          * of the fd_sets for select(2)
     269                 :          */
     270               0 :         if (sop->event_fds < ev->ev_fd) {
     271               0 :                 int fdsz = sop->event_fdsz;
     272                 : 
     273               0 :                 if (fdsz < sizeof(fd_mask))
     274               0 :                         fdsz = sizeof(fd_mask);
     275                 : 
     276               0 :                 while (fdsz <
     277               0 :                     (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
     278               0 :                         fdsz *= 2;
     279                 : 
     280               0 :                 if (fdsz != sop->event_fdsz) {
     281               0 :                         if (select_resize(sop, fdsz)) {
     282                 :                                 check_selectop(sop);
     283               0 :                                 return (-1);
     284                 :                         }
     285                 :                 }
     286                 : 
     287               0 :                 sop->event_fds = ev->ev_fd;
     288                 :         }
     289                 : 
     290               0 :         if (ev->ev_events & EV_READ) {
     291               0 :                 FD_SET(ev->ev_fd, sop->event_readset_in);
     292               0 :                 sop->event_r_by_fd[ev->ev_fd] = ev;
     293                 :         }
     294               0 :         if (ev->ev_events & EV_WRITE) {
     295               0 :                 FD_SET(ev->ev_fd, sop->event_writeset_in);
     296               0 :                 sop->event_w_by_fd[ev->ev_fd] = ev;
     297                 :         }
     298                 :         check_selectop(sop);
     299                 : 
     300               0 :         return (0);
     301                 : }
     302                 : 
     303                 : /*
     304                 :  * Nothing to be done here.
     305                 :  */
     306                 : 
     307                 : static int
     308               0 : select_del(void *arg, struct event *ev)
     309                 : {
     310               0 :         struct selectop *sop = arg;
     311                 : 
     312                 :         check_selectop(sop);
     313               0 :         if (ev->ev_events & EV_SIGNAL)
     314               0 :                 return (evsignal_del(ev));
     315                 : 
     316               0 :         if (sop->event_fds < ev->ev_fd) {
     317                 :                 check_selectop(sop);
     318               0 :                 return (0);
     319                 :         }
     320                 : 
     321               0 :         if (ev->ev_events & EV_READ) {
     322               0 :                 FD_CLR(ev->ev_fd, sop->event_readset_in);
     323               0 :                 sop->event_r_by_fd[ev->ev_fd] = NULL;
     324                 :         }
     325                 : 
     326               0 :         if (ev->ev_events & EV_WRITE) {
     327               0 :                 FD_CLR(ev->ev_fd, sop->event_writeset_in);
     328               0 :                 sop->event_w_by_fd[ev->ev_fd] = NULL;
     329                 :         }
     330                 : 
     331                 :         check_selectop(sop);
     332               0 :         return (0);
     333                 : }
     334                 : 
     335                 : static void
     336               0 : select_dealloc(struct event_base *base, void *arg)
     337                 : {
     338               0 :         struct selectop *sop = arg;
     339                 : 
     340               0 :         evsignal_dealloc(base);
     341               0 :         if (sop->event_readset_in)
     342               0 :                 free(sop->event_readset_in);
     343               0 :         if (sop->event_writeset_in)
     344               0 :                 free(sop->event_writeset_in);
     345               0 :         if (sop->event_readset_out)
     346               0 :                 free(sop->event_readset_out);
     347               0 :         if (sop->event_writeset_out)
     348               0 :                 free(sop->event_writeset_out);
     349               0 :         if (sop->event_r_by_fd)
     350               0 :                 free(sop->event_r_by_fd);
     351               0 :         if (sop->event_w_by_fd)
     352               0 :                 free(sop->event_w_by_fd);
     353                 : 
     354               0 :         memset(sop, 0, sizeof(struct selectop));
     355               0 :         free(sop);
     356               0 : }

Generated by: LCOV version 1.7