1 /* Basic test of statx system call.
2 Copyright (C) 2018-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <support/check.h>
25 #include <support/support.h>
26 #include <support/temp_file.h>
27 #include <support/xunistd.h>
29 #include <sys/syscall.h>
30 #include <sys/sysmacros.h>
33 /* Ensure that the types have the kernel-expected layout. */
34 _Static_assert (sizeof (struct statx_timestamp) == 16, "statx_timestamp size");
35 _Static_assert (sizeof (struct statx) == 256, "statx size");
36 _Static_assert (offsetof (struct statx, stx_nlink) == 16, "statx nlink");
37 _Static_assert (offsetof (struct statx, stx_ino) == 32, "statx ino");
38 _Static_assert (offsetof (struct statx, stx_atime) == 64, "statx atime");
39 _Static_assert (offsetof (struct statx, stx_rdev_major) == 128, "statx rdev");
40 _Static_assert (offsetof (struct statx, __statx_pad2) == 144, "statx pad2");
42 #include "statx_generic.c"
44 typedef int (*statx_function) (int, const char *, int, unsigned int,
47 /* Return true if we have a real implementation of statx. */
49 kernel_supports_statx (void)
53 return syscall (__NR_statx, 0, "", AT_EMPTY_PATH, 0, &buf) == 0
60 /* Tests which apply to both implementations. */
62 both_implementations_tests (statx_function impl, const char *path, int fd)
66 struct statx buf = { 0, };
67 TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &buf), 0);
68 TEST_COMPARE (buf.stx_size, 3);
72 struct statx buf = { 0, };
73 TEST_COMPARE (statx (AT_FDCWD, path, 0, STATX_BASIC_STATS, &buf), 0);
74 TEST_COMPARE (buf.stx_size, 3);
75 TEST_COMPARE (buf.stx_ino, ino);
78 struct statx stx = { 0, };
79 TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &stx), 0);
82 TEST_COMPARE (stx.stx_mode, st.st_mode);
83 TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
84 TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
87 struct statx stx = { 0, };
88 TEST_COMPARE (statx (AT_FDCWD, "/dev/null", 0, STATX_BASIC_STATS, &stx),
91 xstat ("/dev/null", &st);
92 TEST_COMPARE (stx.stx_mode, st.st_mode);
93 TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
94 TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
95 TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
96 TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
100 /* Tests which apply only to the non-kernel (generic)
103 non_kernel_tests (statx_function impl, int fd)
105 /* The non-kernel implementation must always fail for explicit sync
109 TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
110 STATX_BASIC_STATS, &buf), -1);
111 TEST_COMPARE (errno, EINVAL);
113 TEST_COMPARE (impl (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
114 STATX_BASIC_STATS, &buf), -1);
115 TEST_COMPARE (errno, EINVAL);
122 int fd = create_temp_file ("tst-statx-", &path);
123 TEST_VERIFY_EXIT (fd >= 0);
124 support_write_file_string (path, "abc");
126 both_implementations_tests (&statx, path, fd);
127 both_implementations_tests (&statx_generic, path, fd);
129 if (kernel_supports_statx ())
131 puts ("info: kernel supports statx");
134 TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_FORCE_SYNC,
135 STATX_BASIC_STATS, &buf),
137 TEST_COMPARE (buf.stx_size, 3);
139 TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH | AT_STATX_DONT_SYNC,
140 STATX_BASIC_STATS, &buf),
142 TEST_COMPARE (buf.stx_size, 3);
146 puts ("info: kernel does not support statx");
147 non_kernel_tests (&statx, fd);
149 non_kernel_tests (&statx_generic, fd);
157 #include <support/test-driver.c>