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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
36 * Helper tools for several tests. These are kept in a single file due
37 * to the limitations of bsd.prog.mk to build a single program in a
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/event.h>
44 #include <sys/mount.h>
45 #include <sys/statvfs.h>
46 #include <sys/stdint.h>
47 #include <sys/socket.h>
60 /* --------------------------------------------------------------------- */
62 static int getfh_main(int, char **);
63 static int kqueue_main(int, char **);
64 static int rename_main(int, char **);
65 static int sockets_main(int, char **);
66 static int statvfs_main(int, char **);
68 /* --------------------------------------------------------------------- */
71 getfh_main(int argc, char **argv)
79 error = getfh(argv[1], &fh);
81 err(EXIT_FAILURE, "can not getfh");
83 error = write(STDOUT_FILENO, &fh, sizeof(fh));
92 /* --------------------------------------------------------------------- */
95 kqueue_main(int argc, char **argv)
100 struct kevent *changes, event;
108 changes = malloc(sizeof(struct kevent) * (argc - 1));
110 errx(EXIT_FAILURE, "not enough memory");
112 for (i = 0; i < argc; i++) {
115 fd = open(argv[i], O_RDONLY);
117 err(EXIT_FAILURE, "cannot open %s", argv[i]);
119 EV_SET(&changes[i], fd, EVFILT_VNODE,
120 EV_ADD | EV_ENABLE | EV_ONESHOT,
121 NOTE_ATTRIB | NOTE_DELETE | NOTE_EXTEND | NOTE_LINK |
122 NOTE_RENAME | NOTE_REVOKE | NOTE_WRITE,
128 err(EXIT_FAILURE, "kqueue");
130 while ((line = fgetln(stdin, &len)) != NULL) {
137 (void)kevent(kq, changes, argc, &event, 1, &to);
140 assert(line[len - 1] == '\n');
141 line[len - 1] = '\0';
143 if (ec != EXIT_SUCCESS)
144 errx(ec, "%s returned %d", line, ec);
147 nev = kevent(kq, changes, argc, &event, 1, &to);
149 err(EXIT_FAILURE, "kevent");
151 for (i = 0; i < argc; i++)
152 if (event.ident == changes[i].ident)
155 if (event.fflags & NOTE_ATTRIB)
156 printf("%s - NOTE_ATTRIB\n", argv[i]);
157 if (event.fflags & NOTE_DELETE)
158 printf("%s - NOTE_DELETE\n", argv[i]);
159 if (event.fflags & NOTE_EXTEND)
160 printf("%s - NOTE_EXTEND\n", argv[i]);
161 if (event.fflags & NOTE_LINK)
162 printf("%s - NOTE_LINK\n", argv[i]);
163 if (event.fflags & NOTE_RENAME)
164 printf("%s - NOTE_RENAME\n", argv[i]);
165 if (event.fflags & NOTE_REVOKE)
166 printf("%s - NOTE_REVOKE\n", argv[i]);
167 if (event.fflags & NOTE_WRITE)
168 printf("%s - NOTE_WRITE\n", argv[i]);
173 for (i = 0; i < argc; i++)
174 close(changes[i].ident);
180 /* --------------------------------------------------------------------- */
183 rename_main(int argc, char **argv)
189 if (rename(argv[1], argv[2]) == -1) {
197 /* --------------------------------------------------------------------- */
200 sockets_main(int argc, char **argv)
203 struct sockaddr_un addr;
208 fd = socket(PF_LOCAL, SOCK_STREAM, 0);
214 (void)strlcpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
215 addr.sun_family = PF_UNIX;
217 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
228 /* --------------------------------------------------------------------- */
231 statvfs_main(int argc, char **argv)
239 memset(&buf, 0, sizeof(buf));
240 buf.f_version = STATFS_VERSION;
241 error = statfs(argv[1], &buf);
247 (void)printf("f_bsize=%ju\n", (uintmax_t)buf.f_bsize);
248 (void)printf("f_blocks=%ju\n", (uintmax_t)buf.f_blocks);
249 (void)printf("f_bfree=%ju\n", (uintmax_t)buf.f_bfree);
250 (void)printf("f_files=%ju\n", (uintmax_t)buf.f_files);
255 /* --------------------------------------------------------------------- */
258 main(int argc, char **argv)
268 if (strcmp(argv[0], "getfh") == 0)
269 error = getfh_main(argc, argv);
270 else if (strcmp(argv[0], "kqueue") == 0)
271 error = kqueue_main(argc, argv);
272 else if (strcmp(argv[0], "rename") == 0)
273 error = rename_main(argc, argv);
274 else if (strcmp(argv[0], "sockets") == 0)
275 error = sockets_main(argc, argv);
276 else if (strcmp(argv[0], "statvfs") == 0)
277 error = statvfs_main(argc, argv);
279 error = EXIT_FAILURE;