2016-01-25 22:20:54 +00:00
/*
* This file is part of net - yy - unix strace test .
*
2017-04-03 00:56:54 +00:00
* Copyright ( c ) 2013 - 2017 Dmitry V . Levin < ldv @ altlinux . org >
2016-01-25 22:20:54 +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 .
*/
# include "tests.h"
# include <assert.h>
# include <stddef.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <sys/param.h>
# include <sys/socket.h>
# include <sys/un.h>
2017-04-03 00:56:54 +00:00
# define TEST_SOCKET "net-yy-unix.socket"
2016-01-25 22:20:54 +00:00
int
2017-04-03 00:56:54 +00:00
main ( void )
2016-01-25 22:20:54 +00:00
{
2017-04-19 02:16:31 +00:00
skip_if_unavailable ( " /proc/self/fd/ " ) ;
2017-04-03 00:56:54 +00:00
struct sockaddr_un addr = {
. sun_family = AF_UNIX ,
. sun_path = TEST_SOCKET
} ;
2016-01-25 22:20:54 +00:00
struct sockaddr * const listen_sa = tail_memdup ( & addr , sizeof ( addr ) ) ;
2017-03-16 13:46:36 +00:00
TAIL_ALLOC_OBJECT_CONST_PTR ( socklen_t , len ) ;
2017-04-03 00:56:54 +00:00
* len = offsetof ( struct sockaddr_un , sun_path ) + strlen ( TEST_SOCKET ) + 1 ;
2016-01-25 22:20:54 +00:00
if ( * len > sizeof ( addr ) )
* len = sizeof ( addr ) ;
2016-06-20 22:52:49 +00:00
int listen_fd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ;
2016-01-25 22:20:54 +00:00
if ( listen_fd < 0 )
perror_msg_and_skip ( " socket " ) ;
unsigned long listen_inode = inode_of_sockfd ( listen_fd ) ;
2016-06-20 22:52:49 +00:00
printf ( " socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]> \n " ,
2016-01-25 22:20:54 +00:00
listen_fd , listen_inode ) ;
2017-04-03 00:56:54 +00:00
( void ) unlink ( TEST_SOCKET ) ;
2016-01-25 22:20:54 +00:00
if ( bind ( listen_fd , listen_sa , * len ) )
perror_msg_and_skip ( " bind " ) ;
2016-06-20 22:52:49 +00:00
printf ( " bind(%d<UNIX:[%lu]>, {sa_family=AF_UNIX, sun_path= \" %s \" } "
2017-04-03 00:56:54 +00:00
" , %u) = 0 \n " ,
listen_fd , listen_inode , TEST_SOCKET , ( unsigned ) * len ) ;
2016-01-25 22:20:54 +00:00
if ( listen ( listen_fd , 1 ) )
perror_msg_and_skip ( " listen " ) ;
printf ( " listen(%d<UNIX:[%lu, \" %s \" ]>, 1) = 0 \n " ,
2017-04-03 00:56:54 +00:00
listen_fd , listen_inode , TEST_SOCKET ) ;
2016-01-25 22:20:54 +00:00
2017-03-16 13:46:36 +00:00
TAIL_ALLOC_OBJECT_CONST_PTR ( unsigned int , optval ) ;
2016-01-25 22:20:54 +00:00
* len = sizeof ( * optval ) ;
if ( getsockopt ( listen_fd , SOL_SOCKET , SO_PASSCRED , optval , len ) )
perror_msg_and_fail ( " getsockopt " ) ;
printf ( " getsockopt(%d<UNIX:[%lu, \" %s \" ]>, SOL_SOCKET, SO_PASSCRED "
" , [%u], [%u]) = 0 \n " ,
2017-04-03 00:56:54 +00:00
listen_fd , listen_inode , TEST_SOCKET , * optval , ( unsigned ) * len ) ;
2016-01-25 22:20:54 +00:00
memset ( listen_sa , 0 , sizeof ( addr ) ) ;
* len = sizeof ( addr ) ;
if ( getsockname ( listen_fd , listen_sa , len ) )
perror_msg_and_fail ( " getsockname " ) ;
2016-06-20 22:52:49 +00:00
printf ( " getsockname(%d<UNIX:[%lu, \" %s \" ]>, {sa_family=AF_UNIX "
2016-07-09 21:52:58 +00:00
" , sun_path= \" %s \" }, [%d->%d]) = 0 \n " , listen_fd , listen_inode ,
2017-04-03 00:56:54 +00:00
TEST_SOCKET , TEST_SOCKET , ( int ) sizeof ( addr ) , ( int ) * len ) ;
2016-01-25 22:20:54 +00:00
2016-06-20 22:52:49 +00:00
int connect_fd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ;
2016-01-25 22:20:54 +00:00
if ( connect_fd < 0 )
perror_msg_and_fail ( " socket " ) ;
unsigned long connect_inode = inode_of_sockfd ( connect_fd ) ;
2016-06-20 22:52:49 +00:00
printf ( " socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]> \n " ,
2016-01-25 22:20:54 +00:00
connect_fd , connect_inode ) ;
if ( connect ( connect_fd , listen_sa , * len ) )
perror_msg_and_fail ( " connect " ) ;
2016-06-20 22:52:49 +00:00
printf ( " connect(%d<UNIX:[%lu]>, {sa_family=AF_UNIX "
2016-01-25 22:20:54 +00:00
" , sun_path= \" %s \" }, %u) = 0 \n " ,
2017-04-03 00:56:54 +00:00
connect_fd , connect_inode , TEST_SOCKET , ( unsigned ) * len ) ;
2016-01-25 22:20:54 +00:00
struct sockaddr * const accept_sa = tail_alloc ( sizeof ( addr ) ) ;
memset ( accept_sa , 0 , sizeof ( addr ) ) ;
* len = sizeof ( addr ) ;
int accept_fd = accept ( listen_fd , accept_sa , len ) ;
if ( accept_fd < 0 )
perror_msg_and_fail ( " accept " ) ;
unsigned long accept_inode = inode_of_sockfd ( accept_fd ) ;
2016-06-22 23:40:56 +00:00
printf ( " accept(%d<UNIX:[%lu, \" %s \" ]>, {sa_family=AF_UNIX} "
2016-07-09 21:52:58 +00:00
" , [%d->%d]) = %d<UNIX:[%lu->%lu, \" %s \" ]> \n " ,
2017-04-03 00:56:54 +00:00
listen_fd , listen_inode , TEST_SOCKET ,
( int ) sizeof ( addr ) , ( int ) * len ,
accept_fd , accept_inode , connect_inode , TEST_SOCKET ) ;
2016-01-25 22:20:54 +00:00
memset ( listen_sa , 0 , sizeof ( addr ) ) ;
* len = sizeof ( addr ) ;
if ( getpeername ( connect_fd , listen_sa , len ) )
perror_msg_and_fail ( " getpeername " ) ;
2016-06-20 22:52:49 +00:00
printf ( " getpeername(%d<UNIX:[%lu->%lu]>, {sa_family=AF_UNIX "
2016-07-09 21:52:58 +00:00
" , sun_path= \" %s \" }, [%d->%d]) = 0 \n " ,
connect_fd , connect_inode ,
2017-04-03 00:56:54 +00:00
accept_inode , TEST_SOCKET , ( int ) sizeof ( addr ) , ( int ) * len ) ;
2016-01-25 22:20:54 +00:00
char text [ ] = " text " ;
assert ( sendto ( connect_fd , text , sizeof ( text ) - 1 , MSG_DONTWAIT , NULL , 0 )
= = sizeof ( text ) - 1 ) ;
printf ( " sendto(%d<UNIX:[%lu->%lu]>, \" %s \" , %u, MSG_DONTWAIT "
" , NULL, 0) = %u \n " ,
connect_fd , connect_inode , accept_inode , text ,
( unsigned ) sizeof ( text ) - 1 , ( unsigned ) sizeof ( text ) - 1 ) ;
assert ( recvfrom ( accept_fd , text , sizeof ( text ) - 1 , MSG_DONTWAIT , NULL , NULL )
= = sizeof ( text ) - 1 ) ;
printf ( " recvfrom(%d<UNIX:[%lu->%lu, \" %s \" ]>, \" %s \" , %u, MSG_DONTWAIT "
" , NULL, NULL) = %u \n " ,
2017-04-03 00:56:54 +00:00
accept_fd , accept_inode , connect_inode , TEST_SOCKET , text ,
2016-01-25 22:20:54 +00:00
( unsigned ) sizeof ( text ) - 1 , ( unsigned ) sizeof ( text ) - 1 ) ;
assert ( close ( connect_fd ) = = 0 ) ;
printf ( " close(%d<UNIX:[%lu->%lu]>) = 0 \n " ,
connect_fd , connect_inode , accept_inode ) ;
assert ( close ( accept_fd ) = = 0 ) ;
Implement caching of print_sockaddr_by_inode
As -yy parser, compared to -y, needs to do at least 5 extra syscalls
(getxattr, socket, sendmsg, recvmsg, close) to print socket details,
caching results of netlink conversations between strace and kernel
noticeably reduces amount of system time spent by strace.
The caching is safe since sockets do not change their addresses after
successful bind or connect syscall.
* defs.h (string_quote, print_sockaddr_by_inode_cached): New prototypes.
* socketutils.c (cache_entry): New type.
(CACHE_SIZE, CACHE_MASK): New macros.
(cache): New static array.
(cache_and_print_inode_details): New static function.
(print_sockaddr_by_inode_cached): New function.
(inet_parse_response, unix_parse_response): Use
cache_and_print_inode_details.
* util.c (printfd): Use string_quote and print_sockaddr_by_inode_cached.
(string_quote): Remove static qualifier.
* NEWS: Mention this improvement.
* tests/unix-yy.c (main): Update.
2016-02-01 23:14:59 +00:00
printf ( " close(%d<UNIX:[%lu->%lu, \" %s \" ]>) = 0 \n " ,
2017-04-03 00:56:54 +00:00
accept_fd , accept_inode , connect_inode , TEST_SOCKET ) ;
2016-01-25 22:20:54 +00:00
2016-06-20 22:52:49 +00:00
connect_fd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ;
2016-01-25 22:20:54 +00:00
if ( connect_fd < 0 )
perror_msg_and_fail ( " socket " ) ;
connect_inode = inode_of_sockfd ( connect_fd ) ;
2016-06-20 22:52:49 +00:00
printf ( " socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]> \n " ,
2016-01-25 22:20:54 +00:00
connect_fd , connect_inode ) ;
* optval = 1 ;
* len = sizeof ( * optval ) ;
if ( setsockopt ( connect_fd , SOL_SOCKET , SO_PASSCRED , optval , * len ) )
perror_msg_and_fail ( " setsockopt " ) ;
printf ( " setsockopt(%d<UNIX:[%lu]>, SOL_SOCKET, SO_PASSCRED "
" , [%u], %u) = 0 \n " ,
connect_fd , connect_inode , * optval , ( unsigned ) * len ) ;
memset ( listen_sa , 0 , sizeof ( addr ) ) ;
* len = sizeof ( addr ) ;
if ( getsockname ( listen_fd , listen_sa , len ) )
perror_msg_and_fail ( " getsockname " ) ;
2016-06-20 22:52:49 +00:00
printf ( " getsockname(%d<UNIX:[%lu, \" %s \" ]>, {sa_family=AF_UNIX "
2016-07-09 21:52:58 +00:00
" , sun_path= \" %s \" }, [%d->%d]) = 0 \n " , listen_fd , listen_inode ,
2017-04-03 00:56:54 +00:00
TEST_SOCKET , TEST_SOCKET , ( int ) sizeof ( addr ) , ( int ) * len ) ;
2016-01-25 22:20:54 +00:00
if ( connect ( connect_fd , listen_sa , * len ) )
perror_msg_and_fail ( " connect " ) ;
2016-06-20 22:52:49 +00:00
printf ( " connect(%d<UNIX:[%lu]>, {sa_family=AF_UNIX "
2016-01-25 22:20:54 +00:00
" , sun_path= \" %s \" }, %u) = 0 \n " ,
2017-04-03 00:56:54 +00:00
connect_fd , connect_inode , TEST_SOCKET , ( unsigned ) * len ) ;
2016-01-25 22:20:54 +00:00
memset ( accept_sa , 0 , sizeof ( addr ) ) ;
* len = sizeof ( addr ) ;
accept_fd = accept ( listen_fd , accept_sa , len ) ;
if ( accept_fd < 0 )
perror_msg_and_fail ( " accept " ) ;
accept_inode = inode_of_sockfd ( accept_fd ) ;
const char * const sun_path1 =
( ( struct sockaddr_un * ) accept_sa ) - > sun_path + 1 ;
2016-06-20 22:52:49 +00:00
printf ( " accept(%d<UNIX:[%lu, \" %s \" ]>, {sa_family=AF_UNIX "
2016-07-09 21:52:58 +00:00
" , sun_path=@ \" %s \" }, [%d->%d]) = %d<UNIX:[%lu->%lu, \" %s \" ]> \n " ,
2017-04-03 00:56:54 +00:00
listen_fd , listen_inode , TEST_SOCKET , sun_path1 ,
2016-07-09 21:52:58 +00:00
( int ) sizeof ( addr ) , ( int ) * len ,
2017-04-03 00:56:54 +00:00
accept_fd , accept_inode , connect_inode , TEST_SOCKET ) ;
2016-01-25 22:20:54 +00:00
memset ( listen_sa , 0 , sizeof ( addr ) ) ;
* len = sizeof ( addr ) ;
if ( getpeername ( connect_fd , listen_sa , len ) )
perror_msg_and_fail ( " getpeername " ) ;
2016-06-20 22:52:49 +00:00
printf ( " getpeername(%d<UNIX:[%lu->%lu,@ \" %s \" ]>, {sa_family=AF_UNIX "
2016-07-09 21:52:58 +00:00
" , sun_path= \" %s \" }, [%d->%d]) = 0 \n " , connect_fd , connect_inode ,
2017-04-03 00:56:54 +00:00
accept_inode , sun_path1 , TEST_SOCKET , ( int ) sizeof ( addr ) , ( int ) * len ) ;
2016-01-25 22:20:54 +00:00
assert ( sendto ( connect_fd , text , sizeof ( text ) - 1 , MSG_DONTWAIT , NULL , 0 )
= = sizeof ( text ) - 1 ) ;
printf ( " sendto(%d<UNIX:[%lu->%lu,@ \" %s \" ]>, \" %s \" , %u, MSG_DONTWAIT "
" , NULL, 0) = %u \n " ,
connect_fd , connect_inode , accept_inode , sun_path1 , text ,
( unsigned ) sizeof ( text ) - 1 , ( unsigned ) sizeof ( text ) - 1 ) ;
assert ( recvfrom ( accept_fd , text , sizeof ( text ) - 1 , MSG_DONTWAIT , NULL , NULL )
= = sizeof ( text ) - 1 ) ;
printf ( " recvfrom(%d<UNIX:[%lu->%lu, \" %s \" ]>, \" %s \" , %u, MSG_DONTWAIT "
" , NULL, NULL) = %u \n " ,
2017-04-03 00:56:54 +00:00
accept_fd , accept_inode , connect_inode , TEST_SOCKET , text ,
2016-01-25 22:20:54 +00:00
( unsigned ) sizeof ( text ) - 1 , ( unsigned ) sizeof ( text ) - 1 ) ;
assert ( close ( connect_fd ) = = 0 ) ;
printf ( " close(%d<UNIX:[%lu->%lu,@ \" %s \" ]>) = 0 \n " ,
connect_fd , connect_inode , accept_inode , sun_path1 ) ;
assert ( close ( accept_fd ) = = 0 ) ;
Implement caching of print_sockaddr_by_inode
As -yy parser, compared to -y, needs to do at least 5 extra syscalls
(getxattr, socket, sendmsg, recvmsg, close) to print socket details,
caching results of netlink conversations between strace and kernel
noticeably reduces amount of system time spent by strace.
The caching is safe since sockets do not change their addresses after
successful bind or connect syscall.
* defs.h (string_quote, print_sockaddr_by_inode_cached): New prototypes.
* socketutils.c (cache_entry): New type.
(CACHE_SIZE, CACHE_MASK): New macros.
(cache): New static array.
(cache_and_print_inode_details): New static function.
(print_sockaddr_by_inode_cached): New function.
(inet_parse_response, unix_parse_response): Use
cache_and_print_inode_details.
* util.c (printfd): Use string_quote and print_sockaddr_by_inode_cached.
(string_quote): Remove static qualifier.
* NEWS: Mention this improvement.
* tests/unix-yy.c (main): Update.
2016-02-01 23:14:59 +00:00
printf ( " close(%d<UNIX:[%lu->%lu, \" %s \" ]>) = 0 \n " ,
2017-04-03 00:56:54 +00:00
accept_fd , accept_inode , connect_inode , TEST_SOCKET ) ;
2016-01-25 22:20:54 +00:00
2017-04-03 00:56:54 +00:00
assert ( unlink ( TEST_SOCKET ) = = 0 ) ;
2016-01-25 22:20:54 +00:00
assert ( close ( listen_fd ) = = 0 ) ;
printf ( " close(%d<UNIX:[%lu, \" %s \" ]>) = 0 \n " ,
2017-04-03 00:56:54 +00:00
listen_fd , listen_inode , TEST_SOCKET ) ;
2016-01-25 22:20:54 +00:00
puts ( " +++ exited with 0 +++ " ) ;
return 0 ;
}