From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202408 header.b=OPQkCckz; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id A26565A004E for ; Wed, 18 Sep 2024 12:39:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202408; t=1726655972; bh=p9GYHusJhRd4vq/HOb3pFAcZYkqMzsP0vZpMQ1j0rXo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OPQkCckz0vln/GOfs6Gimhtnks+nJB9NsYL6q1enez9DueONGjl4SwgwkIBtGy0IX d84sKaZGlLxm5L4gdX1utyJ1zbZ65I4y0IDjLvWvt114QHcV3nizUdX0ntXpIJEN8u DRSJ+2kwc4+9+kT63V0Kj9G2PN9GRChttxmAhDgsJcskbUOq68qpyZqqlp29aQ1TEW 5OXjjUlL3NUsM+cY3IavIu1EqcbEY0KhaC+86LtDvN4R3QTOSSqSZvsBeK/MW/lhEO HxBIwg51h9l2VAVGYHRBDnMk2vGs2tP9TdxwQ3vfm1i/Ww9ut/pVxXbAGhj9tBLC0n 0pG2924bONH2A== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4X7wBX6BHCz4x42; Wed, 18 Sep 2024 20:39:32 +1000 (AEST) Date: Wed, 18 Sep 2024 20:22:59 +1000 From: David Gibson To: Markus Armbruster Subject: Re: [PATCH v2 1/2] util: Add helper to write() all of a buffer Message-ID: References: <20240917064555.1040365-1-david@gibson.dropbear.id.au> <20240917064555.1040365-2-david@gibson.dropbear.id.au> <878qvp4huh.fsf@pond.sub.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="6lI+IJt23V7S2ot8" Content-Disposition: inline In-Reply-To: <878qvp4huh.fsf@pond.sub.org> Message-ID-Hash: Q6DJX5FFWUU4F2PKMXDE5PQ4QLD3HUWR X-Message-ID-Hash: Q6DJX5FFWUU4F2PKMXDE5PQ4QLD3HUWR 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: passt-dev@passt.top, Stefano Brivio 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: --6lI+IJt23V7S2ot8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Sep 18, 2024 at 08:05:26AM +0200, Markus Armbruster wrote: > David Gibson writes: >=20 > > write(2) might not write all the data it is given. Add a write_all_buf= () > > helper to keep calling it until all the given data is written, or we ge= t an > > error. > > > > Currently we use write_remainder() to do this operation in pcap_frame(). > > That's a little awkward since it requires constructing an iovec, and fu= ture > > changes we want to make to write_remainder() will be easier in terms of > > this single buffer helper. > > > > Signed-off-by: David Gibson > > --- > > pcap.c | 3 +-- > > util.c | 25 +++++++++++++++++++++++++ > > util.h | 1 + > > 3 files changed, 27 insertions(+), 2 deletions(-) > > > > diff --git a/pcap.c b/pcap.c > > index 46cc4b0d..e6b5ced4 100644 > > --- a/pcap.c > > +++ b/pcap.c > > @@ -86,9 +86,8 @@ static void pcap_frame(const struct iovec *iov, size_= t iovcnt, > > .caplen =3D l2len, > > .len =3D l2len > > }; > > - struct iovec hiov =3D { &h, sizeof(h) }; > > =20 > > - if (write_remainder(pcap_fd, &hiov, 1, 0) < 0 || > > + if (write_all_buf(pcap_fd, &h, sizeof(h)) < 0 || > > write_remainder(pcap_fd, iov, iovcnt, offset) < 0) > > debug_perror("Cannot log packet, length %zu", l2len); > > } > > diff --git a/util.c b/util.c > > index eede4e58..7db7c2e7 100644 > > --- a/util.c > > +++ b/util.c > > @@ -582,6 +582,31 @@ int do_clone(int (*fn)(void *), char *stack_area, = size_t stack_size, int flags, > > #endif > > } > > =20 > > +/* write_all_buf() - write all of a buffer to an fd > > + * @fd: File descriptor > > + * @buf: Pointer to base of buffer > > + * @len: Length of buffer > > + * > > + * Return: 0 on success, -1 on error (with errno set) > > + * > > + * #syscalls write > > + */ > > +int write_all_buf(int fd, const void *buf, size_t len) >=20 > I'd call it write_all(), but that's strictly a matter of taste. I usually would too, but given we work pretty heavily with both plain buffers and iovs, I thought it was worth emphasising that this works on the former. > > +{ > > + const char *p =3D buf; > > + size_t left =3D len; > > + > > + while (left) { > > + ssize_t rc =3D write(fd, p, left); > > + > > + if (rc < 0) > > + return -1; >=20 > Loop when errno =3D=3D EINTR? Oh, good idea. --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --6lI+IJt23V7S2ot8 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmbqqeoACgkQzQJF27ox 2GdUHhAAgZQ8E1KcOIdlYVUoLjpcVJYuNnJ6yUKzsGUxqmsvraTpeJbCBKYKsrMZ AYSmFTx2pDYPUAoXQpR3J/zBgBAxlWzIva6qbLqz6/H0DiUyfG1a6Y3MvCllOEwl Lj6y+C+lz7xUIRoOjGNjosC1Rm/u4sMQNY8Ig+C6bXrNchQp0Ql4K3NyXlnFKSED oVkzyF2iFOy0s4IDOTfnoLdpq28adnW7LFhRBlrJZpTKG8bhwp83uRKPJs2OXqTi xTJoNytHx0by7kLCb6oNqARbyTTXbop4sdiYBi/UDCczWiGAqoc5AiC1bJ8MuH1x q9jkNMsFAAVm2k13jQxK5BuOSADRq8J353WjLaqwOiF/f/s3jSacgVUm7A8mnzOh yZahxgmp1segGaUsNMFSoErmGx1ZqQtipMvVXTuDikiDwnw+LsHeQReXzoPS0Aux 9AmlJpNQCfXl/gz4s8A+aOkORBzMpjQRohnNwaJWbKE/97RAVYWwW7CeiFz6gNwK GKe3AXsEgc3GWhRLeH1ZAgfgnyzNpqtxtn5SBlJfiSxyIpDMGkike74diiGque5L wPDsWHfgwDt5fgrytjCE1pECQ/mJl4RKczc0CeliNsLqQ26m6u7ffx5HdWzod8IU 0B111BnVyahoI/t3yS48nSXAK8TpdIF4i0Rwa4K+ObLFBaPMxY0= =BKV+ -----END PGP SIGNATURE----- --6lI+IJt23V7S2ot8--