xref: /freebsd/tools/test/stress2/misc/mkfifo3.sh (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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# Regression test for
32# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162
33
34# FAIL
35# 1001 82102   907   0  20  0 6484 2480 wait   S+    0  0:00,01 /bin/sh ./mkfifo3.sh
36# 1001 82113 82102   0  52  0 3832 1604 fifoow I+    0  0:03,91 /tmp/mkfifo3
37
38# Fixed in r288044.
39
40dir=/tmp
41odir=`pwd`
42cd $dir
43sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo3.c
44mycc -o mkfifo3 -Wall -Wextra -O0 -g mkfifo3.c || exit 1
45rm -f mkfifo3.c
46cd $odir
47
48fifo=/tmp/mkfifo3.fifo
49trap "rm -f $fifo /tmp/mkfifo3" EXIT INT
50
51/tmp/mkfifo3 &
52
53for i in `jot 12`; do
54	pgrep -q mkfifo3 || break
55	sleep 10
56done
57s=0
58if pgrep -q mkfifo3; then
59	s=1
60	pgrep mkfifo3 | xargs ps -lp
61	pkill mkfifo3
62fi
63wait
64
65exit $s
66EOF
67#include <sys/param.h>
68#include <sys/stat.h>
69#include <sys/wait.h>
70
71#include <err.h>
72#include <errno.h>
73#include <fcntl.h>
74#include <signal.h>
75#include <stdio.h>
76#include <stdlib.h>
77#include <unistd.h>
78
79#define ATIME 1
80#define LOOPS 100000
81
82char file[] = "/tmp/mkfifo3.fifo";
83
84static void
85hand(int i __unused) {	/* handler */
86}
87
88int
89main(void)
90{
91	pid_t pid;
92	struct sigaction sa;
93	int fd, i, status;
94
95	sa.sa_handler = hand;
96	sigemptyset(&sa.sa_mask);
97	sa.sa_flags = 0;
98	if (sigaction(SIGALRM, &sa, NULL) == -1)
99		err(1, "sigaction");
100
101	if (unlink(file) == -1)
102		if (errno != ENOENT)
103			err(1, "unlink(%s)", file);
104	if (mkfifo(file, 0640) == -1)
105		err(1, "mkfifo(%s)", file);
106
107	for (i = 0; i < LOOPS; i++) {
108		if ((pid = fork()) == 0) {
109			ualarm(ATIME, 0);
110			do {
111				if((fd = open(file, O_RDONLY)) == -1)
112					if (errno != EINTR)
113						err(1, "open(%s, O_RDONLY @ %d)",
114						    file, i);
115			} while (fd == -1);
116			if (close(fd) == -1)
117				err(1, "close() in child");
118			alarm(0);
119			_exit(0);
120		}
121		ualarm(ATIME, 0);
122		do {
123			if ((fd = open(file, O_WRONLY)) == -1)
124				if (errno != EINTR)
125					err(1, "open(%s, O_WRONLY @ %d)",
126					    file, i);
127		} while (fd == -1);
128		if (close(fd) == -1)
129			err(1, "close() in parent");
130		alarm(0);
131		if (waitpid(pid, &status, 0) == -1)
132			err(1, "wait");
133	}
134
135	if (unlink(file) == -1)
136		err(1, "unlink(%s)", file);
137
138	return (0);
139}
140