blob: 7bce1d380035d08a600b6dc3e50a05d1f446c456 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
subrata_modak56207ce2009-03-23 13:35:39 +000033/* $Id: readdir01.c,v 1.7 2009/03/23 13:36:01 subrata_modak Exp $ */
plars865695b2001-08-27 22:15:12 +000034/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
plars865695b2001-08-27 22:15:12 +000036 * OS Test - Silicon Graphics, Inc.
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
plars865695b2001-08-27 22:15:12 +000038 * TEST IDENTIFIER : readdir01
subrata_modak4bb656a2009-02-26 12:02:09 +000039 *
plars865695b2001-08-27 22:15:12 +000040 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
plars865695b2001-08-27 22:15:12 +000042 * TEST TITLE : write multiple files and try to find them with readdir
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
plars865695b2001-08-27 22:15:12 +000044 * TEST CASE TOTAL :
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
plars865695b2001-08-27 22:15:12 +000046 * WALL CLOCK TIME :
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
plars865695b2001-08-27 22:15:12 +000048 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
plars865695b2001-08-27 22:15:12 +000050 * AUTHOR : Nate Straz
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
plars865695b2001-08-27 22:15:12 +000052 * CO-PILOT :
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
plars865695b2001-08-27 22:15:12 +000054 * DATE STARTED : 02/16/2001
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
plars865695b2001-08-27 22:15:12 +000056 * INITIAL RELEASE : Linux 2.4.x
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
plars865695b2001-08-27 22:15:12 +000058 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
plars865695b2001-08-27 22:15:12 +000060 * 1.) Create n files and check that readdir() finds n files
subrata_modakbdbaec52009-02-26 12:14:51 +000061 *
plars865695b2001-08-27 22:15:12 +000062 * INPUT SPECIFICATIONS
63 * The standard options for system call tests are accepted.
64 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000065 *
plars865695b2001-08-27 22:15:12 +000066 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000067 *$
plars865695b2001-08-27 22:15:12 +000068 * DURATION
69 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000070 *
plars865695b2001-08-27 22:15:12 +000071 * SIGNALS
72 * Uses SIGUSR1 to pause before test if option set.
73 * (See the parse_opts(3) man page).
74 *
75 * RESOURCES
76 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000077 *
plars865695b2001-08-27 22:15:12 +000078 * ENVIRONMENTAL NEEDS
79 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000080 *
plars865695b2001-08-27 22:15:12 +000081 * SPECIAL PROCEDURAL REQUIREMENTS
82 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000083 *
plars865695b2001-08-27 22:15:12 +000084 * INTERCASE DEPENDENCIES
85 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000086 *
plars865695b2001-08-27 22:15:12 +000087 * DETAILED DESCRIPTION
88 * This is a Phase I test for the readdir(2) system call. It is intended
89 * to provide a limited exposure of the system call, for now. It
90 * should/will be extended when full functional tests are written for
91 * readdir(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000092 *
plars865695b2001-08-27 22:15:12 +000093 * Setup:
94 * Setup signal handling.
95 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000096 *
plars865695b2001-08-27 22:15:12 +000097 * Test:
98 * Loop if the proper options are given.
99 * Execute system call
100 * Check return code, if system call failed (return=-1)
101 * Log the errno and Issue a FAIL message.
102 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +0000103 *
plars865695b2001-08-27 22:15:12 +0000104 * Cleanup:
105 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +0000106 *
107 *
plars865695b2001-08-27 22:15:12 +0000108 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
109
110#include <sys/types.h>
111#include <sys/stat.h>
112#include <fcntl.h>
113#include <dirent.h>
114#include <unistd.h>
115#include <errno.h>
116#include <string.h>
117#include <signal.h>
plars865695b2001-08-27 22:15:12 +0000118#include "test.h"
plars865695b2001-08-27 22:15:12 +0000119
120 /* The setup and cleanup functions are basic parts of a test case. These
121 * steps are usually put in separate functions for clarity. The help function
122 * is only needed when you are adding new command line options.
123 */
subrata_modak4bb656a2009-02-26 12:02:09 +0000124void setup();
plars865695b2001-08-27 22:15:12 +0000125void help();
126void cleanup();
127
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200128char *TCID = "readdir01";
129int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +0000130
plars865695b2001-08-27 22:15:12 +0000131#define BASENAME "readdirfile"
132
133char Basename[255];
134char Fname[255];
subrata_modak56207ce2009-03-23 13:35:39 +0000135int Nfiles = 0;
plars865695b2001-08-27 22:15:12 +0000136
137/* To add command line options you need to declare a structure to pass to
138 * parse_opts(). options is the structure used in this example. The format is
139 * the string that should be added to optstring in getopt(3), an integer that
140 * will be used as a flag if the option is given, and a pointer to a string that
141 * should receive the optarg parameter from getopt(3). Here we add a -N
subrata_modak4bb656a2009-02-26 12:02:09 +0000142 * option. Long options are not supported at this time.
plars865695b2001-08-27 22:15:12 +0000143 */
144char *Nfilearg;
subrata_modak56207ce2009-03-23 13:35:39 +0000145int Nflag = 0;
plars865695b2001-08-27 22:15:12 +0000146
147/* for test specific parse_opts options */
148option_t options[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000149 {"N:", &Nflag, &Nfilearg}, /* -N #files */
150 {NULL, NULL, NULL}
plars865695b2001-08-27 22:15:12 +0000151};
152
153/***********************************************************************
154 * Main
155 ***********************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000156int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000157{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200158 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200159 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000160 int cnt;
161 int nfiles, fd;
162 char fname[255];
163 DIR *test_dir;
164 struct dirent *dptr;
plars865695b2001-08-27 22:15:12 +0000165
plars865695b2001-08-27 22:15:12 +0000166 /***************************************************************
167 * parse standard options
168 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000169 /* start off by parsing the command line options. We provide a function
170 * that understands many common options to control looping. If you are not
171 * adding any new options, pass NULL in place of options and &help.
172 */
173 if ((msg = parse_opts(ac, av, options, &help)) != 0) {
174 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000175 }
subrata_modak56207ce2009-03-23 13:35:39 +0000176
177 if (Nflag) {
178 if (sscanf(Nfilearg, "%i", &Nfiles) != 1) {
179 tst_brkm(TBROK, NULL, "--N option arg is not a number");
subrata_modak56207ce2009-03-23 13:35:39 +0000180 }
181 }
plars865695b2001-08-27 22:15:12 +0000182
183 /***************************************************************
184 * perform global setup for test
185 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000186 /* Next you should run a setup routine to make sure your environment is
187 * sane.
188 */
189 setup();
plars865695b2001-08-27 22:15:12 +0000190
plars865695b2001-08-27 22:15:12 +0000191 /***************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +0000192 * check looping state
plars865695b2001-08-27 22:15:12 +0000193 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000194 /* TEST_LOOPING() is a macro that will make sure the test continues
195 * looping according to the standard command line args.
plars865695b2001-08-27 22:15:12 +0000196 */
subrata_modak56207ce2009-03-23 13:35:39 +0000197 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000198
Caspar Zhangd59a6592013-03-07 14:59:12 +0800199 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000200
subrata_modak56207ce2009-03-23 13:35:39 +0000201 if (Nfiles)
202 nfiles = Nfiles;
203 else
204 /* min of 10 links and max of a 100 links */
205 nfiles = (lc % 90) + 10;
206
207 /* create a bunch of files to look at */
208 for (cnt = 0; cnt < nfiles; cnt++) {
209
210 sprintf(fname, "%s%d", Basename, cnt);
211 if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) {
212 tst_brkm(TBROK, cleanup,
213 "open(%s, O_RDWR|O_CREAT,0700) Failed, errno=%d : %s",
214 fname, errno, strerror(errno));
215 } else if (write(fd, "hello\n", 6) < 0) {
216 tst_brkm(TBROK, cleanup,
217 "write(%s, \"hello\\n\", 6) Failed, errno=%d : %s",
218 fname, errno, strerror(errno));
219 } else if (close(fd) < 0) {
220 tst_res(TWARN,
221 "close(%s) Failed, errno=%d : %s",
222 fname, errno, strerror(errno));
223 }
224 }
225
226 if ((test_dir = opendir(".")) == NULL) {
227 tst_resm(TFAIL, "opendir(\".\") Failed, errno=%d : %s",
228 errno, strerror(errno));
229 } else {
230 /* count the entries we find to see if any are missing */
231 cnt = 0;
232 errno = 0;
233 while ((dptr = readdir(test_dir)) != 0) {
234 if (strcmp(dptr->d_name, ".")
235 && strcmp(dptr->d_name, ".."))
236 cnt++;
237 }
238
239 if (errno != 0) {
240 tst_resm(TFAIL,
241 "readir(test_dir) Failed on try %d, errno=%d : %s",
242 cnt + 1, errno, strerror(errno));
243 }
244 if (cnt == nfiles) {
245 tst_resm(TPASS,
246 "found all %d that were created",
247 nfiles);
248 } else if (cnt > nfiles) {
249 tst_resm(TFAIL,
250 "found more files than were created");
251 tst_resm(TINFO, "created: %d, found: %d",
252 nfiles, cnt);
253 } else {
254 tst_resm(TFAIL,
255 "found less files than were created");
256 tst_resm(TINFO, "created: %d, found: %d",
257 nfiles, cnt);
258 }
259 }
260
261 /* Here we clean up after the test case so we can do another iteration.
262 */
263 for (cnt = 0; cnt < nfiles; cnt++) {
264
265 sprintf(fname, "%s%d", Basename, cnt);
266
267 if (unlink(fname) == -1) {
268 tst_res(TWARN,
269 "unlink(%s) Failed, errno=%d : %s",
270 Fname, errno, strerror(errno));
271 }
272 }
273
Garrett Cooper2c282152010-12-16 00:55:50 -0800274 }
plars865695b2001-08-27 22:15:12 +0000275
276 /***************************************************************
277 * cleanup and exit
278 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000279 cleanup();
plars865695b2001-08-27 22:15:12 +0000280
Garrett Cooper2c282152010-12-16 00:55:50 -0800281 tst_exit();
282}
plars865695b2001-08-27 22:15:12 +0000283
284/***************************************************************
285 * help
286 ***************************************************************/
287/* The custom help() function is really simple. Just write your help message to
288 * standard out. Your help function will be called after the standard options
289 * have been printed
290 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400291void help(void)
plars865695b2001-08-27 22:15:12 +0000292{
subrata_modak56207ce2009-03-23 13:35:39 +0000293 printf(" -N #files : create #files files every iteration\n");
plars865695b2001-08-27 22:15:12 +0000294}
295
296/***************************************************************
297 * setup() - performs all ONE TIME setup for this test.
298 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400299void setup(void)
plars865695b2001-08-27 22:15:12 +0000300{
subrata_modak56207ce2009-03-23 13:35:39 +0000301 /* You will want to enable some signal handling so you can capture
302 * unexpected signals like SIGSEGV.
303 */
304 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000305
subrata_modak56207ce2009-03-23 13:35:39 +0000306 /* One cavet that hasn't been fixed yet. TEST_PAUSE contains the code to
307 * fork the test with the -c option. You want to make sure you do this
308 * before you create your temporary directory.
309 */
310 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000311
subrata_modak56207ce2009-03-23 13:35:39 +0000312 /* If you are doing any file work, you should use a temporary directory. We
313 * provide tst_tmpdir() which will create a uniquely named temporary
314 * directory and cd into it. You can now create files in the current
315 * directory without worrying.
316 */
317 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000318
subrata_modak56207ce2009-03-23 13:35:39 +0000319 sprintf(Basename, "%s_%d.", BASENAME, getpid());
plars865695b2001-08-27 22:15:12 +0000320}
321
322/***************************************************************
323 * cleanup() - performs all ONE TIME cleanup for this test at
324 * completion or premature exit.
325 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400326void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000327{
plars865695b2001-08-27 22:15:12 +0000328
subrata_modak56207ce2009-03-23 13:35:39 +0000329 /* If you use a temporary directory, you need to be sure you remove it. Use
330 * tst_rmdir() to do it automatically.$
331 */
332 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000333
Chris Dearmanec6edca2012-10-17 19:54:01 -0700334}