xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_dir.c (revision cc349066556bcdeed0d6cc72aad340d0f383e35c)
1 /* $NetBSD: t_dir.c,v 1.6 2013/10/19 17:45:00 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28 
29 #include <atf-c.h>
30 
31 #include <assert.h>
32 #include <dirent.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <sys/stat.h>
41 
42 #ifdef	__FreeBSD__
43 #include <errno.h>
44 #endif
45 
46 ATF_TC(seekdir_basic);
47 ATF_TC_HEAD(seekdir_basic, tc)
48 {
49 
50 	atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) "
51 	    "for correct behavior (PR lib/24324)");
52 }
53 
54 ATF_TC_BODY(seekdir_basic, tc)
55 {
56 	DIR *dp;
57 	char *wasname;
58 	struct dirent *entry;
59 	long here;
60 
61 #ifdef	__FreeBSD__
62 #define	CREAT(x, m)	do {					\
63 		int _creat_fd;					\
64 		ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))),	\
65 		    "creat(%s, %x) failed: %s", (x), (m),	\
66 		    strerror(errno));				\
67 		(void)close(_creat_fd);			\
68 	} while(0);
69 
70 	ATF_REQUIRE_MSG(mkdir("t", 0755) == 0,
71 	    "mkdir failed: %s", strerror(errno));
72 	CREAT("t/a", 0600);
73 	CREAT("t/b", 0600);
74 	CREAT("t/c", 0600);
75 #else
76 	mkdir("t", 0755);
77 	creat("t/a", 0600);
78 	creat("t/b", 0600);
79 	creat("t/c", 0600);
80 #endif
81 
82 	dp = opendir("t");
83 	if ( dp == NULL)
84 		atf_tc_fail("Could not open temp directory.");
85 
86 	/* skip two for . and .. */
87 	entry = readdir(dp);
88 	entry = readdir(dp);
89 
90 	/* get first entry */
91 	entry = readdir(dp);
92 	here = telldir(dp);
93 #ifdef	__FreeBSD__
94 	ATF_REQUIRE_MSG(here != -1,
95 	    "telldir failed: %s", strerror(errno));
96 #endif
97 
98 	/* get second entry */
99 	entry = readdir(dp);
100 #ifdef	__FreeBSD__
101 	ATF_REQUIRE_MSG(entry != NULL,
102 	    "readdir failed: %s", strerror(errno));
103 #endif
104 	wasname = strdup(entry->d_name);
105 	if (wasname == NULL)
106 		atf_tc_fail("cannot allocate memory");
107 
108 	/* get third entry */
109 	entry = readdir(dp);
110 
111 	/* try to return to the position after the first entry */
112 	seekdir(dp, here);
113 	entry = readdir(dp);
114 
115 	if (entry == NULL)
116 		atf_tc_fail("entry 1 not found");
117 	if (strcmp(entry->d_name, wasname) != 0)
118 		atf_tc_fail("1st seekdir found wrong name");
119 
120 	/* try again, and throw in a telldir() for good measure */
121 	seekdir(dp, here);
122 	here = telldir(dp);
123 	entry = readdir(dp);
124 
125 	if (entry == NULL)
126 		atf_tc_fail("entry 2 not found");
127 	if (strcmp(entry->d_name, wasname) != 0)
128 		atf_tc_fail("2nd seekdir found wrong name");
129 
130 	/* One more time, to make sure that telldir() doesn't affect result */
131 	seekdir(dp, here);
132 	entry = readdir(dp);
133 
134 	if (entry == NULL)
135 		atf_tc_fail("entry 3 not found");
136 	if (strcmp(entry->d_name, wasname) != 0)
137 		atf_tc_fail("3rd seekdir found wrong name");
138 
139 	closedir(dp);
140 #ifdef	__FreeBSD__
141 	free(wasname);
142 #endif
143 }
144 
145 /* There is no sbrk on AArch64 and RISC-V */
146 #if !defined(__aarch64__) && !defined(__riscv__)
147 ATF_TC(telldir_leak);
148 ATF_TC_HEAD(telldir_leak, tc)
149 {
150 
151 	atf_tc_set_md_var(tc, "descr",
152 	    "Check telldir(3) for memory leakage (PR lib/24324)");
153 }
154 
155 ATF_TC_BODY(telldir_leak, tc)
156 {
157 	DIR *dp;
158 	char *memused;
159 	int i;
160 	int oktouse = 4096;
161 
162 	dp = opendir(".");
163 	if (dp == NULL)
164 		atf_tc_fail("Could not open current directory");
165 
166 	(void)telldir(dp);
167 	memused = sbrk(0);
168 	closedir(dp);
169 
170 	for (i = 0; i < 1000; i++) {
171 		dp = opendir(".");
172 		if (dp == NULL)
173 			atf_tc_fail("Could not open current directory");
174 
175 		(void)telldir(dp);
176 		closedir(dp);
177 
178 		if ((char *)sbrk(0) - memused > oktouse) {
179 			(void)printf("Used %td extra bytes for %d telldir "
180 			    "calls", ((char *)sbrk(0) - memused), i);
181 			oktouse = (char *)sbrk(0) - memused;
182 		}
183 	}
184 	if (oktouse > 4096) {
185 		atf_tc_fail("Failure: leaked %d bytes", oktouse);
186 	} else {
187 		(void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
188 	}
189 }
190 #endif
191 
192 ATF_TP_ADD_TCS(tp)
193 {
194 
195 	ATF_TP_ADD_TC(tp, seekdir_basic);
196 #if !defined(__aarch64__) && !defined(__riscv__)
197 	ATF_TP_ADD_TC(tp, telldir_leak);
198 #endif
199 
200 	return atf_no_error();
201 }
202