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