xref: /freebsd/tools/regression/tmpfs/h_tools.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
1 /*	$NetBSD: h_tools.c,v 1.7 2006/11/09 16:20:06 jmmv Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
10  *
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.
26  *
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.
38  *
39  * $FreeBSD$
40  */
41 
42 /*
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.
46  */
47 
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>
56 
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>
65 
66 /* --------------------------------------------------------------------- */
67 
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 **);
73 
74 /* --------------------------------------------------------------------- */
75 
76 int
77 getfh_main(int argc, char **argv)
78 {
79 	int error;
80 	fhandle_t fh;
81 
82 	if (argc < 2)
83 		return EXIT_FAILURE;
84 
85 	error = getfh(argv[1], &fh);
86 	if (error == 0)
87 		err(EXIT_FAILURE, "can not getfh");
88 
89 	error = write(STDOUT_FILENO, &fh, sizeof(fh));
90 	if (error == -1) {
91 		perror("write");
92 		return EXIT_FAILURE;
93 	}
94 
95 	return 0;
96 }
97 
98 /* --------------------------------------------------------------------- */
99 
100 int
101 kqueue_main(int argc, char **argv)
102 {
103 	char *line;
104 	int i, kq;
105 	size_t len;
106 	struct kevent *changes, event;
107 
108 	if (argc < 2)
109 		return EXIT_FAILURE;
110 
111 	argc--;
112 	argv++;
113 
114 	changes = malloc(sizeof(struct kevent) * (argc - 1));
115 	if (changes == NULL)
116 		errx(EXIT_FAILURE, "not enough memory");
117 
118 	for (i = 0; i < argc; i++) {
119 		int fd;
120 
121 		fd = open(argv[i], O_RDONLY);
122 		if (fd == -1)
123 			err(EXIT_FAILURE, "cannot open %s", argv[i]);
124 
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);
130 	}
131 
132 	kq = kqueue();
133 	if (kq == -1)
134 		err(EXIT_FAILURE, "kqueue");
135 
136 	while ((line = fgetln(stdin, &len)) != NULL) {
137 		int ec, nev;
138 		struct timespec to;
139 
140 		to.tv_sec = 0;
141 		to.tv_nsec = 100000;
142 
143 		(void)kevent(kq, changes, argc, &event, 1, &to);
144 
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);
151 
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;
160 
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]);
175 			}
176 		} while (nev > 0);
177 	}
178 
179 	for (i = 0; i < argc; i++)
180 		close(changes[i].ident);
181 	free(changes);
182 
183 	return EXIT_SUCCESS;
184 }
185 
186 /* --------------------------------------------------------------------- */
187 
188 int
189 rename_main(int argc, char **argv)
190 {
191 
192 	if (argc < 3)
193 		return EXIT_FAILURE;
194 
195 	return rename(argv[1], argv[2]);
196 }
197 
198 /* --------------------------------------------------------------------- */
199 
200 int
201 sockets_main(int argc, char **argv)
202 {
203 	int error, fd;
204 	struct sockaddr_un addr;
205 
206 	if (argc < 2)
207 		return EXIT_FAILURE;
208 
209 	fd = socket(PF_LOCAL, SOCK_STREAM, 0);
210 	if (fd == -1) {
211 		perror("socket");
212 		return EXIT_FAILURE;
213 	}
214 
215 	(void)strlcpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
216 	addr.sun_family = PF_UNIX;
217 
218 	error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
219 	if (error == -1) {
220 		perror("connect");
221 		return EXIT_FAILURE;
222 	}
223 
224 	close(fd);
225 
226 	return EXIT_SUCCESS;
227 }
228 
229 /* --------------------------------------------------------------------- */
230 
231 int
232 statvfs_main(int argc, char **argv)
233 {
234 	int error;
235 	struct statfs buf;
236 
237 	if (argc < 2)
238 		return EXIT_FAILURE;
239 
240 	memset(&buf, 0, sizeof(buf));
241 	buf.f_version = STATFS_VERSION;
242 	error = statfs(argv[1], &buf);
243 	if (error != 0) {
244 		perror("statvfs");
245 		return EXIT_FAILURE;
246 	}
247 
248 	(void)printf("f_bsize=%llu\n", buf.f_bsize);
249 	(void)printf("f_blocks=%llu\n", buf.f_blocks);
250 	(void)printf("f_bfree=%llu\n", buf.f_bfree);
251 	(void)printf("f_files=%llu\n", buf.f_files);
252 
253 	return EXIT_SUCCESS;
254 }
255 
256 /* --------------------------------------------------------------------- */
257 
258 int
259 main(int argc, char **argv)
260 {
261 	int error;
262 
263 	if (argc < 2)
264 		return EXIT_FAILURE;
265 
266 	argc -= 1;
267 	argv += 1;
268 
269 	if (strcmp(argv[0], "getfh") == 0)
270 		error = getfh_main(argc, argv);
271 	else if (strcmp(argv[0], "kqueue") == 0)
272 		error = kqueue_main(argc, argv);
273 	else if (strcmp(argv[0], "rename") == 0)
274 		error = rename_main(argc, argv);
275 	else if (strcmp(argv[0], "sockets") == 0)
276 		error = sockets_main(argc, argv);
277 	else if (strcmp(argv[0], "statvfs") == 0)
278 		error = statvfs_main(argc, argv);
279 	else
280 		error = EXIT_FAILURE;
281 
282 	return error;
283 }
284