vnode.c revision 1.5 1 1.5 christos /* $NetBSD: vnode.c,v 1.5 2008/12/29 05:56:02 christos 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 *
19 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jdolecek * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jdolecek */
31 1.1 jdolecek
32 1.1 jdolecek #include <sys/param.h>
33 1.1 jdolecek #include <sys/event.h>
34 1.1 jdolecek #include <sys/time.h>
35 1.1 jdolecek
36 1.1 jdolecek #include <err.h>
37 1.1 jdolecek #include <errno.h>
38 1.1 jdolecek #include <fcntl.h>
39 1.1 jdolecek #include <stdio.h>
40 1.1 jdolecek #include <stdlib.h>
41 1.1 jdolecek #include <unistd.h>
42 1.1 jdolecek #include <inttypes.h>
43 1.1 jdolecek
44 1.1 jdolecek int
45 1.1 jdolecek main(int argc, char **argv)
46 1.1 jdolecek {
47 1.1 jdolecek struct timespec timeout;
48 1.1 jdolecek struct timeval then, now, diff;
49 1.1 jdolecek struct kevent event[2];
50 1.1 jdolecek char buffer[128], *pref;
51 1.1 jdolecek int fd, kq, n, i;
52 1.1 jdolecek
53 1.1 jdolecek if (argc != 2)
54 1.1 jdolecek errx(1, "Usage: %s file", argv[0]);
55 1.1 jdolecek
56 1.1 jdolecek fd = open(argv[1], O_RDONLY|O_CREAT, 0644);
57 1.1 jdolecek if (fd == -1)
58 1.1 jdolecek err(1, "open: %s", argv[1]);
59 1.1 jdolecek
60 1.1 jdolecek #if 0
61 1.1 jdolecek sprintf(buffer, "/mnt/fd/%d", fd);
62 1.1 jdolecek if ((fd = open(buffer, O_RDONLY)) < 0)
63 1.1 jdolecek err(1, "open fdesc: %s", buffer);
64 1.1 jdolecek #endif
65 1.1 jdolecek
66 1.1 jdolecek #ifdef READ_TEST
67 1.1 jdolecek lseek(fd, 0, SEEK_END);
68 1.1 jdolecek #endif
69 1.1 jdolecek
70 1.1 jdolecek kq = kqueue();
71 1.1 jdolecek if (kq == -1)
72 1.1 jdolecek err(1, "kqueue");
73 1.1 jdolecek
74 1.1 jdolecek i=0;
75 1.1 jdolecek EV_SET(&event[i], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
76 1.1 jdolecek NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB |
77 1.2 itojun NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, 0);
78 1.1 jdolecek i++;
79 1.1 jdolecek
80 1.1 jdolecek #ifdef READ_TEST
81 1.1 jdolecek EV_SET(&event[i], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, NULL, NULL);
82 1.1 jdolecek i++;
83 1.1 jdolecek printf("i is %d\n", i);
84 1.1 jdolecek #endif
85 1.1 jdolecek
86 1.1 jdolecek n = kevent(kq, event, i, NULL, 0, NULL);
87 1.1 jdolecek if (n == -1)
88 1.1 jdolecek err(1, "kevent(1)");
89 1.1 jdolecek
90 1.1 jdolecek timeout.tv_sec = 60;
91 1.1 jdolecek timeout.tv_nsec = 0;
92 1.1 jdolecek
93 1.1 jdolecek for (;;) {
94 1.1 jdolecek pref="";
95 1.1 jdolecek if (gettimeofday(&then, NULL) == -1)
96 1.1 jdolecek err(1, "gettimeofday then");
97 1.1 jdolecek n = kevent(kq, NULL, 0, event, 1, &timeout);
98 1.1 jdolecek if (gettimeofday(&now, NULL) == -1)
99 1.1 jdolecek err(1, "gettimeofday now");
100 1.1 jdolecek timersub(&now, &then, &diff);
101 1.5 christos printf("vnode '%s': kevent returned %d in %lld.%06ld\n",
102 1.1 jdolecek argv[1],
103 1.5 christos n, (long long)diff.tv_sec, (long)diff.tv_usec);
104 1.1 jdolecek
105 1.1 jdolecek if (n == -1)
106 1.1 jdolecek err(1, "kevent");
107 1.1 jdolecek else if (n == 0)
108 1.1 jdolecek continue;
109 1.1 jdolecek
110 1.1 jdolecek printf("vnode '%s': kevent: filter %d flags: 0x%02x, fflags: 0x%02x [",
111 1.1 jdolecek argv[1],
112 1.1 jdolecek event[0].filter, event[0].flags, event[0].fflags);
113 1.1 jdolecek
114 1.1 jdolecek #define DUMPFLAG(x) \
115 1.1 jdolecek do \
116 1.1 jdolecek if (event[0].fflags & x) { \
117 1.1 jdolecek printf ("%s%s", pref, #x); \
118 1.1 jdolecek pref=", "; \
119 1.1 jdolecek } \
120 1.1 jdolecek while (0)
121 1.1 jdolecek
122 1.1 jdolecek DUMPFLAG(NOTE_DELETE);
123 1.1 jdolecek DUMPFLAG(NOTE_WRITE);
124 1.1 jdolecek DUMPFLAG(NOTE_EXTEND);
125 1.1 jdolecek DUMPFLAG(NOTE_ATTRIB);
126 1.1 jdolecek DUMPFLAG(NOTE_LINK);
127 1.1 jdolecek DUMPFLAG(NOTE_RENAME);
128 1.1 jdolecek DUMPFLAG(NOTE_REVOKE);
129 1.1 jdolecek printf("], data %" PRId64 "\n", event[0].data);
130 1.1 jdolecek
131 1.1 jdolecek if (event[0].filter == EVFILT_READ) {
132 1.1 jdolecek if (event[0].data < 0)
133 1.1 jdolecek lseek(fd, 0, SEEK_END);
134 1.1 jdolecek n = read(fd, buffer, 128);
135 1.3 christos if (n < 0)
136 1.3 christos err(1, "read");
137 1.1 jdolecek buffer[n] = '\0';
138 1.1 jdolecek printf("[%d] %s", n, buffer);
139 1.1 jdolecek }
140 1.1 jdolecek
141 1.1 jdolecek if (event[0].fflags & NOTE_REVOKE) {
142 1.1 jdolecek printf("%s revoked\n", argv[1]);
143 1.1 jdolecek break;
144 1.1 jdolecek }
145 1.1 jdolecek }
146 1.1 jdolecek return (0);
147 1.1 jdolecek }
148