blob: a5214f763fb239d6149f25dbc0ebec6a0d795972 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: fstat04
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
24 * Verify that, fstat(2) succeeds to get the status of a file pointed by
25 * file descriptor and fills the stat structure elements.
26 *
27 * Expected Result:
28 * fstat() should return value 0 on success and the stat structure should
29 * be filled with specified 'file' information.
30 *
31 * Algorithm:
32 * Setup:
33 * Setup signal handling.
34 * Create temporary directory.
35 * Pause for SIGUSR1 if option specified.
36 *
37 * Test:
38 * Loop if the proper options are given.
39 * Execute system call
40 * Check return code, if system call failed (return=-1)
41 * Log the errno and Issue a FAIL message.
42 * Otherwise,
43 * Verify the Functionality of system call
44 * if successful,
45 * Issue Functionality-Pass message.
46 * Otherwise,
47 * Issue Functionality-Fail message.
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 * Delete the temporary directory created.
51 *
52 * Usage: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000053 * fstat04 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000054 * where, -c n : Run n copies concurrently.
55 * -f : Turn off functionality Testing.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * RESTRICTIONS:
plars865695b2001-08-27 22:15:12 +000065 *
66 */
67#include <stdio.h>
68#include <sys/types.h>
69#include <sys/fcntl.h>
70#include <sys/stat.h>
71#include <errno.h>
72#include <string.h>
73#include <signal.h>
robbiew2fef6012001-08-31 17:43:13 +000074#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000075
76#include "test.h"
77#include "usctest.h"
78
79#define FILE_MODE 0644
80#define TESTFILE "testfile"
81#define FILE_SIZE 1024
82#define BUF_SIZE 256
83#define MASK 0777
84
nstrazfa31d552002-05-14 16:50:06 +000085char *TCID="fstat04"; /* Test program identifier. */
plars865695b2001-08-27 22:15:12 +000086int TST_TOTAL=1; /* Total number of test cases. */
87extern int Tst_count; /* Test Case counter for tst_* routines */
88uid_t User_id; /* user id/group id of test process */
89gid_t Group_id;
90int fildes; /* File descriptor of testfile */
91
robbiew2fef6012001-08-31 17:43:13 +000092char nobody_uid[] = "nobody";
93struct passwd *ltpuser;
94
95
plars865695b2001-08-27 22:15:12 +000096void setup(); /* Setup function for the test */
97void cleanup(); /* Cleanup function for the test */
98
99int
100main(int ac, char **av)
101{
102 struct stat stat_buf; /* stat structure buffer */
103 int lc; /* loop counter */
104 char *msg; /* message returned from parse_opts */
105
106 /* Parse standard options given to run the test. */
107 msg = parse_opts(ac, av, (option_t *) NULL, NULL);
108 if (msg != (char *) NULL) {
109 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
110 tst_exit();
111 }
112
113 /* Perform global setup for test */
114 setup();
115
116 /* Check looping state if -i option given */
117 for (lc = 0; TEST_LOOPING(lc); lc++) {
118 /* Reset Tst_count in case we are looping. */
119 Tst_count = 0;
120
121 /*
122 * Call fstat(2) to get the status of
123 * specified 'file' pointed to 'fd'
124 * into stat structure.
125 */
126 TEST(fstat(fildes, &stat_buf));
127
128 /* check return code of fstat(2) */
129 if (TEST_RETURN == -1) {
130 tst_resm(TFAIL,
131 "fstat on %s Failed, errno=%d : %s",
132 TESTFILE, TEST_ERRNO, strerror(TEST_ERRNO));
133 continue;
134 }
135 /*
136 * Perform functional verification if test
137 * executed without (-f) option.
138 */
139 if (STD_FUNCTIONAL_TEST) {
140 /*
141 * Verify the data returned by fstat(2)
142 * aganist the expected data.
143 */
144 if ((stat_buf.st_uid != User_id) ||
145 (stat_buf.st_gid != Group_id) ||
146 (stat_buf.st_size != FILE_SIZE) ||
147 ((stat_buf.st_mode & MASK) != FILE_MODE)) {
148 tst_resm(TFAIL, "Functionality of fstat(2) on "
149 "'%s' Failed", TESTFILE);
150 } else {
151 tst_resm(TPASS, "Functionality of fstat(2) on "
152 "'%s' Succcessful", TESTFILE);
153 }
154 } else {
155 tst_resm(TPASS, "call succeeded");
156 }
157 } /* End for TEST_LOOPING */
158
159 /* Call cleanup() to undo setup done for the test. */
160 cleanup();
161
162 /*NOTREACHED*/
subrata_modak43337a32009-02-26 11:43:51 +0000163 return 0;
plars865695b2001-08-27 22:15:12 +0000164} /* End main */
165
166/*
167 * void
168 * setup() - Performs setup function for the test.
169 * Creat a temporary directory and chdir to it.
170 * Creat a test file and write some data into it.
171 * Get the user/group id info. of test process.
172 */
173void
174setup()
175{
176 int i; /* counter */
177 char tst_buff[BUF_SIZE]; /* data buffer */
178 int wbytes; /* no. of bytes written */
179 int write_len = 0; /* data length */
180
181 /* capture signals */
182 tst_sig(NOFORK, DEF_HANDLER, cleanup);
183
robbiew2fef6012001-08-31 17:43:13 +0000184 /* Switch to nobody user for correct error code collection */
185 if (geteuid() != 0) {
186 tst_brkm(TBROK, tst_exit, "Test must be run as root");
187 }
188 ltpuser = getpwnam(nobody_uid);
189 if (setuid(ltpuser->pw_uid) == -1) {
190 tst_resm(TINFO, "setuid failed to "
191 "to set the effective uid to %d",
192 ltpuser->pw_uid);
193 perror("setuid");
194 }
195
plars865695b2001-08-27 22:15:12 +0000196
197 /* Pause if that option was specified */
198 TEST_PAUSE;
199
200 /* make a temp directory and cd to it */
201 tst_tmpdir();
202
203 if ((fildes = 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(fildes, tst_buff, sizeof(tst_buff))) <= 0) {
217 tst_brkm(TBROK, cleanup,
218 "write(2) on %s Failed, errno=%d : %s",
219 TESTFILE, errno, strerror(errno));
220 } else {
221 write_len += wbytes;
222 }
223 }
224
225 /* Get the uid/gid of the process */
226 User_id = getuid();
227 Group_id = getgid();
228
229} /* End setup() */
230
231/*
232 * cleanup() - performs all ONE TIME cleanup for this test at
233 * completion or premature exit.
234 * Close the testfile opened for reading/writing.
235 * Delete the testfile and temporary directory.
236 */
237void
238cleanup()
239{
240 /*
241 * print timing stats if that option was specified.
242 * print errno log if that option was specified.
243 */
244 TEST_CLEANUP;
245
246 /* Close the test file */
247 if (close(fildes) == -1) {
248 tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s",
249 TESTFILE, errno, strerror(errno));
250 }
251
252 /* Remove tmp dir and all files in it */
253 tst_rmdir();
254
255 /* exit with return code appropriate for results */
256 tst_exit();
257} /* End cleanup() */