xref: /freebsd/tools/test/stress2/misc/mmap5.sh (revision a0ca4af9455b844c5e094fc1b09b1390ffa979fc)
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 <sys/param.h>
56#include <sys/fcntl.h>
57#include <sys/mman.h>
58#include <sys/stat.h>
59#include <sys/types.h>
60#include <sys/wait.h>
61#include <err.h>
62#include <errno.h>
63#include <stdlib.h>
64#include <time.h>
65#include <unistd.h>
66
67const char *file;
68
69void
70test2(void)
71{
72	struct stat st;
73	char *p;
74	size_t len;
75	int error, fd;
76
77	if ((fd = open(file, O_RDWR)) == -1)
78		err(1, "open %s", file);
79	if ((error = fstat(fd, &st)) == -1)
80		err(1, "stat(%s)", file);
81	len = round_page(st.st_size);
82	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
83		err(1, "mmap");
84	p[arc4random() % len] = 1;
85	if (arc4random() % 100 < 50)
86		if ((error = mlock(p, len)) == -1)
87			err(1, "mlock");
88	p[arc4random() % len] = 1;
89	if (arc4random() % 100 < 50)
90		if ((error = msync(p, len, MS_SYNC | MS_INVALIDATE)) == -1)
91			if (errno != EBUSY)
92				err(1, "msync");
93	if (munmap(p, len) == -1)
94		err(1, "unmap()");
95	close(fd);
96	_exit(0);
97
98}
99
100void
101test(void)
102{
103	int i;
104
105	for (i = 0; i < 3; i++)
106		if (fork() == 0)
107			test2();
108	for (i = 0; i < 3; i++)
109		wait(NULL);
110
111	_exit(0);
112}
113
114int
115main(int argc, char *argv[])
116{
117	time_t start;
118
119	if (argc != 2)
120		errx(1, "Usage: %s <file>", argv[0]);
121	file = argv[1];
122
123	start = time(NULL);
124	while (time(NULL) - start < 120) {
125		if (fork() == 0)
126			test();
127		wait(NULL);
128	}
129
130	return (0);
131}
132