Blame io/tst-statx.c

Packit Service 82fcde
/* Basic test of statx system call.
Packit Service 82fcde
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <stdbool.h>
Packit Service 82fcde
#include <stdint.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
#include <support/support.h>
Packit Service 82fcde
#include <support/temp_file.h>
Packit Service 82fcde
#include <support/xunistd.h>
Packit Service 82fcde
#include <sys/stat.h>
Packit Service 82fcde
#include <sys/syscall.h>
Packit Service 82fcde
#include <sys/sysmacros.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
Packit Service 82fcde
/* Ensure that the types have the kernel-expected layout.  */
Packit Service 82fcde
_Static_assert (sizeof (struct statx_timestamp) == 16, "statx_timestamp size");
Packit Service 82fcde
_Static_assert (sizeof (struct statx) == 256, "statx size");
Packit Service 82fcde
_Static_assert (offsetof (struct statx, stx_nlink) == 16, "statx nlink");
Packit Service 82fcde
_Static_assert (offsetof (struct statx, stx_ino) == 32, "statx ino");
Packit Service 82fcde
_Static_assert (offsetof (struct statx, stx_atime) == 64, "statx atime");
Packit Service 82fcde
_Static_assert (offsetof (struct statx, stx_rdev_major) == 128, "statx rdev");
Packit Service 82fcde
_Static_assert (offsetof (struct statx, __statx_pad2) == 144, "statx pad2");
Packit Service 82fcde
Packit Service 82fcde
#include "statx_generic.c"
Packit Service 82fcde
Packit Service 82fcde
typedef int (*statx_function) (int, const char *, int, unsigned int,
Packit Service 82fcde
                               struct statx *);
Packit Service 82fcde
Packit Service 82fcde
/* Return true if we have a real implementation of statx.  */
Packit Service 82fcde
static bool
Packit Service 82fcde
kernel_supports_statx (void)
Packit Service 82fcde
{
Packit Service 82fcde
#ifdef __NR_statx
Packit Service 82fcde
  struct statx buf;
Packit Service 82fcde
  return syscall (__NR_statx, 0, "", AT_EMPTY_PATH, 0, &buf) == 0
Packit Service 82fcde
    || errno != ENOSYS;
Packit Service 82fcde
#else
Packit Service 82fcde
  return false;
Packit Service 82fcde
#endif
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Tests which apply to both implementations.  */
Packit Service 82fcde
static void
Packit Service 82fcde
both_implementations_tests (statx_function impl, const char *path, int fd)
Packit Service 82fcde
{
Packit Service 82fcde
  uint64_t ino;
Packit Service 82fcde
  {
Packit Service 82fcde
    struct statx buf = { 0, };
Packit Service 82fcde
    TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &buf), 0);
Packit Service 82fcde
    TEST_COMPARE (buf.stx_size, 3);
Packit Service 82fcde
    ino = buf.stx_ino;
Packit Service 82fcde
  }
Packit Service 82fcde
  {
Packit Service 82fcde
    struct statx buf = { 0, };
Packit Service 82fcde
    TEST_COMPARE (statx (AT_FDCWD, path, 0, STATX_BASIC_STATS, &buf), 0);
Packit Service 82fcde
    TEST_COMPARE (buf.stx_size, 3);
Packit Service 82fcde
    TEST_COMPARE (buf.stx_ino, ino);
Packit Service 82fcde
  }
Packit Service 82fcde
  {
Packit Service 82fcde
    struct statx stx = { 0, };
Packit Service 82fcde
    TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &stx), 0);
Packit Service 82fcde
    struct stat64 st;
Packit Service 82fcde
    xfstat (fd, &st);
Packit Service 82fcde
    TEST_COMPARE (stx.stx_mode, st.st_mode);
Packit Service 82fcde
    TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
Packit Service 82fcde
    TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
Packit Service 82fcde
  }
Packit Service 82fcde
  {
Packit Service 82fcde
    struct statx stx = { 0, };
Packit Service 82fcde
    TEST_COMPARE (statx (AT_FDCWD, "/dev/null", 0, STATX_BASIC_STATS, &stx),
Packit Service 82fcde
                  0);
Packit Service 82fcde
    struct stat64 st;
Packit Service 82fcde
    xstat ("/dev/null", &st);
Packit Service 82fcde
    TEST_COMPARE (stx.stx_mode, st.st_mode);
Packit Service 82fcde
    TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
Packit Service 82fcde
    TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
Packit Service 82fcde
    TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
Packit Service 82fcde
    TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
Packit Service 82fcde
  }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Tests which apply only to the non-kernel (generic)
Packit Service 82fcde
   implementation.  */
Packit Service 82fcde
static void
Packit Service 82fcde
non_kernel_tests (statx_function impl, int fd)
Packit Service 82fcde
{
Packit Service 82fcde
  /* The non-kernel implementation must always fail for explicit sync
Packit Service 82fcde
     flags.  */
Packit Service 82fcde
  struct statx buf;
Packit Service 82fcde
  errno = 0;
Packit Service 82fcde
  TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
Packit Service 82fcde
                      STATX_BASIC_STATS, &buf), -1);
Packit Service 82fcde
  TEST_COMPARE (errno, EINVAL);
Packit Service 82fcde
  errno = 0;
Packit Service 82fcde
  TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
Packit Service 82fcde
                      STATX_BASIC_STATS, &buf), -1);
Packit Service 82fcde
  TEST_COMPARE (errno, EINVAL);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  char *path;
Packit Service 82fcde
  int fd = create_temp_file ("tst-statx-", &path);
Packit Service 82fcde
  TEST_VERIFY_EXIT (fd >= 0);
Packit Service 82fcde
  support_write_file_string (path, "abc");
Packit Service 82fcde
Packit Service 82fcde
  both_implementations_tests (&statx, path, fd);
Packit Service 82fcde
  both_implementations_tests (&statx_generic, path, fd);
Packit Service 82fcde
Packit Service 82fcde
  if (kernel_supports_statx ())
Packit Service 82fcde
    {
Packit Service 82fcde
      puts ("info: kernel supports statx");
Packit Service 82fcde
      struct statx buf;
Packit Service 82fcde
      buf.stx_size = 0;
Packit Service 82fcde
      TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
Packit Service 82fcde
                           STATX_BASIC_STATS, &buf),
Packit Service 82fcde
                    0);
Packit Service 82fcde
      TEST_COMPARE (buf.stx_size, 3);
Packit Service 82fcde
      buf.stx_size = 0;
Packit Service 82fcde
      TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
Packit Service 82fcde
                           STATX_BASIC_STATS, &buf),
Packit Service 82fcde
                    0);
Packit Service 82fcde
      TEST_COMPARE (buf.stx_size, 3);
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      puts ("info: kernel does not support statx");
Packit Service 82fcde
      non_kernel_tests (&statx, fd);
Packit Service 82fcde
    }
Packit Service 82fcde
  non_kernel_tests (&statx_generic, fd);
Packit Service 82fcde
Packit Service 82fcde
  xclose (fd);
Packit Service 82fcde
  free (path);
Packit Service 82fcde
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#include <support/test-driver.c>