blob: 3f0d13cf94864093a288187a258a6b06918c691a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis1065dc42013-06-04 19:09:15 +02002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Cyril Hrubis1065dc42013-06-04 19:09:15 +02004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +00008 *
Cyril Hrubis1065dc42013-06-04 19:09:15 +02009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000013 *
Cyril Hrubis1065dc42013-06-04 19:09:15 +020014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
plars865695b2001-08-27 22:15:12 +000020 * Test Description:
21 * Verify that, stat(2) succeeds to get the status of a file and fills the
22 * stat structure elements.
23 *
24 * Expected Result:
25 * stat() should return value 0 on success and fills the stat structure
26 * elements with the specified 'file' information.
27 *
28 * Algorithm:
29 * Setup:
30 * Setup signal handling.
31 * Create temporary directory.
32 * Pause for SIGUSR1 if option specified.
33 *
34 * Test:
35 * Loop if the proper options are given.
36 * Execute system call
37 * Check return code, if system call failed (return=-1)
38 * Log the errno and Issue a FAIL message.
39 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000040 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000041 * if successful,
42 * Issue Functionality-Pass message.
43 * Otherwise,
44 * Issue Functionality-Fail message.
plars865695b2001-08-27 22:15:12 +000045 * History
46 * 07/2001 John George
47 * -Ported
plars865695b2001-08-27 22:15:12 +000048 */
49#include <stdio.h>
50#include <sys/types.h>
51#include <sys/fcntl.h>
52#include <sys/stat.h>
53#include <errno.h>
54#include <string.h>
55#include <signal.h>
robbiewb1c11a12001-08-28 15:47:02 +000056#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000057
58#include "test.h"
plars865695b2001-08-27 22:15:12 +000059
60#define FILE_MODE 0644
61#define TESTFILE "testfile"
62#define FILE_SIZE 1024
63#define BUF_SIZE 256
64#define MASK 0777
65
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020066char *TCID = "stat01";
67int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080068
Cyril Hrubis1065dc42013-06-04 19:09:15 +020069uid_t user_id;
70gid_t group_id;
robbiewb1c11a12001-08-28 15:47:02 +000071char nobody_uid[] = "nobody";
72struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000073
Cyril Hrubis1065dc42013-06-04 19:09:15 +020074static void setup(void);
75static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000076
subrata_modak56207ce2009-03-23 13:35:39 +000077int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000078{
Cyril Hrubis1065dc42013-06-04 19:09:15 +020079 struct stat stat_buf;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020080 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020081 const char *msg;
plars865695b2001-08-27 22:15:12 +000082
Garrett Cooper53740502010-12-16 00:04:01 -080083 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000084 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000085
plars865695b2001-08-27 22:15:12 +000086 setup();
87
plars865695b2001-08-27 22:15:12 +000088 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080089
Caspar Zhangd59a6592013-03-07 14:59:12 +080090 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000091
subrata_modak4bb656a2009-02-26 12:02:09 +000092 /*
plars865695b2001-08-27 22:15:12 +000093 * Call stat(2) to get the status of
94 * specified 'file' into stat structure.
95 */
96 TEST(stat(TESTFILE, &stat_buf));
subrata_modakbdbaec52009-02-26 12:14:51 +000097
plars865695b2001-08-27 22:15:12 +000098 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +000099 tst_resm(TFAIL,
plars865695b2001-08-27 22:15:12 +0000100 "stat(%s, &stat_buf) Failed, errno=%d : %s",
101 TESTFILE, TEST_ERRNO, strerror(TEST_ERRNO));
102 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200103 stat_buf.st_mode &= ~S_IFREG;
plars865695b2001-08-27 22:15:12 +0000104 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200105 * Verify the data returned by stat(2)
106 * aganist the expected data.
plars865695b2001-08-27 22:15:12 +0000107 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200108 if ((stat_buf.st_uid != user_id) ||
109 (stat_buf.st_gid != group_id) ||
110 (stat_buf.st_size != FILE_SIZE) ||
111 ((stat_buf.st_mode & MASK) != FILE_MODE)) {
112 tst_resm(TFAIL, "Functionality of "
113 "stat(2) on '%s' Failed",
114 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000115 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200116 tst_resm(TPASS, "Functionality of "
117 "stat(2) on '%s' Succcessful",
118 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000119 }
120 }
Cyril Hrubis1065dc42013-06-04 19:09:15 +0200121 tst_count++;
Garrett Cooper2c282152010-12-16 00:55:50 -0800122 }
plars865695b2001-08-27 22:15:12 +0000123
plars865695b2001-08-27 22:15:12 +0000124 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800125 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800126}
plars865695b2001-08-27 22:15:12 +0000127
Cyril Hrubis1065dc42013-06-04 19:09:15 +0200128void setup(void)
plars865695b2001-08-27 22:15:12 +0000129{
130 int i, fd;
131 char tst_buff[BUF_SIZE];
132 int wbytes;
133 int write_len = 0;
134
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200135 tst_require_root(NULL);
136
plars865695b2001-08-27 22:15:12 +0000137 tst_sig(NOFORK, DEF_HANDLER, cleanup);
subrata_modak56207ce2009-03-23 13:35:39 +0000138
robbiewb1c11a12001-08-28 15:47:02 +0000139 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000140 ltpuser = getpwnam(nobody_uid);
141 if (setuid(ltpuser->pw_uid) == -1) {
142 tst_resm(TINFO, "setuid failed to "
143 "to set the effective uid to %d", ltpuser->pw_uid);
144 perror("setuid");
145 }
plars865695b2001-08-27 22:15:12 +0000146
147 /* Pause if that option was specified
148 * TEST_PAUSE contains the code to fork the test with the -i option.
149 * You want to make sure you do this before you create your temporary
150 * directory.
151 */
152 TEST_PAUSE;
153
plars865695b2001-08-27 22:15:12 +0000154 tst_tmpdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000155
156 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000157 tst_brkm(TBROK, cleanup,
158 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
159 TESTFILE, FILE_MODE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 }
plars865695b2001-08-27 22:15:12 +0000161
162 /* Fill the test buffer with the known data */
Cyril Hrubis1065dc42013-06-04 19:09:15 +0200163 for (i = 0; i < BUF_SIZE; i++)
plars865695b2001-08-27 22:15:12 +0000164 tst_buff[i] = 'a';
subrata_modakbdbaec52009-02-26 12:14:51 +0000165
plars865695b2001-08-27 22:15:12 +0000166 /* Write to the file 1k data from the buffer */
167 while (write_len < FILE_SIZE) {
168 if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
169 tst_brkm(TBROK, cleanup,
170 "write(2) on %s Failed, errno=%d : %s",
171 TESTFILE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 } else {
plars865695b2001-08-27 22:15:12 +0000173 write_len += wbytes;
174 }
175 }
176
177 /* Close the testfile created */
178 if (close(fd) == -1) {
179 tst_resm(TWARN, "close(%s) Failed, errno=%d : %s",
180 TESTFILE, errno, strerror(errno));
181 }
182
183 /* Get the uid/gid of the process */
Garrett Cooper53740502010-12-16 00:04:01 -0800184 user_id = getuid();
185 group_id = getgid();
Garrett Cooper2c282152010-12-16 00:55:50 -0800186}
plars865695b2001-08-27 22:15:12 +0000187
Cyril Hrubis1065dc42013-06-04 19:09:15 +0200188static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000189{
plars865695b2001-08-27 22:15:12 +0000190 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700191}