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=202510 header.b=KTK3O87f; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id C4A7A5A061A for ; Wed, 15 Oct 2025 01:27:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202510; t=1760484469; bh=9XZzoT9eWA6pkJLVa0Efom85e/BXMutJtvSOeivLz4E=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=KTK3O87ft58J0PS5JnaN3t1IlW1xYY0iZTNpkWdQNmsIBlcN1rsLH5opiDazMFHGl TIu/LUbFJcAcIVaHroELHkiQ+bPmtQRFYTG/OHvKBQFHnND4tnD6njemfUfwmGTl2P jQ48z5NSD5w6kljITz4MT3HEzwic967KmwVaLSnFR1iIsubboDvRsIlNprBQ53Rhzw V6ApHUJaRXigiEtCKACgjM9hn7hxUL9HvdYbdrQmssH7ZTP78b0RTG6BbZncYGk0Eo PlmWcZlvX2Geh5mVj6WBB3fPW7lnodiggqM82jEU4SDYJzbD1M0kGRXpesMsUFuOV0 +56OT4f4T9CkQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4cmVkY5pPvz4w9w; Wed, 15 Oct 2025 10:27:49 +1100 (AEDT) Date: Wed, 15 Oct 2025 10:27:45 +1100 From: David Gibson To: Yumei Huang Subject: Re: [PATCH v3 2/4] util: Introduce read_file() and read_file_long() function Message-ID: References: <20251014073836.18150-1-yuhuang@redhat.com> <20251014073836.18150-3-yuhuang@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="jTEfWEWmyMQG80K0" Content-Disposition: inline In-Reply-To: <20251014073836.18150-3-yuhuang@redhat.com> Message-ID-Hash: UTSKHRWNYIIEW74MFCP4BPR3DLLPQ4HU X-Message-ID-Hash: UTSKHRWNYIIEW74MFCP4BPR3DLLPQ4HU 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, sbrivio@redhat.com 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: --jTEfWEWmyMQG80K0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Oct 14, 2025 at 03:38:34PM +0800, Yumei Huang wrote: > Signed-off-by: Yumei Huang > --- > util.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > util.h | 2 ++ > 2 files changed, 94 insertions(+) >=20 > diff --git a/util.c b/util.c > index c492f90..d331f08 100644 > --- a/util.c > +++ b/util.c > @@ -579,6 +579,98 @@ int write_file(const char *path, const char *buf) > return len =3D=3D 0 ? 0 : -1; > } > =20 > +/** > + * read_file() - Read contents of file into a buffer > + * @path: File to read > + * @buf: Buffer to store file contents > + * @buf_size: Size of buffer > + * > + * Return: number of bytes read on success, -1 on any error, -2 on trunc= ation > +*/ Looks ok, but I think there's a simpler way. > +int read_file(const char *path, char *buf, size_t buf_size) > +{ > + int fd =3D open(path, O_RDONLY | O_CLOEXEC); > + size_t total_read =3D 0; > + ssize_t rc; > + bool truncated =3D false; > + > + if (fd < 0) { > + warn_perror("Could not open %s", path); > + return -1; > + } > + > + while (total_read < buf_size - 1) { > + rc =3D read(fd, buf + total_read, buf_size - 1 - total_read); The '- 1' is to leave space for the \0, but if you instead attempt to read the entire buffer... > + > + if (rc < 0 ) { (nit: extra space before ')') > + warn_perror("Couldn't read from %s", path); > + close(fd); > + return -1; > + } > + > + if (rc =3D=3D 0) { > + break; > + } > + > + total_read +=3D rc; > + > + if (total_read =3D=3D buf_size - 1) { > + char test_byte; > + rc =3D read(fd, &test_byte, 1); > + if (rc >0) { > + truncated =3D true; > + warn_perror("File %s truncated, buffer too small", path); > + } > + } =2E..then you can tell if you have to truncate by finishing the loop then checking if (total_read < buf_size). If it is, there's space for the \0, otherwise there isn't and you report truncation. No need for test_byte. > + } > + > + close(fd); > + > + if (total_read < buf_size){ > + buf[total_read] =3D '\0'; And if you test for truncation and exit early, you can make this unconditional. > + } > + > + return truncated ? -2 : (int)total_read; > +} > + > +/** > + * read_file_long() - Read a long integer value from a file When I first read this name I thought it was for reading a long file, rather than reading a long (int) from a file. Not immediately sure how to clarify that. read_file_long_int() is clear, but awkward. A better choice might be to change this to use strtoimax() and call it read_file_integer(). > + * @path: Path to the sysctl file > + * @fallback: Default value if file can't be read > + * > + * Return: Parameter value, fallback on failure > +*/ > +long read_file_long(const char *path, long fallback) > +{ > + char buf[32]; Rather than just using a semi-arbitrary 32 here, I'd suggest defining a new constant similar to UINT16_STRLEN. Except that's trickier for a type that doesn't have a known fixed width. Pity the C library doesn't have constants for these AFAICT. > + char *end; > + long value; > + int bytes_read; > + > + bytes_read =3D read_file(path, buf, sizeof(buf)); > + if (bytes_read < 0) { > + debug("Unable to read %s", path); If there's a an error on open() or read(), this will produce two very similar error messages in a row, which isn't ideal. > + return fallback; > + } > + > + if (bytes_read =3D=3D 0) { > + debug("Empty file %s", path); > + return fallback; > + } Might be worth checking strtol()'s behaviour on an empty string to see if this special case would already be handled below. > + > + errno =3D 0; > + value =3D strtol(buf, &end, 10); > + if (*end && *end !=3D '\n') { > + debug("Invalid format in %s", path); > + return fallback; > + } > + if (errno || value < 0 || value > LONG_MAX) { No need to exclude negative values here. (value > LONG_MAX) can never be true since value is a long. > + debug("Invalid value in %s: %ld", path, value); If errno !=3D 0, value might be uninitialised here, and certainly won't have something useful. Better to print the contents as a string. > + return fallback; > + } > + return value; > +} > + > #ifdef __ia64__ > /* Needed by do_clone() below: glibc doesn't export the prototype of __c= lone2(), > * use the description from clone(2). > diff --git a/util.h b/util.h > index 22eaac5..e509bec 100644 > --- a/util.h > +++ b/util.h > @@ -222,6 +222,8 @@ void pidfile_write(int fd, pid_t pid); > int __daemon(int pidfile_fd, int devnull_fd); > int fls(unsigned long x); > int write_file(const char *path, const char *buf); > +int read_file(const char *path, char *buf, size_t buf_size); > +long read_file_long(const char *path, long fallback); > 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); > --=20 > 2.47.0 >=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 --jTEfWEWmyMQG80K0 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmju3HAACgkQzQJF27ox 2GeGxhAAn65c7vTLHpCYUMyl/EnOCskFewUpi4rwlIDWN163xfYNtdukTpvLbynX en45a032VEpldP2WFrbORkQiZUra4ahLc/rwjj6nluI0B+uHBHQCmfLkbHP1/pf4 NBjkM7mZKxvAfVDda5aHqH5xhkHo9Et1T+3AVaLGdMLQtarXrcMaoLjTOzwHWzTU c9Z+6OHdnyWKCYrdXYMH+zP2FX23KqdA7Bmjim0WXhB+sacKGdAXS4f9vQxKQqhZ QGXLqzVPmsbki2SOkWfaOfQbTEwTULxfPYFf3tiK1WZM+NnP2HhI9BMCDje8R4Ho JFuhA+jML3Wz0i4PK1Q9DH3BwZ6IO3sw9mpzwYweIHEaptjrFUnULYTZAAuPcNNs ZlY4rvyHoaqCId1TF5/GP+ihwSbp3RKWNvH5IPXTBnqtARBCkEctvZrDVMrvzTqv Sp+KvDoIxMvXO0YJxfSGGV2GCxREx1vSNdU9UQdKy1yLmbKvYXvQip0+wScClZFL CdKlbz7wJoEWVBpfw9uZLrqnHjZXtaXG7tVaSaOhTYqIAhI9/ki7h6z2hR7969wK YS4G88F32o5QkuHoy+d+8HV+d1vdIw2Lh+/ioB3IRmb1Aqv1V06lchp8VrlWMHKI nC+hmJEdbDi3G05h8LUoLbpee0O5OEks8hOJ+yLXEU/tVo0AclI= =GKzr -----END PGP SIGNATURE----- --jTEfWEWmyMQG80K0--