Commit 87b9fa66 authored by Thomas Weißschuh's avatar Thomas Weißschuh Committed by Paul E. McKenney

tools/nolibc: support nanoseconds in stat()

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: default avatarThomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/linux-riscv/3a3edd48-1ace-4c89-89e8-9c594dd1b3c9@t-8ch.de/Co-authored-by: default avatarZhangjin Wu <falcon@tinylab.org>
Signed-off-by: default avatarZhangjin Wu <falcon@tinylab.org>
[wt: squashed Zhangjin & Thomas' patches into one to preserve "bisectability"]
Signed-off-by: default avatarWilly Tarreau <w@1wt.eu>
Signed-off-by: default avatarPaul E. McKenney <paulmck@kernel.org>
parent 9a75575b
......@@ -1175,9 +1175,12 @@ int sys_stat(const char *path, struct stat *buf)
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_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
......@@ -1205,9 +1208,12 @@ int sys_stat(const char *path, struct stat *buf)
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_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
......
......@@ -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 */
......
......@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment