blob: 8b4668aee7364407290acc8ec3d1995dacbe6322 [file] [log] [blame]
vapier4aaf10b2006-08-24 01:57:40 +00001/******************************************************************************
2 *
Cyril Hrubisa0e86112014-05-15 13:21:05 +02003 * Copyright (c) International Business Machines Corp., 2006
4 * Author: Yi Yang <yyangcdl@cn.ibm.com>
5 * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
vapier4aaf10b2006-08-24 01:57:40 +00006 *
Cyril Hrubisa0e86112014-05-15 13:21:05 +02007 * 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.
vapier4aaf10b2006-08-24 01:57:40 +000011 *
Cyril Hrubisa0e86112014-05-15 13:21:05 +020012 * 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.
vapier4aaf10b2006-08-24 01:57:40 +000016 *
Cyril Hrubisa0e86112014-05-15 13:21:05 +020017 * 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
vapier4aaf10b2006-08-24 01:57:40 +000020 *
21 * DESCRIPTION
Cyril Hrubisa0e86112014-05-15 13:21:05 +020022 * This test case will verify basic function of openat
23 * added by kernel 2.6.16 or up.
vapier4aaf10b2006-08-24 01:57:40 +000024 *
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>
Cyril Hrubisa0e86112014-05-15 13:21:05 +020037
vapier4aaf10b2006-08-24 01:57:40 +000038#include "test.h"
Cyril Hrubisa0e86112014-05-15 13:21:05 +020039#include "safe_macros.h"
gux.fnst@cn.fujitsu.com0d1cfbe2014-05-08 17:50:52 +080040#include "lapi/fcntl.h"
41#include "openat.h"
vapier4aaf10b2006-08-24 01:57:40 +000042
Cyril Hrubisa0e86112014-05-15 13:21:05 +020043static void setup(void);
44static void cleanup(void);
vapier4aaf10b2006-08-24 01:57:40 +000045
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020046char *TCID = "openat01";
Cyril Hrubisa0e86112014-05-15 13:21:05 +020047
48static int dirfd, fd;
49static int fd_invalid = 100;
50static int fd_atcwd = AT_FDCWD;
51
52#define TEST_FILE "test_file"
53#define TEST_DIR "test_dir/"
54
55static char glob_path[256];
56
57static struct test_case {
58 int *dirfd;
59 const char *pathname;
60 int exp_ret;
61 int exp_errno;
62} test_cases[] = {
63 {&dirfd, TEST_FILE, 0, 0},
64 {&dirfd, 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
70int TST_TOTAL = ARRAY_SIZE(test_cases);
71
72static void verify_openat(struct test_case *test)
73{
74 TEST(openat(*test->dirfd, 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}
vapier4aaf10b2006-08-24 01:57:40 +000096
vapier4aaf10b2006-08-24 01:57:40 +000097int main(int ac, char **av)
98{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020099 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200100 const char *msg;
vapier4aaf10b2006-08-24 01:57:40 +0000101 int i;
102
Garrett Cooper45e285d2010-11-22 12:19:25 -0800103 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800104 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier4aaf10b2006-08-24 01:57:40 +0000105
vapier4aaf10b2006-08-24 01:57:40 +0000106 setup();
107
vapier4aaf10b2006-08-24 01:57:40 +0000108 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 tst_count = 0;
vapier4aaf10b2006-08-24 01:57:40 +0000110
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200111 for (i = 0; i < TST_TOTAL; i++)
112 verify_openat(test_cases + i);
Garrett Cooper2c282152010-12-16 00:55:50 -0800113 }
vapier4aaf10b2006-08-24 01:57:40 +0000114
vapier4aaf10b2006-08-24 01:57:40 +0000115 cleanup();
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200116 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800117}
vapier4aaf10b2006-08-24 01:57:40 +0000118
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200119static void setup(void)
vapier4aaf10b2006-08-24 01:57:40 +0000120{
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200121 char *tmpdir;
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
vapier4aaf10b2006-08-24 01:57:40 +0000123 tst_sig(NOFORK, DEF_HANDLER, cleanup);
124
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200125 tst_tmpdir();
126
127 SAFE_MKDIR(cleanup, TEST_DIR, 0700);
128 dirfd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
129 fd = SAFE_OPEN(cleanup, TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
130
131 tmpdir = tst_get_tmpdir();
132 snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE,
133 tmpdir);
134 free(tmpdir);
135
vapier4aaf10b2006-08-24 01:57:40 +0000136 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800137}
vapier4aaf10b2006-08-24 01:57:40 +0000138
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200139static void cleanup(void)
vapier4aaf10b2006-08-24 01:57:40 +0000140{
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200141 if (fd > 0 && close(fd))
142 tst_resm(TWARN | TERRNO, "close(fd) failed");
vapier4aaf10b2006-08-24 01:57:40 +0000143
Cyril Hrubisa0e86112014-05-15 13:21:05 +0200144 if (dirfd > 0 && close(dirfd))
145 tst_resm(TWARN | TERRNO, "close(dirfd) failed");
146
147 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700148}