1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * poll.c
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
5*0Sstevel@tonic-gate * This program is free software; you can redistribute it and/or
6*0Sstevel@tonic-gate * modify it under the same terms as Perl itself.
7*0Sstevel@tonic-gate *
8*0Sstevel@tonic-gate * For systems that do not have the poll() system call (for example Linux
9*0Sstevel@tonic-gate * kernels < v2.1.23) try to emulate it as closely as possible using select()
10*0Sstevel@tonic-gate *
11*0Sstevel@tonic-gate */
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate #include "EXTERN.h"
14*0Sstevel@tonic-gate #include "perl.h"
15*0Sstevel@tonic-gate #include "XSUB.h"
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate #include "poll.h"
18*0Sstevel@tonic-gate #ifdef I_SYS_TIME
19*0Sstevel@tonic-gate # include <sys/time.h>
20*0Sstevel@tonic-gate #endif
21*0Sstevel@tonic-gate #ifdef I_TIME
22*0Sstevel@tonic-gate # include <time.h>
23*0Sstevel@tonic-gate #endif
24*0Sstevel@tonic-gate #include <sys/types.h>
25*0Sstevel@tonic-gate #if defined(HAS_SOCKET) && !defined(VMS) && !defined(ultrix) /* VMS handles sockets via vmsish.h, ULTRIX dies of socket struct redefinitions */
26*0Sstevel@tonic-gate # include <sys/socket.h>
27*0Sstevel@tonic-gate #endif
28*0Sstevel@tonic-gate #include <sys/stat.h>
29*0Sstevel@tonic-gate #include <errno.h>
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #ifdef HAS_SELECT
32*0Sstevel@tonic-gate #ifdef I_SYS_SELECT
33*0Sstevel@tonic-gate #include <sys/select.h>
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate #endif
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate #ifdef EMULATE_POLL_WITH_SELECT
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate # define POLL_CAN_READ (POLLIN | POLLRDNORM )
40*0Sstevel@tonic-gate # define POLL_CAN_WRITE (POLLOUT | POLLWRNORM | POLLWRBAND )
41*0Sstevel@tonic-gate # define POLL_HAS_EXCP (POLLRDBAND | POLLPRI )
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate # define POLL_EVENTS_MASK (POLL_CAN_READ | POLL_CAN_WRITE | POLL_HAS_EXCP)
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate int
poll(struct pollfd * fds,unsigned long nfds,int timeout)46*0Sstevel@tonic-gate poll(struct pollfd *fds, unsigned long nfds, int timeout)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate int i,err;
49*0Sstevel@tonic-gate fd_set rfd,wfd,efd,ifd;
50*0Sstevel@tonic-gate struct timeval timebuf;
51*0Sstevel@tonic-gate struct timeval *tbuf = (struct timeval *)0;
52*0Sstevel@tonic-gate int n = 0;
53*0Sstevel@tonic-gate int count;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate FD_ZERO(&ifd);
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate again:
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate FD_ZERO(&rfd);
60*0Sstevel@tonic-gate FD_ZERO(&wfd);
61*0Sstevel@tonic-gate FD_ZERO(&efd);
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate for(i = 0 ; i < (int)nfds ; i++) {
64*0Sstevel@tonic-gate int events = fds[i].events;
65*0Sstevel@tonic-gate int fd = fds[i].fd;
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate fds[i].revents = 0;
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate if(fd < 0 || FD_ISSET(fd, &ifd))
70*0Sstevel@tonic-gate continue;
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate if(fd > n)
73*0Sstevel@tonic-gate n = fd;
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate if(events & POLL_CAN_READ)
76*0Sstevel@tonic-gate FD_SET(fd, &rfd);
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate if(events & POLL_CAN_WRITE)
79*0Sstevel@tonic-gate FD_SET(fd, &wfd);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate if(events & POLL_HAS_EXCP)
82*0Sstevel@tonic-gate FD_SET(fd, &efd);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate if(timeout >= 0) {
86*0Sstevel@tonic-gate timebuf.tv_sec = timeout / 1000;
87*0Sstevel@tonic-gate timebuf.tv_usec = (timeout % 1000) * 1000;
88*0Sstevel@tonic-gate tbuf = &timebuf;
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate err = select(n+1,&rfd,&wfd,&efd,tbuf);
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate if(err < 0) {
94*0Sstevel@tonic-gate #ifdef HAS_FSTAT
95*0Sstevel@tonic-gate if(errno == EBADF) {
96*0Sstevel@tonic-gate for(i = 0 ; i < nfds ; i++) {
97*0Sstevel@tonic-gate struct stat buf;
98*0Sstevel@tonic-gate if((fstat(fds[i].fd,&buf) < 0) && (errno == EBADF)) {
99*0Sstevel@tonic-gate FD_SET(fds[i].fd, &ifd);
100*0Sstevel@tonic-gate goto again;
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate #endif /* HAS_FSTAT */
105*0Sstevel@tonic-gate return err;
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate count = 0;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate for(i = 0 ; i < (int)nfds ; i++) {
111*0Sstevel@tonic-gate int revents = (fds[i].events & POLL_EVENTS_MASK);
112*0Sstevel@tonic-gate int fd = fds[i].fd;
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate if(fd < 0)
115*0Sstevel@tonic-gate continue;
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate if(FD_ISSET(fd, &ifd))
118*0Sstevel@tonic-gate revents = POLLNVAL;
119*0Sstevel@tonic-gate else {
120*0Sstevel@tonic-gate if(!FD_ISSET(fd, &rfd))
121*0Sstevel@tonic-gate revents &= ~POLL_CAN_READ;
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate if(!FD_ISSET(fd, &wfd))
124*0Sstevel@tonic-gate revents &= ~POLL_CAN_WRITE;
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate if(!FD_ISSET(fd, &efd))
127*0Sstevel@tonic-gate revents &= ~POLL_HAS_EXCP;
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate if((fds[i].revents = revents) != 0)
131*0Sstevel@tonic-gate count++;
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate return count;
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate #endif /* EMULATE_POLL_WITH_SELECT */
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate /* gcc for SunOS 4 produces code from an empty (code/symbolwise)
140*0Sstevel@tonic-gate * source code file that makes the SunOS 4.x /usr/bin/ld fail with
141*0Sstevel@tonic-gate * ld: poll.o: premature EOF
142*0Sstevel@tonic-gate * To avoid this, have at least something in here. */
143*0Sstevel@tonic-gate #if defined(__sun) && !defined(__SVR4) && defined(__GNUC__)
144*0Sstevel@tonic-gate static int dummy;
145*0Sstevel@tonic-gate #endif
146*0Sstevel@tonic-gate
147