1 /* $NetBSD: vnode.c,v 1.2 2003/10/21 09:42:48 itojun Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Luke Mewburn. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/event.h> 41 #include <sys/time.h> 42 43 #include <err.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <inttypes.h> 50 51 int 52 main(int argc, char **argv) 53 { 54 struct timespec timeout; 55 struct timeval then, now, diff; 56 struct kevent event[2]; 57 char buffer[128], *pref; 58 int fd, kq, n, i; 59 60 if (argc != 2) 61 errx(1, "Usage: %s file", argv[0]); 62 63 fd = open(argv[1], O_RDONLY|O_CREAT, 0644); 64 if (fd == -1) 65 err(1, "open: %s", argv[1]); 66 67 #if 0 68 sprintf(buffer, "/mnt/fd/%d", fd); 69 if ((fd = open(buffer, O_RDONLY)) < 0) 70 err(1, "open fdesc: %s", buffer); 71 #endif 72 73 #ifdef READ_TEST 74 lseek(fd, 0, SEEK_END); 75 #endif 76 77 kq = kqueue(); 78 if (kq == -1) 79 err(1, "kqueue"); 80 81 i=0; 82 EV_SET(&event[i], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, 83 NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | 84 NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, 0); 85 i++; 86 87 #ifdef READ_TEST 88 EV_SET(&event[i], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, NULL, NULL); 89 i++; 90 printf("i is %d\n", i); 91 #endif 92 93 n = kevent(kq, event, i, NULL, 0, NULL); 94 if (n == -1) 95 err(1, "kevent(1)"); 96 97 timeout.tv_sec = 60; 98 timeout.tv_nsec = 0; 99 100 for (;;) { 101 pref=""; 102 if (gettimeofday(&then, NULL) == -1) 103 err(1, "gettimeofday then"); 104 n = kevent(kq, NULL, 0, event, 1, &timeout); 105 if (gettimeofday(&now, NULL) == -1) 106 err(1, "gettimeofday now"); 107 timersub(&now, &then, &diff); 108 printf("vnode '%s': kevent returned %d in %ld.%06ld\n", 109 argv[1], 110 n, diff.tv_sec, diff.tv_usec); 111 112 if (n == -1) 113 err(1, "kevent"); 114 else if (n == 0) 115 continue; 116 117 printf("vnode '%s': kevent: filter %d flags: 0x%02x, fflags: 0x%02x [", 118 argv[1], 119 event[0].filter, event[0].flags, event[0].fflags); 120 121 #define DUMPFLAG(x) \ 122 do \ 123 if (event[0].fflags & x) { \ 124 printf ("%s%s", pref, #x); \ 125 pref=", "; \ 126 } \ 127 while (0) 128 129 DUMPFLAG(NOTE_DELETE); 130 DUMPFLAG(NOTE_WRITE); 131 DUMPFLAG(NOTE_EXTEND); 132 DUMPFLAG(NOTE_ATTRIB); 133 DUMPFLAG(NOTE_LINK); 134 DUMPFLAG(NOTE_RENAME); 135 DUMPFLAG(NOTE_REVOKE); 136 printf("], data %" PRId64 "\n", event[0].data); 137 138 if (event[0].filter == EVFILT_READ) { 139 if (event[0].data < 0) 140 lseek(fd, 0, SEEK_END); 141 n = read(fd, buffer, 128); 142 buffer[n] = '\0'; 143 printf("[%d] %s", n, buffer); 144 } 145 146 if (event[0].fflags & NOTE_REVOKE) { 147 printf("%s revoked\n", argv[1]); 148 break; 149 } 150 } 151 return (0); 152 } 153