vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 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> |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 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. |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 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. |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 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 |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 20 | * |
| 21 | * DESCRIPTION |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 22 | * This test case will verify basic function of openat |
| 23 | * added by kernel 2.6.16 or up. |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 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> |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 37 | |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 38 | #include "test.h" |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 39 | #include "safe_macros.h" |
gux.fnst@cn.fujitsu.com | 0d1cfbe | 2014-05-08 17:50:52 +0800 | [diff] [blame] | 40 | #include "lapi/fcntl.h" |
| 41 | #include "openat.h" |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 43 | static void setup(void); |
| 44 | static void cleanup(void); |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 45 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 46 | char *TCID = "openat01"; |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 47 | |
| 48 | static int dirfd, 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 *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 | |
| 70 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
| 71 | |
| 72 | static 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 | } |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 96 | |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 97 | int main(int ac, char **av) |
| 98 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 99 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 100 | const char *msg; |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 101 | int i; |
| 102 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 103 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 104 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 105 | |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 106 | setup(); |
| 107 | |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 108 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 109 | tst_count = 0; |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 110 | |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 111 | for (i = 0; i < TST_TOTAL; i++) |
| 112 | verify_openat(test_cases + i); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 113 | } |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 114 | |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 115 | cleanup(); |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 116 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 117 | } |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 118 | |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 119 | static void setup(void) |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 120 | { |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 121 | char *tmpdir; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 123 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 124 | |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 125 | 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 | |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 136 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 137 | } |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 138 | |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 139 | static void cleanup(void) |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 140 | { |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 141 | if (fd > 0 && close(fd)) |
| 142 | tst_resm(TWARN | TERRNO, "close(fd) failed"); |
vapier | 4aaf10b | 2006-08-24 01:57:40 +0000 | [diff] [blame] | 143 | |
Cyril Hrubis | a0e8611 | 2014-05-15 13:21:05 +0200 | [diff] [blame] | 144 | if (dirfd > 0 && close(dirfd)) |
| 145 | tst_resm(TWARN | TERRNO, "close(dirfd) failed"); |
| 146 | |
| 147 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 148 | } |