2015-08-19 01:25:39 +00:00
/*
2016-01-03 21:54:31 +00:00
* Copyright ( c ) 2015 - 2016 Dmitry V . Levin < ldv @ altlinux . org >
2017-05-22 19:14:52 +02:00
* Copyright ( c ) 2015 - 2017 The strace developers .
2015-08-19 01:25:39 +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-08-09 14:38:29 +00:00
# include <asm/unistd.h>
2015-08-19 01:25:39 +00:00
# ifdef __NR_sendfile64
2016-01-03 21:54:31 +00:00
# include <assert.h>
# include <errno.h>
# include <fcntl.h>
# include <stdio.h>
# include <stdint.h>
2017-06-04 21:17:15 +00:00
# include <stdlib.h>
2016-01-03 21:54:31 +00:00
# include <unistd.h>
# include <sys / socket.h>
2015-08-19 01:25:39 +00:00
int
2017-06-04 21:17:15 +00:00
main ( void )
2015-08-19 01:25:39 +00:00
{
( void ) close ( 0 ) ;
if ( open ( " /dev/zero " , O_RDONLY ) ! = 0 )
2016-01-03 21:54:31 +00:00
perror_msg_and_skip ( " open: %s " , " /dev/zero " ) ;
2015-08-19 01:25:39 +00:00
int sv [ 2 ] ;
if ( socketpair ( AF_UNIX , SOCK_STREAM , 0 , sv ) )
2016-01-03 21:54:31 +00:00
perror_msg_and_skip ( " socketpair " ) ;
2015-08-19 01:25:39 +00:00
2017-06-04 21:17:15 +00:00
const unsigned int page_size = get_page_size ( ) ;
assert ( syscall ( __NR_sendfile64 , 0 , 1 , NULL , page_size ) = = - 1 ) ;
2016-01-03 21:54:31 +00:00
if ( EBADF ! = errno )
perror_msg_and_skip ( " sendfile64 " ) ;
2017-06-04 21:17:15 +00:00
printf ( " sendfile64(0, 1, NULL, %u) = -1 EBADF (%m) \n " , page_size ) ;
unsigned int file_size = 0 ;
socklen_t optlen = sizeof ( file_size ) ;
if ( getsockopt ( sv [ 1 ] , SOL_SOCKET , SO_SNDBUF , & file_size , & optlen ) )
perror_msg_and_fail ( " getsockopt " ) ;
if ( file_size < 1024 )
error_msg_and_skip ( " SO_SNDBUF too small: %u " , file_size ) ;
file_size / = 4 ;
if ( file_size / 16 > page_size )
file_size = page_size * 16 ;
const unsigned int blen = file_size / 3 ;
const unsigned int alen = file_size - blen ;
static const char fname [ ] = " sendfile64-tmpfile " ;
int reg_in = open ( fname , O_RDWR | O_CREAT | O_TRUNC , 0600 ) ;
if ( reg_in < 0 )
perror_msg_and_fail ( " open: %s " , fname ) ;
if ( unlink ( fname ) )
perror_msg_and_fail ( " unlink: %s " , fname ) ;
if ( ftruncate ( reg_in , file_size ) )
perror_msg_and_fail ( " ftruncate: %s " , fname ) ;
2015-08-19 01:25:39 +00:00
2017-03-16 13:46:36 +00:00
TAIL_ALLOC_OBJECT_CONST_PTR ( uint64_t , p_off ) ;
2016-01-03 21:54:31 +00:00
void * p = p_off + 1 ;
* p_off = 0 ;
2015-08-19 01:25:39 +00:00
2017-06-04 21:17:15 +00:00
assert ( syscall ( __NR_sendfile64 , 0 , 1 , p , page_size ) = = - 1 ) ;
printf ( " sendfile64(0, 1, %p, %u) = -1 EFAULT (%m) \n " , p , page_size ) ;
2015-08-19 01:25:39 +00:00
2016-01-03 21:54:31 +00:00
assert ( syscall ( __NR_sendfile64 , sv [ 1 ] , reg_in , NULL , alen )
= = ( long ) alen ) ;
2017-06-04 21:17:15 +00:00
printf ( " sendfile64(%d, %d, NULL, %u) = %u \n " ,
sv [ 1 ] , reg_in , alen , alen ) ;
2015-08-19 01:25:39 +00:00
2016-01-03 21:54:31 +00:00
assert ( syscall ( __NR_sendfile64 , sv [ 1 ] , reg_in , p_off , alen )
= = ( long ) alen ) ;
2017-06-04 21:17:15 +00:00
printf ( " sendfile64(%d, %d, [0] => [%u], %u) = %u \n " ,
sv [ 1 ] , reg_in , alen , alen , alen ) ;
2015-08-19 01:25:39 +00:00
2017-06-04 21:17:15 +00:00
assert ( syscall ( __NR_sendfile64 , sv [ 1 ] , reg_in , p_off , file_size + 1 )
2016-01-03 21:54:31 +00:00
= = ( long ) blen ) ;
2017-06-04 21:17:15 +00:00
printf ( " sendfile64(%d, %d, [%u] => [%u], %u) = %u \n " ,
sv [ 1 ] , reg_in , alen , file_size , file_size + 1 , blen ) ;
2015-08-19 01:25:39 +00:00
2016-11-13 19:14:46 +03:00
* p_off = 0xcafef00dfacefeedULL ;
2016-01-03 21:54:31 +00:00
assert ( syscall ( __NR_sendfile64 , sv [ 1 ] , reg_in , p_off , 1 ) = = - 1 ) ;
2015-08-26 23:25:06 +00:00
printf ( " sendfile64(%d, %d, [14627392582579060461], 1) "
2016-01-03 21:54:31 +00:00
" = -1 EINVAL (%m) \n " , sv [ 1 ] , reg_in ) ;
2015-08-19 01:25:39 +00:00
* p_off = 0xfacefeed ;
2016-01-03 21:54:31 +00:00
assert ( syscall ( __NR_sendfile64 , sv [ 1 ] , reg_in , p_off , 1 ) = = 0 ) ;
printf ( " sendfile64(%d, %d, [4207869677], 1) = 0 \n " , sv [ 1 ] , reg_in ) ;
2015-08-19 01:25:39 +00:00
2015-08-26 23:25:06 +00:00
puts ( " +++ exited with 0 +++ " ) ;
2015-08-19 01:25:39 +00:00
return 0 ;
}
# else
2016-01-03 21:54:31 +00:00
SKIP_MAIN_UNDEFINED ( " __NR_sendfile64 " )
2015-08-19 01:25:39 +00:00
# endif