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=202412 header.b=m6A0NYeL; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 8D7E55A0271 for ; Wed, 29 Jan 2025 05:00:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202412; t=1738123240; bh=6THs2Rlq46UiY/lWd/rMBGjTR108W0Hhdi7M+qIRA8w=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=m6A0NYeLA/a9QnEb0WPoc8sbmBHr1rtw0W2MLeCxjoOWQpZTHZbyBaaBwec6HV8Pt 1oqjhEHUONQrV9aV9RGqv6g0knFl8LDe1YUYsnQpql6DeZHDd4tS53OgDq4njYt8Fi KgJmUZf1Q2UdYTmlv/hYVblW4Dw/e97XZTBo8TInrptmaMFpuv9TM+UQAmqCiCsu6E NGysErnDTtT7PW1HHCv5HKpuzTnk+I9YTx6z1Teg10pD8o/66bKr6xcTxXZlS8aCOF SNOoiao6Xr0cbMUnAhoD+b8LrDncCcoNH6zx6YNbzYhowVHSJHfyIU0dXJzHx6earg bUclumffd9sDw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4YjT2w4cNkz4x3d; Wed, 29 Jan 2025 15:00:40 +1100 (AEDT) Date: Wed, 29 Jan 2025 12:37:40 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2 4/8] util: Add read_remainder() and read_all_buf() Message-ID: References: <20250128233940.1235855-1-sbrivio@redhat.com> <20250128233940.1235855-5-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Eps0HusX0V6BT3EO" Content-Disposition: inline In-Reply-To: <20250128233940.1235855-5-sbrivio@redhat.com> Message-ID-Hash: YBKDMHZNT6VAOBUIPDZE4BKGJBIJIIK7 X-Message-ID-Hash: YBKDMHZNT6VAOBUIPDZE4BKGJBIJIIK7 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, Laurent Vivier 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: --Eps0HusX0V6BT3EO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jan 29, 2025 at 12:39:36AM +0100, Stefano Brivio wrote: > These are symmetric to write_remainder() and write_all_buf() and > almost a copy and paste of them, with the most notable differences > being reversed reads/writes and a couple of better-safe-than-sorry > asserts to keep Coverity happy. >=20 > I'll use them in the next patch. At least for the moment, they're > going to be used for vhost-user mode only, so I'm not unconditionally > enabling readv() in the seccomp profile: the caller has to ensure it's > there. >=20 > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson I don't think I've ever encountered ENODATA before, so it seems a good choice. Other options would be a) 0 for success, -ve for read error, 1 for EOF b) -ve for read error, 0 for EOF, # of bytes for success (redundant, but harmless) but this is fine. > --- > util.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > util.h | 2 ++ > 2 files changed, 82 insertions(+) >=20 > diff --git a/util.c b/util.c > index 11973c4..36857d4 100644 > --- a/util.c > +++ b/util.c > @@ -606,6 +606,86 @@ int write_remainder(int fd, const struct iovec *iov,= size_t iovcnt, size_t skip) > return 0; > } > =20 > +/** > + * read_all_buf() - Fill a whole buffer from a file descriptor > + * @fd: File descriptor > + * @buf: Pointer to base of buffer > + * @len: Length of buffer > + * > + * Return: 0 on success, -1 on error (with errno set) > + * > + * #syscalls read > + */ > +int read_all_buf(int fd, void *buf, size_t len) > +{ > + size_t left =3D len; > + char *p =3D buf; > + > + while (left) { > + ssize_t rc; > + > + ASSERT(left <=3D len); > + > + do > + rc =3D read(fd, p, left); > + while ((rc < 0) && errno =3D=3D EINTR); > + > + if (rc < 0) > + return -1; > + > + if (rc =3D=3D 0) { > + errno =3D ENODATA; > + return -1; > + } > + > + p +=3D rc; > + left -=3D rc; > + } > + return 0; > +} > + > +/** > + * read_remainder() - Read the tail of an IO vector from a file descript= or > + * @fd: File descriptor > + * @iov: IO vector > + * @cnt: Number of entries in @iov > + * @skip: Number of bytes of the vector to skip reading > + * > + * Return: 0 on success, -1 on error (with errno set) > + * > + * Note: mode-specific seccomp profiles need to enable readv() to use th= is. > + */ > +int read_remainder(int fd, struct iovec *iov, size_t cnt, size_t skip) > +{ > + size_t i =3D 0, offset; > + > + while ((i +=3D iov_skip_bytes(iov + i, cnt - i, skip, &offset)) < cnt) { > + ssize_t rc; > + > + if (offset) { > + ASSERT(offset < iov[i].iov_len); > + /* Read the remainder of the partially read buffer */ > + if (read_all_buf(fd, (char *)iov[i].iov_base + offset, > + iov[i].iov_len - offset) < 0) > + return -1; > + i++; > + } > + > + /* Fill as many of the remaining buffers as we can */ > + rc =3D readv(fd, &iov[i], cnt - i); > + if (rc < 0) > + return -1; > + > + if (rc =3D=3D 0) { > + errno =3D ENODATA; > + return -1; > + } > + > + skip =3D rc; > + } > + return 0; > +} > + > /** sockaddr_ntop() - Convert a socket address to text format > * @sa: Socket address > * @dst: output buffer, minimum SOCKADDR_STRLEN bytes > diff --git a/util.h b/util.h > index d02333d..73a7a33 100644 > --- a/util.h > +++ b/util.h > @@ -203,6 +203,8 @@ int fls(unsigned long x); > int write_file(const char *path, const char *buf); > int write_all_buf(int fd, const void *buf, size_t len); > int write_remainder(int fd, const struct iovec *iov, size_t iovcnt, size= _t skip); > +int read_all_buf(int fd, void *buf, size_t len); > +int read_remainder(int fd, struct iovec *iov, size_t cnt, size_t skip); > void close_open_files(int argc, char **argv); > bool snprintf_check(char *str, size_t size, const char *format, ...); > =20 --=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 --Eps0HusX0V6BT3EO Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmeZhmMACgkQzQJF27ox 2GfwVQ/8DHOgKiqPQZodJjPFmY7K+HMPGADuOLejCNXLUFX4nvx34VWNkykdb8BT +krSsgq3m3BnYTDzs2grnHBejuhExu7hXq9b2oRUl6W1u9Huq+8BNvjOrIjAmjTM fGYGSgG9Fp9kmTd1er014xbSn/faQ8UwsN9ZnjSvmElEZFRUrjQnwZi5RLJJu8dw rc6joVL3WBWF5oXBCC6wQMpx4wsReIHTkHIZy90tBEduApLNq6vFNTf/ZSmfR2R7 OTK3hB401xSjOAEyxcRJEs8c4aPYAyfxUnZUzCwf10jOtkDgpcfdEDaISEIR3ELj ptSwsIPuD/tldRM1Z93rvYUMaq3PuQuu/Vyg1TfPxJRvdyqDz4oWpVu6EhjUey4M gNdoYHrKOq1jQKSxt3N8y5VNfYRRtuO+xdM9INCJUvW8lw11shUQuv/L+HcKKkig p2EIGeuvvqZobyAkk9IeUzf9R/k0G0nsRo8mbB4P+rlvHHjUgcIIY68KUFgVgj6o YACCqqMhGWmJgsI/4pvNyHU+fQN/UR+oWwsgnHKhwj8GjFPnBqAsDW9+B5q7YM3r iwEGtFziJSpkTs4bs2A/7+LFmUlnC0Ps73iDPzdn/vn2HNuKf84pwB2jpbfpkjAu 7qPdQynBnD5YKryLVd1mf+ZBrRYkjt6/vjKlFBNHq7rWpYkoxSU= =Ej9j -----END PGP SIGNATURE----- --Eps0HusX0V6BT3EO--