]> git.ipfire.org Git - thirdparty/glibc.git/blame - rt/tst-aio.c
support: Add support_capture_subprogram
[thirdparty/glibc.git] / rt / tst-aio.c
CommitLineData
b9337b6a 1/* Tests for AIO in librt.
04277e02 2 Copyright (C) 1998-2019 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 36
b9337b6a
UD
37/* This defines the `main' function and some more. */
38#include <test-skeleton.c>
39
40
310b3460
UD
41/* These are for the temporary file we generate. */
42char *name;
43int fd;
44
45void
46do_prepare (int argc, char *argv[])
47{
30aa5785 48 size_t name_len;
310b3460
UD
49
50 name_len = strlen (test_dir);
850c6760 51 name = xmalloc (name_len + sizeof ("/aioXXXXXX"));
310b3460
UD
52 mempcpy (mempcpy (name, test_dir, name_len),
53 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
310b3460
UD
54
55 /* Open our test file. */
56 fd = mkstemp (name);
57 if (fd == -1)
58 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
cf145565 59 add_temp_file (name);
310b3460
UD
60}
61
62
f71bc3ba 63static int
b9337b6a
UD
64test_file (const void *buf, size_t size, int fd, const char *msg)
65{
66 struct stat st;
67 char tmp[size];
68
69 errno = 0;
70 if (fstat (fd, &st) < 0)
71 {
72 error (0, errno, "%s: failed stat", msg);
73 return 1;
74 }
75
57b36a0a 76 if (st.st_size != (off_t) size)
b9337b6a
UD
77 {
78 error (0, errno, "%s: wrong size: %lu, should be %lu",
79 msg, (unsigned long int) st.st_size, (unsigned long int) size);
80 return 1;
81 }
82
57b36a0a 83 if (pread (fd, tmp, size, 0) != (ssize_t) size)
b9337b6a 84 {
9881cbf8 85 error (0, errno, "%s: failed pread", msg);
b9337b6a
UD
86 return 1;
87 }
88
89 if (memcmp (buf, tmp, size) != 0)
90 {
91 error (0, errno, "%s: failed comparison", msg);
92 return 1;
93 }
94
6b9c2e67 95 printf ("%s test ok\n", msg);
b9337b6a
UD
96
97 return 0;
98}
99
100
f71bc3ba 101static int
92806ee9 102do_wait (struct aiocb **cbp, size_t nent, int allowed_err)
6b9c2e67
UD
103{
104 int go_on;
92806ee9
UD
105 size_t cnt;
106 int result = 0;
107
6b9c2e67
UD
108 do
109 {
6b9c2e67
UD
110 aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
111 go_on = 0;
112 for (cnt = 0; cnt < nent; ++cnt)
92806ee9
UD
113 if (cbp[cnt] != NULL)
114 {
115 if (aio_error (cbp[cnt]) == EINPROGRESS)
116 go_on = 1;
117 else
118 {
119 if (aio_return (cbp[cnt]) == -1
120 && (allowed_err == 0
121 || aio_error (cbp[cnt]) != allowed_err))
122 {
123 error (0, aio_error (cbp[cnt]), "Operation failed\n");
124 result = 1;
125 }
126 cbp[cnt] = NULL;
127 }
128 }
6b9c2e67
UD
129 }
130 while (go_on);
92806ee9
UD
131
132 return result;
6b9c2e67
UD
133}
134
135
b9337b6a
UD
136int
137do_test (int argc, char *argv[])
138{
b9337b6a 139 struct aiocb cbs[10];
92806ee9 140 struct aiocb cbs_fsync;
b9337b6a 141 struct aiocb *cbp[10];
92806ee9 142 struct aiocb *cbp_fsync[1];
b9337b6a
UD
143 char buf[1000];
144 size_t cnt;
b9337b6a 145 int result = 0;
b9337b6a 146
b9337b6a
UD
147 /* Preparation. */
148 for (cnt = 0; cnt < 10; ++cnt)
149 {
150 cbs[cnt].aio_fildes = fd;
151 cbs[cnt].aio_reqprio = 0;
152 cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
153 cbs[cnt].aio_nbytes = 100;
154 cbs[cnt].aio_offset = cnt * 100;
155 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
156
157 cbp[cnt] = &cbs[cnt];
158 }
159
160 /* First a simple test. */
161 for (cnt = 10; cnt > 0; )
ffa8d2a0
RM
162 if (aio_write (cbp[--cnt]) < 0 && errno == ENOSYS)
163 {
164 error (0, 0, "no aio support in this configuration");
165 return 0;
166 }
b9337b6a 167 /* Wait 'til the results are there. */
92806ee9 168 result |= do_wait (cbp, 10, 0);
6b9c2e67
UD
169 /* Test this. */
170 result |= test_file (buf, sizeof (buf), fd, "aio_write");
171
172 /* Read now as we've written it. */
173 memset (buf, '\0', sizeof (buf));
174 /* Issue the commands. */
175 for (cnt = 10; cnt > 0; )
b9337b6a 176 {
6b9c2e67
UD
177 --cnt;
178 cbp[cnt] = &cbs[cnt];
179 aio_read (cbp[cnt]);
b9337b6a 180 }
6b9c2e67 181 /* Wait 'til the results are there. */
92806ee9 182 result |= do_wait (cbp, 10, 0);
b9337b6a 183 /* Test this. */
6b9c2e67
UD
184 for (cnt = 0; cnt < 1000; ++cnt)
185 if (buf[cnt] != '0' + (cnt / 100))
186 {
187 result = 1;
188 error (0, 0, "comparison failed for aio_read test");
189 break;
190 }
191
192 if (cnt == 1000)
193 puts ("aio_read test ok");
194
195 /* Remove the test file contents. */
196 if (ftruncate (fd, 0) < 0)
197 {
198 error (0, errno, "ftruncate failed\n");
199 result = 1;
200 }
201
202 /* Test lio_listio. */
203 for (cnt = 0; cnt < 10; ++cnt)
204 {
205 cbs[cnt].aio_lio_opcode = LIO_WRITE;
206 cbp[cnt] = &cbs[cnt];
207 }
208 /* Issue the command. */
209 lio_listio (LIO_WAIT, cbp, 10, NULL);
210 /* ...and immediately test it since we started it in wait mode. */
211 result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
b9337b6a 212
92806ee9
UD
213 /* Test aio_fsync. */
214 cbs_fsync.aio_fildes = fd;
215 cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
216 cbp_fsync[0] = &cbs_fsync;
217
218 /* Remove the test file contents first. */
219 if (ftruncate (fd, 0) < 0)
220 {
221 error (0, errno, "ftruncate failed\n");
222 result = 1;
223 }
224
225 /* Write again. */
226 for (cnt = 10; cnt > 0; )
227 aio_write (cbp[--cnt]);
228
229 if (aio_fsync (O_SYNC, &cbs_fsync) < 0)
230 {
231 error (0, errno, "aio_fsync failed\n");
232 result = 1;
233 }
234 result |= do_wait (cbp_fsync, 1, 0);
235
236 /* ...and test since all data should be on disk now. */
237 result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
238
239 /* Test aio_cancel. */
240 /* Remove the test file contents first. */
241 if (ftruncate (fd, 0) < 0)
242 {
243 error (0, errno, "ftruncate failed\n");
244 result = 1;
245 }
246
247 /* Write again. */
248 for (cnt = 10; cnt > 0; )
249 aio_write (cbp[--cnt]);
250
251 /* Cancel all requests. */
252 if (aio_cancel (fd, NULL) == -1)
253 printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
254
255 result |= do_wait (cbp, 10, ECANCELED);
256
257 /* Another test for aio_cancel. */
258 /* Remove the test file contents first. */
259 if (ftruncate (fd, 0) < 0)
260 {
261 error (0, errno, "ftruncate failed\n");
262 result = 1;
263 }
264
265 /* Write again. */
266 for (cnt = 10; cnt > 0; )
267 {
268 --cnt;
269 cbp[cnt] = &cbs[cnt];
270 aio_write (cbp[cnt]);
271 }
272 puts ("finished3");
273
274 /* Cancel all requests. */
275 for (cnt = 10; cnt > 0; )
276 if (aio_cancel (fd, cbp[--cnt]) == -1)
277 /* This is not an error. The request can simply be finished. */
278 printf ("aio_cancel (fd, cbp[%Zd]) cannot be canceled\n", cnt);
279 puts ("finished2");
280
281 result |= do_wait (cbp, 10, ECANCELED);
282
283 puts ("finished");
284
b9337b6a
UD
285 return result;
286}