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=PKKhMhRT; 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 411BD5A061A for ; Thu, 27 Nov 2025 23:33:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1764282802; 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=fqoa0M1T2To6Ah1VWbOSiEduE4td90O+muDLxSHdBdo=; b=PKKhMhRTtE3K7FUEE483uKNOBSy5zv+ZfYUQjRNCGVZtpLGH4iFk8jN7xQdElh8khkvoDo a4Of7H3/dWtidVdKqHtXk3Blq0l3W25LdXciQtVRM1ZIhIQq0BvD/4xIy4lcK3xMeX720W q29KZJskNixB8+OVxLb5HEW4ZHtlYQ8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-88-fGZJ-4N7MomEfbkNqRPCCg-1; Thu, 27 Nov 2025 17:33:20 -0500 X-MC-Unique: fGZJ-4N7MomEfbkNqRPCCg-1 X-Mimecast-MFC-AGG-ID: fGZJ-4N7MomEfbkNqRPCCg_1764282799 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4775f51ce36so8844625e9.1 for ; Thu, 27 Nov 2025 14:33:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764282799; x=1764887599; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fqoa0M1T2To6Ah1VWbOSiEduE4td90O+muDLxSHdBdo=; b=rMAq+P1HxZzAJTO+ZajSP1ZXT3QI0ZyzM1S9OqEsnciYoSDXUsSs/F0oXUWJgfslvO Wph5iOvZxycPmZnAOg8DpgP8FveabbbJ0JrYtZUe95p+IzLtR/pXyWQlAOpRJ4kYGCiR 42oyp8xtvveUs9dhjhFmO34g4nQGb/3iEKHWSVZwNyBLx3ZvvmkIhIlDmyiJqbwPb80d a/lSVZrpSbkGyZX6DwXGSkTs5N6cjBoTE0+6VPF2WYT0pwelIPHtF0SakZtUFQO6wFVb C6aj3M46ca/NStV1NvdmO4Ngl+iT+ljb73YPyKbRQE4CEFmGSxSsubwAmyuc89Hi3MuA uVMw== X-Forwarded-Encrypted: i=1; AJvYcCUUtfsGPvqCnhANRJdzvBG+WP1dOJbe5Y2qaCMq1Qoi/8N9BXLfKgMgE0TGDZCdndJpplt3na3rbeY=@passt.top X-Gm-Message-State: AOJu0Yzxw/TP7UAdOyRNK47GIHZX9dtHHduK6S9E22a+7l1WV0ZhIxq8 jDl7pUhXeK/HPeiEVIDG8sEFNqXbezIR27dpr0PLMMuPdFEaG+k4ViasBG1CUsmxx3J0wN17Q92 VS8mw+7Eq9cOthge3psiAMwX93ZPWjROTpw9L+iiKNlOn7826zf9M/w3Qpgmopg== X-Gm-Gg: ASbGnctEK2G/cUOkLPHutn3PLmh1WVQV9pboE35sEC7o38SNzry8CrspXozA2yZ4/Cl LhjWlZTvdNW3adfUtkOV85ZMTaT2d9DpIPFnlja2cfxbBG3xJ2oBjJ0L2HjXj4v6q3QwGhkt6Nd tG7EL0y3Bqkw9ZUf4wneDfLdVnxSPjYfzu8FytvrjsPXpJpmKYrSKGcx9oGrRY7jDMskRdLetU2 ngXELc599QnayqTTc586xQ/oQa5zHRvXmrftfb0lCdMRBSJK2tYHocK0UwUhwboYZfYE4HZ3a6l wDS8yneWt1LJLPyLUJutdCJpdXoZFo+Y+SnoxvSCNsFoGm0TIjy/PBaSTvrgr0132iZVVsjnspd XEkKHIqlyMyFBIrHWP6ggkfVuYG8dWxmevCZBcg== X-Received: by 2002:a05:600c:1ca5:b0:475:dde5:d91b with SMTP id 5b1f17b1804b1-477c1115ff6mr296096845e9.17.1764282798749; Thu, 27 Nov 2025 14:33:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IFcmy4nMYN6XNJfl1yYMpllPVrST/hoNqf5r6wAVFJ/pM37xPLj3F/x5hxcz6vcP6bFrQ8uow== X-Received: by 2002:a05:600c:1ca5:b0:475:dde5:d91b with SMTP id 5b1f17b1804b1-477c1115ff6mr296096725e9.17.1764282798290; Thu, 27 Nov 2025 14:33:18 -0800 (PST) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4791116c0acsm52721005e9.7.2025.11.27.14.33.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Nov 2025 14:33:17 -0800 (PST) Date: Thu, 27 Nov 2025 23:33:16 +0100 From: Stefano Brivio To: Yumei Huang Subject: Re: [PATCH v9 2/5] util: Introduce read_file() and read_file_integer() function Message-ID: <20251127233316.6c92904d@elisabeth> In-Reply-To: References: <20251125072638.88896-1-yuhuang@redhat.com> <20251125072638.88896-3-yuhuang@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.49; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: zElo4yBxfhRUg6WKecZzbBzwg5kSAHMFmyE8T9-X3oI_1764282799 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: GWVFZNW4RDW7FYFZGMQZNJ6WZL7JYWM3 X-Message-ID-Hash: GWVFZNW4RDW7FYFZGMQZNJ6WZL7JYWM3 X-MailFrom: sbrivio@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: David Gibson , passt-dev@passt.top 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, 26 Nov 2025 15:07:25 +1100 David Gibson wrote: > On Tue, Nov 25, 2025 at 03:26:35PM +0800, Yumei Huang wrote: > > Signed-off-by: Yumei Huang > > --- > > util.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > util.h | 1 + > > 2 files changed, 91 insertions(+) > > > > diff --git a/util.c b/util.c > > index ab23463..c56f920 100644 > > --- a/util.c > > +++ b/util.c > > @@ -589,6 +589,96 @@ int write_file(const char *path, const char *buf) > > return len == 0 ? 0 : -1; > > } > > > > +/** > > + * read_file() - Read contents of file into a NULL-terminated buffer > > + * @path: Path to file to read > > + * @buf: Buffer to store file contents > > + * @buf_size: Size of buffer > > + * > > + * Return: number of bytes read on success, negative error code on failure > > + */ > > +static ssize_t read_file(const char *path, char *buf, size_t buf_size) > > +{ > > + int fd = open(path, O_RDONLY | O_CLOEXEC); > > + size_t total_read = 0; > > + > > + if (fd < 0) > > + return -errno; > > + > > + if (!buf_size) { > > + close(fd); > > + return -EINVAL; > > + } > > Nit: usually it's preferable to put the error checks with the least > "impact" first. So in this case, it would be slightly better to check > buf_size first, before even opening the file. What you have is still > correct though, so this isn't worth a respin. > > > + > > + while (total_read < buf_size) { > > + ssize_t rc = read(fd, buf + total_read, buf_size - total_read); > > + > > + if (rc < 0) { > > + int errno_save = errno; > > + close(fd); > > + return -errno_save; > > + } > > + > > + if (rc == 0) > > + break; > > + > > + total_read += rc; > > + } > > + > > + close(fd); > > + > > + if (total_read == buf_size) { > > + buf[buf_size - 1] = '\0'; > > + return -ENOBUFS; > > + } > > + > > + buf[total_read] = '\0'; > > + > > + return total_read; > > +} > > + > > +/** > > + * read_file_integer() - Read an integer value from a file > > + * @path: Path to 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) > > +{ > > + ssize_t bytes_read; > > + char buf[BUFSIZ]; > > + intmax_t value; > > + char *end; > > + > > + bytes_read = read_file(path, buf, sizeof(buf)); > > + > > + if (bytes_read < 0) > > + goto error; > > + > > + if (bytes_read == 0) { > > + debug("Empty file %s", path); > > + goto error; > > + } > > + > > + errno = 0; > > + value = strtoimax(buf, &end, 10); > > + if (*end && *end != '\n') { > > + debug("Non-numeric content in %s", path); > > + goto error; > > + } > > + if (errno) { > > + debug("Out of range value in %s: %s", path, buf); > > + goto error; > > + } > > + > > + return value; > > + > > +error: > > + debug("Using %"PRIdMAX" as default value", fallback); > > I think this does need to say what it's using this default for. So > something like: > "Couldn't read %s, using %"PRIdMAX"d as default value" Right, in some cases it's printed in the message just before (as long as we don't have multi-threading, at least), but in the most common case (kernel too old): --- $ ./pasta -d [...] 0.0163: Using 4 as default value 0.0163: Using 120000 as default value 0.0164: Using TCP RTO parameters, syn_retries: 6, syn_linear_timeouts: 4, rto_max: 120 --- The rest of the comments from myself and David aren't worth a re-spin, but I think this one is, because it might actually confuse users. > > > + return fallback; > > +} > > + > > #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 a0b2ada..6dec14b 100644 > > --- a/util.h > > +++ b/util.h > > @@ -229,6 +229,7 @@ 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); > > +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, size_t skip); > > int read_all_buf(int fd, void *buf, size_t len); > > -- > > 2.51.1 -- Stefano