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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: utime04 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that the system call utime() successfully sets the modification |
| 25 | * and access times of a file to the time specified by times argument, if |
| 26 | * the times argument is not null, and the user ID of the process is "root". |
| 27 | * |
| 28 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * utime succeeds returning zero and sets the access and modification |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * times of the file to that specified by the times argument. |
| 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 | * utime04 [-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. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | * -f : Turn off functionality Testing. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 <sys/types.h> |
| 74 | #include <errno.h> |
| 75 | #include <unistd.h> |
| 76 | #include <fcntl.h> |
| 77 | #include <utime.h> |
| 78 | #include <string.h> |
| 79 | #include <sys/stat.h> |
| 80 | #include <signal.h> |
| 81 | |
| 82 | #include "test.h" |
| 83 | #include "usctest.h" |
| 84 | |
| 85 | #define TEMP_FILE "tmp_file" |
| 86 | #define FILE_MODE S_IRUSR | S_IRGRP | S_IROTH |
| 87 | #define NEW_TIME 10000 |
| 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | char *TCID = "utime04"; /* Test program identifier. */ |
| 90 | int TST_TOTAL = 1; /* Total number of test cases. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | int exp_enos[] = { 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | struct utimbuf times; /* struct. buffer for utime() */ |
| 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. */ |
| 100 | int lc; /* loop counter */ |
| 101 | char *msg; /* message returned from parse_opts */ |
| 102 | time_t modf_time, access_time; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | /* file modification/access time */ |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | /* Parse standard options given to run the test. */ |
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 | |
| 114 | /* set the expected errnos... */ |
| 115 | TEST_EXP_ENOS(exp_enos); |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 118 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 121 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | * Invoke utime(2) to set TEMP_FILE access and |
| 123 | * modification times to that specified by |
| 124 | * times argument. |
| 125 | */ |
| 126 | TEST(utime(TEMP_FILE, ×)); |
| 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | if (TEST_RETURN == -1) { |
| 129 | TEST_ERROR_LOG(TEST_ERRNO); |
| 130 | tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s", |
| 131 | TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 132 | } else { |
| 133 | /* |
| 134 | * Perform functional verification if test |
| 135 | * executed without (-f) option. |
| 136 | */ |
| 137 | if (STD_FUNCTIONAL_TEST) { |
| 138 | /* |
| 139 | * Get the modification and access times of |
| 140 | * temporary file using stat(2). |
| 141 | */ |
| 142 | if (stat(TEMP_FILE, &stat_buf) < 0) { |
| 143 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | "stat(2) of %s failed, " |
| 145 | "error:%d", TEMP_FILE, |
| 146 | TEST_ERRNO); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | modf_time = stat_buf.st_mtime; |
| 149 | access_time = stat_buf.st_atime; |
| 150 | |
| 151 | /* Now do the actual verification */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | if ((modf_time != NEW_TIME) || |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | (access_time != NEW_TIME)) { |
| 154 | tst_resm(TFAIL, "%s access and " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | "modification times not set", |
| 156 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } else { |
| 158 | tst_resm(TPASS, "Functionality of " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | "utime(%s, ×) successful", |
| 160 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | } |
| 162 | } else { |
| 163 | tst_resm(TPASS, "%s call succeeded", TCID); |
| 164 | } |
| 165 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | Tst_count++; /* incr TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 167 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 170 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * void |
| 176 | * setup() - performs all ONE TIME setup for this test. |
| 177 | * Create a temporary directory and change directory to it. |
| 178 | * Create a test file under temporary directory and close it |
| 179 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | int fildes; /* file handle for temp file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 185 | |
| 186 | /* Check that the test process id is super/root */ |
| 187 | if (geteuid() != 0) { |
| 188 | tst_brkm(TBROK, NULL, "Must be super/root for this test!"); |
| 189 | tst_exit(); |
| 190 | } |
| 191 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | TEST_PAUSE; |
| 193 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | tst_tmpdir(); |
| 195 | |
| 196 | /* Creat a temporary file under above directory */ |
| 197 | if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) { |
| 198 | tst_brkm(TBROK, cleanup, |
| 199 | "creat(%s, %#o) Failed, errno=%d :%s", |
| 200 | TEMP_FILE, FILE_MODE, errno, strerror(errno)); |
| 201 | } |
| 202 | |
| 203 | /* Close the temporary file created */ |
| 204 | if (close(fildes) < 0) { |
| 205 | tst_brkm(TBROK, cleanup, |
| 206 | "close(%s) Failed, errno=%d : %s:", |
| 207 | TEMP_FILE, errno, strerror(errno)); |
| 208 | } |
| 209 | |
| 210 | /* Initialize the modification and access time in the times arg */ |
| 211 | times.actime = NEW_TIME; |
| 212 | times.modtime = NEW_TIME; |
| 213 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 214 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * void |
| 218 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 219 | * completion or premature exit. |
| 220 | * Remove the test directory and testfile created in the setup. |
| 221 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | { |
| 224 | /* |
| 225 | * print timing stats if that option was specified. |
| 226 | * print errno log if that option was specified. |
| 227 | */ |
| 228 | TEST_CLEANUP; |
| 229 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | tst_rmdir(); |
| 231 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame^] | 232 | } |