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