Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Crackerjack Project., 2007 ,Hitachi, Ltd |
| 3 | * Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com> |
| 4 | * Ported to LTP: Manas Kumar Nayak maknayak@in.ibm.com> |
| 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 |
| 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. |
| 19 | */ |
| 20 | /* |
| 21 | * Description: |
| 22 | * Verify that, |
| 23 | * 1) utimes() returns -1 and sets errno to EACCES if times |
| 24 | * is NULL, the caller's effective user ID does not match |
| 25 | * the owner of the file, the caller does not have write |
| 26 | * access to the file, and the caller is not privileged. |
| 27 | * 2) utimes() returns -1 and sets errno to ENOENT if filename |
| 28 | * does not exist. |
| 29 | * 3) utimes() returns -1 and sets errno to EFAULT if filename |
| 30 | * is NULL. |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 31 | * 4) utimes() returns -1 and sets errno to EPERM if times is |
| 32 | * not NULL, the caller's effective UID does not match the |
| 33 | * owner of the file, and the caller is not privileged. |
| 34 | * 5) utimes() returns -1 and sets errno to EROFS if path resides |
| 35 | * on a read-only file system. |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 36 | */ |
| 37 | |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <sys/time.h> |
| 41 | #include <dirent.h> |
| 42 | #include <unistd.h> |
| 43 | #include <getopt.h> |
| 44 | #include <string.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <errno.h> |
| 47 | #include <stdio.h> |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 48 | #include <pwd.h> |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 49 | #include <sys/mount.h> |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 50 | |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 51 | #include "test.h" |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 52 | #include "safe_macros.h" |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 53 | #include "linux_syscall_numbers.h" |
| 54 | |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 55 | #define MNTPOINT "mntpoint" |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 56 | #define TESTFILE1 "testfile1" |
| 57 | #define TESTFILE2 "testfile2" |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 58 | #define TESTFILE3 "mntpoint/testfile" |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 59 | #define FILE_MODE (S_IRWXU | S_IRGRP | S_IXGRP | \ |
| 60 | S_IROTH | S_IXOTH) |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 61 | #define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO) |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 62 | |
| 63 | #define LTPUSER1 "nobody" |
| 64 | #define LTPUSER2 "bin" |
| 65 | |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 66 | static const char *device; |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 67 | static int mount_flag; |
| 68 | |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 69 | static struct timeval a_tv[2] = { {0, 0}, {1000, 0} }; |
| 70 | static struct timeval m_tv[2] = { {1000, 0}, {0, 0} }; |
| 71 | static struct timeval tv[2] = { {1000, 0}, {2000, 0} }; |
| 72 | |
| 73 | static struct test_case_t { |
| 74 | char *pathname; |
| 75 | struct timeval *times; |
| 76 | int exp_errno; |
| 77 | } test_cases[] = { |
| 78 | { TESTFILE1, a_tv, 0 }, |
| 79 | { TESTFILE1, m_tv, 0 }, |
| 80 | { TESTFILE2, NULL, EACCES }, |
| 81 | { "notexistfile", tv, ENOENT }, |
| 82 | { NULL, tv, EFAULT }, |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 83 | { TESTFILE2, tv, EPERM }, |
| 84 | { TESTFILE3, tv, EROFS }, |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static void setup(void); |
| 88 | static void cleanup(void); |
| 89 | static void utimes_verify(const struct test_case_t *); |
| 90 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 91 | char *TCID = "utimes01"; |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 92 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 93 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | int main(int ac, char **av) |
| 95 | { |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 96 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 97 | const char *msg; |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 98 | |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 99 | msg = parse_opts(ac, av, NULL, NULL); |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 100 | if (msg != NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 045b222 | 2010-11-22 15:34:12 -0800 | [diff] [blame] | 103 | setup(); |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 104 | |
Garrett Cooper | 045b222 | 2010-11-22 15:34:12 -0800 | [diff] [blame] | 105 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | tst_count = 0; |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 107 | |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 108 | for (i = 0; i < TST_TOTAL; i++) |
| 109 | utimes_verify(&test_cases[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | } |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 111 | |
Garrett Cooper | 045b222 | 2010-11-22 15:34:12 -0800 | [diff] [blame] | 112 | cleanup(); |
subrata_modak | 705ba46 | 2009-05-29 12:21:34 +0000 | [diff] [blame] | 113 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 114 | } |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 115 | |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 116 | static void setup(void) |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 117 | { |
| 118 | struct passwd *ltpuser; |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 119 | const char *fs_type; |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 120 | |
| 121 | tst_require_root(NULL); |
| 122 | |
| 123 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 124 | |
| 125 | TEST_PAUSE; |
| 126 | |
| 127 | tst_tmpdir(); |
| 128 | |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 129 | fs_type = tst_dev_fs_type(); |
| 130 | device = tst_acquire_device(cleanup); |
| 131 | |
| 132 | if (!device) |
| 133 | tst_brkm(TCONF, cleanup, "Failed to obtain block device"); |
| 134 | |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 135 | SAFE_TOUCH(cleanup, TESTFILE1, FILE_MODE, NULL); |
| 136 | ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1); |
| 137 | SAFE_CHOWN(cleanup, TESTFILE1, ltpuser->pw_uid, |
| 138 | ltpuser->pw_gid); |
| 139 | |
| 140 | SAFE_TOUCH(cleanup, TESTFILE2, FILE_MODE, NULL); |
| 141 | ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER2); |
| 142 | SAFE_CHOWN(cleanup, TESTFILE2, ltpuser->pw_uid, |
| 143 | ltpuser->pw_gid); |
| 144 | |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 145 | tst_mkfs(cleanup, device, fs_type, NULL); |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 146 | SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE); |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 147 | if (mount(device, MNTPOINT, fs_type, 0, NULL) == -1) { |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 148 | tst_brkm(TBROK | TERRNO, cleanup, |
| 149 | "mount device:%s failed", device); |
| 150 | } |
| 151 | mount_flag = 1; |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 152 | SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL); |
| 153 | ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1); |
| 154 | SAFE_CHOWN(cleanup, TESTFILE3, ltpuser->pw_uid, |
| 155 | ltpuser->pw_gid); |
| 156 | if (mount(device, MNTPOINT, fs_type, |
| 157 | MS_REMOUNT | MS_RDONLY, NULL) == -1) { |
| 158 | tst_brkm(TBROK | TERRNO, cleanup, |
| 159 | "mount device:%s failed", device); |
| 160 | } |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 161 | |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 162 | ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1); |
| 163 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
| 164 | } |
| 165 | |
| 166 | static void utimes_verify(const struct test_case_t *tc) |
| 167 | { |
| 168 | struct stat st; |
| 169 | struct timeval tmp_tv[2]; |
| 170 | |
| 171 | if (tc->exp_errno == 0) { |
| 172 | SAFE_STAT(cleanup, tc->pathname, &st); |
| 173 | |
| 174 | tmp_tv[0].tv_sec = st.st_atime; |
| 175 | tmp_tv[0].tv_usec = 0; |
| 176 | tmp_tv[1].tv_sec = st.st_mtime; |
| 177 | tmp_tv[1].tv_usec = 0; |
| 178 | } |
| 179 | |
| 180 | TEST(utimes(tc->pathname, tc->times)); |
| 181 | |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 182 | if (TEST_ERRNO == tc->exp_errno) { |
| 183 | tst_resm(TPASS | TTERRNO, "utimes() worked as expected"); |
| 184 | } else { |
| 185 | tst_resm(TFAIL | TTERRNO, |
| 186 | "utimes() failed unexpectedly; expected: %d - %s", |
| 187 | tc->exp_errno, strerror(tc->exp_errno)); |
| 188 | } |
| 189 | |
| 190 | if (TEST_ERRNO == 0 && utimes(tc->pathname, tmp_tv) == -1) |
| 191 | tst_brkm(TBROK | TERRNO, cleanup, "utimes() failed."); |
| 192 | } |
| 193 | |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 194 | static void cleanup(void) |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 195 | { |
| 196 | if (seteuid(0) == -1) |
| 197 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
| 198 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 199 | if (mount_flag && tst_umount(MNTPOINT) == -1) |
Xing Gu | 6b22d9d | 2014-06-13 10:53:19 +0800 | [diff] [blame] | 200 | tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT); |
| 201 | |
Cyril Hrubis | 1e8843d | 2014-06-30 12:17:27 +0200 | [diff] [blame] | 202 | |
| 203 | if (device) |
| 204 | tst_release_device(NULL, device); |
| 205 | |
Xing Gu | c287084 | 2014-06-13 10:53:18 +0800 | [diff] [blame] | 206 | tst_rmdir(); |
| 207 | } |