xref: /netbsd/regress/sys/kern/kqueue/vnode/vnode.c (revision 6550d01e)
1 /*	$NetBSD: vnode.c,v 1.5 2008/12/29 05:56:02 christos 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  *
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.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/event.h>
34 #include <sys/time.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <inttypes.h>
43 
44 int
45 main(int argc, char **argv)
46 {
47 	struct timespec	timeout;
48 	struct timeval	then, now, diff;
49 	struct kevent	event[2];
50 	char		buffer[128], *pref;
51 	int		fd, kq, n, i;
52 
53 	if (argc != 2)
54 		errx(1, "Usage: %s file", argv[0]);
55 
56 	fd = open(argv[1], O_RDONLY|O_CREAT, 0644);
57 	if (fd == -1)
58 		err(1, "open: %s", argv[1]);
59 
60 #if 0
61 	sprintf(buffer, "/mnt/fd/%d", fd);
62 	if ((fd = open(buffer, O_RDONLY)) < 0)
63 		err(1, "open fdesc: %s", buffer);
64 #endif
65 
66 #ifdef READ_TEST
67 	lseek(fd, 0, SEEK_END);
68 #endif
69 
70         kq = kqueue();
71         if (kq == -1)
72                 err(1, "kqueue");
73 
74 	i=0;
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);
78 	i++;
79 
80 #ifdef READ_TEST
81 	EV_SET(&event[i], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, NULL, NULL);
82 	i++;
83 	printf("i is %d\n", i);
84 #endif
85 
86 	n = kevent(kq, event, i, NULL, 0, NULL);
87 	if (n == -1)
88 		err(1, "kevent(1)");
89 
90 	timeout.tv_sec = 60;
91 	timeout.tv_nsec = 0;
92 
93 	for (;;) {
94 		pref="";
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",
102 			argv[1],
103 			n, (long long)diff.tv_sec, (long)diff.tv_usec);
104 
105 		if (n == -1)
106 			err(1, "kevent");
107 		else if (n == 0)
108 			continue;
109 
110 		printf("vnode '%s': kevent: filter %d flags: 0x%02x, fflags: 0x%02x [",
111 		    argv[1],
112 		    event[0].filter, event[0].flags, event[0].fflags);
113 
114 #define DUMPFLAG(x) \
115 	do \
116 		if (event[0].fflags & x) { \
117 			printf ("%s%s", pref, #x); \
118 			pref=", "; \
119 		} \
120 	while (0)
121 
122 		DUMPFLAG(NOTE_DELETE);
123 		DUMPFLAG(NOTE_WRITE);
124 		DUMPFLAG(NOTE_EXTEND);
125 		DUMPFLAG(NOTE_ATTRIB);
126 		DUMPFLAG(NOTE_LINK);
127 		DUMPFLAG(NOTE_RENAME);
128 		DUMPFLAG(NOTE_REVOKE);
129 		printf("], data %" PRId64 "\n", event[0].data);
130 
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);
135 			if (n < 0)
136 				err(1, "read");
137 			buffer[n] = '\0';
138 			printf("[%d] %s", n, buffer);
139 		}
140 
141 		if (event[0].fflags & NOTE_REVOKE) {
142 			printf("%s revoked\n", argv[1]);
143 			break;
144 		}
145 	}
146 	return (0);
147 }
148