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