From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=none 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=IOEJc+mG; 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 ESMTP id 313FE5A004E for ; Wed, 18 Sep 2024 08:05:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726639533; 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: in-reply-to:in-reply-to:references:references; bh=r1wYwHLElgbrYAnrcL2zwFjnnvihIboiP1kKU5pvI5c=; b=IOEJc+mGLxNEwcjk0fhLp+rMyN2fwiXp9xIipJ6v0kZAGGWvk4RXSdSe/5iz+vNJ7SFG4i mSbhjcxJLLwzm+ZPPNuZCauSe0O9ttX3VVscqVJaUR2ZEV69gZ45wAFqfXfquZeLLHbNYp G+NwXS7k6y6WpiV/MIQS4L46TT50/fY= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-315-QtYx-3xROg2ISALKrdMIrQ-1; Wed, 18 Sep 2024 02:05:31 -0400 X-MC-Unique: QtYx-3xROg2ISALKrdMIrQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 49E781955E8C; Wed, 18 Sep 2024 06:05:30 +0000 (UTC) Received: from blackfin.pond.sub.org (unknown [10.39.192.47]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 000B619560AA; Wed, 18 Sep 2024 06:05:28 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id B257321E6A28; Wed, 18 Sep 2024 08:05:26 +0200 (CEST) From: Markus Armbruster To: David Gibson Subject: Re: [PATCH v2 1/2] util: Add helper to write() all of a buffer In-Reply-To: <20240917064555.1040365-2-david@gibson.dropbear.id.au> (David Gibson's message of "Tue, 17 Sep 2024 16:45:54 +1000") References: <20240917064555.1040365-1-david@gibson.dropbear.id.au> <20240917064555.1040365-2-david@gibson.dropbear.id.au> Date: Wed, 18 Sep 2024 08:05:26 +0200 Message-ID: <878qvp4huh.fsf@pond.sub.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-MailFrom: armbru@redhat.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation Message-ID-Hash: OV3YHG6G2ZQTHRWFPM5VCFQHUG4QZN24 X-Message-ID-Hash: OV3YHG6G2ZQTHRWFPM5VCFQHUG4QZN24 X-Mailman-Approved-At: Wed, 18 Sep 2024 08:29:50 +0200 CC: passt-dev@passt.top, Stefano Brivio 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: David Gibson writes: > write(2) might not write all the data it is given. Add a write_all_buf() > helper to keep calling it until all the given data is written, or we get an > error. > > Currently we use write_remainder() to do this operation in pcap_frame(). > That's a little awkward since it requires constructing an iovec, and future > changes we want to make to write_remainder() will be easier in terms of > this single buffer helper. > > Signed-off-by: David Gibson > --- > pcap.c | 3 +-- > util.c | 25 +++++++++++++++++++++++++ > util.h | 1 + > 3 files changed, 27 insertions(+), 2 deletions(-) > > diff --git a/pcap.c b/pcap.c > index 46cc4b0d..e6b5ced4 100644 > --- a/pcap.c > +++ b/pcap.c > @@ -86,9 +86,8 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt, > .caplen = l2len, > .len = l2len > }; > - struct iovec hiov = { &h, sizeof(h) }; > > - if (write_remainder(pcap_fd, &hiov, 1, 0) < 0 || > + if (write_all_buf(pcap_fd, &h, sizeof(h)) < 0 || > write_remainder(pcap_fd, iov, iovcnt, offset) < 0) > debug_perror("Cannot log packet, length %zu", l2len); > } > diff --git a/util.c b/util.c > index eede4e58..7db7c2e7 100644 > --- a/util.c > +++ b/util.c > @@ -582,6 +582,31 @@ int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags, > #endif > } > > +/* write_all_buf() - write all of a buffer to an fd > + * @fd: File descriptor > + * @buf: Pointer to base of buffer > + * @len: Length of buffer > + * > + * Return: 0 on success, -1 on error (with errno set) > + * > + * #syscalls write > + */ > +int write_all_buf(int fd, const void *buf, size_t len) I'd call it write_all(), but that's strictly a matter of taste. > +{ > + const char *p = buf; > + size_t left = len; > + > + while (left) { > + ssize_t rc = write(fd, p, left); > + > + if (rc < 0) > + return -1; Loop when errno == EINTR? > + p += rc; > + left -= rc; > + } > + return 0; > +} > + > /* write_remainder() - write the tail of an IO vector to an fd > * @fd: File descriptor > * @iov: IO vector > diff --git a/util.h b/util.h > index c7a59d5d..5e67f1fa 100644 > --- a/util.h > +++ b/util.h > @@ -200,6 +200,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); > +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); > void close_open_files(int argc, char **argv);