Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 28 | #include "tests.h" |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 29 | #include <sys/syscall.h> |
| 30 | |
| 31 | #ifdef __NR_sendfile |
| 32 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 33 | # include <assert.h> |
| 34 | # include <errno.h> |
| 35 | # include <fcntl.h> |
| 36 | # include <stdio.h> |
| 37 | # include <stdint.h> |
| 38 | # include <unistd.h> |
| 39 | # include <sys/socket.h> |
| 40 | # include <sys/stat.h> |
| 41 | |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 42 | int |
| 43 | main(int ac, const char **av) |
| 44 | { |
| 45 | assert(ac == 2); |
| 46 | |
| 47 | (void) close(0); |
| 48 | if (open("/dev/zero", O_RDONLY) != 0) |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 49 | perror_msg_and_skip("open: %s", "/dev/zero"); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 50 | |
| 51 | int sv[2]; |
| 52 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 53 | perror_msg_and_skip("socketpair"); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 54 | |
| 55 | int reg_in = open(av[1], O_RDONLY); |
| 56 | if (reg_in < 0) |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 57 | perror_msg_and_fail("open: %s", av[1]); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 58 | |
| 59 | struct stat stb; |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 60 | assert(fstat(reg_in, &stb) == 0); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 61 | const size_t blen = stb.st_size / 3; |
| 62 | const size_t alen = stb.st_size - blen; |
| 63 | assert(S_ISREG(stb.st_mode) && blen > 0); |
| 64 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 65 | const size_t page_len = get_page_size(); |
| 66 | assert(syscall(__NR_sendfile, 0, 1, NULL, page_len) == -1); |
| 67 | if (EBADF != errno) |
| 68 | perror_msg_and_skip("sendfile"); |
| 69 | printf("sendfile(0, 1, NULL, %lu) = -1 EBADF (%m)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 70 | (unsigned long) page_len); |
| 71 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 72 | uint32_t *p_off = tail_alloc(sizeof(uint32_t)); |
| 73 | void *p = p_off + 1; |
| 74 | *p_off = 0; |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 75 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 76 | assert(syscall(__NR_sendfile, 0, 1, p, page_len) == -1); |
| 77 | printf("sendfile(0, 1, %#lx, %lu) = -1 EFAULT (%m)\n", |
| 78 | (unsigned long) p, (unsigned long) page_len); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 79 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 80 | assert(syscall(__NR_sendfile, sv[1], reg_in, NULL, alen) |
| 81 | == (long) alen); |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 82 | printf("sendfile(%d, %d, NULL, %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 83 | sv[1], reg_in, (unsigned long) alen, |
| 84 | (unsigned long) alen); |
| 85 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 86 | p = p_off; |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 87 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, alen) != (long) alen) { |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 88 | printf("sendfile(%d, %d, %#lx, %lu) = -1 EFAULT (%m)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 89 | sv[1], reg_in, (unsigned long) p_off, |
| 90 | (unsigned long) alen); |
| 91 | --p_off; |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 92 | *p_off = 0; |
| 93 | assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, alen) |
| 94 | == (long) alen); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 95 | } |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 96 | printf("sendfile(%d, %d, [0] => [%lu], %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 97 | sv[1], reg_in, (unsigned long) alen, |
| 98 | (unsigned long) alen, (unsigned long) alen); |
| 99 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 100 | assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, stb.st_size + 1) |
| 101 | == (long) blen); |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 102 | printf("sendfile(%d, %d, [%lu] => [%lu], %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 103 | sv[1], reg_in, (unsigned long) alen, |
| 104 | (unsigned long) stb.st_size, |
| 105 | (unsigned long) stb.st_size + 1, |
| 106 | (unsigned long) blen); |
| 107 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 108 | if (p_off != p) { |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 109 | uint64_t *p_off64 = (uint64_t *) p_off; |
| 110 | *p_off64 = 0xcafef00dfacefeed; |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 111 | assert(syscall(__NR_sendfile, sv[1], reg_in, p_off64, 1) == -1); |
| 112 | printf("sendfile(%d, %d, [14627392582579060461], 1)" |
| 113 | " = -1 EINVAL (%m)\n", sv[1], reg_in); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 114 | *p_off64 = 0xdefaced; |
| 115 | } else { |
| 116 | *p_off = 0xdefaced; |
| 117 | } |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 118 | assert(syscall(__NR_sendfile, sv[1], reg_in, p_off, 1) == 0); |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 119 | printf("sendfile(%d, %d, [233811181], 1) = 0\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 120 | sv[1], reg_in); |
| 121 | |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 122 | puts("+++ exited with 0 +++"); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | #else |
| 127 | |
Dmitry V. Levin | 54ba3b5 | 2016-01-03 21:52:58 +0000 | [diff] [blame^] | 128 | SKIP_MAIN_UNDEFINED("__NR_sendfile") |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 129 | |
| 130 | #endif |