Blame rt/tst-aio.c

Packit 6c4009
/* Tests for AIO in librt.
Packit 6c4009
   Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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 <error.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Prototype for our test function.  */
Packit 6c4009
extern void do_prepare (int argc, char *argv[]);
Packit 6c4009
extern int do_test (int argc, char *argv[]);
Packit 6c4009
Packit 6c4009
/* We have a preparation function.  */
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
/* We might need a bit longer timeout.  */
Packit 6c4009
#define TIMEOUT 20 /* sec */
Packit 6c4009
Packit 6c4009
/* This defines the `main' function and some more.  */
Packit 6c4009
#include <test-skeleton.c>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* These are for the temporary file we generate.  */
Packit 6c4009
char *name;
Packit 6c4009
int fd;
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
do_prepare (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  size_t name_len;
Packit 6c4009
Packit 6c4009
  name_len = strlen (test_dir);
Packit 6c4009
  name = xmalloc (name_len + sizeof ("/aioXXXXXX"));
Packit 6c4009
  mempcpy (mempcpy (name, test_dir, name_len),
Packit 6c4009
	   "/aioXXXXXX", sizeof ("/aioXXXXXX"));
Packit 6c4009
Packit 6c4009
  /* Open our test file.   */
Packit 6c4009
  fd = mkstemp (name);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
Packit 6c4009
  add_temp_file (name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
test_file (const void *buf, size_t size, int fd, const char *msg)
Packit 6c4009
{
Packit 6c4009
  struct stat st;
Packit 6c4009
  char tmp[size];
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (fstat (fd, &st) < 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "%s: failed stat", msg);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (st.st_size != (off_t) size)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "%s: wrong size: %lu, should be %lu",
Packit 6c4009
	     msg, (unsigned long int) st.st_size, (unsigned long int) size);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pread (fd, tmp, size, 0) != (ssize_t) size)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "%s: failed pread", msg);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (memcmp (buf, tmp, size) != 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "%s: failed comparison", msg);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf ("%s test ok\n", msg);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_wait (struct aiocb **cbp, size_t nent, int allowed_err)
Packit 6c4009
{
Packit 6c4009
  int go_on;
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      aio_suspend ((const struct aiocb *const *) cbp, nent, NULL);
Packit 6c4009
      go_on = 0;
Packit 6c4009
      for (cnt = 0; cnt < nent; ++cnt)
Packit 6c4009
	if (cbp[cnt] != NULL)
Packit 6c4009
	  {
Packit 6c4009
	    if (aio_error (cbp[cnt]) == EINPROGRESS)
Packit 6c4009
	      go_on = 1;
Packit 6c4009
	    else
Packit 6c4009
	      {
Packit 6c4009
		if (aio_return (cbp[cnt]) == -1
Packit 6c4009
		    && (allowed_err == 0
Packit 6c4009
			|| aio_error (cbp[cnt]) != allowed_err))
Packit 6c4009
		  {
Packit 6c4009
		    error (0, aio_error (cbp[cnt]), "Operation failed\n");
Packit 6c4009
		    result = 1;
Packit 6c4009
		  }
Packit 6c4009
		cbp[cnt] = NULL;
Packit 6c4009
	      }
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
  while (go_on);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  struct aiocb cbs[10];
Packit 6c4009
  struct aiocb cbs_fsync;
Packit 6c4009
  struct aiocb *cbp[10];
Packit 6c4009
  struct aiocb *cbp_fsync[1];
Packit 6c4009
  char buf[1000];
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  /* Preparation.  */
Packit 6c4009
  for (cnt = 0; cnt < 10; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      cbs[cnt].aio_fildes = fd;
Packit 6c4009
      cbs[cnt].aio_reqprio = 0;
Packit 6c4009
      cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100);
Packit 6c4009
      cbs[cnt].aio_nbytes = 100;
Packit 6c4009
      cbs[cnt].aio_offset = cnt * 100;
Packit 6c4009
      cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE;
Packit 6c4009
Packit 6c4009
      cbp[cnt] = &cbs[cnt];
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* First a simple test.  */
Packit 6c4009
  for (cnt = 10; cnt > 0; )
Packit 6c4009
    if (aio_write (cbp[--cnt]) < 0 && errno == ENOSYS)
Packit 6c4009
      {
Packit 6c4009
	error (0, 0, "no aio support in this configuration");
Packit 6c4009
	return 0;
Packit 6c4009
      }
Packit 6c4009
  /* Wait 'til the results are there.  */
Packit 6c4009
  result |= do_wait (cbp, 10, 0);
Packit 6c4009
  /* Test this.  */
Packit 6c4009
  result |= test_file (buf, sizeof (buf), fd, "aio_write");
Packit 6c4009
Packit 6c4009
  /* Read now as we've written it.  */
Packit 6c4009
  memset (buf, '\0', sizeof (buf));
Packit 6c4009
  /* Issue the commands.  */
Packit 6c4009
  for (cnt = 10; cnt > 0; )
Packit 6c4009
    {
Packit 6c4009
      --cnt;
Packit 6c4009
      cbp[cnt] = &cbs[cnt];
Packit 6c4009
      aio_read (cbp[cnt]);
Packit 6c4009
    }
Packit 6c4009
  /* Wait 'til the results are there.  */
Packit 6c4009
  result |= do_wait (cbp, 10, 0);
Packit 6c4009
  /* Test this.  */
Packit 6c4009
  for (cnt = 0; cnt < 1000; ++cnt)
Packit 6c4009
    if (buf[cnt] != '0' + (cnt / 100))
Packit 6c4009
      {
Packit 6c4009
	result = 1;
Packit 6c4009
	error (0, 0, "comparison failed for aio_read test");
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (cnt == 1000)
Packit 6c4009
    puts ("aio_read test ok");
Packit 6c4009
Packit 6c4009
  /* Remove the test file contents.  */
Packit 6c4009
  if (ftruncate (fd, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "ftruncate failed\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Test lio_listio.  */
Packit 6c4009
  for (cnt = 0; cnt < 10; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      cbs[cnt].aio_lio_opcode = LIO_WRITE;
Packit 6c4009
      cbp[cnt] = &cbs[cnt];
Packit 6c4009
    }
Packit 6c4009
  /* Issue the command.  */
Packit 6c4009
  lio_listio (LIO_WAIT, cbp, 10, NULL);
Packit 6c4009
  /* ...and immediately test it since we started it in wait mode.  */
Packit 6c4009
  result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)");
Packit 6c4009
Packit 6c4009
  /* Test aio_fsync.  */
Packit 6c4009
  cbs_fsync.aio_fildes = fd;
Packit 6c4009
  cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE;
Packit 6c4009
  cbp_fsync[0] = &cbs_fsync;
Packit 6c4009
Packit 6c4009
  /* Remove the test file contents first.  */
Packit 6c4009
  if (ftruncate (fd, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "ftruncate failed\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Write again.  */
Packit 6c4009
  for (cnt = 10; cnt > 0; )
Packit 6c4009
    aio_write (cbp[--cnt]);
Packit 6c4009
Packit 6c4009
  if (aio_fsync (O_SYNC, &cbs_fsync) < 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "aio_fsync failed\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  result |= do_wait (cbp_fsync, 1, 0);
Packit 6c4009
Packit 6c4009
  /* ...and test since all data should be on disk now.  */
Packit 6c4009
  result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)");
Packit 6c4009
Packit 6c4009
  /* Test aio_cancel.  */
Packit 6c4009
  /* Remove the test file contents first.  */
Packit 6c4009
  if (ftruncate (fd, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "ftruncate failed\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Write again.  */
Packit 6c4009
  for (cnt = 10; cnt > 0; )
Packit 6c4009
    aio_write (cbp[--cnt]);
Packit 6c4009
Packit 6c4009
  /* Cancel all requests.  */
Packit 6c4009
  if (aio_cancel (fd, NULL) == -1)
Packit 6c4009
    printf ("aio_cancel (fd, NULL) cannot cancel anything\n");
Packit 6c4009
Packit 6c4009
  result |= do_wait (cbp, 10, ECANCELED);
Packit 6c4009
Packit 6c4009
  /* Another test for aio_cancel.  */
Packit 6c4009
  /* Remove the test file contents first.  */
Packit 6c4009
  if (ftruncate (fd, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "ftruncate failed\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Write again.  */
Packit 6c4009
  for (cnt = 10; cnt > 0; )
Packit 6c4009
    {
Packit 6c4009
      --cnt;
Packit 6c4009
      cbp[cnt] = &cbs[cnt];
Packit 6c4009
      aio_write (cbp[cnt]);
Packit 6c4009
    }
Packit 6c4009
  puts ("finished3");
Packit 6c4009
Packit 6c4009
  /* Cancel all requests.  */
Packit 6c4009
  for (cnt = 10; cnt > 0; )
Packit 6c4009
    if (aio_cancel (fd, cbp[--cnt]) == -1)
Packit 6c4009
      /* This is not an error.  The request can simply be finished.  */
Packit 6c4009
      printf ("aio_cancel (fd, cbp[%Zd]) cannot be canceled\n", cnt);
Packit 6c4009
  puts ("finished2");
Packit 6c4009
Packit 6c4009
  result |= do_wait (cbp, 10, ECANCELED);
Packit 6c4009
Packit 6c4009
  puts ("finished");
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}