1N/A/*
1N/A * poll.c
1N/A *
1N/A * Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
1N/A * This program is free software; you can redistribute it and/or
1N/A * modify it under the same terms as Perl itself.
1N/A *
1N/A * For systems that do not have the poll() system call (for example Linux
1N/A * kernels < v2.1.23) try to emulate it as closely as possible using select()
1N/A *
1N/A */
1N/A
1N/A#include "EXTERN.h"
1N/A#include "perl.h"
1N/A#include "XSUB.h"
1N/A
1N/A#include "poll.h"
1N/A#ifdef I_SYS_TIME
1N/A# include <sys/time.h>
1N/A#endif
1N/A#ifdef I_TIME
1N/A# include <time.h>
1N/A#endif
1N/A#include <sys/types.h>
1N/A#if defined(HAS_SOCKET) && !defined(VMS) && !defined(ultrix) /* VMS handles sockets via vmsish.h, ULTRIX dies of socket struct redefinitions */
1N/A# include <sys/socket.h>
1N/A#endif
1N/A#include <sys/stat.h>
1N/A#include <errno.h>
1N/A
1N/A#ifdef HAS_SELECT
1N/A#ifdef I_SYS_SELECT
1N/A#include <sys/select.h>
1N/A#endif
1N/A#endif
1N/A
1N/A#ifdef EMULATE_POLL_WITH_SELECT
1N/A
1N/A# define POLL_CAN_READ (POLLIN | POLLRDNORM )
1N/A# define POLL_CAN_WRITE (POLLOUT | POLLWRNORM | POLLWRBAND )
1N/A# define POLL_HAS_EXCP (POLLRDBAND | POLLPRI )
1N/A
1N/A# define POLL_EVENTS_MASK (POLL_CAN_READ | POLL_CAN_WRITE | POLL_HAS_EXCP)
1N/A
1N/Aint
1N/Apoll(struct pollfd *fds, unsigned long nfds, int timeout)
1N/A{
1N/A int i,err;
1N/A fd_set rfd,wfd,efd,ifd;
1N/A struct timeval timebuf;
1N/A struct timeval *tbuf = (struct timeval *)0;
1N/A int n = 0;
1N/A int count;
1N/A
1N/A FD_ZERO(&ifd);
1N/A
1N/Aagain:
1N/A
1N/A FD_ZERO(&rfd);
1N/A FD_ZERO(&wfd);
1N/A FD_ZERO(&efd);
1N/A
1N/A for(i = 0 ; i < (int)nfds ; i++) {
1N/A int events = fds[i].events;
1N/A int fd = fds[i].fd;
1N/A
1N/A fds[i].revents = 0;
1N/A
1N/A if(fd < 0 || FD_ISSET(fd, &ifd))
1N/A continue;
1N/A
1N/A if(fd > n)
1N/A n = fd;
1N/A
1N/A if(events & POLL_CAN_READ)
1N/A FD_SET(fd, &rfd);
1N/A
1N/A if(events & POLL_CAN_WRITE)
1N/A FD_SET(fd, &wfd);
1N/A
1N/A if(events & POLL_HAS_EXCP)
1N/A FD_SET(fd, &efd);
1N/A }
1N/A
1N/A if(timeout >= 0) {
1N/A timebuf.tv_sec = timeout / 1000;
1N/A timebuf.tv_usec = (timeout % 1000) * 1000;
1N/A tbuf = &timebuf;
1N/A }
1N/A
1N/A err = select(n+1,&rfd,&wfd,&efd,tbuf);
1N/A
1N/A if(err < 0) {
1N/A#ifdef HAS_FSTAT
1N/A if(errno == EBADF) {
1N/A for(i = 0 ; i < nfds ; i++) {
1N/A struct stat buf;
1N/A if((fstat(fds[i].fd,&buf) < 0) && (errno == EBADF)) {
1N/A FD_SET(fds[i].fd, &ifd);
1N/A goto again;
1N/A }
1N/A }
1N/A }
1N/A#endif /* HAS_FSTAT */
1N/A return err;
1N/A }
1N/A
1N/A count = 0;
1N/A
1N/A for(i = 0 ; i < (int)nfds ; i++) {
1N/A int revents = (fds[i].events & POLL_EVENTS_MASK);
1N/A int fd = fds[i].fd;
1N/A
1N/A if(fd < 0)
1N/A continue;
1N/A
1N/A if(FD_ISSET(fd, &ifd))
1N/A revents = POLLNVAL;
1N/A else {
1N/A if(!FD_ISSET(fd, &rfd))
1N/A revents &= ~POLL_CAN_READ;
1N/A
1N/A if(!FD_ISSET(fd, &wfd))
1N/A revents &= ~POLL_CAN_WRITE;
1N/A
1N/A if(!FD_ISSET(fd, &efd))
1N/A revents &= ~POLL_HAS_EXCP;
1N/A }
1N/A
1N/A if((fds[i].revents = revents) != 0)
1N/A count++;
1N/A }
1N/A
1N/A return count;
1N/A}
1N/A
1N/A#endif /* EMULATE_POLL_WITH_SELECT */
1N/A
1N/A/* gcc for SunOS 4 produces code from an empty (code/symbolwise)
1N/A * source code file that makes the SunOS 4.x /usr/bin/ld fail with
1N/A * ld: poll.o: premature EOF
1N/A * To avoid this, have at least something in here. */
1N/A#if defined(__sun) && !defined(__SVR4) && defined(__GNUC__)
1N/Astatic int dummy;
1N/A#endif
1N/A