! \brief io_wait_loop_x style function * wait for io using poll() * \param h io_wait handle * \param t timeout in s * \param repeat if !=0 handle_io will be called until it returns <=0 * \return number of IO events handled on success (can be 0), -1 on error */
| 67 | * \return number of IO events handled on success (can be 0), -1 on error |
| 68 | */ |
| 69 | inline static int io_wait_loop_poll(io_wait_h* h, int t, int repeat) |
| 70 | { |
| 71 | int n, r; |
| 72 | int ret; |
| 73 | struct fd_map *e; |
| 74 | unsigned int curr_time; |
| 75 | |
| 76 | again: |
| 77 | ret=n=poll(h->fd_array, h->fd_no, t*1000); |
| 78 | if (n==-1){ |
| 79 | if (errno==EINTR) goto again; /* signal, ignore it */ |
| 80 | else{ |
| 81 | LM_ERR("[%s] poll: %s [%d]\n",h->name, strerror(errno), errno); |
| 82 | goto error; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | curr_time = get_ticks(); |
| 87 | |
| 88 | for (r=h->fd_no-1; (r>=0) ; r--){ |
| 89 | if (h->fd_array[r].revents & POLLOUT) { |
| 90 | /* sanity checks */ |
| 91 | if ((h->fd_array[r].fd >= h->max_fd_no)|| |
| 92 | (h->fd_array[r].fd < 0)){ |
| 93 | LM_CRIT("[%s] bad fd %d (no in the 0 - %d range)\n", |
| 94 | h->name, h->fd_array[r].fd, h->max_fd_no); |
| 95 | /* try to continue anyway */ |
| 96 | h->fd_array[r].events=0; /* clear the events */ |
| 97 | continue; |
| 98 | } |
| 99 | handle_io(get_fd_map(h, h->fd_array[r].fd),r,IO_WATCH_WRITE); |
| 100 | } else if (h->fd_array[r].revents & (POLLIN|POLLERR|POLLHUP)){ |
| 101 | /* sanity checks */ |
| 102 | if ((h->fd_array[r].fd >= h->max_fd_no)|| |
| 103 | (h->fd_array[r].fd < 0)){ |
| 104 | LM_CRIT("[%s] bad fd %d (no in the 0 - %d range)\n", |
| 105 | h->name,h->fd_array[r].fd, h->max_fd_no); |
| 106 | /* try to continue anyway */ |
| 107 | h->fd_array[r].events=0; /* clear the events */ |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | while((handle_io(get_fd_map(h, h->fd_array[r].fd), r, |
| 112 | IO_WATCH_READ) > 0) |
| 113 | && repeat); |
| 114 | } else if ( (e=get_fd_map(h, h->fd_array[r].fd))!=NULL && |
| 115 | e->timeout!=0 && e->timeout<=curr_time ) { |
| 116 | e->timeout = 0; |
| 117 | handle_io( e, r, IO_WATCH_TIMEOUT); |
| 118 | } |
| 119 | } |
| 120 | error: |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | |
| 125 |
no test coverage detected