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

source code of glibc/rt/tst-aio.c