xref: /freebsd/tools/test/stress2/misc/mknod.sh (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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
49bsdlabel -w md$mdstart auto
50newfs $newfs_flags md${mdstart}$part > /dev/null
51mount /dev/md${mdstart}$part $mntpoint
52set +e
53
54cd $mntpoint
55$dir/mknod $mntpoint
56s=$?
57[ -f mknod.core -a $s -eq 0 ] &&
58    { ls -l mknod.core; mv mknod.core /tmp; s=1; }
59cd $odir
60
61for i in `jot 6`; do
62	mount | grep -q "on $mntpoint " || break
63	umount $mntpoint && break || sleep 10
64done
65[ $i -eq 6 ] && exit 1
66mdconfig -d -u $mdstart
67rm -rf $dir/mknod
68exit $s
69
70EOF
71#include <sys/param.h>
72#include <sys/mman.h>
73#include <sys/stat.h>
74#include <sys/wait.h>
75
76#include <machine/atomic.h>
77
78#include <err.h>
79#include <errno.h>
80#include <fcntl.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <time.h>
84#include <unistd.h>
85
86static volatile u_int *share;
87static char *mp;
88
89#define PARALLEL 4
90#define RUNTIME (1 * 60)
91#define SYNC 0
92
93static void
94test(void)
95{
96	dev_t dev;
97	mode_t mode;
98	time_t start;
99	int fd, n, r;
100	char path[128];
101
102	atomic_add_int(&share[SYNC], 1);
103	while (share[SYNC] != PARALLEL)
104		;
105	n = 0;
106	snprintf(path, sizeof(path), "%s/node.%06d.%d", mp, getpid(), n);
107	start = time(NULL);
108	while ((time(NULL) - start) < RUNTIME) {
109		dev = makedev(arc4random(), arc4random());
110		mode = arc4random() % 0x10000;
111		r = mknod(path, mode, dev);
112		if (r == 0) {
113			if ((fd = open(path, O_RDWR)) != -1) {
114				write(fd, "x", 1);
115				close(fd);
116			}
117			unlink(path);
118			n++;
119			snprintf(path, sizeof(path), "%s/node.%06d.%d", mp,
120			    getpid(), n);
121		}
122	}
123
124	_exit(0);
125}
126
127int
128main(int argc, char *argv[])
129{
130	pid_t pids[PARALLEL];
131	size_t len;
132	int e, i, status;
133
134	if (argc != 2)
135		return (1);
136	mp = argv[1];
137	e = 0;
138	len = PAGE_SIZE;
139	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
140	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
141		err(1, "mmap");
142
143	share[SYNC] = 0;
144	for (i = 0; i < PARALLEL; i++) {
145		if ((pids[i] = fork()) == 0)
146			test();
147		if (pids[i] == -1)
148			err(1, "fork()");
149	}
150	for (i = 0; i < PARALLEL; i++) {
151		if (waitpid(pids[i], &status, 0) == -1)
152			err(1, "waitpid(%d)", pids[i]);
153		if (status != 0) {
154			if (WIFSIGNALED(status))
155				fprintf(stderr,
156				    "pid %d exit signal %d\n",
157				    pids[i], WTERMSIG(status));
158		}
159		e += status == 0 ? 0 : 1;
160	}
161
162	return (e);
163}
164