xref: /freebsd/tools/test/stress2/misc/mmap5.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 EMC Corp.
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 scenario inspired by alc@
30# "panic: vm_page_dirty: page is invalid!" seen.
31# Fixed in r255396.
32
33. ../default.cfg
34
35dir=/tmp
36odir=`pwd`
37cd $dir
38sed '1,/^EOF/d' < $odir/$0 > $dir/mmap5.c
39mycc -o mmap5  -Wall -Wextra mmap5.c || exit 1
40rm -f mmap5.c
41cd $odir
42
43cp /tmp/mmap5  /tmp/mmap5.inputfile
44(cd ../testcases/swap; ./swap -t 1m -i 2) &
45cp /tmp/mmap5 /tmp/mmap5.inputfile
46/tmp/mmap5  /tmp/mmap5.inputfile
47while killall -9 swap; do
48	sleep .1
49done > /dev/null 2>&1
50wait
51rm -f /tmp/mmap5  /tmp/mmap5.inputfile
52exit
53
54EOF
55#include <err.h>
56#include <errno.h>
57#include <stdlib.h>
58#include <sys/fcntl.h>
59#include <sys/mman.h>
60#include <sys/param.h>
61#include <sys/stat.h>
62#include <sys/types.h>
63#include <sys/wait.h>
64#include <unistd.h>
65
66const char *file;
67
68void
69test2(void)
70{
71	struct stat st;
72	char *p;
73	size_t len;
74	int error, fd;
75
76	if ((fd = open(file, O_RDWR)) == -1)
77		err(1, "open %s", file);
78	if ((error = fstat(fd, &st)) == -1)
79		err(1, "stat(%s)", file);
80	len = round_page(st.st_size);
81	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
82		err(1, "mmap");
83	p[arc4random() % len] = 1;
84	if (arc4random() % 100 < 50)
85		if ((error = mlock(p, len)) == -1)
86			err(1, "mlock");
87	p[arc4random() % len] = 1;
88	if (arc4random() % 100 < 50)
89		if ((error = msync(p, len, MS_SYNC | MS_INVALIDATE)) == -1)
90			if (errno != EBUSY)
91				err(1, "msync");
92	if (munmap(p, len) == -1)
93		err(1, "unmap()");
94	close(fd);
95	_exit(0);
96
97}
98
99void
100test(void)
101{
102	int i;
103
104	for (i = 0; i < 3; i++)
105		if (fork() == 0)
106			test2();
107	for (i = 0; i < 3; i++)
108		wait(NULL);
109
110	_exit(0);
111}
112
113int
114main(int argc, char *argv[])
115{
116	int i;
117
118	if (argc != 2)
119		errx(1, "Usage: %s <file>", argv[0]);
120	file = argv[1];
121
122	for (i = 0; i < 30000; i++) {
123		if (fork() == 0)
124			test();
125		wait(NULL);
126	}
127
128	return (0);
129}
130