1 /* $NetBSD: h_tools.c,v 1.1 2007/11/12 15:18:21 jmmv Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Helper tools for several tests. These are kept in a single file due
31 * to the limitations of bsd.prog.mk to build a single program in a
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/event.h>
38 #include <sys/mount.h>
39 #include <sys/statvfs.h>
40 #include <sys/socket.h>
53 /* --------------------------------------------------------------------- */
55 static int getfh_main(int, char **);
56 static int kqueue_main(int, char **);
57 static int rename_main(int, char **);
58 static int sockets_main(int, char **);
59 static int statvfs_main(int, char **);
61 /* --------------------------------------------------------------------- */
64 getfh_main(int argc
, char **argv
)
79 fprintf(stderr
, "out of memory");
84 * The kernel provides the necessary size in fh_size -
85 * but it may change if someone moves things around,
86 * so retry untill we have enough memory.
88 error
= getfh(argv
[1], fh
, &fh_size
);
101 error
= write(STDOUT_FILENO
, fh
, fh_size
);
111 /* --------------------------------------------------------------------- */
114 kqueue_main(int argc
, char **argv
)
119 struct kevent
*changes
, event
;
127 changes
= malloc(sizeof(struct kevent
) * (argc
- 1));
129 errx(EXIT_FAILURE
, "not enough memory");
131 for (i
= 0; i
< argc
; i
++) {
134 fd
= open(argv
[i
], O_RDONLY
);
136 err(EXIT_FAILURE
, "cannot open %s", argv
[i
]);
138 EV_SET(&changes
[i
], fd
, EVFILT_VNODE
,
139 EV_ADD
| EV_ENABLE
| EV_ONESHOT
,
140 NOTE_ATTRIB
| NOTE_DELETE
| NOTE_EXTEND
| NOTE_LINK
|
141 NOTE_RENAME
| NOTE_REVOKE
| NOTE_WRITE
,
147 err(EXIT_FAILURE
, "kqueue");
149 while ((line
= fgetln(stdin
, &len
)) != NULL
) {
156 (void)kevent(kq
, changes
, argc
, &event
, 1, &to
);
159 assert(line
[len
- 1] == '\n');
160 line
[len
- 1] = '\0';
162 if (ec
!= EXIT_SUCCESS
)
163 errx(ec
, "%s returned %d", line
, ec
);
166 nev
= kevent(kq
, changes
, argc
, &event
, 1, &to
);
168 err(EXIT_FAILURE
, "kevent");
170 for (i
= 0; i
< argc
; i
++)
171 if (event
.ident
== changes
[i
].ident
)
174 if (event
.fflags
& NOTE_ATTRIB
)
175 printf("%s - NOTE_ATTRIB\n", argv
[i
]);
176 if (event
.fflags
& NOTE_DELETE
)
177 printf("%s - NOTE_DELETE\n", argv
[i
]);
178 if (event
.fflags
& NOTE_EXTEND
)
179 printf("%s - NOTE_EXTEND\n", argv
[i
]);
180 if (event
.fflags
& NOTE_LINK
)
181 printf("%s - NOTE_LINK\n", argv
[i
]);
182 if (event
.fflags
& NOTE_RENAME
)
183 printf("%s - NOTE_RENAME\n", argv
[i
]);
184 if (event
.fflags
& NOTE_REVOKE
)
185 printf("%s - NOTE_REVOKE\n", argv
[i
]);
186 if (event
.fflags
& NOTE_WRITE
)
187 printf("%s - NOTE_WRITE\n", argv
[i
]);
192 for (i
= 0; i
< argc
; i
++)
193 close(changes
[i
].ident
);
199 /* --------------------------------------------------------------------- */
202 rename_main(int argc
, char **argv
)
208 if (rename(argv
[1], argv
[2]) == -1) {
216 /* --------------------------------------------------------------------- */
219 sockets_main(int argc
, char **argv
)
222 struct sockaddr_un addr
;
227 fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
233 (void)strlcpy(addr
.sun_path
, argv
[1], sizeof(addr
.sun_path
));
234 addr
.sun_family
= PF_UNIX
;
236 error
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
247 /* --------------------------------------------------------------------- */
250 statvfs_main(int argc
, char **argv
)
258 error
= statvfs(argv
[1], &buf
);
264 (void)printf("f_bsize=%lu\n", buf
.f_bsize
);
265 (void)printf("f_blocks=%" PRId64
"\n", buf
.f_blocks
);
266 (void)printf("f_bfree=%" PRId64
"\n", buf
.f_bfree
);
267 (void)printf("f_files=%" PRId64
"\n", buf
.f_files
);
272 /* --------------------------------------------------------------------- */
275 main(int argc
, char **argv
)
285 if (strcmp(argv
[0], "getfh") == 0)
286 error
= getfh_main(argc
, argv
);
287 else if (strcmp(argv
[0], "kqueue") == 0)
288 error
= kqueue_main(argc
, argv
);
289 else if (strcmp(argv
[0], "rename") == 0)
290 error
= rename_main(argc
, argv
);
291 else if (strcmp(argv
[0], "sockets") == 0)
292 error
= sockets_main(argc
, argv
);
293 else if (strcmp(argv
[0], "statvfs") == 0)
294 error
= statvfs_main(argc
, argv
);
296 error
= EXIT_FAILURE
;