1 /*
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: utime04
22  *
23  * Test Description:
24  *  Verify that the system call utime() successfully sets the modification
25  *  and access times of a file to the time specified by times argument, if
26  *  the times argument is not null, and the user ID of the process is "root".
27  *
28  * Expected Result:
29  *   utime succeeds returning zero and sets the access and modification
30  *   times of the file to that specified by the times argument.
31  *
32  * Algorithm:
33  *  Setup:
34  *   Setup signal handling.
35  *   Create temporary directory.
36  *   Pause for SIGUSR1 if option specified.
37  *
38  *  Test:
39  *   Loop if the proper options are given.
40  *   Execute system call
41  *   Check return code, if system call failed (return=-1)
42  *	Log the errno and Issue a FAIL message.
43  *   Otherwise,
44  *	Verify the Functionality of system call
45  *      if successful,
46  *		Issue Functionality-Pass message.
47  *      Otherwise,
48  *		Issue Functionality-Fail message.
49  *  Cleanup:
50  *   Print errno log and/or timing stats if options given
51  *   Delete the temporary directory created.
52  *
53  * Usage:  <for command-line>
54  *  utime04 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
55  *	where,  -c n : Run n copies concurrently.
56  *		-e   : Turn on errno logging.
57  *		-f   : Turn off functionality Testing.
58  *		-i n : Execute test n times.
59  *		-I x : Execute test for x seconds.
60  *		-P x : Pause for x seconds between iterations.
61  *		-t   : Turn on syscall timing.
62  *
63  * History
64  *	07/2001 John George
65  *		-Ported
66  *
67  * Restrictions:
68  *  This test should be run by 'super-user' (root) only.
69  *
70  */
71 
72 #include <stdio.h>
73 #include <sys/types.h>
74 #include <errno.h>
75 #include <unistd.h>
76 #include <fcntl.h>
77 #include <utime.h>
78 #include <string.h>
79 #include <sys/stat.h>
80 #include <signal.h>
81 
82 #include "test.h"
83 #include "safe_macros.h"
84 
85 #define TEMP_FILE	"tmp_file"
86 #define FILE_MODE	S_IRUSR | S_IRGRP | S_IROTH
87 #define NEW_TIME	10000
88 
89 char *TCID = "utime04";
90 int TST_TOTAL = 1;
91 
92 struct utimbuf times;		/* struct. buffer for utime() */
93 
94 void setup();			/* Main setup function of test */
95 void cleanup();			/* cleanup function for the test */
96 
main(int ac,char ** av)97 int main(int ac, char **av)
98 {
99 	struct stat stat_buf;	/* struct buffer to hold file info. */
100 	int lc;
101 	time_t modf_time, access_time;
102 	/* file modification/access time */
103 
104 	tst_parse_opts(ac, av, NULL, NULL);
105 
106 	setup();
107 
108 	for (lc = 0; TEST_LOOPING(lc); lc++) {
109 
110 		tst_count = 0;
111 
112 		/*
113 		 * Invoke utime(2) to set TEMP_FILE access and
114 		 * modification times to that specified by
115 		 * times argument.
116 		 */
117 		TEST(utime(TEMP_FILE, &times));
118 
119 		if (TEST_RETURN == -1) {
120 			tst_resm(TFAIL|TTERRNO, "utime(%s) failed", TEMP_FILE);
121 		} else {
122 			/*
123 			 * Get the modification and access times of
124 			 * temporary file using stat(2).
125 			 */
126 			SAFE_STAT(cleanup, TEMP_FILE, &stat_buf);
127 			modf_time = stat_buf.st_mtime;
128 			access_time = stat_buf.st_atime;
129 
130 			/* Now do the actual verification */
131 			if ((modf_time != NEW_TIME) ||
132 			    (access_time != NEW_TIME)) {
133 				tst_resm(TFAIL, "%s access and "
134 					 "modification times not set",
135 					 TEMP_FILE);
136 			} else {
137 				tst_resm(TPASS, "Functionality of "
138 					 "utime(%s, &times) successful",
139 					 TEMP_FILE);
140 			}
141 		}
142 		tst_count++;	/* incr TEST_LOOP counter */
143 	}
144 
145 	cleanup();
146 	tst_exit();
147 }
148 
149 /*
150  * void
151  * setup() - performs all ONE TIME setup for this test.
152  *  Create a temporary directory and change directory to it.
153  *  Create a test file under temporary directory and close it
154  */
setup(void)155 void setup(void)
156 {
157 	int fildes;		/* file handle for temp file */
158 
159 	tst_require_root();
160 
161 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
162 
163 	TEST_PAUSE;
164 
165 	tst_tmpdir();
166 
167 	/* Creat a temporary file under above directory */
168 	fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE);
169 
170 	/* Close the temporary file created */
171 	SAFE_CLOSE(cleanup, fildes);
172 
173 	/* Initialize the modification and access time in the times arg */
174 	times.actime = NEW_TIME;
175 	times.modtime = NEW_TIME;
176 
177 }
178 
179 /*
180  * void
181  * cleanup() - performs all ONE TIME cleanup for this test at
182  *             completion or premature exit.
183  *  Remove the test directory and testfile created in the setup.
184  */
cleanup(void)185 void cleanup(void)
186 {
187 
188 	tst_rmdir();
189 
190 }
191