xref: /freebsd/tools/test/stress2/misc/msync2.sh (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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# "panic: vm_pageout_flush: partially invalid page xx index 0/1" seen.
30# Fixed in r255566.
31
32. ../default.cfg
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/msync2.c
38mycc -o msync2  -Wall -Wextra msync2.c || exit 1
39rm -f msync2.c
40cd $odir
41
42dd if=/dev/zero bs=$((4096 + 1)) of=/tmp/msync2.inputfile count=1 \
43    status=none
44/tmp/msync2  /tmp/msync2.inputfile
45rm -f /tmp/msync2  /tmp/msync2.inputfile
46exit
47
48EOF
49#include <sys/param.h>
50#include <sys/fcntl.h>
51#include <sys/mman.h>
52#include <sys/stat.h>
53#include <sys/wait.h>
54
55#include <err.h>
56#include <errno.h>
57#include <stdlib.h>
58#include <time.h>
59#include <unistd.h>
60
61#define RUNTIME 400
62
63const char *file;
64char c;
65
66void
67wr(void)
68{
69	struct stat st;
70	char *p1;
71	size_t len;
72	int error, fd;
73
74	if ((fd = open(file, O_RDWR)) == -1)
75		err(1, "open %s", file);
76	if ((error = fstat(fd, &st)) == -1)
77		err(1, "stat(%s)", file);
78	len = round_page(st.st_size);
79	if ((p1 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
80	    MAP_FAILED)
81		err(1, "mmap");
82//	p1[len - 1] = 1;		/* No panic with this */
83	p1[arc4random() % len] = 1;	/* Need this for the panic */
84
85	if ((error = msync(p1, len, MS_SYNC | MS_INVALIDATE)) == -1)
86		if (errno != EBUSY)
87			err(1, "msync");
88
89	if (munmap(p1, len) == -1)
90		err(1, "unmap()");
91	close(fd);
92
93	_exit(0);
94}
95
96void
97test(void)
98{
99#if 1
100	int i;
101
102	for (i = 0; i < 3; i++)
103		if (fork() == 0)
104			wr();
105	for (i = 0; i < 3; i++)
106		wait(NULL);
107#else
108	wr();	/* No problem here */
109#endif
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 < RUNTIME) {
125		if (fork() == 0)
126			test();
127		wait(NULL);
128	}
129
130	return (0);
131}
132