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