]> git.ipfire.org Git - thirdparty/glibc.git/blob - rt/tst-aio.c
83833ee117ba0571c97f7f19c7f7906968664983
[thirdparty/glibc.git] / rt / tst-aio.c
1 /* Tests for AIO in librt.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4
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.
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 Library General Public License for more details.
14
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. */
19
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>
27
28
29 /* prototype for our test function. */
30 extern int do_test (int argc, char *argv[]);
31
32
33 /* We might need a bit longer timeout. */
34 #define TIMEOUT 20 /* sec */
35
36 /* This defines the `main' function and some more. */
37 #include <test-skeleton.c>
38
39
40 int
41 test_file (const void *buf, size_t size, int fd, const char *msg)
42 {
43 struct stat st;
44 char tmp[size];
45
46 errno = 0;
47 if (fstat (fd, &st) < 0)
48 {
49 error (0, errno, "%s: failed stat", msg);
50 return 1;
51 }
52
53 if (st.st_size != size)
54 {
55 error (0, errno, "%s: wrong size: %lu, should be %lu",
56 msg, (unsigned long int) st.st_size, (unsigned long int) size);
57 return 1;
58 }
59
60 if (pread (fd, tmp, size, 0) != size)
61 {
62 error (0, errno, "%s: failed stat", msg);
63 return 1;
64 }
65
66 if (memcmp (buf, tmp, size) != 0)
67 {
68 error (0, errno, "%s: failed comparison", msg);
69 return 1;
70 }
71
72 printf ("%s test ok\n", msg);
73
74 return 0;
75 }
76
77
78 void
79 do_wait (struct aiocb **cbp, size_t nent)
80 {
81 int go_on;
82 do
83 {
84 size_t cnt;
85
86 aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
87 go_on = 0;
88 for (cnt = 0; cnt < nent; ++cnt)
89 if (cbp[cnt] != NULL && aio_error (cbp[cnt]) == EINPROGRESS)
90 go_on = 1;
91 else
92 cbp[cnt] = NULL;
93 }
94 while (go_on);
95 }
96
97
98 int
99 do_test (int argc, char *argv[])
100 {
101 char *name;
102 char name_len;
103 struct aiocb cbs[10];
104 struct aiocb *cbp[10];
105 char buf[1000];
106 size_t cnt;
107 int fd;
108 int result = 0;
109
110 name_len = strlen (test_dir);
111 name = malloc (name_len + sizeof ("/aioXXXXXX"));
112 mempcpy (mempcpy (name, test_dir, name_len),
113 "/aioXXXXXX", sizeof ("/aioXXXXXX"));
114 add_temp_file (name);
115
116 /* Open our test file. */
117 fd = mkstemp (name);
118 if (fd == -1)
119 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
120
121 /* Preparation. */
122 for (cnt = 0; cnt < 10; ++cnt)
123 {
124 cbs[cnt].aio_fildes = fd;
125 cbs[cnt].aio_reqprio = 0;
126 cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
127 cbs[cnt].aio_nbytes = 100;
128 cbs[cnt].aio_offset = cnt * 100;
129 cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
130
131 cbp[cnt] = &cbs[cnt];
132 }
133
134 /* First a simple test. */
135 for (cnt = 10; cnt > 0; )
136 aio_write (cbp[--cnt]);
137 /* Wait 'til the results are there. */
138 do_wait (cbp, 10);
139 /* Test this. */
140 result |= test_file (buf, sizeof (buf), fd, "aio_write");
141
142 /* Read now as we've written it. */
143 memset (buf, '\0', sizeof (buf));
144 /* Issue the commands. */
145 for (cnt = 10; cnt > 0; )
146 {
147 --cnt;
148 cbp[cnt] = &cbs[cnt];
149 aio_read (cbp[cnt]);
150 }
151 /* Wait 'til the results are there. */
152 do_wait (cbp, 10);
153 /* Test this. */
154 for (cnt = 0; cnt < 1000; ++cnt)
155 if (buf[cnt] != '0' + (cnt / 100))
156 {
157 result = 1;
158 error (0, 0, "comparison failed for aio_read test");
159 break;
160 }
161
162 if (cnt == 1000)
163 puts ("aio_read test ok");
164
165 /* Remove the test file contents. */
166 if (ftruncate (fd, 0) < 0)
167 {
168 error (0, errno, "ftruncate failed\n");
169 result = 1;
170 }
171
172 /* Test lio_listio. */
173 for (cnt = 0; cnt < 10; ++cnt)
174 {
175 cbs[cnt].aio_lio_opcode = LIO_WRITE;
176 cbp[cnt] = &cbs[cnt];
177 }
178 /* Issue the command. */
179 lio_listio (LIO_WAIT, cbp, 10, NULL);
180 /* ...and immediately test it since we started it in wait mode. */
181 result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
182
183 return result;
184 }