blob: ae6da69f2a83722dd4e7baa7b20a2dde0cac5003 [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: 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:
subrata_modak4bb656a2009-02-26 12:02:09 +000032 * utime succeeds returning zero and sets the access and modification
plars865695b2001-08-27 22:15:12 +000033 * 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)
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000046 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000047 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000048 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000049 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000050 * 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:
plars865695b2001-08-27 22:15:12 +000071 *
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>
robbiewceb574a2001-08-28 16:15:56 +000083#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000084
85#include "test.h"
plars865695b2001-08-27 22:15:12 +000086
87#define TEMP_FILE "tmp_file"
88#define FILE_MODE S_IRUSR | S_IRGRP | S_IROTH
89#define NEW_TIME 10000
90
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020091char *TCID = "utime05";
92int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080093
robbiewceb574a2001-08-28 16:15:56 +000094char nobody_uid[] = "nobody";
95struct passwd *ltpuser;
96
plars865695b2001-08-27 22:15:12 +000097struct utimbuf times; /* struct. buffer for utime() */
98
99void setup(); /* Main setup function of test */
100void cleanup(); /* cleanup function for the test */
101
subrata_modak56207ce2009-03-23 13:35:39 +0000102int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000103{
104 struct stat stat_buf; /* struct buffer to hold file info. */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200105 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200106 const char *msg;
plars865695b2001-08-27 22:15:12 +0000107 time_t modf_time, access_time;
subrata_modak56207ce2009-03-23 13:35:39 +0000108 /* file modification/access time */
109
Garrett Cooper45e285d2010-11-22 12:19:25 -0800110 msg = parse_opts(ac, av, NULL, NULL);
111 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000112 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800113
plars865695b2001-08-27 22:15:12 +0000114 }
115
plars865695b2001-08-27 22:15:12 +0000116 setup();
117
plars865695b2001-08-27 22:15:12 +0000118 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800119
Caspar Zhangd59a6592013-03-07 14:59:12 +0800120 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000121
subrata_modak4bb656a2009-02-26 12:02:09 +0000122 /*
plars865695b2001-08-27 22:15:12 +0000123 * Invoke utime(2) to set TEMP_FILE access and
124 * modification times to that specified by
125 * times argument.
126 */
127 TEST(utime(TEMP_FILE, &times));
128
plars865695b2001-08-27 22:15:12 +0000129 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000130 tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s",
131 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
132 } else {
133 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200134 * Get the modification and access times of
135 * temporary file using stat(2).
plars865695b2001-08-27 22:15:12 +0000136 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200137 if (stat(TEMP_FILE, &stat_buf) < 0) {
138 tst_brkm(TFAIL, cleanup, "stat(2) of "
139 "%s failed, error:%d",
140 TEMP_FILE, TEST_ERRNO);
141 }
142 modf_time = stat_buf.st_mtime;
143 access_time = stat_buf.st_atime;
plars865695b2001-08-27 22:15:12 +0000144
Cyril Hrubise38b9612014-06-02 17:20:57 +0200145 /* Now do the actual verification */
146 if ((modf_time != NEW_TIME) ||
147 (access_time != NEW_TIME)) {
148 tst_resm(TFAIL, "%s access and "
149 "modification times not set",
150 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000151 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200152 tst_resm(TPASS, "Functionality of "
153 "utime(%s, &times) successful",
154 TEMP_FILE);
plars865695b2001-08-27 22:15:12 +0000155 }
156 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800157 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800158 }
plars865695b2001-08-27 22:15:12 +0000159
plars865695b2001-08-27 22:15:12 +0000160 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800161 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800162}
plars865695b2001-08-27 22:15:12 +0000163
164/*
165 * void
166 * setup() - performs all ONE TIME setup for this test.
167 * Create a temporary directory and change directory to it.
168 * Create a test file under temporary directory and close it
169 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400170void setup(void)
plars865695b2001-08-27 22:15:12 +0000171{
subrata_modak56207ce2009-03-23 13:35:39 +0000172 int fildes; /* file handle for temp file */
plars865695b2001-08-27 22:15:12 +0000173
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200174 tst_require_root(NULL);
175
plars865695b2001-08-27 22:15:12 +0000176 tst_sig(FORK, DEF_HANDLER, cleanup);
177
robbiewceb574a2001-08-28 16:15:56 +0000178 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000179 ltpuser = getpwnam(nobody_uid);
180 if (setuid(ltpuser->pw_uid) == -1) {
181 tst_resm(TINFO, "setuid failed to "
182 "to set the effective uid to %d", ltpuser->pw_uid);
183 perror("setuid");
184 }
plars865695b2001-08-27 22:15:12 +0000185
plars865695b2001-08-27 22:15:12 +0000186 TEST_PAUSE;
187
plars865695b2001-08-27 22:15:12 +0000188 tst_tmpdir();
189
190 /* Creat a temporary file under above directory */
191 if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) {
192 tst_brkm(TBROK, cleanup,
193 "creat(%s, %#o) Failed, errno=%d :%s",
194 TEMP_FILE, FILE_MODE, errno, strerror(errno));
195 }
196
197 /* Close the temporary file created */
198 if (close(fildes) < 0) {
199 tst_brkm(TBROK, cleanup,
200 "close(%s) Failed, errno=%d : %s:",
201 TEMP_FILE, errno, strerror(errno));
202 }
203
204 /* Initialize the modification and access time in the times arg */
205 times.actime = NEW_TIME;
206 times.modtime = NEW_TIME;
207
Garrett Cooper2c282152010-12-16 00:55:50 -0800208}
plars865695b2001-08-27 22:15:12 +0000209
210/*
211 * void
212 * cleanup() - performs all ONE TIME cleanup for this test at
213 * completion or premature exit.
214 * Remove the test directory and testfile created in the setup.
215 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400216void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000217{
plars865695b2001-08-27 22:15:12 +0000218
plars865695b2001-08-27 22:15:12 +0000219 tst_rmdir();
220
Chris Dearmanec6edca2012-10-17 19:54:01 -0700221}