glibc/io/tst-statx.c

158 lines
4.9 KiB
C
Raw Normal View History

2018-07-10 14:14:45 +00:00
/* Basic test of statx system call.
Copyright (C) 2018-2019 Free Software Foundation, Inc.
2018-07-10 14:14:45 +00:00
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
2018-07-10 14:14:45 +00:00
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <support/check.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/xunistd.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <unistd.h>
/* Ensure that the types have the kernel-expected layout. */
_Static_assert (sizeof (struct statx_timestamp) == 16, "statx_timestamp size");
_Static_assert (sizeof (struct statx) == 256, "statx size");
_Static_assert (offsetof (struct statx, stx_nlink) == 16, "statx nlink");
_Static_assert (offsetof (struct statx, stx_ino) == 32, "statx ino");
_Static_assert (offsetof (struct statx, stx_atime) == 64, "statx atime");
_Static_assert (offsetof (struct statx, stx_rdev_major) == 128, "statx rdev");
_Static_assert (offsetof (struct statx, __statx_pad2) == 144, "statx pad2");
#include "statx_generic.c"
typedef int (*statx_function) (int, const char *, int, unsigned int,
struct statx *);
/* Return true if we have a real implementation of statx. */
static bool
kernel_supports_statx (void)
{
#ifdef __NR_statx
struct statx buf;
return syscall (__NR_statx, 0, "", AT_EMPTY_PATH, 0, &buf) == 0
|| errno != ENOSYS;
#else
return false;
#endif
}
/* Tests which apply to both implementations. */
static void
both_implementations_tests (statx_function impl, const char *path, int fd)
{
uint64_t ino;
{
struct statx buf = { 0, };
TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &buf), 0);
TEST_COMPARE (buf.stx_size, 3);
ino = buf.stx_ino;
}
{
struct statx buf = { 0, };
TEST_COMPARE (statx (AT_FDCWD, path, 0, STATX_BASIC_STATS, &buf), 0);
TEST_COMPARE (buf.stx_size, 3);
TEST_COMPARE (buf.stx_ino, ino);
}
{
struct statx stx = { 0, };
TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &stx), 0);
struct stat64 st;
xfstat (fd, &st);
TEST_COMPARE (stx.stx_mode, st.st_mode);
TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
}
{
struct statx stx = { 0, };
TEST_COMPARE (statx (AT_FDCWD, "/dev/null", 0, STATX_BASIC_STATS, &stx),
0);
struct stat64 st;
xstat ("/dev/null", &st);
TEST_COMPARE (stx.stx_mode, st.st_mode);
TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
}
}
/* Tests which apply only to the non-kernel (generic)
implementation. */
static void
non_kernel_tests (statx_function impl, int fd)
{
/* The non-kernel implementation must always fail for explicit sync
flags. */
struct statx buf;
errno = 0;
TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
STATX_BASIC_STATS, &buf), -1);
TEST_COMPARE (errno, EINVAL);
errno = 0;
TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
STATX_BASIC_STATS, &buf), -1);
TEST_COMPARE (errno, EINVAL);
}
static int
do_test (void)
{
char *path;
int fd = create_temp_file ("tst-statx-", &path);
TEST_VERIFY_EXIT (fd >= 0);
support_write_file_string (path, "abc");
both_implementations_tests (&statx, path, fd);
both_implementations_tests (&statx_generic, path, fd);
if (kernel_supports_statx ())
{
puts ("info: kernel supports statx");
struct statx buf;
buf.stx_size = 0;
TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
STATX_BASIC_STATS, &buf),
0);
TEST_COMPARE (buf.stx_size, 3);
buf.stx_size = 0;
TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
STATX_BASIC_STATS, &buf),
0);
TEST_COMPARE (buf.stx_size, 3);
}
else
{
puts ("info: kernel does not support statx");
non_kernel_tests (&statx, fd);
}
non_kernel_tests (&statx_generic, fd);
xclose (fd);
free (path);
return 0;
}
#include <support/test-driver.c>