Update.
[glibc.git] / rt / tst-aio.c
bloba6c0762a2af2139b70efcb652ae97d73c8d5279d
1 /* Tests for AIO in librt.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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.
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.
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. */
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>
29 /* Prototype for our test function. */
30 extern void do_prepare (int argc, char *argv[]);
31 extern int do_test (int argc, char *argv[]);
33 /* We have a preparation function. */
34 #define PREPARE do_prepare
36 /* We might need a bit longer timeout. */
37 #define TIMEOUT 20 /* sec */
39 /* This defines the `main' function and some more. */
40 #include <test-skeleton.c>
43 /* These are for the temporary file we generate. */
44 char *name;
45 int fd;
47 void
48 do_prepare (int argc, char *argv[])
50 char name_len;
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);
58 /* Open our test file. */
59 fd = mkstemp (name);
60 if (fd == -1)
61 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
65 int
66 test_file (const void *buf, size_t size, int fd, const char *msg)
68 struct stat st;
69 char tmp[size];
71 errno = 0;
72 if (fstat (fd, &st) < 0)
74 error (0, errno, "%s: failed stat", msg);
75 return 1;
78 if (st.st_size != size)
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;
85 if (pread (fd, tmp, size, 0) != size)
87 error (0, errno, "%s: failed stat", msg);
88 return 1;
91 if (memcmp (buf, tmp, size) != 0)
93 error (0, errno, "%s: failed comparison", msg);
94 return 1;
97 printf ("%s test ok\n", msg);
99 return 0;
103 void
104 do_wait (struct aiocb **cbp, size_t nent)
106 int go_on;
109 size_t cnt;
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;
119 while (go_on);
124 do_test (int argc, char *argv[])
126 struct aiocb cbs[10];
127 struct aiocb *cbp[10];
128 char buf[1000];
129 size_t cnt;
130 int result = 0;
132 /* Preparation. */
133 for (cnt = 0; cnt < 10; ++cnt)
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;
142 cbp[cnt] = &cbs[cnt];
145 /* First a simple test. */
146 for (cnt = 10; cnt > 0; )
147 aio_write (cbp[--cnt]);
148 /* Wait 'til the results are there. */
149 do_wait (cbp, 10);
150 /* Test this. */
151 result |= test_file (buf, sizeof (buf), fd, "aio_write");
153 /* Read now as we've written it. */
154 memset (buf, '\0', sizeof (buf));
155 /* Issue the commands. */
156 for (cnt = 10; cnt > 0; )
158 --cnt;
159 cbp[cnt] = &cbs[cnt];
160 aio_read (cbp[cnt]);
162 /* Wait 'til the results are there. */
163 do_wait (cbp, 10);
164 /* Test this. */
165 for (cnt = 0; cnt < 1000; ++cnt)
166 if (buf[cnt] != '0' + (cnt / 100))
168 result = 1;
169 error (0, 0, "comparison failed for aio_read test");
170 break;
173 if (cnt == 1000)
174 puts ("aio_read test ok");
176 /* Remove the test file contents. */
177 if (ftruncate (fd, 0) < 0)
179 error (0, errno, "ftruncate failed\n");
180 result = 1;
183 /* Test lio_listio. */
184 for (cnt = 0; cnt < 10; ++cnt)
186 cbs[cnt].aio_lio_opcode = LIO_WRITE;
187 cbp[cnt] = &cbs[cnt];
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)");
194 return result;