Fix botched up regeneration in the last commit
[glibc.git] / sysdeps / wordsize-64 / tst-writev.c
blobbda2b3fe74c66c2cee584fb991aef406a523a3ea
1 /* Copyright (C) 2011-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ryan S. Arnold <rsa@us.ibm.com>, 2011.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <paths.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/uio.h>
29 /* The purpose of this test is to verify that the INTERNAL_[V]SYSCALL_NCS
30 macros on 64-bit platforms don't cast the return type to (int) which would
31 erroneously sign extend the return value should the high bit of the bottom
32 half of the word be '1'. */
34 #if 0
35 /* Used to test the non power-of-2 code path. */
36 #undef IOV_MAX
37 #define IOV_MAX 1000
38 #endif
40 /* writev() should report that it has written EXPECTED number of bytes. */
41 #define EXPECTED ((size_t) INT32_MAX + 1)
43 static int
44 do_test (void)
46 struct iovec iv[IOV_MAX];
47 /* POSIX doesn't guarantee that IOV_MAX is pow of 2 but we're optimistic. */
48 size_t bufsz = EXPECTED / IOV_MAX;
49 size_t bufrem = EXPECTED % IOV_MAX;
51 /* If there's a remainder then IOV_MAX probably isn't a power of 2 and we
52 need to make bufsz bigger so that the last iovec, iv[IOV_MAX-1], is free
53 for the remainder. */
54 if (bufrem)
56 bufsz = bufsz + 1;
57 bufrem = EXPECTED - (bufsz * (IOV_MAX - 1));
60 /* We writev to /dev/null since we're just testing writev's return value. */
61 int fd = open (_PATH_DEVNULL, O_WRONLY);
62 if (fd == -1)
64 printf ("Unable to open /dev/null for writing.\n");
65 return -1;
68 iv[0].iov_base = malloc (bufsz);
69 if (iv[0].iov_base == NULL)
71 printf ("malloc (%zu) failed.\n", bufsz);
72 close (fd);
73 return -1;
75 iv[0].iov_len = bufsz;
77 /* We optimistically presume that there isn't a remainder and set all iovec
78 instances to the same base and len as the first instance. */
79 for (int i = 1; i < IOV_MAX; i++)
81 /* We don't care what the data is so reuse the allocation from iv[0]; */
82 iv[i].iov_base = iv[0].iov_base;
83 iv[i].iov_len = iv[0].iov_len;
86 /* If there is a remainder then we correct the last iov_len. */
87 if (bufrem)
88 iv[IOV_MAX - 1].iov_len = bufrem;
90 /* Write junk to /dev/null with the writev syscall in order to get a return
91 of INT32_MAX+1 bytes to verify that the INTERNAL_SYSCALL wrappers aren't
92 mangling the result if the signbit of a 32-bit number is set. */
93 ssize_t ret = writev (fd, iv, IOV_MAX);
95 free (iv[0].iov_base);
96 close (fd);
98 if (ret != (ssize_t) EXPECTED)
100 #ifdef ARTIFICIAL_LIMIT
101 if (ret != (ssize_t) ARTIFICIAL_LIMIT)
102 #endif
104 printf ("writev() return value: %zd != EXPECTED: %zd\n",
105 ret, EXPECTED);
106 return 1;
110 return 0;
113 /* Time enough for a large writev syscall to complete. */
114 #define TIMEOUT 20
116 #define TEST_FUNCTION do_test ()
117 #include "../test-skeleton.c"