xref: /freebsd/tools/test/stress2/misc/linger3.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# 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
45bsdlabel -w md$mdstart auto
46[ $# -eq 1 ] && opt="$1"
47[ $# -eq 0 ] && opt=$newfs_flags	# 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/linger3"
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/linger3
64exit 0
65EOF
66#include <sys/types.h>
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <sched.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <sys/stat.h>
74#include <sys/wait.h>
75#include <unistd.h>
76
77#define PARALLEL 4
78#define ND ((32767 / PARALLEL) - 2)
79
80static pid_t p;
81
82void
83setup(int loop)
84{
85	int d;
86	char name[128];
87
88	for (d = 0; d < ND; d++) {
89		snprintf(name, sizeof(name), "d%06d.%03d", p, d);
90		if (mkdir(name, 00700) == -1)
91			err(1, "mkdir(%s) @ loop #%d", name, loop);
92	}
93}
94
95void
96prune(int loop)
97{
98	int d;
99	char name[128];
100
101	for (d = 0; d < ND; d++) {
102		snprintf(name, sizeof(name), "d%06d.%03d", p, d);
103		if (rmdir(name) == -1)
104			err(1, "rmdir(%s) @ loop #%d", name, loop);
105	}
106}
107
108int
109main()
110{
111	int i, j;
112	int linger = 0;
113
114	if (getenv("LINGER"))
115		linger = atoi(getenv("LINGER"));
116
117	for (i = 0; i < PARALLEL; i++) {
118		if (fork() == 0) {
119			p = getpid();
120			for (j = 0; j < 10; j++) {
121				setup(j);
122				prune(j);
123				if (linger != 0)
124					sleep(linger);
125			}
126			_exit(0);
127		}
128	}
129
130	for (i = 0; i < PARALLEL; i++)
131		wait(NULL);
132
133	return (0);
134}
135