Blame posix/tst-posix_fadvise-common.c

Packit 6c4009
/* Common posix_fadvise tests definitions.
Packit 6c4009
   Copyright (C) 2016-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 <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
Packit 6c4009
static char *temp_filename;
Packit 6c4009
static int temp_fd;
Packit 6c4009
static char fifoname[] = "/tmp/tst-posix_fadvise-fifo-XXXXXX";
Packit 6c4009
static int fifofd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  temp_fd = create_temp_file ("tst-posix_fadvise.", &temp_filename);
Packit 6c4009
  if (temp_fd == -1)
Packit 6c4009
    FAIL_EXIT1 ("cannot create temporary file: %m");
Packit 6c4009
Packit 6c4009
  if (mktemp (fifoname) == NULL)
Packit 6c4009
    FAIL_EXIT1 ("cannot generate temp file name: %m");
Packit 6c4009
  add_temp_file (fifoname);
Packit 6c4009
Packit 6c4009
  if (mkfifo (fifoname, S_IWUSR | S_IRUSR) != 0)
Packit 6c4009
    FAIL_EXIT1 ("cannot create fifo: %m");
Packit 6c4009
Packit 6c4009
  fifofd = open (fifoname, O_RDONLY | O_NONBLOCK);
Packit 6c4009
  if (fifofd == -1)
Packit 6c4009
    FAIL_EXIT1 ("cannot open fifo: %m");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Effectivelly testing posix_fadvise is hard because side effects are not
Packit 6c4009
   observed without checking either performance or any kernel specific
Packit 6c4009
   supplied information.  Also, the syscall is meant to be an advisory,
Packit 6c4009
   so the kernel is free to use this information in any way it deems fit,
Packit 6c4009
   including ignoring it.
Packit 6c4009
Packit 6c4009
   This test check for some invalid returned operation to check argument
Packit 6c4009
   passing and if implementation follows POSIX error definition.  */
Packit 6c4009
static int
Packit 6c4009
do_test_common (void)
Packit 6c4009
{
Packit 6c4009
  /* Add some data to file and ensure it is written to disk.  */
Packit 6c4009
#define BLK_SIZE 2048
Packit 6c4009
  char buffer[BLK_SIZE] = { 0xcd };
Packit 6c4009
  ssize_t ret;
Packit 6c4009
Packit 6c4009
  if ((ret = write (temp_fd, buffer, BLK_SIZE)) != BLK_SIZE)
Packit 6c4009
    FAIL_EXIT1 ("write returned %zd different than expected %d",
Packit 6c4009
		ret, BLK_SIZE);
Packit 6c4009
Packit 6c4009
  if (fsync (temp_fd) != 0)
Packit 6c4009
    FAIL_EXIT1 ("fsync failed");
Packit 6c4009
Packit 6c4009
  /* Test passing an invalid fd.  */
Packit 6c4009
  if (posix_fadvise (-1, 0, 0, POSIX_FADV_NORMAL) != EBADF)
Packit 6c4009
    FAIL_EXIT1 ("posix_fadvise with invalid fd did not return EBADF");
Packit 6c4009
Packit 6c4009
  /* Test passing an invalid operation.  */
Packit 6c4009
  if (posix_fadvise (temp_fd, 0, 0, -1) != EINVAL)
Packit 6c4009
    FAIL_EXIT1 ("posix_fadvise with invalid advise did not return EINVAL");
Packit 6c4009
Packit 6c4009
  /* Test passing a FIFO fd.  */
Packit 6c4009
  if (posix_fadvise (fifofd, 0, 0, POSIX_FADV_NORMAL) != ESPIPE)
Packit 6c4009
    FAIL_EXIT1 ("posix_advise with PIPE fd did not return ESPIPE");
Packit 6c4009
Packit 6c4009
  /* Default fadvise on all file starting at initial position.  */
Packit 6c4009
  if (posix_fadvise (temp_fd, 0, 0, POSIX_FADV_NORMAL) != 0)
Packit 6c4009
    FAIL_EXIT1 ("default posix_fadvise failed");
Packit 6c4009
Packit 6c4009
  if (posix_fadvise (temp_fd, 0, 2 * BLK_SIZE, POSIX_FADV_NORMAL) != 0)
Packit 6c4009
    FAIL_EXIT1 ("posix_fadvise failed (offset = 0, len = %d) failed",
Packit 6c4009
		BLK_SIZE);
Packit 6c4009
Packit 6c4009
  if (posix_fadvise (temp_fd, 2 * BLK_SIZE, 0, POSIX_FADV_NORMAL) != 0)
Packit 6c4009
    FAIL_EXIT1 ("posix_fadvise failed (offset = %d, len = 0) failed",
Packit 6c4009
		BLK_SIZE);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
/* This function is defined by the individual tests.  */
Packit 6c4009
static int do_test (void);
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>