1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-13 12:58:20 +03:00

Merge pull request #20763 from poettering/fileio-test

test-fileio: test read_virtual_file() with more files from /proc
This commit is contained in:
Lennart Poettering 2021-09-16 17:47:03 +02:00 committed by GitHub
commit 281752e406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -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;
}

View File

@ -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);
}