| 1 | /*
 | 
|---|
| 2 |    Unix SMB/CIFS implementation.
 | 
|---|
| 3 |    poll.c - poll wrapper
 | 
|---|
| 4 | 
 | 
|---|
| 5 |    This file is based on code from libssh (LGPLv2.1+ at the time it
 | 
|---|
| 6 |    was downloaded), thus the following copyrights:
 | 
|---|
| 7 | 
 | 
|---|
| 8 |    Copyright (c) 2009-2010 by Andreas Schneider <mail@cynapses.org>
 | 
|---|
| 9 |    Copyright (c) 2003-2009 by Aris Adamantiadis
 | 
|---|
| 10 |    Copyright (c) 2009 Aleksandar Kanchev
 | 
|---|
| 11 |    Copyright (C) Volker Lendecke 2011
 | 
|---|
| 12 | 
 | 
|---|
| 13 |      ** NOTE! The following LGPL license applies to the replace
 | 
|---|
| 14 |      ** library. This does NOT imply that all of Samba is released
 | 
|---|
| 15 |      ** under the LGPL
 | 
|---|
| 16 | 
 | 
|---|
| 17 |    This library is free software; you can redistribute it and/or
 | 
|---|
| 18 |    modify it under the terms of the GNU Lesser General Public
 | 
|---|
| 19 |    License as published by the Free Software Foundation; either
 | 
|---|
| 20 |    version 3 of the License, or (at your option) any later version.
 | 
|---|
| 21 | 
 | 
|---|
| 22 |    This library is distributed in the hope that it will be useful,
 | 
|---|
| 23 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 24 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
|---|
| 25 |    Lesser General Public License for more details.
 | 
|---|
| 26 | 
 | 
|---|
| 27 |    You should have received a copy of the GNU Lesser General Public
 | 
|---|
| 28 |    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #include "replace.h"
 | 
|---|
| 32 | #include "system/select.h"
 | 
|---|
| 33 | #ifdef HAVE_SYS_TIME_H
 | 
|---|
| 34 | #include <sys/time.h>
 | 
|---|
| 35 | #endif
 | 
|---|
| 36 | #ifdef HAVE_SYS_IOCTL_H
 | 
|---|
| 37 | #include <sys/ioctl.h>
 | 
|---|
| 38 | #endif
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | int rep_poll(struct pollfd *fds, nfds_t nfds, int timeout)
 | 
|---|
| 42 | {
 | 
|---|
| 43 |         fd_set rfds, wfds, efds;
 | 
|---|
| 44 |         struct timeval tv, *ptv;
 | 
|---|
| 45 |         int max_fd;
 | 
|---|
| 46 |         int rc;
 | 
|---|
| 47 |         nfds_t i;
 | 
|---|
| 48 | 
 | 
|---|
| 49 |         if ((fds == NULL) && (nfds != 0)) {
 | 
|---|
| 50 |                 errno = EFAULT;
 | 
|---|
| 51 |                 return -1;
 | 
|---|
| 52 |         }
 | 
|---|
| 53 | 
 | 
|---|
| 54 |         FD_ZERO(&rfds);
 | 
|---|
| 55 |         FD_ZERO(&wfds);
 | 
|---|
| 56 |         FD_ZERO(&efds);
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         rc = 0;
 | 
|---|
| 59 |         max_fd = 0;
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         /* compute fd_sets and find largest descriptor */
 | 
|---|
| 62 |         for (i = 0; i < nfds; i++) {
 | 
|---|
| 63 |                 if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
 | 
|---|
| 64 |                         fds[i].revents = POLLNVAL;
 | 
|---|
| 65 |                         continue;
 | 
|---|
| 66 |                 }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |                 if (fds[i].events & (POLLIN | POLLRDNORM)) {
 | 
|---|
| 69 |                         FD_SET(fds[i].fd, &rfds);
 | 
|---|
| 70 |                 }
 | 
|---|
| 71 |                 if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
 | 
|---|
| 72 |                         FD_SET(fds[i].fd, &wfds);
 | 
|---|
| 73 |                 }
 | 
|---|
| 74 |                 if (fds[i].events & (POLLPRI | POLLRDBAND)) {
 | 
|---|
| 75 |                         FD_SET(fds[i].fd, &efds);
 | 
|---|
| 76 |                 }
 | 
|---|
| 77 |                 if (fds[i].fd > max_fd &&
 | 
|---|
| 78 |                     (fds[i].events & (POLLIN | POLLOUT | POLLPRI |
 | 
|---|
| 79 |                                       POLLRDNORM | POLLRDBAND |
 | 
|---|
| 80 |                                       POLLWRNORM | POLLWRBAND))) {
 | 
|---|
| 81 |                         max_fd = fds[i].fd;
 | 
|---|
| 82 |                 }
 | 
|---|
| 83 |         }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         if (timeout < 0) {
 | 
|---|
| 86 |                 ptv = NULL;
 | 
|---|
| 87 |         } else {
 | 
|---|
| 88 |                 ptv = &tv;
 | 
|---|
| 89 |                 if (timeout == 0) {
 | 
|---|
| 90 |                         tv.tv_sec = 0;
 | 
|---|
| 91 |                         tv.tv_usec = 0;
 | 
|---|
| 92 |                 } else {
 | 
|---|
| 93 |                         tv.tv_sec = timeout / 1000;
 | 
|---|
| 94 |                         tv.tv_usec = (timeout % 1000) * 1000;
 | 
|---|
| 95 |                 }
 | 
|---|
| 96 |         }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         rc = select(max_fd + 1, &rfds, &wfds, &efds, ptv);
 | 
|---|
| 99 |         if (rc < 0) {
 | 
|---|
| 100 |                 return -1;
 | 
|---|
| 101 |         }
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         for (rc = 0, i = 0; i < nfds; i++) {
 | 
|---|
| 104 |                 if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
 | 
|---|
| 105 |                         continue;
 | 
|---|
| 106 |                 }
 | 
|---|
| 107 | 
 | 
|---|
| 108 |                 fds[i].revents = 0;
 | 
|---|
| 109 | 
 | 
|---|
| 110 |                 if (FD_ISSET(fds[i].fd, &rfds)) {
 | 
|---|
| 111 |                         int err = errno;
 | 
|---|
| 112 |                         int available = 0;
 | 
|---|
| 113 |                         int ret;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |                         /* support for POLLHUP */
 | 
|---|
| 116 |                         ret = ioctl(fds[i].fd, FIONREAD, &available);
 | 
|---|
| 117 |                         if ((ret == -1) || (available == 0)) {
 | 
|---|
| 118 |                                 fds[i].revents |= POLLHUP;
 | 
|---|
| 119 |                         } else {
 | 
|---|
| 120 |                                 fds[i].revents |= fds[i].events
 | 
|---|
| 121 |                                         & (POLLIN | POLLRDNORM);
 | 
|---|
| 122 |                         }
 | 
|---|
| 123 | 
 | 
|---|
| 124 |                         errno = err;
 | 
|---|
| 125 |                 }
 | 
|---|
| 126 |                 if (FD_ISSET(fds[i].fd, &wfds)) {
 | 
|---|
| 127 |                         fds[i].revents |= fds[i].events
 | 
|---|
| 128 |                                 & (POLLOUT | POLLWRNORM | POLLWRBAND);
 | 
|---|
| 129 |                 }
 | 
|---|
| 130 |                 if (FD_ISSET(fds[i].fd, &efds)) {
 | 
|---|
| 131 |                         fds[i].revents |= fds[i].events
 | 
|---|
| 132 |                                 & (POLLPRI | POLLRDBAND);
 | 
|---|
| 133 |                 }
 | 
|---|
| 134 |                 if (fds[i].revents & ~POLLHUP) {
 | 
|---|
| 135 |                         rc++;
 | 
|---|
| 136 |                 }
 | 
|---|
| 137 |         }
 | 
|---|
| 138 |         return rc;
 | 
|---|
| 139 | }
 | 
|---|