* locale/programs/ld-ctype.c (ctype_read): When given a repertoire
[platform/upstream/glibc.git] / rt / tst-aio7.c
1 /* Test for AIO POSIX compliance.
2    Copyright (C) 2001,02 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <aio.h>
21 #include <error.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27
28 /* We might wait for 3 seconds, so increase timeout to 10 seconds.  */
29 #define TIMEOUT 10
30
31
32 #define TEST_FUNCTION do_test ()
33 static int
34 do_test (void)
35 {
36   int result = 0;
37
38   /* Test for aio_cancel() detecting invalid file descriptor.  */
39   {
40     struct aiocb cb;
41     int fd = -1;
42
43     cb.aio_fildes = fd;
44     cb.aio_offset = 0;
45     cb.aio_buf = NULL;
46     cb.aio_nbytes = 0;
47     cb.aio_reqprio = 0;
48     cb.aio_sigevent.sigev_notify = SIGEV_NONE;
49
50     errno = 0;
51
52     /* Case one: invalid fds that match.  */
53     if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
54       {
55         if (errno == ENOSYS)
56           {
57             puts ("no aio support in this configuration");
58             return 0;
59           }
60
61         puts ("aio_cancel( -1, {-1..} ) did not return -1 or errno != EBADF");
62         ++result;
63       }
64
65     cb.aio_fildes = -2;
66     errno = 0;
67
68     /* Case two: invalid fds that do not match; just print warning.  */
69     if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
70       puts ("aio_cancel( -1, {-2..} ) did not return -1 or errno != EBADF");
71   }
72
73   /* Test for aio_fsync() detecting bad fd, and fd not open for writing.  */
74   {
75     struct aiocb cb;
76     int fd = -1;
77
78     cb.aio_fildes = fd;
79     cb.aio_offset = 0;
80     cb.aio_buf = NULL;
81     cb.aio_nbytes = 0;
82     cb.aio_reqprio = 0;
83     cb.aio_sigevent.sigev_notify = SIGEV_NONE;
84
85     errno = 0;
86
87     /* Case one: invalid fd.  */
88     if (aio_fsync (O_SYNC, &cb) != -1 || errno != EBADF)
89       {
90         puts ("aio_fsync( op, {-1..} ) did not return -1 or errno != EBADF");
91         ++result;
92       }
93
94     if ((fd = open ("/dev/null", O_RDONLY)) < 0)
95       error (1, errno, "opening /dev/null");
96
97     cb.aio_fildes = fd;
98     errno = 0;
99
100     /* Case two: valid fd but open for read only.  */
101     if (aio_fsync (O_SYNC, &cb) != -1 || errno != EBADF)
102       {
103         puts ("aio_fsync( op, {RO..} ) did not return -1 or errno != EBADF");
104         ++result;
105       }
106
107     close (fd);
108   }
109
110   /* Test for aio_suspend() suspending even if completed elements in list.  */
111   {
112     const int BYTES = 8, ELEMS = 2;
113     int i, r, fd;
114     char buff[BYTES];
115     char name[] = "/tmp/aio7.XXXXXX";
116     struct timespec timeout;
117     struct aiocb cb0, cb1;
118     struct aiocb *list[ELEMS];
119
120     fd = mkstemp (name);
121     if (fd < 0)
122       error (1, errno, "creating temp file");
123
124     if (unlink (name))
125       error (1, errno, "unlinking temp file");
126
127     if (write (fd, "01234567", BYTES) != BYTES)
128       error (1, errno, "writing to temp file");
129
130     cb0.aio_fildes = fd;
131     cb0.aio_offset = 0;
132     cb0.aio_buf = buff;
133     cb0.aio_nbytes = BYTES;
134     cb0.aio_reqprio = 0;
135     cb0.aio_sigevent.sigev_notify = SIGEV_NONE;
136
137     r = aio_read (&cb0);
138     if (r != 0)
139       error (1, errno, "reading from file");
140
141     while (aio_error (&(cb0)) == EINPROGRESS)
142       usleep (10);
143
144     for (i = 0; i < BYTES; i++)
145       printf ("%c ", buff[i]);
146     printf ("\n");
147
148     /* At this point, the first read is completed, so start another one on
149      * stdin, which will not complete unless the user inputs something.
150      */
151     cb1.aio_fildes = 0;
152     cb1.aio_offset = 0;
153     cb1.aio_buf = buff;
154     cb1.aio_nbytes = BYTES;
155     cb1.aio_reqprio = 0;
156     cb1.aio_sigevent.sigev_notify = SIGEV_NONE;
157
158     r = aio_read (&cb1);
159     if (r != 0)
160       error (1, errno, "reading from file");
161
162     /* Now call aio_suspend() with the two reads.  It should return
163      * immediately according to the POSIX spec.
164      */
165     list[0] = &cb0;
166     list[1] = &cb1;
167     timeout.tv_sec = 3;
168     timeout.tv_nsec = 0;
169     r = aio_suspend ((const struct aiocb * const *) list, ELEMS, &timeout);
170
171     if (r == -1 && errno == EAGAIN)
172       {
173         puts ("aio_suspend([done,blocked],2,3) suspended thread");
174         ++result;
175       }
176   }
177
178   return result;
179 }
180
181 #include "../test-skeleton.c"