xref: /freebsd/tools/test/stress2/misc/mknod.sh (revision 61e21613)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
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# mknod(2) regression test
30# "panic: ffs_write: type 0xca2b02d0 8 (0,3)" seen.
31# Reported by: Dmitry Vyukov <dvyukov@google.com>
32# Fixed by r324853
33
34. ../default.cfg
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40sed '1,/^EOF/d' < $odir/$0 > $dir/mknod.c
41mycc -o mknod -Wall -Wextra -O0 -g mknod.c || exit 1
42rm -f mknod.c
43cd $odir
44
45set -e
46mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
47[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
48mdconfig -a -t swap -s 2g -u $mdstart
49newfs $newfs_flags md$mdstart > /dev/null
50mount /dev/md$mdstart $mntpoint
51set +e
52
53cd $mntpoint
54$dir/mknod $mntpoint
55s=$?
56[ -f mknod.core -a $s -eq 0 ] &&
57    { ls -l mknod.core; mv mknod.core /tmp; s=1; }
58cd $odir
59
60for i in `jot 6`; do
61	mount | grep -q "on $mntpoint " || break
62	umount $mntpoint && break || sleep 10
63done
64[ $i -eq 6 ] && exit 1
65mdconfig -d -u $mdstart
66rm -rf $dir/mknod
67exit $s
68
69EOF
70#include <sys/param.h>
71#include <sys/mman.h>
72#include <sys/stat.h>
73#include <sys/wait.h>
74
75#include <machine/atomic.h>
76
77#include <err.h>
78#include <errno.h>
79#include <fcntl.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <time.h>
83#include <unistd.h>
84
85static volatile u_int *share;
86static char *mp;
87
88#define PARALLEL 4
89#define RUNTIME (1 * 60)
90#define SYNC 0
91
92static void
93test(void)
94{
95	dev_t dev;
96	mode_t mode;
97	time_t start;
98	int fd, n, r;
99	char path[128];
100
101	atomic_add_int(&share[SYNC], 1);
102	while (share[SYNC] != PARALLEL)
103		;
104	n = 0;
105	snprintf(path, sizeof(path), "%s/node.%06d.%d", mp, getpid(), n);
106	start = time(NULL);
107	while ((time(NULL) - start) < RUNTIME) {
108		dev = makedev(arc4random(), arc4random());
109		mode = arc4random() % 0x10000;
110		r = mknod(path, mode, dev);
111		if (r == 0) {
112			if ((fd = open(path, O_RDWR)) != -1) {
113				write(fd, "x", 1);
114				close(fd);
115			}
116			unlink(path);
117			n++;
118			snprintf(path, sizeof(path), "%s/node.%06d.%d", mp,
119			    getpid(), n);
120		}
121	}
122
123	_exit(0);
124}
125
126int
127main(int argc, char *argv[])
128{
129	pid_t pids[PARALLEL];
130	size_t len;
131	int e, i, status;
132
133	if (argc != 2)
134		return (1);
135	mp = argv[1];
136	e = 0;
137	len = PAGE_SIZE;
138	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
139	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
140		err(1, "mmap");
141
142	share[SYNC] = 0;
143	for (i = 0; i < PARALLEL; i++) {
144		if ((pids[i] = fork()) == 0)
145			test();
146		if (pids[i] == -1)
147			err(1, "fork()");
148	}
149	for (i = 0; i < PARALLEL; i++) {
150		if (waitpid(pids[i], &status, 0) == -1)
151			err(1, "waitpid(%d)", pids[i]);
152		if (status != 0) {
153			if (WIFSIGNALED(status))
154				fprintf(stderr,
155				    "pid %d exit signal %d\n",
156				    pids[i], WTERMSIG(status));
157		}
158		e += status == 0 ? 0 : 1;
159	}
160
161	return (e);
162}
163