1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2865695bbc89088b9526ea9045410e5afb70a985cplars *
3865695bbc89088b9526ea9045410e5afb70a985cplars *   Copyright (c) International Business Machines  Corp., 2001
4865695bbc89088b9526ea9045410e5afb70a985cplars *
5865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is free software;  you can redistribute it and/or modify
6865695bbc89088b9526ea9045410e5afb70a985cplars *   it under the terms of the GNU General Public License as published by
7865695bbc89088b9526ea9045410e5afb70a985cplars *   the Free Software Foundation; either version 2 of the License, or
8865695bbc89088b9526ea9045410e5afb70a985cplars *   (at your option) any later version.
9865695bbc89088b9526ea9045410e5afb70a985cplars *
10865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is distributed in the hope that it will be useful,
11865695bbc89088b9526ea9045410e5afb70a985cplars *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12865695bbc89088b9526ea9045410e5afb70a985cplars *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13865695bbc89088b9526ea9045410e5afb70a985cplars *   the GNU General Public License for more details.
14865695bbc89088b9526ea9045410e5afb70a985cplars *
15865695bbc89088b9526ea9045410e5afb70a985cplars *   You should have received a copy of the GNU General Public License
16865695bbc89088b9526ea9045410e5afb70a985cplars *   along with this program;  if not, write to the Free Software
174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18865695bbc89088b9526ea9045410e5afb70a985cplars */
19865695bbc89088b9526ea9045410e5afb70a985cplars
20865695bbc89088b9526ea9045410e5afb70a985cplars/*
21865695bbc89088b9526ea9045410e5afb70a985cplars * Test Name: utime01
22865695bbc89088b9526ea9045410e5afb70a985cplars *
23865695bbc89088b9526ea9045410e5afb70a985cplars * Test Description:
24865695bbc89088b9526ea9045410e5afb70a985cplars *  Verify that the system call utime() successfully sets the modification
25865695bbc89088b9526ea9045410e5afb70a985cplars *  and access times of a file to the current time, if the times argument
26865695bbc89088b9526ea9045410e5afb70a985cplars *  is null, and the user ID of the process is "root".
27865695bbc89088b9526ea9045410e5afb70a985cplars *
28865695bbc89088b9526ea9045410e5afb70a985cplars * Expected Result:
29865695bbc89088b9526ea9045410e5afb70a985cplars *  utime succeeds returning zero and sets the access and modification
30865695bbc89088b9526ea9045410e5afb70a985cplars *  times of the file to the current time.
31865695bbc89088b9526ea9045410e5afb70a985cplars *
32865695bbc89088b9526ea9045410e5afb70a985cplars * Algorithm:
33865695bbc89088b9526ea9045410e5afb70a985cplars *  Setup:
34865695bbc89088b9526ea9045410e5afb70a985cplars *   Setup signal handling.
35865695bbc89088b9526ea9045410e5afb70a985cplars *   Create temporary directory.
36865695bbc89088b9526ea9045410e5afb70a985cplars *   Pause for SIGUSR1 if option specified.
37865695bbc89088b9526ea9045410e5afb70a985cplars *
38865695bbc89088b9526ea9045410e5afb70a985cplars *  Test:
39865695bbc89088b9526ea9045410e5afb70a985cplars *   Loop if the proper options are given.
40865695bbc89088b9526ea9045410e5afb70a985cplars *   Execute system call
41865695bbc89088b9526ea9045410e5afb70a985cplars *   Check return code, if system call failed (return=-1)
4256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	Log the errno and Issue a FAIL message.
43865695bbc89088b9526ea9045410e5afb70a985cplars *   Otherwise,
4456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	Verify the Functionality of system call
45865695bbc89088b9526ea9045410e5afb70a985cplars *      if successful,
4656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *		Issue Functionality-Pass message.
47865695bbc89088b9526ea9045410e5afb70a985cplars *      Otherwise,
48865695bbc89088b9526ea9045410e5afb70a985cplars *		Issue Functionality-Fail message.
49865695bbc89088b9526ea9045410e5afb70a985cplars *  Cleanup:
50865695bbc89088b9526ea9045410e5afb70a985cplars *   Print errno log and/or timing stats if options given
51865695bbc89088b9526ea9045410e5afb70a985cplars *   Delete the temporary directory created.
52865695bbc89088b9526ea9045410e5afb70a985cplars *
53865695bbc89088b9526ea9045410e5afb70a985cplars * Usage:  <for command-line>
54865695bbc89088b9526ea9045410e5afb70a985cplars *  utime01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
55865695bbc89088b9526ea9045410e5afb70a985cplars *		where,  -c n : Run n copies concurrently.
56865695bbc89088b9526ea9045410e5afb70a985cplars *			-e   : Turn on errno logging.
57865695bbc89088b9526ea9045410e5afb70a985cplars *			-f   : Turn off functionality Testing.
58865695bbc89088b9526ea9045410e5afb70a985cplars *			-i n : Execute test n times.
59865695bbc89088b9526ea9045410e5afb70a985cplars *			-I x : Execute test for x seconds.
60865695bbc89088b9526ea9045410e5afb70a985cplars *			-P x : Pause for x seconds between iterations.
61865695bbc89088b9526ea9045410e5afb70a985cplars *			-t   : Turn on syscall timing.
62865695bbc89088b9526ea9045410e5afb70a985cplars *
63865695bbc89088b9526ea9045410e5afb70a985cplars * History
64865695bbc89088b9526ea9045410e5afb70a985cplars *	07/2001 John George
65865695bbc89088b9526ea9045410e5afb70a985cplars *		-Ported
66865695bbc89088b9526ea9045410e5afb70a985cplars *
67865695bbc89088b9526ea9045410e5afb70a985cplars * Restrictions:
68865695bbc89088b9526ea9045410e5afb70a985cplars *  This test should be run by 'super-user' (root) only.
69865695bbc89088b9526ea9045410e5afb70a985cplars *
70865695bbc89088b9526ea9045410e5afb70a985cplars */
71865695bbc89088b9526ea9045410e5afb70a985cplars
72865695bbc89088b9526ea9045410e5afb70a985cplars#include <stdio.h>
73865695bbc89088b9526ea9045410e5afb70a985cplars#include <unistd.h>
74865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/types.h>
75865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
76865695bbc89088b9526ea9045410e5afb70a985cplars#include <fcntl.h>
77865695bbc89088b9526ea9045410e5afb70a985cplars#include <utime.h>
78865695bbc89088b9526ea9045410e5afb70a985cplars#include <string.h>
79865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/stat.h>
80865695bbc89088b9526ea9045410e5afb70a985cplars#include <signal.h>
8174948ad801d4aba1c872652580b8b331e41aefcaplars#include <time.h>
82865695bbc89088b9526ea9045410e5afb70a985cplars
83865695bbc89088b9526ea9045410e5afb70a985cplars#include "test.h"
84b217f807d027810d26d56720caac60be3ee1f890Nicolas Joly#include "safe_macros.h"
85865695bbc89088b9526ea9045410e5afb70a985cplars
86865695bbc89088b9526ea9045410e5afb70a985cplars#define TEMP_FILE	"tmp_file"
87865695bbc89088b9526ea9045410e5afb70a985cplars#define FILE_MODE	S_IRUSR | S_IRGRP | S_IROTH
88865695bbc89088b9526ea9045410e5afb70a985cplars
89fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "utime01";
90fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1;
91865695bbc89088b9526ea9045410e5afb70a985cplarstime_t curr_time;		/* current time in seconds */
92865695bbc89088b9526ea9045410e5afb70a985cplars
93865695bbc89088b9526ea9045410e5afb70a985cplarsvoid setup();			/* Main setup function of test */
94865695bbc89088b9526ea9045410e5afb70a985cplarsvoid cleanup();			/* cleanup function for the test */
95865695bbc89088b9526ea9045410e5afb70a985cplars
9656207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av)
97865695bbc89088b9526ea9045410e5afb70a985cplars{
98865695bbc89088b9526ea9045410e5afb70a985cplars	struct stat stat_buf;	/* struct buffer to hold file info. */
9989af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
1000f0e3487c46947c6eee21cce1c77aa7e2e3b302aCyril Hrubis	long type;
101865695bbc89088b9526ea9045410e5afb70a985cplars	time_t modf_time, access_time;
102865695bbc89088b9526ea9045410e5afb70a985cplars	time_t pres_time;	/* file modification/access/present time */
10356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
104d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
105865695bbc89088b9526ea9045410e5afb70a985cplars
106865695bbc89088b9526ea9045410e5afb70a985cplars	setup();
107865695bbc89088b9526ea9045410e5afb70a985cplars
1080f0e3487c46947c6eee21cce1c77aa7e2e3b302aCyril Hrubis	switch ((type = tst_fs_type(cleanup, "."))) {
1090f0e3487c46947c6eee21cce1c77aa7e2e3b302aCyril Hrubis	case TST_NFS_MAGIC:
110d623e2c7fe59075bcf0dbf737af6d6620f1b83dfXiong Zhou		if (tst_kvercmp(2, 6, 18) < 0)
111d623e2c7fe59075bcf0dbf737af6d6620f1b83dfXiong Zhou			tst_brkm(TCONF, cleanup, "Cannot do utime on a file"
112d623e2c7fe59075bcf0dbf737af6d6620f1b83dfXiong Zhou				" on %s filesystem before 2.6.18",
113d623e2c7fe59075bcf0dbf737af6d6620f1b83dfXiong Zhou				 tst_fs_type_name(type));
114d623e2c7fe59075bcf0dbf737af6d6620f1b83dfXiong Zhou		break;
1150f0e3487c46947c6eee21cce1c77aa7e2e3b302aCyril Hrubis	case TST_V9FS_MAGIC:
11656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_brkm(TCONF, cleanup,
1170f0e3487c46947c6eee21cce1c77aa7e2e3b302aCyril Hrubis			 "Cannot do utime on a file on %s filesystem",
1180f0e3487c46947c6eee21cce1c77aa7e2e3b302aCyril Hrubis			 tst_fs_type_name(type));
119d623e2c7fe59075bcf0dbf737af6d6620f1b83dfXiong Zhou		break;
120354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	}
121bc5da68248cc963e17862b7a0c556409c29c763eCyril Hrubis
122865695bbc89088b9526ea9045410e5afb70a985cplars	for (lc = 0; TEST_LOOPING(lc); lc++) {
1232c28215423293e443469a07ae7011135d058b671Garrett Cooper
124d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
125865695bbc89088b9526ea9045410e5afb70a985cplars
1264bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak		/*
127865695bbc89088b9526ea9045410e5afb70a985cplars		 * Invoke utime(2) to set TEMP_FILE access and
128865695bbc89088b9526ea9045410e5afb70a985cplars		 * modification times to the current time.
129865695bbc89088b9526ea9045410e5afb70a985cplars		 */
130865695bbc89088b9526ea9045410e5afb70a985cplars		TEST(utime(TEMP_FILE, NULL));
131865695bbc89088b9526ea9045410e5afb70a985cplars
132865695bbc89088b9526ea9045410e5afb70a985cplars		if (TEST_RETURN == -1) {
133b217f807d027810d26d56720caac60be3ee1f890Nicolas Joly			tst_resm(TFAIL|TTERRNO, "utime(%s) failed", TEMP_FILE);
134865695bbc89088b9526ea9045410e5afb70a985cplars		} else {
135865695bbc89088b9526ea9045410e5afb70a985cplars			/*
136e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 * Sleep for a second so that mod time and
137e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 * access times will be different from the
138e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 * current time
139865695bbc89088b9526ea9045410e5afb70a985cplars			 */
140e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			sleep(2);
141e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis
142e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			/*
143e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 * Get the current time now, after calling
144e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 * utime(2)
145e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 */
146b217f807d027810d26d56720caac60be3ee1f890Nicolas Joly			pres_time = time(NULL);
147e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis
148e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			/*
149e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 * Get the modification and access times of
150e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 * temporary file using stat(2).
151e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 */
152b217f807d027810d26d56720caac60be3ee1f890Nicolas Joly			SAFE_STAT(cleanup, TEMP_FILE, &stat_buf);
153e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			modf_time = stat_buf.st_mtime;
154e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			access_time = stat_buf.st_atime;
155e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis
156e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			/* Now do the actual verification */
157e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			if (modf_time <= curr_time ||
158e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			    modf_time >= pres_time ||
159e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			    access_time <= curr_time ||
160e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			    access_time >= pres_time) {
161e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				tst_resm(TFAIL, "%s access and "
162e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "modification times not set",
163e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 TEMP_FILE);
164865695bbc89088b9526ea9045410e5afb70a985cplars			} else {
165e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				tst_resm(TPASS, "Functionality of "
166e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "utime(%s, NULL) successful",
167e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 TEMP_FILE);
168865695bbc89088b9526ea9045410e5afb70a985cplars			}
169865695bbc89088b9526ea9045410e5afb70a985cplars		}
170d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count++;
1712c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
172865695bbc89088b9526ea9045410e5afb70a985cplars
173865695bbc89088b9526ea9045410e5afb70a985cplars	cleanup();
1747d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	tst_exit();
1752c28215423293e443469a07ae7011135d058b671Garrett Cooper}
176865695bbc89088b9526ea9045410e5afb70a985cplars
177865695bbc89088b9526ea9045410e5afb70a985cplars/*
178865695bbc89088b9526ea9045410e5afb70a985cplars * void
179865695bbc89088b9526ea9045410e5afb70a985cplars * setup() - performs all ONE TIME setup for this test.
180865695bbc89088b9526ea9045410e5afb70a985cplars *  Create a temporary directory and change directory to it.
181865695bbc89088b9526ea9045410e5afb70a985cplars *  Create a test file under temporary directory and close it
182865695bbc89088b9526ea9045410e5afb70a985cplars */
183c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid setup(void)
184865695bbc89088b9526ea9045410e5afb70a985cplars{
18556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	int fildes;		/* file handle for temp file */
186865695bbc89088b9526ea9045410e5afb70a985cplars
187d1e794d62b1bf619df8390535e4c2a58899b1145Cyril Hrubis	tst_require_root();
188865695bbc89088b9526ea9045410e5afb70a985cplars
189d4ceb37d3ab506483612ef0ad74c88e5828a9779Nicolas Joly	tst_sig(FORK, DEF_HANDLER, cleanup);
190865695bbc89088b9526ea9045410e5afb70a985cplars
191865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_PAUSE;
192865695bbc89088b9526ea9045410e5afb70a985cplars
193865695bbc89088b9526ea9045410e5afb70a985cplars	tst_tmpdir();
194865695bbc89088b9526ea9045410e5afb70a985cplars
195865695bbc89088b9526ea9045410e5afb70a985cplars	/* Creat a temporary file under above directory */
196b217f807d027810d26d56720caac60be3ee1f890Nicolas Joly	fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE);
197865695bbc89088b9526ea9045410e5afb70a985cplars
198865695bbc89088b9526ea9045410e5afb70a985cplars	/* Close the temporary file created */
199b217f807d027810d26d56720caac60be3ee1f890Nicolas Joly	SAFE_CLOSE(cleanup, fildes);
200865695bbc89088b9526ea9045410e5afb70a985cplars
201865695bbc89088b9526ea9045410e5afb70a985cplars	/* Get the current time */
202b217f807d027810d26d56720caac60be3ee1f890Nicolas Joly	curr_time = time(NULL);
203865695bbc89088b9526ea9045410e5afb70a985cplars
204865695bbc89088b9526ea9045410e5afb70a985cplars	/*
205865695bbc89088b9526ea9045410e5afb70a985cplars	 * Sleep for a second so that mod time and access times will be
206865695bbc89088b9526ea9045410e5afb70a985cplars	 * different from the current time
207865695bbc89088b9526ea9045410e5afb70a985cplars	 */
20856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	sleep(2);		/* sleep(1) on IA64 sometimes sleeps < 1 sec!! */
20956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
2102c28215423293e443469a07ae7011135d058b671Garrett Cooper}
211865695bbc89088b9526ea9045410e5afb70a985cplars
212865695bbc89088b9526ea9045410e5afb70a985cplars/*
213865695bbc89088b9526ea9045410e5afb70a985cplars * void
214865695bbc89088b9526ea9045410e5afb70a985cplars * cleanup() - performs all ONE TIME cleanup for this test at
215865695bbc89088b9526ea9045410e5afb70a985cplars *             completion or premature exit.
216865695bbc89088b9526ea9045410e5afb70a985cplars *  Remove the test directory and testfile created in the setup.
217865695bbc89088b9526ea9045410e5afb70a985cplars */
218c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid cleanup(void)
219865695bbc89088b9526ea9045410e5afb70a985cplars{
220865695bbc89088b9526ea9045410e5afb70a985cplars
221865695bbc89088b9526ea9045410e5afb70a985cplars	tst_rmdir();
222865695bbc89088b9526ea9045410e5afb70a985cplars
223ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
224