plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 4 | * Author: Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 17 | * along with this program; if not, write to the Free Software Foundation, |
| 18 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 22 | * Test that fchmod() fails and sets the proper errno values. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 25 | #ifndef _GNU_SOURCE |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 26 | # define _GNU_SOURCE |
vapier | 5b31d1b | 2006-08-21 06:46:26 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Garrett Cooper | 5741b47 | 2010-12-18 19:36:16 -0800 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
| 37 | #include <signal.h> |
robbiew | 15226cd | 2002-04-10 16:10:40 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | #include <grp.h> |
| 40 | #include <pwd.h> |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 41 | #include <sys/mount.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
| 43 | #include "test.h" |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 44 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 46 | static int fd1; |
| 47 | static int fd2; |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 48 | static int fd3; |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 49 | static const char *device; |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 50 | static int mount_flag; |
| 51 | |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 52 | static struct test_case_t { |
| 53 | char *name; |
| 54 | int *fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | int mode; |
| 56 | int exp_errno; |
Garrett Cooper | 5741b47 | 2010-12-18 19:36:16 -0800 | [diff] [blame] | 57 | } test_cases[] = { |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 58 | {"EPERM", &fd1, 0644, EPERM}, |
| 59 | {"EBADF", &fd2, 0644, EBADF}, |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 60 | {"EROFS", &fd3, 0644, EROFS}, |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 61 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 63 | char *TCID = "fchmod06"; |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 64 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 66 | static void setup(void); |
| 67 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 71 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 73 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 75 | msg = parse_opts(ac, av, NULL, NULL); |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 76 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 78 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | setup(); |
| 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 82 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 83 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
Garrett Cooper | 5741b47 | 2010-12-18 19:36:16 -0800 | [diff] [blame] | 85 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 87 | TEST(fchmod(*test_cases[i].fd, test_cases[i].mode)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 88 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | if (TEST_RETURN == -1) { |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 90 | if (TEST_ERRNO == test_cases[i].exp_errno) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | tst_resm(TPASS | TTERRNO, |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 92 | "fchmod: test %s success", |
| 93 | test_cases[i].name); |
| 94 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | tst_resm(TFAIL | TTERRNO, |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 96 | "fchmod: test %s FAILED with " |
| 97 | "unexpect errno: %d", |
| 98 | test_cases[i].name, |
| 99 | TEST_ERRNO); |
| 100 | } |
| 101 | } else { |
Garrett Cooper | 5741b47 | 2010-12-18 19:36:16 -0800 | [diff] [blame] | 102 | tst_resm(TFAIL, |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 103 | "fchmod: test %s success unexpectly", |
| 104 | test_cases[i].name); |
| 105 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 106 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 108 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | cleanup(); |
Garrett Cooper | 5741b47 | 2010-12-18 19:36:16 -0800 | [diff] [blame] | 111 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 114 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | { |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 116 | struct passwd *ltpuser; |
| 117 | const char *fs_type; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 120 | |
Garrett Cooper | 5741b47 | 2010-12-18 19:36:16 -0800 | [diff] [blame] | 121 | tst_require_root(NULL); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | TEST_PAUSE; |
| 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_tmpdir(); |
| 126 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 127 | fs_type = tst_dev_fs_type(); |
| 128 | device = tst_acquire_device(cleanup); |
| 129 | |
| 130 | if (!device) |
| 131 | tst_brkm(TCONF, cleanup, "Failed to obtain block device"); |
| 132 | |
| 133 | tst_mkfs(cleanup, device, fs_type, NULL); |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 134 | |
| 135 | SAFE_MKDIR(cleanup, "mntpoint", 0755); |
| 136 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 137 | if (mount(device, "mntpoint", fs_type, 0, NULL) < 0) { |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 138 | tst_brkm(TBROK | TERRNO, cleanup, |
| 139 | "mount device:%s failed", device); |
| 140 | } |
| 141 | mount_flag = 1; |
| 142 | |
| 143 | /* Create a file in the file system, then remount it as read-only */ |
| 144 | SAFE_TOUCH(cleanup, "mntpoint/tfile_3", 0644, NULL); |
| 145 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 146 | if (mount(device, "mntpoint", fs_type, |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 147 | MS_REMOUNT | MS_RDONLY, NULL) < 0) { |
| 148 | tst_brkm(TBROK | TERRNO, cleanup, |
| 149 | "mount device:%s failed", device); |
| 150 | } |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 151 | |
| 152 | fd3 = SAFE_OPEN(cleanup, "mntpoint/tfile_3", O_RDONLY); |
| 153 | |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 154 | fd1 = SAFE_OPEN(cleanup, "tfile_1", O_RDWR | O_CREAT, 0666); |
| 155 | |
| 156 | fd2 = SAFE_OPEN(cleanup, "tfile_2", O_RDWR | O_CREAT, 0666); |
| 157 | |
| 158 | SAFE_CLOSE(cleanup, fd2); |
| 159 | |
| 160 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 161 | |
| 162 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 163 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
zenglg.jy | f166fc8 | 2013-12-06 19:56:27 +0800 | [diff] [blame] | 165 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | { |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 167 | if (seteuid(0)) |
| 168 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 170 | if (fd1 > 0 && close(fd1)) |
| 171 | tst_resm(TWARN | TERRNO, "close(fd1) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 173 | if (fd3 > 0 && close(fd3)) |
| 174 | tst_resm(TWARN | TERRNO, "close(fd1) failed"); |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 175 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 176 | if (mount_flag && tst_umount("mntpoint") < 0) { |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 177 | tst_brkm(TBROK | TERRNO, NULL, |
| 178 | "umount device:%s failed", device); |
| 179 | } |
| 180 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 181 | if (device) |
| 182 | tst_release_device(NULL, device); |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 183 | |
Cyril Hrubis | 7118910 | 2014-06-30 12:10:07 +0200 | [diff] [blame] | 184 | tst_rmdir(); |
zenglg.jy | 7020544 | 2013-12-06 19:57:18 +0800 | [diff] [blame] | 185 | } |