Home | History | Annotate | Download | only in utime
      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: utime02
     22  *
     23  * Test Description:
     24  *  Verify that the system call utime() successfully sets the modification
     25  *  and access times of a file to the current time, under the following
     26  *  constraints,
     27  *	- The times argument is 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 the current time.
     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  *	utime02 [-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 <unistd.h>
     76 #include <sys/types.h>
     77 #include <errno.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 #include <time.h>
     85 
     86 #include "test.h"
     87 #include "safe_macros.h"
     88 
     89 #define TEMP_FILE	"tmp_file"
     90 #define FILE_MODE	S_IRUSR | S_IRGRP | S_IROTH
     91 
     92 char *TCID = "utime02";
     93 int TST_TOTAL = 1;
     94 time_t curr_time;		/* current time in seconds */
     95 
     96 char nobody_uid[] = "nobody";
     97 struct passwd *ltpuser;
     98 
     99 void setup();			/* Main setup function of test */
    100 void cleanup();			/* cleanup function for the test */
    101 
    102 int main(int ac, char **av)
    103 {
    104 	struct stat stat_buf;	/* struct buffer to hold file info. */
    105 	int lc;
    106 	long type;
    107 	time_t modf_time, access_time;
    108 	time_t pres_time;	/* file modification/access/present time */
    109 
    110 	tst_parse_opts(ac, av, NULL, NULL);
    111 
    112 	setup();
    113 
    114 	switch ((type = tst_fs_type(cleanup, "."))) {
    115 	case TST_NFS_MAGIC:
    116 		if (tst_kvercmp(2, 6, 18) < 0)
    117 			tst_brkm(TCONF, cleanup, "Cannot do utime on a file"
    118 				" on %s filesystem before 2.6.18",
    119 				 tst_fs_type_name(type));
    120 		break;
    121 	case TST_V9FS_MAGIC:
    122 		tst_brkm(TCONF, cleanup,
    123 			 "Cannot do utime on a file on %s filesystem",
    124 			 tst_fs_type_name(type));
    125 		break;
    126 	}
    127 
    128 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    129 
    130 		tst_count = 0;
    131 
    132 		/*
    133 		 * Invoke utime(2) to set TEMP_FILE access and
    134 		 * modification times to the current time.
    135 		 */
    136 		TEST(utime(TEMP_FILE, NULL));
    137 
    138 		if (TEST_RETURN == -1) {
    139 			tst_resm(TFAIL|TTERRNO, "utime(%s) failed", TEMP_FILE);
    140 		} else {
    141 			/*
    142 			 * Sleep for a second so that mod time and
    143 			 * access times will be different from the
    144 			 * current time
    145 			 */
    146 			sleep(2);
    147 
    148 			/*
    149 			 * Get the current time now, after calling
    150 			 * utime(2)
    151 			 */
    152 			pres_time = time(NULL);
    153 
    154 			/*
    155 			 * Get the modification and access times of
    156 			 * temporary file using stat(2).
    157 			 */
    158 			SAFE_STAT(cleanup, TEMP_FILE, &stat_buf);
    159 			modf_time = stat_buf.st_mtime;
    160 			access_time = stat_buf.st_atime;
    161 
    162 			/* Now do the actual verification */
    163 			if (modf_time <= curr_time ||
    164 			    modf_time >= pres_time ||
    165 			    access_time <= curr_time ||
    166 			    access_time >= pres_time) {
    167 				tst_resm(TFAIL, "%s access and "
    168 					 "modification times not set",
    169 					 TEMP_FILE);
    170 			} else {
    171 				tst_resm(TPASS, "Functionality of "
    172 					 "utime(%s, NULL) successful",
    173 					 TEMP_FILE);
    174 			}
    175 		}
    176 		tst_count++;
    177 	}
    178 
    179 	cleanup();
    180 	tst_exit();
    181 }
    182 
    183 /*
    184  * void
    185  * setup() - performs all ONE TIME setup for this test.
    186  *  Create a temporary directory and change directory to it.
    187  *  Create a test file under temporary directory and close it
    188  */
    189 void setup(void)
    190 {
    191 	int fildes;		/* file handle for temp file */
    192 
    193 	tst_require_root();
    194 
    195 	tst_sig(FORK, DEF_HANDLER, cleanup);
    196 
    197 	/* Switch to nobody user for correct error code collection */
    198 	ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);
    199 	SAFE_SETUID(NULL, ltpuser->pw_uid);
    200 
    201 	/* Pause if that option was specified
    202 	 * TEST_PAUSE contains the code to fork the test with the -i option.
    203 	 * You want to make sure you do this before you create your temporary
    204 	 * directory.
    205 	 */
    206 	TEST_PAUSE;
    207 
    208 	tst_tmpdir();
    209 
    210 	/* Creat a temporary file under above directory */
    211 	fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE);
    212 
    213 	/* Close the temporary file created */
    214 	SAFE_CLOSE(cleanup, fildes);
    215 
    216 	/* Get the current time */
    217 	curr_time = time(NULL);
    218 
    219 	/*
    220 	 * Sleep for a second so that mod time and access times will be
    221 	 * different from the current time
    222 	 */
    223 	sleep(2);		/* sleep(1) on IA64 sometimes sleeps < 1 sec!! */
    224 
    225 }
    226 
    227 /*
    228  * void
    229  * cleanup() - performs all ONE TIME cleanup for this test at
    230  *             completion or premature exit.
    231  *  Remove the test directory and testfile created in the setup.
    232  */
    233 void cleanup(void)
    234 {
    235 
    236 	tst_rmdir();
    237 
    238 }
    239