Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*-
 * Copyright (c) 2005 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/*
 * Regression test to exercise various POSIX-defined parts of fifo behavior
 * described for open(2):
 *
 * O_NONBLOCK
 * When opening a FIFO with O_RDONLY or O_WRONLY set:
 *
 * - If O_NONBLOCK is set, an open() for reading-only shall return without
 *   delay. An open() for writing-only shall return an error if no process
 *   currently has the file open for reading.
 *
 * - If O_NONBLOCK is clear, an open() for reading-only shall block the
 *   calling thread until a thread opens the file for writing. An open()
 *   for writing-only shall block the calling thread until a thread opens
 *   the file for reading.
 *
 * When opening a block special or character special file that supports
 * non-blocking opens:
 *
 * - If O_NONBLOCK is set, the open() function shall return without blocking
 *   for the device to be ready or available. Subsequent behavior of the
 *   device is device-specific.
 *
 * - If O_NONBLOCK is clear, the open() function shall block the calling
 *   thread until the device is ready or available before returning.
 *
 * Special errors:
 *
 * [ENXIO]
 * O_NONBLOCK is set, the named file is a FIFO, O_WRONLY is set, and no
 * process has the file open for reading.
 */

/*
 * In order to test blocking/non-blocking behavior, test processes must
 * potentially block themselves until released.  As bugs in blocking result
 * in processes that won't un-block, we must sacrifice a process to the task,
 * watching and potentially killing it after a time-out.  The main test
 * process is never used to open or act directly on a fifo (other than to
 * create or unlink it) in order to avoid the main test process being
 * blocked.
 */

/*
 * All activity occurs within a temporary directory created early in the
 * test.
 */
static char	temp_dir[PATH_MAX];

static void __unused
atexit_temp_dir(void)
{

	rmdir(temp_dir);
}

/*
 * Run a function in a particular test process.
 */
static int
run_in_process(int (*func)(void), pid_t *pidp, const char *errstr)
{
	pid_t pid;

	pid = fork();
	if (pid < 0) {
		warn("%s: run_in_process: fork", errstr);
		return (-1);
	}

	if (pid == 0)
		exit(func());

	if (pidp != NULL)
		*pidp = pid;

	return (0);
}

/*
 * Wait for a process on a timeout, and if the timeout expires, kill the
 * process.  Test each second rather than waiting the full timeout at once to
 * minimize the amount of time spent hanging around unnecessarily.
 */
static int
wait_and_timeout(pid_t pid, int timeout, int *status, const char *errstr)
{
	pid_t wpid;
	int i;

	/*
	 * Count up to the timeout, but do a non-hanging waitpid() after each
	 * second so we can avoid waiting a lot of extra time.
	 */
	for (i = 0; i < timeout; i++) {
		wpid = waitpid(pid, status, WNOHANG);
		if (wpid < 0) {
			warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
			return (-1);
		}

		if (wpid == pid)
			return (0);

		sleep(1);
	}

	wpid = waitpid(pid, status, WNOHANG);
	if (wpid < 0) {
		warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
		return (-1);
	}

	if (wpid == pid)
		return (0);

	if (kill(pid, SIGTERM) < 0) {
		warn("%s: wait_and_timeout: kill %d", errstr, pid);
		return (-1);
	}

	wpid = waitpid(pid, status, 0);
	if (wpid < 0) {
		warn("%s: wait_and_timeout: waitpid %d", errstr, pid);
		return (-1);
	}

	if (wpid != pid) {
		warn("%s: waitpid: returned %d not %d", errstr, wpid, pid);
		return (-1);
	}

	warnx("%s: process blocked", errstr);
	return (-1);
}

static int
non_blocking_open_reader(void)
{
	int fd;

	fd = open("testfifo", O_RDONLY | O_NONBLOCK);
	if (fd < 0)
		return (errno);
	close(fd);

	return (0);
}

static int
non_blocking_open_writer(void)
{
	int fd;

	fd = open("testfifo", O_WRONLY | O_NONBLOCK);
	if (fd < 0)
		return (errno);
	close(fd);

	return (0);
}

static int
blocking_open_reader(void)
{
	int fd;

	fd = open("testfifo", O_RDONLY);
	if (fd < 0)
		return (errno);
	close(fd);

	return (0);
}

static int
blocking_open_writer(void)
{
	int fd;

	fd = open("testfifo", O_WRONLY);
	if (fd < 0)
		return (errno);
	close(fd);

	return (0);
}

static void
test_blocking_reader(void)
{
	pid_t reader_pid, writer_pid, wpid;
	int error, status;

	if (mkfifo("testfifo", 0600) < 0)
		err(-1, "test_blocking_reader: mkfifo: testfifo");

	/*
	 * Block a process in opening the fifo.
	 */
	if (run_in_process(blocking_open_reader, &reader_pid,
	    "test_blocking_reader: blocking_open_reader") < 0) {
		(void)unlink("testfifo");
		exit(-1);
	}

	/*
	 * Test that it blocked.
	 */
	sleep(5);
	wpid = waitpid(reader_pid, &status, WNOHANG);
	if (wpid < 0) {
		error = errno;
		(void)unlink("testfifo");
		errno = error;
		err(-1, "test_blocking_reader: waitpid %d", reader_pid);
	}

	if (wpid != 0 && wpid != reader_pid) {
		(void)unlink("testfifo");
		errx(-1, "test_blocking_reader: waitpid %d returned %d",
		    reader_pid, wpid);
	}

	if (wpid == reader_pid) {
		(void)unlink("testfifo");
		errx(-1, "test_blocking_reader: blocking child didn't "
		    "block");
	}

	/*
	 * Unblock the blocking reader.
	 */
	if (run_in_process(blocking_open_writer, &writer_pid,
	    "test_blocking_reader: blocking_open_writer") < 0) {
		(void)unlink("testfifo");
		(void)kill(reader_pid, SIGTERM);
		(void)waitpid(reader_pid, &status, 0);
		exit(-1);
	}

	/*
	 * Make sure both processes exited quickly (<1 second) to make sure
	 * they didn't block, and GC.
	 */
	if (wait_and_timeout(reader_pid, 1, &status,
	    "test_blocking_reader: blocking_open_reader") < 0) {
		(void)unlink("testinfo");
		(void)kill(reader_pid, SIGTERM);
		(void)kill(writer_pid, SIGTERM);
		exit(-1);
	}
	
	if (wait_and_timeout(writer_pid, 1, &status,
	    "test_blocking_reader: blocking_open_writer") < 0) {
		(void)unlink("testinfo");
		(void)kill(writer_pid, SIGTERM);
		exit(-1);
	}
	
	if (unlink("testfifo") < 0)
		err(-1, "test_blocking_reader: unlink: testfifo");
}
static void
test_blocking_writer(void)
{
	pid_t reader_pid, writer_pid, wpid;
	int error, status;

	if (mkfifo("testfifo", 0600) < 0)
		err(-1, "test_blocking_writer: mkfifo: testfifo");

	/*
	 * Block a process in opening the fifo.
	 */
	if (run_in_process(blocking_open_writer, &writer_pid,
	    "test_blocking_writer: blocking_open_writer") < 0) {
		(void)unlink("testfifo");
		exit(-1);
	}

	/*
	 * Test that it blocked.
	 */
	sleep(5);
	wpid = waitpid(writer_pid, &status, WNOHANG);
	if (wpid < 0) {
		error = errno;
		(void)unlink("testfifo");
		errno = error;
		err(-1, "test_blocking_writer: waitpid %d", writer_pid);
	}

	if (wpid != 0 && wpid != writer_pid) {
		(void)unlink("testfifo");
		errx(-1, "test_blocking_writer: waitpid %d returned %d",
		    writer_pid, wpid);
	}

	if (wpid == writer_pid) {
		(void)unlink("testfifo");
		errx(-1, "test_blocking_writer: blocking child didn't "
		    "block");
	}

	/*
	 * Unblock the blocking writer.
	 */
	if (run_in_process(blocking_open_reader, &reader_pid,
	    "test_blocking_writer: blocking_open_reader") < 0) {
		(void)unlink("testfifo");
		(void)kill(writer_pid, SIGTERM);
		(void)waitpid(writer_pid, &status, 0);
		exit(-1);
	}

	/*
	 * Make sure both processes exited quickly (<1 second) to make sure
	 * they didn't block, and GC.
	 */
	if (wait_and_timeout(writer_pid, 1, &status,
	    "test_blocking_writer: blocking_open_writer") < 0) {
		(void)unlink("testinfo");
		(void)kill(writer_pid, SIGTERM);
		(void)kill(reader_pid, SIGTERM);
		(void)waitpid(writer_pid, &status, 0);
		(void)waitpid(reader_pid, &status, 0);
		exit(-1);
	}
	
	if (wait_and_timeout(reader_pid, 1, &status,
	    "test_blocking_writer: blocking_open_reader") < 0) {
		(void)unlink("testinfo");
		(void)kill(reader_pid, SIGTERM);
		(void)waitpid(reader_pid, &status, 0);
		exit(-1);
	}
	
	if (unlink("testfifo") < 0)
		err(-1, "test_blocking_writer: unlink: testfifo");
}

static void
test_non_blocking_reader(void)
{
	int status;
	pid_t pid;

	if (mkfifo("testfifo", 0600) < 0)
		err(-1, "test_non_blocking_reader: mkfifo: testfifo");

	if (run_in_process(non_blocking_open_reader, &pid,
	    "test_non_blocking_reader: non_blocking_open_reader") < 0) {
		(void)unlink("testfifo");
		exit(-1);
	}

	status = -1;
	if (wait_and_timeout(pid, 5, &status,
	    "test_non_blocking_reader: non_blocking_open_reader") < 0) {
		(void)unlink("testfifo");
		exit(-1);
	}

	if (WEXITSTATUS(status) != 0) {
		(void)unlink("testfifo");
		errno = WEXITSTATUS(status);
		err(-1, "test_non_blocking_reader: "
		    "non_blocking_open_reader: open: testfifo");
	}

	if (unlink("testfifo") < 0)
		err(-1, "test_non_blocking_reader: unlink: testfifo");
}

static void
test_non_blocking_writer(void)
{
	int status;
	pid_t pid;

	if (mkfifo("testfifo", 0600) < 0)
		err(-1, "test_non_blocking_writer: mkfifo: testfifo");

	if (run_in_process(non_blocking_open_writer, &pid,
	    "test_non_blocking_writer: non_blocking_open_writer") < 0) {
		(void)unlink("testfifo");
		exit(-1);
	}

	status = -1;
	if (wait_and_timeout(pid, 5, &status,
	    "test_non_blocking_writer: non_blocking_open_writer") < 0) {
		(void)unlink("testfifo");
		exit(-1);
	}

	if (WEXITSTATUS(status) != ENXIO) {
		(void)unlink("testfifo");

		errno = WEXITSTATUS(status);
		if (errno == 0)
			errx(-1, "test_non_blocking_writer: "
			    "non_blocking_open_writer: open succeeded");
		err(-1, "test_non_blocking_writer: "
		    "non_blocking_open_writer: open: testfifo");
	}

	if (unlink("testfifo") < 0)
		err(-1, "test_non_blocking_writer: unlink: testfifo");
}

int
main(void)
{

	if (geteuid() != 0)
		errx(-1, "must be run as root");

	strcpy(temp_dir, "fifo_open.XXXXXXXXXXX");
	if (mkdtemp(temp_dir) == NULL)
		err(-1, "mkdtemp");
	if (chdir(temp_dir) < 0)
		err(-1, "chdir: %s", temp_dir);
	atexit(atexit_temp_dir);

	test_non_blocking_reader();
	test_non_blocking_writer();

	test_blocking_reader();
	test_blocking_writer();

	return (0);
}