xref: /freebsd/tools/test/stress2/misc/linger.sh (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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# Demonstrate premature "out of inodes" problem with SU
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > linger.c
38mycc -o linger -Wall -O2 linger.c
39rm -f linger.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
45[ $# -eq 1 ] && opt="$1"
46[ $# -eq 0 ] && opt=$newfs_flags	# No argument == default flag
47echo "newfs $opt md$mdstart"
48newfs $opt md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50chmod 777 $mntpoint
51
52if ! su $testuser -c "cd $mntpoint; /tmp/linger $size"; then
53	min=2
54	[ -r $mntpoint/.sujournal ] && min=3
55	r=`df -hi $mntpoint | head -1`
56	echo "         $r"
57	for i in `jot 10`; do
58		r=`df -hi $mntpoint | tail -1`
59		echo "`date '+%T'` $r"
60		[ `echo $r | awk '{print $6}'` = $min ] && break
61		sleep 10
62	done
63	ls -lR $mntpoint
64fi
65
66while mount | grep "$mntpoint " | grep -q /dev/md; do
67	umount $mntpoint || sleep 1
68done
69mdconfig -d -u $mdstart
70rm -f /tmp/linger
71exit
72EOF
73#include <sys/mount.h>
74#include <sys/param.h>
75#include <sys/stat.h>
76#include <sys/wait.h>
77
78#include <err.h>
79#include <errno.h>
80#include <fcntl.h>
81#include <sched.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <time.h>
85#include <unistd.h>
86
87#define PARALLEL 10
88#define TIMEOUT 1200
89static int size = 6552;	/* 10 free inodes */
90
91static int
92test(void)
93{
94	int fd, i, j;
95	pid_t pid;
96	char file[128];
97
98	for (;;) {
99		if (access("rendezvous", R_OK) == 0)
100			break;
101		sched_yield();
102	}
103	pid = getpid();
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				break;
111			}
112		}
113		if (fd != -1 && close(fd) == -1)
114			err(2, "close(%d)", j);
115
116	}
117	sleep(3);
118
119	for (i = --j; i >= 0; i--) {
120		sprintf(file,"p%05d.%05d", pid, i);
121		if (unlink(file) == -1)
122			warn("unlink(%s)", file);
123
124	}
125	return (0);
126}
127
128int
129main(void)
130{
131	time_t start;
132	int error = 0, fd, i, j, status;
133
134	umask(0);
135	if ((fd = open("continue", O_CREAT, 0644)) == -1)
136		err(1, "open()");
137	close(fd);
138	start = time(NULL);
139	for (i = 0; i < 100; i++) {
140		for (j = 0; j < PARALLEL; j++) {
141			if (fork() == 0) {
142				test();
143				exit(0);
144			}
145		}
146
147		if ((fd = open("rendezvous", O_CREAT, 0644)) == -1)
148			err(1, "open()");
149		close(fd);
150
151		for (j = 0; j < PARALLEL; j++) {
152			wait(&status);
153			error += status;
154		}
155
156		unlink("rendezvous");
157		if (access("continue", R_OK) == -1)
158			break;
159		if (time(NULL) - start > TIMEOUT) {
160			fprintf(stderr, "FAIL Timeout\n");
161			break;
162		}
163	}
164	unlink("continue");
165
166	return (error != 0);
167}
168