Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Fujitsu Ltd. |
| 3 | * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | /* |
| 18 | * Test Description: |
| 19 | * Verify that, |
| 20 | * 1. lchown() fails with -1 return value and sets errno to ELOOP |
| 21 | * if too many symbolic links were encountered in resolving path. |
| 22 | * 2. lchown() fails with -1 return value and sets errno to EROFS |
| 23 | * if the file is on a read-only file system. |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <errno.h> |
| 31 | #include <string.h> |
| 32 | #include <signal.h> |
| 33 | #include <grp.h> |
| 34 | #include <pwd.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <sys/mount.h> |
| 39 | |
| 40 | #include "test.h" |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 41 | #include "safe_macros.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 42 | #include "compat_16.h" |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 43 | |
| 44 | #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ |
| 45 | S_IXGRP|S_IROTH|S_IXOTH) |
| 46 | #define TEST_EROFS "mntpoint" |
| 47 | |
| 48 | static char test_eloop[PATH_MAX] = "."; |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 49 | static const char *device; |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 50 | static int mount_flag; |
| 51 | |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 52 | static struct test_case_t { |
| 53 | char *pathname; |
| 54 | int exp_errno; |
| 55 | } test_cases[] = { |
| 56 | {test_eloop, ELOOP}, |
| 57 | {TEST_EROFS, EROFS}, |
| 58 | }; |
| 59 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 60 | TCID_DEFINE(lchown03); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 61 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 62 | |
| 63 | static void setup(void); |
| 64 | static void lchown_verify(const struct test_case_t *); |
| 65 | static void cleanup(void); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 66 | |
| 67 | int main(int argc, char *argv[]) |
| 68 | { |
| 69 | int lc; |
| 70 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 71 | const char *msg; |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 72 | |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 73 | msg = parse_opts(argc, argv, NULL, NULL); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 74 | if (msg != NULL) |
| 75 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 76 | |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 77 | setup(); |
| 78 | |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 79 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 80 | tst_count = 0; |
| 81 | for (i = 0; i < TST_TOTAL; i++) |
| 82 | lchown_verify(&test_cases[i]); |
| 83 | } |
| 84 | |
| 85 | cleanup(); |
| 86 | tst_exit(); |
| 87 | } |
| 88 | |
| 89 | static void setup(void) |
| 90 | { |
| 91 | int i; |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 92 | const char *fs_type; |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 93 | |
| 94 | tst_require_root(NULL); |
| 95 | |
| 96 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 97 | |
| 98 | TEST_PAUSE; |
| 99 | |
| 100 | tst_tmpdir(); |
| 101 | |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 102 | fs_type = tst_dev_fs_type(); |
| 103 | device = tst_acquire_device(cleanup); |
| 104 | |
| 105 | if (!device) |
| 106 | tst_brkm(TCONF, cleanup, "Failed to acquire device"); |
| 107 | |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 108 | SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE); |
| 109 | SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); |
| 110 | for (i = 0; i < 43; i++) |
| 111 | strcat(test_eloop, "/test_eloop"); |
| 112 | |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 113 | tst_mkfs(cleanup, device, fs_type, NULL); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 114 | SAFE_MKDIR(cleanup, TEST_EROFS, DIR_MODE); |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 115 | if (mount(device, TEST_EROFS, fs_type, MS_RDONLY, NULL) < 0) { |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 116 | tst_brkm(TBROK | TERRNO, cleanup, |
| 117 | "mount device:%s failed", device); |
| 118 | } |
| 119 | mount_flag = 1; |
| 120 | } |
| 121 | |
| 122 | static void lchown_verify(const struct test_case_t *test) |
| 123 | { |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 124 | UID16_CHECK(geteuid(), "lchown", cleanup) |
| 125 | GID16_CHECK(getegid(), "lchown", cleanup) |
| 126 | |
| 127 | TEST(LCHOWN(cleanup, test->pathname, geteuid(), getegid())); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 128 | |
| 129 | if (TEST_RETURN != -1) { |
| 130 | tst_resm(TFAIL, "lchown() returned %ld, expected -1, errno=%d", |
| 131 | TEST_RETURN, test->exp_errno); |
| 132 | return; |
| 133 | } |
| 134 | |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 135 | if (TEST_ERRNO == test->exp_errno) { |
| 136 | tst_resm(TPASS | TTERRNO, "lchown() failed as expected"); |
| 137 | } else { |
| 138 | tst_resm(TFAIL | TTERRNO, |
| 139 | "lchown() failed unexpectedly; expected: %d - %s", |
| 140 | test->exp_errno, |
| 141 | strerror(test->exp_errno)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static void cleanup(void) |
| 146 | { |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 147 | if (mount_flag && tst_umount(TEST_EROFS) < 0) |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 148 | tst_resm(TWARN | TERRNO, "umount device:%s failed", device); |
| 149 | |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 150 | if (device) |
| 151 | tst_release_device(NULL, device); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 152 | |
Cyril Hrubis | bbffc17 | 2014-06-19 17:03:55 +0200 | [diff] [blame] | 153 | tst_rmdir(); |
Zeng Linggang | c1dcdca | 2014-02-18 21:10:05 +0800 | [diff] [blame] | 154 | } |