2015-12-17 17:56:48 +00:00
/*
2016-01-05 23:18:25 +00:00
* Copyright ( c ) 2015 - 2016 Dmitry V . Levin < ldv @ altlinux . org >
2017-09-05 10:01:01 +02:00
* Copyright ( c ) 2015 - 2017 The strace developers .
2015-12-17 17:56:48 +00:00
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions
* are met :
* 1. Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* 2. Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission .
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ` ` AS IS ' ' AND ANY EXPRESS OR
* IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
* INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
* NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2016-01-02 13:28:43 +00:00
# include "tests.h"
2016-01-05 23:18:25 +00:00
2015-07-15 00:09:08 +00:00
# ifdef HAVE_SYS_XATTR_H
2016-01-05 23:18:25 +00:00
2016-05-10 18:18:32 +00:00
# include <stdio.h>
2015-07-15 00:09:08 +00:00
# include <sys / xattr.h>
2016-05-10 18:18:32 +00:00
# ifndef XATTR_SIZE_MAX
# define XATTR_SIZE_MAX 65536
# endif
2015-07-15 00:09:08 +00:00
int
main ( void )
{
2016-05-10 18:18:32 +00:00
static const char name [ ] = " strace.test " ;
static const char c_value [ ] = " foo \0 bar " ;
static const char q_value [ ] = " foo \\ 0bar " ;
const char * const z_value = tail_memdup ( c_value , sizeof ( c_value ) ) ;
char * const efault = tail_alloc ( 1 ) + 1 ;
const char * const value = tail_memdup ( c_value , sizeof ( c_value ) - 1 ) ;
char * const big = tail_alloc ( XATTR_SIZE_MAX + 1 ) ;
2016-09-18 00:00:53 +00:00
long rc ;
const char * errstr ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fsetxattr ( - 1 , 0 , 0 , 0 , XATTR_CREATE ) ;
printf ( " fsetxattr(-1, NULL, NULL, 0, XATTR_CREATE) = %s \n " ,
sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fsetxattr ( - 1 , 0 , z_value , 0 , XATTR_CREATE ) ;
printf ( " fsetxattr(-1, NULL, \" \" , 0, XATTR_CREATE) = %s \n " ,
sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fsetxattr ( - 1 , name , big , XATTR_SIZE_MAX + 1 , XATTR_CREATE ) ;
printf ( " fsetxattr(-1, \" %s \" , %p, %u, XATTR_CREATE) = %s \n " ,
name , big , XATTR_SIZE_MAX + 1 , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fsetxattr ( - 1 , name , value , sizeof ( c_value ) , XATTR_CREATE ) ;
printf ( " fsetxattr(-1, \" %s \" , %p, %u, XATTR_CREATE) = %s \n " ,
name , value , ( unsigned ) sizeof ( c_value ) , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fsetxattr ( - 1 , name , z_value , sizeof ( c_value ) , XATTR_REPLACE ) ;
printf ( " fsetxattr(-1, \" %s \" , \" %s \" , %u, XATTR_REPLACE) = %s \n " ,
name , q_value , ( unsigned ) sizeof ( c_value ) , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fsetxattr ( - 1 , name , value , sizeof ( c_value ) - 1 , XATTR_CREATE | XATTR_REPLACE ) ;
2016-05-10 18:18:32 +00:00
printf ( " fsetxattr(-1, \" %s \" , \" %s \" , %u, XATTR_CREATE|XATTR_REPLACE) "
2016-09-18 00:00:53 +00:00
" = %s \n " ,
name , q_value , ( unsigned ) sizeof ( c_value ) - 1 , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = setxattr ( " . " , name , z_value , sizeof ( c_value ) , XATTR_CREATE ) ;
printf ( " setxattr( \" . \" , \" %s \" , \" %s \" , %u, XATTR_CREATE) = %s \n " ,
name , q_value , ( unsigned ) sizeof ( c_value ) , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = lsetxattr ( " . " , name , value , sizeof ( c_value ) - 1 , XATTR_CREATE ) ;
printf ( " lsetxattr( \" . \" , \" %s \" , \" %s \" , %u, XATTR_CREATE) = %s \n " ,
name , q_value , ( unsigned ) sizeof ( c_value ) - 1 , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fgetxattr ( - 1 , name , efault , 4 ) ;
printf ( " fgetxattr(-1, \" %s \" , %p, 4) = %s \n " ,
name , efault , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = getxattr ( " . " , name , big , XATTR_SIZE_MAX + 1 ) ;
printf ( " getxattr( \" . \" , \" %s \" , %p, %u) = %s \n " ,
name , big , XATTR_SIZE_MAX + 1 , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = lgetxattr ( " . " , name , big + 1 , XATTR_SIZE_MAX ) ;
printf ( " lgetxattr( \" . \" , \" %s \" , %p, %u) = %s \n " ,
name , big + 1 , XATTR_SIZE_MAX , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = flistxattr ( - 1 , efault , 4 ) ;
printf ( " flistxattr(-1, %p, 4) = %s \n " , efault , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = llistxattr ( " " , efault + 1 , 4 ) ;
printf ( " llistxattr( \" \" , %p, 4) = %s \n " , efault + 1 , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = listxattr ( " . " , big , 0 ) ;
printf ( " listxattr( \" . \" , %p, 0) = %s \n " , big , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
rc = listxattr ( " . " , big , XATTR_SIZE_MAX + 1 ) ;
2016-09-18 00:00:53 +00:00
errstr = sprintrc ( rc ) ;
printf ( " listxattr( \" . \" , " ) ;
2016-05-10 18:18:32 +00:00
if ( rc < 0 )
2016-09-18 00:00:53 +00:00
printf ( " %p " , big ) ;
2016-05-10 18:18:32 +00:00
else {
2016-12-22 22:34:23 +00:00
const int ellipsis = rc > DEFAULT_STRLEN ;
print_quoted_memory ( big , ellipsis ? DEFAULT_STRLEN : rc ) ;
if ( ellipsis )
fputs ( " ... " , stdout ) ;
2016-05-10 18:18:32 +00:00
}
2016-09-18 00:00:53 +00:00
printf ( " , %u) = %s \n " , XATTR_SIZE_MAX + 1 , errstr ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = fremovexattr ( - 1 , name ) ;
printf ( " fremovexattr(-1, \" %s \" ) = %s \n " , name , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = removexattr ( " . " , name ) ;
printf ( " removexattr( \" . \" , \" %s \" ) = %s \n " , name , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
2016-09-18 00:00:53 +00:00
rc = lremovexattr ( " . " , name ) ;
printf ( " lremovexattr( \" . \" , \" %s \" ) = %s \n " , name , sprintrc ( rc ) ) ;
2016-05-10 18:18:32 +00:00
puts ( " +++ exited with 0 +++ " ) ;
2015-07-15 00:09:08 +00:00
return 0 ;
}
# else
2016-01-05 23:18:25 +00:00
SKIP_MAIN_UNDEFINED ( " HAVE_SYS_XATTR_H " )
2015-07-15 00:09:08 +00:00
# endif