]>
Commit | Line | Data |
---|---|---|
b9337b6a UD |
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 | ||
310b3460 UD |
29 | /* Prototype for our test function. */ |
30 | extern void do_prepare (int argc, char *argv[]); | |
b9337b6a UD |
31 | extern int do_test (int argc, char *argv[]); |
32 | ||
310b3460 UD |
33 | /* We have a preparation function. */ |
34 | #define PREPARE do_prepare | |
b9337b6a UD |
35 | |
36 | /* We might need a bit longer timeout. */ | |
37 | #define TIMEOUT 20 /* sec */ | |
38 | ||
39 | /* This defines the `main' function and some more. */ | |
40 | #include <test-skeleton.c> | |
41 | ||
42 | ||
310b3460 UD |
43 | /* These are for the temporary file we generate. */ |
44 | char *name; | |
45 | int fd; | |
46 | ||
47 | void | |
48 | do_prepare (int argc, char *argv[]) | |
49 | { | |
50 | char name_len; | |
51 | ||
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); | |
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); | |
62 | } | |
63 | ||
64 | ||
b9337b6a UD |
65 | int |
66 | test_file (const void *buf, size_t size, int fd, const char *msg) | |
67 | { | |
68 | struct stat st; | |
69 | char tmp[size]; | |
70 | ||
71 | errno = 0; | |
72 | if (fstat (fd, &st) < 0) | |
73 | { | |
74 | error (0, errno, "%s: failed stat", msg); | |
75 | return 1; | |
76 | } | |
77 | ||
78 | if (st.st_size != size) | |
79 | { | |
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; | |
83 | } | |
84 | ||
85 | if (pread (fd, tmp, size, 0) != size) | |
86 | { | |
87 | error (0, errno, "%s: failed stat", msg); | |
88 | return 1; | |
89 | } | |
90 | ||
91 | if (memcmp (buf, tmp, size) != 0) | |
92 | { | |
93 | error (0, errno, "%s: failed comparison", msg); | |
94 | return 1; | |
95 | } | |
96 | ||
6b9c2e67 | 97 | printf ("%s test ok\n", msg); |
b9337b6a UD |
98 | |
99 | return 0; | |
100 | } | |
101 | ||
102 | ||
6b9c2e67 UD |
103 | void |
104 | do_wait (struct aiocb **cbp, size_t nent) | |
105 | { | |
106 | int go_on; | |
107 | do | |
108 | { | |
109 | size_t cnt; | |
110 | ||
111 | aio_suspend ((const struct aiocb *const *) cbp, nent, NULL); | |
112 | go_on = 0; | |
113 | for (cnt = 0; cnt < nent; ++cnt) | |
114 | if (cbp[cnt] != NULL && aio_error (cbp[cnt]) == EINPROGRESS) | |
115 | go_on = 1; | |
116 | else | |
117 | cbp[cnt] = NULL; | |
118 | } | |
119 | while (go_on); | |
120 | } | |
121 | ||
122 | ||
b9337b6a UD |
123 | int |
124 | do_test (int argc, char *argv[]) | |
125 | { | |
b9337b6a UD |
126 | struct aiocb cbs[10]; |
127 | struct aiocb *cbp[10]; | |
128 | char buf[1000]; | |
129 | size_t cnt; | |
b9337b6a | 130 | int result = 0; |
b9337b6a | 131 | |
b9337b6a UD |
132 | /* Preparation. */ |
133 | for (cnt = 0; cnt < 10; ++cnt) | |
134 | { | |
135 | cbs[cnt].aio_fildes = fd; | |
136 | cbs[cnt].aio_reqprio = 0; | |
137 | cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100); | |
138 | cbs[cnt].aio_nbytes = 100; | |
139 | cbs[cnt].aio_offset = cnt * 100; | |
140 | cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE; | |
141 | ||
142 | cbp[cnt] = &cbs[cnt]; | |
143 | } | |
144 | ||
145 | /* First a simple test. */ | |
146 | for (cnt = 10; cnt > 0; ) | |
147 | aio_write (cbp[--cnt]); | |
148 | /* Wait 'til the results are there. */ | |
6b9c2e67 UD |
149 | do_wait (cbp, 10); |
150 | /* Test this. */ | |
151 | result |= test_file (buf, sizeof (buf), fd, "aio_write"); | |
152 | ||
153 | /* Read now as we've written it. */ | |
154 | memset (buf, '\0', sizeof (buf)); | |
155 | /* Issue the commands. */ | |
156 | for (cnt = 10; cnt > 0; ) | |
b9337b6a | 157 | { |
6b9c2e67 UD |
158 | --cnt; |
159 | cbp[cnt] = &cbs[cnt]; | |
160 | aio_read (cbp[cnt]); | |
b9337b6a | 161 | } |
6b9c2e67 UD |
162 | /* Wait 'til the results are there. */ |
163 | do_wait (cbp, 10); | |
b9337b6a | 164 | /* Test this. */ |
6b9c2e67 UD |
165 | for (cnt = 0; cnt < 1000; ++cnt) |
166 | if (buf[cnt] != '0' + (cnt / 100)) | |
167 | { | |
168 | result = 1; | |
169 | error (0, 0, "comparison failed for aio_read test"); | |
170 | break; | |
171 | } | |
172 | ||
173 | if (cnt == 1000) | |
174 | puts ("aio_read test ok"); | |
175 | ||
176 | /* Remove the test file contents. */ | |
177 | if (ftruncate (fd, 0) < 0) | |
178 | { | |
179 | error (0, errno, "ftruncate failed\n"); | |
180 | result = 1; | |
181 | } | |
182 | ||
183 | /* Test lio_listio. */ | |
184 | for (cnt = 0; cnt < 10; ++cnt) | |
185 | { | |
186 | cbs[cnt].aio_lio_opcode = LIO_WRITE; | |
187 | cbp[cnt] = &cbs[cnt]; | |
188 | } | |
189 | /* Issue the command. */ | |
190 | lio_listio (LIO_WAIT, cbp, 10, NULL); | |
191 | /* ...and immediately test it since we started it in wait mode. */ | |
192 | result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)"); | |
b9337b6a UD |
193 | |
194 | return result; | |
195 | } |