Back to index

glibc  2.9
tst-aio6.c
Go to the documentation of this file.
00001 /* Test for timeout handling.
00002    Copyright (C) 2000, 2002 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 <errno.h>
00022 #include <stdio.h>
00023 #include <unistd.h>
00024 #include <sys/time.h>
00025 
00026 
00027 /* We expect to wait for 3 seconds so we have to increase the timeout.  */
00028 #define TIMEOUT 10 /* sec */
00029 
00030 
00031 #define TEST_FUNCTION do_test ()
00032 static int
00033 do_test (void)
00034 {
00035   struct aiocb *arr[1];
00036   struct aiocb cb;
00037   char buf[100];
00038   struct timeval before;
00039   struct timeval after;
00040   struct timespec timeout;
00041   int fd[2];
00042   int result = 0;
00043 
00044   if (pipe (fd) != 0)
00045     {
00046       printf ("cannot create pipe: %m\n");
00047       return 1;
00048     }
00049 
00050   arr[0] = &cb;
00051 
00052   cb.aio_fildes = fd[0];
00053   cb.aio_lio_opcode = LIO_WRITE;
00054   cb.aio_reqprio = 0;
00055   cb.aio_buf = (void *) buf;
00056   cb.aio_nbytes = sizeof (buf) - 1;
00057   cb.aio_offset = 0;
00058   cb.aio_sigevent.sigev_notify = SIGEV_NONE;
00059 
00060   /* Try to read from stdin where nothing will be available.  */
00061   if (aio_read (arr[0]) < 0)
00062     {
00063       if (errno == ENOSYS)
00064        {
00065          puts ("no aio support in this configuration");
00066          return 0;
00067        }
00068       printf ("aio_read failed: %m\n");
00069       return 1;
00070     }
00071 
00072   /* Get the current time.  */
00073   gettimeofday (&before, NULL);
00074 
00075   /* Wait for input which is unsuccessful and therefore the function will
00076      time out.  */
00077   timeout.tv_sec = 3;
00078   timeout.tv_nsec = 0;
00079   if (aio_suspend ((const struct aiocb *const*) arr, 1, &timeout) != -1)
00080     {
00081       puts ("aio_suspend() didn't return -1");
00082       result = 1;
00083     }
00084   else if (errno != EAGAIN)
00085     {
00086       puts ("error not set to EAGAIN");
00087       result = 1;
00088     }
00089   else
00090     {
00091       gettimeofday (&after, NULL);
00092       if (after.tv_sec < before.tv_sec + 1)
00093        {
00094          puts ("timeout came too early");
00095          result = 1;
00096        }
00097     }
00098 
00099   return result;
00100 }
00101 
00102 #include "../test-skeleton.c"