• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014 Fujitsu Ltd.
3  * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 /*
18  * Test Description:
19  *  Verify that,
20  *   1. Creat a directory and open it, then delete the directory, ENOENT would
21  *	return.
22  *   2. File descriptor does not refer to a directory, ENOTDIR would return.
23  *   3. Invalid file descriptor fd, EBADF would return.
24  *   4. Argument points outside the calling process's address space, EFAULT
25  *	would return.
26  *
27  *  PS:
28  *   This file is for readdir(2) and the other files(readdir01.c and
29  *   readdir02.c) are for readdir(3).
30  */
31 
32 #define _GNU_SOURCE
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <sys/mman.h>
41 #include "test.h"
42 #include "safe_macros.h"
43 #include "lapi/syscalls.h"
44 #include "lapi/readdir.h"
45 
46 char *TCID = "readdir21";
47 
48 #define TEST_DIR	"test_dir"
49 #define TEST_DIR4	"test_dir4"
50 #define TEST_FILE	"test_file"
51 #define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
52 			 S_IXGRP|S_IROTH|S_IXOTH)
53 
54 static unsigned int del_dir_fd, file_fd;
55 static unsigned int invalid_fd = 999;
56 static unsigned int dir_fd;
57 static struct old_linux_dirent dirp;
58 static void setup(void);
59 static void cleanup(void);
60 
61 static struct test_case_t {
62 	unsigned int *fd;
63 	struct old_linux_dirent *dirp;
64 	unsigned int count;
65 	int exp_errno;
66 } test_cases[] = {
67 	{&del_dir_fd, &dirp, sizeof(struct old_linux_dirent), ENOENT},
68 	{&file_fd, &dirp, sizeof(struct old_linux_dirent), ENOTDIR},
69 	{&invalid_fd, &dirp, sizeof(struct old_linux_dirent), EBADF},
70 #if !defined(UCLINUX)
71 	{&dir_fd, (struct old_linux_dirent *)-1,
72 	 sizeof(struct old_linux_dirent), EFAULT},
73 #endif
74 };
75 
76 int TST_TOTAL = ARRAY_SIZE(test_cases);
77 static void readdir_verify(const struct test_case_t *);
78 
main(int argc,char ** argv)79 int main(int argc, char **argv)
80 {
81 	int i, lc;
82 
83 	tst_parse_opts(argc, argv, NULL, NULL);
84 
85 	setup();
86 
87 	for (lc = 0; TEST_LOOPING(lc); lc++) {
88 		tst_count = 0;
89 		for (i = 0; i < TST_TOTAL; i++)
90 			readdir_verify(&test_cases[i]);
91 	}
92 
93 	cleanup();
94 	tst_exit();
95 }
96 
setup(void)97 static void setup(void)
98 {
99 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
100 
101 	TEST_PAUSE;
102 
103 	tst_tmpdir();
104 
105 	SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE);
106 	del_dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_RDONLY | O_DIRECTORY);
107 	SAFE_RMDIR(cleanup, TEST_DIR);
108 
109 	file_fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0777);
110 
111 	SAFE_MKDIR(cleanup, TEST_DIR4, DIR_MODE);
112 	dir_fd = SAFE_OPEN(cleanup, TEST_DIR4, O_RDONLY | O_DIRECTORY);
113 
114 #if !defined(UCLINUX)
115 	test_cases[3].dirp = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
116 				       MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
117 #endif
118 }
119 
readdir_verify(const struct test_case_t * test)120 static void readdir_verify(const struct test_case_t *test)
121 {
122 	TEST(ltp_syscall(__NR_readdir, *test->fd, test->dirp, test->count));
123 
124 	if (TEST_RETURN != -1) {
125 		tst_resm(TFAIL, "readdir() succeeded unexpectedly");
126 		return;
127 	}
128 
129 	if (TEST_ERRNO == test->exp_errno) {
130 		tst_resm(TPASS | TTERRNO, "readdir() failed as expected");
131 	} else {
132 		tst_resm(TFAIL | TTERRNO,
133 			 "readdir() failed unexpectedly; expected: %d - %s",
134 			 test->exp_errno, strerror(test->exp_errno));
135 	}
136 }
137 
cleanup(void)138 static void cleanup(void)
139 {
140 	if (dir_fd > 0)
141 		close(dir_fd);
142 
143 	if (del_dir_fd > 0)
144 		close(del_dir_fd);
145 
146 	if (file_fd > 0)
147 		close(file_fd);
148 
149 	tst_rmdir();
150 }
151