xref: /freebsd/tools/test/stress2/misc/suj17.sh (revision 9768746b)
1#!/bin/sh
2
3#
4# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Truncate scenario for suj.
32# "panic: worklist_insert: 0xc8bc5b00 freework(0x8009) already on list"
33# seen.
34
35. ../default.cfg
36
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > suj17.c
40mycc -o suj17 -Wall -Wextra -O2 suj17.c
41rm -f suj17.c
42cd $here
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 2g -u $mdstart || exit 1
48[ $# -eq 1 ] && opt="$1"
49[ $# -eq 0 ] && opt="-j"
50echo "newfs $opt md$mdstart"
51newfs $opt md$mdstart > /dev/null
52mount /dev/md$mdstart $mntpoint
53chmod 777 $mntpoint
54
55su $testuser -c "cd $mntpoint; /tmp/suj17"
56s=$?
57
58while mount | grep $mntpoint | grep -q /dev/md; do
59	umount $mntpoint || sleep 1
60done
61checkfs /dev/md$mdstart || s=$?
62mdconfig -d -u $mdstart
63rm -f /tmp/suj17
64exit $s
65EOF
66#include <fcntl.h>
67#include <err.h>
68#include <sched.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <unistd.h>
72#include <sys/wait.h>
73
74#define SIZ (1024 * 1024 - 1)
75char buf[SIZ];
76
77void
78test()
79{
80	int fd, i;
81	char name[128];
82	off_t len = 104857600LL; /* 100 Mb */
83	off_t pos;
84
85	sprintf(name, "%06d", getpid());
86	if ((fd = open(name, O_WRONLY | O_CREAT, 0666)) == -1)
87			err(1, "open(%s)", name);
88	for (i = 0; i < 100; i++) {
89		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
90			err(1, "write");
91	}
92
93	for (;;) {
94		if (access("rendezvous", R_OK) == 0)
95			break;
96		sched_yield();
97	}
98
99	srand48(getpid());
100	for (i = 0; i < 50000; i++) {
101		pos = lrand48() % (len - sizeof(buf));
102		if (ftruncate(fd, pos) == -1)
103			err(1, "ftruncate");
104		pos = lrand48() % (len - sizeof(buf));
105		if (lseek(fd, pos, SEEK_SET) == -1)
106			err(1, "lseek");
107		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
108			err(1, "write");
109	}
110	close(fd);
111	unlink(name);
112	_exit(0);
113}
114
115int
116main()
117{
118	int fd, i, j, status;
119
120	for (i = 0; i < 1; i++) {
121		for (j = 0; j < 6; j++) {
122			if (fork() == 0)
123				test();
124		}
125		if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
126			err(1, "open()");
127		close(fd);
128
129		for (j = 0; j < 6; j++)
130			wait(&status);
131		unlink("rendezvous");
132	}
133
134	return (0);
135}
136