blob: e9510c189469467bd34f34087e96532820b36e2c [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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)
subrata_modak56207ce2009-03-23 13:35:39 +000041 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000042 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000046 * 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:
subrata_modak4bb656a2009-02-26 12:02:09 +000065 *
plars865695b2001-08-27 22:15:12 +000066 */
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"
plars865695b2001-08-27 22:15:12 +000077
78#define FILE_MODE 0644
79#define TESTFILE "testfile"
80#define FILE_SIZE 1024
81#define BUF_SIZE 256
82#define MASK 0777
83
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020084char *TCID = "fstat04";
85int TST_TOTAL = 1;
Garrett Cooper53740502010-12-16 00:04:01 -080086uid_t user_id; /* user id/group id of test process */
87gid_t group_id;
plars865695b2001-08-27 22:15:12 +000088int fildes; /* File descriptor of testfile */
89
robbiew2fef6012001-08-31 17:43:13 +000090char nobody_uid[] = "nobody";
91struct passwd *ltpuser;
92
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020093void setup();
94void cleanup();
plars865695b2001-08-27 22:15:12 +000095
subrata_modak56207ce2009-03-23 13:35:39 +000096int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000097{
98 struct stat stat_buf; /* stat structure buffer */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020099 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200100 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000101
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800102 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000103 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800104
plars865695b2001-08-27 22:15:12 +0000105 setup();
106
plars865695b2001-08-27 22:15:12 +0000107 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800108
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000110
subrata_modak4bb656a2009-02-26 12:02:09 +0000111 /*
plars865695b2001-08-27 22:15:12 +0000112 * Call fstat(2) to get the status of
113 * specified 'file' pointed to 'fd'
114 * into stat structure.
115 */
116 TEST(fstat(fildes, &stat_buf));
subrata_modakbdbaec52009-02-26 12:14:51 +0000117
plars865695b2001-08-27 22:15:12 +0000118 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 tst_resm(TFAIL | TTERRNO, "fstat failed");
plars865695b2001-08-27 22:15:12 +0000120 continue;
121 }
122 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200123 * Verify the data returned by fstat(2)
124 * aganist the expected data.
plars865695b2001-08-27 22:15:12 +0000125 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 if (stat_buf.st_uid != user_id ||
127 stat_buf.st_gid != group_id ||
128 stat_buf.st_size != FILE_SIZE ||
129 (stat_buf.st_mode & MASK) != FILE_MODE) {
130 tst_resm(TFAIL,
131 "fstat functionality incorrect");
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800132 } else
Cyril Hrubise38b9612014-06-02 17:20:57 +0200133 tst_resm(TPASS, "fstat functionality correct");
Garrett Cooper2c282152010-12-16 00:55:50 -0800134 }
plars865695b2001-08-27 22:15:12 +0000135
plars865695b2001-08-27 22:15:12 +0000136 cleanup();
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800137 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800138}
plars865695b2001-08-27 22:15:12 +0000139
140/*
141 * void
142 * setup() - Performs setup function for the test.
143 * Creat a temporary directory and chdir to it.
144 * Creat a test file and write some data into it.
145 * Get the user/group id info. of test process.
146 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400147void setup(void)
plars865695b2001-08-27 22:15:12 +0000148{
subrata_modak56207ce2009-03-23 13:35:39 +0000149 char tst_buff[BUF_SIZE]; /* data buffer */
150 int wbytes; /* no. of bytes written */
151 int write_len = 0; /* data length */
plars865695b2001-08-27 22:15:12 +0000152
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800153 tst_require_root(NULL);
robbiew2fef6012001-08-31 17:43:13 +0000154
subrata_modak56207ce2009-03-23 13:35:39 +0000155 ltpuser = getpwnam(nobody_uid);
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800156 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
vapier293f19b2009-08-28 12:51:34 +0000158 if (setuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 tst_brkm(TBROK | TERRNO, NULL, "setuid failed");
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800160
161 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000162
plars865695b2001-08-27 22:15:12 +0000163 TEST_PAUSE;
subrata_modak56207ce2009-03-23 13:35:39 +0000164
plars865695b2001-08-27 22:15:12 +0000165 tst_tmpdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000166
vapier293f19b2009-08-28 12:51:34 +0000167 fildes = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
168 if (fildes == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
plars865695b2001-08-27 22:15:12 +0000170
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 memset(tst_buff, 'a', BUF_SIZE - 1);
subrata_modakbdbaec52009-02-26 12:14:51 +0000172
plars865695b2001-08-27 22:15:12 +0000173 while (write_len < FILE_SIZE) {
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800174 if ((wbytes = write(fildes, tst_buff, sizeof(tst_buff))) <= 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_brkm(TBROK | TERRNO, cleanup, "write failed");
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800176 else
plars865695b2001-08-27 22:15:12 +0000177 write_len += wbytes;
plars865695b2001-08-27 22:15:12 +0000178 }
179
Garrett Cooper53740502010-12-16 00:04:01 -0800180 user_id = getuid();
181 group_id = getgid();
plars865695b2001-08-27 22:15:12 +0000182
Garrett Cooper2c282152010-12-16 00:55:50 -0800183}
plars865695b2001-08-27 22:15:12 +0000184
Mike Frysingerc57fba52014-04-09 18:56:30 -0400185void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000186{
vapier293f19b2009-08-28 12:51:34 +0000187 if (close(fildes) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 tst_resm(TWARN | TERRNO, "close failed");
plars865695b2001-08-27 22:15:12 +0000189
plars865695b2001-08-27 22:15:12 +0000190 tst_rmdir();
191
Chris Dearmanec6edca2012-10-17 19:54:01 -0700192}