Fix up POSIX testing in conformtest
[platform/upstream/glibc.git] / rt / tst-aio.c
1 /* Tests for AIO in librt.
2    Copyright (C) 1998,2000,02 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6    The GNU C Library is free software; you can redistribute it and/or
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.
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
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
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 void do_prepare (int argc, char *argv[]);
31 extern int do_test (int argc, char *argv[]);
32
33 /* We have a preparation function.  */
34 #define PREPARE do_prepare
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
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   size_t 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
65 static 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 != (off_t) 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) != (ssize_t) size)
86     {
87       error (0, errno, "%s: failed pread", 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
97   printf ("%s test ok\n", msg);
98
99   return 0;
100 }
101
102
103 static int
104 do_wait (struct aiocb **cbp, size_t nent, int allowed_err)
105 {
106   int go_on;
107   size_t cnt;
108   int result = 0;
109
110   do
111     {
112       aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
113       go_on = 0;
114       for (cnt = 0; cnt < nent; ++cnt)
115         if (cbp[cnt] != NULL)
116           {
117             if (aio_error (cbp[cnt]) == EINPROGRESS)
118               go_on = 1;
119             else
120               {
121                 if (aio_return (cbp[cnt]) == -1
122                     && (allowed_err == 0
123                         || aio_error (cbp[cnt]) != allowed_err))
124                   {
125                     error (0, aio_error (cbp[cnt]), "Operation failed\n");
126                     result = 1;
127                   }
128                 cbp[cnt] = NULL;
129               }
130           }
131     }
132   while (go_on);
133
134   return result;
135 }
136
137
138 int
139 do_test (int argc, char *argv[])
140 {
141   struct aiocb cbs[10];
142   struct aiocb cbs_fsync;
143   struct aiocb *cbp[10];
144   struct aiocb *cbp_fsync[1];
145   char buf[1000];
146   size_t cnt;
147   int result = 0;
148
149   /* Preparation.  */
150   for (cnt = 0; cnt < 10; ++cnt)
151     {
152       cbs[cnt].aio_fildes = fd;
153       cbs[cnt].aio_reqprio = 0;
154       cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
155       cbs[cnt].aio_nbytes = 100;
156       cbs[cnt].aio_offset = cnt * 100;
157       cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
158
159       cbp[cnt] = &cbs[cnt];
160     }
161
162   /* First a simple test.  */
163   for (cnt = 10; cnt > 0; )
164     if (aio_write (cbp[--cnt]) < 0 && errno == ENOSYS)
165       {
166         error (0, 0, "no aio support in this configuration");
167         return 0;
168       }
169   /* Wait 'til the results are there.  */
170   result |= do_wait (cbp, 10, 0);
171   /* Test this.  */
172   result |= test_file (buf, sizeof (buf), fd, "aio_write");
173
174   /* Read now as we've written it.  */
175   memset (buf, '\0', sizeof (buf));
176   /* Issue the commands.  */
177   for (cnt = 10; cnt > 0; )
178     {
179       --cnt;
180       cbp[cnt] = &cbs[cnt];
181       aio_read (cbp[cnt]);
182     }
183   /* Wait 'til the results are there.  */
184   result |= do_wait (cbp, 10, 0);
185   /* Test this.  */
186   for (cnt = 0; cnt < 1000; ++cnt)
187     if (buf[cnt] != '0' + (cnt / 100))
188       {
189         result = 1;
190         error (0, 0, "comparison failed for aio_read test");
191         break;
192       }
193
194   if (cnt == 1000)
195     puts ("aio_read test ok");
196
197   /* Remove the test file contents.  */
198   if (ftruncate (fd, 0) < 0)
199     {
200       error (0, errno, "ftruncate failed\n");
201       result = 1;
202     }
203
204   /* Test lio_listio.  */
205   for (cnt = 0; cnt < 10; ++cnt)
206     {
207       cbs[cnt].aio_lio_opcode = LIO_WRITE;
208       cbp[cnt] = &cbs[cnt];
209     }
210   /* Issue the command.  */
211   lio_listio (LIO_WAIT, cbp, 10, NULL);
212   /* ...and immediately test it since we started it in wait mode.  */
213   result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
214
215   /* Test aio_fsync.  */
216   cbs_fsync.aio_fildes = fd;
217   cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
218   cbp_fsync[0] = &cbs_fsync;
219
220   /* Remove the test file contents first.  */
221   if (ftruncate (fd, 0) < 0)
222     {
223       error (0, errno, "ftruncate failed\n");
224       result = 1;
225     }
226
227   /* Write again.  */
228   for (cnt = 10; cnt > 0; )
229     aio_write (cbp[--cnt]);
230
231   if (aio_fsync (O_SYNC, &cbs_fsync) < 0)
232     {
233       error (0, errno, "aio_fsync failed\n");
234       result = 1;
235     }
236   result |= do_wait (cbp_fsync, 1, 0);
237
238   /* ...and test since all data should be on disk now.  */
239   result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
240
241   /* Test aio_cancel.  */
242   /* Remove the test file contents first.  */
243   if (ftruncate (fd, 0) < 0)
244     {
245       error (0, errno, "ftruncate failed\n");
246       result = 1;
247     }
248
249   /* Write again.  */
250   for (cnt = 10; cnt > 0; )
251     aio_write (cbp[--cnt]);
252
253   /* Cancel all requests.  */
254   if (aio_cancel (fd, NULL) == -1)
255     printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
256
257   result |= do_wait (cbp, 10, ECANCELED);
258
259   /* Another test for aio_cancel.  */
260   /* Remove the test file contents first.  */
261   if (ftruncate (fd, 0) < 0)
262     {
263       error (0, errno, "ftruncate failed\n");
264       result = 1;
265     }
266
267   /* Write again.  */
268   for (cnt = 10; cnt > 0; )
269     {
270       --cnt;
271       cbp[cnt] = &cbs[cnt];
272       aio_write (cbp[cnt]);
273     }
274   puts ("finished3");
275
276   /* Cancel all requests.  */
277   for (cnt = 10; cnt > 0; )
278     if (aio_cancel (fd, cbp[--cnt]) == -1)
279       /* This is not an error.  The request can simply be finished.  */
280       printf ("aio_cancel (fd, cbp[%Zd]) cannot be canceled\n", cnt);
281   puts ("finished2");
282
283   result |= do_wait (cbp, 10, ECANCELED);
284
285   puts ("finished");
286
287   return result;
288 }