Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * 07/2001 John George |
| 4 | * -Ported |
| 5 | * |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 6 | * check stat() with various error conditions that should produce |
| 7 | * EACCES, EFAULT, ENAMETOOLONG, ENOENT, ENOTDIR, ELOOP |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 10 | #include <fcntl.h> |
| 11 | #include <errno.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 14 | #include <pwd.h> |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 15 | #include "tst_test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 16 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 17 | #define TST_EACCES_DIR "tst_eaccesdir" |
| 18 | #define TST_EACCES_FILE "tst_eaccesdir/tst" |
| 19 | #define TST_ENOENT "tst_enoent/tst" |
| 20 | #define TST_ENOTDIR_DIR "tst_enotdir/tst" |
| 21 | #define TST_ENOTDIR_FILE "tst_enotdir" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 23 | #define MODE_RW 0666 |
| 24 | #define DIR_MODE 0755 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 26 | struct passwd *ltpuser; |
| 27 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 28 | static char long_dir[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'}; |
| 29 | static char loop_dir[PATH_MAX] = "."; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 31 | static struct tcase{ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | char *pathname; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | int exp_errno; |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 34 | } TC[] = { |
| 35 | {TST_EACCES_FILE, EACCES}, |
| 36 | {NULL, EFAULT}, |
| 37 | {long_dir, ENAMETOOLONG}, |
| 38 | {TST_ENOENT, ENOENT}, |
| 39 | {TST_ENOTDIR_DIR, ENOTDIR}, |
| 40 | {loop_dir, ELOOP} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 43 | static void verify_stat(unsigned int n) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | { |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 45 | struct tcase *tc = TC + n; |
| 46 | struct stat stat_buf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 48 | TEST(stat(tc->pathname, &stat_buf)); |
| 49 | if (TST_RET != -1) { |
| 50 | tst_res(TFAIL, "stat() succeeded unexpectedly"); |
| 51 | return; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 52 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 54 | if (TST_ERR == tc->exp_errno) { |
| 55 | tst_res(TPASS | TTERRNO, "stat() failed as expected"); |
| 56 | } else { |
| 57 | tst_res(TFAIL | TTERRNO, |
| 58 | "stat() failed unexpectedly; expected: %d - %s", |
| 59 | tc->exp_errno, tst_strerrno(tc->exp_errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 61 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 63 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | { |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 65 | unsigned int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 67 | ltpuser = SAFE_GETPWNAM("nobody"); |
| 68 | SAFE_SETUID(ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 70 | SAFE_MKDIR(TST_EACCES_DIR, DIR_MODE); |
| 71 | SAFE_TOUCH(TST_EACCES_FILE, DIR_MODE, NULL); |
| 72 | SAFE_CHMOD(TST_EACCES_DIR, MODE_RW); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 74 | for (i = 0; i < ARRAY_SIZE(TC); i++) { |
| 75 | if (TC[i].exp_errno == EFAULT) |
| 76 | TC[i].pathname = tst_get_bad_addr(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 79 | SAFE_TOUCH(TST_ENOTDIR_FILE, DIR_MODE, NULL); |
| 80 | |
| 81 | SAFE_MKDIR("test_eloop", DIR_MODE); |
| 82 | SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop"); |
| 83 | for (i = 0; i < 43; i++) |
| 84 | strcat(loop_dir, "/test_eloop"); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 85 | } |
Yang Xu | 7782f40 | 2019-04-08 14:52:27 +0800 | [diff] [blame] | 86 | |
| 87 | static struct tst_test test = { |
| 88 | .tcnt = ARRAY_SIZE(TC), |
| 89 | .needs_tmpdir = 1, |
| 90 | .needs_root = 1, |
| 91 | .setup = setup, |
| 92 | .test = verify_stat, |
| 93 | }; |