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