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: utime03 |
| 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, under the following |
| 26 | * constraints, |
| 27 | * - The times argument is null. |
| 28 | * - The user ID of the process is not "root". |
| 29 | * - The file is not owned by the user ID of the process. |
| 30 | * - The user ID of the process has write access to the file. |
| 31 | * |
| 32 | * Expected Result: |
| 33 | * utime succeeds returning zero and sets the access and modificatio |
| 34 | * times of the file to the current time. |
| 35 | * |
| 36 | * Algorithm: |
| 37 | * Setup: |
| 38 | * Setup signal handling. |
| 39 | * Create temporary directory. |
| 40 | * Pause for SIGUSR1 if option specified. |
| 41 | * |
| 42 | * Test: |
| 43 | * Loop if the proper options are given. |
| 44 | * Execute system call |
| 45 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 48 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 50 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | * Otherwise, |
| 52 | * Issue Functionality-Fail message. |
| 53 | * Cleanup: |
| 54 | * Print errno log and/or timing stats if options given |
| 55 | * Delete the temporary directory created. |
| 56 | * |
| 57 | * Usage: <for command-line> |
| 58 | * utime03 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 59 | * where, -c n : Run n copies concurrently. |
| 60 | * -e : Turn on errno logging. |
| 61 | * -f : Turn off functionality Testing. |
| 62 | * -i n : Execute test n times. |
| 63 | * -I x : Execute test for x seconds. |
| 64 | * -P x : Pause for x seconds between iterations. |
| 65 | * -t : Turn on syscall timing. |
| 66 | * |
| 67 | * History |
| 68 | * 07/2001 John George |
| 69 | * -Ported |
| 70 | * |
| 71 | * Restrictions: |
| 72 | * This test should be run by root only. |
plars | 5a35166 | 2001-08-31 20:01:52 +0000 | [diff] [blame] | 73 | * nobody and bin must be valid users. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 74 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | */ |
| 76 | |
| 77 | #include <errno.h> |
| 78 | #include <fcntl.h> |
| 79 | #include <pwd.h> |
| 80 | #include <signal.h> |
| 81 | #include <stdio.h> |
| 82 | #include <stdlib.h> |
| 83 | #include <string.h> |
| 84 | #include <unistd.h> |
| 85 | #include <utime.h> |
| 86 | #include <wait.h> |
| 87 | #include <sys/stat.h> |
| 88 | #include <sys/types.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 89 | #include <time.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| 91 | #include "test.h" |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 92 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | #define TEMP_FILE "tmp_file" |
| 95 | #define FILE_MODE S_IRWXU | S_IRGRP | S_IWGRP| S_IROTH | S_IWOTH |
| 96 | #define LTPUSER1 "nobody" |
plars | 5a35166 | 2001-08-31 20:01:52 +0000 | [diff] [blame] | 97 | #define LTPUSER2 "bin" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 99 | char *TCID = "utime03"; |
| 100 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | time_t curr_time; /* current time in seconds */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | struct passwd *ltpuser; /* password struct for ltpusers */ |
| 104 | uid_t user_uid; /* user id of ltpuser */ |
| 105 | gid_t group_gid; /* group id of ltpuser */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | int status; |
| 107 | |
| 108 | void setup(); /* Main setup function of test */ |
| 109 | void cleanup(); /* cleanup function for the test */ |
| 110 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | { |
| 113 | struct stat stat_buf; /* struct buffer to hold file info. */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 114 | int lc; |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 115 | long type; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | time_t modf_time, access_time; |
| 117 | time_t pres_time; /* file modification/access/present time */ |
| 118 | pid_t pid; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 120 | tst_parse_opts(ac, av, NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | setup(); |
| 123 | |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 124 | switch ((type = tst_fs_type(cleanup, "."))) { |
| 125 | case TST_NFS_MAGIC: |
Xiong Zhou | d623e2c | 2014-09-25 03:24:19 -0400 | [diff] [blame] | 126 | if (tst_kvercmp(2, 6, 18) < 0) |
| 127 | tst_brkm(TCONF, cleanup, "Cannot do utime on a file" |
| 128 | " on %s filesystem before 2.6.18", |
| 129 | tst_fs_type_name(type)); |
| 130 | break; |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 131 | case TST_V9FS_MAGIC: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | tst_brkm(TCONF, cleanup, |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 133 | "Cannot do utime on a file on %s filesystem", |
| 134 | tst_fs_type_name(type)); |
Xiong Zhou | d623e2c | 2014-09-25 03:24:19 -0400 | [diff] [blame] | 135 | break; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | } |
Cyril Hrubis | bc5da68 | 2011-02-24 20:04:44 +0100 | [diff] [blame] | 137 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 138 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| 140 | if (pid == -1) { |
| 141 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 142 | } else if (pid == 0) { |
| 143 | if ((ltpuser = getpwnam(LTPUSER1)) == NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | tst_brkm(TBROK, cleanup, "%s not found in /etc/passwd", |
| 145 | LTPUSER1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* get uid/gid of user accordingly */ |
| 149 | user_uid = ltpuser->pw_uid; |
| 150 | |
| 151 | seteuid(user_uid); |
| 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 155 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 157 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | * Invoke utime(2) to set TEMP_FILE access and |
| 159 | * modification times to the current time. |
| 160 | */ |
| 161 | TEST(utime(TEMP_FILE, NULL)); |
| 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | if (TEST_RETURN == -1) { |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 164 | tst_resm(TFAIL|TTERRNO, |
| 165 | "utime(%s) failed", TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } else { |
| 167 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 168 | * Sleep for a second so that mod time |
| 169 | * and access times will be different |
| 170 | * from the current time. |
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 | sleep(2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 174 | /* |
| 175 | * Get the current time now, after |
| 176 | * calling utime(2) |
| 177 | */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 178 | pres_time = time(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Get the modification and access |
| 182 | * times of temporary file using |
| 183 | * stat(2). |
| 184 | */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 185 | SAFE_STAT(cleanup, TEMP_FILE, &stat_buf); |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 186 | modf_time = stat_buf.st_mtime; |
| 187 | access_time = stat_buf.st_atime; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 188 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 189 | /* Now do the actual verification */ |
| 190 | if (modf_time <= curr_time || |
| 191 | modf_time >= pres_time || |
| 192 | access_time <= curr_time || |
| 193 | access_time >= pres_time) { |
| 194 | tst_resm(TFAIL, "%s access and " |
| 195 | "modification times " |
| 196 | "not set", TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 198 | tst_resm(TPASS, "Functionality " |
| 199 | "of utime(%s, NULL) " |
| 200 | "successful", |
| 201 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 204 | tst_count++; /* incr. TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 205 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | } else { |
| 207 | waitpid(pid, &status, 0); |
| 208 | _exit(0); /* |
| 209 | * Exit here and let the child clean up. |
| 210 | * This allows the errno information set |
| 211 | * by the TEST_ERROR_LOG macro and the |
| 212 | * PASS/FAIL status to be preserved for |
| 213 | * use during cleanup. |
| 214 | */ |
| 215 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 217 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 218 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 219 | |
| 220 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * void |
| 224 | * setup() - performs all ONE TIME setup for this test. |
| 225 | * Create a temporary directory and change directory to it. |
| 226 | * Create a test file under temporary directory and close it |
plars | 5a35166 | 2001-08-31 20:01:52 +0000 | [diff] [blame] | 227 | * Change the ownership of testfile to that of "bin" user. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | * Record the current time. |
| 229 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 230 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | int fildes; /* file handle for temp file */ |
plars | 5a35166 | 2001-08-31 20:01:52 +0000 | [diff] [blame] | 233 | char *tmpd = NULL; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | |
Cyril Hrubis | d1e794d | 2015-07-30 23:52:51 +0200 | [diff] [blame] | 235 | tst_require_root(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 236 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 237 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 238 | |
| 239 | /* Pause if that option was specified |
| 240 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 241 | * You want to make sure you do this before you create your temporary |
| 242 | * directory. |
| 243 | */ |
| 244 | TEST_PAUSE; |
| 245 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | tst_tmpdir(); |
| 247 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 248 | /* get the name of the temporary directory */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 249 | tmpd = SAFE_GETCWD(NULL, tmpd, 0); |
plars | 5a35166 | 2001-08-31 20:01:52 +0000 | [diff] [blame] | 250 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | /* Creat a temporary file under above directory */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 252 | fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 253 | |
| 254 | /* Close the temporary file created */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 255 | SAFE_CLOSE(cleanup, fildes); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * Make sure that specified Mode permissions set as |
| 259 | * umask value may be different. |
| 260 | */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 261 | SAFE_CHMOD(cleanup, TEMP_FILE, FILE_MODE); |
| 262 | SAFE_CHMOD(cleanup, tmpd, 0711); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 263 | |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 264 | ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 265 | |
| 266 | /* get uid/gid of user accordingly */ |
| 267 | user_uid = ltpuser->pw_uid; |
| 268 | group_gid = ltpuser->pw_gid; |
| 269 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 270 | /* |
| 271 | * Change the ownership of test directory/file specified by |
| 272 | * pathname to that of user_uid and group_gid. |
| 273 | */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 274 | SAFE_CHOWN(cleanup, TEMP_FILE, user_uid, group_gid); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 275 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 276 | /* Get the current time */ |
Nicolas Joly | b217f80 | 2015-11-28 10:26:14 +0100 | [diff] [blame] | 277 | curr_time = time(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * Sleep for a second so that mod time and access times will be |
| 281 | * different from the current time |
| 282 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | sleep(2); /* sleep(1) on IA64 sometimes sleeps < 1 sec!! */ |
| 284 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 285 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * void |
| 289 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 290 | * completion or premature exit. |
| 291 | * Remove the test directory and testfile created in the setup. |
| 292 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 293 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 294 | { |
| 295 | seteuid(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 296 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 297 | tst_rmdir(); |
| 298 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 299 | } |