sparc: Fix sparc64 memmove length comparison (BZ 31266)
[glibc.git] / rt / tst-aio64.c
blobdfae2c7a096d162d50f6e28936f49fa1cf6579c5
1 /* Tests for 64bit AIO in librt.
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <https://www.gnu.org/licenses/>. */
19 #define _LARGEFILE_SOURCE 1
20 #include <aio.h>
21 #include <errno.h>
22 #include <error.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
30 /* Prototype for our test function. */
31 extern void do_prepare (int argc, char *argv[]);
32 extern int do_test (int argc, char *argv[]);
34 /* We have a preparation function. */
35 #define PREPARE do_prepare
37 /* This defines the `main' function and some more. */
38 #include <test-skeleton.c>
41 /* These are for the temporary file we generate. */
42 char *name;
43 int fd;
45 void
46 do_prepare (int argc, char *argv[])
48 size_t name_len;
50 name_len = strlen (test_dir);
51 name = xmalloc (name_len + sizeof ("/aioXXXXXX"));
52 mempcpy (mempcpy (name, test_dir, name_len),
53 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
55 /* Open our test file. */
56 fd = mkstemp (name);
57 if (fd == -1)
58 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
59 add_temp_file (name);
63 static int
64 test_file (const void *buf, size_t size, int fd, const char *msg)
66 struct stat st;
67 char tmp[size];
69 errno = 0;
70 if (fstat (fd, &st) < 0)
72 error (0, errno, "%s: failed stat", msg);
73 return 1;
76 if (st.st_size != (off_t) size)
78 error (0, errno, "%s: wrong size: %lu, should be %lu",
79 msg, (unsigned long int) st.st_size, (unsigned long int) size);
80 return 1;
83 if (pread (fd, tmp, size, 0) != (ssize_t) size)
85 error (0, errno, "%s: failed pread", msg);
86 return 1;
89 if (memcmp (buf, tmp, size) != 0)
91 error (0, errno, "%s: failed comparison", msg);
92 return 1;
95 printf ("%s test ok\n", msg);
97 return 0;
101 static int
102 do_wait (struct aiocb64 **cbp, size_t nent, int allowed_err)
104 int go_on;
105 size_t cnt;
106 int result = 0;
110 aio_suspend64 ((const struct aiocb64 *const *) cbp, nent, NULL);
111 go_on = 0;
112 for (cnt = 0; cnt < nent; ++cnt)
113 if (cbp[cnt] != NULL)
115 if (aio_error64 (cbp[cnt]) == EINPROGRESS)
116 go_on = 1;
117 else
119 if (aio_return64 (cbp[cnt]) == -1
120 && (allowed_err == 0
121 || aio_error64 (cbp[cnt]) != allowed_err))
123 error (0, aio_error64 (cbp[cnt]), "Operation failed\n");
124 result = 1;
126 cbp[cnt] = NULL;
130 while (go_on);
132 return result;
137 do_test (int argc, char *argv[])
139 struct aiocb64 cbs[10];
140 struct aiocb64 cbs_fsync;
141 struct aiocb64 *cbp[10];
142 struct aiocb64 *cbp_fsync[1];
143 char buf[1000];
144 size_t cnt;
145 int result = 0;
147 /* Preparation. */
148 for (cnt = 0; cnt < 10; ++cnt)
150 cbs[cnt].aio_fildes = fd;
151 cbs[cnt].aio_reqprio = 0;
152 cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
153 cbs[cnt].aio_nbytes = 100;
154 cbs[cnt].aio_offset = cnt * 100;
155 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
157 cbp[cnt] = &cbs[cnt];
160 /* First a simple test. */
161 for (cnt = 10; cnt > 0; )
162 if (aio_write64 (cbp[--cnt]) < 0 && errno == ENOSYS)
164 error (0, 0, "no aio support in this configuration");
165 return 0;
167 /* Wait 'til the results are there. */
168 result |= do_wait (cbp, 10, 0);
169 /* Test this. */
170 result |= test_file (buf, sizeof (buf), fd, "aio_write");
172 /* Read now as we've written it. */
173 memset (buf, '\0', sizeof (buf));
174 /* Issue the commands. */
175 for (cnt = 10; cnt > 0; )
177 --cnt;
178 cbp[cnt] = &cbs[cnt];
179 aio_read64 (cbp[cnt]);
181 /* Wait 'til the results are there. */
182 result |= do_wait (cbp, 10, 0);
183 /* Test this. */
184 for (cnt = 0; cnt < 1000; ++cnt)
185 if (buf[cnt] != '0' + (cnt / 100))
187 result = 1;
188 error (0, 0, "comparison failed for aio_read test");
189 break;
192 if (cnt == 1000)
193 puts ("aio_read test ok");
195 /* Remove the test file contents. */
196 if (ftruncate64 (fd, 0) < 0)
198 error (0, errno, "ftruncate failed\n");
199 result = 1;
202 /* Test lio_listio. */
203 for (cnt = 0; cnt < 10; ++cnt)
205 cbs[cnt].aio_lio_opcode = LIO_WRITE;
206 cbp[cnt] = &cbs[cnt];
208 /* Issue the command. */
209 lio_listio64 (LIO_WAIT, cbp, 10, NULL);
210 /* ...and immediately test it since we started it in wait mode. */
211 result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
213 /* Test aio_fsync. */
214 cbs_fsync.aio_fildes = fd;
215 cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
216 cbp_fsync[0] = &cbs_fsync;
218 /* Remove the test file contents first. */
219 if (ftruncate64 (fd, 0) < 0)
221 error (0, errno, "ftruncate failed\n");
222 result = 1;
225 /* Write again. */
226 for (cnt = 10; cnt > 0; )
227 aio_write64 (cbp[--cnt]);
229 if (aio_fsync64 (O_SYNC, &cbs_fsync) < 0)
231 error (0, errno, "aio_fsync failed\n");
232 result = 1;
234 result |= do_wait (cbp_fsync, 1, 0);
236 /* ...and test since all data should be on disk now. */
237 result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
239 /* Test aio_cancel. */
240 /* Remove the test file contents first. */
241 if (ftruncate64 (fd, 0) < 0)
243 error (0, errno, "ftruncate failed\n");
244 result = 1;
247 /* Write again. */
248 for (cnt = 10; cnt > 0; )
249 aio_write64 (cbp[--cnt]);
251 /* Cancel all requests. */
252 if (aio_cancel64 (fd, NULL) == -1)
253 printf ("aio_cancel64 (fd, NULL) cannot cancel anything\n");
255 result |= do_wait (cbp, 10, ECANCELED);
257 /* Another test for aio_cancel. */
258 /* Remove the test file contents first. */
259 if (ftruncate64 (fd, 0) < 0)
261 error (0, errno, "ftruncate failed\n");
262 result = 1;
265 /* Write again. */
266 for (cnt = 10; cnt > 0; )
268 --cnt;
269 cbp[cnt] = &cbs[cnt];
270 aio_write64 (cbp[cnt]);
272 puts ("finished3");
274 /* Cancel all requests. */
275 for (cnt = 10; cnt > 0; )
276 if (aio_cancel64 (fd, cbp[--cnt]) == -1)
277 /* This is not an error. The request can simply be finished. */
278 printf ("aio_cancel64 (fd, cbp[%zd]) cannot be canceled\n", cnt);
279 puts ("finished2");
281 result |= do_wait (cbp, 10, ECANCELED);
283 puts ("finished");
285 return result;