From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QDzIMZp5; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 110095A0619 for ; Thu, 16 Oct 2025 09:49:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760600995; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KqqKQROqletuKgjOIJZsRQcEniq7pSrO0pL3ll3PSCU=; b=QDzIMZp5Rzeg1U9+Tp3rkkG2U0ce6w5rzTaWRf+FKdPE8VhKcW1LDZ2fQL9yUxegkyxyAT WNGRM7QERHdruPAoycAJfR8Gc3e9PoSIGIs+1e/rfM7osNBbDkGa9UOH2etgRaJc0Nvbfm 2ahq3+WFjmVYRMyibIPoMgA6HW4eEQ4= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-691-4a9pI32cOi6jiPqh5JHt5Q-1; Thu, 16 Oct 2025 03:49:53 -0400 X-MC-Unique: 4a9pI32cOi6jiPqh5JHt5Q-1 X-Mimecast-MFC-AGG-ID: 4a9pI32cOi6jiPqh5JHt5Q_1760600992 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-63b9bcd8cb4so548443a12.0 for ; Thu, 16 Oct 2025 00:49:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760600992; x=1761205792; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KqqKQROqletuKgjOIJZsRQcEniq7pSrO0pL3ll3PSCU=; b=tKafpJP1oxXJhJUsAyNYV4dlocb3FR7cOmx+PQ2HP/s7fVBSCRJVlji76wxumTnhCM f31RD/vqWhsJ1vxeOz76QNKGxMPp9L+/xc133gXswGs+Z1fiKdSGc9X6eS2jUwYA2FxR O0Y/bIB1+y3SiO6NsXsK4QJat8fpb1OYdJlIyeV5/YwRKgBv9VscXEVCmKxctjkA+qOg RLpIdnIJn1BegKj3+Qw0PM7n0U+rO4v2B4eTh35N60A8XVOuDpFYCzY/JhpxqZUcBKep qV8fvb5n2+vrf85myaG6PCHnVz4WOXJsJEEgF0kwPfbKQxmVCwwgvArgiYA6mH17TTde gmvA== X-Gm-Message-State: AOJu0YzwHirGpu7cYW5IRdm1/0X63OlXXsSeZPnbz5UJllTm1/5jsKD7 eNVFggGPiFJRqJoPqMmYNC0d/i5Ihu4QoOQxpHJBszNP1dSVN4TQ1eCpm/p1/ozAVGza62Klc4+ EMW0W6yOdzlXn9SDJZ++aL4aTvnjgXWk53Pf5aT8/OIPyNualrxesNz7IY4XrgICUkpS/GSsbB9 Up19BKqaOewJFNc8sEdRwsRIWlBsMfvTrv1TlsKdnY3w== X-Gm-Gg: ASbGncufjRDH0r9zfMksUhUJ9AW3XNh2jBqwvLUooZ4WZrSL5GSFqG49q9I1YYg0b8K wPUiYQ2nW1fDhVlSaPz0+P+AjCJQyith9nA4/KTNLWchniw8Ufia2oKE+1aZD9K52Uxu8vWYCsg no9AVN0GdErA2sruX/oSZhvlqNI+8wd0fCHxWYOKResqNQlEMIS3MO3Mqh X-Received: by 2002:a05:6402:440d:b0:639:dd3f:f27f with SMTP id 4fb4d7f45d1cf-639dd3ff812mr25205333a12.4.1760600991688; Thu, 16 Oct 2025 00:49:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGCJaR9j4vliRsHZgXqzwYgU2Kg7D8DDZZhsZHkeiC8+Ivt9cF/mi2ez2ex8/P8JGTHt0e/mpNWSPXhmUkdkXo= X-Received: by 2002:a05:6402:440d:b0:639:dd3f:f27f with SMTP id 4fb4d7f45d1cf-639dd3ff812mr25205318a12.4.1760600991262; Thu, 16 Oct 2025 00:49:51 -0700 (PDT) MIME-Version: 1.0 References: <20251016023423.8923-1-yuhuang@redhat.com> <20251016023423.8923-3-yuhuang@redhat.com> In-Reply-To: From: Yumei Huang Date: Thu, 16 Oct 2025 15:49:39 +0800 X-Gm-Features: AS18NWB1aDH3czeXZaIyxihFZ4YoZOC8L9gokcGc6skCN6wZW8xBQQIDDVYknP0 Message-ID: Subject: Re: [PATCH v4 2/4] util: Introduce read_file() and read_file_integer() function To: David Gibson X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: e4-pk8Wip62Z2zzTlgOEcOzDQX3RK9HwweYZTDQQ-Oc_1760600992 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: DMWPTM7Z63BWQZMHSO6XXEFDWML4NDF3 X-Message-ID-Hash: DMWPTM7Z63BWQZMHSO6XXEFDWML4NDF3 X-MailFrom: yuhuang@redhat.com 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: On Thu, Oct 16, 2025 at 2:30=E2=80=AFPM David Gibson wrote: > > On Thu, Oct 16, 2025 at 10:34:21AM +0800, Yumei Huang wrote: > > Signed-off-by: Yumei Huang > > --- > > util.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > util.h | 3 +++ > > 2 files changed, 87 insertions(+) > > > > diff --git a/util.c b/util.c > > index c492f90..197677e 100644 > > --- a/util.c > > +++ b/util.c > > @@ -579,6 +579,90 @@ int write_file(const char *path, const char *buf) > > return len =3D=3D 0 ? 0 : -1; > > } > > > > +/** > > + * 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 tru= ncation > > +*/ > > +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; > > + > > + if (fd < 0) { > > + warn_perror("Could not open %s", path); > > + return -1; > > + } > > + > > + while (total_read < buf_size) { > > + rc =3D read(fd, buf + total_read, buf_size - total_read); > > + > > + if (rc < 0) { > > + warn_perror("Couldn't read from %s", path); > > + close(fd); > > + return -1; > > + } > > + > > + if (rc =3D=3D 0) > > + break; > > + > > + total_read +=3D rc; > > + } > > + > > + close(fd); > > + > > + if (total_read =3D=3D buf_size) { > > + warn_perror("File %s truncated, buffer too small", path); > > + return -2; > > + } > > + > > + buf[total_read] =3D '\0'; > > + > > + return (int)total_read; > > Probably makes more sense for total_read and the return type to be ssize_= t. Just tried to be consistent with write_file(). I can change it to ssize_t if needed. > > > +} > > + > > +/** > > + * read_file_integer() - Read an integer value from a file > > + * @path: File to read > > + * @fallback: Default value if file can't be read > > + * > > + * Return: Integer value, fallback on failure > > +*/ > > +intmax_t read_file_integer(const char *path, intmax_t fallback) > > +{ > > + char buf[INTMAX_STRLEN]; > > + char *end; > > passt coding style is to list (where possible) local variables in > reverse order of line length, so this should go after bytes_read. Oh, I didn't notice that. Will update later. > > > + intmax_t value; > > + int bytes_read; > > + > > + bytes_read =3D read_file(path, buf, sizeof(buf)); > > + > > + if (bytes_read < 0) > > + return fallback; > > + > > + if (bytes_read =3D=3D 0) { > > + debug("Empty file %s", path); > > + return fallback; > > + } > > + > > + errno =3D 0; > > + value =3D strtoimax(buf, &end, 10); > > + if (*end && *end !=3D '\n') { > > + debug("Invalid format in %s", path); > > + return fallback; > > + } > > + if (errno) { > > + debug("Invalid value in %s: %s", path, buf); > > + return fallback; > > + } > > + > > + return value; > > +} > > + > > #ifdef __ia64__ > > /* Needed by do_clone() below: glibc doesn't export the prototype of _= _clone2(), > > * use the description from clone(2). > > diff --git a/util.h b/util.h > > index 22eaac5..887d795 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); > > +intmax_t read_file_integer(const char *path, intmax_t 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, si= ze_t skip); > > int read_all_buf(int fd, void *buf, size_t len); > > @@ -249,6 +251,7 @@ static inline const char *af_name(sa_family_t af) > > } > > > > #define UINT16_STRLEN (sizeof("65535")) > > +#define INTMAX_STRLEN (sizeof("-9223372036854775808")) > > It's correct for now, and probably for any systems we're likely to run > on, but I dislike hard-assuming the size of intmax_t here. I feel > like there must be a better way to derive the correct string length, > but I haven't figured out what it is yet :(. How about this: #define INTMAX_STRLEN (sizeof(intmax_t) * 3 + 2) Each byte can represent about 2.4 decimal digits as below, sizeof(intmax_t) * 3 gives us a safe upper bound, +2 for sign and null terminator. 1 bit =3D log=E2=82=81=E2=82=80(2) =E2=89=88 0.30103 decimal digits 1 byte =3D 8 bits =3D 8 =C3=97 0.30103 =E2=89=88 2.408 decimal digits > > > > > /* inet address (- '\0') + port (u16) (- '\0') + ':' + '\0' */ > > #define SOCKADDR_INET_STRLEN \ > > -- > > 2.47.0 > > > > -- > 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 wa= y > | around. > http://www.ozlabs.org/~dgibson --=20 Thanks, Yumei Huang