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
|
/*
* 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"
/*
Semantics:
Current: Ignore suspended reliable sockets.
Future: Reconnect suspended reliable sockets if no other
descriptors are ready.
*/
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 > max)
max = i;
}
*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;
}
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 sysrs, sysws, syses; /* other descriptors */
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);
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 n = 0;
fd_set s;
rs_log("Select came back with bad fds\n");
FD_ZERO(&s);
if (rp)
merge_fdset(&n, &s, nn, rp);
if (wp)
merge_fdset(&n, &s, nn, wp);
if (ep)
merge_fdset(&n, &s, nn, ep);
assert(0); /* FIXME: figure out whether reconnect
should block */
if (!rs_recover_bad_rocks(n, &s))
goto retry;
/* Otherwise, the bad fd is the application's problem */
}
/* 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;
}
|