Home | History | Annotate | Download | only in getxattr
      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 
     41 #include "config.h"
     42 #include <sys/types.h>
     43 #include <sys/stat.h>
     44 #include <sys/wait.h>
     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>
     52 #ifdef HAVE_SYS_XATTR_H
     53 # include <sys/xattr.h>
     54 #endif
     55 #include "test.h"
     56 
     57 char *TCID = "getxattr02";
     58 
     59 #ifdef HAVE_SYS_XATTR_H
     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 
     70 static char *tc[] = {
     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 */
     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;
     83 	int exp_eno;
     84 	char buf[BUFSIZ];
     85 
     86 	tst_parse_opts(argc, argv, NULL, NULL);
     87 
     88 	setup();
     89 
     90 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     91 		tst_count = 0;
     92 
     93 		/*
     94 		 * Before kernel 3.0.0, getxattr(2) will set errno with 'EPERM'
     95 		 * when the file is not a regular file and directory, refer to
     96 		 * commitid 55b23bd
     97 		 */
     98 		if (tst_kvercmp(3, 0, 0) >= 0)
     99 			exp_eno = ENODATA;
    100 		else
    101 			exp_eno = EPERM;
    102 
    103 		for (i = 0; i < TST_TOTAL; i++) {
    104 			TEST(getxattr(tc[0], XATTR_TEST_KEY, buf, BUFSIZ));
    105 
    106 			if (TEST_RETURN == -1 && TEST_ERRNO == exp_eno)
    107 				tst_resm(TPASS | TTERRNO, "expected behavior");
    108 			else
    109 				tst_resm(TFAIL | TTERRNO, "unexpected behavior"
    110 					 " - expected errno %d - Got", exp_eno);
    111 		}
    112 	}
    113 
    114 	cleanup();
    115 	tst_exit();
    116 }
    117 
    118 static void setup(void)
    119 {
    120 	int fd;
    121 	dev_t dev;
    122 
    123 	tst_require_root();
    124 
    125 	tst_tmpdir();
    126 
    127 	/* Test for xattr support */
    128 	fd = creat("testfile", 0644);
    129 	if (fd == -1)
    130 		tst_brkm(TBROK | TERRNO, cleanup, "Create testfile failed");
    131 	close(fd);
    132 	if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
    133 		if (errno == ENOTSUP)
    134 			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
    135 				 "mount without user_xattr option");
    136 	unlink("testfile");
    137 
    138 	/* Create test files */
    139 	if (mknod(FIFO, S_IFIFO | 0777, 0) == -1)
    140 		tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed",
    141 			 FIFO);
    142 
    143 	dev = makedev(1, 3);
    144 	if (mknod(CHR, S_IFCHR | 0777, dev) == -1)
    145 		tst_brkm(TBROK | TERRNO, cleanup, "Create char special(%s)"
    146 			 " failed", CHR);
    147 
    148 	if (mknod(BLK, S_IFBLK | 0777, 0) == -1)
    149 		tst_brkm(TBROK | TERRNO, cleanup, "Create block special(%s)"
    150 			 " failed", BLK);
    151 
    152 	if (mknod(SOCK, S_IFSOCK | 0777, 0) == -1)
    153 		tst_brkm(TBROK | TERRNO, cleanup, "Create socket(%s) failed",
    154 			 SOCK);
    155 
    156 	TEST_PAUSE;
    157 }
    158 
    159 static void cleanup(void)
    160 {
    161 	tst_rmdir();
    162 }
    163 #else /* HAVE_SYS_XATTR_H */
    164 int main(int argc, char *argv[])
    165 {
    166 	tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
    167 }
    168 #endif
    169