1 /* Tests for AIO in librt.
2 Copyright (C) 1998-2016 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 <http://www.gnu.org/licenses/>. */
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 /* We might need a bit longer timeout. */
38 #define TIMEOUT 20 /* sec */
40 /* This defines the `main' function and some more. */
41 #include <test-skeleton.c>
44 /* These are for the temporary file we generate. */
49 do_prepare (int argc
, char *argv
[])
53 name_len
= strlen (test_dir
);
54 name
= xmalloc (name_len
+ sizeof ("/aioXXXXXX"));
55 mempcpy (mempcpy (name
, test_dir
, name_len
),
56 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
58 /* Open our test file. */
61 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
67 test_file (const void *buf
, size_t size
, int fd
, const char *msg
)
73 if (fstat (fd
, &st
) < 0)
75 error (0, errno
, "%s: failed stat", msg
);
79 if (st
.st_size
!= (off_t
) size
)
81 error (0, errno
, "%s: wrong size: %lu, should be %lu",
82 msg
, (unsigned long int) st
.st_size
, (unsigned long int) size
);
86 if (pread (fd
, tmp
, size
, 0) != (ssize_t
) size
)
88 error (0, errno
, "%s: failed pread", msg
);
92 if (memcmp (buf
, tmp
, size
) != 0)
94 error (0, errno
, "%s: failed comparison", msg
);
98 printf ("%s test ok\n", msg
);
105 do_wait (struct aiocb
**cbp
, size_t nent
, int allowed_err
)
113 aio_suspend ((const struct aiocb
*const *) cbp
, nent
, NULL
);
115 for (cnt
= 0; cnt
< nent
; ++cnt
)
116 if (cbp
[cnt
] != NULL
)
118 if (aio_error (cbp
[cnt
]) == EINPROGRESS
)
122 if (aio_return (cbp
[cnt
]) == -1
124 || aio_error (cbp
[cnt
]) != allowed_err
))
126 error (0, aio_error (cbp
[cnt
]), "Operation failed\n");
140 do_test (int argc
, char *argv
[])
142 struct aiocb cbs
[10];
143 struct aiocb cbs_fsync
;
144 struct aiocb
*cbp
[10];
145 struct aiocb
*cbp_fsync
[1];
151 for (cnt
= 0; cnt
< 10; ++cnt
)
153 cbs
[cnt
].aio_fildes
= fd
;
154 cbs
[cnt
].aio_reqprio
= 0;
155 cbs
[cnt
].aio_buf
= memset (&buf
[cnt
* 100], '0' + cnt
, 100);
156 cbs
[cnt
].aio_nbytes
= 100;
157 cbs
[cnt
].aio_offset
= cnt
* 100;
158 cbs
[cnt
].aio_sigevent
.sigev_notify
= SIGEV_NONE
;
160 cbp
[cnt
] = &cbs
[cnt
];
163 /* First a simple test. */
164 for (cnt
= 10; cnt
> 0; )
165 if (aio_write (cbp
[--cnt
]) < 0 && errno
== ENOSYS
)
167 error (0, 0, "no aio support in this configuration");
170 /* Wait 'til the results are there. */
171 result
|= do_wait (cbp
, 10, 0);
173 result
|= test_file (buf
, sizeof (buf
), fd
, "aio_write");
175 /* Read now as we've written it. */
176 memset (buf
, '\0', sizeof (buf
));
177 /* Issue the commands. */
178 for (cnt
= 10; cnt
> 0; )
181 cbp
[cnt
] = &cbs
[cnt
];
184 /* Wait 'til the results are there. */
185 result
|= do_wait (cbp
, 10, 0);
187 for (cnt
= 0; cnt
< 1000; ++cnt
)
188 if (buf
[cnt
] != '0' + (cnt
/ 100))
191 error (0, 0, "comparison failed for aio_read test");
196 puts ("aio_read test ok");
198 /* Remove the test file contents. */
199 if (ftruncate (fd
, 0) < 0)
201 error (0, errno
, "ftruncate failed\n");
205 /* Test lio_listio. */
206 for (cnt
= 0; cnt
< 10; ++cnt
)
208 cbs
[cnt
].aio_lio_opcode
= LIO_WRITE
;
209 cbp
[cnt
] = &cbs
[cnt
];
211 /* Issue the command. */
212 lio_listio (LIO_WAIT
, cbp
, 10, NULL
);
213 /* ...and immediately test it since we started it in wait mode. */
214 result
|= test_file (buf
, sizeof (buf
), fd
, "lio_listio (write)");
216 /* Test aio_fsync. */
217 cbs_fsync
.aio_fildes
= fd
;
218 cbs_fsync
.aio_sigevent
.sigev_notify
= SIGEV_NONE
;
219 cbp_fsync
[0] = &cbs_fsync
;
221 /* Remove the test file contents first. */
222 if (ftruncate (fd
, 0) < 0)
224 error (0, errno
, "ftruncate failed\n");
229 for (cnt
= 10; cnt
> 0; )
230 aio_write (cbp
[--cnt
]);
232 if (aio_fsync (O_SYNC
, &cbs_fsync
) < 0)
234 error (0, errno
, "aio_fsync failed\n");
237 result
|= do_wait (cbp_fsync
, 1, 0);
239 /* ...and test since all data should be on disk now. */
240 result
|= test_file (buf
, sizeof (buf
), fd
, "aio_fsync (aio_write)");
242 /* Test aio_cancel. */
243 /* Remove the test file contents first. */
244 if (ftruncate (fd
, 0) < 0)
246 error (0, errno
, "ftruncate failed\n");
251 for (cnt
= 10; cnt
> 0; )
252 aio_write (cbp
[--cnt
]);
254 /* Cancel all requests. */
255 if (aio_cancel (fd
, NULL
) == -1)
256 printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
258 result
|= do_wait (cbp
, 10, ECANCELED
);
260 /* Another test for aio_cancel. */
261 /* Remove the test file contents first. */
262 if (ftruncate (fd
, 0) < 0)
264 error (0, errno
, "ftruncate failed\n");
269 for (cnt
= 10; cnt
> 0; )
272 cbp
[cnt
] = &cbs
[cnt
];
273 aio_write (cbp
[cnt
]);
277 /* Cancel all requests. */
278 for (cnt
= 10; cnt
> 0; )
279 if (aio_cancel (fd
, cbp
[--cnt
]) == -1)
280 /* This is not an error. The request can simply be finished. */
281 printf ("aio_cancel (fd, cbp[%Zd]) cannot be canceled\n", cnt
);
284 result
|= do_wait (cbp
, 10, ECANCELED
);