Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 1 | /* |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2006 |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 3 | * AUTHOR: Yi Yang <yyangcdl@cn.ibm.com> |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 16 | * along with this program; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | /* |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 20 | * DESCRIPTION |
| 21 | * This test case will verify basic function of fchownat |
| 22 | * added by kernel 2.6.16 or up. |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 23 | */ |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 24 | |
| 25 | #define _GNU_SOURCE |
| 26 | |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
| 31 | #include <error.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
| 35 | #include <signal.h> |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 36 | |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 37 | #include "test.h" |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 38 | #include "safe_macros.h" |
| 39 | #include "fchownat.h" |
| 40 | #include "lapi/fcntl.h" |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 41 | |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 42 | #define TESTFILE "testfile" |
| 43 | |
| 44 | static void setup(void); |
| 45 | static void cleanup(void); |
| 46 | |
| 47 | static int dirfd; |
| 48 | static int fd; |
| 49 | static int no_fd = -1; |
| 50 | static int cu_fd = AT_FDCWD; |
| 51 | |
| 52 | static struct test_case_t { |
| 53 | int exp_ret; |
| 54 | int exp_errno; |
| 55 | int flag; |
| 56 | int *fds; |
| 57 | char *filenames; |
| 58 | } test_cases[] = { |
| 59 | {0, 0, 0, &dirfd, TESTFILE}, |
| 60 | {-1, ENOTDIR, 0, &fd, TESTFILE}, |
| 61 | {-1, EBADF, 0, &no_fd, TESTFILE}, |
| 62 | {-1, EINVAL, 9999, &dirfd, TESTFILE}, |
| 63 | {0, 0, 0, &cu_fd, TESTFILE}, |
| 64 | }; |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 65 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 66 | char *TCID = "fchownat01"; |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 67 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
| 68 | static void fchownat_verify(const struct test_case_t *); |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 69 | |
| 70 | int main(int ac, char **av) |
| 71 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 72 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 73 | const char *msg; |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 74 | int i; |
| 75 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 76 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 78 | |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 79 | setup(); |
| 80 | |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 82 | tst_count = 0; |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 83 | for (i = 0; i < TST_TOTAL; i++) |
| 84 | fchownat_verify(&test_cases[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 85 | } |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 86 | |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 87 | cleanup(); |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 88 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 89 | } |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 90 | |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 91 | static void setup(void) |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 92 | { |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 93 | if ((tst_kvercmp(2, 6, 16)) < 0) |
| 94 | tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer"); |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 95 | |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 96 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 97 | |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 98 | TEST_PAUSE; |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 99 | |
| 100 | tst_tmpdir(); |
| 101 | |
| 102 | dirfd = SAFE_OPEN(cleanup, "./", O_DIRECTORY); |
| 103 | |
| 104 | SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL); |
| 105 | |
| 106 | fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 107 | } |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 108 | |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 109 | static void fchownat_verify(const struct test_case_t *test) |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 110 | { |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 111 | TEST(fchownat(*(test->fds), test->filenames, geteuid(), |
| 112 | getegid(), test->flag)); |
subrata_modak | 381cb39 | 2008-03-14 19:23:15 +0000 | [diff] [blame] | 113 | |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 114 | if (TEST_RETURN != test->exp_ret) { |
| 115 | tst_resm(TFAIL | TTERRNO, |
| 116 | "fchownat() returned %ld, expected %d, errno=%d", |
| 117 | TEST_RETURN, test->exp_ret, test->exp_errno); |
| 118 | return; |
| 119 | } |
vapier | 032481e | 2006-08-24 00:24:45 +0000 | [diff] [blame] | 120 | |
Zeng Linggang | f911361 | 2014-02-26 17:35:13 +0800 | [diff] [blame] | 121 | if (TEST_ERRNO == test->exp_errno) { |
| 122 | tst_resm(TPASS | TTERRNO, |
| 123 | "fchownat() returned the expected errno %d: %s", |
| 124 | test->exp_ret, strerror(test->exp_errno)); |
| 125 | } else { |
| 126 | tst_resm(TFAIL | TTERRNO, |
| 127 | "fchownat() failed unexpectedly; expected: %d - %s", |
| 128 | test->exp_errno, strerror(test->exp_errno)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void cleanup(void) |
| 133 | { |
| 134 | if (fd > 0) |
| 135 | close(fd); |
| 136 | |
| 137 | if (dirfd > 0) |
| 138 | close(dirfd); |
| 139 | |
| 140 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 141 | } |