Updated to fedora-glibc-20071212T1051
[glibc.git] / rtkaio / tst-aiod.c
bloba8b58106dca6a179ece92b63a28992ec076be3e5
1 /* Tests for AIO in librt.
2 Copyright (C) 1998, 2000, 2002, 2006 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <aio.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include "tst-aiod.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 /* We might need a bit longer timeout. */
39 #define TIMEOUT 20 /* sec */
41 /* This defines the `main' function and some more. */
42 #include <test-skeleton.c>
45 /* These are for the temporary file we generate. */
46 char *name;
47 int fd;
48 char *tmpbuf;
49 int blksz = 100;
51 void
52 do_prepare (int argc, char *argv[])
54 size_t name_len;
56 name_len = strlen (test_dir);
57 name = malloc (name_len + sizeof ("/aioXXXXXX"));
58 mempcpy (mempcpy (name, test_dir, name_len),
59 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
60 add_temp_file (name);
62 /* Open our test file. */
63 fd = mkstemp (name);
64 if (fd == -1)
65 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
67 int sz = set_o_direct (fd);
68 if (sz != -1)
70 blksz = sz;
71 printf ("Using O_DIRECT with block size %d\n", blksz);
76 static int
77 test_file (const void *buf, size_t size, int fd, const char *msg)
79 struct stat st;
80 char *tmp = tmpbuf;
82 errno = 0;
83 if (fstat (fd, &st) < 0)
85 error (0, errno, "%s: failed stat", msg);
86 return 1;
89 if (st.st_size != (off_t) size)
91 error (0, errno, "%s: wrong size: %lu, should be %lu",
92 msg, (unsigned long int) st.st_size, (unsigned long int) size);
93 return 1;
96 if (pread (fd, tmp, size, 0) != (ssize_t) size)
98 error (0, errno, "%s: failed pread", msg);
99 return 1;
102 if (memcmp (buf, tmp, size) != 0)
104 error (0, errno, "%s: failed comparison", msg);
105 return 1;
108 printf ("%s test ok\n", msg);
110 return 0;
114 static int
115 do_wait (struct aiocb **cbp, size_t nent, int allowed_err)
117 int go_on;
118 size_t cnt;
119 int result = 0;
123 aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
124 go_on = 0;
125 for (cnt = 0; cnt < nent; ++cnt)
126 if (cbp[cnt] != NULL)
128 if (aio_error (cbp[cnt]) == EINPROGRESS)
129 go_on = 1;
130 else
132 if (aio_return (cbp[cnt]) == -1
133 && (allowed_err == 0
134 || aio_error (cbp[cnt]) != allowed_err))
136 error (0, aio_error (cbp[cnt]), "Operation failed\n");
137 result = 1;
139 cbp[cnt] = NULL;
143 while (go_on);
145 return result;
150 do_test (int argc, char *argv[])
152 struct aiocb cbs[10];
153 struct aiocb cbs_fsync;
154 struct aiocb *cbp[10];
155 struct aiocb *cbp_fsync[1];
156 char *buf;
157 size_t cnt;
158 int result = 0;
160 buf = mmap (NULL, 20 * blksz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
161 tmpbuf = buf + 10 * blksz;
162 if (buf == MAP_FAILED)
164 error (0, errno, "mmap failed");
165 return 1;
168 /* Preparation. */
169 for (cnt = 0; cnt < 10; ++cnt)
171 cbs[cnt].aio_fildes = fd;
172 cbs[cnt].aio_reqprio = 0;
173 cbs[cnt].aio_buf = memset (&buf[cnt * blksz], '0' + cnt, blksz);
174 cbs[cnt].aio_nbytes = blksz;
175 cbs[cnt].aio_offset = cnt * blksz;
176 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
178 cbp[cnt] = &cbs[cnt];
181 /* First a simple test. */
182 for (cnt = 10; cnt > 0; )
183 if (aio_write (cbp[--cnt]) < 0 && errno == ENOSYS)
185 error (0, 0, "no aio support in this configuration");
186 return 0;
188 /* Wait 'til the results are there. */
189 result |= do_wait (cbp, 10, 0);
190 /* Test this. */
191 result |= test_file (buf, 10 * blksz, fd, "aio_write");
193 /* Read now as we've written it. */
194 memset (buf, '\0', 10 * blksz);
195 /* Issue the commands. */
196 for (cnt = 10; cnt > 0; )
198 --cnt;
199 cbp[cnt] = &cbs[cnt];
200 aio_read (cbp[cnt]);
202 /* Wait 'til the results are there. */
203 result |= do_wait (cbp, 10, 0);
204 /* Test this. */
205 for (cnt = 0; cnt < 10 * blksz; ++cnt)
206 if (buf[cnt] != '0' + (cnt / blksz))
208 result = 1;
209 error (0, 0, "comparison failed for aio_read test");
210 break;
213 if (cnt == 10 * blksz)
214 puts ("aio_read test ok");
216 /* Remove the test file contents. */
217 if (ftruncate (fd, 0) < 0)
219 error (0, errno, "ftruncate failed\n");
220 result = 1;
223 /* Test lio_listio. */
224 for (cnt = 0; cnt < 10; ++cnt)
226 cbs[cnt].aio_lio_opcode = LIO_WRITE;
227 cbp[cnt] = &cbs[cnt];
229 /* Issue the command. */
230 lio_listio (LIO_WAIT, cbp, 10, NULL);
231 /* ...and immediately test it since we started it in wait mode. */
232 result |= test_file (buf, 10 * blksz, fd, "lio_listio (write)");
234 /* Test aio_fsync. */
235 cbs_fsync.aio_fildes = fd;
236 cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
237 cbp_fsync[0] = &cbs_fsync;
239 /* Remove the test file contents first. */
240 if (ftruncate (fd, 0) < 0)
242 error (0, errno, "ftruncate failed\n");
243 result = 1;
246 /* Write again. */
247 for (cnt = 10; cnt > 0; )
248 aio_write (cbp[--cnt]);
250 if (aio_fsync (O_SYNC, &cbs_fsync) < 0)
252 error (0, errno, "aio_fsync failed\n");
253 result = 1;
255 result |= do_wait (cbp_fsync, 1, 0);
257 /* ...and test since all data should be on disk now. */
258 result |= test_file (buf, 10 * blksz, fd, "aio_fsync (aio_write)");
260 /* Test aio_cancel. */
261 /* Remove the test file contents first. */
262 if (ftruncate (fd, 0) < 0)
264 error (0, errno, "ftruncate failed\n");
265 result = 1;
268 /* Write again. */
269 for (cnt = 10; cnt > 0; )
270 aio_write (cbp[--cnt]);
272 /* Cancel all requests. */
273 if (aio_cancel (fd, NULL) == -1)
274 printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
276 result |= do_wait (cbp, 10, ECANCELED);
278 /* Another test for aio_cancel. */
279 /* Remove the test file contents first. */
280 if (ftruncate (fd, 0) < 0)
282 error (0, errno, "ftruncate failed\n");
283 result = 1;
286 /* Write again. */
287 for (cnt = 10; cnt > 0; )
289 --cnt;
290 cbp[cnt] = &cbs[cnt];
291 aio_write (cbp[cnt]);
293 puts ("finished3");
295 /* Cancel all requests. */
296 for (cnt = 10; cnt > 0; )
297 if (aio_cancel (fd, cbp[--cnt]) == -1)
298 /* This is not an error. The request can simply be finished. */
299 printf ("aio_cancel (fd, cbp[%Zd]) cannot be canceled\n", cnt);
300 puts ("finished2");
302 result |= do_wait (cbp, 10, ECANCELED);
304 puts ("finished");
306 return result;