xref: /freebsd/tools/test/stress2/misc/truncate9.sh (revision 10ff414c)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
43bsdlabel -w md$mdstart auto
44newfs $newfs_flags -n md${mdstart}$part > /dev/null
45mount /dev/md${mdstart}$part $mntpoint
46set +e
47
48(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -l 100) > /dev/null &
49cd $mntpoint
50for i in `jot 5`; do
51	/tmp/truncate9 &
52	pids="$pids $!"
53done
54for i in $pids; do
55	wait $i
56done
57while pkill swap; do :; done
58wait
59cd $odir
60
61umount $mntpoint
62mdconfig -d -u $mdstart
63rm /tmp/truncate9
64exit $s
65EOF
66#include <sys/mman.h>
67
68#include <err.h>
69#include <fcntl.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <time.h>
74#include <unistd.h>
75
76int
77main (void)
78{
79	off_t opos, pos;
80	time_t start;
81	int fd, i;
82	char file[80], *cp;
83
84	snprintf(file, sizeof(file), "file.%d", getpid());
85	start = time(NULL);
86	if ((fd = open(file, O_RDWR | O_TRUNC | O_CREAT, 0644)) == -1)
87		err(1, "open(%s)", file);
88	close(fd);
89	while (time(NULL) - start < 120) {
90		if ((fd = open(file, O_RDWR)) == -1)
91			err(1, "open(%s)", file);
92		pos = arc4random();
93		opos = pos = (pos << 12) | arc4random();
94		if (ftruncate(fd, pos) == -1)
95			err(1, "ftruncate()");
96		if ((cp = mmap(NULL, pos, PROT_READ | PROT_WRITE,
97		    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
98			err(1, "mmap");
99		cp[0] = 1;
100		cp[pos - 1] = 2;
101		for (i = 0; i < 10; i++) {
102			while (pos >= opos) {
103				pos = arc4random();
104				pos = (pos << 12) | arc4random();
105			}
106			if (ftruncate(fd, pos) == -1)
107				err(1, "ftruncate()");
108			cp[0] = 1;
109			cp[pos - 1] = 2;
110		}
111
112		if (munmap(cp, opos) == -1)
113			err(1, "munmap");
114		close(fd);
115	}
116	unlink(file);
117
118	return (0);
119}
120