Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcs/cspoll.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* Glenn Fowler
23
* AT&T Research
24
*
25
* poll for read/write/control on fds with ms timeout
26
* number of selected fd's returned
27
*/
28
29
#include "cslib.h"
30
31
#ifdef POLLIST
32
#undef CS_LIB_SOCKET
33
#undef CS_LIB_STREAM
34
#undef CS_LIB_V10
35
#undef _sys_select
36
#define CS_LIB_STREAM 1
37
#endif
38
39
#if _sys_select && !defined(FD_SET)
40
#include <sys/select.h>
41
#endif
42
43
int
44
cspoll(Cs_t* state, Cspoll_t* fds, int num, int ms)
45
{
46
register Cspoll_t* pp;
47
register Cspoll_t* mp;
48
49
#if CS_LIB_SOCKET || CS_LIB_V10
50
register int events;
51
register int width;
52
fd_set rd;
53
register fd_set* rp = &rd;
54
fd_set wr;
55
register fd_set* wp = &wr;
56
#if CS_LIB_SOCKET
57
fd_set ex;
58
register fd_set* ep = &ex;
59
struct timeval* tp;
60
struct timeval tv;
61
62
if (ms >= 0)
63
{
64
tv.tv_sec = ms / 1000;
65
tv.tv_usec = (ms % 1000) * 1000;
66
tp = &tv;
67
}
68
else tp = 0;
69
FD_ZERO(ep);
70
#endif
71
FD_ZERO(rp);
72
FD_ZERO(wp);
73
events = width = 0;
74
for (mp = (pp = fds) + num; pp < mp; pp++)
75
if (pp->fd >= 0)
76
{
77
if (pp->fd > width)
78
width = pp->fd;
79
if (pp->events & CS_POLL_READ)
80
{
81
events |= CS_POLL_READ;
82
FD_SET(pp->fd, rp);
83
}
84
if (pp->events & CS_POLL_WRITE)
85
{
86
events |= CS_POLL_WRITE;
87
FD_SET(pp->fd, wp);
88
}
89
if (pp->events & CS_POLL_CONTROL)
90
{
91
events |= CS_POLL_CONTROL;
92
#if CS_LIB_SOCKET
93
FD_SET(pp->fd, ep);
94
#endif
95
}
96
}
97
if (!(events & CS_POLL_WRITE))
98
wp = 0;
99
#if CS_LIB_SOCKET
100
if (!(events & CS_POLL_READ))
101
rp = 0;
102
if (!(events & CS_POLL_CONTROL))
103
ep = 0;
104
messagef((state->id, NiL, -6, "poll: %s num=%d ms=%d sec=%d usec=%d", fmttime("%K", CSTIME()), num, ms, tp ? tp->tv_sec : 0, tp ? tp->tv_usec : 0));
105
num = select(width + 1, rp, wp, ep, tp);
106
#else
107
if (!(events & (CS_POLL_READ|CS_POLL_CONTROL)))
108
rp = 0;
109
messagef((state->id, NiL, -6, "poll: %s num=%d ms=%d", fmttime("%K", CSTIME()), num, ms);
110
num = select(width + 1, rp, wp, ms);
111
#endif
112
messagef((state->id, NiL, -6, "poll: %s num=%d", fmttime("%K", CSTIME()), num));
113
if (num < 0)
114
messagef((state->id, NiL, -1, "poll: select error"));
115
else
116
for (num = 0, pp = fds; pp < mp; pp++)
117
{
118
pp->status = 0;
119
if (pp->fd >= 0)
120
{
121
if (rp && FD_ISSET(pp->fd, rp))
122
{
123
#if CS_LIB_V10
124
long n;
125
126
if ((pp->event & CS_POLL_CONTROL) && ioctl(fd, FIONREAD, &n))
127
pp->status |= CS_POLL_CLOSE;
128
else if (!n)
129
pp->status |= CS_POLL_CONTROL;
130
else
131
#endif
132
pp->status |= CS_POLL_READ;
133
}
134
if (wp && FD_ISSET(pp->fd, wp))
135
pp->status |= CS_POLL_WRITE;
136
#if CS_LIB_SOCKET
137
if (ep && FD_ISSET(pp->fd, ep))
138
pp->status |= CS_POLL_CONTROL;
139
#endif
140
if (pp->status)
141
{
142
pp->status |= pp->events & (CS_POLL_AUTH|CS_POLL_CONNECT|CS_POLL_USER);
143
num++;
144
}
145
}
146
}
147
return num;
148
149
#else
150
151
#if CS_LIB_STREAM
152
153
int n;
154
155
#if _lib_poll_fd_2
156
n = poll(num, fds, ms);
157
#else
158
n = poll(fds, num, ms);
159
#endif
160
if (n < 0)
161
messagef((state->id, NiL, -1, "poll: poll error"));
162
else if (n > 0)
163
{
164
int i;
165
#ifdef RS_HIPRI
166
struct strbuf buf;
167
168
buf.maxlen = 0;
169
#endif
170
for (mp = (pp = fds) + num, i = n; pp < mp; pp++)
171
{
172
if (pp->status)
173
{
174
pp->status |= pp->events & (CS_POLL_AUTH|CS_POLL_CONNECT|CS_POLL_USER);
175
#ifdef RS_HIPRI
176
if (pp->status & CS_POLL_CONTROL)
177
{
178
int f = RS_HIPRI;
179
180
if (getmsg(pp->fd, NiL, &buf, &f))
181
pp->status &= ~CS_POLL_CONTROL;
182
}
183
#endif
184
if (--i <= 0)
185
break;
186
}
187
}
188
}
189
return n;
190
191
#else
192
193
errno = EINVAL;
194
messagef((state->id, NiL, -1, "poll: not supported"));
195
return -1;
196
197
#endif
198
199
#endif
200
201
}
202
203
int
204
_cs_poll(Cspoll_t* fds, int num, int ms)
205
{
206
return cspoll(&cs, fds, num, ms);
207
}
208
209