Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv (at) altlinux.org>
      3  * Copyright (c) 2016-2018 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 
     31 #ifdef HAVE_PWRITEV
     32 
     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("{iov_base=\"", stdout);
     48 	for (i = 0; i < iov->iov_len; ++i) {
     49 		if (i < LIM)
     50 			printf("\\%d", (int) buf[i]);
     51 	}
     52 	printf("\"%s, iov_len=%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 == size) {
     69 			printf("... /* %p */", &iov[i]);
     70 			break;
     71 		}
     72 		if (i == LIM) {
     73 			fputs("...", stdout);
     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 
    100 	const off_t offset = 0xdefaceddeadbeefLL;
    101 	long rc;
    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 		fputs("pwritev(0, ", stdout);
    116 		print_iovec(iov + i, n, LEN - i);
    117 		rc = pwritev(0, iov + i, n, offset + LEN + i);
    118 		printf(", %u, %lld) = %ld %s (%m)\n",
    119 		       n, (long long) offset + LEN + i, rc, errno2name());
    120 	}
    121 
    122 	iov->iov_base = iov + LEN * 2;
    123 	rc = pwritev(0, iov, 1, -1);
    124 	printf("pwritev(0, [{iov_base=%p, iov_len=%d}], 1, -1) = %ld %s (%m)\n",
    125 	       iov->iov_base, LEN, rc, errno2name());
    126 
    127 	iov += LEN;
    128 	rc = pwritev(0, iov, 42, -2);
    129 	printf("pwritev(0, %p, 42, -2) = %ld %s (%m)\n",
    130 	       iov, rc, errno2name());
    131 
    132 	rc = pwritev(0, NULL, 1, -3);
    133 	printf("pwritev(0, NULL, 1, -3) = %ld %s (%m)\n",
    134 	       rc, errno2name());
    135 
    136 	rc = pwritev(0, iov, 0, -4);
    137 	printf("pwritev(0, [], 0, -4) = %ld %s (%m)\n",
    138 	       rc, errno2name());
    139 
    140 	puts("+++ exited with 0 +++");
    141 	return 0;
    142 }
    143 
    144 #else
    145 
    146 SKIP_MAIN_UNDEFINED("HAVE_PWRITEV")
    147 
    148 #endif
    149