blob: 1f0964aed787c2860f9197fc1a0adbe39e306e0c [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/*
21 * Test Name: fstat02
22 *
23 * Test Description:
24 * Verify that, fstat(2) succeeds to get the status of a file and fills
25 * the stat structure elements though file pointed to by file descriptor
26 * not opened for reading.
27 *
28 * Expected Result:
29 * fstat() 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)
subrata_modak56207ce2009-03-23 13:35:39 +000042 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000043 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000046 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000047 * 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 * fstat02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
55 * where, -c n : Run n copies concurrently.
56 * -f : Turn off functionality Testing.
57 * -i n : Execute test n times.
58 * -I x : Execute test for x seconds.
59 * -P x : Pause for x seconds between iterations.
60 * -t : Turn on syscall timing.
61 *
62 * HISTORY
63 * 07/2001 Ported by Wayne Boyer
64 *
65 * RESTRICTIONS:
plars865695b2001-08-27 22:15:12 +000066 *
67 */
68#include <stdio.h>
69#include <sys/types.h>
70#include <sys/fcntl.h>
71#include <sys/stat.h>
72#include <errno.h>
73#include <string.h>
74#include <signal.h>
robbiew2fef6012001-08-31 17:43:13 +000075#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000076
77#include "test.h"
plars865695b2001-08-27 22:15:12 +000078
79#define FILE_MODE 0644
80#define TESTFILE "testfile"
81#define FILE_SIZE 1024
82#define BUF_SIZE 256
83#define MASK 0777
84
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020085char *TCID = "fstat02";
86int TST_TOTAL = 1;
Garrett Cooper53740502010-12-16 00:04:01 -080087uid_t user_id; /* user id/group id of test process */
88gid_t group_id;
plars865695b2001-08-27 22:15:12 +000089int fildes; /* File descriptor of testfile */
90
robbiew2fef6012001-08-31 17:43:13 +000091char nobody_uid[] = "nobody";
92struct passwd *ltpuser;
93
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020094void setup();
95void cleanup();
plars865695b2001-08-27 22:15:12 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000098{
99 struct stat stat_buf; /* stat structure buffer */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200100 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200101 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000102
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800103 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000104 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800105
plars865695b2001-08-27 22:15:12 +0000106 setup();
107
plars865695b2001-08-27 22:15:12 +0000108 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800109
Caspar Zhangd59a6592013-03-07 14:59:12 +0800110 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000111
plars865695b2001-08-27 22:15:12 +0000112 TEST(fstat(fildes, &stat_buf));
subrata_modakbdbaec52009-02-26 12:14:51 +0000113
plars865695b2001-08-27 22:15:12 +0000114 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115 tst_resm(TFAIL | TTERRNO, "fstat(%s) failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000116 continue;
117 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200118 if (stat_buf.st_uid != user_id ||
119 stat_buf.st_gid != group_id ||
120 stat_buf.st_size != FILE_SIZE ||
121 (stat_buf.st_mode & MASK) != FILE_MODE) {
122 tst_resm(TFAIL,
123 "functionality of fstat incorrect");
124 } else {
125 tst_resm(TPASS,
126 "functionality of fstat correct");
127 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800128 }
plars865695b2001-08-27 22:15:12 +0000129
plars865695b2001-08-27 22:15:12 +0000130 cleanup();
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800131 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800132}
plars865695b2001-08-27 22:15:12 +0000133
Mike Frysingerc57fba52014-04-09 18:56:30 -0400134void setup(void)
plars865695b2001-08-27 22:15:12 +0000135{
plars865695b2001-08-27 22:15:12 +0000136 char tst_buff[BUF_SIZE];
137 int wbytes;
138 int write_len = 0;
139
plars865695b2001-08-27 22:15:12 +0000140 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew2fef6012001-08-31 17:43:13 +0000141
subrata_modak56207ce2009-03-23 13:35:39 +0000142 ltpuser = getpwnam(nobody_uid);
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800143 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
vapier293f19b2009-08-28 12:51:34 +0000145 if (setuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 tst_brkm(TBROK | TERRNO, NULL, "setuid failed");
plars865695b2001-08-27 22:15:12 +0000147
plars865695b2001-08-27 22:15:12 +0000148 TEST_PAUSE;
subrata_modak56207ce2009-03-23 13:35:39 +0000149
plars865695b2001-08-27 22:15:12 +0000150 tst_tmpdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000151
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 fildes = open(TESTFILE, O_WRONLY | O_CREAT, FILE_MODE);
vapier293f19b2009-08-28 12:51:34 +0000153 if (fildes == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
plars865695b2001-08-27 22:15:12 +0000155
156 /* Fill the test buffer with the known data */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 memset(tst_buff, 'a', BUF_SIZE - 1);
subrata_modakbdbaec52009-02-26 12:14:51 +0000158
plars865695b2001-08-27 22:15:12 +0000159 /* Write to the file 1k data from the buffer */
160 while (write_len < FILE_SIZE) {
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800161 if ((wbytes = write(fildes, tst_buff, sizeof(tst_buff))) <= 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 tst_brkm(TBROK | TERRNO, cleanup, "write failed");
Garrett Cooper5790ecf2010-12-18 21:45:20 -0800163 else
plars865695b2001-08-27 22:15:12 +0000164 write_len += wbytes;
plars865695b2001-08-27 22:15:12 +0000165 }
166
Garrett Cooper53740502010-12-16 00:04:01 -0800167 user_id = getuid();
168 group_id = getgid();
plars865695b2001-08-27 22:15:12 +0000169
Garrett Cooper2c282152010-12-16 00:55:50 -0800170}
plars865695b2001-08-27 22:15:12 +0000171
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000173{
vapier293f19b2009-08-28 12:51:34 +0000174 if (close(fildes) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_resm(TWARN | TERRNO, "close failed");
plars865695b2001-08-27 22:15:12 +0000176
plars865695b2001-08-27 22:15:12 +0000177 tst_rmdir();
178
Chris Dearmanec6edca2012-10-17 19:54:01 -0700179}