]> git.ipfire.org Git - thirdparty/glibc.git/blame - rt/tst-aio.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / rt / tst-aio.c
CommitLineData
b9337b6a 1/* Tests for AIO in librt.
f7a9f785 2 Copyright (C) 1998-2016 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
b9337b6a
UD
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
b9337b6a
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
b9337b6a 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
b9337b6a
UD
19
20#include <aio.h>
21#include <errno.h>
22#include <error.h>
84ad622b 23#include <fcntl.h>
b9337b6a
UD
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/stat.h>
28
29
310b3460
UD
30/* Prototype for our test function. */
31extern void do_prepare (int argc, char *argv[]);
b9337b6a
UD
32extern int do_test (int argc, char *argv[]);
33
310b3460
UD
34/* We have a preparation function. */
35#define PREPARE do_prepare
b9337b6a
UD
36
37/* We might need a bit longer timeout. */
38#define TIMEOUT 20 /* sec */
39
40/* This defines the `main' function and some more. */
41#include <test-skeleton.c>
42
43
310b3460
UD
44/* These are for the temporary file we generate. */
45char *name;
46int fd;
47
48void
49do_prepare (int argc, char *argv[])
50{
30aa5785 51 size_t name_len;
310b3460
UD
52
53 name_len = strlen (test_dir);
54 name = malloc (name_len + sizeof ("/aioXXXXXX"));
55 mempcpy (mempcpy (name, test_dir, name_len),
56 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
310b3460
UD
57
58 /* Open our test file. */
59 fd = mkstemp (name);
60 if (fd == -1)
61 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
cf145565 62 add_temp_file (name);
310b3460
UD
63}
64
65
f71bc3ba 66static int
b9337b6a
UD
67test_file (const void *buf, size_t size, int fd, const char *msg)
68{
69 struct stat st;
70 char tmp[size];
71
72 errno = 0;
73 if (fstat (fd, &st) < 0)
74 {
75 error (0, errno, "%s: failed stat", msg);
76 return 1;
77 }
78
57b36a0a 79 if (st.st_size != (off_t) size)
b9337b6a
UD
80 {
81 error (0, errno, "%s: wrong size: %lu, should be %lu",
82 msg, (unsigned long int) st.st_size, (unsigned long int) size);
83 return 1;
84 }
85
57b36a0a 86 if (pread (fd, tmp, size, 0) != (ssize_t) size)
b9337b6a 87 {
9881cbf8 88 error (0, errno, "%s: failed pread", msg);
b9337b6a
UD
89 return 1;
90 }
91
92 if (memcmp (buf, tmp, size) != 0)
93 {
94 error (0, errno, "%s: failed comparison", msg);
95 return 1;
96 }
97
6b9c2e67 98 printf ("%s test ok\n", msg);
b9337b6a
UD
99
100 return 0;
101}
102
103
f71bc3ba 104static int
92806ee9 105do_wait (struct aiocb **cbp, size_t nent, int allowed_err)
6b9c2e67
UD
106{
107 int go_on;
92806ee9
UD
108 size_t cnt;
109 int result = 0;
110
6b9c2e67
UD
111 do
112 {
6b9c2e67
UD
113 aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
114 go_on = 0;
115 for (cnt = 0; cnt < nent; ++cnt)
92806ee9
UD
116 if (cbp[cnt] != NULL)
117 {
118 if (aio_error (cbp[cnt]) == EINPROGRESS)
119 go_on = 1;
120 else
121 {
122 if (aio_return (cbp[cnt]) == -1
123 && (allowed_err == 0
124 || aio_error (cbp[cnt]) != allowed_err))
125 {
126 error (0, aio_error (cbp[cnt]), "Operation failed\n");
127 result = 1;
128 }
129 cbp[cnt] = NULL;
130 }
131 }
6b9c2e67
UD
132 }
133 while (go_on);
92806ee9
UD
134
135 return result;
6b9c2e67
UD
136}
137
138
b9337b6a
UD
139int
140do_test (int argc, char *argv[])
141{
b9337b6a 142 struct aiocb cbs[10];
92806ee9 143 struct aiocb cbs_fsync;
b9337b6a 144 struct aiocb *cbp[10];
92806ee9 145 struct aiocb *cbp_fsync[1];
b9337b6a
UD
146 char buf[1000];
147 size_t cnt;
b9337b6a 148 int result = 0;
b9337b6a 149
b9337b6a
UD
150 /* Preparation. */
151 for (cnt = 0; cnt < 10; ++cnt)
152 {
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;
159
160 cbp[cnt] = &cbs[cnt];
161 }
162
163 /* First a simple test. */
164 for (cnt = 10; cnt > 0; )
ffa8d2a0
RM
165 if (aio_write (cbp[--cnt]) < 0 && errno == ENOSYS)
166 {
167 error (0, 0, "no aio support in this configuration");
168 return 0;
169 }
b9337b6a 170 /* Wait 'til the results are there. */
92806ee9 171 result |= do_wait (cbp, 10, 0);
6b9c2e67
UD
172 /* Test this. */
173 result |= test_file (buf, sizeof (buf), fd, "aio_write");
174
175 /* Read now as we've written it. */
176 memset (buf, '\0', sizeof (buf));
177 /* Issue the commands. */
178 for (cnt = 10; cnt > 0; )
b9337b6a 179 {
6b9c2e67
UD
180 --cnt;
181 cbp[cnt] = &cbs[cnt];
182 aio_read (cbp[cnt]);
b9337b6a 183 }
6b9c2e67 184 /* Wait 'til the results are there. */
92806ee9 185 result |= do_wait (cbp, 10, 0);
b9337b6a 186 /* Test this. */
6b9c2e67
UD
187 for (cnt = 0; cnt < 1000; ++cnt)
188 if (buf[cnt] != '0' + (cnt / 100))
189 {
190 result = 1;
191 error (0, 0, "comparison failed for aio_read test");
192 break;
193 }
194
195 if (cnt == 1000)
196 puts ("aio_read test ok");
197
198 /* Remove the test file contents. */
199 if (ftruncate (fd, 0) < 0)
200 {
201 error (0, errno, "ftruncate failed\n");
202 result = 1;
203 }
204
205 /* Test lio_listio. */
206 for (cnt = 0; cnt < 10; ++cnt)
207 {
208 cbs[cnt].aio_lio_opcode = LIO_WRITE;
209 cbp[cnt] = &cbs[cnt];
210 }
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)");
b9337b6a 215
92806ee9
UD
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;
220
221 /* Remove the test file contents first. */
222 if (ftruncate (fd, 0) < 0)
223 {
224 error (0, errno, "ftruncate failed\n");
225 result = 1;
226 }
227
228 /* Write again. */
229 for (cnt = 10; cnt > 0; )
230 aio_write (cbp[--cnt]);
231
232 if (aio_fsync (O_SYNC, &cbs_fsync) < 0)
233 {
234 error (0, errno, "aio_fsync failed\n");
235 result = 1;
236 }
237 result |= do_wait (cbp_fsync, 1, 0);
238
239 /* ...and test since all data should be on disk now. */
240 result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
241
242 /* Test aio_cancel. */
243 /* Remove the test file contents first. */
244 if (ftruncate (fd, 0) < 0)
245 {
246 error (0, errno, "ftruncate failed\n");
247 result = 1;
248 }
249
250 /* Write again. */
251 for (cnt = 10; cnt > 0; )
252 aio_write (cbp[--cnt]);
253
254 /* Cancel all requests. */
255 if (aio_cancel (fd, NULL) == -1)
256 printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
257
258 result |= do_wait (cbp, 10, ECANCELED);
259
260 /* Another test for aio_cancel. */
261 /* Remove the test file contents first. */
262 if (ftruncate (fd, 0) < 0)
263 {
264 error (0, errno, "ftruncate failed\n");
265 result = 1;
266 }
267
268 /* Write again. */
269 for (cnt = 10; cnt > 0; )
270 {
271 --cnt;
272 cbp[cnt] = &cbs[cnt];
273 aio_write (cbp[cnt]);
274 }
275 puts ("finished3");
276
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);
282 puts ("finished2");
283
284 result |= do_wait (cbp, 10, ECANCELED);
285
286 puts ("finished");
287
b9337b6a
UD
288 return result;
289}