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
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. */
47 do_prepare (int argc
, char *argv
[])
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. */
59 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
65 test_file (const void *buf
, size_t size
, int fd
, const char *msg
)
71 if (fstat (fd
, &st
) < 0)
73 error (0, errno
, "%s: failed stat", msg
);
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
);
84 if (pread (fd
, tmp
, size
, 0) != (ssize_t
) size
)
86 error (0, errno
, "%s: failed pread", msg
);
90 if (memcmp (buf
, tmp
, size
) != 0)
92 error (0, errno
, "%s: failed comparison", msg
);
96 printf ("%s test ok\n", msg
);
103 do_wait (struct aiocb64
**cbp
, size_t nent
, int allowed_err
)
111 aio_suspend64 ((const struct aiocb64
*const *) cbp
, nent
, NULL
);
113 for (cnt
= 0; cnt
< nent
; ++cnt
)
114 if (cbp
[cnt
] != NULL
)
116 if (aio_error64 (cbp
[cnt
]) == EINPROGRESS
)
120 if (aio_return64 (cbp
[cnt
]) == -1
122 || aio_error64 (cbp
[cnt
]) != allowed_err
))
124 error (0, aio_error64 (cbp
[cnt
]), "Operation failed\n");
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];
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");
168 /* Wait 'til the results are there. */
169 result
|= do_wait (cbp
, 10, 0);
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; )
179 cbp
[cnt
] = &cbs
[cnt
];
180 aio_read64 (cbp
[cnt
]);
182 /* Wait 'til the results are there. */
183 result
|= do_wait (cbp
, 10, 0);
185 for (cnt
= 0; cnt
< 1000; ++cnt
)
186 if (buf
[cnt
] != '0' + (cnt
/ 100))
189 error (0, 0, "comparison failed for aio_read test");
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");
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");
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");
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");
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");
267 for (cnt
= 10; cnt
> 0; )
270 cbp
[cnt
] = &cbs
[cnt
];
271 aio_write64 (cbp
[cnt
]);
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
);
282 result
|= do_wait (cbp
, 10, ECANCELED
);