emulab-sync.c 6.3 KB
Newer Older
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2004 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * All rights reserved.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
27
#include <setjmp.h>
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "decls.h"
#include "config.h"

#ifndef SYNCSERVER
#define SYNCSERVER	"/var/emulab/boot/syncserver"
#endif

static int	debug = 0;

/* Forward decls */
static int	doudp(barrier_req_t *, struct in_addr, int);
static int	dotcp(barrier_req_t *, struct in_addr, int);

41
42
43
44
45
/* For connect timeouts. */
static jmp_buf	deadline;
static int	deadsyncdflag;
static void	deadsyncd(int sig);

46
47
48
49
50
51
52
char *usagestr = 
 "usage: emulab-sync [options]\n"
 " -n <name>       Optional barrier name; must be less than 64 bytes long\n"
 " -d		   Turn on debugging\n"
 " -s server	   Specify a sync server to connect to\n"
 " -p portnum	   Specify a port number to connect to\n"
 " -i count	   Initialize named barrier to count waiters\n"
53
 " -a              Master returns immediately when initializing barrier\n"
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 " -u		   Use UDP instead of TCP\n"
 "\n";

void
usage()
{
	fprintf(stderr, usagestr);
	exit(1);
}

int
main(int argc, char **argv)
{
	int			n, ch;
	struct hostent		*he;
	FILE			*fp;
	struct in_addr		serverip;
	int			portnum = SERVER_PORTNUM;
	int			useudp  = 0;
	char			*server = NULL;
	int			initme  = 0;
75
	int			nowait  = 0;
76
77
78
	char			*barrier= DEFAULT_BARRIER;
	barrier_req_t		barrier_req;
	
79
	while ((ch = getopt(argc, argv, "ds:p:ui:n:a")) != -1)
80
		switch(ch) {
81
82
83
		case 'a':
			nowait++;
			break;
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
		case 'd':
			debug++;
			break;
		case 'p':
			portnum = atoi(optarg);
			break;
		case 'i':
			initme = atoi(optarg);
			break;
		case 'n':
			barrier = optarg;
			break;
		case 's':
			server = optarg;
			break;
		case 'u':
			useudp = 1;
			break;
		default:
			usage();
		}

	argv += optind;
	argc -= optind;

	if (argc)
		usage();

	if (strlen(barrier) >= sizeof(barrier_req.name))
		usage();

	/*
	 * Look for the syncserver access file.
	 */
	if (!server &&
	    (access(SYNCSERVER, R_OK) == 0) &&
	    ((fp = fopen(SYNCSERVER, "r")) != NULL)) {
		char	buf[BUFSIZ], *bp;
		
		if (fgets(buf, sizeof(buf), fp)) {
			if ((bp = strchr(buf, '\n')))
				*bp = (char) NULL;

			/*
			 * Look for port spec
			 */
			if ((bp = strchr(buf, ':'))) {
				*bp++   = (char) NULL;
				portnum = atoi(bp);
			}
			server = strdup(buf);
		}
		fclose(fp);
	}
	if (!server)
		usage();
	
	/*
	 * Map server to IP.
	 */
	he = gethostbyname(server);
	if (he)
		memcpy((char *)&serverip, he->h_addr, he->h_length);
	else {
		fprintf(stderr, "gethostbyname(%s) failed\n", server); 
		exit(1);
	}

	/*
	 * Build up the request structure to send to the server.
	 */
155
	bzero(&barrier_req, sizeof(barrier_req));
156
157
158
159
	strcpy(barrier_req.name, barrier);
	if (initme) {
		barrier_req.request = BARRIER_INIT;
		barrier_req.count   = initme;
160
161
		if (nowait)
			barrier_req.flags = BARRIER_INIT_NOWAIT;
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
	}
	else
		barrier_req.request = BARRIER_WAIT;
	
	if (useudp)
		n = doudp(&barrier_req, serverip, portnum);
	else
		n = dotcp(&barrier_req, serverip, portnum);
	exit(n);
}

/*
 * TCP version, which uses ssl if compiled in.
 */
static int
dotcp(barrier_req_t *barrier_reqp, struct in_addr serverip, int portnum)
{
	int			sock, n, cc;
	struct sockaddr_in	name;
	char			*bp, buf[BUFSIZ];
	
	while (1) {
		/* Create socket from which to read. */
		sock = socket(AF_INET, SOCK_STREAM, 0);
		if (sock < 0) {
			perror("creating stream socket:");
			return -1;
		}

		/* Create name. */
		name.sin_family = AF_INET;
		name.sin_addr   = serverip;
		name.sin_port   = htons(portnum);

196
197
198
199
200
201
202
		/* For alarm. */
		deadsyncdflag = 0;
		signal(SIGALRM, deadsyncd);
		if (setjmp(deadline)) {
			alarm(0);
			signal(SIGALRM, SIG_DFL);
			goto refused;
203
		}
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
		alarm(5);

		cc = connect(sock, (struct sockaddr *) &name, sizeof(name));
		/*
		 * If we get the alarm now, no problem. We will close the
		 * connection and try again after a short wait. The server
		 * will recongnize this for what is, not recording any state
		 * until we actually send it the request below (write to sock).
		 */
		alarm(0);
		signal(SIGALRM, SIG_DFL);

		/* Success */
		if (cc == 0)
			break;

220
		if (errno != ECONNREFUSED && errno != ETIMEDOUT) {
221
222
223
224
			perror("connecting stream socket");
			close(sock);
			return -1;
		}
225
	refused:
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
		close(sock);
		if (debug) 
			fprintf(stderr, "Connection refused. Waiting ...\n");
		sleep(5);
	}

	n = 1;
	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &n, sizeof(n)) < 0) {
		perror("setsockopt SO_KEEPALIVE");
		goto bad;
	}

	/*
	 * Write the command to the socket and wait for the response.
	 */
	bp = (char *) barrier_reqp;
	n  = sizeof(*barrier_reqp);
	while (n) {
		if ((cc = write(sock, bp, n)) <= 0) {
			if (cc < 0) {
				perror("Writing to socket:");
				goto bad;
			}
			fprintf(stderr, "write aborted");
			goto bad;
		}
		bp += cc;
		n  -= cc;
	}

	while (1) {
		if ((cc = read(sock, buf, sizeof(buf) - 1)) <= 0) {
			if (cc < 0) {
				perror("Reading from socket:");
				goto bad;
			}
			break;
		}
	}
	close(sock);
	return 0;
 bad:
	close(sock);
	return -1;
}

/*
 * Not very robust ...
 */
static int
doudp(barrier_req_t *barrier_reqp, struct in_addr serverip, int portnum)
{
	int			sock, length, n, cc;
	struct sockaddr_in	name, client;
	char			buf[BUFSIZ];

	/* Create socket from which to read. */
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		perror("creating dgram socket:");
		return -1;
	}

	/* Create name. */
	name.sin_family = AF_INET;
	name.sin_addr   = serverip;
	name.sin_port   = htons(portnum);

	/*
	 * Write the command to the socket and wait for the response
	 */
	n  = sizeof(*barrier_reqp);
	cc = sendto(sock, barrier_reqp,
		    n, 0, (struct sockaddr *)&name, sizeof(name));
	if (cc != n) {
		if (cc < 0) {
			perror("Writing to socket:");
			return -1;
		}
		fprintf(stderr, "short write (%d != %d)\n", cc, n);
		return -1;
	}

	cc = recvfrom(sock, buf, sizeof(buf) - 1, 0,
		      (struct sockaddr *)&client, &length);

	if (cc < 0) {
		perror("Reading from socket:");
		return -1;
	}
	close(sock);
	return 0;
}
319
320
321
322
323
324
325

void
deadsyncd(int sig)
{
	deadsyncdflag = 1;
	longjmp(deadline, 1);
}