Home | History | Annotate | Download | only in stat
      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: stat02
     22  *
     23  * Test Description:
     24  *  Verify that, stat(2) succeeds to get the status of a file and fills the
     25  *  stat structure elements though process doesn't have read access to the
     26  *  file.
     27  *
     28  * Expected Result:
     29  *  stat() should return value 0 on success and the stat structure elements
     30  *  should be filled with specified 'file' information.
     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)
     42  *   	Log the errno and Issue a FAIL message.
     43  *   Otherwise,
     44  *   	Verify the Functionality of system call
     45  *      if successful,
     46  *      	Issue Functionality-Pass message.
     47  *      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  *  stat02 [-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.
     57  *		-f   : Turn off functionality Testing.
     58  *		-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  *
     69  */
     70 #include <stdio.h>
     71 #include <sys/types.h>
     72 #include <fcntl.h>
     73 #include <sys/stat.h>
     74 #include <errno.h>
     75 #include <string.h>
     76 #include <signal.h>
     77 #include <pwd.h>
     78 
     79 #include "test.h"
     80 
     81 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     82 #define TESTFILE	"testfile"
     83 #define FILE_SIZE       1024
     84 #define BUF_SIZE	256
     85 #define NEW_MODE	0222
     86 #define MASK		0777
     87 
     88 char *TCID = "stat02";
     89 int TST_TOTAL = 1;
     90 
     91 uid_t user_id;			/* eff. user id/group id of test process */
     92 gid_t group_id;
     93 char nobody_uid[] = "nobody";
     94 struct passwd *ltpuser;
     95 
     96 void setup();
     97 void cleanup();
     98 
     99 int main(int ac, char **av)
    100 {
    101 	struct stat stat_buf;	/* stat structure buffer */
    102 	int lc;
    103 
    104 	tst_parse_opts(ac, av, NULL, NULL);
    105 
    106 	setup();
    107 
    108 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    109 
    110 		tst_count = 0;
    111 
    112 		/*
    113 		 * Call stat(2) to get the status of
    114 		 * specified 'file' into stat structure.
    115 		 */
    116 		TEST(stat(TESTFILE, &stat_buf));
    117 
    118 		if (TEST_RETURN == -1) {
    119 			tst_resm(TFAIL,
    120 				 "stat(%s, &stat_buf) Failed, errno=%d : %s",
    121 				 TESTFILE, TEST_ERRNO, strerror(TEST_ERRNO));
    122 		} else {
    123 			stat_buf.st_mode &= ~S_IFREG;
    124 			/*
    125 			 * Verify the data returned by stat(2)
    126 			 * aganist the expected data.
    127 			 */
    128 			if ((stat_buf.st_uid != user_id) ||
    129 			    (stat_buf.st_gid != group_id) ||
    130 			    (stat_buf.st_size != FILE_SIZE) ||
    131 			    ((stat_buf.st_mode & MASK) != NEW_MODE)) {
    132 				tst_resm(TFAIL, "Functionality of "
    133 					 "stat(2) on '%s' Failed",
    134 					 TESTFILE);
    135 			} else {
    136 				tst_resm(TPASS, "Functionality of "
    137 					 "stat(2) on '%s' Succcessful",
    138 					 TESTFILE);
    139 			}
    140 		}
    141 		tst_count++;	/* incr TEST_LOOP counter */
    142 	}
    143 
    144 	cleanup();
    145 	tst_exit();
    146 }
    147 
    148 /*
    149  * void
    150  * setup() -  Performs setup function for the test.
    151  *  Creat a temporary directory and change directory to it.
    152  *  Creat a testfile and write some data into it.
    153  *  Modify the mode permissions of testfile such that test process
    154  *  has read-only access to testfile.
    155  */
    156 void setup(void)
    157 {
    158 	int i, fd;		/* counter, file handle for file */
    159 	char tst_buff[BUF_SIZE];	/* data buffer for file */
    160 	int wbytes;		/* no. of bytes written to file */
    161 	int write_len = 0;
    162 
    163 	tst_require_root();
    164 
    165 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    166 
    167 	/* Switch to nobody user for correct error code collection */
    168 	ltpuser = getpwnam(nobody_uid);
    169 	if (setuid(ltpuser->pw_uid) == -1) {
    170 		tst_resm(TINFO, "setuid failed to "
    171 			 "to set the effective uid to %d", ltpuser->pw_uid);
    172 		perror("setuid");
    173 	}
    174 
    175 	/* Pause if that option was specified
    176 	 * TEST_PAUSE contains the code to fork the test with the -i option.
    177 	 * You want to make sure you do this before you create your temporary
    178 	 * directory.
    179 	 */
    180 	TEST_PAUSE;
    181 
    182 	tst_tmpdir();
    183 
    184 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
    185 		tst_brkm(TBROK, cleanup,
    186 			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
    187 			 TESTFILE, FILE_MODE, errno, strerror(errno));
    188 	}
    189 
    190 	/* Fill the test buffer with the known data */
    191 	for (i = 0; i < BUF_SIZE; i++) {
    192 		tst_buff[i] = 'a';
    193 	}
    194 
    195 	/* Write to the file 1k data from the buffer */
    196 	while (write_len < FILE_SIZE) {
    197 		if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
    198 			tst_brkm(TBROK | TERRNO, cleanup, "write to %s failed",
    199 				 TESTFILE);
    200 		} else {
    201 			write_len += wbytes;
    202 		}
    203 	}
    204 
    205 	if (close(fd) == -1) {
    206 		tst_resm(TWARN | TERRNO, "closing %s failed", TESTFILE);
    207 	}
    208 
    209 	/* Modify mode permissions on the testfile */
    210 	if (chmod(TESTFILE, NEW_MODE) < 0) {
    211 		tst_brkm(TBROK | TERRNO, cleanup, "chmodding %s failed",
    212 			 TESTFILE);
    213 	}
    214 
    215 	/* Get the uid/gid of the process */
    216 	user_id = getuid();
    217 	group_id = getgid();
    218 
    219 }
    220 
    221 /*
    222  * cleanup() - performs all ONE TIME cleanup for this test at
    223  *	       completion or premature exit.
    224  *  Remove the temporary directory and file created.
    225  */
    226 void cleanup(void)
    227 {
    228 
    229 	tst_rmdir();
    230 }
    231