xref: /freebsd/tools/test/stress2/misc/fstat.sh (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Regression test for r368486
31# https://reviews.freebsd.org/D27513
32
33# Problem not seen.
34
35. ../default.cfg
36[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
37
38dir=/tmp
39odir=`pwd`
40cd $dir
41sed '1,/^EOF/d' < $odir/$0 > $dir/fstat.c
42mycc -o fstat -Wall -Wextra -O0 -g fstat.c || exit 1
43rm -f fstat.c
44cd $odir
45
46set -e
47mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
48[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
49mdconfig -a -t swap -s 2g -u $mdstart
50newfs $newfs_flags md$mdstart > /dev/null
51mount /dev/md$mdstart $mntpoint
52set +e
53
54(cd ../testcases/swap; ./swap -t 10m -i 20 > /dev/null 2>&1) &
55cd $mntpoint
56$dir/fstat
57s=$?
58while pkill swap; do :; done
59wait
60[ -f fstat.core -a $s -eq 0 ] &&
61    { ls -l fstat.core; mv fstat.core $dir; s=1; }
62cd $odir
63
64for i in `jot 6`; do
65	mount | grep -q "on $mntpoint " || break
66	umount $mntpoint && break || sleep 10
67	[ $i -eq 6 ] &&
68	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
69done
70mdconfig -d -u $mdstart
71rm -rf $dir/fstat
72exit $s
73
74EOF
75#include <sys/param.h>
76#include <sys/mman.h>
77#include <sys/socket.h>
78#include <sys/stat.h>
79#include <sys/wait.h>
80
81#include <err.h>
82#include <errno.h>
83#include <fcntl.h>
84#include <stdatomic.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <time.h>
88#include <unistd.h>
89
90static _Atomic(int) *share;
91
92#define PARALLEL 5
93#define RUNTIME (2 * 60)
94#define SYNC 0
95#define DONE 1
96
97static void
98test(void)
99{
100	pid_t pid;
101	time_t start;
102	int fd[2], i;
103	char cmd[60];
104
105	(void)atomic_fetch_add(&share[SYNC], 1);
106	while (atomic_load(&share[SYNC]) != PARALLEL)
107		usleep(10);
108
109	start = time(NULL);
110	while (time(NULL) - start < RUNTIME) {
111		share[DONE] = 0;
112		if ((pid = fork()) == 0) {
113			(void)atomic_fetch_add(&share[DONE], 1);
114			for (i = 0; i < 1000; i++) {
115				if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd) == -1)
116					err(1, "socketpair()");
117				usleep(arc4random() % 1000);
118				close(fd[0]);
119				close(fd[1]);
120			}
121			(void)atomic_fetch_add(&share[DONE], 1);
122			_exit(0);
123		}
124		if (pid == -1)
125			err(1, "fork()");
126		setproctitle("master");
127		while (atomic_load(&share[DONE]) == 0)
128			usleep(10);
129		snprintf(cmd, sizeof(cmd), "fstat -p %d > /dev/null 2>&1",
130		    pid);
131		while (atomic_load(&share[DONE]) == 1)
132			system(cmd);
133		system(cmd);
134		if (waitpid(pid, NULL, 0) != pid)
135			err(1, "waitpid()");
136	}
137
138	_exit(0);
139}
140
141int
142main(void)
143{
144	pid_t pids[PARALLEL];
145	size_t len;
146	time_t start;
147	int e, i, status;
148
149	e = 0;
150	len = PAGE_SIZE;
151	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
152	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
153		err(1, "mmap");
154
155	start = time(NULL);
156	while ((time(NULL) - start) < RUNTIME && e == 0) {
157		share[SYNC] = 0;
158		for (i = 0; i < PARALLEL; i++) {
159			if ((pids[i] = fork()) == 0)
160				test();
161			if (pids[i] == -1)
162				err(1, "fork()");
163		}
164		for (i = 0; i < PARALLEL; i++) {
165			if (waitpid(pids[i], &status, 0) == -1)
166				err(1, "waitpid(%d)", pids[i]);
167			if (status != 0) {
168				if (WIFSIGNALED(status))
169					fprintf(stderr,
170					    "pid %d exit signal %d\n",
171					    pids[i], WTERMSIG(status));
172			}
173			e += status == 0 ? 0 : 1;
174		}
175	}
176
177	return (e);
178}
179