• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (c) International Business Machines  Corp., 2006
4  *  Author: Yi Yang <yyangcdl@cn.ibm.com>
5  * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
6  *
7  * 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.
11  *
12  * 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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program;  if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * DESCRIPTION
22  *  This test case will verify basic function of openat
23  *  added by kernel 2.6.16 or up.
24  *
25  *****************************************************************************/
26 
27 #define _GNU_SOURCE
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <signal.h>
36 
37 #include "test.h"
38 #include "safe_macros.h"
39 #include "lapi/fcntl.h"
40 #include "openat.h"
41 
42 static void setup(void);
43 static void cleanup(void);
44 
45 char *TCID = "openat01";
46 
47 static int dir_fd, fd;
48 static int fd_invalid = 100;
49 static int fd_atcwd = AT_FDCWD;
50 
51 #define TEST_FILE "test_file"
52 #define TEST_DIR "test_dir/"
53 
54 static char glob_path[256];
55 
56 static struct test_case {
57 	int *dir_fd;
58 	const char *pathname;
59 	int exp_ret;
60 	int exp_errno;
61 } test_cases[] = {
62 	{&dir_fd, TEST_FILE, 0, 0},
63 	{&dir_fd, glob_path, 0, 0},
64 	{&fd, TEST_FILE, -1, ENOTDIR},
65 	{&fd_invalid, TEST_FILE, -1, EBADF},
66 	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
67 };
68 
69 int TST_TOTAL = ARRAY_SIZE(test_cases);
70 
verify_openat(struct test_case * test)71 static void verify_openat(struct test_case *test)
72 {
73 	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
74 
75 	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
76 	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
77 		tst_resm(TFAIL | TTERRNO,
78 		         "openat() returned %ldl, expected %d",
79 			 TEST_RETURN, test->exp_ret);
80 		return;
81 	}
82 
83 	if (TEST_RETURN > 0)
84 		SAFE_CLOSE(cleanup, TEST_RETURN);
85 
86 	if (TEST_ERRNO != test->exp_errno) {
87 		tst_resm(TFAIL | TTERRNO,
88 		         "openat() returned wrong errno, expected %s(%d)",
89 			 tst_strerrno(test->exp_errno), test->exp_errno);
90 		return;
91 	}
92 
93 	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
94 }
95 
main(int ac,char ** av)96 int main(int ac, char **av)
97 {
98 	int lc;
99 	int i;
100 
101 	tst_parse_opts(ac, av, NULL, NULL);
102 
103 	setup();
104 
105 	for (lc = 0; TEST_LOOPING(lc); lc++) {
106 		tst_count = 0;
107 
108 		for (i = 0; i < TST_TOTAL; i++)
109 			verify_openat(test_cases + i);
110 	}
111 
112 	cleanup();
113 	tst_exit();
114 }
115 
setup(void)116 static void setup(void)
117 {
118 	char *tmpdir;
119 
120 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
121 
122 	tst_tmpdir();
123 
124 	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
125 	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
126 	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
127 
128 	tmpdir = tst_get_tmpdir();
129 	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
130 	         tmpdir);
131 	free(tmpdir);
132 
133 	TEST_PAUSE;
134 }
135 
cleanup(void)136 static void cleanup(void)
137 {
138 	if (fd > 0 && close(fd))
139 		tst_resm(TWARN | TERRNO, "close(fd) failed");
140 
141 	if (dir_fd > 0 && close(dir_fd))
142 		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
143 
144 	tst_rmdir();
145 }
146