summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-09-16 17:47:03 +0200
committerGitHub <noreply@github.com>2021-09-16 17:47:03 +0200
commit281752e4062f8fd3b3284548473109a43d7be506 (patch)
treec61e29e55aedaa53b50094ae4f8dde292e758af4
parentf0c5e8d4529577d24f7f5b7231fee6e592a50af0 (diff)
parentf3b751220bb842ce22a4f607d817f481f0961d40 (diff)
downloadsystemd-281752e4062f8fd3b3284548473109a43d7be506.tar.gz
Merge pull request #20763 from poettering/fileio-test
test-fileio: test read_virtual_file() with more files from /proc
-rw-r--r--src/basic/fileio.c11
-rw-r--r--src/test/test-fileio.c12
2 files changed, 18 insertions, 5 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index e69b888315..60a9885108 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -470,9 +470,14 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
if (n <= size)
break;
- /* If a maximum size is specified and we already read as much, no need to try again */
- if (max_size != SIZE_MAX && n >= max_size) {
- n = max_size;
+ /* If a maximum size is specified and we already read more we know the file is larger, and
+ * can handle this as truncation case. Note that if the size of what we read equals the
+ * maximum size then this doesn't mean truncation, the file might or might not end on that
+ * byte. We need to rerun the loop in that case, with a larger buffer size, so that we read
+ * at least one more byte to be able to distinguish EOF from truncation. */
+ if (max_size != SIZE_MAX && n > max_size) {
+ n = size; /* Make sure we never use more than what we sized the buffer for (so that
+ * we have one free byte in it for the trailing NUL we add below).*/
truncated = true;
break;
}
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index 2b6ef495aa..fa8a66df04 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -1047,7 +1047,11 @@ static void test_read_virtual_file(size_t max_size) {
FOREACH_STRING(filename,
"/proc/1/cmdline",
"/etc/nsswitch.conf",
- "/sys/kernel/uevent_seqnum") {
+ "/sys/kernel/uevent_seqnum",
+ "/proc/kcore",
+ "/proc/kallsyms",
+ "/proc/self/exe",
+ "/proc/self/pagemap") {
_cleanup_free_ char *buf = NULL;
size_t size = 0;
@@ -1055,7 +1059,11 @@ static void test_read_virtual_file(size_t max_size) {
r = read_virtual_file(filename, max_size, &buf, &size);
if (r < 0) {
log_info_errno(r, "read_virtual_file(\"%s\", %zu): %m", filename, max_size);
- assert_se(ERRNO_IS_PRIVILEGE(r) || r == -ENOENT);
+ assert_se(ERRNO_IS_PRIVILEGE(r) || /* /proc/kcore is not accessible to unpriv */
+ IN_SET(r,
+ -ENOENT, /* Some of the files might be absent */
+ -EINVAL, /* too small reads from /proc/self/pagemap trigger EINVAL */
+ -EFBIG)); /* /proc/kcore and /proc/self/pagemap should be too large */
} else
log_info("read_virtual_file(\"%s\", %zu): %s (%zu bytes)", filename, max_size, r ? "non-truncated" : "truncated", size);
}