plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 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 |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 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 | /* |
| 21 | * Test Name: utime01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that the system call utime() successfully sets the modification |
| 25 | * and access times of a file to the current time, if the times argument |
| 26 | * is null, and the user ID of the process is "root". |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * utime succeeds returning zero and sets the access and modification |
| 30 | * times of the file to the current time. |
| 31 | * |
| 32 | * Algorithm: |
| 33 | * Setup: |
| 34 | * Setup signal handling. |
| 35 | * Create temporary directory. |
| 36 | * Pause for SIGUSR1 if option specified. |
| 37 | * |
| 38 | * Test: |
| 39 | * Loop if the proper options are given. |
| 40 | * Execute system call |
| 41 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * Otherwise, |
| 48 | * Issue Functionality-Fail message. |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * Delete the temporary directory created. |
| 52 | * |
| 53 | * Usage: <for command-line> |
| 54 | * utime01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 55 | * where, -c n : Run n copies concurrently. |
| 56 | * -e : Turn on errno logging. |
| 57 | * -f : Turn off functionality Testing. |
| 58 | * -i n : Execute test n times. |
| 59 | * -I x : Execute test for x seconds. |
| 60 | * -P x : Pause for x seconds between iterations. |
| 61 | * -t : Turn on syscall timing. |
| 62 | * |
| 63 | * History |
| 64 | * 07/2001 John George |
| 65 | * -Ported |
| 66 | * |
| 67 | * Restrictions: |
| 68 | * This test should be run by 'super-user' (root) only. |
| 69 | * |
| 70 | */ |
| 71 | |
| 72 | #include <stdio.h> |
| 73 | #include <unistd.h> |
| 74 | #include <sys/types.h> |
| 75 | #include <errno.h> |
| 76 | #include <fcntl.h> |
| 77 | #include <utime.h> |
| 78 | #include <string.h> |
| 79 | #include <sys/stat.h> |
| 80 | #include <signal.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 81 | #include <time.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
| 83 | #include "test.h" |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 84 | #include "tst_fs_type.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
| 86 | #define TEMP_FILE "tmp_file" |
| 87 | #define FILE_MODE S_IRUSR | S_IRGRP | S_IROTH |
| 88 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 89 | char *TCID = "utime01"; |
| 90 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | time_t curr_time; /* current time in seconds */ |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 92 | time_t tloc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | void setup(); /* Main setup function of test */ |
| 95 | void cleanup(); /* cleanup function for the test */ |
| 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | { |
| 99 | struct stat stat_buf; /* struct buffer to hold file info. */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 100 | int lc; |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 101 | long type; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 102 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | time_t modf_time, access_time; |
| 104 | time_t pres_time; /* file modification/access/present time */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 106 | msg = parse_opts(ac, av, NULL, NULL); |
| 107 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | } |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | setup(); |
| 113 | |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 114 | switch ((type = tst_fs_type(cleanup, "."))) { |
| 115 | case TST_NFS_MAGIC: |
Xiong Zhou | d623e2c | 2014-09-25 03:24:19 -0400 | [diff] [blame] | 116 | if (tst_kvercmp(2, 6, 18) < 0) |
| 117 | tst_brkm(TCONF, cleanup, "Cannot do utime on a file" |
| 118 | " on %s filesystem before 2.6.18", |
| 119 | tst_fs_type_name(type)); |
| 120 | break; |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 121 | case TST_V9FS_MAGIC: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | tst_brkm(TCONF, cleanup, |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 123 | "Cannot do utime on a file on %s filesystem", |
| 124 | tst_fs_type_name(type)); |
Xiong Zhou | d623e2c | 2014-09-25 03:24:19 -0400 | [diff] [blame] | 125 | break; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | } |
Cyril Hrubis | bc5da68 | 2011-02-24 20:04:44 +0100 | [diff] [blame] | 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 129 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 130 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 132 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | * Invoke utime(2) to set TEMP_FILE access and |
| 134 | * modification times to the current time. |
| 135 | */ |
| 136 | TEST(utime(TEMP_FILE, NULL)); |
| 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s", |
| 140 | TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 141 | } else { |
| 142 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 143 | * Sleep for a second so that mod time and |
| 144 | * access times will be different from the |
| 145 | * current time |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 147 | sleep(2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 149 | /* |
| 150 | * Get the current time now, after calling |
| 151 | * utime(2) |
| 152 | */ |
| 153 | if ((pres_time = time(&tloc)) < 0) { |
| 154 | tst_brkm(TFAIL, cleanup, "time() " |
| 155 | "failed to get present time " |
| 156 | "after utime, error=%d", |
| 157 | errno); |
| 158 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 160 | /* |
| 161 | * Get the modification and access times of |
| 162 | * temporary file using stat(2). |
| 163 | */ |
| 164 | if (stat(TEMP_FILE, &stat_buf) < 0) { |
| 165 | tst_brkm(TFAIL, cleanup, "stat(2) of " |
| 166 | "%s failed, error:%d", |
| 167 | TEMP_FILE, TEST_ERRNO); |
| 168 | } |
| 169 | modf_time = stat_buf.st_mtime; |
| 170 | access_time = stat_buf.st_atime; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 172 | /* Now do the actual verification */ |
| 173 | if (modf_time <= curr_time || |
| 174 | modf_time >= pres_time || |
| 175 | access_time <= curr_time || |
| 176 | access_time >= pres_time) { |
| 177 | tst_resm(TFAIL, "%s access and " |
| 178 | "modification times not set", |
| 179 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 181 | tst_resm(TPASS, "Functionality of " |
| 182 | "utime(%s, NULL) successful", |
| 183 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 186 | tst_count++; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 187 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 190 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 191 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * void |
| 195 | * setup() - performs all ONE TIME setup for this test. |
| 196 | * Create a temporary directory and change directory to it. |
| 197 | * Create a test file under temporary directory and close it |
| 198 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 199 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | int fildes; /* file handle for temp file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 203 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 205 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | TEST_PAUSE; |
| 208 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | tst_tmpdir(); |
| 210 | |
| 211 | /* Creat a temporary file under above directory */ |
| 212 | if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) { |
| 213 | tst_brkm(TBROK, cleanup, |
| 214 | "creat(%s, %#o) Failed, errno=%d :%s", |
| 215 | TEMP_FILE, FILE_MODE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 216 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | |
| 218 | /* Close the temporary file created */ |
| 219 | if (close(fildes) < 0) { |
| 220 | tst_brkm(TBROK, cleanup, |
| 221 | "close(%s) Failed, errno=%d : %s:", |
| 222 | TEMP_FILE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 223 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | |
| 225 | /* Get the current time */ |
| 226 | if ((curr_time = time(&tloc)) < 0) { |
| 227 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 228 | "time() failed to get current time, errno=%d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 229 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * Sleep for a second so that mod time and access times will be |
| 233 | * different from the current time |
| 234 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | sleep(2); /* sleep(1) on IA64 sometimes sleeps < 1 sec!! */ |
| 236 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 237 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * void |
| 241 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 242 | * completion or premature exit. |
| 243 | * Remove the test directory and testfile created in the setup. |
| 244 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 245 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 247 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 248 | tst_rmdir(); |
| 249 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 250 | } |