This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.c
231 lines (212 loc) · 4.94 KB
/
select.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* rocks/select.c
*
* Sockets API implementation for select.
*
* Copyright (C) 2001 Victor Zandy
* See COPYING for distribution terms.
*/
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/poll.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "rs.h"
#include "log.h"
static void
choose(int fd, const fd_set *set,
int *rsn, int *sysn, fd_set *rs_set, fd_set *sys_set)
{
/* FIXME: I never understand what this does when I look at it.
Improve the variable names. */
rs_t rs;
assert(FD_ISSET(fd, set));
rs = rs_lookup(fd);
if (rs && RS_SUSPENDED == rs->state) {
FD_SET(fd, rs_set);
if (fd > *rsn)
*rsn = fd;
} else {
FD_SET(fd, sys_set);
if (fd > *sysn)
*sysn = fd;
}
}
/* NN is largest fd set in O, plus 1. N is same wrt S. If FD is set
in S or O, then upon return it is set in S, and N is the largest fd
set in S, plus 1. */
static void
merge_fdset(int *n, fd_set *s, int nn, const fd_set *o)
{
int i;
int max;
max = *n;
for (i = 0; i < nn; i++)
if (FD_ISSET(i, o)) {
FD_SET(i, s);
if (i+1 > max)
max = i+1;
}
*n = max;
}
/* Assumes native context. Returns 0 if we found and recovered a bad
rock among FDS. */
int
rs_recover_bad_rocks(int n, fd_set *fds)
{
int i;
rs_t rs;
fd_set t;
struct timeval tv;
int rv;
int ret;
ret = -1;
for (i = 0; i < n; i++) {
rs = rs_lookup(i);
if (!rs)
continue;
FD_ZERO(&t);
FD_SET(i, &t);
tv.tv_sec = tv.tv_usec = 0;
rv = select(i+1, &t, NULL, NULL, &tv);
if (0 > rv && errno == EBADF) {
rs_log("select badfd -> begin reconnect");
rs_reconnect(rs, RS_NOBLOCK);
ret = 0;
} else if (0 > rv)
assert(0); /* Unexpected */
}
return ret;
}
static void
check_spilled(int n, fd_set *rset, int *nspilled, fd_set *spilled)
{
int i;
for (i = 0; i < n; i++) {
rs_t x = rs_lookup(i);
if (x && FD_ISSET(i, rset) && (x->edpspill || x->clospill)) {
FD_SET(i, spilled);
*nspilled = i+1;
}
}
}
/* Test the fds in RP up to NN for rocks that have just
failed. Remove them from RP and return the number removed. */
static int
checkrocks(int nn, fd_set *rp)
{
int i;
rs_t rs;
int n = 0;
for (i = 0; i < nn; i++)
if (FD_ISSET(i, rp)
&& (rs = rs_lookup(i))
&& rs->state == RS_ESTABLISHED) {
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
if (0 > getpeername(i, (struct sockaddr *)&addr,
&len)) {
rs_reconnect(rs, RS_NOBLOCK);
FD_CLR(i, rp);
n++;
}
}
return n;
}
int
rs_select(int n, fd_set *rs, fd_set *ws, fd_set *es, struct timeval *tv)
{
int rv;
int i, rsn, sysn, nn;
fd_set rsrs, rsws, rses; /* suspended rs descriptors */
fd_set spilled; /* rocks with a non-empty spill ring */
int nspilled; /* max fd set in spilled plus 1 */
fd_set sysrs, sysws, syses; /* descriptors for kernel to test */
fd_set args[3], *rp, *wp, *ep;
int caller_fdsn;
/* Don't waste time if caller just wants timing */
if (n == 0 || (!rs && !ws && !es))
return select(n, rs, ws, es, tv);
nspilled = 0;
FD_ZERO(&spilled);
if (rs)
check_spilled(n, rs, &nspilled, &spilled);
retry:
rsn = sysn = 0;
FD_ZERO(&rsrs);
FD_ZERO(&rsws);
FD_ZERO(&rses);
FD_ZERO(&sysrs);
FD_ZERO(&sysws);
FD_ZERO(&syses);
/* Separate suspended rs descriptors */
for (i = 0; i < n; i++) {
if (rs && FD_ISSET(i, rs))
choose(i, rs, &rsn, &sysn, &rsrs, &sysrs);
if (ws && FD_ISSET(i, ws))
choose(i, ws, &rsn, &sysn, &rsws, &sysws);
if (es && FD_ISSET(i, es))
choose(i, es, &rsn, &sysn, &rses, &syses);
}
/* FIXME: Non portable select semantics: on Linux, interrupted
select returns the time not slept. */
if (sysn > 0) {
rp = &args[0];
wp = &args[1];
ep = &args[2];
memcpy(rp, &sysrs, sizeof(fd_set));
memcpy(wp, &sysws, sizeof(fd_set));
memcpy(ep, &syses, sizeof(fd_set));
nn = sysn + 1;
} else {
rp = wp = ep = NULL;
nn = 0;
}
rv = select(nn, rp, wp, ep, tv);
if (0 > rv && errno == EINTR)
goto retry;
/* Bad descriptors can arise following a checkpoint restart */
if (0 > rv && errno == EBADF) {
int m = 0;
fd_set s;
rs_log("Select came back with bad fds");
FD_ZERO(&s);
if (rp)
merge_fdset(&m, &s, nn, rp);
if (wp)
merge_fdset(&m, &s, nn, wp);
if (ep)
merge_fdset(&m, &s, nn, ep);
if (!rs_recover_bad_rocks(m, &s))
goto retry;
/* Otherwise, the bad fd is the application's problem */
}
if (rv >= 0 && rp && nspilled > 0)
merge_fdset(&n, rp, nspilled, &spilled);
if (rv > 0 && rp) {
/* check for newly failed rocks */
rv -= checkrocks(nn, rp);
if (!rv)
goto retry;
}
/* Copy results to caller. Since not every caller passes in a
whole fd_set, do a minimal copy. */
caller_fdsn = n / 8;
if (n % 8)
++caller_fdsn;
if (rs && rp)
memcpy(rs, rp, caller_fdsn);
if (ws && wp)
memcpy(ws, wp, caller_fdsn);
if (es && ep)
memcpy(es, ep, caller_fdsn);
return rv;
}
int
rs_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
{
assert(0);
return 0;
}