xref: /freebsd/tools/test/stress2/misc/fifo2.sh (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 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# Page fault seen
30# http://people.freebsd.org/~pho/stress/log/kostik654.txt
31# Fixed by r259521.
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37odir=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $odir/$0 > fifo2.c
40rm -f /tmp/fifo2
41mycc -o fifo2 -Wall -Wextra -O2 -g fifo2.c -lpthread || exit 1
42rm -f fifo2.c
43
44mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
45mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
46
47mdconfig -a -t swap -s 1g -u $mdstart || exit 1
48newfs $newfs_flags md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50chmod 777 $mntpoint
51mkfifo $mntpoint/f
52chmod 777 $mntpoint/f
53
54sleeptime=12
55st=`date '+%s'`
56while [ $((`date '+%s'` - st)) -lt $((10 * sleeptime)) ]; do
57	(cd $mntpoint; /tmp/fifo2) &
58	start=`date '+%s'`
59	while [ $((`date '+%s'` - start)) -lt $sleeptime ]; do
60		pgrep -q fifo2 || break
61		sleep .5
62	done
63	while pkill -9 fifo2; do :; done
64	wait
65done
66
67for i in `jot 10`; do
68	mount | grep -q md$mdstart  && \
69		umount $mntpoint > /dev/null 2>&1 &&
70		    mdconfig -d -u $mdstart && break
71	sleep 10
72done
73s=0
74mount | grep -q md$mdstart &&
75    { echo "umount $mntpoint failed"; s=1; }
76rm -f /tmp/fifo2
77exit $s
78EOF
79#include <sys/types.h>
80#include <sys/resource.h>
81#include <sys/stat.h>
82#include <sys/syscall.h>
83#include <sys/wait.h>
84
85#include <err.h>
86#include <errno.h>
87#include <fcntl.h>
88#include <libutil.h>
89#include <pthread.h>
90#include <pwd.h>
91#include <signal.h>
92#include <stdint.h>
93#include <stdio.h>
94#include <stdlib.h>
95#include <string.h>
96#include <unistd.h>
97
98#define N (128 * 1024 / (int)sizeof(u_int32_t))
99static int debug; /* Set to 1 for debug output */
100u_int32_t r[N];
101
102static void
103hand(int i __unused) {	/* handler */
104	_exit(1);
105}
106
107static unsigned long
108makearg(void)
109{
110	unsigned int i;
111	unsigned long val;
112
113	val = arc4random();
114	i   = arc4random() % 100;
115	if (i < 20)
116		val = val & 0xff;
117	if (i >= 20 && i < 40)
118		val = val & 0xffff;
119	if (i >= 40 && i < 60)
120		val = (unsigned long)(r) | (val & 0xffff);
121#if defined(__LP64__)
122	if (i >= 60) {
123		val = (val << 32) | arc4random();
124		if (i > 80)
125			val = val & 0x00007fffffffffffUL;
126	}
127#endif
128
129	return(val);
130}
131
132static void *
133calls(void *arg __unused)
134{
135	unsigned long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
136	int i;
137
138	for (i = 0;; i++) {
139		arg1 = (unsigned long)(void *)"f";
140		arg2 = makearg();
141		arg3 = makearg();
142		arg4 = makearg();
143		arg5 = makearg();
144		arg6 = makearg();
145		arg7 = makearg();
146
147		if (debug != 0)
148			fprintf(stderr, "%2d : syscall(%3d, %lx, %lx, %lx, %lx, %lx, %lx, %lx)\n",
149			    i, SYS_open, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
150		usleep(100000);
151		alarm(1);
152		syscall(SYS_open, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
153	}
154
155	return (0);
156}
157
158int
159main(void)
160{
161	struct passwd *pw;
162	struct rlimit limit;
163	pthread_t cp[50];
164	time_t start;
165	int e, j;
166
167	if ((pw = getpwnam("nobody")) == NULL)
168		err(1, "no such user: nobody");
169
170	if (setgroups(1, &pw->pw_gid) ||
171	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
172	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
173		err(1, "Can't drop privileges to \"nobody\"");
174	endpwent();
175
176	limit.rlim_cur = limit.rlim_max = 1000;
177	if (setrlimit(RLIMIT_NPTS, &limit) < 0)
178		err(1, "setrlimit");
179
180	signal(SIGALRM, hand);
181	signal(SIGILL,  hand);
182	signal(SIGFPE,  hand);
183	signal(SIGSEGV, hand);
184	signal(SIGBUS,  hand);
185	signal(SIGURG,  hand);
186	signal(SIGSYS,  hand);
187	signal(SIGTRAP, hand);
188
189	start = time(NULL);
190	while ((time(NULL) - start) < 120) {
191		if (fork() == 0) {
192			for (j = 0; j < 1; j++)
193				if ((e = pthread_create(&cp[j], NULL, calls, NULL)) != 0)
194					errc(1, e,"pthread_create");
195
196			for (j = 0; j < 1; j++)
197				pthread_join(cp[j], NULL);
198			_exit(0);
199		}
200		wait(NULL);
201	}
202
203	return (0);
204}
205