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