xref: /freebsd/tools/test/stress2/misc/linger3.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 false EMLINK
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 > linger3.c
38mycc -o linger3 -Wall -Wextra -O2 linger3.c
39rm -f linger3.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	# 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/linger3"
55
56cd $here
57
58while mount | grep -q $mntpoint; do
59	umount $mntpoint 2> /dev/null || sleep 1
60done
61mdconfig -d -u $mdstart
62rm -f /tmp/linger3
63exit 0
64EOF
65#include <sys/types.h>
66#include <err.h>
67#include <errno.h>
68#include <fcntl.h>
69#include <sched.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <sys/stat.h>
73#include <sys/wait.h>
74#include <unistd.h>
75
76#define PARALLEL 4
77#define ND ((32767 / PARALLEL) - 2)
78
79static pid_t p;
80
81void
82setup(int loop)
83{
84	int d;
85	char name[128];
86
87	for (d = 0; d < ND; d++) {
88		snprintf(name, sizeof(name), "d%06d.%03d", p, d);
89		if (mkdir(name, 00700) == -1)
90			err(1, "mkdir(%s) @ loop #%d", name, loop);
91	}
92}
93
94void
95prune(int loop)
96{
97	int d;
98	char name[128];
99
100	for (d = 0; d < ND; d++) {
101		snprintf(name, sizeof(name), "d%06d.%03d", p, d);
102		if (rmdir(name) == -1)
103			err(1, "rmdir(%s) @ loop #%d", name, loop);
104	}
105}
106
107int
108main()
109{
110	int i, j;
111	int linger = 0;
112
113	if (getenv("LINGER"))
114		linger = atoi(getenv("LINGER"));
115
116	for (i = 0; i < PARALLEL; i++) {
117		if (fork() == 0) {
118			p = getpid();
119			for (j = 0; j < 10; j++) {
120				setup(j);
121				prune(j);
122				if (linger != 0)
123					sleep(linger);
124			}
125			_exit(0);
126		}
127	}
128
129	for (i = 0; i < PARALLEL; i++)
130		wait(NULL);
131
132	return (0);
133}
134