blob: 5271b9fbde873de9452a68b682b33e870e1abda8 [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: 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * 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:
plars865695b2001-08-27 22:15:12 +000068 *
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>
robbiewb1c11a12001-08-28 15:47:02 +000077#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000078
79#include "test.h"
plars865695b2001-08-27 22:15:12 +000080
81#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
82#define TESTFILE "testfile"
83#define FILE_SIZE 1024
84#define BUF_SIZE 256
85#define NEW_MODE 0222
86#define MASK 0777
87
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020088char *TCID = "stat02";
89int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080090
Garrett Cooper53740502010-12-16 00:04:01 -080091uid_t user_id; /* eff. user id/group id of test process */
92gid_t group_id;
robbiewb1c11a12001-08-28 15:47:02 +000093char nobody_uid[] = "nobody";
94struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000095
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020096void setup();
97void cleanup();
plars865695b2001-08-27 22:15:12 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000100{
101 struct stat stat_buf; /* stat structure buffer */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200102 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200103 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000104
Garrett Cooper53740502010-12-16 00:04:01 -0800105 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000106 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000107
plars865695b2001-08-27 22:15:12 +0000108 setup();
109
plars865695b2001-08-27 22:15:12 +0000110 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800111
Caspar Zhangd59a6592013-03-07 14:59:12 +0800112 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000113
subrata_modak4bb656a2009-02-26 12:02:09 +0000114 /*
plars865695b2001-08-27 22:15:12 +0000115 * Call stat(2) to get the status of
116 * specified 'file' into stat structure.
117 */
118 TEST(stat(TESTFILE, &stat_buf));
119
plars865695b2001-08-27 22:15:12 +0000120 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000121 tst_resm(TFAIL,
122 "stat(%s, &stat_buf) Failed, errno=%d : %s",
123 TESTFILE, TEST_ERRNO, strerror(TEST_ERRNO));
124 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200125 stat_buf.st_mode &= ~S_IFREG;
plars865695b2001-08-27 22:15:12 +0000126 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200127 * Verify the data returned by stat(2)
128 * aganist the expected data.
plars865695b2001-08-27 22:15:12 +0000129 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 if ((stat_buf.st_uid != user_id) ||
131 (stat_buf.st_gid != group_id) ||
132 (stat_buf.st_size != FILE_SIZE) ||
133 ((stat_buf.st_mode & MASK) != NEW_MODE)) {
134 tst_resm(TFAIL, "Functionality of "
135 "stat(2) on '%s' Failed",
136 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000137 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 tst_resm(TPASS, "Functionality of "
139 "stat(2) on '%s' Succcessful",
140 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000141 }
142 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800143 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800144 }
plars865695b2001-08-27 22:15:12 +0000145
plars865695b2001-08-27 22:15:12 +0000146 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800147 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800148}
plars865695b2001-08-27 22:15:12 +0000149
150/*
151 * void
152 * setup() - Performs setup function for the test.
153 * Creat a temporary directory and change directory to it.
154 * Creat a testfile and write some data into it.
155 * Modify the mode permissions of testfile such that test process
156 * has read-only access to testfile.
157 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400158void setup(void)
plars865695b2001-08-27 22:15:12 +0000159{
subrata_modak56207ce2009-03-23 13:35:39 +0000160 int i, fd; /* counter, file handle for file */
plars865695b2001-08-27 22:15:12 +0000161 char tst_buff[BUF_SIZE]; /* data buffer for file */
subrata_modak56207ce2009-03-23 13:35:39 +0000162 int wbytes; /* no. of bytes written to file */
plars865695b2001-08-27 22:15:12 +0000163 int write_len = 0;
164
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200165 tst_require_root(NULL);
166
plars865695b2001-08-27 22:15:12 +0000167 tst_sig(NOFORK, DEF_HANDLER, cleanup);
168
robbiewb1c11a12001-08-28 15:47:02 +0000169 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000170 ltpuser = getpwnam(nobody_uid);
171 if (setuid(ltpuser->pw_uid) == -1) {
172 tst_resm(TINFO, "setuid failed to "
173 "to set the effective uid to %d", ltpuser->pw_uid);
174 perror("setuid");
175 }
plars865695b2001-08-27 22:15:12 +0000176
177 /* Pause if that option was specified
178 * TEST_PAUSE contains the code to fork the test with the -i option.
179 * You want to make sure you do this before you create your temporary
180 * directory.
181 */
182 TEST_PAUSE;
183
plars865695b2001-08-27 22:15:12 +0000184 tst_tmpdir();
185
subrata_modak56207ce2009-03-23 13:35:39 +0000186 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000187 tst_brkm(TBROK, cleanup,
188 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
189 TESTFILE, FILE_MODE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 }
plars865695b2001-08-27 22:15:12 +0000191
192 /* Fill the test buffer with the known data */
193 for (i = 0; i < BUF_SIZE; i++) {
194 tst_buff[i] = 'a';
195 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000196
plars865695b2001-08-27 22:15:12 +0000197 /* Write to the file 1k data from the buffer */
198 while (write_len < FILE_SIZE) {
199 if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200 tst_brkm(TBROK | TERRNO, cleanup, "write to %s failed",
201 TESTFILE);
Garrett Cooper53740502010-12-16 00:04:01 -0800202 } else {
plars865695b2001-08-27 22:15:12 +0000203 write_len += wbytes;
204 }
205 }
206
207 if (close(fd) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 tst_resm(TWARN | TERRNO, "closing %s failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000209 }
210
211 /* Modify mode permissions on the testfile */
212 if (chmod(TESTFILE, NEW_MODE) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 tst_brkm(TBROK | TERRNO, cleanup, "chmodding %s failed",
214 TESTFILE);
215 }
subrata_modak56207ce2009-03-23 13:35:39 +0000216
plars865695b2001-08-27 22:15:12 +0000217 /* Get the uid/gid of the process */
Garrett Cooper53740502010-12-16 00:04:01 -0800218 user_id = getuid();
219 group_id = getgid();
plars865695b2001-08-27 22:15:12 +0000220
Garrett Cooper2c282152010-12-16 00:55:50 -0800221}
plars865695b2001-08-27 22:15:12 +0000222
223/*
224 * cleanup() - performs all ONE TIME cleanup for this test at
225 * completion or premature exit.
226 * Remove the temporary directory and file created.
227 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400228void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000229{
plars865695b2001-08-27 22:15:12 +0000230
plars865695b2001-08-27 22:15:12 +0000231 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700232}