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=ENqvRtxc; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 485555A0619 for ; Wed, 15 Oct 2025 05:51:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760500268; 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=DPp5BZKK4whxa6n0uLr2LzcOELFKi0k2j9W9VxtF8ko=; b=ENqvRtxc2Y2HE++AdVFntujzngzE85kDOMyKtDzNon5uZOxFz3FShDgsE1xqol55htyPLB g0BnQSzoWM1AKGGY8hR2VdVrHBC+Qtc/Y/rtquw8XaTqFnqxih+gYxEVwtDKSrSwNqdipp 4F3/MjqZ9NfJ0abcWpbugKoC/LHu5+Y= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-308-xa8IjnxLOFm1j0ccE2FSUg-1; Tue, 14 Oct 2025 23:51:06 -0400 X-MC-Unique: xa8IjnxLOFm1j0ccE2FSUg-1 X-Mimecast-MFC-AGG-ID: xa8IjnxLOFm1j0ccE2FSUg_1760500265 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-b4068ff9c19so826945866b.0 for ; Tue, 14 Oct 2025 20:51:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760500265; x=1761105065; 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=DPp5BZKK4whxa6n0uLr2LzcOELFKi0k2j9W9VxtF8ko=; b=Whc/7y2oCZy4WNMxcbay+MIErmH0Gs6f87M5V2bsm5KvlMKHgPugvZYikFWBF20HKX EZvbHfEt9eKZObJzvslC19JRKOFlcZ7HgRX3C05kgvnePres0M+rNSXTU7sQFrdPE0IJ tLM8pGkrrkBU4uQ3LZfrpJobFDrxN3xilcs6yrk8gyAxf8k49ytlBU2vKsdPR0ZN+9ZA HmR/umK0ErNWb8vu1Ad6bU7jw4+gtQfIfUpMdZNn88v4piQ7By8pFt9zK4IcQDK88act jkDNRW6V1kYDpUUnRdkaNhHCKlH9tx3vzqNE0tFTXatP0BsBdhfow4bufPEh2w2eS40L NU/Q== X-Gm-Message-State: AOJu0Yw+nqN0xqAXYeWVqE3EoHl7FFKJydwNhQmJuZdzibwEqfmRzgyx VK9XOMYH+hQ16P06Kzb9MrzF2gn5kl+IVv+1z/ClfstYQI61Cm6REav2vARKUFeBFXfO1/pj89j VrPhFWBwdRDh2e2OxKLkKu3YlCFySN3HCd2Bh4H45pMPiYfI3384MEuQ+V1C77bwRdm9irZMvAw yaGTyHxi61E4kk8Ba+MwRHf0TglFU2 X-Gm-Gg: ASbGnctyq6Xk7eyxOPVFhy09VGzAA30bN1eppr9e+hrCDoFCtUMv+Efj09RG7THyoIu tt9lY/+FcB8042QHGWo2KcSgkuULkQ7uxOYAWD7uHUwC0uPeG6raMzCALDUlkK7wnKmCFuA+4si iah+4stDPktw12yTYZ+uDyww== X-Received: by 2002:a17:907:e8e:b0:b45:eea7:e97c with SMTP id a640c23a62f3a-b50abfd5effmr2622473566b.47.1760500264954; Tue, 14 Oct 2025 20:51:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGrKwRF8bEI2tkYtcsjnv5qI4aK+qkeV2BK73j3zuIFTN3UVsOenr6MeZld8I4zJ6cOOLxiVCBIq08/xTMDXYA= X-Received: by 2002:a17:907:e8e:b0:b45:eea7:e97c with SMTP id a640c23a62f3a-b50abfd5effmr2622471766b.47.1760500264468; Tue, 14 Oct 2025 20:51:04 -0700 (PDT) MIME-Version: 1.0 References: <20251014073836.18150-1-yuhuang@redhat.com> <20251014073836.18150-3-yuhuang@redhat.com> In-Reply-To: From: Yumei Huang Date: Wed, 15 Oct 2025 11:50:53 +0800 X-Gm-Features: AS18NWDirCUSmvg2l4d-_hKAVC4Zj5CG4sy2PPHzDHDRmQkhaRhUnVOJyoo-0YI Message-ID: Subject: Re: [PATCH v3 2/4] util: Introduce read_file() and read_file_long() function To: David Gibson X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: OjBGx5saA5drsM52c9E0f9b50YpKY4Ybv9B_GGg5_ac_1760500265 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: P3VBCESRBWBGLFTMKVO2WTQECYHFTGR7 X-Message-ID-Hash: P3VBCESRBWBGLFTMKVO2WTQECYHFTGR7 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 Wed, Oct 15, 2025 at 7:28=E2=80=AFAM David Gibson wrote: > > 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(+) > > > > 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; > > } > > > > +/** > > + * 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 > > +*/ > > 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_re= ad); > > 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 to= o small", path); > > + } > > + } > > ...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. Yeah, that's much simpler. Will update in v4. > > > + } > > + > > + 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. Agree. > > > + } > > + > > + 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(). Good point. Will use strtoimax() and return intmax_t. > > > + * @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. I will just define a UINTMAX_STRLEN with (sizeof("2147483647")). > > > + 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. Checked both strtol() and strtoimax(), if the string is empty, it will return 0 and set end to buf, which is \0, and errno remains unchanged. So it's not 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. Right. > > > + 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..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, si= ze_t skip); > > int read_all_buf(int fd, void *buf, size_t len); > > -- > > 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