blob: 7e3b02b560150e13ea6dad3047ab23b9fd825106 [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000083
84#define TEMP_FILE "tmp_file"
85#define FILE_MODE S_IRUSR | S_IRGRP | S_IROTH
86#define NEW_TIME 10000
87
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020088char *TCID = "utime04";
89int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080090
plars865695b2001-08-27 22:15:12 +000091struct utimbuf times; /* struct. buffer for utime() */
92
93void setup(); /* Main setup function of test */
94void cleanup(); /* cleanup function for the test */
95
subrata_modak56207ce2009-03-23 13:35:39 +000096int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000097{
98 struct stat stat_buf; /* struct buffer to hold file info. */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020099 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200100 const char *msg;
plars865695b2001-08-27 22:15:12 +0000101 time_t modf_time, access_time;
subrata_modak56207ce2009-03-23 13:35:39 +0000102 /* file modification/access time */
103
Garrett Cooper45e285d2010-11-22 12:19:25 -0800104 msg = parse_opts(ac, av, NULL, NULL);
105 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000106 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800107
plars865695b2001-08-27 22:15:12 +0000108 }
109
plars865695b2001-08-27 22:15:12 +0000110 setup();
111
plars865695b2001-08-27 22:15:12 +0000112 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800113
Caspar Zhangd59a6592013-03-07 14:59:12 +0800114 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000115
subrata_modak4bb656a2009-02-26 12:02:09 +0000116 /*
plars865695b2001-08-27 22:15:12 +0000117 * Invoke utime(2) to set TEMP_FILE access and
118 * modification times to that specified by
119 * times argument.
120 */
121 TEST(utime(TEMP_FILE, &times));
122
plars865695b2001-08-27 22:15:12 +0000123 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000124 tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s",
125 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
126 } else {
127 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200128 * Get the modification and access times of
129 * temporary file using stat(2).
plars865695b2001-08-27 22:15:12 +0000130 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200131 if (stat(TEMP_FILE, &stat_buf) < 0) {
132 tst_brkm(TFAIL, cleanup,
133 "stat(2) of %s failed, "
134 "error:%d", TEMP_FILE,
135 TEST_ERRNO);
136 }
137 modf_time = stat_buf.st_mtime;
138 access_time = stat_buf.st_atime;
plars865695b2001-08-27 22:15:12 +0000139
Cyril Hrubise38b9612014-06-02 17:20:57 +0200140 /* Now do the actual verification */
141 if ((modf_time != NEW_TIME) ||
142 (access_time != NEW_TIME)) {
143 tst_resm(TFAIL, "%s access and "
144 "modification times not set",
145 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000146 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200147 tst_resm(TPASS, "Functionality of "
148 "utime(%s, &times) successful",
149 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000150 }
151 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800152 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800153 }
plars865695b2001-08-27 22:15:12 +0000154
plars865695b2001-08-27 22:15:12 +0000155 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800156 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800157}
plars865695b2001-08-27 22:15:12 +0000158
159/*
160 * void
161 * setup() - performs all ONE TIME setup for this test.
162 * Create a temporary directory and change directory to it.
163 * Create a test file under temporary directory and close it
164 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400165void setup(void)
plars865695b2001-08-27 22:15:12 +0000166{
subrata_modak56207ce2009-03-23 13:35:39 +0000167 int fildes; /* file handle for temp file */
plars865695b2001-08-27 22:15:12 +0000168
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200169 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000170
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200171 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000172
plars865695b2001-08-27 22:15:12 +0000173 TEST_PAUSE;
174
plars865695b2001-08-27 22:15:12 +0000175 tst_tmpdir();
176
177 /* Creat a temporary file under above directory */
178 if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) {
179 tst_brkm(TBROK, cleanup,
180 "creat(%s, %#o) Failed, errno=%d :%s",
181 TEMP_FILE, FILE_MODE, errno, strerror(errno));
182 }
183
184 /* Close the temporary file created */
185 if (close(fildes) < 0) {
186 tst_brkm(TBROK, cleanup,
187 "close(%s) Failed, errno=%d : %s:",
188 TEMP_FILE, errno, strerror(errno));
189 }
190
191 /* Initialize the modification and access time in the times arg */
192 times.actime = NEW_TIME;
193 times.modtime = NEW_TIME;
194
Garrett Cooper2c282152010-12-16 00:55:50 -0800195}
plars865695b2001-08-27 22:15:12 +0000196
197/*
198 * void
199 * cleanup() - performs all ONE TIME cleanup for this test at
200 * completion or premature exit.
201 * Remove the test directory and testfile created in the setup.
202 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400203void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000204{
plars865695b2001-08-27 22:15:12 +0000205
plars865695b2001-08-27 22:15:12 +0000206 tst_rmdir();
207
Chris Dearmanec6edca2012-10-17 19:54:01 -0700208}