plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * 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: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | * |
| 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> |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 77 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 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 Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 88 | char *TCID = "stat02"; |
| 89 | int TST_TOTAL = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 91 | uid_t user_id; /* eff. user id/group id of test process */ |
| 92 | gid_t group_id; |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 93 | char nobody_uid[] = "nobody"; |
| 94 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 96 | void setup(); |
| 97 | void cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | { |
| 101 | struct stat stat_buf; /* stat structure buffer */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 102 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 103 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 105 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | setup(); |
| 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 111 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 112 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 114 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | * Call stat(2) to get the status of |
| 116 | * specified 'file' into stat structure. |
| 117 | */ |
| 118 | TEST(stat(TESTFILE, &stat_buf)); |
| 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_resm(TFAIL, |
| 122 | "stat(%s, &stat_buf) Failed, errno=%d : %s", |
| 123 | TESTFILE, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 124 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 125 | stat_buf.st_mode &= ~S_IFREG; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 127 | * Verify the data returned by stat(2) |
| 128 | * aganist the expected data. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | tst_resm(TPASS, "Functionality of " |
| 139 | "stat(2) on '%s' Succcessful", |
| 140 | TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 143 | tst_count++; /* incr TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 144 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 147 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 158 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | int i, fd; /* counter, file handle for file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | char tst_buff[BUF_SIZE]; /* data buffer for file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | int wbytes; /* no. of bytes written to file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | int write_len = 0; |
| 164 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 165 | tst_require_root(NULL); |
| 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 168 | |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 169 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | |
| 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | tst_tmpdir(); |
| 185 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | tst_brkm(TBROK, cleanup, |
| 188 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s", |
| 189 | TESTFILE, FILE_MODE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
| 192 | /* Fill the test buffer with the known data */ |
| 193 | for (i = 0; i < BUF_SIZE; i++) { |
| 194 | tst_buff[i] = 'a'; |
| 195 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 196 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | /* 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 200 | tst_brkm(TBROK | TERRNO, cleanup, "write to %s failed", |
| 201 | TESTFILE); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 202 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | write_len += wbytes; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (close(fd) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 208 | tst_resm(TWARN | TERRNO, "closing %s failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* Modify mode permissions on the testfile */ |
| 212 | if (chmod(TESTFILE, NEW_MODE) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 213 | tst_brkm(TBROK | TERRNO, cleanup, "chmodding %s failed", |
| 214 | TESTFILE); |
| 215 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | /* Get the uid/gid of the process */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 218 | user_id = getuid(); |
| 219 | group_id = getgid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 221 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 228 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 232 | } |