Re: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
From: Zhu Yanjun
Date: Sun Apr 05 2026 - 12:31:05 EST
在 2026/4/5 0:34, Pratyush Yadav 写道:
On Sat, Apr 04 2026, Zhu Yanjun wrote:
在 2026/4/4 3:24, Pratyush Yadav 写道:I don't think it is needed. bytes_read will always be smaller than or
From: "Pratyush Yadav (Google)" <pratyush@xxxxxxxxxx>while (remain > 0) ?
Add some helper functions that will be used by memfd tests. This moves
some of the complexity out of the test itself, which results in better
test readability and less code duplication.
Reviewed-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
Signed-off-by: Pratyush Yadav <ptyadav@xxxxxxxxx>
Signed-off-by: Pratyush Yadav (Google) <pratyush@xxxxxxxxxx>
---
.../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++-
.../selftests/liveupdate/luo_test_utils.h | 10 +
2 files changed, 200 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c
b/tools/testing/selftests/liveupdate/luo_test_utils.c
index 3c8721c505df..ceb918ef9813 100644
--- a/tools/testing/selftests/liveupdate/luo_test_utils.c
+++ b/tools/testing/selftests/liveupdate/luo_test_utils.c
@@ -1,8 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * Copyright (c) 2025, Google LLC.
+ * Copyright (c) 2025-2026, Google LLC.
* Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
+ * Pratyush Yadav (Google) <pratyush@xxxxxxxxxx>
+ *
+ * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
+ * Pratyush Yadav <ptyadav@xxxxxxxxx>
*/
#define _GNU_SOURCE
@@ -20,9 +24,194 @@
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
+#include <sys/vfs.h>
+#include <linux/magic.h>
#include "luo_test_utils.h"
+int cwd_is_tmpfs(void)
+{
+ struct statfs buf;
+
+ if (statfs(".", &buf) < 0)
+ return -errno;
+
+ return buf.f_type == TMPFS_MAGIC;
+}
+
+/* Read exactly specified size from fd. Any less results in error. */
+int read_size(int fd, char *buffer, size_t size)
+{
+ size_t remain = size;
+ ssize_t bytes_read;
+
+ while (remain) {
equal to remain, so remain will never go below 0 anyway. And while
(remain) is nicer to read.
If a wrong number is transferred, this makes remain less than 0. Then this loop will run again and again.
This while (remain > 0) will avoid this kind of scenario.
ZhuYanjun
--
[...]+ bytes_read = read(fd, buffer, remain);
+ if (bytes_read == 0)
+ return -ENODATA;
+ if (bytes_read < 0) {
+ if (errno == EINTR)
+ continue;
+ else
+ return -errno;
+ }
+
+ remain -= bytes_read;
+ buffer += bytes_read;
+ }
+
+ return 0;
+}
+
Best Regards,
Yanjun.Zhu