]> granicus.if.org Git - strace/blob - tests/sendfile.c
Add copyright headers
[strace] / tests / sendfile.c
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_sendfile
33
34 # include <assert.h>
35 # include <errno.h>
36 # include <fcntl.h>
37 # include <stdio.h>
38 # include <stdint.h>
39 # include <unistd.h>
40 # include <sys/socket.h>
41 # include <sys/stat.h>
42
43 int
44 main(int ac, const char **av)
45 {
46         assert(ac == 1);
47
48         (void) close(0);
49         if (open("/dev/zero", O_RDONLY) != 0)
50                 perror_msg_and_skip("open: %s", "/dev/zero");
51
52         int sv[2];
53         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
54                 perror_msg_and_skip("socketpair");
55
56         int reg_in = open(av[0], O_RDONLY);
57         if (reg_in < 0)
58                 perror_msg_and_fail("open: %s", av[0]);
59
60         struct stat stb;
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);
65
66         const size_t page_len = get_page_size();
67         assert(syscall(__NR_sendfile, 0, 1, NULL, page_len) == -1);
68         if (EBADF != errno)
69                 perror_msg_and_skip("sendfile");
70         printf("sendfile(0, 1, NULL, %lu) = -1 EBADF (%m)\n",
71                (unsigned long) page_len);
72
73         TAIL_ALLOC_OBJECT_VAR_PTR(uint32_t, p_off);
74         void *p = p_off + 1;
75         *p_off = 0;
76
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);
80
81         assert(syscall(__NR_sendfile, sv[1], reg_in, NULL, alen)
82                == (long) alen);
83         printf("sendfile(%d, %d, NULL, %lu) = %lu\n",
84                sv[1], reg_in, (unsigned long) alen,
85                (unsigned long) alen);
86
87         p = p_off;
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);
92                 --p_off;
93                 *p_off = 0;
94                 assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, alen)
95                        == (long) alen);
96         }
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);
100
101         assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, stb.st_size + 1)
102                == (long) blen);
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);
108
109         if (p_off != p) {
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;
116         } else {
117                 *p_off = 0xdefaced;
118         }
119         assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, 1) == 0);
120         printf("sendfile(%d, %d, [233811181], 1) = 0\n",
121                sv[1], reg_in);
122
123         puts("+++ exited with 0 +++");
124         return 0;
125 }
126
127 #else
128
129 SKIP_MAIN_UNDEFINED("__NR_sendfile")
130
131 #endif