xref: /freebsd/tools/test/stress2/misc/kevent5.sh (revision 4d846d26)
1#!/bin/sh
2
3#
4# Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
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 EVFILT_VNODE. Found page fault in knlist_add+0x39
30# Test scenario by kib@
31
32. ../default.cfg
33
34odir=`pwd`
35
36cd /tmp
37sed '1,/^EOF/d' < $odir/$0 > kevent5.c
38mycc -o kevent5 -Wall -Wextra -O2 -g kevent5.c || exit 1
39rm -f kevent5.c
40
41[ -d $RUNDIR ] || mkdir -p $RUNDIR
42cd $RUNDIR
43/tmp/kevent5 kevent5.xxx kevent5.yyy
44s=$?
45
46rm -f /tmp/kevent5 kevent.xxx kevent.yyy
47
48exit $s
49EOF
50#include <sys/param.h>
51#include <sys/stat.h>
52#include <sys/mman.h>
53#include <sys/event.h>
54
55#include <machine/atomic.h>
56
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <unistd.h>
64#include <sys/wait.h>
65
66volatile u_int *share;
67
68static char *file1, *file2;
69
70#define N 1000
71#define SYNC 0
72
73static void
74test(void) {
75
76	struct kevent ev[2];
77	struct timespec ts;
78	int fd, kq, n;
79
80	if ((fd = open(file1, O_RDONLY, 0)) == -1)
81		err(1, "open(%s)(2)", file1);
82	atomic_add_int(&share[SYNC], 1);
83
84	if ((kq = kqueue()) < 0)
85		err(1, "kqueue()");
86
87	n = 0;
88	EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
89		NOTE_DELETE, 0, 0);
90	n++;
91
92	ts.tv_sec  = 5;
93	ts.tv_nsec = 0;
94
95	if (kevent(kq, ev, n, NULL, 0, &ts) < 0)
96		err(1, "kevent()");
97
98	memset(&ev, 0, sizeof(ev));
99	n = kevent(kq, NULL, 0, ev, 1, NULL);
100	close(fd);
101	close(kq);
102
103/* Once the rendezvous file is gone create a new kevent */
104
105	if ((fd = open(file2, O_RDONLY, 0)) == -1)
106		err(1, "open(%s)(2)", file2);
107
108	if ((kq = kqueue()) < 0)
109		err(1, "kqueue()");
110
111	n = 0;
112	EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
113		NOTE_DELETE, 0, 0);
114	n++;
115
116	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
117		err(1, "kevent()");
118
119	memset(&ev, 0, sizeof(ev));
120	n = kevent(kq, NULL, 0, ev, 1, &ts);
121	close(fd);
122	close(kq);
123}
124
125int
126main(int argc, char **argv) {
127	size_t len;
128	int e, fd, i, j, status;
129
130	if (argc != 3) {
131		fprintf(stderr, "Usage: %s <rendezvous file> <tail file>\n",
132		    argv[0]);
133		return (1);
134	}
135	len = PAGE_SIZE;
136	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
137	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
138		err(1, "mmap");
139
140	e = 0;
141	file1 = argv[1];
142	file2 = argv[2];
143
144	for (j = 0; j < 100 && e == 0; j++) {
145		if ((fd = open(file1, O_CREAT | O_TRUNC | O_RDWR, 0660)) ==
146		    -1)
147			err(1, "open(%s)", file1);
148		close(fd);
149		if ((fd = open(file2, O_CREAT | O_TRUNC | O_RDWR, 0660)) ==
150		    -1)
151			err(1, "open(%s)", file2);
152		close(fd);
153
154		share[SYNC] = 0;
155		for (i = 0; i < N; i++) {
156			if (fork() == 0) {
157				test();
158				return (0);
159			}
160		}
161
162		while (share[SYNC] != N)
163			usleep(200);
164
165		sleep(1);
166		if (unlink(file1) == -1)
167			err(1, "unlink(%s). %s:%d\n", file1, __FILE__,
168			    __LINE__);
169		sleep(2);
170		if (unlink(file2) == -1)
171			err(1, "unlink(%s). %s:%d\n", file2, __FILE__,
172			    __LINE__);
173
174		for (i = 0; i < N; i++) {
175			if (wait(&status) == -1)
176				err(1, "wait(), %s:%d", __FILE__, __LINE__);
177			if (status != 0)
178				e = 1;
179		}
180	}
181
182	return (e);
183}
184