File: reconnect.c

package info (click to toggle)
rocks 2.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 284 kB
  • ctags: 412
  • sloc: ansic: 5,218; makefile: 111
file content (410 lines) | stat: -rw-r--r-- 8,721 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* 
 *  rocks/reconnect.c
 *
 *  Reconnection.
 *
 *  Copyright (C) 2001 Victor Zandy
 *  See COPYING for distribution terms.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/uio.h>

#include "rs.h"
#include "flight.h"
#include "log.h"

static void reconnect0(rs_t rs, block_t block);

/* c = a+b */
static void
tv_add(const struct timeval *a,
       const struct timeval *b,
       struct timeval *c)
{
	c->tv_sec = a->tv_sec + b->tv_sec;
	c->tv_usec = a->tv_usec + b->tv_usec;
	if (c->tv_usec >= 1000000) {
		c->tv_sec += 1;
		c->tv_usec -= 1000000;
	}
}

static void
set_timeout(rs_t rs)
{
	struct timeval tv;
	int rv;
	rv = gettimeofday(&tv, NULL);
	assert(!rv);
	tv_add(&tv, &rs->lim, &rs->tout);
	rs_log("reconnect timeout at %lu sec (cur %lu)",
	       rs->tout.tv_sec, tv.tv_sec);
}

/* Obtain a reconnection address and tell the other side about it */
int
rs_addr_exchange(rs_t rs)
{
	int len;

	/* bind up a fresh socket in any case */
	if (rs->rec_fd >= 0) {
		close(rs->rec_fd);
		rs->rec_fd = -1;
	}
	
	/* bind a server socket with IP address of the rock */
	rs->rec_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (0 > rs->rec_fd)
		goto err;
	/* Non blocking avoids the timing problem described in
	   Stevens Network Programming V1 (2nd ed.), sec. 15.6 */
	if (0 > rs_nonblock(rs->rec_fd, 1))
		assert(0);
	if (0 > rs_reuseaddr(rs->rec_fd))
		assert(0);
	len = sizeof(rs->sa_rl);
	if (0 > getsockname(rs->sd, (struct sockaddr *) &rs->sa_rl, &len))
		goto err;
	rs->sa_rl.sin_port = htons(0);
	if (0 > bind(rs->rec_fd, (struct sockaddr *) &rs->sa_rl,
		     sizeof(rs->sa_rl)))
		goto err;
	len = sizeof(rs->sa_rl);
	if (0 > getsockname(rs->rec_fd, (struct sockaddr *) &rs->sa_rl, &len))
		goto err;
	/* we don't listen until reconnection */

	/* Exchange reconnection addresses with peer - network order */
	if (0 >= rs_xwrite(rs->sd, &rs->sa_rl, sizeof(rs->sa_rl)))
		goto err;
	if (0 >= rs_xread(rs->sd, &rs->sa_rp, sizeof(rs->sa_rp), 0))
		goto err;
	return 0;
err:
	rs->rec_fd = -1;	
	return -1;
}

static int
reconnect(rs_t rs)
{
	int fd, lfd, rv;

	/* assume hb signals are already blocked */

	/* close everything except log,
	   link to parent, and current reconnection listener */
	lfd = rs_logfileno();
	for (fd = 0; fd < RS_MAXFD; fd++) {
		if (fd == lfd
		    || fd == rs->sd
		    || fd == rs->rec_fd)
			continue;
		close(fd);
	}
retry:
	/* establish connection with peer */
	fd = rs_1of2(&rs->sa_rl, &rs->sa_rp, rs->rec_fd, &rs->tout, rs->role);
	if (fd < 0)
		return -1; /* timeout */

	/* Authenticate */
#ifndef NO_AUTH
	if (rs_opt_auth) {
		rs_log("Reconnect: Authenticating");
		rv = rs_authenticate(rs->key, fd);
		if (0 >= rv) {
			rs_log("Authentication of incoming connection failed");
			close(fd);
			goto retry;
		}
	}
#endif
	return fd;
}

enum {
	REC_TIMEOUT, REC_OK
};

struct rec_msg {
	int fd;
	int stat;
};

/* send M over SD */
static int
send_rec_msg(int sd, struct rec_msg *m)
{
	struct cmsghdr *cmsg;
	struct msghdr msg;
	int buf[128];
	int len, rv;
	struct iovec iv;

	if (m->stat != REC_OK)
		/* no fd to pass */
		return rs_xwrite(sd, m, sizeof(*m));

	/* pass fd */
	assert(m->fd >= 0);
	bzero(&msg, sizeof(msg));
	len = CMSG_SPACE(sizeof(int));
	assert(len <= sizeof(buf));
	iv.iov_base = m;
	iv.iov_len = sizeof(*m);
	msg.msg_iov = &iv;
	msg.msg_iovlen = 1;
	msg.msg_control = buf;
	msg.msg_controllen = len;
	cmsg = CMSG_FIRSTHDR(&msg);
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_RIGHTS;
	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
	*(int *)CMSG_DATA(cmsg) = m->fd;
	msg.msg_controllen = cmsg->cmsg_len;
	rv = sendmsg(sd, &msg, 0);
	if (0 > rv)
		rs_log("sendmsg failed: %s (%d)", strerror(errno), errno);
	return rv;
}

/* receive a fd or error message from SD; return it or -1 on failure */
static int
recv_rec_msg(int sd, struct rec_msg *m)
{
	struct cmsghdr *cmsg;
	struct msghdr msg;
	struct iovec iv;
	int buf[128];
	int len;

	len = CMSG_SPACE(sizeof(int));
	assert(len <= sizeof(buf));
	bzero(&msg, sizeof(msg));
	iv.iov_base = m;
	iv.iov_len = sizeof(*m);
	msg.msg_iov = &iv;
	msg.msg_iovlen = 1;
	msg.msg_control = buf;
	msg.msg_controllen = len;
	if (0 > recvmsg(sd, &msg, 0))
		return -1;
	cmsg = CMSG_FIRSTHDR(&msg);
	if (!cmsg) {
		assert(m->stat != REC_OK);
		m->fd = -1;
		return 0;
	}
	assert(m->stat == REC_OK);
	assert(cmsg->cmsg_level == SOL_SOCKET);
	assert(cmsg->cmsg_type == SCM_RIGHTS);
	assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
	m->fd = *(int *)CMSG_DATA(cmsg);
	return 0;
}

/* Pass ROCK descriptor to parent (PPID) over FD, or notify parent of
   error if ROCK is negative. */
static void
notify(int rock, int fd, pid_t ppid)
{
	int rv;
	struct rec_msg rm;

	if (rock >= 0) {
		rm.fd = rock;
		rm.stat = REC_OK;
		rs_log("reconnection ok; notifying parent");
	} else {
		rm.fd = -1;
		rm.stat = REC_TIMEOUT;
		rs_log("reconnection timed out; notifying parent");
	}
	rv = send_rec_msg(fd, &rm);
	close(fd);
	if (0 > rv)
		rs_log("Unable to notify parent of reconnect status (1)\n");
	if (0 > kill(ppid, SIGURG))
		rs_log("Unable to notify parent of reconnect status (2)\n");
}

int rs_rec_failed(rs_t rs)
{
	return 0;
}

void
rs_rec_complete(rs_t rs, block_t block)
{
	int rv;
	int len;
	sigset_t hbsigs;
	struct rec_msg m;

	rs_log("enter rec_complete");
	if (0 > recv_rec_msg(rs->sd, &m))
		goto failed;
	if (m.stat == REC_TIMEOUT) {
		rs_log("rock <%p> hung up", rs);
		rs->state = RS_HUNGUP;
		return;
	}
	assert(m.fd != rs->sd);
	if (0 > dup2(m.fd, rs->sd))
		goto failed;
	close(m.fd);

	/* Heartbeat must be established and beating before in-flight
	   recovery so that full fault detection is available during
	   in flight recovery. */
	if (rs_opt_hb)
		if (0 > rs_hb_establish(rs->sd, rs->hb, rs->role))
			goto failed;
	rs->state = RS_ESTABLISHED;
	rs_resume_heartbeat(&hbsigs);

	/* obtain and exchange new reconnection addresses */
	if (0 > rs_addr_exchange(rs))
		goto failed;

	/* Update addresses */
	len = sizeof(rs->sa_locl);
	if (0 > getsockname(rs->sd, (struct sockaddr *) &rs->sa_locl, &len))
		goto failed;
	len = sizeof(rs->sa_locl);
	if (0 > getpeername(rs->sd, (struct sockaddr *) &rs->sa_peer, &len))
		goto failed;

	/* Finally recover in-flight data */
	if (rs_opt_flight) {
		rv = rs_inflight_recover(rs->sd, rs->ring,
					 rs->rcvseq, rs->sndseq,
					 &rs->maxsnd, &rs->maxrcv);
		if (0 > rv)
			goto failed;
	}
	rs_tty_print("reconnected");
	return;
failed:
	rs_log("reconnection completion failed");
	/* whatever the failure, try again */
	reconnect0(rs, block);
}

void
rs_wait_reconnect(rs_t rs)
{
	while (RS_SUSPENDED == rs->state)
		pause();
}

/* - FIXME: make it safe to migrate the child process, or not to;
            either way, the reconnect effort should proceed. 
   - assumes we are in native mode
*/ 


static void
reconnect0(rs_t rs, block_t block)
{
	pid_t pid;
	sigset_t hbsigs;
	sigset_t cur, old;
	int sv[2];

	assert(rs);

	/* suspend */
	sigemptyset(&cur);
	sigaddset(&cur, SIGCHLD);
	if (0 > sigprocmask(SIG_BLOCK, &cur, &old)) {
		rs_log("Cannot block SIGCHLD");
		assert(0);
	}
	rs_stop_heartbeat(&hbsigs);
	close(rs->sd);
	rs->state = RS_SUSPENDED;
	if (rs_opt_hb)
		rs_hb_cancel(rs->hb);
	if (0 > socketpair(PF_UNIX, SOCK_DGRAM, 0, sv)) {
		rs_log("Unable to create unix socketpair");
		assert(0);
		return;
	}

	pid = fork();
	if (0 > pid) {
		rs_log("reconnect unable to fork");
		assert(0);
		return;
	}
	if (!pid) {
		/* child - with signals still blocked */
		int ppid;
		int fd;

		ppid = getppid();
		/* fork again to reparent */
		pid = fork();
		if (0 > pid) {
			rs_log("reconnect unable to fork");
			exit(1);
		}
		if (pid)
			exit(0);

		close(sv[0]);
		if (sv[1] != rs->sd) {
			dup2(sv[1], rs->sd);
			close(sv[1]);
		}
		pid = getpid();
		if (0 > rs_xwrite(rs->sd, &pid, sizeof(pid))) {
			rs_log("unable to initialize reconnection daemon");
			exit(1);
		}
		fd = reconnect(rs);
		notify(fd, rs->sd, ppid);
		exit(0);
	}

	/* parent */ 
	close(sv[1]);
	if (sv[0] != rs->sd) {
		dup2(sv[0], rs->sd); /* dup child pipe to rock socket fd */
		close(sv[0]);
	}
	if (0 > rs_xread(rs->sd, &rs->rec_pid, sizeof(rs->rec_pid), 0)) {
		rs_log("unable to initialize reconnection daemon");
		assert(0);
	}
	if (rs->rec_fd)
		close(rs->rec_fd); /* child manages it now */
	rs_resume_heartbeat(&hbsigs);
	if (0 > sigprocmask(SIG_SETMASK, &old, NULL)) {
		rs_log("Cannot unblock SIGCHLD");
		assert(0);
	}
	waitpid(pid, NULL, 0);

	if (block == RS_BLOCK)
		rs_wait_reconnect(rs);
}

void
rs_reconnect(rs_t rs, block_t block)
{
	rs_tty_print("suspended", rs->sd);
	set_timeout(rs);
	reconnect0(rs, block);
}