blob: 613a7d8deb6530c83facae63963ab010d04ad33e [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: utime02
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 owned by the user ID of the process.
30 *
31 * Expected Result:
32 * utime succeeds returning zero and sets the access and modification
33 * times of the file to the current time.
34 *
35 * Algorithm:
36 * Setup:
37 * Setup signal handling.
38 * Create temporary directory.
39 * Pause for SIGUSR1 if option specified.
40 *
41 * Test:
42 * Loop if the proper options are given.
43 * Execute system call
44 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000046 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000047 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000048 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000049 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000050 * Otherwise,
51 * Issue Functionality-Fail message.
52 * Cleanup:
53 * Print errno log and/or timing stats if options given
54 * Delete the temporary directory created.
55 *
56 * Usage: <for command-line>
57 * utime02 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
58 * where, -c n : Run n copies concurrently.
59 * -e : Turn on errno logging.
60 * -f : Turn off functionality Testing.
61 * -i n : Execute test n times.
62 * -I x : Execute test for x seconds.
63 * -P x : Pause for x seconds between iterations.
64 * -t : Turn on syscall timing.
65 *
66 * History
67 * 07/2001 John George
68 * -Ported
69 *
70 * Restrictions:
plars865695b2001-08-27 22:15:12 +000071 *
72 */
73
74#include <stdio.h>
75#include <unistd.h>
76#include <sys/types.h>
77#include <errno.h>
78#include <fcntl.h>
79#include <utime.h>
80#include <string.h>
81#include <sys/stat.h>
82#include <signal.h>
robbiewceb574a2001-08-28 16:15:56 +000083#include <pwd.h>
plars74948ad2002-11-14 16:16:14 +000084#include <time.h>
plars865695b2001-08-27 22:15:12 +000085
86#include "test.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010087#include "tst_fs_type.h"
plars865695b2001-08-27 22:15:12 +000088
89#define TEMP_FILE "tmp_file"
90#define FILE_MODE S_IRUSR | S_IRGRP | S_IROTH
91
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020092char *TCID = "utime02";
93int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000094time_t curr_time; /* current time in seconds */
Cyril Hrubis605fa332015-02-04 13:11:20 +010095time_t tloc;
Wanlong Gao354ebb42012-12-07 10:10:04 +080096
robbiewceb574a2001-08-28 16:15:56 +000097char nobody_uid[] = "nobody";
98struct passwd *ltpuser;
99
plars865695b2001-08-27 22:15:12 +0000100void setup(); /* Main setup function of test */
101void cleanup(); /* cleanup function for the test */
102
subrata_modak56207ce2009-03-23 13:35:39 +0000103int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000104{
105 struct stat stat_buf; /* struct buffer to hold file info. */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200106 int lc;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100107 long type;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200108 const char *msg;
plars865695b2001-08-27 22:15:12 +0000109 time_t modf_time, access_time;
110 time_t pres_time; /* file modification/access/present time */
subrata_modak56207ce2009-03-23 13:35:39 +0000111
Garrett Cooper45e285d2010-11-22 12:19:25 -0800112 msg = parse_opts(ac, av, NULL, NULL);
113 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000114 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800115
plars865695b2001-08-27 22:15:12 +0000116 }
117
plars865695b2001-08-27 22:15:12 +0000118 setup();
119
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100120 switch ((type = tst_fs_type(cleanup, "."))) {
121 case TST_NFS_MAGIC:
Xiong Zhoud623e2c2014-09-25 03:24:19 -0400122 if (tst_kvercmp(2, 6, 18) < 0)
123 tst_brkm(TCONF, cleanup, "Cannot do utime on a file"
124 " on %s filesystem before 2.6.18",
125 tst_fs_type_name(type));
126 break;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100127 case TST_V9FS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000128 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100129 "Cannot do utime on a file on %s filesystem",
130 tst_fs_type_name(type));
Xiong Zhoud623e2c2014-09-25 03:24:19 -0400131 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 }
Cyril Hrubisbc5da682011-02-24 20:04:44 +0100133
plars865695b2001-08-27 22:15:12 +0000134 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800135
Caspar Zhangd59a6592013-03-07 14:59:12 +0800136 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000137
subrata_modak4bb656a2009-02-26 12:02:09 +0000138 /*
plars865695b2001-08-27 22:15:12 +0000139 * Invoke utime(2) to set TEMP_FILE access and
140 * modification times to the current time.
141 */
142 TEST(utime(TEMP_FILE, NULL));
143
plars865695b2001-08-27 22:15:12 +0000144 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000145 tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s",
146 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
147 } else {
148 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200149 * Sleep for a second so that mod time and
150 * access times will be different from the
151 * current time
plars865695b2001-08-27 22:15:12 +0000152 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200153 sleep(2);
plars865695b2001-08-27 22:15:12 +0000154
Cyril Hrubise38b9612014-06-02 17:20:57 +0200155 /*
156 * Get the current time now, after calling
157 * utime(2)
158 */
159 if ((pres_time = time(&tloc)) < 0) {
160 tst_brkm(TFAIL, cleanup, "time() "
161 "failed to get present time "
162 "after utime, error=%d",
163 errno);
164 }
plars865695b2001-08-27 22:15:12 +0000165
Cyril Hrubise38b9612014-06-02 17:20:57 +0200166 /*
167 * Get the modification and access times of
168 * temporary file using stat(2).
169 */
170 if (stat(TEMP_FILE, &stat_buf) < 0) {
171 tst_brkm(TFAIL, cleanup, "stat(2) of "
172 "%s failed, errno:%d",
173 TEMP_FILE, TEST_ERRNO);
174 }
175 modf_time = stat_buf.st_mtime;
176 access_time = stat_buf.st_atime;
plars865695b2001-08-27 22:15:12 +0000177
Cyril Hrubise38b9612014-06-02 17:20:57 +0200178 /* Now do the actual verification */
179 if (modf_time <= curr_time ||
180 modf_time >= pres_time ||
181 access_time <= curr_time ||
182 access_time >= pres_time) {
183 tst_resm(TFAIL, "%s access and "
184 "modification times not set",
185 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000186 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200187 tst_resm(TPASS, "Functionality of "
188 "utime(%s, NULL) successful",
189 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000190 }
191 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800192 tst_count++;
Garrett Cooper2c282152010-12-16 00:55:50 -0800193 }
plars865695b2001-08-27 22:15:12 +0000194
plars865695b2001-08-27 22:15:12 +0000195 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800196 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800197}
plars865695b2001-08-27 22:15:12 +0000198
199/*
200 * void
201 * setup() - performs all ONE TIME setup for this test.
202 * Create a temporary directory and change directory to it.
203 * Create a test file under temporary directory and close it
204 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400205void setup(void)
plars865695b2001-08-27 22:15:12 +0000206{
subrata_modak56207ce2009-03-23 13:35:39 +0000207 int fildes; /* file handle for temp file */
plars865695b2001-08-27 22:15:12 +0000208
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200209 tst_require_root(NULL);
210
plars865695b2001-08-27 22:15:12 +0000211 tst_sig(FORK, DEF_HANDLER, cleanup);
212
robbiewceb574a2001-08-28 16:15:56 +0000213 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000214 ltpuser = getpwnam(nobody_uid);
215 if (setuid(ltpuser->pw_uid) == -1) {
216 tst_resm(TINFO, "setuid failed to "
217 "to set the effective uid to %d", ltpuser->pw_uid);
218 perror("setuid");
219 }
plars865695b2001-08-27 22:15:12 +0000220
221 /* Pause if that option was specified
222 * TEST_PAUSE contains the code to fork the test with the -i option.
223 * You want to make sure you do this before you create your temporary
224 * directory.
225 */
226 TEST_PAUSE;
227
plars865695b2001-08-27 22:15:12 +0000228 tst_tmpdir();
229
230 /* Creat a temporary file under above directory */
231 if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) {
232 tst_brkm(TBROK, cleanup,
233 "creat(%s, %#o) Failed, errno=%d :%s",
234 TEMP_FILE, FILE_MODE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 }
plars865695b2001-08-27 22:15:12 +0000236
237 /* Close the temporary file created */
238 if (close(fildes) < 0) {
239 tst_brkm(TBROK, cleanup,
240 "close(%s) Failed, errno=%d : %s:",
241 TEMP_FILE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800242 }
plars865695b2001-08-27 22:15:12 +0000243
244 /* Get the current time */
245 if ((curr_time = time(&tloc)) < 0) {
246 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000247 "time() failed to get current time, errno=%d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800248 }
plars865695b2001-08-27 22:15:12 +0000249
250 /*
251 * Sleep for a second so that mod time and access times will be
252 * different from the current time
253 */
subrata_modak56207ce2009-03-23 13:35:39 +0000254 sleep(2); /* sleep(1) on IA64 sometimes sleeps < 1 sec!! */
255
Garrett Cooper2c282152010-12-16 00:55:50 -0800256}
plars865695b2001-08-27 22:15:12 +0000257
258/*
259 * void
260 * cleanup() - performs all ONE TIME cleanup for this test at
261 * completion or premature exit.
262 * Remove the test directory and testfile created in the setup.
263 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400264void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000265{
plars865695b2001-08-27 22:15:12 +0000266
plars865695b2001-08-27 22:15:12 +0000267 tst_rmdir();
268
Chris Dearmanec6edca2012-10-17 19:54:01 -0700269}