2.5-18.1
[glibc.git] / rtkaio / tst-aiod4.c
blobd9ce18ae28a4ab3ce385af4f48800380d9c4b5ed
1 /* Test for completion signal handling.
2 Copyright (C) 2000, 2001, 2002, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <aio.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include "tst-aiod.h"
28 /* We might need a bit longer timeout. */
29 #define TIMEOUT 10 /* sec */
31 int my_signo;
33 volatile sig_atomic_t flag;
36 static void
37 sighandler (const int signo)
39 flag = signo;
42 static int
43 wait_flag (void)
45 while (flag == 0)
47 puts ("Sleeping...");
48 sleep (1);
51 if (flag != my_signo)
53 printf ("signal handler received wrong signal, flag is %d\n", flag);
54 return 1;
57 return 0;
60 #ifndef SIGRTMIN
61 # define SIGRTMIN -1
62 # define SIGRTMAX -1
63 #endif
65 static int
66 do_test (int argc, char *argv[])
68 char name[] = "/tmp/aio4.XXXXXX";
69 int fd;
70 struct aiocb *arr[1];
71 struct aiocb cb;
72 static const char buf[] = "Hello World\n";
73 struct aioinit init = {10, 20, 0};
74 struct sigaction sa;
75 struct sigevent ev;
77 if (SIGRTMIN == -1)
79 printf ("RT signals not supported.\n");
80 return 0;
83 /* Select a signal from the middle of the available choices... */
84 my_signo = (SIGRTMAX + SIGRTMIN) / 2;
86 fd = mkstemp (name);
87 if (fd == -1)
89 printf ("cannot open temp name: %m\n");
90 return 1;
93 unlink (name);
95 /* Test also aio_init. */
96 aio_init (&init);
98 arr[0] = &cb;
100 void *p;
101 int sz = set_o_direct (fd);
102 if (sz != -1)
104 int err = posix_memalign (&p, sz, sz);
105 if (err)
107 errno = err;
108 printf ("cannot allocate memory: %m\n");
109 return 1;
111 memcpy (p, buf, sizeof (buf) - 1);
112 memset (p + sizeof (buf) - 1, ' ', sz - sizeof (buf) + 1);
113 printf ("Using O_DIRECT with block size %d\n", sz);
115 else
117 p = (void *) buf;
118 sz = sizeof (buf) - 1;
121 cb.aio_fildes = fd;
122 cb.aio_lio_opcode = LIO_WRITE;
123 cb.aio_reqprio = 0;
124 cb.aio_buf = p;
125 cb.aio_nbytes = sz;
126 cb.aio_offset = 0;
127 cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
128 cb.aio_sigevent.sigev_notify_function = NULL;
129 cb.aio_sigevent.sigev_notify_attributes = NULL;
130 cb.aio_sigevent.sigev_signo = my_signo;
131 cb.aio_sigevent.sigev_value.sival_ptr = NULL;
133 ev.sigev_notify = SIGEV_SIGNAL;
134 ev.sigev_notify_function = NULL;
135 ev.sigev_notify_attributes = NULL;
136 ev.sigev_signo = my_signo;
138 sa.sa_handler = sighandler;
139 sigemptyset (&sa.sa_mask);
140 sa.sa_flags = SA_RESTART;
142 if (sigaction (my_signo, &sa, NULL) < 0)
144 printf ("sigaction failed: %m\n");
145 return 1;
148 flag = 0;
149 /* First use aio_write. */
150 if (aio_write (arr[0]) < 0)
152 if (errno == ENOSYS)
154 puts ("no aio support in this configuration");
155 return 0;
157 printf ("aio_write failed: %m\n");
158 return 1;
161 if (wait_flag ())
162 return 1;
164 puts ("aio_write OK");
166 flag = 0;
167 /* Again with lio_listio. */
168 if (lio_listio (LIO_NOWAIT, arr, 1, &ev) < 0)
170 printf ("lio_listio failed: %m\n");
171 return 1;
174 if (wait_flag ())
175 return 1;
177 puts ("all OK");
179 return 0;
182 #include "../test-skeleton.c"