From: David Gibson <david@gibson.dropbear.id.au>
To: Markus Armbruster <armbru@redhat.com>
Cc: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Subject: Re: [PATCH v2 1/2] util: Add helper to write() all of a buffer
Date: Wed, 18 Sep 2024 20:22:59 +1000 [thread overview]
Message-ID: <ZuqqA0ntGUCgzrQY@zatzit.fritz.box> (raw)
In-Reply-To: <878qvp4huh.fsf@pond.sub.org>
[-- Attachment #1: Type: text/plain, Size: 2578 bytes --]
On Wed, Sep 18, 2024 at 08:05:26AM +0200, Markus Armbruster wrote:
> David Gibson <david@gibson.dropbear.id.au> 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 <david@gibson.dropbear.id.au>
> > ---
> > 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.
I usually would too, but given we work pretty heavily with both plain
buffers and iovs, I thought it was worth emphasising that this works
on the former.
> > +{
> > + 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?
Oh, good idea.
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-09-18 10:39 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-17 6:45 [PATCH v2 0/2] util: Fix some problems in write_remainder() David Gibson
2024-09-17 6:45 ` [PATCH v2 1/2] util: Add helper to write() all of a buffer David Gibson
2024-09-18 6:05 ` Markus Armbruster
2024-09-18 10:22 ` David Gibson [this message]
2024-09-17 6:45 ` [PATCH v2 2/2] util: Remove possible quadratic behaviour from write_remainder() David Gibson
2024-09-18 6:20 ` Markus Armbruster
2024-09-18 10:27 ` David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZuqqA0ntGUCgzrQY@zatzit.fritz.box \
--to=david@gibson.dropbear.id.au \
--cc=armbru@redhat.com \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).