2.5-18.1
[glibc.git] / rtkaio / tst-aiod64.c
blob3b8f014421f14b606d416856b031beaffe8621ec
1 /* Tests for 64bit AIO in librt.
2 Copyright (C) 1998, 1999, 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 #define _LARGEFILE_SOURCE 1
22 #include <aio.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include "tst-aiod.h"
32 /* Prototype for our test function. */
33 extern void do_prepare (int argc, char *argv[]);
34 extern int do_test (int argc, char *argv[]);
36 /* We have a preparation function. */
37 #define PREPARE do_prepare
39 /* We might need a bit longer timeout. */
40 #define TIMEOUT 20 /* sec */
42 /* This defines the `main' function and some more. */
43 #include <test-skeleton.c>
46 /* These are for the temporary file we generate. */
47 char *name;
48 int fd;
49 char *tmpbuf;
50 int blksz = 100;
52 void
53 do_prepare (int argc, char *argv[])
55 size_t name_len;
57 name_len = strlen (test_dir);
58 name = malloc (name_len + sizeof ("/aioXXXXXX"));
59 mempcpy (mempcpy (name, test_dir, name_len),
60 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
61 add_temp_file (name);
63 /* Open our test file. */
64 fd = mkstemp (name);
65 if (fd == -1)
66 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
68 int sz = set_o_direct (fd);
69 if (sz != -1)
71 blksz = sz;
72 printf ("Using O_DIRECT with block size %d\n", blksz);
77 static int
78 test_file (const void *buf, size_t size, int fd, const char *msg)
80 struct stat st;
81 char *tmp = tmpbuf;
83 errno = 0;
84 if (fstat (fd, &st) < 0)
86 error (0, errno, "%s: failed stat", msg);
87 return 1;
90 if (st.st_size != (off_t) size)
92 error (0, errno, "%s: wrong size: %lu, should be %lu",
93 msg, (unsigned long int) st.st_size, (unsigned long int) size);
94 return 1;
97 if (pread (fd, tmp, size, 0) != (ssize_t) size)
99 error (0, errno, "%s: failed pread", msg);
100 return 1;
103 if (memcmp (buf, tmp, size) != 0)
105 error (0, errno, "%s: failed comparison", msg);
106 return 1;
109 printf ("%s test ok\n", msg);
111 return 0;
115 static int
116 do_wait (struct aiocb64 **cbp, size_t nent, int allowed_err)
118 int go_on;
119 size_t cnt;
120 int result = 0;
124 aio_suspend64 ((const struct aiocb64 *const *) cbp, nent, NULL);
125 go_on = 0;
126 for (cnt = 0; cnt < nent; ++cnt)
127 if (cbp[cnt] != NULL)
129 if (aio_error64 (cbp[cnt]) == EINPROGRESS)
130 go_on = 1;
131 else
133 if (aio_return64 (cbp[cnt]) == -1
134 && (allowed_err == 0
135 || aio_error64 (cbp[cnt]) != allowed_err))
137 error (0, aio_error64 (cbp[cnt]), "Operation failed\n");
138 result = 1;
140 cbp[cnt] = NULL;
144 while (go_on);
146 return result;
151 do_test (int argc, char *argv[])
153 struct aiocb64 cbs[10];
154 struct aiocb64 cbs_fsync;
155 struct aiocb64 *cbp[10];
156 struct aiocb64 *cbp_fsync[1];
157 char *buf;
158 size_t cnt;
159 int result = 0;
161 buf = mmap (NULL, 20 * blksz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
162 tmpbuf = buf + 10 * blksz;
163 if (buf == MAP_FAILED)
165 error (0, errno, "mmap failed");
166 return 1;
169 /* Preparation. */
170 for (cnt = 0; cnt < 10; ++cnt)
172 cbs[cnt].aio_fildes = fd;
173 cbs[cnt].aio_reqprio = 0;
174 cbs[cnt].aio_buf = memset (&buf[cnt * blksz], '0' + cnt, blksz);
175 cbs[cnt].aio_nbytes = blksz;
176 cbs[cnt].aio_offset = cnt * blksz;
177 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
179 cbp[cnt] = &cbs[cnt];
182 /* First a simple test. */
183 for (cnt = 10; cnt > 0; )
184 if (aio_write64 (cbp[--cnt]) < 0 && errno == ENOSYS)
186 error (0, 0, "no aio support in this configuration");
187 return 0;
189 /* Wait 'til the results are there. */
190 result |= do_wait (cbp, 10, 0);
191 /* Test this. */
192 result |= test_file (buf, 10 * blksz, fd, "aio_write");
194 /* Read now as we've written it. */
195 memset (buf, '\0', 10 * blksz);
196 /* Issue the commands. */
197 for (cnt = 10; cnt > 0; )
199 --cnt;
200 cbp[cnt] = &cbs[cnt];
201 aio_read64 (cbp[cnt]);
203 /* Wait 'til the results are there. */
204 result |= do_wait (cbp, 10, 0);
205 /* Test this. */
206 for (cnt = 0; cnt < 10 * blksz; ++cnt)
207 if (buf[cnt] != '0' + (cnt / blksz))
209 result = 1;
210 error (0, 0, "comparison failed for aio_read test");
211 break;
214 if (cnt == 10 * blksz)
215 puts ("aio_read test ok");
217 /* Remove the test file contents. */
218 if (ftruncate64 (fd, 0) < 0)
220 error (0, errno, "ftruncate failed\n");
221 result = 1;
224 /* Test lio_listio. */
225 for (cnt = 0; cnt < 10; ++cnt)
227 cbs[cnt].aio_lio_opcode = LIO_WRITE;
228 cbp[cnt] = &cbs[cnt];
230 /* Issue the command. */
231 lio_listio64 (LIO_WAIT, cbp, 10, NULL);
232 /* ...and immediately test it since we started it in wait mode. */
233 result |= test_file (buf, 10 * blksz, fd, "lio_listio (write)");
235 /* Test aio_fsync. */
236 cbs_fsync.aio_fildes = fd;
237 cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
238 cbp_fsync[0] = &cbs_fsync;
240 /* Remove the test file contents first. */
241 if (ftruncate64 (fd, 0) < 0)
243 error (0, errno, "ftruncate failed\n");
244 result = 1;
247 /* Write again. */
248 for (cnt = 10; cnt > 0; )
249 aio_write64 (cbp[--cnt]);
251 if (aio_fsync64 (O_SYNC, &cbs_fsync) < 0)
253 error (0, errno, "aio_fsync failed\n");
254 result = 1;
256 result |= do_wait (cbp_fsync, 1, 0);
258 /* ...and test since all data should be on disk now. */
259 result |= test_file (buf, 10 * blksz, fd, "aio_fsync (aio_write)");
261 /* Test aio_cancel. */
262 /* Remove the test file contents first. */
263 if (ftruncate64 (fd, 0) < 0)
265 error (0, errno, "ftruncate failed\n");
266 result = 1;
269 /* Write again. */
270 for (cnt = 10; cnt > 0; )
271 aio_write64 (cbp[--cnt]);
273 /* Cancel all requests. */
274 if (aio_cancel64 (fd, NULL) == -1)
275 printf ("aio_cancel64 (fd, NULL) cannot cancel anything\n");
277 result |= do_wait (cbp, 10, ECANCELED);
279 /* Another test for aio_cancel. */
280 /* Remove the test file contents first. */
281 if (ftruncate64 (fd, 0) < 0)
283 error (0, errno, "ftruncate failed\n");
284 result = 1;
287 /* Write again. */
288 for (cnt = 10; cnt > 0; )
290 --cnt;
291 cbp[cnt] = &cbs[cnt];
292 aio_write64 (cbp[cnt]);
294 puts ("finished3");
296 /* Cancel all requests. */
297 for (cnt = 10; cnt > 0; )
298 if (aio_cancel64 (fd, cbp[--cnt]) == -1)
299 /* This is not an error. The request can simply be finished. */
300 printf ("aio_cancel64 (fd, cbp[%Zd]) cannot be canceled\n", cnt);
301 puts ("finished2");
303 result |= do_wait (cbp, 10, ECANCELED);
305 puts ("finished");
307 return result;