getdents02.c revision e2c8bdbfea9ecb895c2540af665d97390c8d8fdd
1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2001
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 *	getdents02.c
23 *
24 * DESCRIPTION
25 *	getdents02 - check that we get a failure with a bad file descriptor
26 *
27 * ALGORITHM
28 *	loop if that option was specified
29 *	issue the system call using a bad file descriptor
30 *	check the errno value
31 *	  issue a PASS message if we get EBADF - errno 9
32 *	otherwise, the tests fails
33 *	  issue a FAIL message
34 *	  break any remaining tests
35 *	  call cleanup
36 *
37 * USAGE:  <for command-line>
38 *  getdents02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39 *     where,  -c n : Run n copies concurrently.
40 *             -e   : Turn on errno logging.
41 *	       -i n : Execute test n times.
42 *	       -I x : Execute test for x seconds.
43 *	       -P x : Pause for x seconds between iterations.
44 *	       -t   : Turn on syscall timing.
45 *
46 * HISTORY
47 *	03/2001 - Written by Wayne Boyer
48 *
49 * RESTRICTIONS
50 *	none
51 */
52
53#include "getdents.h"
54#include "test.h"
55#include "usctest.h"
56
57#include <errno.h>
58#include <fcntl.h>
59#include <linux/types.h>
60#include <dirent.h>
61#include <linux/unistd.h>
62#include <unistd.h>
63
64void cleanup(void);
65void setup(void);
66
67char *TCID = "getdents02";
68int TST_TOTAL = 1;
69
70int exp_enos[] = { EBADF, 0 };	/* 0 terminated list of expected errnos */
71
72#ifndef __i386__
73int main(void)
74{
75
76	tst_brkm(TCONF, NULL, "this test will only run on i386");
77}
78#else
79
80int main(int ac, char **av)
81{
82	int lc;
83	char *msg;
84	int rval, fd;
85	int count;
86	const int cnum = __NR_getdents;
87	size_t size = 0;
88	char *dir_name = NULL;
89	struct dirent *dirp;
90
91	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
92		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
93
94	setup();
95
96	for (lc = 0; TEST_LOOPING(lc); lc++) {
97		Tst_count = 0;
98
99		if ((dir_name = getcwd(dir_name, size)) == NULL)
100			tst_brkm(TBROK, cleanup, "Can not get current "
101				 "directory name");
102
103		if ((dirp = malloc(sizeof(struct dirent))) == NULL)
104			tst_brkm(TBROK, cleanup, "malloc failed");
105
106		count = (int)sizeof(struct dirent);
107
108		/* set up a bad file descriptor */
109
110		fd = -5;
111
112		/*
113		 * here's a case where invoking the system call directly
114		 * doesn't seem to work.  getdents.h has an assembly
115		 * macro to do the job.
116		 *
117		 * equivalent to  - getdents(fd, dirp, count);
118		 * if we could call getdents that way.
119		 */
120
121		rval = GETDENTS_ASM();
122
123		/*
124		 * Hopefully we get an error due to the bad file descriptor.
125		 */
126		if (rval < 0) {
127			rval *= -1;
128			TEST_ERROR_LOG(rval);
129
130			switch (rval) {
131			case EBADF:
132				tst_resm(TPASS,
133				    "failed as expected with EBADF");
134				break;
135			default:
136				tst_resm(TFAIL|TERRNO,
137				    "getdents failed unexpectedly");
138				break;
139			}
140		} else
141			tst_resm(TFAIL, "call succeeded unexpectedly");
142
143		free(dir_name);
144		dir_name = NULL;
145
146		free(dirp);
147	}
148
149	cleanup();
150
151	tst_exit();
152}
153
154void setup(void)
155{
156
157	tst_sig(NOFORK, DEF_HANDLER, cleanup);
158
159	tst_tmpdir();
160
161	TEST_EXP_ENOS(exp_enos);
162
163	TEST_PAUSE;
164}
165
166void cleanup(void)
167{
168
169	TEST_CLEANUP;
170
171	tst_rmdir();
172}
173
174#endif /* __i386__ */
175