Allow SO* symbols.
[glibc.git] / rt / tst-aio.c
blobd425ff14eff76442e3e6b4b25a72744cd516f48d
1 /* Tests for AIO in librt.
2 Copyright (C) 1998, 2000 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <aio.h>
21 #include <errno.h>
22 #include <error.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
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 /* We might need a bit longer timeout. */
37 #define TIMEOUT 20 /* sec */
39 /* This defines the `main' function and some more. */
40 #include <test-skeleton.c>
43 /* These are for the temporary file we generate. */
44 char *name;
45 int fd;
47 void
48 do_prepare (int argc, char *argv[])
50 char name_len;
52 name_len = strlen (test_dir);
53 name = malloc (name_len + sizeof ("/aioXXXXXX"));
54 mempcpy (mempcpy (name, test_dir, name_len),
55 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
56 add_temp_file (name);
58 /* Open our test file. */
59 fd = mkstemp (name);
60 if (fd == -1)
61 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
65 static int
66 test_file (const void *buf, size_t size, int fd, const char *msg)
68 struct stat st;
69 char tmp[size];
71 errno = 0;
72 if (fstat (fd, &st) < 0)
74 error (0, errno, "%s: failed stat", msg);
75 return 1;
78 if (st.st_size != size)
80 error (0, errno, "%s: wrong size: %lu, should be %lu",
81 msg, (unsigned long int) st.st_size, (unsigned long int) size);
82 return 1;
85 if (pread (fd, tmp, size, 0) != size)
87 error (0, errno, "%s: failed pread", msg);
88 return 1;
91 if (memcmp (buf, tmp, size) != 0)
93 error (0, errno, "%s: failed comparison", msg);
94 return 1;
97 printf ("%s test ok\n", msg);
99 return 0;
103 static int
104 do_wait (struct aiocb **cbp, size_t nent, int allowed_err)
106 int go_on;
107 size_t cnt;
108 int result = 0;
112 aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
113 go_on = 0;
114 for (cnt = 0; cnt < nent; ++cnt)
115 if (cbp[cnt] != NULL)
117 if (aio_error (cbp[cnt]) == EINPROGRESS)
118 go_on = 1;
119 else
121 if (aio_return (cbp[cnt]) == -1
122 && (allowed_err == 0
123 || aio_error (cbp[cnt]) != allowed_err))
125 error (0, aio_error (cbp[cnt]), "Operation failed\n");
126 result = 1;
128 cbp[cnt] = NULL;
132 while (go_on);
134 return result;
139 do_test (int argc, char *argv[])
141 struct aiocb cbs[10];
142 struct aiocb cbs_fsync;
143 struct aiocb *cbp[10];
144 struct aiocb *cbp_fsync[1];
145 char buf[1000];
146 size_t cnt;
147 int result = 0;
149 /* Preparation. */
150 for (cnt = 0; cnt < 10; ++cnt)
152 cbs[cnt].aio_fildes = fd;
153 cbs[cnt].aio_reqprio = 0;
154 cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
155 cbs[cnt].aio_nbytes = 100;
156 cbs[cnt].aio_offset = cnt * 100;
157 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
159 cbp[cnt] = &cbs[cnt];
162 /* First a simple test. */
163 for (cnt = 10; cnt > 0; )
164 aio_write (cbp[--cnt]);
165 /* Wait 'til the results are there. */
166 result |= do_wait (cbp, 10, 0);
167 /* Test this. */
168 result |= test_file (buf, sizeof (buf), fd, "aio_write");
170 /* Read now as we've written it. */
171 memset (buf, '\0', sizeof (buf));
172 /* Issue the commands. */
173 for (cnt = 10; cnt > 0; )
175 --cnt;
176 cbp[cnt] = &cbs[cnt];
177 aio_read (cbp[cnt]);
179 /* Wait 'til the results are there. */
180 result |= do_wait (cbp, 10, 0);
181 /* Test this. */
182 for (cnt = 0; cnt < 1000; ++cnt)
183 if (buf[cnt] != '0' + (cnt / 100))
185 result = 1;
186 error (0, 0, "comparison failed for aio_read test");
187 break;
190 if (cnt == 1000)
191 puts ("aio_read test ok");
193 /* Remove the test file contents. */
194 if (ftruncate (fd, 0) < 0)
196 error (0, errno, "ftruncate failed\n");
197 result = 1;
200 /* Test lio_listio. */
201 for (cnt = 0; cnt < 10; ++cnt)
203 cbs[cnt].aio_lio_opcode = LIO_WRITE;
204 cbp[cnt] = &cbs[cnt];
206 /* Issue the command. */
207 lio_listio (LIO_WAIT, cbp, 10, NULL);
208 /* ...and immediately test it since we started it in wait mode. */
209 result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
211 /* Test aio_fsync. */
212 cbs_fsync.aio_fildes = fd;
213 cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
214 cbp_fsync[0] = &cbs_fsync;
216 /* Remove the test file contents first. */
217 if (ftruncate (fd, 0) < 0)
219 error (0, errno, "ftruncate failed\n");
220 result = 1;
223 /* Write again. */
224 for (cnt = 10; cnt > 0; )
225 aio_write (cbp[--cnt]);
227 if (aio_fsync (O_SYNC, &cbs_fsync) < 0)
229 error (0, errno, "aio_fsync failed\n");
230 result = 1;
232 result |= do_wait (cbp_fsync, 1, 0);
234 /* ...and test since all data should be on disk now. */
235 result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
237 /* Test aio_cancel. */
238 /* Remove the test file contents first. */
239 if (ftruncate (fd, 0) < 0)
241 error (0, errno, "ftruncate failed\n");
242 result = 1;
245 /* Write again. */
246 for (cnt = 10; cnt > 0; )
247 aio_write (cbp[--cnt]);
249 /* Cancel all requests. */
250 if (aio_cancel (fd, NULL) == -1)
251 printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
253 result |= do_wait (cbp, 10, ECANCELED);
255 /* Another test for aio_cancel. */
256 /* Remove the test file contents first. */
257 if (ftruncate (fd, 0) < 0)
259 error (0, errno, "ftruncate failed\n");
260 result = 1;
263 /* Write again. */
264 for (cnt = 10; cnt > 0; )
266 --cnt;
267 cbp[cnt] = &cbs[cnt];
268 aio_write (cbp[cnt]);
270 puts ("finished3");
272 /* Cancel all requests. */
273 for (cnt = 10; cnt > 0; )
274 if (aio_cancel (fd, cbp[--cnt]) == -1)
275 /* This is not an error. The request can simply be finished. */
276 printf ("aio_cancel (fd, cbp[%Zd]) cannot be canceled\n", cnt);
277 puts ("finished2");
279 result |= do_wait (cbp, 10, ECANCELED);
281 puts ("finished");
283 return result;