Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv (at) 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 #include <asm/unistd.h>
     31 
     32 #ifdef __NR_sendfile
     33 
     34 # include <assert.h>
     35 # include <errno.h>
     36 # include <fcntl.h>
     37 # include <stdio.h>
     38 # include <stdint.h>
     39 # include <stdlib.h>
     40 # include <unistd.h>
     41 # include <sys/socket.h>
     42 
     43 int
     44 main(void)
     45 {
     46 	(void) close(0);
     47 	if (open("/dev/zero", O_RDONLY) != 0)
     48 		perror_msg_and_skip("open: %s", "/dev/zero");
     49 
     50 	int sv[2];
     51 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
     52 		perror_msg_and_skip("socketpair");
     53 
     54 	const unsigned int page_size = get_page_size();
     55 	assert(syscall(__NR_sendfile, 0, 1, NULL, page_size) == -1);
     56 	if (EBADF != errno)
     57 		perror_msg_and_skip("sendfile");
     58 	printf("sendfile(0, 1, NULL, %u) = -1 EBADF (%m)\n", page_size);
     59 
     60 	unsigned int file_size = 0;
     61 	socklen_t optlen = sizeof(file_size);
     62 	if (getsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &file_size, &optlen))
     63 		perror_msg_and_fail("getsockopt");
     64 	if (file_size < 1024)
     65 		error_msg_and_skip("SO_SNDBUF too small: %u", file_size);
     66 
     67 	file_size /= 4;
     68 	if (file_size / 16 > page_size)
     69 		file_size = page_size * 16;
     70 	const unsigned int blen = file_size / 3;
     71 	const unsigned int alen = file_size - blen;
     72 
     73 	static const char fname[] = "sendfile-tmpfile";
     74 	int reg_in = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0600);
     75 	if (reg_in < 0)
     76 		perror_msg_and_fail("open: %s", fname);
     77 	if (unlink(fname))
     78 		perror_msg_and_fail("unlink: %s", fname);
     79 	if (ftruncate(reg_in, file_size))
     80 		perror_msg_and_fail("ftruncate: %s", fname);
     81 
     82 	TAIL_ALLOC_OBJECT_VAR_PTR(uint32_t, p_off);
     83 	void *p = p_off + 1;
     84 	*p_off = 0;
     85 
     86 	assert(syscall(__NR_sendfile, 0, 1, p, page_size) == -1);
     87 	printf("sendfile(0, 1, %p, %u) = -1 EFAULT (%m)\n", p, page_size);
     88 
     89 	assert(syscall(__NR_sendfile, sv[1], reg_in, NULL, alen)
     90 	       == (long) alen);
     91 	printf("sendfile(%d, %d, NULL, %u) = %u\n",
     92 	       sv[1], reg_in, alen, alen);
     93 
     94 	p = p_off;
     95 	if (syscall(__NR_sendfile, sv[1], reg_in, p_off, alen) != (long) alen) {
     96 		printf("sendfile(%d, %d, %#lx, %u) = -1 EFAULT (%m)\n",
     97 		       sv[1], reg_in, (unsigned long) p_off, alen);
     98 		--p_off;
     99 		*p_off = 0;
    100 		assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, alen)
    101 		       == (long) alen);
    102 	}
    103 	printf("sendfile(%d, %d, [0] => [%u], %u) = %u\n",
    104 	       sv[1], reg_in, alen, alen, alen);
    105 
    106 	assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, file_size + 1)
    107 	       == (long) blen);
    108 	printf("sendfile(%d, %d, [%u] => [%u], %u) = %u\n",
    109 	       sv[1], reg_in, alen, file_size, file_size + 1, blen);
    110 
    111 	if (p_off != p) {
    112 		uint64_t *p_off64 = (uint64_t *) p_off;
    113 		*p_off64 = 0xcafef00dfacefeedULL;
    114 		assert(syscall(__NR_sendfile, sv[1], reg_in, p_off64, 1) == -1);
    115 		printf("sendfile(%d, %d, [14627392582579060461], 1)"
    116 		       " = -1 EINVAL (%m)\n", sv[1], reg_in);
    117 		*p_off64 = 0xdefaced;
    118 	} else {
    119 		*p_off = 0xdefaced;
    120 	}
    121 	assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, 1) == 0);
    122 	printf("sendfile(%d, %d, [233811181], 1) = 0\n",
    123 	       sv[1], reg_in);
    124 
    125 	puts("+++ exited with 0 +++");
    126 	return 0;
    127 }
    128 
    129 #else
    130 
    131 SKIP_MAIN_UNDEFINED("__NR_sendfile")
    132 
    133 #endif
    134