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