1 /* $NetBSD: h_tools.c,v 1.8 2007/07/23 15:05:43 jmmv Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
43 * Helper tools for several tests. These are kept in a single file due
44 * to the limitations of bsd.prog.mk to build a single program in a
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/event.h>
51 #include <sys/mount.h>
52 #include <sys/statvfs.h>
53 #include <sys/socket.h>
66 /* --------------------------------------------------------------------- */
68 static int getfh_main(int, char **);
69 static int kqueue_main(int, char **);
70 static int rename_main(int, char **);
71 static int sockets_main(int, char **);
72 static int statvfs_main(int, char **);
74 /* --------------------------------------------------------------------- */
77 getfh_main(int argc, char **argv)
85 error = getfh(argv[1], &fh);
87 err(EXIT_FAILURE, "can not getfh");
89 error = write(STDOUT_FILENO, &fh, sizeof(fh));
98 /* --------------------------------------------------------------------- */
101 kqueue_main(int argc, char **argv)
106 struct kevent *changes, event;
114 changes = malloc(sizeof(struct kevent) * (argc - 1));
116 errx(EXIT_FAILURE, "not enough memory");
118 for (i = 0; i < argc; i++) {
121 fd = open(argv[i], O_RDONLY);
123 err(EXIT_FAILURE, "cannot open %s", argv[i]);
125 EV_SET(&changes[i], fd, EVFILT_VNODE,
126 EV_ADD | EV_ENABLE | EV_ONESHOT,
127 NOTE_ATTRIB | NOTE_DELETE | NOTE_EXTEND | NOTE_LINK |
128 NOTE_RENAME | NOTE_REVOKE | NOTE_WRITE,
134 err(EXIT_FAILURE, "kqueue");
136 while ((line = fgetln(stdin, &len)) != NULL) {
143 (void)kevent(kq, changes, argc, &event, 1, &to);
146 assert(line[len - 1] == '\n');
147 line[len - 1] = '\0';
149 if (ec != EXIT_SUCCESS)
150 errx(ec, "%s returned %d", line, ec);
153 nev = kevent(kq, changes, argc, &event, 1, &to);
155 err(EXIT_FAILURE, "kevent");
157 for (i = 0; i < argc; i++)
158 if (event.ident == changes[i].ident)
161 if (event.fflags & NOTE_ATTRIB)
162 printf("%s - NOTE_ATTRIB\n", argv[i]);
163 if (event.fflags & NOTE_DELETE)
164 printf("%s - NOTE_DELETE\n", argv[i]);
165 if (event.fflags & NOTE_EXTEND)
166 printf("%s - NOTE_EXTEND\n", argv[i]);
167 if (event.fflags & NOTE_LINK)
168 printf("%s - NOTE_LINK\n", argv[i]);
169 if (event.fflags & NOTE_RENAME)
170 printf("%s - NOTE_RENAME\n", argv[i]);
171 if (event.fflags & NOTE_REVOKE)
172 printf("%s - NOTE_REVOKE\n", argv[i]);
173 if (event.fflags & NOTE_WRITE)
174 printf("%s - NOTE_WRITE\n", argv[i]);
179 for (i = 0; i < argc; i++)
180 close(changes[i].ident);
186 /* --------------------------------------------------------------------- */
189 rename_main(int argc, char **argv)
195 if (rename(argv[1], argv[2]) == -1) {
203 /* --------------------------------------------------------------------- */
206 sockets_main(int argc, char **argv)
209 struct sockaddr_un addr;
214 fd = socket(PF_LOCAL, SOCK_STREAM, 0);
220 (void)strlcpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
221 addr.sun_family = PF_UNIX;
223 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
234 /* --------------------------------------------------------------------- */
237 statvfs_main(int argc, char **argv)
245 memset(&buf, 0, sizeof(buf));
246 buf.f_version = STATFS_VERSION;
247 error = statfs(argv[1], &buf);
253 (void)printf("f_bsize=%llu\n", buf.f_bsize);
254 (void)printf("f_blocks=%llu\n", buf.f_blocks);
255 (void)printf("f_bfree=%llu\n", buf.f_bfree);
256 (void)printf("f_files=%llu\n", buf.f_files);
261 /* --------------------------------------------------------------------- */
264 main(int argc, char **argv)
274 if (strcmp(argv[0], "getfh") == 0)
275 error = getfh_main(argc, argv);
276 else if (strcmp(argv[0], "kqueue") == 0)
277 error = kqueue_main(argc, argv);
278 else if (strcmp(argv[0], "rename") == 0)
279 error = rename_main(argc, argv);
280 else if (strcmp(argv[0], "sockets") == 0)
281 error = sockets_main(argc, argv);
282 else if (strcmp(argv[0], "statvfs") == 0)
283 error = statvfs_main(argc, argv);
285 error = EXIT_FAILURE;