Back to index

glibc  2.9
tst-aio4.c
Go to the documentation of this file.
00001 /* Test for completion signal handling.
00002    Copyright (C) 2000,01,02 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <aio.h>
00021 #include <signal.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <errno.h>
00026 
00027 /* We might need a bit longer timeout.  */
00028 #define TIMEOUT 10 /* sec */
00029 
00030 int my_signo;
00031 
00032 volatile sig_atomic_t flag;
00033 
00034 
00035 static void
00036 sighandler (const int signo)
00037 {
00038   flag = signo;
00039 }
00040 
00041 static int
00042 wait_flag (void)
00043 {
00044   while (flag == 0)
00045     {
00046       puts ("Sleeping...");
00047       sleep (1);
00048     }
00049 
00050   if (flag != my_signo)
00051     {
00052       printf ("signal handler received wrong signal, flag is %d\n", flag);
00053       return 1;
00054     }
00055 
00056   return 0;
00057 }
00058 
00059 #ifndef SIGRTMIN
00060 # define SIGRTMIN -1
00061 # define SIGRTMAX -1
00062 #endif
00063 
00064 static int
00065 do_test (int argc, char *argv[])
00066 {
00067   char name[] = "/tmp/aio4.XXXXXX";
00068   int fd;
00069   struct aiocb *arr[1];
00070   struct aiocb cb;
00071   static const char buf[] = "Hello World\n";
00072   struct aioinit init = {10, 20, 0};
00073   struct sigaction sa;
00074   struct sigevent ev;
00075 
00076   if (SIGRTMIN == -1)
00077   {
00078       printf ("RT signals not supported.\n");
00079       return 0;
00080   }
00081 
00082   /* Select a signal from the middle of the available choices... */
00083   my_signo = (SIGRTMAX + SIGRTMIN) / 2;
00084 
00085   fd = mkstemp (name);
00086   if (fd == -1)
00087     {
00088       printf ("cannot open temp name: %m\n");
00089       return 1;
00090     }
00091 
00092   unlink (name);
00093 
00094   /* Test also aio_init.  */
00095   aio_init (&init);
00096 
00097   arr[0] = &cb;
00098 
00099   cb.aio_fildes = fd;
00100   cb.aio_lio_opcode = LIO_WRITE;
00101   cb.aio_reqprio = 0;
00102   cb.aio_buf = (void *) buf;
00103   cb.aio_nbytes = sizeof (buf) - 1;
00104   cb.aio_offset = 0;
00105   cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
00106   cb.aio_sigevent.sigev_notify_function = NULL;
00107   cb.aio_sigevent.sigev_notify_attributes = NULL;
00108   cb.aio_sigevent.sigev_signo = my_signo;
00109   cb.aio_sigevent.sigev_value.sival_ptr = NULL;
00110 
00111   ev.sigev_notify = SIGEV_SIGNAL;
00112   ev.sigev_notify_function = NULL;
00113   ev.sigev_notify_attributes = NULL;
00114   ev.sigev_signo = my_signo;
00115 
00116   sa.sa_handler = sighandler;
00117   sigemptyset (&sa.sa_mask);
00118   sa.sa_flags = SA_RESTART;
00119 
00120   if (sigaction (my_signo, &sa, NULL) < 0)
00121     {
00122       printf ("sigaction failed: %m\n");
00123       return 1;
00124     }
00125 
00126   flag = 0;
00127   /* First use aio_write.  */
00128   if (aio_write (arr[0]) < 0)
00129     {
00130       if (errno == ENOSYS)
00131        {
00132          puts ("no aio support in this configuration");
00133          return 0;
00134        }
00135       printf ("aio_write failed: %m\n");
00136       return 1;
00137     }
00138 
00139   if (wait_flag ())
00140     return 1;
00141 
00142   puts ("aio_write OK");
00143 
00144   flag = 0;
00145   /* Again with lio_listio.  */
00146   if (lio_listio (LIO_NOWAIT, arr, 1, &ev) < 0)
00147     {
00148       printf ("lio_listio failed: %m\n");
00149       return 1;
00150     }
00151 
00152   if (wait_flag ())
00153     return 1;
00154 
00155   puts ("all OK");
00156 
00157   return 0;
00158 }
00159 
00160 #include "../test-skeleton.c"