blob: 536feef8096bd0ec383d216cc217737bb4dc6ebe [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubiscaf3d342013-03-18 14:10:32 +01002 * Copyright (c) International Business Machines Corp., 2001
Cyril Hrubis85f662d2013-03-18 19:22:39 +01003 * written by Wayne Boyer
4 * Copyright (c) 2013 Markos Chandras
5 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00006 *
Cyril Hrubiscaf3d342013-03-18 14:10:32 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +000011 *
Cyril Hrubiscaf3d342013-03-18 14:10:32 +010012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000016 *
Cyril Hrubiscaf3d342013-03-18 14:10:32 +010017 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000020 */
21
Xiaoguang Wange906e332014-02-12 12:50:24 +080022/*
23 * Test Description:
24 * Verify that,
25 * 1. getdents() fails with -1 return value and sets errno to EBADF
26 * if file descriptor fd is invalid.
27 * 2. getdents() fails with -1 return value and sets errno to EINVAL
28 * if result buffer is too small.
29 * 3. getdents() fails with -1 return value and sets errno to ENOTDIR
30 * if file descriptor does not refer to a directory.
31 * 4. getdents() fails with -1 return value and sets errno to ENOENT
32 * if there is no such directory.
33 *
34 */
35
36#define _GNU_SOURCE
Cyril Hrubis85f662d2013-03-18 19:22:39 +010037#include <stdio.h>
38#include <errno.h>
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000042
plars865695b2001-08-27 22:15:12 +000043#include "test.h"
Cyril Hrubis85f662d2013-03-18 19:22:39 +010044#include "getdents.h"
Xiaoguang Wange906e332014-02-12 12:50:24 +080045#include "safe_macros.h"
46
47#define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
48 S_IXGRP|S_IROTH|S_IXOTH)
49#define TEST_DIR "test_dir"
plars865695b2001-08-27 22:15:12 +000050
Cyril Hrubiscaf3d342013-03-18 14:10:32 +010051static void cleanup(void);
52static void setup(void);
Xiaoguang Wange906e332014-02-12 12:50:24 +080053static void print_test_result(int err, int exp_errno);
plars865695b2001-08-27 22:15:12 +000054
55char *TCID = "getdents02";
plars865695b2001-08-27 22:15:12 +000056
Xiaoguang Wange906e332014-02-12 12:50:24 +080057static void test_ebadf(void);
58static void test_einval(void);
59static void test_enotdir(void);
60static void test_enoent(void);
61
62static void (*testfunc[])(void) = { test_ebadf, test_einval,
63 test_enotdir, test_enoent };
64
65int TST_TOTAL = ARRAY_SIZE(testfunc);
plars865695b2001-08-27 22:15:12 +000066
Markos Chandras28894f52013-03-18 10:54:53 +000067static int longsyscall;
68
Cyril Hrubiscaf3d342013-03-18 14:10:32 +010069option_t options[] = {
70 /* -l long option. Tests getdents64 */
71 {"l", &longsyscall, NULL},
Markos Chandras28894f52013-03-18 10:54:53 +000072 {NULL, NULL, NULL}
73};
74
Cyril Hrubiscaf3d342013-03-18 14:10:32 +010075static void help(void)
Markos Chandras28894f52013-03-18 10:54:53 +000076{
77 printf(" -l Test the getdents64 system call\n");
78}
79
plars74948ad2002-11-14 16:16:14 +000080int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000081{
Xiaoguang Wange906e332014-02-12 12:50:24 +080082 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020083 const char *msg;
plars865695b2001-08-27 22:15:12 +000084
Cyril Hrubiscaf3d342013-03-18 14:10:32 +010085 if ((msg = parse_opts(ac, av, options, &help)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080086 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000087
Garrett Cooper2cb52912011-03-04 01:21:59 -080088 setup();
plars865695b2001-08-27 22:15:12 +000089
90 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080091 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000092
Xiaoguang Wange906e332014-02-12 12:50:24 +080093 for (i = 0; i < TST_TOTAL; i++)
94 (*testfunc[i])();
plars865695b2001-08-27 22:15:12 +000095 }
96
97 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080098 tst_exit();
plars865695b2001-08-27 22:15:12 +000099}
100
Cyril Hrubiscaf3d342013-03-18 14:10:32 +0100101static void setup(void)
plars865695b2001-08-27 22:15:12 +0000102{
plars865695b2001-08-27 22:15:12 +0000103 tst_sig(NOFORK, DEF_HANDLER, cleanup);
104
plars865695b2001-08-27 22:15:12 +0000105 tst_tmpdir();
106
Garrett Cooper2cb52912011-03-04 01:21:59 -0800107 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000108}
109
Xiaoguang Wange906e332014-02-12 12:50:24 +0800110static void print_test_result(int err, int exp_errno)
111{
Xiaoguang Wange906e332014-02-12 12:50:24 +0800112 if (err == 0) {
113 tst_resm(TFAIL, "call succeeded unexpectedly");
114 } else if (err == exp_errno) {
115 tst_resm(TPASS, "getdents failed as expected: %s",
116 strerror(err));
117 } else if (err == ENOSYS) {
118 tst_resm(TCONF, "syscall not implemented");
119 } else {
120 tst_resm(TFAIL, "getdents failed unexpectedly: %s",
121 strerror(err));
122 }
123}
124
125static void test_ebadf(void)
126{
127 int fd = -5;
128 struct linux_dirent64 dirp64;
129 struct linux_dirent dirp;
130
131 if (longsyscall)
132 getdents64(fd, &dirp64, sizeof(dirp64));
133 else
134 getdents(fd, &dirp, sizeof(dirp));
135
136 print_test_result(errno, EBADF);
137}
138
139static void test_einval(void)
140{
141 int fd;
142 char buf[1];
143
144 fd = SAFE_OPEN(cleanup, ".", O_RDONLY);
145
146 /* Pass one byte long buffer. The result should be EINVAL */
147 if (longsyscall)
148 getdents64(fd, (void *)buf, sizeof(buf));
149 else
150 getdents(fd, (void *)buf, sizeof(buf));
151
152 print_test_result(errno, EINVAL);
153
154 SAFE_CLOSE(cleanup, fd);
155}
156
157static void test_enotdir(void)
158{
159 int fd;
160 struct linux_dirent64 dir64;
161 struct linux_dirent dir;
162
163 fd = SAFE_OPEN(cleanup, "test", O_CREAT | O_RDWR);
164
165 if (longsyscall)
166 getdents64(fd, &dir64, sizeof(dir64));
167 else
168 getdents(fd, &dir, sizeof(dir));
169
170 print_test_result(errno, ENOTDIR);
171
172 SAFE_CLOSE(cleanup, fd);
173}
174
175static void test_enoent(void)
176{
177 int fd;
178 struct linux_dirent64 dir64;
179 struct linux_dirent dir;
180
181 SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE);
182
183 fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
184 if (rmdir(TEST_DIR) == -1) {
185 tst_brkm(TBROK | TERRNO, cleanup,
186 "rmdir(%s) failed", TEST_DIR);
187 }
188
189 if (longsyscall)
190 getdents64(fd, &dir64, sizeof(dir64));
191 else
192 getdents(fd, &dir, sizeof(dir));
193
194 print_test_result(errno, ENOENT);
195
196 SAFE_CLOSE(cleanup, fd);
197}
198
Cyril Hrubiscaf3d342013-03-18 14:10:32 +0100199static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000200{
Garrett Cooper2cb52912011-03-04 01:21:59 -0800201 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000202}