blob: 9af5034e33f883cbe70438e3c04d0367df6acc67 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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:
subrata_modak4bb656a2009-02-26 12:02:09 +000029 * utime succeeds returning zero and sets the access and modification
plars865695b2001-08-27 22:15:12 +000030 * 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)
subrata_modak56207ce2009-03-23 13:35:39 +000042 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000043 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000046 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000047 * 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.
subrata_modak56207ce2009-03-23 13:35:39 +000057 * -f : Turn off functionality Testing.
plars865695b2001-08-27 22:15:12 +000058 * -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 "usctest.h"
84
85#define TEMP_FILE "tmp_file"
86#define FILE_MODE S_IRUSR | S_IRGRP | S_IROTH
87#define NEW_TIME 10000
88
subrata_modak56207ce2009-03-23 13:35:39 +000089char *TCID = "utime04"; /* Test program identifier. */
90int TST_TOTAL = 1; /* Total number of test cases. */
subrata_modak56207ce2009-03-23 13:35:39 +000091int exp_enos[] = { 0 };
plars865695b2001-08-27 22:15:12 +000092struct utimbuf times; /* struct. buffer for utime() */
93
94void setup(); /* Main setup function of test */
95void cleanup(); /* cleanup function for the test */
96
subrata_modak56207ce2009-03-23 13:35:39 +000097int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000098{
99 struct stat stat_buf; /* struct buffer to hold file info. */
100 int lc; /* loop counter */
101 char *msg; /* message returned from parse_opts */
102 time_t modf_time, access_time;
subrata_modak56207ce2009-03-23 13:35:39 +0000103 /* file modification/access time */
104
plars865695b2001-08-27 22:15:12 +0000105 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800106 msg = parse_opts(ac, av, NULL, NULL);
107 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000108 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800109
plars865695b2001-08-27 22:15:12 +0000110 }
111
plars865695b2001-08-27 22:15:12 +0000112 setup();
113
114 /* set the expected errnos... */
115 TEST_EXP_ENOS(exp_enos);
116
plars865695b2001-08-27 22:15:12 +0000117 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800118
subrata_modak56207ce2009-03-23 13:35:39 +0000119 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000120
subrata_modak4bb656a2009-02-26 12:02:09 +0000121 /*
plars865695b2001-08-27 22:15:12 +0000122 * Invoke utime(2) to set TEMP_FILE access and
123 * modification times to that specified by
124 * times argument.
125 */
126 TEST(utime(TEMP_FILE, &times));
127
plars865695b2001-08-27 22:15:12 +0000128 if (TEST_RETURN == -1) {
129 TEST_ERROR_LOG(TEST_ERRNO);
130 tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s",
131 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
132 } else {
133 /*
134 * Perform functional verification if test
135 * executed without (-f) option.
136 */
137 if (STD_FUNCTIONAL_TEST) {
138 /*
139 * Get the modification and access times of
140 * temporary file using stat(2).
141 */
142 if (stat(TEMP_FILE, &stat_buf) < 0) {
143 tst_brkm(TFAIL, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000144 "stat(2) of %s failed, "
145 "error:%d", TEMP_FILE,
146 TEST_ERRNO);
Garrett Cooper53740502010-12-16 00:04:01 -0800147 }
plars865695b2001-08-27 22:15:12 +0000148 modf_time = stat_buf.st_mtime;
149 access_time = stat_buf.st_atime;
150
151 /* Now do the actual verification */
subrata_modak56207ce2009-03-23 13:35:39 +0000152 if ((modf_time != NEW_TIME) ||
plars865695b2001-08-27 22:15:12 +0000153 (access_time != NEW_TIME)) {
154 tst_resm(TFAIL, "%s access and "
subrata_modak56207ce2009-03-23 13:35:39 +0000155 "modification times not set",
156 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000157 } else {
158 tst_resm(TPASS, "Functionality of "
subrata_modak56207ce2009-03-23 13:35:39 +0000159 "utime(%s, &times) successful",
160 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000161 }
162 } else {
163 tst_resm(TPASS, "%s call succeeded", TCID);
164 }
165 }
subrata_modak56207ce2009-03-23 13:35:39 +0000166 Tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800167 }
plars865695b2001-08-27 22:15:12 +0000168
plars865695b2001-08-27 22:15:12 +0000169 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800170 tst_exit();
plars865695b2001-08-27 22:15:12 +0000171
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
plars865695b2001-08-27 22:15:12 +0000173
174/*
175 * void
176 * setup() - performs all ONE TIME setup for this test.
177 * Create a temporary directory and change directory to it.
178 * Create a test file under temporary directory and close it
179 */
subrata_modak56207ce2009-03-23 13:35:39 +0000180void setup()
plars865695b2001-08-27 22:15:12 +0000181{
subrata_modak56207ce2009-03-23 13:35:39 +0000182 int fildes; /* file handle for temp file */
plars865695b2001-08-27 22:15:12 +0000183
plars865695b2001-08-27 22:15:12 +0000184 tst_sig(NOFORK, DEF_HANDLER, cleanup);
185
186 /* Check that the test process id is super/root */
187 if (geteuid() != 0) {
188 tst_brkm(TBROK, NULL, "Must be super/root for this test!");
189 tst_exit();
190 }
191
plars865695b2001-08-27 22:15:12 +0000192 TEST_PAUSE;
193
plars865695b2001-08-27 22:15:12 +0000194 tst_tmpdir();
195
196 /* Creat a temporary file under above directory */
197 if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) {
198 tst_brkm(TBROK, cleanup,
199 "creat(%s, %#o) Failed, errno=%d :%s",
200 TEMP_FILE, FILE_MODE, errno, strerror(errno));
201 }
202
203 /* Close the temporary file created */
204 if (close(fildes) < 0) {
205 tst_brkm(TBROK, cleanup,
206 "close(%s) Failed, errno=%d : %s:",
207 TEMP_FILE, errno, strerror(errno));
208 }
209
210 /* Initialize the modification and access time in the times arg */
211 times.actime = NEW_TIME;
212 times.modtime = NEW_TIME;
213
Garrett Cooper2c282152010-12-16 00:55:50 -0800214}
plars865695b2001-08-27 22:15:12 +0000215
216/*
217 * void
218 * cleanup() - performs all ONE TIME cleanup for this test at
219 * completion or premature exit.
220 * Remove the test directory and testfile created in the setup.
221 */
subrata_modak56207ce2009-03-23 13:35:39 +0000222void cleanup()
plars865695b2001-08-27 22:15:12 +0000223{
224 /*
225 * print timing stats if that option was specified.
226 * print errno log if that option was specified.
227 */
228 TEST_CLEANUP;
229
plars865695b2001-08-27 22:15:12 +0000230 tst_rmdir();
231
Chris Dearmanec6edca2012-10-17 19:54:01 -0700232}