Dmitry V. Levin | d461151 | 2016-03-30 03:54:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org> |
| 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 | |
| 28 | #include "tests.h" |
| 29 | |
| 30 | #ifdef HAVE_PWRITEV |
| 31 | |
Dmitry V. Levin | f73a3a4 | 2016-04-19 21:10:44 +0000 | [diff] [blame] | 32 | # include <errno.h> |
Dmitry V. Levin | d461151 | 2016-03-30 03:54:21 +0000 | [diff] [blame] | 33 | # include <fcntl.h> |
| 34 | # include <stdio.h> |
| 35 | # include <sys/uio.h> |
| 36 | # include <unistd.h> |
| 37 | |
| 38 | # define LEN 8 |
| 39 | # define LIM (LEN - 1) |
| 40 | |
| 41 | static void |
| 42 | print_iov(const struct iovec *iov) |
| 43 | { |
| 44 | unsigned int i; |
| 45 | unsigned char *buf = iov->iov_base; |
| 46 | |
| 47 | fputs("{\"", stdout); |
| 48 | for (i = 0; i < iov->iov_len; ++i) { |
| 49 | if (i < LIM) |
| 50 | printf("\\%d", (int) buf[i]); |
| 51 | } |
| 52 | printf("\"%s, %u}", |
| 53 | i > LIM ? "..." : "", (unsigned) iov->iov_len); |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | print_iovec(const struct iovec *iov, unsigned int cnt, unsigned int size) |
| 58 | { |
| 59 | if (!size) { |
| 60 | printf("%p", iov); |
| 61 | return; |
| 62 | } |
| 63 | unsigned int i; |
| 64 | putchar('['); |
| 65 | for (i = 0; i < cnt; ++i) { |
| 66 | if (i) |
| 67 | fputs(", ", stdout); |
| 68 | if (i == LIM) { |
| 69 | fputs("...", stdout); |
| 70 | break; |
| 71 | } |
| 72 | if (i == size) { |
| 73 | printf("%p", &iov[i]); |
| 74 | break; |
| 75 | } |
| 76 | print_iov(&iov[i]); |
| 77 | } |
| 78 | putchar(']'); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | main(void) |
| 83 | { |
| 84 | (void) close(0); |
| 85 | if (open("/dev/null", O_WRONLY)) |
| 86 | perror_msg_and_fail("open"); |
| 87 | |
| 88 | char *buf = tail_alloc(LEN); |
| 89 | unsigned i; |
| 90 | for (i = 0; i < LEN; ++i) |
| 91 | buf[i] = i; |
| 92 | |
| 93 | struct iovec *iov = tail_alloc(sizeof(*iov) * LEN); |
| 94 | for (i = 0; i < LEN; ++i) { |
| 95 | buf[i] = i; |
| 96 | iov[i].iov_base = &buf[i]; |
| 97 | iov[i].iov_len = LEN - i; |
| 98 | } |
| 99 | tail_alloc(1); |
| 100 | |
| 101 | const off_t offset = 0xdefaceddeadbeefLL; |
| 102 | int written = 0; |
| 103 | for (i = 0; i < LEN; ++i) { |
| 104 | written += iov[i].iov_len; |
| 105 | if (pwritev(0, iov, i + 1, offset + i) != written) |
| 106 | perror_msg_and_fail("pwritev"); |
| 107 | fputs("pwritev(0, ", stdout); |
| 108 | print_iovec(iov, i + 1, LEN); |
| 109 | printf(", %u, %lld) = %d\n", |
| 110 | i + 1, (long long) offset + i, written); |
| 111 | } |
| 112 | |
| 113 | for (i = 0; i <= LEN; ++i) { |
| 114 | unsigned int n = LEN + 1 - i; |
| 115 | if (pwritev(0, iov + i, n, offset + LEN + i) != -1) |
| 116 | perror_msg_and_fail("pwritev"); |
| 117 | fputs("pwritev(0, ", stdout); |
| 118 | print_iovec(iov + i, n, LEN - i); |
Dmitry V. Levin | f73a3a4 | 2016-04-19 21:10:44 +0000 | [diff] [blame] | 119 | printf(", %u, %lld) = -1 %s (%m)\n", |
| 120 | n, (long long) offset + LEN + i, |
| 121 | errno == EINVAL ? "EINVAL" : "EFAULT"); |
Dmitry V. Levin | d461151 | 2016-03-30 03:54:21 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | iov->iov_base = iov + LEN * 2; |
| 125 | if (pwritev(0, iov, 1, -1) != -1) |
| 126 | perror_msg_and_fail("pwritev"); |
| 127 | printf("pwritev(0, [{%p, %d}], 1, -1) = -1 EINVAL (%m)\n", |
| 128 | iov->iov_base, LEN); |
| 129 | |
| 130 | iov += LEN; |
| 131 | if (pwritev(0, iov, 42, -2) != -1) |
| 132 | perror_msg_and_fail("pwritev"); |
| 133 | printf("pwritev(0, %p, 42, -2) = -1 EINVAL (%m)\n", iov); |
| 134 | |
| 135 | if (pwritev(0, NULL, 1, -3) != -1) |
| 136 | perror_msg_and_fail("pwritev"); |
| 137 | printf("pwritev(0, NULL, 1, -3) = -1 EINVAL (%m)\n"); |
| 138 | |
| 139 | if (pwritev(0, NULL, 0, -4) != -1) |
| 140 | perror_msg_and_fail("pwritev"); |
| 141 | printf("pwritev(0, [], 0, -4) = -1 EINVAL (%m)\n"); |
| 142 | |
| 143 | puts("+++ exited with 0 +++"); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | #else |
| 148 | |
| 149 | SKIP_MAIN_UNDEFINED("HAVE_PWRITEV") |
| 150 | |
| 151 | #endif |