Blame io/tst-statx.c

Packit 6c4009
/* Basic test of statx system call.
Packit 6c4009
   Copyright (C) 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 <stdbool.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/syscall.h>
Packit 6c4009
#include <sys/sysmacros.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* Ensure that the types have the kernel-expected layout.  */
Packit 6c4009
_Static_assert (sizeof (struct statx_timestamp) == 16, "statx_timestamp size");
Packit 6c4009
_Static_assert (sizeof (struct statx) == 256, "statx size");
Packit 6c4009
_Static_assert (offsetof (struct statx, stx_nlink) == 16, "statx nlink");
Packit 6c4009
_Static_assert (offsetof (struct statx, stx_ino) == 32, "statx ino");
Packit 6c4009
_Static_assert (offsetof (struct statx, stx_atime) == 64, "statx atime");
Packit 6c4009
_Static_assert (offsetof (struct statx, stx_rdev_major) == 128, "statx rdev");
Packit 6c4009
_Static_assert (offsetof (struct statx, __statx_pad2) == 144, "statx pad2");
Packit 6c4009
Packit 6c4009
#include "statx_generic.c"
Packit 6c4009
Packit 6c4009
typedef int (*statx_function) (int, const char *, int, unsigned int,
Packit 6c4009
                               struct statx *);
Packit 6c4009
Packit 6c4009
/* Return true if we have a real implementation of statx.  */
Packit 6c4009
static bool
Packit 6c4009
kernel_supports_statx (void)
Packit 6c4009
{
Packit 6c4009
#ifdef __NR_statx
Packit 6c4009
  struct statx buf;
Packit 6c4009
  return syscall (__NR_statx, 0, "", AT_EMPTY_PATH, 0, &buf) == 0
Packit 6c4009
    || errno != ENOSYS;
Packit 6c4009
#else
Packit 6c4009
  return false;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Tests which apply to both implementations.  */
Packit 6c4009
static void
Packit 6c4009
both_implementations_tests (statx_function impl, const char *path, int fd)
Packit 6c4009
{
Packit 6c4009
  uint64_t ino;
Packit 6c4009
  {
Packit 6c4009
    struct statx buf = { 0, };
Packit 6c4009
    TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &buf), 0);
Packit 6c4009
    TEST_COMPARE (buf.stx_size, 3);
Packit 6c4009
    ino = buf.stx_ino;
Packit 6c4009
  }
Packit 6c4009
  {
Packit 6c4009
    struct statx buf = { 0, };
Packit 6c4009
    TEST_COMPARE (statx (AT_FDCWD, path, 0, STATX_BASIC_STATS, &buf), 0);
Packit 6c4009
    TEST_COMPARE (buf.stx_size, 3);
Packit 6c4009
    TEST_COMPARE (buf.stx_ino, ino);
Packit 6c4009
  }
Packit 6c4009
  {
Packit 6c4009
    struct statx stx = { 0, };
Packit 6c4009
    TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &stx), 0);
Packit 6c4009
    struct stat64 st;
Packit 6c4009
    xfstat (fd, &st);
Packit 6c4009
    TEST_COMPARE (stx.stx_mode, st.st_mode);
Packit 6c4009
    TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
Packit 6c4009
    TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
Packit 6c4009
  }
Packit 6c4009
  {
Packit 6c4009
    struct statx stx = { 0, };
Packit 6c4009
    TEST_COMPARE (statx (AT_FDCWD, "/dev/null", 0, STATX_BASIC_STATS, &stx),
Packit 6c4009
                  0);
Packit 6c4009
    struct stat64 st;
Packit 6c4009
    xstat ("/dev/null", &st);
Packit 6c4009
    TEST_COMPARE (stx.stx_mode, st.st_mode);
Packit 6c4009
    TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
Packit 6c4009
    TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
Packit 6c4009
    TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
Packit 6c4009
    TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
Packit 6c4009
  }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Tests which apply only to the non-kernel (generic)
Packit 6c4009
   implementation.  */
Packit 6c4009
static void
Packit 6c4009
non_kernel_tests (statx_function impl, int fd)
Packit 6c4009
{
Packit 6c4009
  /* The non-kernel implementation must always fail for explicit sync
Packit 6c4009
     flags.  */
Packit 6c4009
  struct statx buf;
Packit 6c4009
  errno = 0;
Packit 6c4009
  TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
Packit 6c4009
                      STATX_BASIC_STATS, &buf), -1);
Packit 6c4009
  TEST_COMPARE (errno, EINVAL);
Packit 6c4009
  errno = 0;
Packit 6c4009
  TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
Packit 6c4009
                      STATX_BASIC_STATS, &buf), -1);
Packit 6c4009
  TEST_COMPARE (errno, EINVAL);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char *path;
Packit 6c4009
  int fd = create_temp_file ("tst-statx-", &path);
Packit 6c4009
  TEST_VERIFY_EXIT (fd >= 0);
Packit 6c4009
  support_write_file_string (path, "abc");
Packit 6c4009
Packit 6c4009
  both_implementations_tests (&statx, path, fd);
Packit 6c4009
  both_implementations_tests (&statx_generic, path, fd);
Packit 6c4009
Packit 6c4009
  if (kernel_supports_statx ())
Packit 6c4009
    {
Packit 6c4009
      puts ("info: kernel supports statx");
Packit 6c4009
      struct statx buf;
Packit 6c4009
      buf.stx_size = 0;
Packit 6c4009
      TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
Packit 6c4009
                           STATX_BASIC_STATS, &buf),
Packit 6c4009
                    0);
Packit 6c4009
      TEST_COMPARE (buf.stx_size, 3);
Packit 6c4009
      buf.stx_size = 0;
Packit 6c4009
      TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
Packit 6c4009
                           STATX_BASIC_STATS, &buf),
Packit 6c4009
                    0);
Packit 6c4009
      TEST_COMPARE (buf.stx_size, 3);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      puts ("info: kernel does not support statx");
Packit 6c4009
      non_kernel_tests (&statx, fd);
Packit 6c4009
    }
Packit 6c4009
  non_kernel_tests (&statx_generic, fd);
Packit 6c4009
Packit 6c4009
  xclose (fd);
Packit 6c4009
  free (path);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>