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 <error.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <signal.h>
37 
38 #include "test.h"
39 #include "safe_macros.h"
40 #include "lapi/fcntl.h"
41 #include "openat.h"
42 
43 static void setup(void);
44 static void cleanup(void);
45 
46 char *TCID = "openat01";
47 
48 static int dir_fd, fd;
49 static int fd_invalid = 100;
50 static int fd_atcwd = AT_FDCWD;
51 
52 #define TEST_FILE "test_file"
53 #define TEST_DIR "test_dir/"
54 
55 static char glob_path[256];
56 
57 static struct test_case {
58 	int *dir_fd;
59 	const char *pathname;
60 	int exp_ret;
61 	int exp_errno;
62 } test_cases[] = {
63 	{&dir_fd, TEST_FILE, 0, 0},
64 	{&dir_fd, glob_path, 0, 0},
65 	{&fd, TEST_FILE, -1, ENOTDIR},
66 	{&fd_invalid, TEST_FILE, -1, EBADF},
67 	{&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
68 };
69 
70 int TST_TOTAL = ARRAY_SIZE(test_cases);
71 
verify_openat(struct test_case * test)72 static void verify_openat(struct test_case *test)
73 {
74 	TEST(openat(*test->dir_fd, test->pathname, O_RDWR, 0600));
75 
76 	if ((test->exp_ret == -1 && TEST_RETURN != -1) ||
77 	    (test->exp_ret == 0 && TEST_RETURN < 0)) {
78 		tst_resm(TFAIL | TTERRNO,
79 		         "openat() returned %ldl, expected %d",
80 			 TEST_RETURN, test->exp_ret);
81 		return;
82 	}
83 
84 	if (TEST_RETURN > 0)
85 		SAFE_CLOSE(cleanup, TEST_RETURN);
86 
87 	if (TEST_ERRNO != test->exp_errno) {
88 		tst_resm(TFAIL | TTERRNO,
89 		         "openat() returned wrong errno, expected %s(%d)",
90 			 tst_strerrno(test->exp_errno), test->exp_errno);
91 		return;
92 	}
93 
94 	tst_resm(TPASS | TTERRNO, "openat() returned %ld", TEST_RETURN);
95 }
96 
main(int ac,char ** av)97 int main(int ac, char **av)
98 {
99 	int lc;
100 	int i;
101 
102 	tst_parse_opts(ac, av, NULL, NULL);
103 
104 	setup();
105 
106 	for (lc = 0; TEST_LOOPING(lc); lc++) {
107 		tst_count = 0;
108 
109 		for (i = 0; i < TST_TOTAL; i++)
110 			verify_openat(test_cases + i);
111 	}
112 
113 	cleanup();
114 	tst_exit();
115 }
116 
setup(void)117 static void setup(void)
118 {
119 	char *tmpdir;
120 
121 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
122 
123 	tst_tmpdir();
124 
125 	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
126 	dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
127 	fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
128 
129 	tmpdir = tst_get_tmpdir();
130 	snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
131 	         tmpdir);
132 	free(tmpdir);
133 
134 	TEST_PAUSE;
135 }
136 
cleanup(void)137 static void cleanup(void)
138 {
139 	if (fd > 0 && close(fd))
140 		tst_resm(TWARN | TERRNO, "close(fd) failed");
141 
142 	if (dir_fd > 0 && close(dir_fd))
143 		tst_resm(TWARN | TERRNO, "close(dir_fd) failed");
144 
145 	tst_rmdir();
146 }
147