xref: /freebsd/tools/test/stress2/misc/linger2.sh (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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# Variation of linger.sh, with emphasis on blocks.
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > linger2.c
38mycc -o linger2 -Wall -O2 linger2.c
39rm -f linger2.c
40cd $here
41
42mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
43mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
44mdconfig -a -t swap -s 1g -u $mdstart
45bsdlabel -w md$mdstart auto
46[ $# -eq 1 ] && opt="$1"
47[ $# -eq 0 ] && opt=$newfs_flags	# No argument == default flag
48newfs $opt -n md${mdstart}$part > /dev/null
49mount /dev/md${mdstart}$part $mntpoint
50chmod 777 $mntpoint
51set `df -i $mntpoint | tail -1 | awk '{print $3, $6}'`
52
53min=24
54[ -r $mntpoint/.sujournal ] && { size=88; min=8232; }
55if ! su $testuser -c "cd $mntpoint; /tmp/linger2 $size 2>/dev/null"; then
56	r=`df -i $mntpoint | head -1`
57	echo "         $r"
58	for i in `jot 12`; do
59		r=`df -ik $mntpoint | tail -1`
60		[ "$r" != "$old" ] && echo "`date '+%T'` $r"
61		old=$r
62		[ `echo $r | awk '{print $3}'` -le $min ] && break
63		sleep 10
64	done
65fi
66
67while mount | grep "$mntpoint " | grep -q /dev/md; do
68	umount $mntpoint || sleep 1
69done
70mdconfig -d -u $mdstart
71rm -f /tmp/linger2
72exit
73EOF
74#include <err.h>
75#include <errno.h>
76#include <fcntl.h>
77#include <sched.h>
78#include <stdio.h>
79#include <stdlib.h>
80#include <string.h>
81#include <sys/param.h>
82#include <sys/stat.h>
83#include <sys/wait.h>
84#include <unistd.h>
85
86#define PARALLEL 10
87static int size = 89;
88
89int
90test(void)
91{
92	int error = 0, fd, i, j;
93	pid_t pid;
94	char file[128];
95	char *buf;
96
97	for (;;) {
98		if (access("rendezvous", R_OK) == 0)
99			break;
100		sched_yield();
101	}
102	pid = getpid();
103	buf = malloc(1024 * 1024);
104	for (j = 0; j < size; j++) {
105		sprintf(file,"p%05d.%05d", pid, j);
106		if ((fd = creat(file, 0660)) == -1) {
107			if (errno != EINTR) {
108				warn("creat(%s). %s:%d", file, __FILE__, __LINE__);
109				unlink("continue");
110				error = 1;
111				break;
112			}
113		}
114		if (write(fd, buf, 1024 * 1024) != 1024 * 1024) {
115			warn("write()");
116			unlink("continue");
117			error = 1;
118			break;
119		}
120		if (fd != -1 && close(fd) == -1)
121			err(2, "close(%d)", j);
122
123	}
124	sleep(3);
125
126	if (error == 0)
127		j--;
128	for (i = j; i >= 0; i--) {
129		sprintf(file,"p%05d.%05d", pid, i);
130		if (unlink(file) == -1)
131			warn("unlink(%s)", file);
132
133	}
134	return (error);
135}
136
137int
138main(int argc, char **argv)
139{
140	int error = 0, fd, i, j, status;
141
142	if (argc == 2)
143		size = atoi(argv[1]);
144
145	umask(0);
146	if ((fd = open("continue", O_CREAT, 0644)) == -1)
147		err(1, "open()");
148	close(fd);
149	for (i = 0; i < 200; i++) {
150		for (j = 0; j < PARALLEL; j++) {
151			if (fork() == 0)
152				exit(test());
153		}
154
155		if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
156			err(1, "open()");
157		close(fd);
158
159		for (j = 0; j < PARALLEL; j++) {
160			wait(&status);
161			error += status;
162		}
163
164		unlink("rendezvous");
165		if (access("continue", R_OK) == -1) {
166			break;
167		}
168	}
169	unlink("continue");
170
171	return (error != 0);
172}
173