xref: /freebsd/tools/regression/tmpfs/h_tools.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1d73f7c17SXin LI /*	$NetBSD: h_tools.c,v 1.8 2007/07/23 15:05:43 jmmv Exp $	*/
2f8c94cecSXin LI 
3f8c94cecSXin LI /*-
4f8c94cecSXin LI  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5f8c94cecSXin LI  * All rights reserved.
6f8c94cecSXin LI  *
7f8c94cecSXin LI  * This code is derived from software contributed to The NetBSD Foundation
8f8c94cecSXin LI  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9f8c94cecSXin LI  * 2005 program.
10f8c94cecSXin LI  *
11f8c94cecSXin LI  * Redistribution and use in source and binary forms, with or without
12f8c94cecSXin LI  * modification, are permitted provided that the following conditions
13f8c94cecSXin LI  * are met:
14f8c94cecSXin LI  * 1. Redistributions of source code must retain the above copyright
15f8c94cecSXin LI  *    notice, this list of conditions and the following disclaimer.
16f8c94cecSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
17f8c94cecSXin LI  *    notice, this list of conditions and the following disclaimer in the
18f8c94cecSXin LI  *    documentation and/or other materials provided with the distribution.
19f8c94cecSXin LI  *
20f8c94cecSXin LI  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21f8c94cecSXin LI  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22f8c94cecSXin LI  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23f8c94cecSXin LI  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24f8c94cecSXin LI  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8c94cecSXin LI  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8c94cecSXin LI  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8c94cecSXin LI  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8c94cecSXin LI  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8c94cecSXin LI  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30f8c94cecSXin LI  * POSSIBILITY OF SUCH DAMAGE.
31f8c94cecSXin LI  */
32f8c94cecSXin LI 
33f8c94cecSXin LI /*
34f8c94cecSXin LI  * Helper tools for several tests.  These are kept in a single file due
35f8c94cecSXin LI  * to the limitations of bsd.prog.mk to build a single program in a
36f8c94cecSXin LI  * given directory.
37f8c94cecSXin LI  */
38f8c94cecSXin LI 
39f8c94cecSXin LI #include <sys/param.h>
40f8c94cecSXin LI #include <sys/types.h>
41f8c94cecSXin LI #include <sys/event.h>
42f8c94cecSXin LI #include <sys/mount.h>
43f8c94cecSXin LI #include <sys/statvfs.h>
44c27838c7SXin LI #include <sys/stdint.h>
45f8c94cecSXin LI #include <sys/socket.h>
46f8c94cecSXin LI #include <sys/time.h>
47f8c94cecSXin LI #include <sys/un.h>
48f8c94cecSXin LI 
49f8c94cecSXin LI #include <assert.h>
50f8c94cecSXin LI #include <err.h>
51f8c94cecSXin LI #include <errno.h>
52f8c94cecSXin LI #include <fcntl.h>
53f8c94cecSXin LI #include <stdio.h>
54f8c94cecSXin LI #include <stdlib.h>
55f8c94cecSXin LI #include <string.h>
56f8c94cecSXin LI #include <unistd.h>
57f8c94cecSXin LI 
58f8c94cecSXin LI /* --------------------------------------------------------------------- */
59f8c94cecSXin LI 
60f8c94cecSXin LI static int getfh_main(int, char **);
61f8c94cecSXin LI static int kqueue_main(int, char **);
62f8c94cecSXin LI static int rename_main(int, char **);
63f8c94cecSXin LI static int sockets_main(int, char **);
64f8c94cecSXin LI static int statvfs_main(int, char **);
65f8c94cecSXin LI 
66f8c94cecSXin LI /* --------------------------------------------------------------------- */
67f8c94cecSXin LI 
68f8c94cecSXin LI int
getfh_main(int argc,char ** argv)69f8c94cecSXin LI getfh_main(int argc, char **argv)
70f8c94cecSXin LI {
71f8c94cecSXin LI 	int error;
72f8c94cecSXin LI 	fhandle_t fh;
73f8c94cecSXin LI 
74f8c94cecSXin LI 	if (argc < 2)
75f8c94cecSXin LI 		return EXIT_FAILURE;
76f8c94cecSXin LI 
77f8c94cecSXin LI 	error = getfh(argv[1], &fh);
78f8c94cecSXin LI 	if (error == 0)
79f8c94cecSXin LI 		err(EXIT_FAILURE, "can not getfh");
80f8c94cecSXin LI 
81f8c94cecSXin LI 	error = write(STDOUT_FILENO, &fh, sizeof(fh));
82f8c94cecSXin LI 	if (error == -1) {
83f8c94cecSXin LI 		perror("write");
84f8c94cecSXin LI 		return EXIT_FAILURE;
85f8c94cecSXin LI 	}
86f8c94cecSXin LI 
87f8c94cecSXin LI 	return 0;
88f8c94cecSXin LI }
89f8c94cecSXin LI 
90f8c94cecSXin LI /* --------------------------------------------------------------------- */
91f8c94cecSXin LI 
92f8c94cecSXin LI int
kqueue_main(int argc,char ** argv)93f8c94cecSXin LI kqueue_main(int argc, char **argv)
94f8c94cecSXin LI {
95f8c94cecSXin LI 	char *line;
96f8c94cecSXin LI 	int i, kq;
97f8c94cecSXin LI 	size_t len;
98f8c94cecSXin LI 	struct kevent *changes, event;
99f8c94cecSXin LI 
100f8c94cecSXin LI 	if (argc < 2)
101f8c94cecSXin LI 		return EXIT_FAILURE;
102f8c94cecSXin LI 
103f8c94cecSXin LI 	argc--;
104f8c94cecSXin LI 	argv++;
105f8c94cecSXin LI 
106f8c94cecSXin LI 	changes = malloc(sizeof(struct kevent) * (argc - 1));
107f8c94cecSXin LI 	if (changes == NULL)
108f8c94cecSXin LI 		errx(EXIT_FAILURE, "not enough memory");
109f8c94cecSXin LI 
110f8c94cecSXin LI 	for (i = 0; i < argc; i++) {
111f8c94cecSXin LI 		int fd;
112f8c94cecSXin LI 
113f8c94cecSXin LI 		fd = open(argv[i], O_RDONLY);
114f8c94cecSXin LI 		if (fd == -1)
115f8c94cecSXin LI 			err(EXIT_FAILURE, "cannot open %s", argv[i]);
116f8c94cecSXin LI 
117f8c94cecSXin LI 		EV_SET(&changes[i], fd, EVFILT_VNODE,
118f8c94cecSXin LI 		    EV_ADD | EV_ENABLE | EV_ONESHOT,
119f8c94cecSXin LI 		    NOTE_ATTRIB | NOTE_DELETE | NOTE_EXTEND | NOTE_LINK |
120f8c94cecSXin LI 		    NOTE_RENAME | NOTE_REVOKE | NOTE_WRITE,
121f8c94cecSXin LI 		    0, 0);
122f8c94cecSXin LI 	}
123f8c94cecSXin LI 
124f8c94cecSXin LI 	kq = kqueue();
125f8c94cecSXin LI 	if (kq == -1)
126f8c94cecSXin LI 		err(EXIT_FAILURE, "kqueue");
127f8c94cecSXin LI 
128f8c94cecSXin LI 	while ((line = fgetln(stdin, &len)) != NULL) {
129f8c94cecSXin LI 		int ec, nev;
130f8c94cecSXin LI 		struct timespec to;
131f8c94cecSXin LI 
132f8c94cecSXin LI 		to.tv_sec = 0;
133f8c94cecSXin LI 		to.tv_nsec = 100000;
134f8c94cecSXin LI 
135f8c94cecSXin LI 		(void)kevent(kq, changes, argc, &event, 1, &to);
136f8c94cecSXin LI 
137f8c94cecSXin LI 		assert(len > 0);
138f8c94cecSXin LI 		assert(line[len - 1] == '\n');
139f8c94cecSXin LI 		line[len - 1] = '\0';
140f8c94cecSXin LI 		ec = system(line);
141f8c94cecSXin LI 		if (ec != EXIT_SUCCESS)
142f8c94cecSXin LI 			errx(ec, "%s returned %d", line, ec);
143f8c94cecSXin LI 
144f8c94cecSXin LI 		do {
145f8c94cecSXin LI 			nev = kevent(kq, changes, argc, &event, 1, &to);
146f8c94cecSXin LI 			if (nev == -1)
147f8c94cecSXin LI 				err(EXIT_FAILURE, "kevent");
148f8c94cecSXin LI 			else if (nev > 0) {
149f8c94cecSXin LI 				for (i = 0; i < argc; i++)
150f8c94cecSXin LI 					if (event.ident == changes[i].ident)
151f8c94cecSXin LI 						break;
152f8c94cecSXin LI 
153f8c94cecSXin LI 				if (event.fflags & NOTE_ATTRIB)
154f8c94cecSXin LI 					printf("%s - NOTE_ATTRIB\n", argv[i]);
155f8c94cecSXin LI 				if (event.fflags & NOTE_DELETE)
156f8c94cecSXin LI 					printf("%s - NOTE_DELETE\n", argv[i]);
157f8c94cecSXin LI 				if (event.fflags & NOTE_EXTEND)
158f8c94cecSXin LI 					printf("%s - NOTE_EXTEND\n", argv[i]);
159f8c94cecSXin LI 				if (event.fflags & NOTE_LINK)
160f8c94cecSXin LI 					printf("%s - NOTE_LINK\n", argv[i]);
161f8c94cecSXin LI 				if (event.fflags & NOTE_RENAME)
162f8c94cecSXin LI 					printf("%s - NOTE_RENAME\n", argv[i]);
163f8c94cecSXin LI 				if (event.fflags & NOTE_REVOKE)
164f8c94cecSXin LI 					printf("%s - NOTE_REVOKE\n", argv[i]);
165f8c94cecSXin LI 				if (event.fflags & NOTE_WRITE)
166f8c94cecSXin LI 					printf("%s - NOTE_WRITE\n", argv[i]);
167f8c94cecSXin LI 			}
168f8c94cecSXin LI 		} while (nev > 0);
169f8c94cecSXin LI 	}
170f8c94cecSXin LI 
171f8c94cecSXin LI 	for (i = 0; i < argc; i++)
172f8c94cecSXin LI 		close(changes[i].ident);
173f8c94cecSXin LI 	free(changes);
174f8c94cecSXin LI 
175f8c94cecSXin LI 	return EXIT_SUCCESS;
176f8c94cecSXin LI }
177f8c94cecSXin LI 
178f8c94cecSXin LI /* --------------------------------------------------------------------- */
179f8c94cecSXin LI 
180f8c94cecSXin LI int
rename_main(int argc,char ** argv)181f8c94cecSXin LI rename_main(int argc, char **argv)
182f8c94cecSXin LI {
183f8c94cecSXin LI 
184f8c94cecSXin LI 	if (argc < 3)
185f8c94cecSXin LI 		return EXIT_FAILURE;
186f8c94cecSXin LI 
187d73f7c17SXin LI 	if (rename(argv[1], argv[2]) == -1) {
188d73f7c17SXin LI 		perror("rename");
189d73f7c17SXin LI 		return EXIT_FAILURE;
190d73f7c17SXin LI 	}
191d73f7c17SXin LI 
192d73f7c17SXin LI 	return EXIT_SUCCESS;
193f8c94cecSXin LI }
194f8c94cecSXin LI 
195f8c94cecSXin LI /* --------------------------------------------------------------------- */
196f8c94cecSXin LI 
197f8c94cecSXin LI int
sockets_main(int argc,char ** argv)198f8c94cecSXin LI sockets_main(int argc, char **argv)
199f8c94cecSXin LI {
200f8c94cecSXin LI 	int error, fd;
201f8c94cecSXin LI 	struct sockaddr_un addr;
202f8c94cecSXin LI 
203f8c94cecSXin LI 	if (argc < 2)
204f8c94cecSXin LI 		return EXIT_FAILURE;
205f8c94cecSXin LI 
206f8c94cecSXin LI 	fd = socket(PF_LOCAL, SOCK_STREAM, 0);
207f8c94cecSXin LI 	if (fd == -1) {
208f8c94cecSXin LI 		perror("socket");
209f8c94cecSXin LI 		return EXIT_FAILURE;
210f8c94cecSXin LI 	}
211f8c94cecSXin LI 
212f8c94cecSXin LI 	(void)strlcpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
213f8c94cecSXin LI 	addr.sun_family = PF_UNIX;
214f8c94cecSXin LI 
215f8c94cecSXin LI 	error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
216f8c94cecSXin LI 	if (error == -1) {
217f8c94cecSXin LI 		perror("connect");
218f8c94cecSXin LI 		return EXIT_FAILURE;
219f8c94cecSXin LI 	}
220f8c94cecSXin LI 
221f8c94cecSXin LI 	close(fd);
222f8c94cecSXin LI 
223f8c94cecSXin LI 	return EXIT_SUCCESS;
224f8c94cecSXin LI }
225f8c94cecSXin LI 
226f8c94cecSXin LI /* --------------------------------------------------------------------- */
227f8c94cecSXin LI 
228f8c94cecSXin LI int
statvfs_main(int argc,char ** argv)229f8c94cecSXin LI statvfs_main(int argc, char **argv)
230f8c94cecSXin LI {
231f8c94cecSXin LI 	int error;
232f8c94cecSXin LI 	struct statfs buf;
233f8c94cecSXin LI 
234f8c94cecSXin LI 	if (argc < 2)
235f8c94cecSXin LI 		return EXIT_FAILURE;
236f8c94cecSXin LI 
237f8c94cecSXin LI 	memset(&buf, 0, sizeof(buf));
238f8c94cecSXin LI 	buf.f_version = STATFS_VERSION;
239f8c94cecSXin LI 	error = statfs(argv[1], &buf);
240f8c94cecSXin LI 	if (error != 0) {
241f8c94cecSXin LI 		perror("statvfs");
242f8c94cecSXin LI 		return EXIT_FAILURE;
243f8c94cecSXin LI 	}
244f8c94cecSXin LI 
245c27838c7SXin LI 	(void)printf("f_bsize=%ju\n", (uintmax_t)buf.f_bsize);
246c27838c7SXin LI 	(void)printf("f_blocks=%ju\n", (uintmax_t)buf.f_blocks);
247c27838c7SXin LI 	(void)printf("f_bfree=%ju\n", (uintmax_t)buf.f_bfree);
248c27838c7SXin LI 	(void)printf("f_files=%ju\n", (uintmax_t)buf.f_files);
249f8c94cecSXin LI 
250f8c94cecSXin LI 	return EXIT_SUCCESS;
251f8c94cecSXin LI }
252f8c94cecSXin LI 
253f8c94cecSXin LI /* --------------------------------------------------------------------- */
254f8c94cecSXin LI 
255f8c94cecSXin LI int
main(int argc,char ** argv)256f8c94cecSXin LI main(int argc, char **argv)
257f8c94cecSXin LI {
258f8c94cecSXin LI 	int error;
259f8c94cecSXin LI 
260f8c94cecSXin LI 	if (argc < 2)
261f8c94cecSXin LI 		return EXIT_FAILURE;
262f8c94cecSXin LI 
263f8c94cecSXin LI 	argc -= 1;
264f8c94cecSXin LI 	argv += 1;
265f8c94cecSXin LI 
266f8c94cecSXin LI 	if (strcmp(argv[0], "getfh") == 0)
267f8c94cecSXin LI 		error = getfh_main(argc, argv);
268f8c94cecSXin LI 	else if (strcmp(argv[0], "kqueue") == 0)
269f8c94cecSXin LI 		error = kqueue_main(argc, argv);
270f8c94cecSXin LI 	else if (strcmp(argv[0], "rename") == 0)
271f8c94cecSXin LI 		error = rename_main(argc, argv);
272f8c94cecSXin LI 	else if (strcmp(argv[0], "sockets") == 0)
273f8c94cecSXin LI 		error = sockets_main(argc, argv);
274f8c94cecSXin LI 	else if (strcmp(argv[0], "statvfs") == 0)
275f8c94cecSXin LI 		error = statvfs_main(argc, argv);
276f8c94cecSXin LI 	else
277f8c94cecSXin LI 		error = EXIT_FAILURE;
278f8c94cecSXin LI 
279f8c94cecSXin LI 	return error;
280f8c94cecSXin LI }
281