Fix up POSIX testing in conformtest
[platform/upstream/glibc.git] / rt / tst-aio3.c
1 /* Test for notification mechanism in lio_listio.
2    Copyright (C) 2000, 2002, 2006 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, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <aio.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <errno.h>
25
26 static pthread_barrier_t b;
27
28
29 static void
30 thrfct (sigval_t arg)
31 {
32   int e = pthread_barrier_wait (&b);
33   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
34     {
35       puts ("child: barrier_wait failed");
36       exit (1);
37     }
38 }
39
40
41 static int
42 do_test (int argc, char *argv[])
43 {
44   char name[] = "/tmp/aio3.XXXXXX";
45   int fd;
46   struct aiocb *arr[1];
47   struct aiocb cb;
48   static const char buf[] = "Hello World\n";
49
50   fd = mkstemp (name);
51   if (fd == -1)
52     {
53       printf ("cannot open temp name: %m\n");
54       return 1;
55     }
56
57   unlink (name);
58
59   if (pthread_barrier_init (&b, NULL, 2) != 0)
60     {
61       puts ("barrier_init failed");
62       return 1;
63     }
64
65   arr[0] = &cb;
66
67   cb.aio_fildes = fd;
68   cb.aio_lio_opcode = LIO_WRITE;
69   cb.aio_reqprio = 0;
70   cb.aio_buf = (void *) buf;
71   cb.aio_nbytes = sizeof (buf) - 1;
72   cb.aio_offset = 0;
73   cb.aio_sigevent.sigev_notify = SIGEV_THREAD;
74   cb.aio_sigevent.sigev_notify_function = thrfct;
75   cb.aio_sigevent.sigev_notify_attributes = NULL;
76   cb.aio_sigevent.sigev_value.sival_ptr = NULL;
77
78   if (lio_listio (LIO_NOWAIT, arr, 1, NULL) < 0)
79     {
80       if (errno == ENOSYS)
81         {
82           puts ("no aio support in this configuration");
83           return 0;
84         }
85       printf ("lio_listio failed: %m\n");
86       return 1;
87     }
88
89   if (aio_suspend ((const struct aiocb *const *) arr, 1, NULL) < 0)
90     {
91       printf ("aio_suspend failed: %m\n");
92       return 1;
93     }
94
95   int e = pthread_barrier_wait (&b);
96   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
97     {
98       puts ("parent: barrier_wait failed");
99       return 1;
100     }
101
102   puts ("all OK");
103
104   return 0;
105 }
106
107 #include "../test-skeleton.c"