File: main.c

package info (click to toggle)
spiped 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,328 kB
  • sloc: ansic: 11,951; sh: 1,081; makefile: 629; perl: 121
file content (488 lines) | stat: -rw-r--r-- 9,248 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <sys/socket.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "getopt.h"
#include "millisleep.h"
#include "noeintr.h"
#include "parsenum.h"
#include "warnp.h"

#include "pushbits.h"

static int
basic_copy(const char * filename_in, const char * filename_out)
{
	pthread_t thread;
	int in, out;
	int rc;

	/* Open input and output. */
	if ((in = open(filename_in, O_RDONLY)) == -1) {
		warnp("open(%s)", filename_in);
		goto err0;
	}
	if ((out = open(filename_out, O_WRONLY | O_CREAT | O_TRUNC,
	    S_IRUSR | S_IWUSR)) == -1) {
		warnp("open(%s)", filename_out);
		goto err1;
	}

	/* Start copying. */
	if (pushbits(in, out, &thread)) {
		warnp("pushbits");
		goto err2;
	}

	/* Wait for thread to finish. */
	if ((rc = pthread_join(thread, NULL)) != 0) {
		warn0("pthread_join: %s", strerror(rc));
		goto err2;
	}

	/* Clean up. */
	if (close(out))
		warnp("close");
	if (close(in))
		warnp("close");

	/* Success! */
	return (0);

err2:
	if (close(out))
		warnp("close");
err1:
	if (close(in))
		warnp("close");
err0:
	/* Failure! */
	return (-1);
}

static int
echo_stdin_stdout(void)
{
	pthread_t thread;
	int rc;

	/* Start echoing. */
	if (pushbits(STDIN_FILENO, STDOUT_FILENO, &thread)) {
		warnp("pushbits");
		goto err0;
	}

	/*
	 * Wait a short while, then cancel the thread.  Note that if stdin
	 * returned an EOF, the thread might have already stopped, and
	 * certainly will not be running after a second.  This is particularly
	 * relevant if running the tests inside a virtualization or container
	 * framework.
	 */
	sleep(1);
	if ((rc = pthread_cancel(thread)) != 0) {
		/*
		 * These tests ignore an ESRCH error returned by
		 * pthread_cancel.  According to the POSIX standard, a
		 * thread ID should still be valid after pthread_exit has
		 * been invoked by the thread if pthread_join() has not yet
		 * been called.  However, many platforms return ESRCH in
		 * this situation.
		 */
		if (rc != ESRCH) {
			warn0("pthread_cancel: %s", strerror(rc));
			goto err0;
		}
	}

	/* Wait for thread to finish. */
	if ((rc = pthread_join(thread, NULL)) != 0) {
		warn0("pthread_join: %s", strerror(rc));
		goto err0;
	}

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}

static int
chain_one(void)
{
	pthread_t thread;
	int in[2];
	int out[2];
	const char * msg = "ping";
	size_t msglen = strlen(msg) + 1;
	char * buf;
	ssize_t r;
	int rc;

	/* Initialize output message buffer. */
	if ((buf = malloc(msglen)) == NULL) {
		warnp("malloc");
		goto err0;
	}

	/* Create socket pairs for the input and output. */
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, in)) {
		warnp("socketpair");
		goto err1;
	}
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, out)) {
		warnp("socketpair");
		goto err1;
	}

	/* Link the two pairs with pushbits. */
	if (pushbits(in[1], out[0], &thread)) {
		warnp("pushbits");
		goto err1;
	}

	/* Wait for thread to start. */
	millisleep(100);

	/*
	 * Send the message; this will not block because the message is
	 * small enough that it's entirely buffered within the kernel.
	 */
	if (noeintr_write(in[0], msg, msglen) != (ssize_t)msglen) {
		warnp("noeintr_write");
		goto err3;
	}

	/* Message was sent successfully; close the input. */
	if (close(in[0])) {
		warnp("close");
		goto err2;
	}

	/* Read message; assume that we'll get it all at once. */
	if ((r = read(out[1], buf, msglen)) == -1) {
		warnp("read");
		goto err2;
	}
	if ((size_t)r != msglen) {
		warn0("Message is not the expected length");
		goto err2;
	}
	if (memcmp(buf, msg, msglen) != 0) {
		warn0("failed to get the (full?) message");
		goto err2;
	}

	/* Wait for thread to finish, then clean up. */
	if ((rc = pthread_join(thread, NULL)) != 0) {
		warn0("pthread_join: %s", strerror(rc));
		goto err1;
	}
	free(buf);

	/* Clean up. */
	if (close(in[1])) {
		warnp("close");
		goto err0;
	}
	if (close(out[0])) {
		warnp("close");
		goto err0;
	}
	if (close(out[1])) {
		warnp("close");
		goto err0;
	}

	/* Success! */
	return (0);

err3:
	if ((rc = pthread_cancel(thread)) != 0)
		warn0("pthread_cancel: %s", strerror(rc));
err2:
	if ((rc = pthread_join(thread, NULL)) != 0)
		warn0("pthread_join: %s", strerror(rc));
err1:
	free(buf);
err0:
	/* Failure! */
	return (-1);
}

static int
chain_two(void)
{
	pthread_t thread[2];
	int in[2];
	int middle[2];
	int out[2];
	const char * msg = "ping";
	size_t msglen = strlen(msg) + 1;
	char * buf;
	ssize_t r;
	int rc;

	/* Initialize output message buffer. */
	if ((buf = malloc(msglen)) == NULL) {
		warnp("malloc");
		goto err0;
	}

	/* Create socket pairs for the input, middle, and output. */
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, in)) {
		warnp("socketpair");
		goto err1;
	}
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, middle)) {
		warnp("socketpair");
		goto err1;
	}
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, out)) {
		warnp("socketpair");
		goto err1;
	}

	/* Link the pairs with pushbits. */
	if (pushbits(middle[1], out[0], &thread[1])) {
		warnp("pushbits");
		goto err1;
	}
	if (pushbits(in[1], middle[0], &thread[0])) {
		warnp("pushbits");
		goto err2;
	}

	/* Wait for thread to start. */
	millisleep(100);

	/*
	 * Send the message; this will not block because the message is
	 * small enough that it's entirely buffered within the kernel.
	 */
	if (noeintr_write(in[0], msg, msglen) != (ssize_t)msglen) {
		warnp("noeintr_write");
		goto err3;
	}

	/* Message was sent successfully; close the input. */
	if (close(in[0])) {
		warnp("close");
		goto err3;
	}

	/* Shut down the first thread. */
	if ((rc = pthread_join(thread[0], NULL)) != 0) {
		warn0("pthread_join: %s", strerror(rc));
		goto err2;
	}

	/* Read message; assume that we'll get it all at once. */
	if ((r = read(out[1], buf, msglen)) == -1) {
		warnp("read");
		goto err2;
	}
	if ((size_t)r != msglen) {
		warn0("Message is not the expected length");
		goto err2;
	}
	if (memcmp(buf, msg, msglen) != 0) {
		warn0("failed to get the (full?) message");
		goto err2;
	}

	/* Wait for the second thread to finish, then clean up. */
	if ((rc = pthread_join(thread[1], NULL)) != 0) {
		warn0("pthread_join: %s", strerror(rc));
		goto err1;
	}
	free(buf);

	/* Clean up. */
	if (close(in[1])) {
		warnp("close");
		goto err0;
	}
	if (close(middle[0])) {
		warnp("close");
		goto err0;
	}
	if (close(middle[1])) {
		warnp("close");
		goto err0;
	}
	if (close(out[0])) {
		warnp("close");
		goto err0;
	}
	if (close(out[1])) {
		warnp("close");
		goto err0;
	}


	/* Success! */
	return (0);

err3:
	if ((rc = pthread_cancel(thread[0])) != 0)
		warn0("pthread_cancel: %s", strerror(rc));
	if ((rc = pthread_join(thread[0], NULL)) != 0)
		warn0("pthread_join: %s", strerror(rc));
err2:
	if ((rc = pthread_cancel(thread[1])) != 0)
		warn0("pthread_cancel: %s", strerror(rc));
	if ((rc = pthread_join(thread[1], NULL)) != 0)
		warn0("pthread_join: %s", strerror(rc));
err1:
	free(buf);
err0:
	/* Failure! */
	return (-1);
}

static int
start_stop(void)
{
	pthread_t thread;
	int in[2];
	int rc;

	/* Create socket pair for the input. */
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, in)) {
		warnp("socketpair");
		goto err0;
	}

	/* Start echoing. */
	if (pushbits(in[1], STDOUT_FILENO, &thread)) {
		warnp("pushbits");
		goto err1;
	}

	/* Cancel the thread immediately. */
	if ((rc = pthread_cancel(thread)) != 0) {
		warn0("pthread_cancel: %s", strerror(rc));
		goto err1;
	}

	/* Wait for thread to finish. */
	if ((rc = pthread_join(thread, NULL)) != 0) {
		warn0("pthread_join: %s", strerror(rc));
		goto err1;
	}

	/* Clean up. */
	if (close(in[0])) {
		warnp("close");
		goto err0;
	}
	if (close(in[1])) {
		warnp("close");
		goto err0;
	}

	/* Success! */
	return (0);

err1:
	if (close(in[0]))
		warnp("close");
	if (close(in[1]))
		warnp("close");
err0:
	/* Failure! */
	return (-1);
}

static void
usage(void)
{

	fprintf(stderr, "usage: test_pushbits [-i filename -o filename] NUM\n");
	exit(1);
}

int
main(int argc, char ** argv)
{
	const char * ch;
	const char * filename_in = NULL;
	const char * filename_out = NULL;
	int desired_test;

	WARNP_INIT;

	/* Parse the command line. */
	while ((ch = GETOPT(argc, argv)) != NULL) {
		GETOPT_SWITCH(ch) {
		GETOPT_OPTARG("-i"):
			filename_in = optarg;
			break;
		GETOPT_OPTARG("-o"):
			filename_out = optarg;
			break;
		GETOPT_MISSING_ARG:
			warn0("Missing argument to %s", ch);
			usage();
		GETOPT_DEFAULT:
			warn0("illegal option -- %s", ch);
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/* We should have processed all the arguments except for one. */
	if (argc != 1)
		usage();
	if (PARSENUM(&desired_test, argv[0], 1, 5)) {
		warnp("parsenum");
		goto err0;
	}

	/* Run the desired test. */
	switch (desired_test) {
	case 1:
		if ((filename_in == NULL) || (filename_out == NULL))
			usage();
		if (basic_copy(filename_in, filename_out))
			goto err0;
		break;
	case 2:
		if (echo_stdin_stdout())
			goto err0;
		break;
	case 3:
		if (chain_one())
			goto err0;
		break;
	case 4:
		if (chain_two())
			goto err0;
		break;
	case 5:
		if (start_stop())
			goto err0;
		break;
	default:
		warn0("invalid test number");
		goto err0;
	}

	/* Success! */
	exit(0);

err0:
	/* Failure! */
	exit(1);
}