blob: c5956712fca6ef9eb1660afc55992b77f836b4d6 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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_modak56207ce2009-03-23 13:35:39 +000046 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000047 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000048 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000049 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000050 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000051 * 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.
plars5a351662001-08-31 20:01:52 +000073 * nobody and bin must be valid users.
subrata_modak4bb656a2009-02-26 12:02:09 +000074 *
plars865695b2001-08-27 22:15:12 +000075 */
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>
Steven Jackson249f4052016-12-13 16:16:00 +000086#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000087#include <sys/stat.h>
88#include <sys/types.h>
plars74948ad2002-11-14 16:16:14 +000089#include <time.h>
plars865695b2001-08-27 22:15:12 +000090
91#include "test.h"
Nicolas Jolyb217f802015-11-28 10:26:14 +010092#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000093
94#define TEMP_FILE "tmp_file"
95#define FILE_MODE S_IRWXU | S_IRGRP | S_IWGRP| S_IROTH | S_IWOTH
96#define LTPUSER1 "nobody"
plars5a351662001-08-31 20:01:52 +000097#define LTPUSER2 "bin"
plars865695b2001-08-27 22:15:12 +000098
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020099char *TCID = "utime03";
100int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +0000101time_t curr_time; /* current time in seconds */
plars865695b2001-08-27 22:15:12 +0000102
subrata_modak56207ce2009-03-23 13:35:39 +0000103struct passwd *ltpuser; /* password struct for ltpusers */
104uid_t user_uid; /* user id of ltpuser */
105gid_t group_gid; /* group id of ltpuser */
plars865695b2001-08-27 22:15:12 +0000106int status;
107
108void setup(); /* Main setup function of test */
109void cleanup(); /* cleanup function for the test */
110
subrata_modak56207ce2009-03-23 13:35:39 +0000111int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000112{
113 struct stat stat_buf; /* struct buffer to hold file info. */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200114 int lc;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100115 long type;
plars865695b2001-08-27 22:15:12 +0000116 time_t modf_time, access_time;
117 time_t pres_time; /* file modification/access/present time */
118 pid_t pid;
subrata_modak56207ce2009-03-23 13:35:39 +0000119
Cyril Hrubisd6d11d02015-03-09 17:35:43 +0100120 tst_parse_opts(ac, av, NULL, NULL);
plars865695b2001-08-27 22:15:12 +0000121
plars865695b2001-08-27 22:15:12 +0000122 setup();
123
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100124 switch ((type = tst_fs_type(cleanup, "."))) {
125 case TST_NFS_MAGIC:
Xiong Zhoud623e2c2014-09-25 03:24:19 -0400126 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 Hrubis0f0e3482014-02-27 16:08:04 +0100131 case TST_V9FS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000132 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100133 "Cannot do utime on a file on %s filesystem",
134 tst_fs_type_name(type));
Xiong Zhoud623e2c2014-09-25 03:24:19 -0400135 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 }
Cyril Hrubisbc5da682011-02-24 20:04:44 +0100137
robbiewd34d5812005-07-11 22:28:09 +0000138 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000139
140 if (pid == -1) {
141 tst_brkm(TBROK, cleanup, "fork() failed");
142 } else if (pid == 0) {
143 if ((ltpuser = getpwnam(LTPUSER1)) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000144 tst_brkm(TBROK, cleanup, "%s not found in /etc/passwd",
145 LTPUSER1);
plars865695b2001-08-27 22:15:12 +0000146 }
147
148 /* get uid/gid of user accordingly */
149 user_uid = ltpuser->pw_uid;
150
151 seteuid(user_uid);
152
plars865695b2001-08-27 22:15:12 +0000153 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800154
Caspar Zhangd59a6592013-03-07 14:59:12 +0800155 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000156
subrata_modak4bb656a2009-02-26 12:02:09 +0000157 /*
plars865695b2001-08-27 22:15:12 +0000158 * Invoke utime(2) to set TEMP_FILE access and
159 * modification times to the current time.
160 */
161 TEST(utime(TEMP_FILE, NULL));
162
plars865695b2001-08-27 22:15:12 +0000163 if (TEST_RETURN == -1) {
Nicolas Jolyb217f802015-11-28 10:26:14 +0100164 tst_resm(TFAIL|TTERRNO,
165 "utime(%s) failed", TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000166 } else {
167 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200168 * Sleep for a second so that mod time
169 * and access times will be different
170 * from the current time.
plars865695b2001-08-27 22:15:12 +0000171 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200172 sleep(2);
plars865695b2001-08-27 22:15:12 +0000173
Cyril Hrubise38b9612014-06-02 17:20:57 +0200174 /*
175 * Get the current time now, after
176 * calling utime(2)
177 */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100178 pres_time = time(NULL);
plars865695b2001-08-27 22:15:12 +0000179
Cyril Hrubise38b9612014-06-02 17:20:57 +0200180 /*
181 * Get the modification and access
182 * times of temporary file using
183 * stat(2).
184 */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100185 SAFE_STAT(cleanup, TEMP_FILE, &stat_buf);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200186 modf_time = stat_buf.st_mtime;
187 access_time = stat_buf.st_atime;
subrata_modakbdbaec52009-02-26 12:14:51 +0000188
Cyril Hrubise38b9612014-06-02 17:20:57 +0200189 /* 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);
plars865695b2001-08-27 22:15:12 +0000197 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200198 tst_resm(TPASS, "Functionality "
199 "of utime(%s, NULL) "
200 "successful",
201 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000202 }
203 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800204 tst_count++; /* incr. TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800205 }
plars865695b2001-08-27 22:15:12 +0000206 } 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 }
plars865695b2001-08-27 22:15:12 +0000216
Garrett Cooper2c282152010-12-16 00:55:50 -0800217 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800218 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800219
220}
plars865695b2001-08-27 22:15:12 +0000221
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
plars5a351662001-08-31 20:01:52 +0000227 * Change the ownership of testfile to that of "bin" user.
plars865695b2001-08-27 22:15:12 +0000228 * Record the current time.
229 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400230void setup(void)
plars865695b2001-08-27 22:15:12 +0000231{
subrata_modak56207ce2009-03-23 13:35:39 +0000232 int fildes; /* file handle for temp file */
plars5a351662001-08-31 20:01:52 +0000233 char *tmpd = NULL;
plars865695b2001-08-27 22:15:12 +0000234
Cyril Hrubisd1e794d2015-07-30 23:52:51 +0200235 tst_require_root();
plars865695b2001-08-27 22:15:12 +0000236
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200237 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000238
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
plars865695b2001-08-27 22:15:12 +0000246 tst_tmpdir();
247
subrata_modak56207ce2009-03-23 13:35:39 +0000248 /* get the name of the temporary directory */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100249 tmpd = SAFE_GETCWD(NULL, tmpd, 0);
plars5a351662001-08-31 20:01:52 +0000250
plars865695b2001-08-27 22:15:12 +0000251 /* Creat a temporary file under above directory */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100252 fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE);
plars865695b2001-08-27 22:15:12 +0000253
254 /* Close the temporary file created */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100255 SAFE_CLOSE(cleanup, fildes);
plars865695b2001-08-27 22:15:12 +0000256
257 /*
258 * Make sure that specified Mode permissions set as
259 * umask value may be different.
260 */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100261 SAFE_CHMOD(cleanup, TEMP_FILE, FILE_MODE);
262 SAFE_CHMOD(cleanup, tmpd, 0711);
plars865695b2001-08-27 22:15:12 +0000263
Nicolas Jolyb217f802015-11-28 10:26:14 +0100264 ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER2);
plars865695b2001-08-27 22:15:12 +0000265
266 /* get uid/gid of user accordingly */
267 user_uid = ltpuser->pw_uid;
268 group_gid = ltpuser->pw_gid;
269
subrata_modak56207ce2009-03-23 13:35:39 +0000270 /*
271 * Change the ownership of test directory/file specified by
272 * pathname to that of user_uid and group_gid.
273 */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100274 SAFE_CHOWN(cleanup, TEMP_FILE, user_uid, group_gid);
subrata_modak56207ce2009-03-23 13:35:39 +0000275
plars865695b2001-08-27 22:15:12 +0000276 /* Get the current time */
Nicolas Jolyb217f802015-11-28 10:26:14 +0100277 curr_time = time(NULL);
plars865695b2001-08-27 22:15:12 +0000278
279 /*
280 * Sleep for a second so that mod time and access times will be
281 * different from the current time
282 */
subrata_modak56207ce2009-03-23 13:35:39 +0000283 sleep(2); /* sleep(1) on IA64 sometimes sleeps < 1 sec!! */
284
Garrett Cooper2c282152010-12-16 00:55:50 -0800285}
plars865695b2001-08-27 22:15:12 +0000286
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 Frysingerc57fba52014-04-09 18:56:30 -0400293void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000294{
295 seteuid(0);
plars865695b2001-08-27 22:15:12 +0000296
plars865695b2001-08-27 22:15:12 +0000297 tst_rmdir();
298
Chris Dearmanec6edca2012-10-17 19:54:01 -0700299}