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# $FreeBSD$ 29# 30# Test EVFILT_VNODE. Found page fault in knlist_add+0x39 31# 32# Test scenario by kib@ 33 34. ../default.cfg 35 36odir=`pwd` 37 38cd /tmp 39sed '1,/^EOF/d' < $odir/$0 > kevent.c 40cc -o kevent -Wall kevent.c 41rm -f kevent.c 42 43cd $RUNDIR/.. 44/tmp/kevent xxx yyy 45 46rm -f /tmp/kevent 47 48exit 49EOF 50#include <sys/types.h> 51#include <sys/stat.h> 52#include <sys/mman.h> 53#include <sys/event.h> 54 55#include <err.h> 56#include <errno.h> 57#include <fcntl.h> 58#include <stdio.h> 59#include <stdlib.h> 60#include <string.h> 61#include <unistd.h> 62#include <sys/wait.h> 63 64static char *file1, *file2; 65 66#define N 1000 67 68 69void 70test(test) { 71 int kq = -1; 72 int n; 73 struct kevent ev[2]; 74 int fd; 75 76 if ((fd = open(file1, O_RDONLY, 0)) == -1) 77 err(1, "open(%s)(2)", file1); 78 79 if ((kq = kqueue()) < 0) 80 err(1, "kqueue()"); 81 82 n = 0; 83 EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, 84 NOTE_DELETE, 0, 0); 85 n++; 86 87 if (kevent(kq, ev, n, NULL, 0, NULL) < 0) 88 err(1, "kevent()"); 89 90 memset(&ev, 0, sizeof(ev)); 91 n = kevent(kq, NULL, 0, ev, 1, NULL); 92// printf("Event 1\n"); 93 close(fd); 94 close(kq); 95 96/* Once the rendezvous file is gone create a new kevent */ 97 98 if ((fd = open(file2, O_RDONLY, 0)) == -1) 99 err(1, "open(%s)(2)", file2); 100 101 if ((kq = kqueue()) < 0) 102 err(1, "kqueue()"); 103 104 n = 0; 105 EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, 106 NOTE_DELETE, 0, 0); 107 n++; 108 109 if (kevent(kq, ev, n, NULL, 0, NULL) < 0) 110 err(1, "kevent()"); 111 112 memset(&ev, 0, sizeof(ev)); 113 n = kevent(kq, NULL, 0, ev, 1, NULL); 114// printf("Event 2\n"); 115 close(fd); 116 close(kq); 117} 118 119int 120main(int argc, char **argv) { 121 int i, j; 122 int fd; 123 int status; 124 125 if (argc != 3) { 126 fprintf(stderr, "Usage: %s <rendezvous file> <tail file>\n", argv[0]); 127 return (1); 128 } 129 file1 = argv[1]; 130 file2 = argv[2]; 131 132 for (j = 0; j < 100; j++) { 133 if ((fd = open(file1, O_CREAT | O_TRUNC | O_RDWR, 0660)) == -1) 134 err(1, "open(%s)", file1); 135 close(fd); 136 if ((fd = open(file2, O_CREAT | O_TRUNC | O_RDWR, 0660)) == -1) 137 err(1, "open(%s)", file2); 138 close(fd); 139 140 for (i = 0; i < N; i++) { 141 if (fork() == 0) { 142 test(); 143 return (0); 144 } 145 } 146 147 sleep(1); 148 if (unlink(file1) == -1) 149 err(1, "unlink(%s). %s:%d\n", file1, __FILE__, __LINE__); 150 sleep(1); 151 if (unlink(file2) == -1) 152 err(1, "unlink(%s). %s:%d\n", file2, __FILE__, __LINE__); 153 154 for (i = 0; i < N; i++) { 155 if (wait(&status) == -1) 156 err(1, "wait(), %s:%d", __FILE__, __LINE__); 157 } 158 } 159 160 return (0); 161} 162