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>
40 # include <sys/socket.h>
41 # include <sys/stat.h>
44 main(int ac, const char **av)
49 if (open("/dev/zero", O_RDONLY) != 0)
50 perror_msg_and_skip("open: %s", "/dev/zero");
53 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
54 perror_msg_and_skip("socketpair");
56 int reg_in = open(av[0], O_RDONLY);
58 perror_msg_and_fail("open: %s", av[0]);
61 assert(fstat(reg_in, &stb) == 0);
62 const size_t blen = stb.st_size / 3;
63 const size_t alen = stb.st_size - blen;
64 assert(S_ISREG(stb.st_mode) && blen > 0);
66 const size_t page_len = get_page_size();
67 assert(syscall(__NR_sendfile, 0, 1, NULL, page_len) == -1);
69 perror_msg_and_skip("sendfile");
70 printf("sendfile(0, 1, NULL, %lu) = -1 EBADF (%m)\n",
71 (unsigned long) page_len);
73 TAIL_ALLOC_OBJECT_VAR_PTR(uint32_t, p_off);
77 assert(syscall(__NR_sendfile, 0, 1, p, page_len) == -1);
78 printf("sendfile(0, 1, %#lx, %lu) = -1 EFAULT (%m)\n",
79 (unsigned long) p, (unsigned long) page_len);
81 assert(syscall(__NR_sendfile, sv[1], reg_in, NULL, alen)
83 printf("sendfile(%d, %d, NULL, %lu) = %lu\n",
84 sv[1], reg_in, (unsigned long) alen,
85 (unsigned long) alen);
88 if (syscall(__NR_sendfile, sv[1], reg_in, p_off, alen) != (long) alen) {
89 printf("sendfile(%d, %d, %#lx, %lu) = -1 EFAULT (%m)\n",
90 sv[1], reg_in, (unsigned long) p_off,
91 (unsigned long) alen);
94 assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, alen)
97 printf("sendfile(%d, %d, [0] => [%lu], %lu) = %lu\n",
98 sv[1], reg_in, (unsigned long) alen,
99 (unsigned long) alen, (unsigned long) alen);
101 assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, stb.st_size + 1)
103 printf("sendfile(%d, %d, [%lu] => [%lu], %lu) = %lu\n",
104 sv[1], reg_in, (unsigned long) alen,
105 (unsigned long) stb.st_size,
106 (unsigned long) stb.st_size + 1,
107 (unsigned long) blen);
110 uint64_t *p_off64 = (uint64_t *) p_off;
111 *p_off64 = 0xcafef00dfacefeedULL;
112 assert(syscall(__NR_sendfile, sv[1], reg_in, p_off64, 1) == -1);
113 printf("sendfile(%d, %d, [14627392582579060461], 1)"
114 " = -1 EINVAL (%m)\n", sv[1], reg_in);
115 *p_off64 = 0xdefaced;
119 assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, 1) == 0);
120 printf("sendfile(%d, %d, [233811181], 1) = 0\n",
123 puts("+++ exited with 0 +++");
129 SKIP_MAIN_UNDEFINED("__NR_sendfile")