1 /* Tests for 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/>. */
29 /* Prototype for our test function. */
30 extern void do_prepare (int argc, char *argv[]);
31 extern int do_test (int argc, char *argv[]);
33 /* We have a preparation function. */
34 #define PREPARE do_prepare
36 /* This defines the `main' function and some more. */
37 #include <test-skeleton.c>
40 /* These are for the temporary file we generate. */
45 do_prepare (int argc, char *argv[])
49 name_len = strlen (test_dir);
50 name = xmalloc (name_len + sizeof ("/aioXXXXXX"));
51 mempcpy (mempcpy (name, test_dir, name_len),
52 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
54 /* Open our test file. */
57 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
63 test_file (const void *buf, size_t size, int fd, const char *msg)
69 if (fstat (fd, &st) < 0)
71 error (0, errno, "%s: failed stat", msg);
75 if (st.st_size != (off_t) size)
77 error (0, errno, "%s: wrong size: %lu, should be %lu",
78 msg, (unsigned long int) st.st_size, (unsigned long int) size);
82 if (pread (fd, tmp, size, 0) != (ssize_t) size)
84 error (0, errno, "%s: failed pread", msg);
88 if (memcmp (buf, tmp, size) != 0)
90 error (0, errno, "%s: failed comparison", msg);
94 printf ("%s test ok\n", msg);
101 do_wait (struct aiocb **cbp, size_t nent, int allowed_err)
109 aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
111 for (cnt = 0; cnt < nent; ++cnt)
112 if (cbp[cnt] != NULL)
114 if (aio_error (cbp[cnt]) == EINPROGRESS)
118 if (aio_return (cbp[cnt]) == -1
120 || aio_error (cbp[cnt]) != allowed_err))
122 error (0, aio_error (cbp[cnt]), "Operation failed\n");
136 do_test (int argc, char *argv[])
138 struct aiocb cbs[10];
139 struct aiocb cbs_fsync;
140 struct aiocb *cbp[10];
141 struct aiocb *cbp_fsync[1];
147 for (cnt = 0; cnt < 10; ++cnt)
149 cbs[cnt].aio_fildes = fd;
150 cbs[cnt].aio_reqprio = 0;
151 cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
152 cbs[cnt].aio_nbytes = 100;
153 cbs[cnt].aio_offset = cnt * 100;
154 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
156 cbp[cnt] = &cbs[cnt];
159 /* First a simple test. */
160 for (cnt = 10; cnt > 0; )
161 if (aio_write (cbp[--cnt]) < 0 && errno == ENOSYS)
163 error (0, 0, "no aio support in this configuration");
166 /* Wait 'til the results are there. */
167 result |= do_wait (cbp, 10, 0);
169 result |= test_file (buf, sizeof (buf), fd, "aio_write");
171 /* Read now as we've written it. */
172 memset (buf, '\0', sizeof (buf));
173 /* Issue the commands. */
174 for (cnt = 10; cnt > 0; )
177 cbp[cnt] = &cbs[cnt];
180 /* Wait 'til the results are there. */
181 result |= do_wait (cbp, 10, 0);
183 for (cnt = 0; cnt < 1000; ++cnt)
184 if (buf[cnt] != '0' + (cnt / 100))
187 error (0, 0, "comparison failed for aio_read test");
192 puts ("aio_read test ok");
194 /* Remove the test file contents. */
195 if (ftruncate (fd, 0) < 0)
197 error (0, errno, "ftruncate failed\n");
201 /* Test lio_listio. */
202 for (cnt = 0; cnt < 10; ++cnt)
204 cbs[cnt].aio_lio_opcode = LIO_WRITE;
205 cbp[cnt] = &cbs[cnt];
207 /* Issue the command. */
208 lio_listio (LIO_WAIT, cbp, 10, NULL);
209 /* ...and immediately test it since we started it in wait mode. */
210 result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
212 /* Test aio_fsync. */
213 cbs_fsync.aio_fildes = fd;
214 cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
215 cbp_fsync[0] = &cbs_fsync;
217 /* Remove the test file contents first. */
218 if (ftruncate (fd, 0) < 0)
220 error (0, errno, "ftruncate failed\n");
225 for (cnt = 10; cnt > 0; )
226 aio_write (cbp[--cnt]);
228 if (aio_fsync (O_SYNC, &cbs_fsync) < 0)
230 error (0, errno, "aio_fsync failed\n");
233 result |= do_wait (cbp_fsync, 1, 0);
235 /* ...and test since all data should be on disk now. */
236 result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
238 /* Test aio_cancel. */
239 /* Remove the test file contents first. */
240 if (ftruncate (fd, 0) < 0)
242 error (0, errno, "ftruncate failed\n");
247 for (cnt = 10; cnt > 0; )
248 aio_write (cbp[--cnt]);
250 /* Cancel all requests. */
251 if (aio_cancel (fd, NULL) == -1)
252 printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
254 result |= do_wait (cbp, 10, ECANCELED);
256 /* Another test for aio_cancel. */
257 /* Remove the test file contents first. */
258 if (ftruncate (fd, 0) < 0)
260 error (0, errno, "ftruncate failed\n");
265 for (cnt = 10; cnt > 0; )
268 cbp[cnt] = &cbs[cnt];
269 aio_write (cbp[cnt]);
273 /* Cancel all requests. */
274 for (cnt = 10; cnt > 0; )
275 if (aio_cancel (fd, cbp[--cnt]) == -1)
276 /* This is not an error. The request can simply be finished. */
277 printf ("aio_cancel (fd, cbp[%zd]) cannot be canceled\n", cnt);
280 result |= do_wait (cbp, 10, ECANCELED);