blob: eb78d545349e10c33bf07248651b03a5bf036703 [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>
86#include <wait.h>
87#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"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010092#include "tst_fs_type.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 */
Cyril Hrubis605fa332015-02-04 13:11:20 +0100102time_t tloc;
plars865695b2001-08-27 22:15:12 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104struct passwd *ltpuser; /* password struct for ltpusers */
105uid_t user_uid; /* user id of ltpuser */
106gid_t group_gid; /* group id of ltpuser */
plars865695b2001-08-27 22:15:12 +0000107int status;
108
109void setup(); /* Main setup function of test */
110void cleanup(); /* cleanup function for the test */
111
subrata_modak56207ce2009-03-23 13:35:39 +0000112int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000113{
114 struct stat stat_buf; /* struct buffer to hold file info. */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200115 int lc;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100116 long type;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200117 const char *msg;
plars865695b2001-08-27 22:15:12 +0000118 time_t modf_time, access_time;
119 time_t pres_time; /* file modification/access/present time */
120 pid_t pid;
subrata_modak56207ce2009-03-23 13:35:39 +0000121
Garrett Cooper45e285d2010-11-22 12:19:25 -0800122 msg = parse_opts(ac, av, NULL, NULL);
123 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000124 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
plars865695b2001-08-27 22:15:12 +0000126 }
127
plars865695b2001-08-27 22:15:12 +0000128 setup();
129
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100130 switch ((type = tst_fs_type(cleanup, "."))) {
131 case TST_NFS_MAGIC:
Xiong Zhoud623e2c2014-09-25 03:24:19 -0400132 if (tst_kvercmp(2, 6, 18) < 0)
133 tst_brkm(TCONF, cleanup, "Cannot do utime on a file"
134 " on %s filesystem before 2.6.18",
135 tst_fs_type_name(type));
136 break;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100137 case TST_V9FS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000138 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100139 "Cannot do utime on a file on %s filesystem",
140 tst_fs_type_name(type));
Xiong Zhoud623e2c2014-09-25 03:24:19 -0400141 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 }
Cyril Hrubisbc5da682011-02-24 20:04:44 +0100143
robbiewd34d5812005-07-11 22:28:09 +0000144 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000145
146 if (pid == -1) {
147 tst_brkm(TBROK, cleanup, "fork() failed");
148 } else if (pid == 0) {
149 if ((ltpuser = getpwnam(LTPUSER1)) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000150 tst_brkm(TBROK, cleanup, "%s not found in /etc/passwd",
151 LTPUSER1);
plars865695b2001-08-27 22:15:12 +0000152 }
153
154 /* get uid/gid of user accordingly */
155 user_uid = ltpuser->pw_uid;
156
157 seteuid(user_uid);
158
plars865695b2001-08-27 22:15:12 +0000159 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800160
Caspar Zhangd59a6592013-03-07 14:59:12 +0800161 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000162
subrata_modak4bb656a2009-02-26 12:02:09 +0000163 /*
plars865695b2001-08-27 22:15:12 +0000164 * Invoke utime(2) to set TEMP_FILE access and
165 * modification times to the current time.
166 */
167 TEST(utime(TEMP_FILE, NULL));
168
plars865695b2001-08-27 22:15:12 +0000169 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000170 tst_resm(TFAIL,
subrata_modak56207ce2009-03-23 13:35:39 +0000171 "utime(%s) Failed, errno=%d : %s",
172 TEMP_FILE, TEST_ERRNO,
173 strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000174 } else {
175 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200176 * Sleep for a second so that mod time
177 * and access times will be different
178 * from the current time.
plars865695b2001-08-27 22:15:12 +0000179 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200180 sleep(2);
plars865695b2001-08-27 22:15:12 +0000181
Cyril Hrubise38b9612014-06-02 17:20:57 +0200182 /*
183 * Get the current time now, after
184 * calling utime(2)
185 */
186 if ((pres_time = time(&tloc)) < 0) {
187 tst_brkm(TFAIL, cleanup,
188 "time() failed to get "
189 "present time after "
190 "utime, error=%d",
191 errno);
192 }
plars865695b2001-08-27 22:15:12 +0000193
Cyril Hrubise38b9612014-06-02 17:20:57 +0200194 /*
195 * Get the modification and access
196 * times of temporary file using
197 * stat(2).
198 */
199 if (stat(TEMP_FILE, &stat_buf) < 0) {
200 tst_brkm(TFAIL, cleanup,
201 "stat(2) of %s failed, "
202 "error:%d", TEMP_FILE,
203 TEST_ERRNO);
204 }
205 modf_time = stat_buf.st_mtime;
206 access_time = stat_buf.st_atime;
subrata_modakbdbaec52009-02-26 12:14:51 +0000207
Cyril Hrubise38b9612014-06-02 17:20:57 +0200208 /* Now do the actual verification */
209 if (modf_time <= curr_time ||
210 modf_time >= pres_time ||
211 access_time <= curr_time ||
212 access_time >= pres_time) {
213 tst_resm(TFAIL, "%s access and "
214 "modification times "
215 "not set", TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000216 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200217 tst_resm(TPASS, "Functionality "
218 "of utime(%s, NULL) "
219 "successful",
220 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000221 }
222 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800223 tst_count++; /* incr. TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800224 }
plars865695b2001-08-27 22:15:12 +0000225 } else {
226 waitpid(pid, &status, 0);
227 _exit(0); /*
228 * Exit here and let the child clean up.
229 * This allows the errno information set
230 * by the TEST_ERROR_LOG macro and the
231 * PASS/FAIL status to be preserved for
232 * use during cleanup.
233 */
234 }
plars865695b2001-08-27 22:15:12 +0000235
Garrett Cooper2c282152010-12-16 00:55:50 -0800236 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800237 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800238
239}
plars865695b2001-08-27 22:15:12 +0000240
241/*
242 * void
243 * setup() - performs all ONE TIME setup for this test.
244 * Create a temporary directory and change directory to it.
245 * Create a test file under temporary directory and close it
plars5a351662001-08-31 20:01:52 +0000246 * Change the ownership of testfile to that of "bin" user.
plars865695b2001-08-27 22:15:12 +0000247 * Record the current time.
248 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400249void setup(void)
plars865695b2001-08-27 22:15:12 +0000250{
subrata_modak56207ce2009-03-23 13:35:39 +0000251 int fildes; /* file handle for temp file */
plars5a351662001-08-31 20:01:52 +0000252 char *tmpd = NULL;
plars865695b2001-08-27 22:15:12 +0000253
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200254 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000255
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200256 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000257
258 /* Pause if that option was specified
259 * TEST_PAUSE contains the code to fork the test with the -i option.
260 * You want to make sure you do this before you create your temporary
261 * directory.
262 */
263 TEST_PAUSE;
264
plars865695b2001-08-27 22:15:12 +0000265 tst_tmpdir();
266
subrata_modak56207ce2009-03-23 13:35:39 +0000267 /* get the name of the temporary directory */
268 if ((tmpd = getcwd(tmpd, 0)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800269 tst_brkm(TBROK, NULL, "getcwd failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000270 }
plars5a351662001-08-31 20:01:52 +0000271
plars865695b2001-08-27 22:15:12 +0000272 /* Creat a temporary file under above directory */
273 if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) {
274 tst_brkm(TBROK, cleanup,
275 "creat(%s, %#o) Failed, errno=%d :%s",
276 TEMP_FILE, FILE_MODE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800277 }
plars865695b2001-08-27 22:15:12 +0000278
279 /* Close the temporary file created */
280 if (close(fildes) < 0) {
281 tst_brkm(TBROK, cleanup,
282 "close(%s) Failed, errno=%d : %s:",
283 TEMP_FILE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800284 }
plars865695b2001-08-27 22:15:12 +0000285
286 /*
287 * Make sure that specified Mode permissions set as
288 * umask value may be different.
289 */
290 if (chmod(TEMP_FILE, FILE_MODE) < 0) {
291 tst_brkm(TBROK, cleanup,
292 "chmod(%s) Failed, errno=%d : %s:",
293 TEMP_FILE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800294 }
plars865695b2001-08-27 22:15:12 +0000295
subrata_modak56207ce2009-03-23 13:35:39 +0000296 if (chmod(tmpd, 0711) != 0) {
297 tst_brkm(TBROK, cleanup, "chmod() failed");
298 }
plars5a351662001-08-31 20:01:52 +0000299
plars865695b2001-08-27 22:15:12 +0000300 if ((ltpuser = getpwnam(LTPUSER2)) == NULL) {
vapiercff4af02006-02-11 04:46:30 +0000301 tst_brkm(TBROK, cleanup, "%s not found in /etc/passwd",
subrata_modak56207ce2009-03-23 13:35:39 +0000302 LTPUSER2);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800303 }
plars865695b2001-08-27 22:15:12 +0000304
305 /* get uid/gid of user accordingly */
306 user_uid = ltpuser->pw_uid;
307 group_gid = ltpuser->pw_gid;
308
subrata_modak56207ce2009-03-23 13:35:39 +0000309 /*
310 * Change the ownership of test directory/file specified by
311 * pathname to that of user_uid and group_gid.
312 */
313 if (chown(TEMP_FILE, user_uid, group_gid) < 0) {
vapiercff4af02006-02-11 04:46:30 +0000314 tst_brkm(TBROK, cleanup, "chown() of %s failed, error %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000315 TEMP_FILE, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800316 }
subrata_modak56207ce2009-03-23 13:35:39 +0000317
plars865695b2001-08-27 22:15:12 +0000318 /* Get the current time */
319 if ((curr_time = time(&tloc)) < 0) {
320 tst_brkm(TBROK, cleanup,
321 "time() failed to get current time, errno=%d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800322 }
plars865695b2001-08-27 22:15:12 +0000323
324 /*
325 * Sleep for a second so that mod time and access times will be
326 * different from the current time
327 */
subrata_modak56207ce2009-03-23 13:35:39 +0000328 sleep(2); /* sleep(1) on IA64 sometimes sleeps < 1 sec!! */
329
Garrett Cooper2c282152010-12-16 00:55:50 -0800330}
plars865695b2001-08-27 22:15:12 +0000331
332/*
333 * void
334 * cleanup() - performs all ONE TIME cleanup for this test at
335 * completion or premature exit.
336 * Remove the test directory and testfile created in the setup.
337 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400338void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000339{
340 seteuid(0);
plars865695b2001-08-27 22:15:12 +0000341
plars865695b2001-08-27 22:15:12 +0000342 tst_rmdir();
343
Chris Dearmanec6edca2012-10-17 19:54:01 -0700344}