From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 7014C1FF2CA
	for <inbox@lore.proxmox.com>; Tue, 23 Jul 2024 10:28:56 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 84E133E759;
	Tue, 23 Jul 2024 10:29:29 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 23 Jul 2024 10:29:25 +0200
Message-Id: <20240723082925.934603-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.016 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [sysfstools.pm]
Subject: [pve-devel] [RFC PATCH common] sysfstools: file_write: properly
 catch errors
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

since `print` is doing buffered IO, we don't always get an error there,
even if the underlying write does not work.

To properly catch that, do an unbuffered `syswrite` which circumvents
all buffers and writes directly to the file handle.

We aren't actually interested in the specific error here, but only if
the write was successful or not.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---

Note: this is heavily used when doing PCI passthrough, and it seems we
did not catch as many errors as we thought. Maybe we should introduce an
'noerr' parameter and pass that on all current code paths, since i fear
that there are many situations where the sysfs write had failed but it
still works because we ignored it and qemu/the kernel do the right thing
anyway.

 src/PVE/SysFSTools.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/PVE/SysFSTools.pm b/src/PVE/SysFSTools.pm
index 57f0ac8..8eb9f2e 100644
--- a/src/PVE/SysFSTools.pm
+++ b/src/PVE/SysFSTools.pm
@@ -198,7 +198,7 @@ sub file_write {
     my $fh = IO::File->new($filename, "w");
     return undef if !$fh;
 
-    my $res = print $fh $buf;
+    my $res = defined(syswrite($fh, $buf)) ? 1 : 0;
 
     $fh->close();
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel