xref: /freebsd/tools/test/stress2/misc/truncate9.sh (revision d91f8db5f1822c43cd256f19aae1d059e4b25a26)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Truncate test scenario. No problems seen.
31
32. ../default.cfg
33
34odir=`pwd`
35cd /tmp
36sed '1,/^EOF/d' < $odir/$0 > truncate9.c
37mycc -o truncate9 -Wall -Wextra truncate9.c || exit 1
38rm -f truncate9.c
39set -e
40mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
41[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
42mdconfig -a -t swap -s 2g -u $mdstart
43newfs $newfs_flags -n md$mdstart > /dev/null
44mount /dev/md$mdstart $mntpoint
45set +e
46
47(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100) > /dev/null &
48cd $mntpoint
49for i in `jot 5`; do
50	/tmp/truncate9 &
51	pids="$pids $!"
52done
53for i in $pids; do
54	wait $i
55done
56while pkill swap; do :; done
57wait
58cd $odir
59
60umount $mntpoint
61mdconfig -d -u $mdstart
62rm /tmp/truncate9
63exit $s
64EOF
65#include <sys/mman.h>
66
67#include <err.h>
68#include <fcntl.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72#include <time.h>
73#include <unistd.h>
74
75int
76main (void)
77{
78	off_t opos, pos;
79	time_t start;
80	int fd, i;
81	char file[80], *cp;
82
83	snprintf(file, sizeof(file), "file.%d", getpid());
84	start = time(NULL);
85	if ((fd = open(file, O_RDWR | O_TRUNC | O_CREAT, 0644)) == -1)
86		err(1, "open(%s)", file);
87	close(fd);
88	while (time(NULL) - start < 120) {
89		if ((fd = open(file, O_RDWR)) == -1)
90			err(1, "open(%s)", file);
91		pos = arc4random();
92		opos = pos = (pos << 12) | arc4random();
93		if (ftruncate(fd, pos) == -1)
94			err(1, "ftruncate()");
95		if ((cp = mmap(NULL, pos, PROT_READ | PROT_WRITE,
96		    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
97			err(1, "mmap");
98		cp[0] = 1;
99		cp[pos - 1] = 2;
100		for (i = 0; i < 10; i++) {
101			while (pos >= opos) {
102				pos = arc4random();
103				pos = (pos << 12) | arc4random();
104			}
105			if (ftruncate(fd, pos) == -1)
106				err(1, "ftruncate()");
107			cp[0] = 1;
108			cp[pos - 1] = 2;
109		}
110
111		if (munmap(cp, opos) == -1)
112			err(1, "munmap");
113		close(fd);
114	}
115	unlink(file);
116
117	return (0);
118}
119