linux: Fix fstat64 on alpha and sparc64
[glibc.git] / rt / tst-aio4.c
blobb0f93ad03ed90e2e78c558169c8a6e009f27b86d
1 /* Test for completion signal handling.
2 Copyright (C) 2000-2024 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 <https://www.gnu.org/licenses/>. */
19 #include <aio.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <errno.h>
26 int my_signo;
28 volatile sig_atomic_t flag;
31 static void
32 sighandler (const int signo)
34 flag = signo;
37 static int
38 wait_flag (void)
40 while (flag == 0)
42 puts ("Sleeping...");
43 sleep (1);
46 if (flag != my_signo)
48 printf ("signal handler received wrong signal, flag is %d\n", flag);
49 return 1;
52 return 0;
55 #ifndef SIGRTMIN
56 # define SIGRTMIN -1
57 # define SIGRTMAX -1
58 #endif
60 static int
61 do_test (int argc, char *argv[])
63 char name[] = "/tmp/aio4.XXXXXX";
64 int fd;
65 struct aiocb *arr[1];
66 struct aiocb cb;
67 static const char buf[] = "Hello World\n";
68 struct aioinit init = {10, 20, 0};
69 struct sigaction sa;
70 struct sigevent ev;
72 if (SIGRTMIN == -1)
74 printf ("RT signals not supported.\n");
75 return 0;
78 /* Select a signal from the middle of the available choices... */
79 my_signo = (SIGRTMAX + SIGRTMIN) / 2;
81 fd = mkstemp (name);
82 if (fd == -1)
84 printf ("cannot open temp name: %m\n");
85 return 1;
88 unlink (name);
90 /* Test also aio_init. */
91 aio_init (&init);
93 arr[0] = &cb;
95 cb.aio_fildes = fd;
96 cb.aio_lio_opcode = LIO_WRITE;
97 cb.aio_reqprio = 0;
98 cb.aio_buf = (void *) buf;
99 cb.aio_nbytes = sizeof (buf) - 1;
100 cb.aio_offset = 0;
101 cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
102 cb.aio_sigevent.sigev_notify_function = NULL;
103 cb.aio_sigevent.sigev_notify_attributes = NULL;
104 cb.aio_sigevent.sigev_signo = my_signo;
105 cb.aio_sigevent.sigev_value.sival_ptr = NULL;
107 ev.sigev_notify = SIGEV_SIGNAL;
108 ev.sigev_notify_function = NULL;
109 ev.sigev_notify_attributes = NULL;
110 ev.sigev_signo = my_signo;
112 sa.sa_handler = sighandler;
113 sigemptyset (&sa.sa_mask);
114 sa.sa_flags = SA_RESTART;
116 if (sigaction (my_signo, &sa, NULL) < 0)
118 printf ("sigaction failed: %m\n");
119 return 1;
122 flag = 0;
123 /* First use aio_write. */
124 if (aio_write (arr[0]) < 0)
126 if (errno == ENOSYS)
128 puts ("no aio support in this configuration");
129 return 0;
131 printf ("aio_write failed: %m\n");
132 return 1;
135 if (wait_flag ())
136 return 1;
138 puts ("aio_write OK");
140 flag = 0;
141 /* Again with lio_listio. */
142 if (lio_listio (LIO_NOWAIT, arr, 1, &ev) < 0)
144 printf ("lio_listio failed: %m\n");
145 return 1;
148 if (wait_flag ())
149 return 1;
151 puts ("all OK");
153 return 0;
156 #include "../test-skeleton.c"