plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 4 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 9 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 14 | * 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 40 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * if successful, |
| 42 | * Issue Functionality-Pass message. |
| 43 | * Otherwise, |
| 44 | * Issue Functionality-Fail message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * History |
| 46 | * 07/2001 John George |
| 47 | * -Ported |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | */ |
| 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> |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 56 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 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 Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 66 | char *TCID = "stat01"; |
| 67 | int TST_TOTAL = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 68 | |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 69 | uid_t user_id; |
| 70 | gid_t group_id; |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 71 | char nobody_uid[] = "nobody"; |
| 72 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 74 | static void setup(void); |
| 75 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | { |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 79 | struct stat stat_buf; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 80 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 81 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 83 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | setup(); |
| 87 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 89 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 90 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 91 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 92 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | * Call stat(2) to get the status of |
| 94 | * specified 'file' into stat structure. |
| 95 | */ |
| 96 | TEST(stat(TESTFILE, &stat_buf)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | tst_resm(TFAIL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | "stat(%s, &stat_buf) Failed, errno=%d : %s", |
| 101 | TESTFILE, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 102 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 103 | stat_buf.st_mode &= ~S_IFREG; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 105 | * Verify the data returned by stat(2) |
| 106 | * aganist the expected data. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 108 | 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 116 | tst_resm(TPASS, "Functionality of " |
| 117 | "stat(2) on '%s' Succcessful", |
| 118 | TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 121 | tst_count++; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 125 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 126 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 128 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | { |
| 130 | int i, fd; |
| 131 | char tst_buff[BUF_SIZE]; |
| 132 | int wbytes; |
| 133 | int write_len = 0; |
| 134 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 135 | tst_require_root(NULL); |
| 136 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 139 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
| 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | tst_tmpdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | |
| 156 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | tst_brkm(TBROK, cleanup, |
| 158 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s", |
| 159 | TESTFILE, FILE_MODE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
| 162 | /* Fill the test buffer with the known data */ |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 163 | for (i = 0; i < BUF_SIZE; i++) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | tst_buff[i] = 'a'; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 165 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | /* 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | 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 Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 184 | user_id = getuid(); |
| 185 | group_id = getgid(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 186 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | |
Cyril Hrubis | 1065dc4 | 2013-06-04 19:09:15 +0200 | [diff] [blame] | 188 | static void cleanup(void) |
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(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 191 | } |