• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2015-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #ifdef HAVE_SYS_XATTR_H
32 
33 # include <stdio.h>
34 # include <sys/xattr.h>
35 
36 # ifndef XATTR_SIZE_MAX
37 #  define XATTR_SIZE_MAX 65536
38 # endif
39 
40 int
main(void)41 main(void)
42 {
43 	static const char name[] = "strace.test";
44 	static const char c_value[] = "foo\0bar";
45 	static const char q_value[] = "foo\\0bar";
46 
47 	const char *const z_value = tail_memdup(c_value, sizeof(c_value));
48 	char *const efault = tail_alloc(1) + 1;
49 	const char *const value = tail_memdup(c_value, sizeof(c_value) - 1);
50 	char *const big = tail_alloc(XATTR_SIZE_MAX + 1);
51 	long rc;
52 	const char *errstr;
53 
54 	rc = fsetxattr(-1, 0, 0, 0, XATTR_CREATE);
55 	printf("fsetxattr(-1, NULL, NULL, 0, XATTR_CREATE) = %s\n",
56 	       sprintrc(rc));
57 
58 	rc = fsetxattr(-1, 0, z_value, 0, XATTR_CREATE);
59 	printf("fsetxattr(-1, NULL, \"\", 0, XATTR_CREATE) = %s\n",
60 	       sprintrc(rc));
61 
62 	rc = fsetxattr(-1, name, big, XATTR_SIZE_MAX + 1, XATTR_CREATE);
63 	printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE) = %s\n",
64 	       name, big, XATTR_SIZE_MAX + 1, sprintrc(rc));
65 
66 	rc = fsetxattr(-1, name, value, sizeof(c_value), XATTR_CREATE);
67 	printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE) = %s\n",
68 	       name, value, (unsigned) sizeof(c_value), sprintrc(rc));
69 
70 	rc = fsetxattr(-1, name, z_value, sizeof(c_value), XATTR_REPLACE);
71 	printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_REPLACE) = %s\n",
72 	       name, q_value, (unsigned) sizeof(c_value), sprintrc(rc));
73 
74 	rc = fsetxattr(-1, name, value, sizeof(c_value) - 1, XATTR_CREATE|XATTR_REPLACE);
75 	printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_CREATE|XATTR_REPLACE)"
76 	       " = %s\n",
77 	       name, q_value, (unsigned) sizeof(c_value) - 1, sprintrc(rc));
78 
79 	rc = setxattr(".", name, z_value, sizeof(c_value), XATTR_CREATE);
80 	printf("setxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE) = %s\n",
81 	       name, q_value, (unsigned) sizeof(c_value), sprintrc(rc));
82 
83 	rc = lsetxattr(".", name, value, sizeof(c_value) - 1, XATTR_CREATE);
84 	printf("lsetxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE) = %s\n",
85 	       name, q_value, (unsigned) sizeof(c_value) - 1, sprintrc(rc));
86 
87 	rc = fgetxattr(-1, name, efault, 4);
88 	printf("fgetxattr(-1, \"%s\", %p, 4) = %s\n",
89 	       name, efault, sprintrc(rc));
90 
91 	rc = getxattr(".", name, big, XATTR_SIZE_MAX + 1);
92 	printf("getxattr(\".\", \"%s\", %p, %u) = %s\n",
93 	       name, big, XATTR_SIZE_MAX + 1, sprintrc(rc));
94 
95 	rc = lgetxattr(".", name, big + 1, XATTR_SIZE_MAX);
96 	printf("lgetxattr(\".\", \"%s\", %p, %u) = %s\n",
97 	       name, big + 1, XATTR_SIZE_MAX, sprintrc(rc));
98 
99 	rc = flistxattr(-1, efault, 4);
100 	printf("flistxattr(-1, %p, 4) = %s\n", efault, sprintrc(rc));
101 
102 	rc = llistxattr("", efault + 1, 4);
103 	printf("llistxattr(\"\", %p, 4) = %s\n", efault + 1, sprintrc(rc));
104 
105 	rc = listxattr(".", big, 0);
106 	printf("listxattr(\".\", %p, 0) = %s\n", big, sprintrc(rc));
107 
108 	rc = listxattr(".", big, XATTR_SIZE_MAX + 1);
109 	errstr = sprintrc(rc);
110 	printf("listxattr(\".\", ");
111 	if (rc < 0)
112 		printf("%p", big);
113 	else {
114 		const int ellipsis = rc > DEFAULT_STRLEN;
115 
116 		print_quoted_memory(big, ellipsis ? DEFAULT_STRLEN : rc);
117 		if (ellipsis)
118 			fputs("...", stdout);
119 	}
120 	printf(", %u) = %s\n", XATTR_SIZE_MAX + 1, errstr);
121 
122 	rc = fremovexattr(-1, name);
123 	printf("fremovexattr(-1, \"%s\") = %s\n", name, sprintrc(rc));
124 
125 	rc = removexattr(".", name);
126 	printf("removexattr(\".\", \"%s\") = %s\n", name, sprintrc(rc));
127 
128 	rc = lremovexattr(".", name);
129 	printf("lremovexattr(\".\", \"%s\") = %s\n", name, sprintrc(rc));
130 
131 	puts("+++ exited with 0 +++");
132 	return 0;
133 }
134 
135 #else
136 
137 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")
138 
139 #endif
140