Pass read/write CRx registers to userspace
[freebsd-src/fkvm-freebsd.git] / tools / regression / tmpfs / h_tools.c
blob1339060bc9a1311e3adc8c1d46d6f4c643685028
1 /* $NetBSD: h_tools.c,v 1.8 2007/07/23 15:05:43 jmmv Exp $ */
3 /*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
9 * 2005 program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
39 * $FreeBSD$
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
45 * given directory.
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>
54 #include <sys/time.h>
55 #include <sys/un.h>
57 #include <assert.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.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 /* --------------------------------------------------------------------- */
76 int
77 getfh_main(int argc, char **argv)
79 int error;
80 fhandle_t fh;
82 if (argc < 2)
83 return EXIT_FAILURE;
85 error = getfh(argv[1], &fh);
86 if (error == 0)
87 err(EXIT_FAILURE, "can not getfh");
89 error = write(STDOUT_FILENO, &fh, sizeof(fh));
90 if (error == -1) {
91 perror("write");
92 return EXIT_FAILURE;
95 return 0;
98 /* --------------------------------------------------------------------- */
101 kqueue_main(int argc, char **argv)
103 char *line;
104 int i, kq;
105 size_t len;
106 struct kevent *changes, event;
108 if (argc < 2)
109 return EXIT_FAILURE;
111 argc--;
112 argv++;
114 changes = malloc(sizeof(struct kevent) * (argc - 1));
115 if (changes == NULL)
116 errx(EXIT_FAILURE, "not enough memory");
118 for (i = 0; i < argc; i++) {
119 int fd;
121 fd = open(argv[i], O_RDONLY);
122 if (fd == -1)
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,
129 0, 0);
132 kq = kqueue();
133 if (kq == -1)
134 err(EXIT_FAILURE, "kqueue");
136 while ((line = fgetln(stdin, &len)) != NULL) {
137 int ec, nev;
138 struct timespec to;
140 to.tv_sec = 0;
141 to.tv_nsec = 100000;
143 (void)kevent(kq, changes, argc, &event, 1, &to);
145 assert(len > 0);
146 assert(line[len - 1] == '\n');
147 line[len - 1] = '\0';
148 ec = system(line);
149 if (ec != EXIT_SUCCESS)
150 errx(ec, "%s returned %d", line, ec);
152 do {
153 nev = kevent(kq, changes, argc, &event, 1, &to);
154 if (nev == -1)
155 err(EXIT_FAILURE, "kevent");
156 else if (nev > 0) {
157 for (i = 0; i < argc; i++)
158 if (event.ident == changes[i].ident)
159 break;
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]);
176 } while (nev > 0);
179 for (i = 0; i < argc; i++)
180 close(changes[i].ident);
181 free(changes);
183 return EXIT_SUCCESS;
186 /* --------------------------------------------------------------------- */
189 rename_main(int argc, char **argv)
192 if (argc < 3)
193 return EXIT_FAILURE;
195 if (rename(argv[1], argv[2]) == -1) {
196 perror("rename");
197 return EXIT_FAILURE;
200 return EXIT_SUCCESS;
203 /* --------------------------------------------------------------------- */
206 sockets_main(int argc, char **argv)
208 int error, fd;
209 struct sockaddr_un addr;
211 if (argc < 2)
212 return EXIT_FAILURE;
214 fd = socket(PF_LOCAL, SOCK_STREAM, 0);
215 if (fd == -1) {
216 perror("socket");
217 return EXIT_FAILURE;
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));
224 if (error == -1) {
225 perror("connect");
226 return EXIT_FAILURE;
229 close(fd);
231 return EXIT_SUCCESS;
234 /* --------------------------------------------------------------------- */
237 statvfs_main(int argc, char **argv)
239 int error;
240 struct statfs buf;
242 if (argc < 2)
243 return EXIT_FAILURE;
245 memset(&buf, 0, sizeof(buf));
246 buf.f_version = STATFS_VERSION;
247 error = statfs(argv[1], &buf);
248 if (error != 0) {
249 perror("statvfs");
250 return EXIT_FAILURE;
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);
258 return EXIT_SUCCESS;
261 /* --------------------------------------------------------------------- */
264 main(int argc, char **argv)
266 int error;
268 if (argc < 2)
269 return EXIT_FAILURE;
271 argc -= 1;
272 argv += 1;
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);
284 else
285 error = EXIT_FAILURE;
287 return error;