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 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: fstat04 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 41 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 53 | * fstat04 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 65 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | */ |
| 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> |
robbiew | 2fef601 | 2001-08-31 17:43:13 +0000 | [diff] [blame] | 74 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 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 Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 84 | char *TCID = "fstat04"; |
| 85 | int TST_TOTAL = 1; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 86 | uid_t user_id; /* user id/group id of test process */ |
| 87 | gid_t group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | int fildes; /* File descriptor of testfile */ |
| 89 | |
robbiew | 2fef601 | 2001-08-31 17:43:13 +0000 | [diff] [blame] | 90 | char nobody_uid[] = "nobody"; |
| 91 | struct passwd *ltpuser; |
| 92 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 93 | void setup(); |
| 94 | void cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | { |
| 98 | struct stat stat_buf; /* stat structure buffer */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 99 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 100 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 102 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | setup(); |
| 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 108 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 109 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 110 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 111 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | * 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | tst_resm(TFAIL | TTERRNO, "fstat failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 123 | * Verify the data returned by fstat(2) |
| 124 | * aganist the expected data. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 126 | 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 Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 132 | } else |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | tst_resm(TPASS, "fstat functionality correct"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | cleanup(); |
Garrett Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 137 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 138 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 147 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | char tst_buff[BUF_SIZE]; /* data buffer */ |
| 150 | int wbytes; /* no. of bytes written */ |
| 151 | int write_len = 0; /* data length */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
Garrett Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 153 | tst_require_root(NULL); |
robbiew | 2fef601 | 2001-08-31 17:43:13 +0000 | [diff] [blame] | 154 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 156 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed"); |
vapier | 293f19b | 2009-08-28 12:51:34 +0000 | [diff] [blame] | 158 | if (setuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | tst_brkm(TBROK | TERRNO, NULL, "setuid failed"); |
Garrett Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 160 | |
| 161 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | TEST_PAUSE; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | tst_tmpdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | |
vapier | 293f19b | 2009-08-28 12:51:34 +0000 | [diff] [blame] | 167 | fildes = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE); |
| 168 | if (fildes == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | tst_brkm(TBROK | TERRNO, cleanup, "open failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 171 | memset(tst_buff, 'a', BUF_SIZE - 1); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | while (write_len < FILE_SIZE) { |
Garrett Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 174 | if ((wbytes = write(fildes, tst_buff, sizeof(tst_buff))) <= 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_brkm(TBROK | TERRNO, cleanup, "write failed"); |
Garrett Cooper | 5790ecf | 2010-12-18 21:45:20 -0800 | [diff] [blame] | 176 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | write_len += wbytes; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 180 | user_id = getuid(); |
| 181 | group_id = getgid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 183 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 185 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | { |
vapier | 293f19b | 2009-08-28 12:51:34 +0000 | [diff] [blame] | 187 | if (close(fildes) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | tst_resm(TWARN | TERRNO, "close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | tst_rmdir(); |
| 191 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 192 | } |