File: glue-dist.c

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (350 lines) | stat: -rw-r--r-- 6,811 bytes parent folder | download | duplicates (2)
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
/*
 * $Id: glue-dist.c,v 1.12 2009-05-18 09:03:42 vrsieh Exp $
 *
 * Copyright (C) 2003-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include <assert.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "glue-dist.h"
#include "glue-io.h"
#include "glue-main.h"

static char def_user[1024];
static char def_host[1024];
static char def_path[1024];

static struct {
	char *name;
	char *user;
	char *host;
	char *path;
	pid_t pid;
	int io;
	int err;
	unsigned int port;
	int fd;
} dist_node[64];
static unsigned int dist_node_count = 0;
static unsigned int myself;

static void
dist_err(int fd, void *_cpssp)
{
	char buffer[256];
	int len;
	int ret;

	for (;;) {
		len = read(fd, buffer, sizeof(buffer));
		if (len <= 0) {
			break;
		}
		ret = write(2, buffer, len);
		assert(ret == len);
	}
}

int
dist_node_lookup(const char *name)
{
	unsigned int i;

	for (i = 0; ; i++) {
		if (i == dist_node_count) {
			/* Not found. */
			return -1;
		}
		if (strcmp(dist_node[i].name, name) == 0) {
			/* Found. */
			return i;
		}
	}
}

int
dist_node_create(
	const char *name,
	const char *user,
	const char *host,
	const char *path
)
{
	unsigned int i;
	char buf[32];

	assert(name);
	if (! user) user = def_user;
	if (! host || strcmp(host, "localhost") == 0) host = def_host;
	if (! path) path = def_path;

	if (dist_node_count == 0
	 && (strcmp(def_user, user) != 0
	  || strcmp(def_host, host) != 0)) {
		/* Configuration error. */
		fprintf(stderr, "%s: ERROR: First configured host must be local host!\n",
				progname);
		return -1;
	}

	for (i = 0; i < dist_node_count; i++) {
		if (strcmp(dist_node[i].name, name) == 0) {
			/* Node with same name found. */
			return -1;
		}
		if (strcmp(dist_node[i].host, host) == 0
		 && strcmp(dist_node[i].path, path) == 0) {
			/* Node with same host/path setting found */
			return -1;
		}
	}

	if (dist_node_count == 0) {
		/*
		 * Use this process.
		 */

	} else {
		/*
		 * Create new process.
		 */
		int io[2];
		int err[2];
		int ret;

		ret = socketpair(PF_UNIX, SOCK_STREAM, 0, io);
		assert(0 <= ret);
		ret = socketpair(PF_UNIX, SOCK_STREAM, 0, err);
		assert(0 <= ret);

		switch (dist_node[i].pid = fork()) {
		case -1:
			/* Error */
			return -1;
		case 0:
			/* Child */
			ret = close(0);
			assert(0 <= ret);
			ret = dup(io[0]);
			assert(ret == 0);

			ret = close(1);
			assert(0 <= ret);
			ret = dup(io[0]);
			assert(ret == 1);

			ret = close(2);
			assert(0 <= ret);
			ret = dup(err[0]);
			assert(ret == 2);

			ret = close(io[0]);
			assert(0 <= ret);
			ret = close(io[1]);
			assert(0 <= ret);

			ret = close(err[0]);
			assert(0 <= ret);
			ret = close(err[1]);
			assert(0 <= ret);

			if (strcmp(def_user, user) != 0
			 || strcmp(def_host, host) != 0) {
				/*
				 * Start with different user name
				 * or
				 * start on remote host.
				 */
				/* Start program via ssh... */
				char cmd[3*1024];

				sprintf(cmd, "env PATH=%s %s -s -B %s",
					getenv("PATH"), progname, path);
				execlp("ssh", "ssh", "-l", user, host,
					cmd, (char *) NULL);
				fprintf(stderr, "%s: ERROR: \"ssh -l %s %s %s -s -B %s\" failed: %s.\n",
						progname,
						user, host, progname, path,
						strerror(errno));
			} else {
				/*
				 * Start with same user name
				 * and
				 * start on localhost.
				 */
				/* Start program directly... */
				execlp(progname,
					progname, "-s", "-B", path,
					(char *) NULL);
				fprintf(stderr, "%s: ERROR: \"%s -s -B %s\" failed: %s.\n",
						progname, progname, path,
						strerror(errno));
			}
			_exit(1);

		default:
			/* Parent */
			dist_node[i].io = io[1];
			dist_node[i].err = err[1];
			ret = close(io[0]);
			assert(0 <= ret);
			ret = close(err[0]);
			assert(0 <= ret);

			sprintf(buf, "%u", i);
			ret = write(dist_node[i].io, buf, sizeof(buf));
			if (ret != sizeof(buf)) {
				assert(0); /* FIXME */
			}

			ret = read(dist_node[i].io, buf, sizeof(buf));
			if (ret != sizeof(buf)) {
				assert(0); /* FIXME */
			}
			dist_node[i].port = atoi(buf);
			assert(1024 <= dist_node[i].port
			    && dist_node[i].port < 0x10000);

			/* proc_register(...); FIXME */
			io_register(err[1], (void *) 1, dist_err);
			break;
		}
	}

	dist_node[dist_node_count].name = strdup(name);
	assert(dist_node[dist_node_count].name);
	dist_node[dist_node_count].user = strdup(user);
	assert(dist_node[dist_node_count].user);
	dist_node[dist_node_count].host = strdup(host);
	assert(dist_node[dist_node_count].host);
	dist_node[dist_node_count].path = strdup(path);
	assert(dist_node[dist_node_count].path);
	return dist_node_count++;
}

int
dist_node_destroy(unsigned int id)
{
	/* FIXME */
	return 0;
}

void
dist_init(void)
{
	int ret;

	if (! slave) {
		/*
		 * Master
		 */
		struct passwd *pw;

		dist_node_count = 0;

		/*
		 * Get Default User
		 */
		pw = getpwuid(getuid());
		assert(pw);

		strcpy(def_user, pw->pw_name);

		/*
		 * Get Default Host
		 */
		ret = gethostname(def_host, sizeof(def_host));
		assert(0 <= ret);

		/*
		 * Get Default Path
		 */
		strcpy(def_path, "node.def");

	} else {
		/*
		 * Slave
		 */
		char buf[32];
		struct sockaddr_in addr;

		/* Get node number. */
		ret = read(0, buf, sizeof(buf));
		if (ret != sizeof(buf)) {
			assert(0); /* FIXME */
		}
		myself = atoi(buf);
		assert(1 <= myself
		    && myself < sizeof(dist_node) / sizeof(dist_node[0]));

		/* Connection to node 0 is established. */
		dist_node[0].io = 0;

		/* Create socket. */
		dist_node[myself].fd = socket(PF_INET, SOCK_STREAM, 0);
		assert(0 < dist_node[myself].fd);

		/* Set TCP_NODELAY option (see "man 7 tcp"). */
		/* FIXME */

		for (dist_node[myself].port = 1024;
		    ;
		    dist_node[myself].port++) {
			memset(&addr, 0, sizeof(addr));
			addr.sin_family = AF_INET;
			addr.sin_port = htons(dist_node[myself].port);
			addr.sin_addr.s_addr = INADDR_ANY;

			ret = bind(dist_node[myself].fd,
				(const struct sockaddr *) &addr, sizeof(addr));
			if (0 <= ret) {
				break;
			}
		}

		ret = listen(dist_node[myself].fd, 64);
		assert(0 <= ret);

		/* Send port number. */
		sprintf(buf, "%u", dist_node[myself].port);
		ret = write(1, buf, sizeof(buf));
		if (ret != sizeof(buf)) {
			assert(0); /* FIXME */
		}

		fprintf(stderr, "Running as node %u port %u.\n",
				myself, dist_node[myself].port);
	}
}

void
dist_exit(void)
{
	int ret;

	if (! slave) {
		/*
		 * Master
		 */
		/* FIXME */

	} else {
		/*
		 * Slave
		 */
		ret = close(dist_node[myself].fd);
		assert(0 <= ret);
	}
}