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: utime05
22  *
23  * Test Description:
24  *  Verify that the system call utime() successfully sets the modification
25  *  and access times of a file to the value specified by the times argument
26  *  under the following constraints,
27  *		- The times argument is not 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 that specified by the times argument.
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)
45  *	Log the errno and Issue a FAIL message.
46  *   Otherwise,
47  *	Verify the Functionality of system call
48  *      if successful,
49  *		Issue Functionality-Pass message.
50  *      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  *  utime05 [-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:
71  *
72  */
73 
74 #include <stdio.h>
75 #include <sys/types.h>
76 #include <errno.h>
77 #include <unistd.h>
78 #include <fcntl.h>
79 #include <utime.h>
80 #include <string.h>
81 #include <sys/stat.h>
82 #include <signal.h>
83 #include <pwd.h>
84 
85 #include "test.h"
86 #include "safe_macros.h"
87 
88 #define TEMP_FILE	"tmp_file"
89 #define FILE_MODE	S_IRUSR | S_IRGRP | S_IROTH
90 #define NEW_TIME	10000
91 
92 char *TCID = "utime05";
93 int TST_TOTAL = 1;
94 
95 char nobody_uid[] = "nobody";
96 struct passwd *ltpuser;
97 
98 struct utimbuf times;		/* struct. buffer for utime() */
99 
100 void setup();			/* Main setup function of test */
101 void cleanup();			/* cleanup function for the test */
102 
main(int ac,char ** av)103 int main(int ac, char **av)
104 {
105 	struct stat stat_buf;	/* struct buffer to hold file info. */
106 	int lc;
107 	time_t modf_time, access_time;
108 	/* file modification/access time */
109 
110 	tst_parse_opts(ac, av, NULL, NULL);
111 
112 	setup();
113 
114 	for (lc = 0; TEST_LOOPING(lc); lc++) {
115 
116 		tst_count = 0;
117 
118 		/*
119 		 * Invoke utime(2) to set TEMP_FILE access and
120 		 * modification times to that specified by
121 		 * times argument.
122 		 */
123 		TEST(utime(TEMP_FILE, &times));
124 
125 		if (TEST_RETURN == -1) {
126 			tst_resm(TFAIL|TTERRNO, "utime(%s) failed", TEMP_FILE);
127 		} else {
128 			/*
129 			 * Get the modification and access times of
130 			 * temporary file using stat(2).
131 			 */
132 			SAFE_STAT(cleanup, TEMP_FILE, &stat_buf);
133 			modf_time = stat_buf.st_mtime;
134 			access_time = stat_buf.st_atime;
135 
136 			/* Now do the actual verification */
137 			if ((modf_time != NEW_TIME) ||
138 			    (access_time != NEW_TIME)) {
139 				tst_resm(TFAIL, "%s access and "
140 					 "modification times not set",
141 					 TEMP_FILE);
142 			} else {
143 				tst_resm(TPASS, "Functionality of "
144 					 "utime(%s, &times) successful",
145 					 TEMP_FILE);
146 			}
147 		}
148 		tst_count++;	/* incr TEST_LOOP counter */
149 	}
150 
151 	cleanup();
152 	tst_exit();
153 }
154 
155 /*
156  * void
157  * setup() - performs all ONE TIME setup for this test.
158  *  Create a temporary directory and change directory to it.
159  *  Create a test file under temporary directory and close it
160  */
setup(void)161 void setup(void)
162 {
163 	int fildes;		/* file handle for temp file */
164 
165 	tst_require_root();
166 
167 	tst_sig(FORK, DEF_HANDLER, cleanup);
168 
169 	/* Switch to nobody user for correct error code collection */
170 	ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);
171 	SAFE_SETUID(NULL, ltpuser->pw_uid);
172 
173 	TEST_PAUSE;
174 
175 	tst_tmpdir();
176 
177 	/* Creat a temporary file under above directory */
178 	fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE);
179 
180 	/* Close the temporary file created */
181 	SAFE_CLOSE(cleanup, fildes);
182 
183 	/* Initialize the modification and access time in the times arg */
184 	times.actime = NEW_TIME;
185 	times.modtime = NEW_TIME;
186 
187 }
188 
189 /*
190  * void
191  * cleanup() -	performs all ONE TIME cleanup for this test at
192  *		completion or premature exit.
193  *		Remove the test directory and testfile created in the setup.
194  */
cleanup(void)195 void cleanup(void)
196 {
197 
198 	tst_rmdir();
199 
200 }
201