4 * Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the same terms as Perl itself.
8 * For systems that do not have the poll() system call (for example Linux
9 * kernels < v2.1.23) try to emulate it as closely as possible using select()
17 # include <sys/time.h>
22 #include <sys/types.h>
23 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
24 # include <sys/socket.h>
31 #include <sys/select.h>
35 #ifdef EMULATE_POLL_WITH_SELECT
37 # define POLL_CAN_READ (POLLIN | POLLRDNORM )
38 # define POLL_CAN_WRITE (POLLOUT | POLLWRNORM | POLLWRBAND )
39 # define POLL_HAS_EXCP (POLLRDBAND | POLLPRI )
41 # define POLL_EVENTS_MASK (POLL_CAN_READ | POLL_CAN_WRITE | POLL_HAS_EXCP)
44 poll(struct pollfd *fds, unsigned long nfds, int timeout)
47 fd_set rfd,wfd,efd,ifd;
48 struct timeval timebuf;
49 struct timeval *tbuf = (struct timeval *)0;
61 for(i = 0 ; i < nfds ; i++) {
62 int events = fds[i].events;
67 if(fd < 0 || FD_ISSET(fd, &ifd))
73 if(events & POLL_CAN_READ)
76 if(events & POLL_CAN_WRITE)
79 if(events & POLL_HAS_EXCP)
84 timebuf.tv_sec = timeout / 1000;
85 timebuf.tv_usec = (timeout % 1000) * 1000;
89 err = select(n+1,&rfd,&wfd,&efd,tbuf);
94 for(i = 0 ; i < nfds ; i++) {
96 if((fstat(fds[i].fd,&buf) < 0) && (errno == EBADF)) {
97 FD_SET(fds[i].fd, &ifd);
102 #endif /* HAS_FSTAT */
108 for(i = 0 ; i < nfds ; i++) {
109 int revents = (fds[i].events & POLL_EVENTS_MASK);
115 if(FD_ISSET(fd, &ifd))
118 if(!FD_ISSET(fd, &rfd))
119 revents &= ~POLL_CAN_READ;
121 if(!FD_ISSET(fd, &wfd))
122 revents &= ~POLL_CAN_WRITE;
124 if(!FD_ISSET(fd, &efd))
125 revents &= ~POLL_HAS_EXCP;
128 if((fds[i].revents = revents) != 0)
135 #endif /* EMULATE_POLL_WITH_SELECT */