2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015-2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
30 #include <asm/unistd.h>
32 #ifdef __NR_sendfile64
41 # include <sys/socket.h>
47 if (open("/dev/zero", O_RDONLY) != 0)
48 perror_msg_and_skip("open: %s", "/dev/zero");
51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
52 perror_msg_and_skip("socketpair");
54 const unsigned int page_size = get_page_size();
55 assert(syscall(__NR_sendfile64, 0, 1, NULL, page_size) == -1);
57 perror_msg_and_skip("sendfile64");
58 printf("sendfile64(0, 1, NULL, %u) = -1 EBADF (%m)\n", page_size);
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");
65 error_msg_and_skip("SO_SNDBUF too small: %u", file_size);
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;
73 static const char fname[] = "sendfile64-tmpfile";
74 int reg_in = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0600);
76 perror_msg_and_fail("open: %s", fname);
78 perror_msg_and_fail("unlink: %s", fname);
79 if (ftruncate(reg_in, file_size))
80 perror_msg_and_fail("ftruncate: %s", fname);
82 TAIL_ALLOC_OBJECT_CONST_PTR(uint64_t, p_off);
86 assert(syscall(__NR_sendfile64, 0, 1, p, page_size) == -1);
87 printf("sendfile64(0, 1, %p, %u) = -1 EFAULT (%m)\n", p, page_size);
89 assert(syscall(__NR_sendfile64, sv[1], reg_in, NULL, alen)
91 printf("sendfile64(%d, %d, NULL, %u) = %u\n",
92 sv[1], reg_in, alen, alen);
94 assert(syscall(__NR_sendfile64, sv[1], reg_in, p_off, alen)
96 printf("sendfile64(%d, %d, [0] => [%u], %u) = %u\n",
97 sv[1], reg_in, alen, alen, alen);
99 assert(syscall(__NR_sendfile64, sv[1], reg_in, p_off, file_size + 1)
101 printf("sendfile64(%d, %d, [%u] => [%u], %u) = %u\n",
102 sv[1], reg_in, alen, file_size, file_size + 1, blen);
104 *p_off = 0xcafef00dfacefeedULL;
105 assert(syscall(__NR_sendfile64, sv[1], reg_in, p_off, 1) == -1);
106 printf("sendfile64(%d, %d, [14627392582579060461], 1)"
107 " = -1 EINVAL (%m)\n", sv[1], reg_in);
110 assert(syscall(__NR_sendfile64, sv[1], reg_in, p_off, 1) == 0);
111 printf("sendfile64(%d, %d, [4207869677], 1) = 0\n", sv[1], reg_in);
113 puts("+++ exited with 0 +++");
119 SKIP_MAIN_UNDEFINED("__NR_sendfile64")