blob: a4cba9c21a6a8e9e4bffa496c608d11651cfbe2c [file] [log] [blame]
robbiewb95e3622002-05-03 15:43:15 +00001/*
Cyril Hrubis1bbfae92013-06-04 18:55:10 +02002 * Copyright (C) Bull S.A. 2001
3 * Copyright (c) International Business Machines Corp., 2001
robbiewb95e3622002-05-03 15:43:15 +00004 *
Cyril Hrubis1bbfae92013-06-04 18:55:10 +02005 * 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.
robbiewb95e3622002-05-03 15:43:15 +00009 *
Cyril Hrubis1bbfae92013-06-04 18:55:10 +020010 * 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.
robbiewb95e3622002-05-03 15:43:15 +000014 *
Cyril Hrubis1bbfae92013-06-04 18:55:10 +020015 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewb95e3622002-05-03 15:43:15 +000018 */
19
20/*
robbiewb95e3622002-05-03 15:43:15 +000021 * DESCRIPTION
Cyril Hrubis1bbfae92013-06-04 18:55:10 +020022 * Try to readdir with Invalid directory stream descriptor dir.
robbiewb95e3622002-05-03 15:43:15 +000023 *
24 * ALGORITHM
25 * loop if that option was specified
26 * call readdir() with an invalid file descriptor
27 * check the errno value
28 * issue a PASS message if we get EBADF - errno 9
29 * otherwise, the tests fails
30 * issue a FAIL message
31 * call cleanup
32 *
robbiewb95e3622002-05-03 15:43:15 +000033 * NOTE
34 * The POSIX standard says:
35 * The readdir() function may fail if:
subrata_modakbdbaec52009-02-26 12:14:51 +000036 * [EBADF] The dirp argument does not refer to an open directory stream.
robbiewb95e3622002-05-03 15:43:15 +000037 * (Note that readdir() is not _required_ to fail in this case.)
38 *
39 * HISTORY
40 * 04/2002 - Written by Jacky Malcles
41 *
robbiew88873012003-06-25 22:35:02 +000042 * 06/2003 - Added code to catch SIGSEGV and return TCONF.
43 * Robbie Williamson<robbiew@us.ibm.com>
robbiewb95e3622002-05-03 15:43:15 +000044 */
45
robbiewb95e3622002-05-03 15:43:15 +000046#include <sys/types.h>
47#include <sys/stat.h>
48#include <fcntl.h>
49#include <dirent.h>
50#include <unistd.h>
51#include <errno.h>
52#include <string.h>
53#include <signal.h>
Cyril Hrubis1bbfae92013-06-04 18:55:10 +020054
robbiewb95e3622002-05-03 15:43:15 +000055#include "test.h"
robbiewb95e3622002-05-03 15:43:15 +000056
Cyril Hrubis1bbfae92013-06-04 18:55:10 +020057static void setup(void);
58static void cleanup(void);
robbiewb95e3622002-05-03 15:43:15 +000059
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020060char *TCID = "readdir02";
61int TST_TOTAL = 1;
robbiewb95e3622002-05-03 15:43:15 +000062
subrata_modak56207ce2009-03-23 13:35:39 +000063int main(int ac, char **av)
robbiewb95e3622002-05-03 15:43:15 +000064{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020065 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020066 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000067 DIR *test_dir;
68 struct dirent *dptr;
robbiewb95e3622002-05-03 15:43:15 +000069
Cyril Hrubis1bbfae92013-06-04 18:55:10 +020070 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080071 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewb95e3622002-05-03 15:43:15 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 setup();
robbiewb95e3622002-05-03 15:43:15 +000074
subrata_modak56207ce2009-03-23 13:35:39 +000075 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiewb95e3622002-05-03 15:43:15 +000076
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 tst_count = 0;
robbiewb95e3622002-05-03 15:43:15 +000078
subrata_modak56207ce2009-03-23 13:35:39 +000079 if ((test_dir = opendir(".")) == NULL) {
80 tst_resm(TFAIL, "opendir(\".\") Failed, errno=%d : %s",
81 errno, strerror(errno));
robbiewb95e3622002-05-03 15:43:15 +000082 } else {
subrata_modak56207ce2009-03-23 13:35:39 +000083 if (closedir(test_dir) < 0) {
84 tst_resm(TFAIL,
85 "closedir(\".\") Failed, errno=%d : %s",
86 errno, strerror(errno));
robbiewb95e3622002-05-03 15:43:15 +000087 } else {
subrata_modak56207ce2009-03-23 13:35:39 +000088 dptr = readdir(test_dir);
89 switch (errno) {
90 case EBADF:
91 tst_resm(TPASS,
92 "expected failure - errno = %d : %s",
93 errno, strerror(errno));
94 break;
95 default:
96 if (dptr != NULL) {
97 tst_brkm(TFAIL, cleanup,
98 "call failed with an "
99 "unexpected error - %d : %s",
100 errno,
101 strerror(errno));
102 } else {
103 tst_resm(TINFO,
104 "readdir() is not _required_ to fail, "
105 "errno = %d ", errno);
106 }
107 }
robbiewb95e3622002-05-03 15:43:15 +0000108 }
robbiewb95e3622002-05-03 15:43:15 +0000109
subrata_modak56207ce2009-03-23 13:35:39 +0000110 }
robbiewb95e3622002-05-03 15:43:15 +0000111
Garrett Cooper2c282152010-12-16 00:55:50 -0800112 }
robbiewb95e3622002-05-03 15:43:15 +0000113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800115 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800116}
robbiewb95e3622002-05-03 15:43:15 +0000117
Cyril Hrubis1bbfae92013-06-04 18:55:10 +0200118static void sigsegv_handler(int sig)
robbiew88873012003-06-25 22:35:02 +0000119{
subrata_modak56207ce2009-03-23 13:35:39 +0000120 tst_resm(TCONF,
121 "This system's implementation of closedir() will not allow this test to execute properly.");
robbiew88873012003-06-25 22:35:02 +0000122 cleanup();
123}
robbiewb95e3622002-05-03 15:43:15 +0000124
Cyril Hrubis1bbfae92013-06-04 18:55:10 +0200125static void setup(void)
robbiewb95e3622002-05-03 15:43:15 +0000126{
subrata_modak56207ce2009-03-23 13:35:39 +0000127 struct sigaction act;
robbiewb95e3622002-05-03 15:43:15 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew88873012003-06-25 22:35:02 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131 act.sa_handler = sigsegv_handler;
Jan Stancekc5532e42013-05-14 11:59:33 +0200132 act.sa_flags = 0;
133 sigemptyset(&act.sa_mask);
Cyril Hrubis1bbfae92013-06-04 18:55:10 +0200134 sigaction(SIGSEGV, &act, NULL);
subrata_modak56207ce2009-03-23 13:35:39 +0000135
robbiewb95e3622002-05-03 15:43:15 +0000136 TEST_PAUSE;
137
subrata_modak56207ce2009-03-23 13:35:39 +0000138 tst_tmpdir();
robbiewb95e3622002-05-03 15:43:15 +0000139}
140
Cyril Hrubis1bbfae92013-06-04 18:55:10 +0200141static void cleanup(void)
robbiewb95e3622002-05-03 15:43:15 +0000142{
subrata_modak56207ce2009-03-23 13:35:39 +0000143 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700144}