1 /*
2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@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_sendfile64
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
main(void)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_sendfile64, 0, 1, NULL, page_size) == -1);
56 if (EBADF != errno)
57 perror_msg_and_skip("sendfile64");
58 printf("sendfile64(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[] = "sendfile64-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_CONST_PTR(uint64_t, p_off);
83 void *p = p_off + 1;
84 *p_off = 0;
85
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);
88
89 assert(syscall(__NR_sendfile64, sv[1], reg_in, NULL, alen)
90 == (long) alen);
91 printf("sendfile64(%d, %d, NULL, %u) = %u\n",
92 sv[1], reg_in, alen, alen);
93
94 assert(syscall(__NR_sendfile64, sv[1], reg_in, p_off, alen)
95 == (long) alen);
96 printf("sendfile64(%d, %d, [0] => [%u], %u) = %u\n",
97 sv[1], reg_in, alen, alen, alen);
98
99 assert(syscall(__NR_sendfile64, sv[1], reg_in, p_off, file_size + 1)
100 == (long) blen);
101 printf("sendfile64(%d, %d, [%u] => [%u], %u) = %u\n",
102 sv[1], reg_in, alen, file_size, file_size + 1, blen);
103
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);
108
109 *p_off = 0xfacefeed;
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);
112
113 puts("+++ exited with 0 +++");
114 return 0;
115 }
116
117 #else
118
119 SKIP_MAIN_UNDEFINED("__NR_sendfile64")
120
121 #endif
122