Blame rt/tst-aio6.c

Packit 6c4009
/* Test for timeout handling.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <aio.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We expect to wait for 3 seconds so we have to increase the timeout.  */
Packit 6c4009
#define TIMEOUT 10 /* sec */
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  struct aiocb *arr[1];
Packit 6c4009
  struct aiocb cb;
Packit 6c4009
  char buf[100];
Packit 6c4009
  struct timeval before;
Packit 6c4009
  struct timeval after;
Packit 6c4009
  struct timespec timeout;
Packit 6c4009
  int fd[2];
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  if (pipe (fd) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create pipe: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  arr[0] = &cb;
Packit 6c4009
Packit 6c4009
  cb.aio_fildes = fd[0];
Packit 6c4009
  cb.aio_lio_opcode = LIO_WRITE;
Packit 6c4009
  cb.aio_reqprio = 0;
Packit 6c4009
  cb.aio_buf = (void *) buf;
Packit 6c4009
  cb.aio_nbytes = sizeof (buf) - 1;
Packit 6c4009
  cb.aio_offset = 0;
Packit 6c4009
  cb.aio_sigevent.sigev_notify = SIGEV_NONE;
Packit 6c4009
Packit 6c4009
  /* Try to read from stdin where nothing will be available.  */
Packit 6c4009
  if (aio_read (arr[0]) < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("no aio support in this configuration");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
      printf ("aio_read failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Get the current time.  */
Packit 6c4009
  gettimeofday (&before, NULL);
Packit 6c4009
Packit 6c4009
  /* Wait for input which is unsuccessful and therefore the function will
Packit 6c4009
     time out.  */
Packit 6c4009
  timeout.tv_sec = 3;
Packit 6c4009
  timeout.tv_nsec = 0;
Packit 6c4009
  if (aio_suspend ((const struct aiocb *const*) arr, 1, &timeout) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("aio_suspend() didn't return -1");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EAGAIN)
Packit 6c4009
    {
Packit 6c4009
      puts ("error not set to EAGAIN");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      gettimeofday (&after, NULL);
Packit 6c4009
      if (after.tv_sec < before.tv_sec + 1)
Packit 6c4009
	{
Packit 6c4009
	  puts ("timeout came too early");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"