stat02.c revision 0b9589f3f9c0345b29cfcf7da5a1253c708303eb
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 <sys/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 "usctest.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
89char *TCID = "stat02";
90int TST_TOTAL = 1;
91int exp_enos[] = { 0 };
92
93uid_t user_id;			/* eff. user id/group id of test process */
94gid_t group_id;
95char nobody_uid[] = "nobody";
96struct passwd *ltpuser;
97
98void setup();
99void cleanup();
100
101int main(int ac, char **av)
102{
103	struct stat stat_buf;	/* stat structure buffer */
104	int lc;
105	const char *msg;
106
107	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
108		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
109
110	setup();
111
112	/* set the expected errnos... */
113	TEST_EXP_ENOS(exp_enos);
114
115	for (lc = 0; TEST_LOOPING(lc); lc++) {
116
117		tst_count = 0;
118
119		/*
120		 * Call stat(2) to get the status of
121		 * specified 'file' into stat structure.
122		 */
123		TEST(stat(TESTFILE, &stat_buf));
124
125		if (TEST_RETURN == -1) {
126			TEST_ERROR_LOG(TEST_ERRNO);
127			tst_resm(TFAIL,
128				 "stat(%s, &stat_buf) Failed, errno=%d : %s",
129				 TESTFILE, TEST_ERRNO, strerror(TEST_ERRNO));
130		} else {
131			/*
132			 * Perform functional verification if test
133			 * executed without (-f) option.
134			 */
135			if (STD_FUNCTIONAL_TEST) {
136				stat_buf.st_mode &= ~S_IFREG;
137				/*
138				 * Verify the data returned by stat(2)
139				 * aganist the expected data.
140				 */
141				if ((stat_buf.st_uid != user_id) ||
142				    (stat_buf.st_gid != group_id) ||
143				    (stat_buf.st_size != FILE_SIZE) ||
144				    ((stat_buf.st_mode & MASK) != NEW_MODE)) {
145					tst_resm(TFAIL, "Functionality of "
146						 "stat(2) on '%s' Failed",
147						 TESTFILE);
148				} else {
149					tst_resm(TPASS, "Functionality of "
150						 "stat(2) on '%s' Succcessful",
151						 TESTFILE);
152				}
153			} else {
154				tst_resm(TPASS, "Call succeeded");
155			}
156		}
157		tst_count++;	/* incr TEST_LOOP counter */
158	}
159
160	cleanup();
161	tst_exit();
162	tst_exit();
163
164}
165
166/*
167 * void
168 * setup() -  Performs setup function for the test.
169 *  Creat a temporary directory and change directory to it.
170 *  Creat a testfile and write some data into it.
171 *  Modify the mode permissions of testfile such that test process
172 *  has read-only access to testfile.
173 */
174void setup(void)
175{
176	int i, fd;		/* counter, file handle for file */
177	char tst_buff[BUF_SIZE];	/* data buffer for file */
178	int wbytes;		/* no. of bytes written to file */
179	int write_len = 0;
180
181	tst_sig(NOFORK, DEF_HANDLER, cleanup);
182
183	/* Switch to nobody user for correct error code collection */
184	if (geteuid() != 0) {
185		tst_brkm(TBROK, NULL, "Test must be run as root");
186	}
187	ltpuser = getpwnam(nobody_uid);
188	if (setuid(ltpuser->pw_uid) == -1) {
189		tst_resm(TINFO, "setuid failed to "
190			 "to set the effective uid to %d", ltpuser->pw_uid);
191		perror("setuid");
192	}
193
194	/* Pause if that option was specified
195	 * TEST_PAUSE contains the code to fork the test with the -i option.
196	 * You want to make sure you do this before you create your temporary
197	 * directory.
198	 */
199	TEST_PAUSE;
200
201	tst_tmpdir();
202
203	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
204		tst_brkm(TBROK, cleanup,
205			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
206			 TESTFILE, FILE_MODE, errno, strerror(errno));
207	}
208
209	/* Fill the test buffer with the known data */
210	for (i = 0; i < BUF_SIZE; i++) {
211		tst_buff[i] = 'a';
212	}
213
214	/* Write to the file 1k data from the buffer */
215	while (write_len < FILE_SIZE) {
216		if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
217			tst_brkm(TBROK | TERRNO, cleanup, "write to %s failed",
218				 TESTFILE);
219		} else {
220			write_len += wbytes;
221		}
222	}
223
224	if (close(fd) == -1) {
225		tst_resm(TWARN | TERRNO, "closing %s failed", TESTFILE);
226	}
227
228	/* Modify mode permissions on the testfile */
229	if (chmod(TESTFILE, NEW_MODE) < 0) {
230		tst_brkm(TBROK | TERRNO, cleanup, "chmodding %s failed",
231			 TESTFILE);
232	}
233
234	/* Get the uid/gid of the process */
235	user_id = getuid();
236	group_id = getgid();
237
238}
239
240/*
241 * cleanup() - performs all ONE TIME cleanup for this test at
242 *	       completion or premature exit.
243 *  Remove the temporary directory and file created.
244 */
245void cleanup(void)
246{
247	/*
248	 * print timing stats if that option was specified.
249	 * print errno log if that option was specified.
250	 */
251	TEST_CLEANUP;
252
253	tst_rmdir();
254}
255