utime04.c revision 354ebb48db8e66a853a58379a4808d5dcd1ceac3
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: utime04
22865695bbc89088b9526ea9045410e5afb70a985cplars *
23865695bbc89088b9526ea9045410e5afb70a985cplars * Test Description:
24865695bbc89088b9526ea9045410e5afb70a985cplars *  Verify that the system call utime() successfully sets the modification
25865695bbc89088b9526ea9045410e5afb70a985cplars *  and access times of a file to the time specified by times argument, if
26865695bbc89088b9526ea9045410e5afb70a985cplars *  the times argument is not null, and the user ID of the process is "root".
27865695bbc89088b9526ea9045410e5afb70a985cplars *
28865695bbc89088b9526ea9045410e5afb70a985cplars * Expected Result:
294bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *   utime succeeds returning zero and sets the access and modification
30865695bbc89088b9526ea9045410e5afb70a985cplars *   times of the file to that specified by the times argument.
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 *  utime04 [-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.
5756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *		-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 <sys/types.h>
74865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
75865695bbc89088b9526ea9045410e5afb70a985cplars#include <unistd.h>
76865695bbc89088b9526ea9045410e5afb70a985cplars#include <fcntl.h>
77865695bbc89088b9526ea9045410e5afb70a985cplars#include <utime.h>
78865695bbc89088b9526ea9045410e5afb70a985cplars#include <string.h>
79865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/stat.h>
80865695bbc89088b9526ea9045410e5afb70a985cplars#include <signal.h>
81865695bbc89088b9526ea9045410e5afb70a985cplars
82865695bbc89088b9526ea9045410e5afb70a985cplars#include "test.h"
83865695bbc89088b9526ea9045410e5afb70a985cplars#include "usctest.h"
84865695bbc89088b9526ea9045410e5afb70a985cplars
85865695bbc89088b9526ea9045410e5afb70a985cplars#define TEMP_FILE	"tmp_file"
86865695bbc89088b9526ea9045410e5afb70a985cplars#define FILE_MODE	S_IRUSR | S_IRGRP | S_IROTH
87865695bbc89088b9526ea9045410e5afb70a985cplars#define NEW_TIME	10000
88865695bbc89088b9526ea9045410e5afb70a985cplars
8956207cec7732e09c216c751c0b5f88a242bacae6subrata_modakchar *TCID = "utime04";		/* Test program identifier.    */
9056207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint TST_TOTAL = 1;		/* Total number of test cases. */
9156207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint exp_enos[] = { 0 };
92354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao
93865695bbc89088b9526ea9045410e5afb70a985cplarsstruct utimbuf times;		/* struct. buffer for utime() */
94865695bbc89088b9526ea9045410e5afb70a985cplars
95865695bbc89088b9526ea9045410e5afb70a985cplarsvoid setup();			/* Main setup function of test */
96865695bbc89088b9526ea9045410e5afb70a985cplarsvoid cleanup();			/* cleanup function for the test */
97865695bbc89088b9526ea9045410e5afb70a985cplars
9856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av)
99865695bbc89088b9526ea9045410e5afb70a985cplars{
100865695bbc89088b9526ea9045410e5afb70a985cplars	struct stat stat_buf;	/* struct buffer to hold file info. */
10189af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
10289af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	char *msg;
103865695bbc89088b9526ea9045410e5afb70a985cplars	time_t modf_time, access_time;
10456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* file modification/access time */
10556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
106865695bbc89088b9526ea9045410e5afb70a985cplars	/* Parse standard options given to run the test. */
10745e285d46ab47b0ff76c88acb5ba97b0bd5f753dGarrett Cooper	msg = parse_opts(ac, av, NULL, NULL);
10845e285d46ab47b0ff76c88acb5ba97b0bd5f753dGarrett Cooper	if (msg != NULL) {
109865695bbc89088b9526ea9045410e5afb70a985cplars		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
1102c28215423293e443469a07ae7011135d058b671Garrett Cooper
111865695bbc89088b9526ea9045410e5afb70a985cplars	}
112865695bbc89088b9526ea9045410e5afb70a985cplars
113865695bbc89088b9526ea9045410e5afb70a985cplars	setup();
114865695bbc89088b9526ea9045410e5afb70a985cplars
115865695bbc89088b9526ea9045410e5afb70a985cplars	/* set the expected errnos... */
116865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_EXP_ENOS(exp_enos);
117865695bbc89088b9526ea9045410e5afb70a985cplars
118865695bbc89088b9526ea9045410e5afb70a985cplars	for (lc = 0; TEST_LOOPING(lc); lc++) {
1192c28215423293e443469a07ae7011135d058b671Garrett Cooper
12056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		Tst_count = 0;
121865695bbc89088b9526ea9045410e5afb70a985cplars
1224bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak		/*
123865695bbc89088b9526ea9045410e5afb70a985cplars		 * Invoke utime(2) to set TEMP_FILE access and
124865695bbc89088b9526ea9045410e5afb70a985cplars		 * modification times to that specified by
125865695bbc89088b9526ea9045410e5afb70a985cplars		 * times argument.
126865695bbc89088b9526ea9045410e5afb70a985cplars		 */
127865695bbc89088b9526ea9045410e5afb70a985cplars		TEST(utime(TEMP_FILE, &times));
128865695bbc89088b9526ea9045410e5afb70a985cplars
129865695bbc89088b9526ea9045410e5afb70a985cplars		if (TEST_RETURN == -1) {
130865695bbc89088b9526ea9045410e5afb70a985cplars			TEST_ERROR_LOG(TEST_ERRNO);
131865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s",
132865695bbc89088b9526ea9045410e5afb70a985cplars				 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
133865695bbc89088b9526ea9045410e5afb70a985cplars		} else {
134865695bbc89088b9526ea9045410e5afb70a985cplars			/*
135865695bbc89088b9526ea9045410e5afb70a985cplars			 * Perform functional verification if test
136865695bbc89088b9526ea9045410e5afb70a985cplars			 * executed without (-f) option.
137865695bbc89088b9526ea9045410e5afb70a985cplars			 */
138865695bbc89088b9526ea9045410e5afb70a985cplars			if (STD_FUNCTIONAL_TEST) {
139865695bbc89088b9526ea9045410e5afb70a985cplars				/*
140865695bbc89088b9526ea9045410e5afb70a985cplars				 * Get the modification and access times of
141865695bbc89088b9526ea9045410e5afb70a985cplars				 * temporary file using stat(2).
142865695bbc89088b9526ea9045410e5afb70a985cplars				 */
143865695bbc89088b9526ea9045410e5afb70a985cplars				if (stat(TEMP_FILE, &stat_buf) < 0) {
144865695bbc89088b9526ea9045410e5afb70a985cplars					tst_brkm(TFAIL, cleanup,
14556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 "stat(2) of %s failed, "
14656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 "error:%d", TEMP_FILE,
14756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 TEST_ERRNO);
148354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				}
149865695bbc89088b9526ea9045410e5afb70a985cplars				modf_time = stat_buf.st_mtime;
150865695bbc89088b9526ea9045410e5afb70a985cplars				access_time = stat_buf.st_atime;
151865695bbc89088b9526ea9045410e5afb70a985cplars
152865695bbc89088b9526ea9045410e5afb70a985cplars				/* Now do the actual verification */
15356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				if ((modf_time != NEW_TIME) ||
154865695bbc89088b9526ea9045410e5afb70a985cplars				    (access_time != NEW_TIME)) {
155865695bbc89088b9526ea9045410e5afb70a985cplars					tst_resm(TFAIL, "%s access and "
15656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 "modification times not set",
15756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 TEMP_FILE);
158865695bbc89088b9526ea9045410e5afb70a985cplars				} else {
159865695bbc89088b9526ea9045410e5afb70a985cplars					tst_resm(TPASS, "Functionality of "
16056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 "utime(%s, &times) successful",
16156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 TEMP_FILE);
162865695bbc89088b9526ea9045410e5afb70a985cplars				}
163865695bbc89088b9526ea9045410e5afb70a985cplars			} else {
164865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TPASS, "%s call succeeded", TCID);
165865695bbc89088b9526ea9045410e5afb70a985cplars			}
166865695bbc89088b9526ea9045410e5afb70a985cplars		}
16756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		Tst_count++;	/* incr TEST_LOOP counter */
1682c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
169865695bbc89088b9526ea9045410e5afb70a985cplars
170865695bbc89088b9526ea9045410e5afb70a985cplars	cleanup();
1717d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	tst_exit();
1729a66de1f5c76306b79413dfab00946b7642c06dcrobbiew
1732c28215423293e443469a07ae7011135d058b671Garrett Cooper}
174865695bbc89088b9526ea9045410e5afb70a985cplars
175865695bbc89088b9526ea9045410e5afb70a985cplars/*
176865695bbc89088b9526ea9045410e5afb70a985cplars * void
177865695bbc89088b9526ea9045410e5afb70a985cplars * setup() - performs all ONE TIME setup for this test.
178865695bbc89088b9526ea9045410e5afb70a985cplars *  Create a temporary directory and change directory to it.
179865695bbc89088b9526ea9045410e5afb70a985cplars *  Create a test file under temporary directory and close it
180865695bbc89088b9526ea9045410e5afb70a985cplars */
18156207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid setup()
182865695bbc89088b9526ea9045410e5afb70a985cplars{
18356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	int fildes;		/* file handle for temp file */
184865695bbc89088b9526ea9045410e5afb70a985cplars
185865695bbc89088b9526ea9045410e5afb70a985cplars	tst_sig(NOFORK, DEF_HANDLER, cleanup);
186865695bbc89088b9526ea9045410e5afb70a985cplars
187865695bbc89088b9526ea9045410e5afb70a985cplars	/* Check that the test process id is super/root  */
188865695bbc89088b9526ea9045410e5afb70a985cplars	if (geteuid() != 0) {
189865695bbc89088b9526ea9045410e5afb70a985cplars		tst_brkm(TBROK, NULL, "Must be super/root for this test!");
190865695bbc89088b9526ea9045410e5afb70a985cplars		tst_exit();
191865695bbc89088b9526ea9045410e5afb70a985cplars	}
192865695bbc89088b9526ea9045410e5afb70a985cplars
193865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_PAUSE;
194865695bbc89088b9526ea9045410e5afb70a985cplars
195865695bbc89088b9526ea9045410e5afb70a985cplars	tst_tmpdir();
196865695bbc89088b9526ea9045410e5afb70a985cplars
197865695bbc89088b9526ea9045410e5afb70a985cplars	/* Creat a temporary file under above directory */
198865695bbc89088b9526ea9045410e5afb70a985cplars	if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) {
199865695bbc89088b9526ea9045410e5afb70a985cplars		tst_brkm(TBROK, cleanup,
200865695bbc89088b9526ea9045410e5afb70a985cplars			 "creat(%s, %#o) Failed, errno=%d :%s",
201865695bbc89088b9526ea9045410e5afb70a985cplars			 TEMP_FILE, FILE_MODE, errno, strerror(errno));
202865695bbc89088b9526ea9045410e5afb70a985cplars	}
203865695bbc89088b9526ea9045410e5afb70a985cplars
204865695bbc89088b9526ea9045410e5afb70a985cplars	/* Close the temporary file created */
205865695bbc89088b9526ea9045410e5afb70a985cplars	if (close(fildes) < 0) {
206865695bbc89088b9526ea9045410e5afb70a985cplars		tst_brkm(TBROK, cleanup,
207865695bbc89088b9526ea9045410e5afb70a985cplars			 "close(%s) Failed, errno=%d : %s:",
208865695bbc89088b9526ea9045410e5afb70a985cplars			 TEMP_FILE, errno, strerror(errno));
209865695bbc89088b9526ea9045410e5afb70a985cplars	}
210865695bbc89088b9526ea9045410e5afb70a985cplars
211865695bbc89088b9526ea9045410e5afb70a985cplars	/* Initialize the modification and access time in the times arg */
212865695bbc89088b9526ea9045410e5afb70a985cplars	times.actime = NEW_TIME;
213865695bbc89088b9526ea9045410e5afb70a985cplars	times.modtime = NEW_TIME;
214865695bbc89088b9526ea9045410e5afb70a985cplars
2152c28215423293e443469a07ae7011135d058b671Garrett Cooper}
216865695bbc89088b9526ea9045410e5afb70a985cplars
217865695bbc89088b9526ea9045410e5afb70a985cplars/*
218865695bbc89088b9526ea9045410e5afb70a985cplars * void
219865695bbc89088b9526ea9045410e5afb70a985cplars * cleanup() - performs all ONE TIME cleanup for this test at
220865695bbc89088b9526ea9045410e5afb70a985cplars *             completion or premature exit.
221865695bbc89088b9526ea9045410e5afb70a985cplars *  Remove the test directory and testfile created in the setup.
222865695bbc89088b9526ea9045410e5afb70a985cplars */
22356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid cleanup()
224865695bbc89088b9526ea9045410e5afb70a985cplars{
225865695bbc89088b9526ea9045410e5afb70a985cplars	/*
226865695bbc89088b9526ea9045410e5afb70a985cplars	 * print timing stats if that option was specified.
227865695bbc89088b9526ea9045410e5afb70a985cplars	 * print errno log if that option was specified.
228865695bbc89088b9526ea9045410e5afb70a985cplars	 */
229865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_CLEANUP;
230865695bbc89088b9526ea9045410e5afb70a985cplars
231865695bbc89088b9526ea9045410e5afb70a985cplars	tst_rmdir();
232865695bbc89088b9526ea9045410e5afb70a985cplars
233ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
234