GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], [email protected],
	"Thomas Weißschuh" <[email protected]>,
	"Zhangjin Wu" <[email protected]>, "Willy Tarreau" <[email protected]>,
	"Paul E . McKenney" <[email protected]>
Subject: [PATCH v2 nolibc 36/53] tools/nolibc: support nanoseconds in stat()
Date: Mon, 12 Jun 2023 13:44:57 -0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <8b757cc0-3719-4e63-a755-9710384137bc@paulmck-laptop>

From: Thomas Weißschuh <[email protected]>

Keep backwards compatibility through unions.

The compatibility macros like

 #define st_atime st_atim.tv_sec

as documented in stat(3type) don't work for nolibc because it would
break with other stat-like structures that contain the field st_atime.

The stx_atime, stx_mtime, stx_ctime are in type of 'struct
statx_timestamp', which is incompatible with 'struct timespec', should
be converted explicitly.

    /* include/uapi/linux/stat.h */

    struct statx_timestamp {
    	__s64	tv_sec;
    	__u32	tv_nsec;
    	__s32	__reserved;
    };

    /* include/uapi/linux/time.h */
    struct timespec {
    	__kernel_old_time_t	tv_sec;		/* seconds */
    	long			tv_nsec;	/* nanoseconds */
    };

Signed-off-by: Thomas Weißschuh <[email protected]>
Link: https://lore.kernel.org/linux-riscv/[email protected]/
Co-authored-by: Zhangjin Wu <[email protected]>
Signed-off-by: Zhangjin Wu <[email protected]>
[wt: squashed Zhangjin & Thomas' patches into one to preserve "bisectability"]
Signed-off-by: Willy Tarreau <[email protected]>
Signed-off-by: Paul E. McKenney <[email protected]>
---
 tools/include/nolibc/sys.h                   | 66 +++++++++++---------
 tools/include/nolibc/types.h                 |  6 +-
 tools/testing/selftests/nolibc/nolibc-test.c | 23 +++++++
 3 files changed, 62 insertions(+), 33 deletions(-)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index c688b410f9e4..7836d7e7760d 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -1161,23 +1161,26 @@ int sys_stat(const char *path, struct stat *buf)
 	long ret;
 
 	ret = sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx);
-	buf->st_dev     = ((statx.stx_dev_minor & 0xff)
-			  | (statx.stx_dev_major << 8)
-			  | ((statx.stx_dev_minor & ~0xff) << 12));
-	buf->st_ino     = statx.stx_ino;
-	buf->st_mode    = statx.stx_mode;
-	buf->st_nlink   = statx.stx_nlink;
-	buf->st_uid     = statx.stx_uid;
-	buf->st_gid     = statx.stx_gid;
-	buf->st_rdev    = ((statx.stx_rdev_minor & 0xff)
-			  | (statx.stx_rdev_major << 8)
-			  | ((statx.stx_rdev_minor & ~0xff) << 12));
-	buf->st_size    = statx.stx_size;
-	buf->st_blksize = statx.stx_blksize;
-	buf->st_blocks  = statx.stx_blocks;
-	buf->st_atime   = statx.stx_atime.tv_sec;
-	buf->st_mtime   = statx.stx_mtime.tv_sec;
-	buf->st_ctime   = statx.stx_ctime.tv_sec;
+	buf->st_dev          = ((statx.stx_dev_minor & 0xff)
+			       | (statx.stx_dev_major << 8)
+			       | ((statx.stx_dev_minor & ~0xff) << 12));
+	buf->st_ino          = statx.stx_ino;
+	buf->st_mode         = statx.stx_mode;
+	buf->st_nlink        = statx.stx_nlink;
+	buf->st_uid          = statx.stx_uid;
+	buf->st_gid          = statx.stx_gid;
+	buf->st_rdev         = ((statx.stx_rdev_minor & 0xff)
+			       | (statx.stx_rdev_major << 8)
+			       | ((statx.stx_rdev_minor & ~0xff) << 12));
+	buf->st_size         = statx.stx_size;
+	buf->st_blksize      = statx.stx_blksize;
+	buf->st_blocks       = statx.stx_blocks;
+	buf->st_atim.tv_sec  = statx.stx_atime.tv_sec;
+	buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec;
+	buf->st_mtim.tv_sec  = statx.stx_mtime.tv_sec;
+	buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
+	buf->st_ctim.tv_sec  = statx.stx_ctime.tv_sec;
+	buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
 	return ret;
 }
 #else
@@ -1195,19 +1198,22 @@ int sys_stat(const char *path, struct stat *buf)
 #else
 #error Neither __NR_newfstatat nor __NR_stat defined, cannot implement sys_stat()
 #endif
-	buf->st_dev     = stat.st_dev;
-	buf->st_ino     = stat.st_ino;
-	buf->st_mode    = stat.st_mode;
-	buf->st_nlink   = stat.st_nlink;
-	buf->st_uid     = stat.st_uid;
-	buf->st_gid     = stat.st_gid;
-	buf->st_rdev    = stat.st_rdev;
-	buf->st_size    = stat.st_size;
-	buf->st_blksize = stat.st_blksize;
-	buf->st_blocks  = stat.st_blocks;
-	buf->st_atime   = stat.st_atime;
-	buf->st_mtime   = stat.st_mtime;
-	buf->st_ctime   = stat.st_ctime;
+	buf->st_dev          = stat.st_dev;
+	buf->st_ino          = stat.st_ino;
+	buf->st_mode         = stat.st_mode;
+	buf->st_nlink        = stat.st_nlink;
+	buf->st_uid          = stat.st_uid;
+	buf->st_gid          = stat.st_gid;
+	buf->st_rdev         = stat.st_rdev;
+	buf->st_size         = stat.st_size;
+	buf->st_blksize      = stat.st_blksize;
+	buf->st_blocks       = stat.st_blocks;
+	buf->st_atim.tv_sec  = stat.st_atime;
+	buf->st_atim.tv_nsec = stat.st_atime_nsec;
+	buf->st_mtim.tv_sec  = stat.st_mtime;
+	buf->st_mtim.tv_nsec = stat.st_mtime_nsec;
+	buf->st_ctim.tv_sec  = stat.st_ctime;
+	buf->st_ctim.tv_nsec = stat.st_ctime_nsec;
 	return ret;
 }
 #endif
diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h
index 15b0baffd336..f96e28bff4ba 100644
--- a/tools/include/nolibc/types.h
+++ b/tools/include/nolibc/types.h
@@ -198,9 +198,9 @@ struct stat {
 	off_t     st_size;    /* total size, in bytes */
 	blksize_t st_blksize; /* blocksize for file system I/O */
 	blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
-	time_t    st_atime;   /* time of last access */
-	time_t    st_mtime;   /* time of last modification */
-	time_t    st_ctime;   /* time of last status change */
+	union { time_t st_atime; struct timespec st_atim; }; /* time of last access */
+	union { time_t st_mtime; struct timespec st_mtim; }; /* time of last modification */
+	union { time_t st_ctime; struct timespec st_ctim; }; /* time of last status change */
 };
 
 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 84a1b02eb6f9..0d76790ffb0d 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -501,6 +501,28 @@ static int test_fork(void)
 	}
 }
 
+static int test_stat_timestamps(void)
+{
+	struct stat st;
+
+	if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime))
+		return 1;
+
+	if (stat("/proc/self/", &st))
+		return 1;
+
+	if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
+		return 1;
+
+	if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
+		return 1;
+
+	if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
+		return 1;
+
+	return 0;
+}
+
 /* Run syscall tests between IDs <min> and <max>.
  * Return 0 on success, non-zero on failure.
  */
@@ -589,6 +611,7 @@ int run_syscall(int min, int max)
 		CASE_TEST(select_fault);      EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
 		CASE_TEST(stat_blah);         EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
 		CASE_TEST(stat_fault);        EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
+		CASE_TEST(stat_timestamps);   EXPECT_SYSZR(1, test_stat_timestamps()); break;
 		CASE_TEST(symlink_root);      EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
 		CASE_TEST(unlink_root);       EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
 		CASE_TEST(unlink_blah);       EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
-- 
2.40.1


  parent reply	other threads:[~2023-06-12 20:45 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-18 14:55 [PATCH nolibc 0/15] nolibc updates for v6.5] Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 01/15] tools/nolibc: tests: use volatile to force stack smashing Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 02/15] tools/nolibc: tests: fix build on non-c99 compliant compilers Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 03/15] tools/nolibc: fix build of the test case using glibc Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 04/15] tools/nolibc: add libc-test binary Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 05/15] tools/nolibc: add wrapper for memfd_create Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 06/15] tools/nolibc: implement fd-based FILE streams Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 07/15] tools/nolibc: add testcases for vfprintf Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 08/15] tools/nolibc: Fix build of stdio.h due to header ordering Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 09/15] tools/nolibc: use standard __asm__ statements Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 10/15] tools/nolibc: use __inline__ syntax Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 11/15] tools/nolibc: use C89 comment syntax Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 12/15] tools/nolibc: validate C89 compatibility Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 13/15] tools/nolibc: s390: provide custom implementation for sys_fork Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 14/15] tools/nolibc: add testcase for fork()/waitpid() Paul E. McKenney
2023-05-18 14:55 ` [PATCH v2 nolibc 15/15] tools/nolibc: remove LINUX_REBOOT_ constants Paul E. McKenney
2023-06-12 20:44 ` [PATCH v2 nolibc 0/15] nolibc updates for v6.5] Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 01/53] tools/nolibc: tests: use volatile to force stack smashing Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 02/53] tools/nolibc: tests: fix build on non-c99 compliant compilers Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 03/53] tools/nolibc: fix build of the test case using glibc Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 04/53] tools/nolibc: add libc-test binary Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 05/53] tools/nolibc: add wrapper for memfd_create Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 06/53] tools/nolibc: implement fd-based FILE streams Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 07/53] tools/nolibc: add testcases for vfprintf Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 08/53] tools/nolibc: Fix build of stdio.h due to header ordering Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 09/53] tools/nolibc: use standard __asm__ statements Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 10/53] tools/nolibc: use __inline__ syntax Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 11/53] tools/nolibc: use C89 comment syntax Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 12/53] tools/nolibc: validate C89 compatibility Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 13/53] tools/nolibc: s390: provide custom implementation for sys_fork Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 14/53] tools/nolibc: add testcase for fork()/waitpid() Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 15/53] tools/nolibc: remove LINUX_REBOOT_ constants Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 16/53] tools/nolibc: riscv: Fix up load/store instructions for rv32 Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 17/53] tools/nolibc/unistd: add syscall() Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 18/53] selftests/nolibc: syscall_args: use generic __NR_statx Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 19/53] selftests/nolibc: reduce syscalls during space padding Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 20/53] tools/nolibc: aarch64: add stackprotector support Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 21/53] tools/nolibc: arm: " Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 22/53] tools/nolibc: loongarch: " Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 23/53] tools/nolibc: mips: " Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 24/53] tools/nolibc: riscv: " Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 25/53] tools/nolibc: fix typo pint -> point Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 26/53] tools/nolibc: x86_64: disable stack protector for _start Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 27/53] tools/nolibc: ensure stack protector guard is never zero Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 28/53] tools/nolibc: add test for __stack_chk_guard initialization Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 29/53] tools/nolibc: reformat list of headers to be installed Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 30/53] tools/nolibc: add autodetection for stackprotector support Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 31/53] tools/nolibc: simplify stackprotector compiler flags Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 32/53] tools/nolibc: fix segfaults on compilers without attribute no_stack_protector Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 33/53] tools/nolibc: s390: disable stackprotector in _start Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 34/53] tools/nolibc: add support for prctl() Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 35/53] selftests/nolibc: prevent coredumps during test execution Paul E. McKenney
2023-06-12 20:44   ` Paul E. McKenney [this message]
2023-06-12 20:44   ` [PATCH v2 nolibc 37/53] selftests/nolibc: print name instead of number for EOVERFLOW Paul E. McKenney
2023-06-12 20:44   ` [PATCH v2 nolibc 38/53] selftests/nolibc: remove the duplicated gettimeofday_bad2 Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 39/53] tools/nolibc: ppoll/ppoll_time64: add a missing argument Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 40/53] selftests/nolibc: test_fork: fix up duplicated print Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 41/53] tools/nolibc: ensure fast64 integer types have 64 bits Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 42/53] selftests/nolibc: remove test gettimeofday_null Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 43/53] selftests/nolibc: allow specify extra arguments for qemu Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 44/53] selftests/nolibc: fix up compile warning with glibc on x86_64 Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 45/53] selftests/nolibc: not include limits.h for nolibc Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 46/53] selftests/nolibc: use INT_MAX instead of __INT_MAX__ Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 47/53] tools/nolibc: arm: add missing my_syscall6 Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 48/53] tools/nolibc: open: fix up compile warning for arm Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 49/53] selftests/nolibc: support two errnos with EXPECT_SYSER2() Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 50/53] selftests/nolibc: remove gettimeofday_bad1/2 completely Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 51/53] selftests/nolibc: add new gettimeofday test cases Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 52/53] selftests/nolibc: also count skipped and failed tests in output Paul E. McKenney
2023-06-12 20:45   ` [PATCH v2 nolibc 53/53] selftests/nolibc: make sure gcc always use little endian on MIPS Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox