[PATCH v2] gen_init_cpio: Ignore fsync() returning EINVAL on pipes

Dmitry Safonov via B4 Relay posted 1 patch 4 months ago
usr/gen_init_cpio.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH v2] gen_init_cpio: Ignore fsync() returning EINVAL on pipes
Posted by Dmitry Safonov via B4 Relay 4 months ago
From: Dmitry Safonov <dima@arista.com>

The reproducer:
echo | ./usr/gen_init_cpio /dev/stdin > /dev/null

fsync() on a pipe fd returns -EINVAL, which makes gen_init_cpio fail.
Ignore -EINVAL from fsync().

Fixes: ae18b94099b0 ("gen_init_cpio: support -o <output_file> parameter")
Cc: David Disseldorp <ddiss@suse.de>
Cc: Nicolas Schier <nsc@kernel.org>
Signed-off-by: Dmitry Safonov <dima@arista.com>
Reviewed-by: David Disseldorp <ddiss@suse.de>
---
Changes in v2:
- Instead of isfdtype() just ignore -EINVAL from fsync() (Suggested by David)
- Add minimal reproducer and shrink the patch description (David)
- Link to v1: https://lore.kernel.org/r/20251007-gen_init_cpio-pipe-v1-1-d782674d4926@arista.com
---
 usr/gen_init_cpio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
index 75e9561ba31392e12536e76a918245a8ea07f9b8..b7296edc6626fb0cf7c709082797d1e6a7376d6e 100644
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -112,7 +112,10 @@ static int cpio_trailer(void)
 	    push_pad(padlen(offset, 512)) < 0)
 		return -1;
 
-	return fsync(outfd);
+	if (fsync(outfd) < 0 && errno != EINVAL)
+		return -1;
+
+	return 0;
 }
 
 static int cpio_mkslink(const char *name, const char *target,

---
base-commit: c746c3b5169831d7fb032a1051d8b45592ae8d78
change-id: 20251007-gen_init_cpio-pipe-5ad87f616a40

Best regards,
-- 
Dmitry Safonov <dima@arista.com>
Re: [PATCH v2] gen_init_cpio: Ignore fsync() returning EINVAL on pipes
Posted by Nathan Chancellor 4 months ago
On Tue, 07 Oct 2025 04:46:47 +0100, Dmitry Safonov wrote:
> The reproducer:
> echo | ./usr/gen_init_cpio /dev/stdin > /dev/null
> 
> fsync() on a pipe fd returns -EINVAL, which makes gen_init_cpio fail.
> Ignore -EINVAL from fsync().
> 
> 
> [...]

Applied, thanks!

[1/1] gen_init_cpio: Ignore fsync() returning EINVAL on pipes
      https://git.kernel.org/kbuild/c/38492c5743f8b

Best regards,
-- 
Nathan Chancellor <nathan@kernel.org>