1 /* 2 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv (at) 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_PREADV 31 32 # include <fcntl.h> 33 # include <stdio.h> 34 # include <sys/uio.h> 35 # include <unistd.h> 36 37 # define LEN 8 38 39 static void 40 print_iov(const struct iovec *iov) 41 { 42 unsigned int i; 43 unsigned char *buf = iov->iov_base; 44 45 fputs("{iov_base=\"", stdout); 46 for (i = 0; i < iov->iov_len; ++i) 47 printf("\\%d", (int) buf[i]); 48 printf("\", iov_len=%u}", (unsigned) iov->iov_len); 49 } 50 51 static void 52 print_iovec(const struct iovec *iov, unsigned int cnt) 53 { 54 unsigned int i; 55 putchar('['); 56 for (i = 0; i < cnt; ++i) { 57 if (i) 58 fputs(", ", stdout); 59 print_iov(&iov[i]); 60 } 61 putchar(']'); 62 } 63 64 int 65 main(void) 66 { 67 const off_t offset = 0xdefaceddeadbeefLL; 68 char *buf = tail_alloc(LEN); 69 struct iovec *iov = tail_alloc(sizeof(*iov)); 70 iov->iov_base = buf; 71 iov->iov_len = LEN; 72 73 (void) close(0); 74 if (open("/dev/zero", O_RDONLY)) 75 perror_msg_and_fail("open"); 76 77 if (preadv(0, iov, 1, offset) != LEN) 78 perror_msg_and_fail("preadv"); 79 printf("preadv(0, "); 80 print_iovec(iov, 1); 81 printf(", 1, %lld) = %u\n", (long long) offset, LEN); 82 83 if (preadv(0, iov, 1, -1) != -1) 84 perror_msg_and_fail("preadv"); 85 printf("preadv(0, [{iov_base=%p, iov_len=%zu}], 1, -1) = " 86 "-1 EINVAL (%m)\n", iov->iov_base, iov->iov_len); 87 88 if (preadv(0, NULL, 1, -2) != -1) 89 perror_msg_and_fail("preadv"); 90 printf("preadv(0, NULL, 1, -2) = -1 EINVAL (%m)\n"); 91 92 if (preadv(0, iov, 0, -3) != -1) 93 perror_msg_and_fail("preadv"); 94 printf("preadv(0, [], 0, -3) = -1 EINVAL (%m)\n"); 95 96 static const char tmp[] = "preadv-tmpfile"; 97 int fd = open(tmp, O_RDWR | O_CREAT | O_TRUNC, 0600); 98 if (fd < 0) 99 perror_msg_and_fail("open"); 100 if (unlink(tmp)) 101 perror_msg_and_fail("unlink"); 102 103 static const char w[] = "0123456789abcde"; 104 if (write(fd, w, LENGTH_OF(w)) != LENGTH_OF(w)) 105 perror_msg_and_fail("write"); 106 107 static const char r0_c[] = "01234567"; 108 static const char r1_c[] = "89abcde"; 109 110 const unsigned int r_len = (LENGTH_OF(w) + 1) / 2; 111 void *r0 = tail_alloc(r_len); 112 const struct iovec r0_iov_[] = { 113 { 114 .iov_base = r0, 115 .iov_len = r_len 116 } 117 }; 118 const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_)); 119 120 long rc; 121 122 rc = preadv(fd, r_iov, ARRAY_SIZE(r0_iov_), 0); 123 if (rc != (int) r_len) 124 perror_msg_and_fail("preadv: expected %u, returned %ld", 125 r_len, rc); 126 printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}], %u, 0) = %u\n", 127 fd, r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len); 128 129 void *r1 = tail_alloc(r_len); 130 void *r2 = tail_alloc(LENGTH_OF(w)); 131 const struct iovec r1_iov_[] = { 132 { 133 .iov_base = r1, 134 .iov_len = r_len 135 }, 136 { 137 .iov_base = r2, 138 .iov_len = LENGTH_OF(w) 139 } 140 }; 141 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_)); 142 143 rc = preadv(fd, r_iov, ARRAY_SIZE(r1_iov_), r_len); 144 if (rc != (int) LENGTH_OF(w) - (int) r_len) 145 perror_msg_and_fail("preadv: expected %d, returned %ld", 146 (int) LENGTH_OF(w) - r_len, rc); 147 printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}" 148 ", {iov_base=\"\", iov_len=%u}], %u, %u) = %u\n", 149 fd, r1_c, r_len, LENGTH_OF(w), ARRAY_SIZE(r1_iov_), 150 r_len, LENGTH_OF(w) - r_len); 151 152 puts("+++ exited with 0 +++"); 153 return 0; 154 } 155 156 #else 157 158 SKIP_MAIN_UNDEFINED("HAVE_PREADV") 159 160 #endif 161