1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "sfhdr.h"
23da2e3ebdSchin
24da2e3ebdSchin /* Poll a set of streams to see if any is available for I/O.
25da2e3ebdSchin ** Ready streams are moved to front of array but retain the
26da2e3ebdSchin ** same relative order.
27da2e3ebdSchin **
28da2e3ebdSchin ** Written by Kiem-Phong Vo.
29da2e3ebdSchin */
30da2e3ebdSchin
31da2e3ebdSchin #if __STD_C
sfpoll(Sfio_t ** fa,reg int n,int tm)32da2e3ebdSchin int sfpoll(Sfio_t** fa, reg int n, int tm)
33da2e3ebdSchin #else
34da2e3ebdSchin int sfpoll(fa, n, tm)
35da2e3ebdSchin Sfio_t** fa; /* array of streams to poll */
36da2e3ebdSchin reg int n; /* number of streams in array */
37da2e3ebdSchin int tm; /* time in millisecs for select/poll */
38da2e3ebdSchin #endif
39da2e3ebdSchin {
40da2e3ebdSchin reg int r, c, m, np;
41da2e3ebdSchin reg Sfio_t* f;
42da2e3ebdSchin reg int *status, *check;
43da2e3ebdSchin
44da2e3ebdSchin if(n <= 0 || !fa)
45da2e3ebdSchin return -1;
46da2e3ebdSchin
47da2e3ebdSchin if(!(status = (int*)malloc(2*n*sizeof(int))) )
48da2e3ebdSchin return -1;
49da2e3ebdSchin check = status+n; /* streams that need polling */
50da2e3ebdSchin
51da2e3ebdSchin /* a SF_READ stream is ready if there is buffered read data */
52da2e3ebdSchin #define RDREADY(f) (((f->mode&SF_READ) && f->next < f->endb) || \
53da2e3ebdSchin ((f->mode&SF_WRITE) && f->proc && f->proc->ndata > 0) )
54da2e3ebdSchin
55da2e3ebdSchin /* a SF_WRITE stream is ready if there is no write data */
56da2e3ebdSchin #define WRREADY(f) (!(f->mode&SF_WRITE) || f->next == f->data)
57da2e3ebdSchin
58da2e3ebdSchin #define HASAUXFD(f) (f->proc && f->proc->file >= 0 && f->proc->file != f->file)
59da2e3ebdSchin
60da2e3ebdSchin for(r = c = 0; r < n; ++r) /* compute streams that must be checked */
61da2e3ebdSchin { f = fa[r];
62da2e3ebdSchin status[r] = 0;
63da2e3ebdSchin
64da2e3ebdSchin /* check accessibility */
65da2e3ebdSchin m = f->mode&SF_RDWR;
66da2e3ebdSchin if((int)f->mode != m && _sfmode(f,m,0) < 0)
67da2e3ebdSchin continue;
68da2e3ebdSchin
69da2e3ebdSchin if((f->flags&SF_READ) && RDREADY(f))
70da2e3ebdSchin status[r] |= SF_READ;
71da2e3ebdSchin
72da2e3ebdSchin if((f->flags&SF_WRITE) && WRREADY(f))
73da2e3ebdSchin status[r] |= SF_WRITE;
74da2e3ebdSchin
75da2e3ebdSchin if((f->flags&SF_RDWR) == status[r])
76da2e3ebdSchin continue;
77da2e3ebdSchin
78da2e3ebdSchin /* has discipline, ask its opinion */
79da2e3ebdSchin if(f->disc && f->disc->exceptf)
80da2e3ebdSchin { if((m = (*f->disc->exceptf)(f,SF_DPOLL,&tm,f->disc)) < 0)
81da2e3ebdSchin continue;
82da2e3ebdSchin else if(m > 0)
83da2e3ebdSchin { status[r] = m&SF_RDWR;
84da2e3ebdSchin continue;
85da2e3ebdSchin }
86da2e3ebdSchin }
87da2e3ebdSchin
88da2e3ebdSchin if(f->extent < 0) /* unseekable stream, must poll/select */
89da2e3ebdSchin check[c++] = r;
90da2e3ebdSchin else /* seekable streams are always ready */
91da2e3ebdSchin { if(f->flags&SF_READ)
92da2e3ebdSchin status[r] |= SF_READ;
93da2e3ebdSchin if(f->flags&SF_WRITE)
94da2e3ebdSchin status[r] |= SF_WRITE;
95da2e3ebdSchin }
96da2e3ebdSchin }
97da2e3ebdSchin
98da2e3ebdSchin np = -1;
99da2e3ebdSchin #if _lib_poll
100da2e3ebdSchin if(c > 0)
101da2e3ebdSchin { struct pollfd* fds;
102da2e3ebdSchin
103da2e3ebdSchin /* construct the poll array */
104da2e3ebdSchin for(m = 0, r = 0; r < c; ++r, ++m)
105da2e3ebdSchin { f = fa[check[r]];
106da2e3ebdSchin if(HASAUXFD(f))
107da2e3ebdSchin m += 1;
108da2e3ebdSchin }
109da2e3ebdSchin if(!(fds = (struct pollfd*)malloc(m*sizeof(struct pollfd))) )
110da2e3ebdSchin return -1;
111da2e3ebdSchin
112da2e3ebdSchin for(m = 0, r = 0; r < c; ++r, ++m)
113da2e3ebdSchin { f = fa[check[r]];
114da2e3ebdSchin
115da2e3ebdSchin fds[m].fd = f->file;
116da2e3ebdSchin fds[m].events = fds[m].revents = 0;
117da2e3ebdSchin
118da2e3ebdSchin if((f->flags&SF_WRITE) && !WRREADY(f) )
119da2e3ebdSchin fds[m].events |= POLLOUT;
120da2e3ebdSchin
121da2e3ebdSchin if((f->flags&SF_READ) && !RDREADY(f) )
122da2e3ebdSchin { /* a sfpopen situation with two file descriptors */
123da2e3ebdSchin if((f->mode&SF_WRITE) && HASAUXFD(f))
124da2e3ebdSchin { m += 1;
125da2e3ebdSchin fds[m].fd = f->proc->file;
126da2e3ebdSchin fds[m].revents = 0;
127da2e3ebdSchin }
128da2e3ebdSchin
129da2e3ebdSchin fds[m].events |= POLLIN;
130da2e3ebdSchin }
131da2e3ebdSchin }
132da2e3ebdSchin
133da2e3ebdSchin while((np = SFPOLL(fds,m,tm)) < 0 )
134da2e3ebdSchin { if(errno == EINTR || errno == EAGAIN)
135da2e3ebdSchin errno = 0;
136da2e3ebdSchin else break;
137da2e3ebdSchin }
138da2e3ebdSchin if(np > 0) /* poll succeeded */
139da2e3ebdSchin np = c;
140da2e3ebdSchin
141da2e3ebdSchin for(m = 0, r = 0; r < np; ++r, ++m)
142da2e3ebdSchin { f = fa[check[r]];
143da2e3ebdSchin
144da2e3ebdSchin if((f->flags&SF_WRITE) && !WRREADY(f) )
145da2e3ebdSchin { if(fds[m].revents&POLLOUT)
146da2e3ebdSchin status[check[r]] |= SF_WRITE;
147da2e3ebdSchin }
148da2e3ebdSchin
149da2e3ebdSchin if((f->flags&SF_READ) && !RDREADY(f))
150da2e3ebdSchin { if((f->mode&SF_WRITE) && HASAUXFD(f))
151da2e3ebdSchin m += 1;
152da2e3ebdSchin if(fds[m].revents&POLLIN)
153da2e3ebdSchin status[check[r]] |= SF_READ;
154da2e3ebdSchin }
155da2e3ebdSchin }
156da2e3ebdSchin
157da2e3ebdSchin free((Void_t*)fds);
158da2e3ebdSchin }
159da2e3ebdSchin #endif /*_lib_poll*/
160da2e3ebdSchin
161da2e3ebdSchin #if _lib_select
162da2e3ebdSchin if(np < 0 && c > 0)
163da2e3ebdSchin { fd_set rd, wr;
164da2e3ebdSchin struct timeval tmb, *tmp;
165da2e3ebdSchin
166da2e3ebdSchin FD_ZERO(&rd);
167da2e3ebdSchin FD_ZERO(&wr);
168da2e3ebdSchin m = 0;
169da2e3ebdSchin for(r = 0; r < c; ++r)
170da2e3ebdSchin { f = fa[check[r]];
171da2e3ebdSchin
172da2e3ebdSchin if(f->file > m)
173da2e3ebdSchin m = f->file;
174da2e3ebdSchin
175da2e3ebdSchin if((f->flags&SF_WRITE) && !WRREADY(f))
176da2e3ebdSchin FD_SET(f->file,&wr);
177da2e3ebdSchin
178da2e3ebdSchin if((f->flags&SF_READ) && !RDREADY(f))
179da2e3ebdSchin { if((f->mode&SF_WRITE) && HASAUXFD(f))
180da2e3ebdSchin { if(f->proc->file > m)
181da2e3ebdSchin m = f->proc->file;
182da2e3ebdSchin FD_SET(f->proc->file, &rd);
183da2e3ebdSchin }
184da2e3ebdSchin else FD_SET(f->file,&rd);
185da2e3ebdSchin }
186da2e3ebdSchin }
187da2e3ebdSchin if(tm < 0)
188da2e3ebdSchin tmp = NIL(struct timeval*);
189da2e3ebdSchin else
190da2e3ebdSchin { tmp = &tmb;
191da2e3ebdSchin tmb.tv_sec = tm/SECOND;
192da2e3ebdSchin tmb.tv_usec = (tm%SECOND)*SECOND;
193da2e3ebdSchin }
194da2e3ebdSchin
195da2e3ebdSchin while((np = select(m+1,&rd,&wr,NIL(fd_set*),tmp)) < 0 )
196da2e3ebdSchin { if(errno == EINTR)
197da2e3ebdSchin errno = 0;
198da2e3ebdSchin else break;
199da2e3ebdSchin }
200da2e3ebdSchin if(np > 0)
201da2e3ebdSchin np = c;
202da2e3ebdSchin
203da2e3ebdSchin for(r = 0; r < np; ++r)
204da2e3ebdSchin { f = fa[check[r]];
205da2e3ebdSchin
206da2e3ebdSchin if((f->flags&SF_WRITE) && !WRREADY(f) )
207da2e3ebdSchin { if(FD_ISSET(f->file,&wr) )
208da2e3ebdSchin status[check[r]] |= SF_WRITE;
209da2e3ebdSchin }
210da2e3ebdSchin
211da2e3ebdSchin if((f->flags&SF_READ) && !RDREADY(f) )
212da2e3ebdSchin { if((f->mode&SF_WRITE) && HASAUXFD(f) )
213da2e3ebdSchin { if(FD_ISSET(f->proc->file, &rd) )
214da2e3ebdSchin status[check[r]] |= SF_READ;
215da2e3ebdSchin }
216da2e3ebdSchin else
217da2e3ebdSchin { if(FD_ISSET(f->file,&rd) )
218da2e3ebdSchin status[check[r]] |= SF_READ;
219da2e3ebdSchin }
220da2e3ebdSchin }
221da2e3ebdSchin }
222da2e3ebdSchin }
223da2e3ebdSchin #endif /*_lib_select*/
224da2e3ebdSchin
225da2e3ebdSchin for(r = c = 0; c < n; ++c)
226da2e3ebdSchin { if(status[c] == 0)
227da2e3ebdSchin continue;
228da2e3ebdSchin
229da2e3ebdSchin f = fa[c];
230da2e3ebdSchin f->val = (ssize_t)status[c];
231da2e3ebdSchin
232da2e3ebdSchin /* announce status */
233da2e3ebdSchin if(f->disc && f->disc->exceptf)
234da2e3ebdSchin (*f->disc->exceptf)(f,SF_READY,(Void_t*)(long)status[c],f->disc);
235da2e3ebdSchin
236da2e3ebdSchin if(c > r) /* move to front of list */
237da2e3ebdSchin { fa[c] = fa[r];
238da2e3ebdSchin fa[r] = f;
239da2e3ebdSchin }
240da2e3ebdSchin r += 1;
241da2e3ebdSchin }
242da2e3ebdSchin
243da2e3ebdSchin free((Void_t*)status);
244da2e3ebdSchin return r;
245da2e3ebdSchin }
246