Replace FSF snail mail address with URLs.
[glibc.git] / rt / tst-aio7.c
blob327d28f1bf35e43c185c9c156bb9f4ee4450c3c2
1 /* Test for AIO POSIX compliance.
2 Copyright (C) 2001,02, 03 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <aio.h>
20 #include <error.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
27 /* We might wait for 3 seconds, so increase timeout to 10 seconds. */
28 #define TIMEOUT 10
31 #define TEST_FUNCTION do_test ()
32 static int
33 do_test (void)
35 int result = 0;
36 int piped[2];
38 /* Make a pipe that we will never write to, so we can block reading it. */
39 if (pipe (piped) < 0)
41 perror ("pipe");
42 return 1;
45 /* Test for aio_cancel() detecting invalid file descriptor. */
47 struct aiocb cb;
48 int fd = -1;
50 cb.aio_fildes = fd;
51 cb.aio_offset = 0;
52 cb.aio_buf = NULL;
53 cb.aio_nbytes = 0;
54 cb.aio_reqprio = 0;
55 cb.aio_sigevent.sigev_notify = SIGEV_NONE;
57 errno = 0;
59 /* Case one: invalid fds that match. */
60 if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
62 if (errno == ENOSYS)
64 puts ("no aio support in this configuration");
65 return 0;
68 puts ("aio_cancel( -1, {-1..} ) did not return -1 or errno != EBADF");
69 ++result;
72 cb.aio_fildes = -2;
73 errno = 0;
75 /* Case two: invalid fds that do not match; just print warning. */
76 if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
77 puts ("aio_cancel( -1, {-2..} ) did not return -1 or errno != EBADF");
80 /* Test for aio_fsync() detecting bad fd, and fd not open for writing. */
82 struct aiocb cb;
83 int fd = -1;
85 cb.aio_fildes = fd;
86 cb.aio_offset = 0;
87 cb.aio_buf = NULL;
88 cb.aio_nbytes = 0;
89 cb.aio_reqprio = 0;
90 cb.aio_sigevent.sigev_notify = SIGEV_NONE;
92 errno = 0;
94 /* Case one: invalid fd. */
95 if (aio_fsync (O_SYNC, &cb) != -1 || errno != EBADF)
97 puts ("aio_fsync( op, {-1..} ) did not return -1 or errno != EBADF");
98 ++result;
101 if ((fd = open ("/dev/null", O_RDONLY)) < 0)
102 error (1, errno, "opening /dev/null");
104 cb.aio_fildes = fd;
105 errno = 0;
107 /* Case two: valid fd but open for read only. */
108 if (aio_fsync (O_SYNC, &cb) != -1 || errno != EBADF)
110 puts ("aio_fsync( op, {RO..} ) did not return -1 or errno != EBADF");
111 ++result;
114 close (fd);
117 /* Test for aio_suspend() suspending even if completed elements in list. */
119 #define BYTES 8
120 const int ELEMS = 2;
121 int i, r, fd;
122 static char buff[BYTES];
123 char name[] = "/tmp/aio7.XXXXXX";
124 struct timespec timeout;
125 static struct aiocb cb0, cb1;
126 struct aiocb *list[ELEMS];
128 fd = mkstemp (name);
129 if (fd < 0)
130 error (1, errno, "creating temp file");
132 if (unlink (name))
133 error (1, errno, "unlinking temp file");
135 if (write (fd, "01234567", BYTES) != BYTES)
136 error (1, errno, "writing to temp file");
138 cb0.aio_fildes = fd;
139 cb0.aio_offset = 0;
140 cb0.aio_buf = buff;
141 cb0.aio_nbytes = BYTES;
142 cb0.aio_reqprio = 0;
143 cb0.aio_sigevent.sigev_notify = SIGEV_NONE;
145 r = aio_read (&cb0);
146 if (r != 0)
147 error (1, errno, "reading from file");
149 while (aio_error (&(cb0)) == EINPROGRESS)
150 usleep (10);
152 for (i = 0; i < BYTES; i++)
153 printf ("%c ", buff[i]);
154 printf ("\n");
156 /* At this point, the first read is completed, so start another one on
157 the read half of a pipe on which nothing will be written. */
158 cb1.aio_fildes = piped[0];
159 cb1.aio_offset = 0;
160 cb1.aio_buf = buff;
161 cb1.aio_nbytes = BYTES;
162 cb1.aio_reqprio = 0;
163 cb1.aio_sigevent.sigev_notify = SIGEV_NONE;
165 r = aio_read (&cb1);
166 if (r != 0)
167 error (1, errno, "reading from file");
169 /* Now call aio_suspend() with the two reads. It should return
170 * immediately according to the POSIX spec.
172 list[0] = &cb0;
173 list[1] = &cb1;
174 timeout.tv_sec = 3;
175 timeout.tv_nsec = 0;
176 r = aio_suspend ((const struct aiocb * const *) list, ELEMS, &timeout);
178 if (r == -1 && errno == EAGAIN)
180 puts ("aio_suspend([done,blocked],2,3) suspended thread");
181 ++result;
184 /* Note that CB1 is still pending, and so cannot be an auto variable.
185 Thus we also test that exiting with an outstanding request works. */
188 return result;
191 #include "../test-skeleton.c"