xref: /freebsd/tools/test/stress2/misc/linger4.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# Test for SU problem with "out of inodes" for CREAT
30
31[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > linger4.c
38mycc -o linger4 -Wall -Wextra -O2 linger4.c
39rm -f linger4.c
40
41mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
42mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
43
44mdconfig -a -t swap -s 2g -u $mdstart
45[ $# -eq 1 ] && opt="$1"
46[ $# -eq 0 ] && opt="$newfs_flags -n"	# No argument == default flag
47echo "newfs $opt md$mdstart"
48newfs $opt md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50
51cd $mntpoint
52chmod 777 $mntpoint
53
54su $testuser -c "/tmp/linger4" ||
55    { ls -la $mntpoint; df -i $mntpoint; }
56
57cd $here
58
59while mount | grep -q $mntpoint; do
60	umount $mntpoint 2> /dev/null || sleep 1
61done
62mdconfig -d -u $mdstart
63rm -f /tmp/linger4
64exit 0
65EOF
66#include <sys/types.h>
67#include <sys/stat.h>
68#include <sys/wait.h>
69
70#include <err.h>
71#include <errno.h>
72#include <fcntl.h>
73#include <stdio.h>
74#include <stdlib.h>
75#include <time.h>
76#include <unistd.h>
77
78#define LOOPS 300
79#define NUMBER 80000 / PARALLEL		/* Number of files to use. Max is 131068 */
80#define PARALLEL 4
81#define TIMEOUT (20 * 60)
82
83void
84Creat(int loopno)
85{
86	int e, fd, i, j;
87	char file[128];
88	char path[128];
89	pid_t pid;
90
91	e = 0;
92	pid = getpid();
93	sprintf(path, "f%06d", pid);
94        if (mkdir(path, 0770) == -1)
95                warn("mkdir(%s), %s:%d, loop #%d", path, __FILE__, __LINE__, loopno);
96	chdir(path);
97
98	for (j = 0; j < NUMBER; j++) {
99		sprintf(file,"p%05d.%05d", pid, j);
100		if ((fd = creat(file, 0660)) == -1) {
101			if (errno != EINTR) {
102				warn("creat(%s). %s:%d, loop #%d", file, __FILE__, __LINE__, loopno);
103				e = 1;
104				break;
105			}
106		}
107		if (fd != -1 && close(fd) == -1)
108			err(2, "close(%d)", j);
109
110	}
111
112	for (i = --j; i >= 0; i--) {
113		sprintf(file,"p%05d.%05d", pid, i);
114		if (unlink(file) == -1)
115			err(3, "unlink(%s)", file);
116
117	}
118        chdir ("..");
119        if (rmdir(path) == -1)
120                err(1, "rmdir(%s), %s:%d, loop #%d", path, __FILE__, __LINE__, loopno);
121
122	_exit(e);
123}
124
125int
126main()
127{
128	time_t start;
129	int e, i, j, status;
130
131	e = 0;
132	start = time(NULL);
133	for (j = 0; j < LOOPS; j++) {
134		for (i = 0; i < PARALLEL; i++) {
135			if (fork() == 0)
136				Creat(j);
137		}
138
139		for (i = 0; i < PARALLEL; i++) {
140			wait(&status);
141			e += WEXITSTATUS(status);
142		}
143		if (e != 0)
144			break;
145//		sleep(60); /* No problems if this is included */
146		if (time(NULL) - start > TIMEOUT) {
147			fprintf(stderr, "Timeout.\n");
148			e++;
149			break;
150		}
151	}
152
153	return (e);
154}
155