Blame rt/tst-aio7.c

Packit 6c4009
/* Test for AIO POSIX compliance.
Packit 6c4009
   Copyright (C) 2001-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 <error.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We might wait for 3 seconds, so increase timeout to 10 seconds.  */
Packit 6c4009
#define TIMEOUT 10
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  int piped[2];
Packit 6c4009
Packit 6c4009
  /* Make a pipe that we will never write to, so we can block reading it.  */
Packit 6c4009
  if (pipe (piped) < 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("pipe");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Test for aio_cancel() detecting invalid file descriptor.  */
Packit 6c4009
  {
Packit 6c4009
    struct aiocb cb;
Packit 6c4009
    int fd = -1;
Packit 6c4009
Packit 6c4009
    cb.aio_fildes = fd;
Packit 6c4009
    cb.aio_offset = 0;
Packit 6c4009
    cb.aio_buf = NULL;
Packit 6c4009
    cb.aio_nbytes = 0;
Packit 6c4009
    cb.aio_reqprio = 0;
Packit 6c4009
    cb.aio_sigevent.sigev_notify = SIGEV_NONE;
Packit 6c4009
Packit 6c4009
    errno = 0;
Packit 6c4009
Packit 6c4009
    /* Case one: invalid fds that match.  */
Packit 6c4009
    if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
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
Packit 6c4009
	puts ("aio_cancel( -1, {-1..} ) did not return -1 or errno != EBADF");
Packit 6c4009
	++result;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    cb.aio_fildes = -2;
Packit 6c4009
    errno = 0;
Packit 6c4009
Packit 6c4009
    /* Case two: invalid fds that do not match; just print warning.  */
Packit 6c4009
    if (aio_cancel (fd, &cb) != -1 || errno != EBADF)
Packit 6c4009
      puts ("aio_cancel( -1, {-2..} ) did not return -1 or errno != EBADF");
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Test for aio_fsync() detecting bad fd.  */
Packit 6c4009
  {
Packit 6c4009
    struct aiocb cb;
Packit 6c4009
    int fd = -1;
Packit 6c4009
Packit 6c4009
    cb.aio_fildes = fd;
Packit 6c4009
    cb.aio_offset = 0;
Packit 6c4009
    cb.aio_buf = NULL;
Packit 6c4009
    cb.aio_nbytes = 0;
Packit 6c4009
    cb.aio_reqprio = 0;
Packit 6c4009
    cb.aio_sigevent.sigev_notify = SIGEV_NONE;
Packit 6c4009
Packit 6c4009
    errno = 0;
Packit 6c4009
Packit 6c4009
    /* Case one: invalid fd.  */
Packit 6c4009
    if (aio_fsync (O_SYNC, &cb) != -1 || errno != EBADF)
Packit 6c4009
      {
Packit 6c4009
	puts ("aio_fsync( op, {-1..} ) did not return -1 or errno != EBADF");
Packit 6c4009
	++result;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Test for aio_suspend() suspending even if completed elements in list.  */
Packit 6c4009
  {
Packit 6c4009
#define BYTES 8
Packit 6c4009
    const int ELEMS = 2;
Packit 6c4009
    int i, r, fd;
Packit 6c4009
    static char buff[BYTES];
Packit 6c4009
    char name[] = "/tmp/aio7.XXXXXX";
Packit 6c4009
    struct timespec timeout;
Packit 6c4009
    static struct aiocb cb0, cb1;
Packit 6c4009
    struct aiocb *list[ELEMS];
Packit 6c4009
Packit 6c4009
    fd = mkstemp (name);
Packit 6c4009
    if (fd < 0)
Packit 6c4009
      error (1, errno, "creating temp file");
Packit 6c4009
Packit 6c4009
    if (unlink (name))
Packit 6c4009
      error (1, errno, "unlinking temp file");
Packit 6c4009
Packit 6c4009
    if (write (fd, "01234567", BYTES) != BYTES)
Packit 6c4009
      error (1, errno, "writing to temp file");
Packit 6c4009
Packit 6c4009
    cb0.aio_fildes = fd;
Packit 6c4009
    cb0.aio_offset = 0;
Packit 6c4009
    cb0.aio_buf = buff;
Packit 6c4009
    cb0.aio_nbytes = BYTES;
Packit 6c4009
    cb0.aio_reqprio = 0;
Packit 6c4009
    cb0.aio_sigevent.sigev_notify = SIGEV_NONE;
Packit 6c4009
Packit 6c4009
    r = aio_read (&cb0);
Packit 6c4009
    if (r != 0)
Packit 6c4009
      error (1, errno, "reading from file");
Packit 6c4009
Packit 6c4009
    while (aio_error (&(cb0)) == EINPROGRESS)
Packit 6c4009
      usleep (10);
Packit 6c4009
Packit 6c4009
    for (i = 0; i < BYTES; i++)
Packit 6c4009
      printf ("%c ", buff[i]);
Packit 6c4009
    printf ("\n");
Packit 6c4009
Packit 6c4009
    /* At this point, the first read is completed, so start another one on
Packit 6c4009
       the read half of a pipe on which nothing will be written.  */
Packit 6c4009
    cb1.aio_fildes = piped[0];
Packit 6c4009
    cb1.aio_offset = 0;
Packit 6c4009
    cb1.aio_buf = buff;
Packit 6c4009
    cb1.aio_nbytes = BYTES;
Packit 6c4009
    cb1.aio_reqprio = 0;
Packit 6c4009
    cb1.aio_sigevent.sigev_notify = SIGEV_NONE;
Packit 6c4009
Packit 6c4009
    r = aio_read (&cb1);
Packit 6c4009
    if (r != 0)
Packit 6c4009
      error (1, errno, "reading from file");
Packit 6c4009
Packit 6c4009
    /* Now call aio_suspend() with the two reads.  It should return
Packit 6c4009
     * immediately according to the POSIX spec.
Packit 6c4009
     */
Packit 6c4009
    list[0] = &cb;;
Packit 6c4009
    list[1] = &cb;;
Packit 6c4009
    timeout.tv_sec = 3;
Packit 6c4009
    timeout.tv_nsec = 0;
Packit 6c4009
    r = aio_suspend ((const struct aiocb * const *) list, ELEMS, &timeout);
Packit 6c4009
Packit 6c4009
    if (r == -1 && errno == EAGAIN)
Packit 6c4009
      {
Packit 6c4009
	puts ("aio_suspend([done,blocked],2,3) suspended thread");
Packit 6c4009
	++result;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    /* Note that CB1 is still pending, and so cannot be an auto variable.
Packit 6c4009
       Thus we also test that exiting with an outstanding request works.  */
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"