df98bb
From 8df650c7c5adc2bb24a0077d8332f5ee342e7fd8 Mon Sep 17 00:00:00 2001
df98bb
From: Lennart Poettering <lennart@poettering.net>
df98bb
Date: Fri, 26 Feb 2021 10:25:24 +0100
df98bb
Subject: [PATCH] copy: handle copy_file_range() weirdness on procfs/sysfs
df98bb
df98bb
This addresses the issue described in https://lwn.net/Articles/846403/
df98bb
and makes sure we will be able to stream bytes from procfs/sysfs via
df98bb
copy_bytes() if people ask us to.
df98bb
df98bb
Based on: ee1aa61c4710ae567a2b844e0f0bb8cb0456ab8c
df98bb
Related: #1970860
df98bb
---
df98bb
 src/basic/copy.c     | 75 +++++++++++++++++++++++++++++---------------
df98bb
 src/test/test-copy.c | 17 ++++++++++
df98bb
 2 files changed, 66 insertions(+), 26 deletions(-)
df98bb
df98bb
diff --git a/src/basic/copy.c b/src/basic/copy.c
df98bb
index e06a503a29..a48c42c5c6 100644
df98bb
--- a/src/basic/copy.c
df98bb
+++ b/src/basic/copy.c
df98bb
@@ -92,7 +92,7 @@ int copy_bytes_full(
df98bb
                 void **ret_remains,
df98bb
                 size_t *ret_remains_size) {
df98bb
 
df98bb
-        bool try_cfr = true, try_sendfile = true, try_splice = true;
df98bb
+        bool try_cfr = true, try_sendfile = true, try_splice = true, copied_something = false;
df98bb
         int r, nonblock_pipe = -1;
df98bb
         size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
df98bb
 
df98bb
@@ -185,9 +185,20 @@ int copy_bytes_full(
df98bb
 
df98bb
                                 try_cfr = false;
df98bb
                                 /* use fallback below */
df98bb
-                        } else if (n == 0) /* EOF */
df98bb
-                                break;
df98bb
-                        else
df98bb
+                        } else if (n == 0) { /* likely EOF */
df98bb
+
df98bb
+                                if (copied_something)
df98bb
+                                        break;
df98bb
+
df98bb
+                                /* So, we hit EOF immediately, without having copied a single byte. This
df98bb
+                                 * could indicate two things: the file is actually empty, or we are on some
df98bb
+                                 * virtual file system such as procfs/sysfs where the syscall actually
df98bb
+                                 * doesn't work but doesn't return an error. Try to handle that, by falling
df98bb
+                                 * back to simple read()s in case we encounter empty files.
df98bb
+                                 *
df98bb
+                                 * See: https://lwn.net/Articles/846403/ */
df98bb
+                                try_cfr = try_sendfile = try_splice = false;
df98bb
+                        } else
df98bb
                                 /* Success! */
df98bb
                                 goto next;
df98bb
                 }
df98bb
@@ -201,9 +212,14 @@ int copy_bytes_full(
df98bb
 
df98bb
                                 try_sendfile = false;
df98bb
                                 /* use fallback below */
df98bb
-                        } else if (n == 0) /* EOF */
df98bb
+                        } else if (n == 0) { /* likely EOF */
df98bb
+
df98bb
+                                if (copied_something)
df98bb
+                                        break;
df98bb
+
df98bb
+                                try_sendfile = try_splice = false; /* same logic as above for copy_file_range() */
df98bb
                                 break;
df98bb
-                        else
df98bb
+                        } else
df98bb
                                 /* Success! */
df98bb
                                 goto next;
df98bb
                 }
df98bb
@@ -213,14 +229,14 @@ int copy_bytes_full(
df98bb
 
df98bb
                         /* splice()'s asynchronous I/O support is a bit weird. When it encounters a pipe file
df98bb
                          * descriptor, then it will ignore its O_NONBLOCK flag and instead only honour the
df98bb
-                         * SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour here, and
df98bb
-                         * check if either of the specified fds are a pipe, and if so, let's pass the flag
df98bb
-                         * automatically, depending on O_NONBLOCK being set.
df98bb
+                         * SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour
df98bb
+                         * here, and check if either of the specified fds are a pipe, and if so, let's pass
df98bb
+                         * the flag automatically, depending on O_NONBLOCK being set.
df98bb
                          *
df98bb
-                         * Here's a twist though: when we use it to move data between two pipes of which one has
df98bb
-                         * O_NONBLOCK set and the other has not, then we have no individual control over O_NONBLOCK
df98bb
-                         * behaviour. Hence in that case we can't use splice() and still guarantee systematic
df98bb
-                         * O_NONBLOCK behaviour, hence don't. */
df98bb
+                         * Here's a twist though: when we use it to move data between two pipes of which one
df98bb
+                         * has O_NONBLOCK set and the other has not, then we have no individual control over
df98bb
+                         * O_NONBLOCK behaviour. Hence in that case we can't use splice() and still guarantee
df98bb
+                         * systematic O_NONBLOCK behaviour, hence don't. */
df98bb
 
df98bb
                         if (nonblock_pipe < 0) {
df98bb
                                 int a, b;
df98bb
@@ -238,12 +254,13 @@ int copy_bytes_full(
df98bb
                                     (a == FD_IS_BLOCKING_PIPE && b == FD_IS_NONBLOCKING_PIPE) ||
df98bb
                                     (a == FD_IS_NONBLOCKING_PIPE && b == FD_IS_BLOCKING_PIPE))
df98bb
 
df98bb
-                                        /* splice() only works if one of the fds is a pipe. If neither is, let's skip
df98bb
-                                         * this step right-away. As mentioned above, if one of the two fds refers to a
df98bb
-                                         * blocking pipe and the other to a non-blocking pipe, we can't use splice()
df98bb
-                                         * either, hence don't try either. This hence means we can only use splice() if
df98bb
-                                         * either only one of the two fds is a pipe, or if both are pipes with the same
df98bb
-                                         * nonblocking flag setting. */
df98bb
+                                        /* splice() only works if one of the fds is a pipe. If neither is,
df98bb
+                                         * let's skip this step right-away. As mentioned above, if one of the
df98bb
+                                         * two fds refers to a blocking pipe and the other to a non-blocking
df98bb
+                                         * pipe, we can't use splice() either, hence don't try either. This
df98bb
+                                         * hence means we can only use splice() if either only one of the two
df98bb
+                                         * fds is a pipe, or if both are pipes with the same nonblocking flag
df98bb
+                                         * setting. */
df98bb
 
df98bb
                                         try_splice = false;
df98bb
                                 else
df98bb
@@ -259,9 +276,13 @@ int copy_bytes_full(
df98bb
 
df98bb
                                 try_splice = false;
df98bb
                                 /* use fallback below */
df98bb
-                        } else if (n == 0) /* EOF */
df98bb
-                                break;
df98bb
-                        else
df98bb
+                        } else if (n == 0) { /* likely EOF */
df98bb
+
df98bb
+                                if (copied_something)
df98bb
+                                        break;
df98bb
+
df98bb
+                                try_splice = false; /* same logic as above for copy_file_range() + sendfile() */
df98bb
+                        } else
df98bb
                                 /* Success! */
df98bb
                                 goto next;
df98bb
                 }
df98bb
@@ -312,11 +333,13 @@ int copy_bytes_full(
df98bb
                         assert(max_bytes >= (uint64_t) n);
df98bb
                         max_bytes -= n;
df98bb
                 }
df98bb
-                /* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
df98bb
-                 * so reduce our maximum by the amount we already copied,
df98bb
-                 * but don't go below our copy buffer size, unless we are
df98bb
-                 * close the limit of bytes we are allowed to copy. */
df98bb
+
df98bb
+                /* sendfile accepts at most SSIZE_MAX-offset bytes to copy, so reduce our maximum by the
df98bb
+                 * amount we already copied, but don't go below our copy buffer size, unless we are close the
df98bb
+                 * limit of bytes we are allowed to copy. */
df98bb
                 m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
df98bb
+
df98bb
+                copied_something = true;
df98bb
         }
df98bb
 
df98bb
         return 0; /* return 0 if we hit EOF earlier than the size limit */
df98bb
diff --git a/src/test/test-copy.c b/src/test/test-copy.c
df98bb
index 2e8d251ac1..29ac33e47a 100644
df98bb
--- a/src/test/test-copy.c
df98bb
+++ b/src/test/test-copy.c
df98bb
@@ -253,6 +253,22 @@ static void test_copy_atomic(void) {
df98bb
         assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, COPY_REPLACE) >= 0);
df98bb
 }
df98bb
 
df98bb
+static void test_copy_proc(void) {
df98bb
+        _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
df98bb
+        _cleanup_free_ char *f = NULL, *a = NULL, *b = NULL;
df98bb
+
df98bb
+        /* Check if copying data from /proc/ works correctly, i.e. let's see if https://lwn.net/Articles/846403/ is a problem for us */
df98bb
+
df98bb
+        assert_se(mkdtemp_malloc(NULL, &p) >= 0);
df98bb
+        assert_se(f = path_join(NULL, p, "version"));
df98bb
+        assert_se(copy_file("/proc/version", f, 0, (mode_t) -1, 0, 0) >= 0);
df98bb
+
df98bb
+        assert_se(read_one_line_file("/proc/version", &a) >= 0);
df98bb
+        assert_se(read_one_line_file(f, &b) >= 0);
df98bb
+        assert_se(streq(a, b));
df98bb
+        assert_se(strlen(a) > 0);
df98bb
+}
df98bb
+
df98bb
 int main(int argc, char *argv[]) {
df98bb
         log_set_max_level(LOG_DEBUG);
df98bb
 
df98bb
@@ -267,6 +283,7 @@ int main(int argc, char *argv[]) {
df98bb
         test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */
df98bb
         test_copy_bytes_regular_file(argv[0], true, 32000);
df98bb
         test_copy_atomic();
df98bb
+        test_copy_proc();
df98bb
 
df98bb
         return 0;
df98bb
 }