plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 John George |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Description: |
| 22 | * 1. Verify that the system call utime() fails to set the modification |
| 23 | * and access times of a file to the current time, under the following |
| 24 | * constraints, |
| 25 | * - The times argument is null. |
| 26 | * - The user ID of the process is not "root". |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * 2. Verify that the system call utime() fails to set the modification |
| 28 | * and access times of a file if the specified file doesn't exist. |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 29 | * 3. Verify that the system call utime() fails to set the modification |
| 30 | * and access times of a file to the current time, under the following |
| 31 | * constraints, |
| 32 | * - The times argument is not null. |
| 33 | * - The user ID of the process is not "root". |
| 34 | * 4. Verify that the system call utime() fails to set the modification |
| 35 | * and access times of a file that resides on a read-only file system. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include <errno.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <pwd.h> |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <signal.h> |
| 44 | #include <string.h> |
| 45 | #include <unistd.h> |
| 46 | #include <utime.h> |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 47 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | #include <sys/stat.h> |
| 49 | #include <sys/types.h> |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 50 | #include <sys/mount.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
| 52 | #include "test.h" |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 53 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | #define TEMP_FILE "tmp_file" |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 56 | #define MNT_POINT "mntpoint" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 58 | char *TCID = "utime06"; |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 59 | static struct passwd *ltpuser; |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 60 | static const struct utimbuf times; |
| 61 | static const char *dev; |
| 62 | static int mount_flag; |
| 63 | static void setup_nobody(void); |
| 64 | static void cleanup_nobody(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 66 | struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | char *pathname; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | int exp_errno; |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 69 | const struct utimbuf *times; |
| 70 | void (*setup_func)(void); |
| 71 | void (*cleanup_func)(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | } Test_cases[] = { |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 73 | {TEMP_FILE, EACCES, NULL, setup_nobody, cleanup_nobody}, |
| 74 | {"", ENOENT, NULL, NULL, NULL}, |
| 75 | {TEMP_FILE, EPERM, ×, setup_nobody, cleanup_nobody}, |
| 76 | {MNT_POINT, EROFS, NULL, NULL, NULL}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 79 | int TST_TOTAL = ARRAY_SIZE(Test_cases); |
| 80 | static void setup(void); |
| 81 | static void utime_verify(const struct test_case_t *); |
| 82 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 86 | int lc; |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 87 | int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 89 | tst_parse_opts(ac, av, NULL, NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 90 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | setup(); |
| 92 | |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 93 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 94 | tst_count = 0; |
| 95 | for (i = 0; i < TST_TOTAL; i++) |
| 96 | utime_verify(&Test_cases[i]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 100 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 103 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | { |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 105 | const char *fs_type; |
| 106 | |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 107 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
Cyril Hrubis | d1e794d | 2015-07-30 23:52:51 +0200 | [diff] [blame] | 109 | tst_require_root(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | TEST_PAUSE; |
| 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | tst_tmpdir(); |
| 114 | |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 115 | SAFE_TOUCH(cleanup, TEMP_FILE, 0644, NULL); |
| 116 | |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 117 | fs_type = tst_dev_fs_type(); |
| 118 | dev = tst_acquire_device(cleanup); |
| 119 | if (!dev) |
| 120 | tst_brkm(TCONF, cleanup, "Failed to acquire test device"); |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 121 | |
Zorro Lang | d5e1788 | 2016-03-15 11:37:48 +0800 | [diff] [blame] | 122 | tst_mkfs(cleanup, dev, fs_type, NULL, NULL); |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 123 | |
| 124 | SAFE_MKDIR(cleanup, MNT_POINT, 0644); |
| 125 | if (mount(dev, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) { |
| 126 | tst_brkm(TBROK | TERRNO, cleanup, |
| 127 | "mount device:%s failed", dev); |
| 128 | } |
| 129 | mount_flag = 1; |
| 130 | |
| 131 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void utime_verify(const struct test_case_t *test) |
| 135 | { |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 136 | if (test->setup_func != NULL) |
| 137 | test->setup_func(); |
| 138 | |
| 139 | TEST(utime(test->pathname, test->times)); |
| 140 | |
| 141 | if (test->cleanup_func != NULL) |
| 142 | test->cleanup_func(); |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 143 | |
| 144 | if (TEST_RETURN != -1) { |
| 145 | tst_resm(TFAIL, "utime succeeded unexpectedly"); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (TEST_ERRNO == test->exp_errno) { |
| 150 | tst_resm(TPASS | TTERRNO, "utime failed as expected"); |
| 151 | } else { |
| 152 | tst_resm(TFAIL | TTERRNO, |
| 153 | "utime failed unexpectedly; expected: %d - %s", |
| 154 | test->exp_errno, strerror(test->exp_errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 158 | static void setup_nobody(void) |
| 159 | { |
| 160 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
| 161 | } |
| 162 | |
| 163 | static void cleanup_nobody(void) |
| 164 | { |
| 165 | SAFE_SETEUID(cleanup, 0); |
| 166 | } |
| 167 | |
Cyril Hrubis | 3ae6e39 | 2014-06-24 14:57:07 +0200 | [diff] [blame] | 168 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | { |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 170 | if (mount_flag && tst_umount(MNT_POINT) < 0) |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 171 | tst_resm(TWARN | TERRNO, "umount device:%s failed", dev); |
| 172 | |
| 173 | if (dev) |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 174 | tst_release_device(dev); |
Zeng Linggang | 42e8600 | 2014-06-19 10:21:13 +0800 | [diff] [blame] | 175 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 177 | } |