Enable support for GCC 11 -Wmismatched-dealloc.
[glibc.git] / rt / tst-aio64.c
blobc73534553c6a347630ce8d3ac69076ed9af9bc15
1 /* Tests for 64bit AIO in librt.
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #define _LARGEFILE_SOURCE 1
21 #include <aio.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
31 /* Prototype for our test function. */
32 extern void do_prepare (int argc, char *argv[]);
33 extern int do_test (int argc, char *argv[]);
35 /* We have a preparation function. */
36 #define PREPARE do_prepare
38 /* This defines the `main' function and some more. */
39 #include <test-skeleton.c>
42 /* These are for the temporary file we generate. */
43 char *name;
44 int fd;
46 void
47 do_prepare (int argc, char *argv[])
49 size_t name_len;
51 name_len = strlen (test_dir);
52 name = xmalloc (name_len + sizeof ("/aioXXXXXX"));
53 mempcpy (mempcpy (name, test_dir, name_len),
54 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
56 /* Open our test file. */
57 fd = mkstemp (name);
58 if (fd == -1)
59 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
60 add_temp_file (name);
64 static int
65 test_file (const void *buf, size_t size, int fd, const char *msg)
67 struct stat st;
68 char tmp[size];
70 errno = 0;
71 if (fstat (fd, &st) < 0)
73 error (0, errno, "%s: failed stat", msg);
74 return 1;
77 if (st.st_size != (off_t) size)
79 error (0, errno, "%s: wrong size: %lu, should be %lu",
80 msg, (unsigned long int) st.st_size, (unsigned long int) size);
81 return 1;
84 if (pread (fd, tmp, size, 0) != (ssize_t) size)
86 error (0, errno, "%s: failed pread", msg);
87 return 1;
90 if (memcmp (buf, tmp, size) != 0)
92 error (0, errno, "%s: failed comparison", msg);
93 return 1;
96 printf ("%s test ok\n", msg);
98 return 0;
102 static int
103 do_wait (struct aiocb64 **cbp, size_t nent, int allowed_err)
105 int go_on;
106 size_t cnt;
107 int result = 0;
111 aio_suspend64 ((const struct aiocb64 *const *) cbp, nent, NULL);
112 go_on = 0;
113 for (cnt = 0; cnt < nent; ++cnt)
114 if (cbp[cnt] != NULL)
116 if (aio_error64 (cbp[cnt]) == EINPROGRESS)
117 go_on = 1;
118 else
120 if (aio_return64 (cbp[cnt]) == -1
121 && (allowed_err == 0
122 || aio_error64 (cbp[cnt]) != allowed_err))
124 error (0, aio_error64 (cbp[cnt]), "Operation failed\n");
125 result = 1;
127 cbp[cnt] = NULL;
131 while (go_on);
133 return result;
138 do_test (int argc, char *argv[])
140 struct aiocb64 cbs[10];
141 struct aiocb64 cbs_fsync;
142 struct aiocb64 *cbp[10];
143 struct aiocb64 *cbp_fsync[1];
144 char buf[1000];
145 size_t cnt;
146 int result = 0;
148 /* Preparation. */
149 for (cnt = 0; cnt < 10; ++cnt)
151 cbs[cnt].aio_fildes = fd;
152 cbs[cnt].aio_reqprio = 0;
153 cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
154 cbs[cnt].aio_nbytes = 100;
155 cbs[cnt].aio_offset = cnt * 100;
156 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
158 cbp[cnt] = &cbs[cnt];
161 /* First a simple test. */
162 for (cnt = 10; cnt > 0; )
163 if (aio_write64 (cbp[--cnt]) < 0 && errno == ENOSYS)
165 error (0, 0, "no aio support in this configuration");
166 return 0;
168 /* Wait 'til the results are there. */
169 result |= do_wait (cbp, 10, 0);
170 /* Test this. */
171 result |= test_file (buf, sizeof (buf), fd, "aio_write");
173 /* Read now as we've written it. */
174 memset (buf, '\0', sizeof (buf));
175 /* Issue the commands. */
176 for (cnt = 10; cnt > 0; )
178 --cnt;
179 cbp[cnt] = &cbs[cnt];
180 aio_read64 (cbp[cnt]);
182 /* Wait 'til the results are there. */
183 result |= do_wait (cbp, 10, 0);
184 /* Test this. */
185 for (cnt = 0; cnt < 1000; ++cnt)
186 if (buf[cnt] != '0' + (cnt / 100))
188 result = 1;
189 error (0, 0, "comparison failed for aio_read test");
190 break;
193 if (cnt == 1000)
194 puts ("aio_read test ok");
196 /* Remove the test file contents. */
197 if (ftruncate64 (fd, 0) < 0)
199 error (0, errno, "ftruncate failed\n");
200 result = 1;
203 /* Test lio_listio. */
204 for (cnt = 0; cnt < 10; ++cnt)
206 cbs[cnt].aio_lio_opcode = LIO_WRITE;
207 cbp[cnt] = &cbs[cnt];
209 /* Issue the command. */
210 lio_listio64 (LIO_WAIT, cbp, 10, NULL);
211 /* ...and immediately test it since we started it in wait mode. */
212 result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
214 /* Test aio_fsync. */
215 cbs_fsync.aio_fildes = fd;
216 cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
217 cbp_fsync[0] = &cbs_fsync;
219 /* Remove the test file contents first. */
220 if (ftruncate64 (fd, 0) < 0)
222 error (0, errno, "ftruncate failed\n");
223 result = 1;
226 /* Write again. */
227 for (cnt = 10; cnt > 0; )
228 aio_write64 (cbp[--cnt]);
230 if (aio_fsync64 (O_SYNC, &cbs_fsync) < 0)
232 error (0, errno, "aio_fsync failed\n");
233 result = 1;
235 result |= do_wait (cbp_fsync, 1, 0);
237 /* ...and test since all data should be on disk now. */
238 result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
240 /* Test aio_cancel. */
241 /* Remove the test file contents first. */
242 if (ftruncate64 (fd, 0) < 0)
244 error (0, errno, "ftruncate failed\n");
245 result = 1;
248 /* Write again. */
249 for (cnt = 10; cnt > 0; )
250 aio_write64 (cbp[--cnt]);
252 /* Cancel all requests. */
253 if (aio_cancel64 (fd, NULL) == -1)
254 printf ("aio_cancel64 (fd, NULL) cannot cancel anything\n");
256 result |= do_wait (cbp, 10, ECANCELED);
258 /* Another test for aio_cancel. */
259 /* Remove the test file contents first. */
260 if (ftruncate64 (fd, 0) < 0)
262 error (0, errno, "ftruncate failed\n");
263 result = 1;
266 /* Write again. */
267 for (cnt = 10; cnt > 0; )
269 --cnt;
270 cbp[cnt] = &cbs[cnt];
271 aio_write64 (cbp[cnt]);
273 puts ("finished3");
275 /* Cancel all requests. */
276 for (cnt = 10; cnt > 0; )
277 if (aio_cancel64 (fd, cbp[--cnt]) == -1)
278 /* This is not an error. The request can simply be finished. */
279 printf ("aio_cancel64 (fd, cbp[%Zd]) cannot be canceled\n", cnt);
280 puts ("finished2");
282 result |= do_wait (cbp, 10, ECANCELED);
284 puts ("finished");
286 return result;