aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/net/af_unix/test_unix_oob.c
blob: b57e91e1c3f28fa1630bcd39c6b4254f8fda96c5 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0-or-later
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include <sys/signal.h>
#include <sys/poll.h>

static int pipefd[2];
static int signal_recvd;
static pid_t producer_id;
static char sock_name[32];

static void sig_hand(int sn, siginfo_t *si, void *p)
{
	signal_recvd = sn;
}

static int set_sig_handler(int signal)
{
	struct sigaction sa;

	sa.sa_sigaction = sig_hand;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_SIGINFO | SA_RESTART;

	return sigaction(signal, &sa, NULL);
}

static void set_filemode(int fd, int set)
{
	int flags = fcntl(fd, F_GETFL, 0);

	if (set)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;
	fcntl(fd, F_SETFL, flags);
}

static void signal_producer(int fd)
{
	char cmd;

	cmd = 'S';
	write(fd, &cmd, sizeof(cmd));
}

static void wait_for_signal(int fd)
{
	char buf[5];

	read(fd, buf, 5);
}

static void die(int status)
{
	fflush(NULL);
	unlink(sock_name);
	kill(producer_id, SIGTERM);
	exit(status);
}

int is_sioctatmark(int fd)
{
	int ans = -1;

	if (ioctl(fd, SIOCATMARK, &ans, sizeof(ans)) < 0) {
#ifdef DEBUG
		perror("SIOCATMARK Failed");
#endif
	}
	return ans;
}

void read_oob(int fd, char *c)
{

	*c = ' ';
	if (recv(fd, c, sizeof(*c), MSG_OOB) < 0) {
#ifdef DEBUG
		perror("Reading MSG_OOB Failed");
#endif
	}
}

int read_data(int pfd, char *buf, int size)
{
	int len = 0;

	memset(buf, size, '0');
	len = read(pfd, buf, size);
#ifdef DEBUG
	if (len < 0)
		perror("read failed");
#endif
	return len;
}

static void wait_for_data(int pfd, int event)
{
	struct pollfd pfds[1];

	pfds[0].fd = pfd;
	pfds[0].events = event;
	poll(pfds, 1, -1);
}

void producer(struct sockaddr_un *consumer_addr)
{
	int cfd;
	char buf[64];
	int i;

	memset(buf, 'x', sizeof(buf));
	cfd = socket(AF_UNIX, SOCK_STREAM, 0);

	wait_for_signal(pipefd[0]);
	if (connect(cfd, (struct sockaddr *)consumer_addr,
		     sizeof(struct sockaddr)) != 0) {
		perror("Connect failed");
		kill(0, SIGTERM);
		exit(1);
	}

	for (i = 0; i < 2; i++) {
		/* Test 1: Test for SIGURG and OOB */
		wait_for_signal(pipefd[0]);
		memset(buf, 'x', sizeof(buf));
		buf[63] = '@';
		send(cfd, buf, sizeof(buf), MSG_OOB);

		wait_for_signal(pipefd[0]);

		/* Test 2: Test for OOB being overwitten */
		memset(buf, 'x', sizeof(buf));
		buf[63] = '%';
		send(cfd, buf, sizeof(buf), MSG_OOB);

		memset(buf, 'x', sizeof(buf));
		buf[63] = '#';
		send(cfd, buf, sizeof(buf), MSG_OOB);

		wait_for_signal(pipefd[0]);

		/* Test 3: Test for SIOCATMARK */
		memset(buf, 'x', sizeof(buf));
		buf[63] = '@';
		send(cfd, buf, sizeof(buf), MSG_OOB);

		memset(buf, 'x', sizeof(buf));
		buf[63] = '%';
		send(cfd, buf, sizeof(buf), MSG_OOB);

		memset(buf, 'x', sizeof(buf));
		send(cfd, buf, sizeof(buf), 0);

		wait_for_signal(pipefd[0]);

		/* Test 4: Test for 1byte OOB msg */
		memset(buf, 'x', sizeof(buf));
		buf[0] = '@';
		send(cfd, buf, 1, MSG_OOB);
	}
}

int
main(int argc, char **argv)
{
	int lfd, pfd;
	struct sockaddr_un consumer_addr, paddr;
	socklen_t len = sizeof(consumer_addr);
	char buf[1024];
	int on = 0;
	char oob;
	int flags;
	int atmark;
	char *tmp_file;

	lfd = socket(AF_UNIX, SOCK_STREAM, 0);
	memset(&consumer_addr, 0, sizeof(consumer_addr));
	consumer_addr.sun_family = AF_UNIX;
	sprintf(sock_name, "unix_oob_%d", getpid());
	unlink(sock_name);
	strcpy(consumer_addr.sun_path, sock_name);

	if ((bind(lfd, (struct sockaddr *)&consumer_addr,
		  sizeof(consumer_addr))) != 0) {
		perror("socket bind failed");
		exit(1);
	}

	pipe(pipefd);

	listen(lfd, 1);

	producer_id = fork();
	if (producer_id == 0) {
		producer(&consumer_addr);
		exit(0);
	}

	set_sig_handler(SIGURG);
	signal_producer(pipefd[1]);

	pfd = accept(lfd, (struct sockaddr *) &paddr, &len);
	fcntl(pfd, F_SETOWN, getpid());

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 1:
	 * veriyf that SIGURG is
	 * delivered, 63 bytes are
	 * read, oob is '@', and POLLPRI works.
	 */
	wait_for_data(pfd, POLLPRI);
	read_oob(pfd, &oob);
	len = read_data(pfd, buf, 1024);
	if (!signal_recvd || len != 63 || oob != '@') {
		fprintf(stderr, "Test 1 failed sigurg %d len %d %c\n",
			 signal_recvd, len, oob);
			die(1);
	}

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 2:
	 * Verify that the first OOB is over written by
	 * the 2nd one and the first OOB is returned as
	 * part of the read, and sigurg is received.
	 */
	wait_for_data(pfd, POLLIN | POLLPRI);
	len = 0;
	while (len < 70)
		len = recv(pfd, buf, 1024, MSG_PEEK);
	len = read_data(pfd, buf, 1024);
	read_oob(pfd, &oob);
	if (!signal_recvd || len != 127 || oob != '#') {
		fprintf(stderr, "Test 2 failed, sigurg %d len %d OOB %c\n",
		signal_recvd, len, oob);
		die(1);
	}

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 3:
	 * verify that 2nd oob over writes
	 * the first one and read breaks at
	 * oob boundary returning 127 bytes
	 * and sigurg is received and atmark
	 * is set.
	 * oob is '%' and second read returns
	 * 64 bytes.
	 */
	len = 0;
	wait_for_data(pfd, POLLIN | POLLPRI);
	while (len < 150)
		len = recv(pfd, buf, 1024, MSG_PEEK);
	len = read_data(pfd, buf, 1024);
	atmark = is_sioctatmark(pfd);
	read_oob(pfd, &oob);

	if (!signal_recvd || len != 127 || oob != '%' || atmark != 1) {
		fprintf(stderr,
			"Test 3 failed, sigurg %d len %d OOB %c atmark %d\n",
			signal_recvd, len, oob, atmark);
		die(1);
	}

	signal_recvd = 0;

	len = read_data(pfd, buf, 1024);
	if (len != 64) {
		fprintf(stderr, "Test 3.1 failed, sigurg %d len %d OOB %c\n",
			signal_recvd, len, oob);
		die(1);
	}

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 4:
	 * verify that a single byte
	 * oob message is delivered.
	 * set non blocking mode and
	 * check proper error is
	 * returned and sigurg is
	 * received and correct
	 * oob is read.
	 */

	set_filemode(pfd, 0);

	wait_for_data(pfd, POLLIN | POLLPRI);
	len = read_data(pfd, buf, 1024);
	if ((len == -1) && (errno == 11))
		len = 0;

	read_oob(pfd, &oob);

	if (!signal_recvd || len != 0 || oob != '@') {
		fprintf(stderr, "Test 4 failed, sigurg %d len %d OOB %c\n",
			 signal_recvd, len, oob);
		die(1);
	}

	set_filemode(pfd, 1);

	/* Inline Testing */

	on = 1;
	if (setsockopt(pfd, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on))) {
		perror("SO_OOBINLINE");
		die(1);
	}

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 1 -- Inline:
	 * Check that SIGURG is
	 * delivered and 63 bytes are
	 * read and oob is '@'
	 */

	wait_for_data(pfd, POLLIN | POLLPRI);
	len = read_data(pfd, buf, 1024);

	if (!signal_recvd || len != 63) {
		fprintf(stderr, "Test 1 Inline failed, sigurg %d len %d\n",
			signal_recvd, len);
		die(1);
	}

	len = read_data(pfd, buf, 1024);

	if (len != 1) {
		fprintf(stderr,
			 "Test 1.1 Inline failed, sigurg %d len %d oob %c\n",
			 signal_recvd, len, oob);
		die(1);
	}

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 2 -- Inline:
	 * Verify that the first OOB is over written by
	 * the 2nd one and read breaks correctly on
	 * 2nd OOB boundary with the first OOB returned as
	 * part of the read, and sigurg is delivered and
	 * siocatmark returns true.
	 * next read returns one byte, the oob byte
	 * and siocatmark returns false.
	 */
	len = 0;
	wait_for_data(pfd, POLLIN | POLLPRI);
	while (len < 70)
		len = recv(pfd, buf, 1024, MSG_PEEK);
	len = read_data(pfd, buf, 1024);
	atmark = is_sioctatmark(pfd);
	if (len != 127 || atmark != 1 || !signal_recvd) {
		fprintf(stderr, "Test 2 Inline failed, len %d atmark %d\n",
			 len, atmark);
		die(1);
	}

	len = read_data(pfd, buf, 1024);
	atmark = is_sioctatmark(pfd);
	if (len != 1 || buf[0] != '#' || atmark == 1) {
		fprintf(stderr, "Test 2.1 Inline failed, len %d data %c atmark %d\n",
			len, buf[0], atmark);
		die(1);
	}

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 3 -- Inline:
	 * verify that 2nd oob over writes
	 * the first one and read breaks at
	 * oob boundary returning 127 bytes
	 * and sigurg is received and siocatmark
	 * is true after the read.
	 * subsequent read returns 65 bytes
	 * because of oob which should be '%'.
	 */
	len = 0;
	wait_for_data(pfd, POLLIN | POLLPRI);
	while (len < 126)
		len = recv(pfd, buf, 1024, MSG_PEEK);
	len = read_data(pfd, buf, 1024);
	atmark = is_sioctatmark(pfd);
	if (!signal_recvd || len != 127 || !atmark) {
		fprintf(stderr,
			 "Test 3 Inline failed, sigurg %d len %d data %c\n",
			 signal_recvd, len, buf[0]);
		die(1);
	}

	len = read_data(pfd, buf, 1024);
	atmark = is_sioctatmark(pfd);
	if (len != 65 || buf[0] != '%' || atmark != 0) {
		fprintf(stderr,
			 "Test 3.1 Inline failed, len %d oob %c atmark %d\n",
			 len, buf[0], atmark);
		die(1);
	}

	signal_recvd = 0;
	signal_producer(pipefd[1]);

	/* Test 4 -- Inline:
	 * verify that a single
	 * byte oob message is delivered
	 * and read returns one byte, the oob
	 * byte and sigurg is received
	 */
	wait_for_data(pfd, POLLIN | POLLPRI);
	len = read_data(pfd, buf, 1024);
	if (!signal_recvd || len != 1 || buf[0] != '@') {
		fprintf(stderr,
			"Test 4 Inline failed, signal %d len %d data %c\n",
		signal_recvd, len, buf[0]);
		die(1);
	}
	die(0);
}