Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Red Hat, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it |
| 13 | * is free of the rightful claim of any third person regarding |
| 14 | * infringement or the like. Any license provided herein, whether |
| 15 | * implied or otherwise, applies only to this software file. Patent |
| 16 | * licenses, if any, provided herein do not apply to combinations of |
| 17 | * this program with other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 22 | * 02110-1301, USA. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * In the user.* namespace, only regular files and directories can |
| 27 | * have extended attributes. Otherwise getxattr(2) will return -1 |
| 28 | * and set errno to ENODATA. |
| 29 | * |
| 30 | * There are 4 test cases: |
| 31 | * 1. Get attribute from a FIFO, setxattr(2) should return -1 and |
| 32 | * set errno to ENODATA |
| 33 | * 2. Get attribute from a char special file, setxattr(2) should |
| 34 | * return -1 and set errno to ENODATA |
| 35 | * 3. Get attribute from a block special file, setxattr(2) should |
| 36 | * return -1 and set errno to ENODATA |
| 37 | * 4. Get attribute from a UNIX domain socket, setxattr(2) should |
| 38 | * return -1 and set errno to ENODATA |
| 39 | */ |
| 40 | |
Caspar Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 41 | #include "config.h" |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/wait.h> |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 45 | #include <errno.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <unistd.h> |
| 48 | #include <signal.h> |
| 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
Caspar Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 52 | #ifdef HAVE_ATTR_XATTR_H |
| 53 | #include <attr/xattr.h> |
| 54 | #endif |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 55 | #include "test.h" |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 56 | |
Caspar Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 57 | char *TCID = "getxattr02"; |
| 58 | |
| 59 | #ifdef HAVE_ATTR_XATTR_H |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 60 | #define XATTR_TEST_KEY "user.testkey" |
| 61 | |
| 62 | #define FIFO "getxattr02fifo" |
| 63 | #define CHR "getxattr02chr" |
| 64 | #define BLK "getxattr02blk" |
| 65 | #define SOCK "getxattr02sock" |
| 66 | |
| 67 | static void setup(void); |
| 68 | static void cleanup(void); |
| 69 | |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 70 | static char *tc[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | FIFO, /* case 00, get attr from fifo */ |
| 72 | CHR, /* case 01, get attr from char special */ |
| 73 | BLK, /* case 02, get attr from block special */ |
| 74 | SOCK, /* case 03, get attr from UNIX domain socket */ |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | int TST_TOTAL = sizeof(tc) / sizeof(tc[0]); |
| 78 | |
| 79 | int main(int argc, char *argv[]) |
| 80 | { |
| 81 | int lc; |
| 82 | int i; |
Peng Haitao | 3d2ae5e | 2012-01-10 11:34:14 +0800 | [diff] [blame] | 83 | int exp_eno; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 84 | const char *msg; |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 85 | char buf[BUFSIZ]; |
| 86 | |
| 87 | msg = parse_opts(argc, argv, NULL, NULL); |
| 88 | if (msg != NULL) |
| 89 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 90 | |
| 91 | setup(); |
| 92 | |
| 93 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 94 | tst_count = 0; |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 95 | |
Peng Haitao | 3d2ae5e | 2012-01-10 11:34:14 +0800 | [diff] [blame] | 96 | /* |
| 97 | * Before kernel 3.0.0, getxattr(2) will set errno with 'EPERM' |
| 98 | * when the file is not a regular file and directory, refer to |
| 99 | * commitid 55b23bd |
| 100 | */ |
| 101 | if (tst_kvercmp(3, 0, 0) >= 0) |
| 102 | exp_eno = ENODATA; |
| 103 | else |
| 104 | exp_eno = EPERM; |
| 105 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | for (i = 0; i < TST_TOTAL; i++) { |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 107 | TEST(getxattr(tc[0], XATTR_TEST_KEY, buf, BUFSIZ)); |
| 108 | |
Peng Haitao | 3d2ae5e | 2012-01-10 11:34:14 +0800 | [diff] [blame] | 109 | if (TEST_RETURN == -1 && TEST_ERRNO == exp_eno) |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 110 | tst_resm(TPASS | TTERRNO, "expected behavior"); |
Peng Haitao | 3d2ae5e | 2012-01-10 11:34:14 +0800 | [diff] [blame] | 111 | else |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 112 | tst_resm(TFAIL | TTERRNO, "unexpected behavior" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | " - expected errno %d - Got", exp_eno); |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | cleanup(); |
| 118 | tst_exit(); |
| 119 | } |
| 120 | |
| 121 | static void setup(void) |
| 122 | { |
| 123 | int fd; |
| 124 | |
| 125 | tst_require_root(NULL); |
| 126 | |
| 127 | tst_tmpdir(); |
| 128 | |
| 129 | /* Test for xattr support */ |
| 130 | fd = creat("testfile", 0644); |
| 131 | if (fd == -1) |
| 132 | tst_brkm(TBROK | TERRNO, cleanup, "Create testfile failed"); |
| 133 | close(fd); |
| 134 | if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1) |
| 135 | if (errno == ENOTSUP) |
| 136 | tst_brkm(TCONF, cleanup, "No xattr support in fs or " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | "mount without user_xattr option"); |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 138 | unlink("testfile"); |
| 139 | |
| 140 | /* Create test files */ |
| 141 | if (mknod(FIFO, S_IFIFO | 0777, 0) == -1) |
| 142 | tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | FIFO); |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 144 | |
| 145 | if (mknod(CHR, S_IFCHR | 0777, 0) == -1) |
| 146 | tst_brkm(TBROK | TERRNO, cleanup, "Create char special(%s)" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | " failed", CHR); |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 148 | |
| 149 | if (mknod(BLK, S_IFBLK | 0777, 0) == -1) |
| 150 | tst_brkm(TBROK | TERRNO, cleanup, "Create block special(%s)" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | " failed", BLK); |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 152 | |
| 153 | if (mknod(SOCK, S_IFSOCK | 0777, 0) == -1) |
| 154 | tst_brkm(TBROK | TERRNO, cleanup, "Create socket(%s) failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | SOCK); |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 156 | |
| 157 | TEST_PAUSE; |
| 158 | } |
| 159 | |
| 160 | static void cleanup(void) |
| 161 | { |
Eryu Guan | fb19ffd | 2012-01-06 20:46:12 +0800 | [diff] [blame] | 162 | tst_rmdir(); |
| 163 | } |
Caspar Zhang | a97441d | 2012-01-10 11:01:10 +0800 | [diff] [blame] | 164 | #else /* HAVE_ATTR_XATTR_H */ |
| 165 | int main(int argc, char *argv[]) |
| 166 | { |
| 167 | tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist."); |
| 168 | } |
| 169 | #endif |