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