From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 235265A027C for ; Wed, 28 Feb 2024 02:52:18 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709085128; bh=naVu2Unl/N53XjbS/LPykBhzJ4oBG7PRWFVkEKIAoU4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gMeSx8empSls4tee5R3FHf61eY2a0wXaGEPxmQMCRT05gn89ldw6jQ9p+tl93OBtW NKUxKMjaWZHP28jNeTdDqmvUlPvwTl8Nm023M/KoxslY2RabDoYXsaumxQ0rPRPm83 yuQ06GfND/OJA9LFoxxiEY3hoVvnPTBKyyxGpiDWlk7bvicCkc9XWOi9ozSgmpv7EO 6JZtQSh1Kl7kRFnOABfNumbh/pyfC2XGutsk5X1Aqhl5LoZlVZLFay0hDCFHQvKOwg hSkYwDHzreulp0DKBTwFEuMwwHk1aAy/jlUP7fOhc61g1Fvfbj//rOHRjMiNsUSjvg z57tKfo2YcXFQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tky5h6XBdz4wyf; Wed, 28 Feb 2024 12:52:08 +1100 (AEDT) From: David Gibson To: Laurent Vivier , passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 5/7] pcap: Handle short writes in pcap_frame() Date: Wed, 28 Feb 2024 12:52:04 +1100 Message-ID: <20240228015206.1214242-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240228015206.1214242-1-david@gibson.dropbear.id.au> References: <20240228015206.1214242-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: KITUQ24Q46N6VBGDMRYDMQKAI5HINFJF X-Message-ID-Hash: KITUQ24Q46N6VBGDMRYDMQKAI5HINFJF X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Currently pcap_frame() assumes that if write() doesn't return an error, it has written everything we want. That's not necessarily true, because it could return a short write. That's not likely to happen on a regular file, but there's not a lot of reason not to be robust here; it's conceivable we might want to direct the pcap fd at a named pipe or similar. So, make pcap_frame() handle short frames by using the write_remainder() helper. Signed-off-by: David Gibson --- pcap.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/pcap.c b/pcap.c index 5cd7a8cc..317fc844 100644 --- a/pcap.c +++ b/pcap.c @@ -77,15 +77,18 @@ static void pcap_frame(const struct iovec *iov, size_t offset, const struct timeval *tv) { size_t len = iov->iov_len - offset; - struct pcap_pkthdr h; - - h.tv_sec = tv->tv_sec; - h.tv_usec = tv->tv_usec; - h.caplen = h.len = len; - - if (write(pcap_fd, &h, sizeof(h)) < 0 || - write(pcap_fd, (char *)iov->iov_base + offset, len) < 0) - debug("Cannot log packet, length %zu", len); + struct pcap_pkthdr h = { + .tv_sec = tv->tv_sec, + .tv_usec = tv->tv_usec, + .caplen = len, + .len = len + }; + struct iovec hiov = { &h, sizeof(h) }; + + if (write_remainder(pcap_fd, &hiov, 1, 0) < 0 || + write_remainder(pcap_fd, iov, 1, offset) < 0) + debug("Cannot log packet, length %zu: %s", + len, strerror(errno)); } /** -- 2.43.2