Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / wordsize-64 / tst-writev.c
blob939f3b36eeea46055aaccdfd5b3dad561c785188
1 /* Copyright (C) 2011-2014 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 <paths.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/uio.h>
27 /* The purpose of this test is to verify that the INTERNAL_[V]SYSCALL_NCS
28 macros on 64-bit platforms don't cast the return type to (int) which would
29 erroneously sign extend the return value should the high bit of the bottom
30 half of the word be '1'. */
32 #if 0
33 /* Used to test the non power-of-2 code path. */
34 #undef IOV_MAX
35 #define IOV_MAX 1000
36 #endif
38 /* writev() should report that it has written EXPECTED number of bytes. */
39 #define EXPECTED ((size_t) INT32_MAX + 1)
41 static int
42 do_test (void)
44 struct iovec iv[IOV_MAX];
45 /* POSIX doesn't guarantee that IOV_MAX is pow of 2 but we're optimistic. */
46 size_t bufsz = EXPECTED / IOV_MAX;
47 size_t bufrem = EXPECTED % IOV_MAX;
49 /* If there's a remainder then IOV_MAX probably isn't a power of 2 and we
50 need to make bufsz bigger so that the last iovec, iv[IOV_MAX-1], is free
51 for the remainder. */
52 if (bufrem)
54 bufsz = bufsz + 1;
55 bufrem = EXPECTED - (bufsz * (IOV_MAX - 1));
58 /* We writev to /dev/null since we're just testing writev's return value. */
59 int fd = open (_PATH_DEVNULL, O_WRONLY);
60 if (fd == -1)
62 printf ("Unable to open /dev/null for writing.\n");
63 return -1;
66 iv[0].iov_base = malloc (bufsz);
67 if (iv[0].iov_base == NULL)
69 printf ("malloc (%zu) failed.\n", bufsz);
70 close (fd);
71 return -1;
73 iv[0].iov_len = bufsz;
75 /* We optimistically presume that there isn't a remainder and set all iovec
76 instances to the same base and len as the first instance. */
77 for (int i = 1; i < IOV_MAX; i++)
79 /* We don't care what the data is so reuse the allocation from iv[0]; */
80 iv[i].iov_base = iv[0].iov_base;
81 iv[i].iov_len = iv[0].iov_len;
84 /* If there is a remainder then we correct the last iov_len. */
85 if (bufrem)
86 iv[IOV_MAX - 1].iov_len = bufrem;
88 /* Write junk to /dev/null with the writev syscall in order to get a return
89 of INT32_MAX+1 bytes to verify that the INTERNAL_SYSCALL wrappers aren't
90 mangling the result if the signbit of a 32-bit number is set. */
91 ssize_t ret = writev (fd, iv, IOV_MAX);
93 free (iv[0].iov_base);
94 close (fd);
96 if (ret != (ssize_t) EXPECTED)
98 #ifdef ARTIFICIAL_LIMIT
99 if (ret != (ssize_t) ARTIFICIAL_LIMIT)
100 #endif
102 printf ("writev() return value: %zd != EXPECTED: %zd\n",
103 ret, EXPECTED);
104 return 1;
108 return 0;
111 /* Time enough for a large writev syscall to complete. */
112 #define TIMEOUT 20
114 #define TEST_FUNCTION do_test ()
115 #include "../test-skeleton.c"