1 /* $NetBSD: vnode.c,v 1.4 2008/04/28 20:23:06 martin Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/event.h>
45 main(int argc
, char **argv
)
47 struct timespec timeout
;
48 struct timeval then
, now
, diff
;
49 struct kevent event
[2];
50 char buffer
[128], *pref
;
54 errx(1, "Usage: %s file", argv
[0]);
56 fd
= open(argv
[1], O_RDONLY
|O_CREAT
, 0644);
58 err(1, "open: %s", argv
[1]);
61 sprintf(buffer
, "/mnt/fd/%d", fd
);
62 if ((fd
= open(buffer
, O_RDONLY
)) < 0)
63 err(1, "open fdesc: %s", buffer
);
67 lseek(fd
, 0, SEEK_END
);
75 EV_SET(&event
[i
], fd
, EVFILT_VNODE
, EV_ADD
| EV_ENABLE
| EV_CLEAR
,
76 NOTE_DELETE
| NOTE_WRITE
| NOTE_EXTEND
| NOTE_ATTRIB
|
77 NOTE_LINK
| NOTE_RENAME
| NOTE_REVOKE
, 0, 0);
81 EV_SET(&event
[i
], fd
, EVFILT_READ
, EV_ADD
| EV_ENABLE
, 0, NULL
, NULL
);
83 printf("i is %d\n", i
);
86 n
= kevent(kq
, event
, i
, NULL
, 0, NULL
);
95 if (gettimeofday(&then
, NULL
) == -1)
96 err(1, "gettimeofday then");
97 n
= kevent(kq
, NULL
, 0, event
, 1, &timeout
);
98 if (gettimeofday(&now
, NULL
) == -1)
99 err(1, "gettimeofday now");
100 timersub(&now
, &then
, &diff
);
101 printf("vnode '%s': kevent returned %d in %lld.%06ld\n",
103 n
, (long long)diff
.tv_sec
, (long)diff
.tv_usec
);
110 printf("vnode '%s': kevent: filter %d flags: 0x%02x, fflags: 0x%02x [",
112 event
[0].filter
, event
[0].flags
, event
[0].fflags
);
114 #define DUMPFLAG(x) \
116 if (event[0].fflags & x) { \
117 printf ("%s%s", pref, #x); \
122 DUMPFLAG(NOTE_DELETE
);
123 DUMPFLAG(NOTE_WRITE
);
124 DUMPFLAG(NOTE_EXTEND
);
125 DUMPFLAG(NOTE_ATTRIB
);
127 DUMPFLAG(NOTE_RENAME
);
128 DUMPFLAG(NOTE_REVOKE
);
129 printf("], data %" PRId64
"\n", event
[0].data
);
131 if (event
[0].filter
== EVFILT_READ
) {
132 if (event
[0].data
< 0)
133 lseek(fd
, 0, SEEK_END
);
134 n
= read(fd
, buffer
, 128);
138 printf("[%d] %s", n
, buffer
);
141 if (event
[0].fflags
& NOTE_REVOKE
) {
142 printf("%s revoked\n", argv
[1]);