xref: /freebsd/tools/test/stress2/misc/mkfifo4.sh (revision 744bfb213144c63cbaf38d91a1c4f7aebb9b9fbc)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29. ../default.cfg
30
31# "Assertion vap->va_type == VDIR failed" seen on non HEAD.
32
33dir=/tmp
34odir=`pwd`
35cd $dir
36sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo4.c
37mycc -o mkfifo4 -Wall -Wextra -O0 -g mkfifo4.c || exit 1
38rm -f mkfifo4.c
39cd $odir
40
41fifo=/tmp/mkfifo4.fifo
42trap "rm -f $fifo /tmp/mkfifo4" EXIT INT
43
44export runRUNTIME=5m
45(cd ..; ./run.sh disk.cfg) > /dev/null 2>&1 &
46sleep .2
47
48while pgrep -fq run.sh; do
49	timeout 300 /tmp/mkfifo4 | grep -v Done
50	[ $? -eq 124 ] &&
51	    { echo "Timedout"; exit 1; }
52done
53wait
54
55exit $s
56EOF
57#include <sys/param.h>
58#include <sys/stat.h>
59#include <sys/wait.h>
60
61#include <err.h>
62#include <errno.h>
63#include <fcntl.h>
64#include <signal.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <unistd.h>
68
69#define ATIME 1
70#define MXRETRY 100
71#define LOOPS 1000000
72
73volatile int sigs;
74char file[] = "/tmp/mkfifo4.fifo";
75
76static void
77hand(int i __unused) {	/* handler */
78	sigs++;
79}
80
81int
82main(void)
83{
84	pid_t pid, hpid;
85	struct sigaction sa;
86	int e, fd, fd2, i, status;
87	int failures, r, retries, w;
88	char c;
89
90	sa.sa_handler = hand;
91	sigemptyset(&sa.sa_mask);
92	sa.sa_flags = 0;
93	if (sigaction(SIGALRM, &sa, NULL) == -1)
94		err(1, "sigaction");
95
96	sa.sa_handler = SIG_IGN;
97	sigemptyset(&sa.sa_mask);
98	sa.sa_flags = 0;
99	if (sigaction(SIGPIPE, &sa, NULL) == -1)
100		err(1, "sigaction");
101
102	unlink(file);
103	if (mkfifo(file, 0640) == -1)
104		err(1, "mkfifo(%s)", file);
105	if ((hpid = fork()) == 0) {
106		if ((fd2 = open(file, O_WRONLY | O_APPEND)) == -1)
107			err(1, "hold open of fifo");
108		for (;;)
109			pause();
110		_exit(0);
111	}
112
113	if ((pid = fork()) == 0) {
114		setproctitle("child");
115		r = 0;
116		for (i = 0; i < LOOPS; i++) {
117			failures = 0;
118restart:
119			do {
120				if ((fd = open(file, O_RDONLY |
121				    O_NONBLOCK)) == -1)
122					if (errno != EINTR) /* on OS X */
123						err(1, "open(%s, O_RDONLY)",
124						    file);
125			} while (fd == -1);
126			retries = 0;
127			do {
128				if ((e = read(fd, &c, 1)) == -1) {
129					if (errno != EINTR &&
130					    errno != EAGAIN)
131						err(1, "read(%d, ...)", fd);
132				} else if (retries++ > MXRETRY) {
133					close(fd);
134					usleep(1000);
135					fprintf(stderr,
136					    "Re-open for read @ %d\n", i);
137					if (failures++ > 100)
138						errx(1,
139						    "FAIL: Failure to read");
140					goto restart;
141				}
142			} while (e <= 0);
143			r++;
144			ualarm(ATIME, 0);
145			if (close(fd) == -1)
146				err(1, "close() in child");
147			alarm(0);
148		}
149		fprintf(stdout, "Done  child. %d  reads, %d signals.\n", r,
150		    sigs);
151		fflush(stdout);
152		_exit(0);
153	}
154	setproctitle("parent");
155	w = 0;
156	for (i = 0; i < LOOPS; i++) {
157		do {
158			if ((fd = open(file, O_WRONLY | O_APPEND |
159			    O_NONBLOCK)) == -1)
160				if (errno != EINTR && errno != ENXIO)
161					err(1, "open(%s, O_WRONLY)", file);
162		} while (fd == -1);
163		do {
164			if ((e = write(fd, "a", 1)) != 1)
165				if (errno != EPIPE && errno != EAGAIN)
166					err(1, "write(%d, ...)", fd);
167		} while (e == -1);
168		w++;
169		ualarm(ATIME, 0);
170		if (close(fd) == -1)
171			err(1, "close() in parent");
172		alarm(0);
173	}
174	fprintf(stdout, "Done parent. %d writes, %d signals.\n", w, sigs);
175
176	if (waitpid(pid, &status, 0) == -1)
177		err(1, "wait");
178	if (kill(hpid, SIGHUP) == -1)
179		err(1, "kill %d", hpid);
180	if (waitpid(hpid, NULL, 0) == -1)
181		err(1, "wait");
182	if (unlink(file) == -1)
183		err(1, "unlink(%s)", file);
184
185	return (WEXITSTATUS(status));
186}
187