(root)/
strace-6.5/
tests-mx32/
pwritev.c
       1  /*
       2   * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@strace.io>
       3   * Copyright (c) 2016-2021 The strace developers.
       4   * All rights reserved.
       5   *
       6   * SPDX-License-Identifier: GPL-2.0-or-later
       7   */
       8  
       9  #include "tests.h"
      10  
      11  #ifdef HAVE_PWRITEV
      12  
      13  # include <fcntl.h>
      14  # include <stdio.h>
      15  # include <sys/uio.h>
      16  # include <unistd.h>
      17  
      18  # define LEN 8
      19  # define LIM (LEN - 1)
      20  
      21  static void
      22  print_iov(const struct iovec *iov)
      23  {
      24  	unsigned int i;
      25  	unsigned char *buf = iov->iov_base;
      26  
      27  	fputs("{iov_base=\"", stdout);
      28  	for (i = 0; i < iov->iov_len; ++i) {
      29  		if (i < LIM)
      30  			printf("\\%d", (int) buf[i]);
      31  	}
      32  	printf("\"%s, iov_len=%u}",
      33  	       i > LIM ? "..." : "", (unsigned) iov->iov_len);
      34  }
      35  
      36  static void
      37  print_iovec(const struct iovec *iov, unsigned int cnt, unsigned int size)
      38  {
      39  	if (!size) {
      40  		printf("%p", iov);
      41  		return;
      42  	}
      43  	putchar('[');
      44  	for (unsigned int i = 0; i < cnt; ++i) {
      45  		if (i)
      46  			fputs(", ", stdout);
      47  		if (i == size) {
      48  			printf("... /* %p */", &iov[i]);
      49  			break;
      50  		}
      51  		if (i == LIM) {
      52  			fputs("...", stdout);
      53  			break;
      54  		}
      55  		print_iov(&iov[i]);
      56  	}
      57  	putchar(']');
      58  }
      59  
      60  /* for pwritev(0, NULL, 1, -3) */
      61  DIAG_PUSH_IGNORE_NONNULL
      62  
      63  int
      64  main(void)
      65  {
      66  	(void) close(0);
      67  	if (open("/dev/null", O_WRONLY))
      68  		perror_msg_and_fail("open");
      69  
      70  	char *buf = tail_alloc(LEN);
      71  	for (unsigned int i = 0; i < LEN; ++i)
      72  		buf[i] = i;
      73  
      74  	struct iovec *iov = tail_alloc(sizeof(*iov) * LEN);
      75  	for (unsigned int i = 0; i < LEN; ++i) {
      76  		buf[i] = i;
      77  		iov[i].iov_base = &buf[i];
      78  		iov[i].iov_len = LEN - i;
      79  	}
      80  
      81  	const off_t offset = 0xdefaceddeadbeefLL;
      82  	long rc;
      83  	int written = 0;
      84  	for (unsigned int i = 0; i < LEN; ++i) {
      85  		written += iov[i].iov_len;
      86  		if (pwritev(0, iov, i + 1, offset + i) != written)
      87  			perror_msg_and_fail("pwritev");
      88  		fputs("pwritev(0, ", stdout);
      89  		print_iovec(iov, i + 1, LEN);
      90  		printf(", %u, %lld) = %d\n",
      91  		       i + 1, (long long) offset + i, written);
      92  	}
      93  
      94  	for (unsigned int i = 0; i <= LEN; ++i) {
      95  		unsigned int n = LEN + 1 - i;
      96  		fputs("pwritev(0, ", stdout);
      97  		print_iovec(iov + i, n, LEN - i);
      98  		rc = pwritev(0, iov + i, n, offset + LEN + i);
      99  		printf(", %u, %lld) = %ld %s (%m)\n",
     100  		       n, (long long) offset + LEN + i, rc, errno2name());
     101  	}
     102  
     103  	iov->iov_base = iov + LEN * 2;
     104  	rc = pwritev(0, iov, 1, -1);
     105  	printf("pwritev(0, [{iov_base=%p, iov_len=%d}], 1, -1) = %ld %s (%m)\n",
     106  	       iov->iov_base, LEN, rc, errno2name());
     107  
     108  	iov += LEN;
     109  	rc = pwritev(0, iov, 42, -2);
     110  	printf("pwritev(0, %p, 42, -2) = %ld %s (%m)\n",
     111  	       iov, rc, errno2name());
     112  
     113  	rc = pwritev(0, NULL, 1, -3);
     114  	printf("pwritev(0, NULL, 1, -3) = %ld %s (%m)\n",
     115  	       rc, errno2name());
     116  
     117  	rc = pwritev(0, iov, 0, -4);
     118  	printf("pwritev(0, [], 0, -4) = %ld %s (%m)\n",
     119  	       rc, errno2name());
     120  
     121  	puts("+++ exited with 0 +++");
     122  	return 0;
     123  }
     124  
     125  DIAG_POP_IGNORE_NONNULL
     126  
     127  #else
     128  
     129  SKIP_MAIN_UNDEFINED("HAVE_PWRITEV")
     130  
     131  #endif