From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTP id 530145A026F for ; Tue, 27 Feb 2024 15:26:33 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709043992; 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=iUzc8bPyQ7dzas/9XGnaLja0fKWWOqRNEFWkh37a2U0=; b=U0N3UgAUpwD5aHSv6/5lOqXMgwRkexoHPVcIDf+Y21jftrBkRlhHyNklSbiYjLMXz94d9T Hnm+oSH8QNFslDiGctPM04OJTzR+UHdUDsmImdWj8JE0i1Al9oVttZPtcKMFukXXX/U5CH VZ4sN7pDi3jhGDd+lTdJdnyDB/z8wMI= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-620-36Uk0UyUMQGBgD1-dLAuUg-1; Tue, 27 Feb 2024 09:26:27 -0500 X-MC-Unique: 36Uk0UyUMQGBgD1-dLAuUg-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a3f52ebbb6cso210445466b.0 for ; Tue, 27 Feb 2024 06:26:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709043986; x=1709648786; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=iUzc8bPyQ7dzas/9XGnaLja0fKWWOqRNEFWkh37a2U0=; b=UmPEZFA8teSpGip+FT43aOBGc+grlZtWzv1AYIvPHd0uINaf78S2R8Gvg7J8I3xaKQ ZiqHduoPlcH7WpINsT9bw9pFVBvPdQnuCePEDuXWEgHzlb9Q+2/CEM2gwWykZKEr5rYc eTHyHJYV1zT4EAqcZlTj2r2ZS1sj/OUAvr+AL6pLGpGONqKVIIhHnTTwi2l2pTSah67Y F8SI8qiLQNVK9ReLNJQ0iGwaRfkZ2iwb9juOlkxq3b+av8NV0kZtPl5GJdt+OldKle0Y vqI2yf3ze7Arrls7eI2UGkWTpwEQZDeOFtecIe6BlKzE9CuHIunc5OVQ3VGJR9aEipsW 4nNQ== X-Gm-Message-State: AOJu0YzdLRtCt8T69PeNAtyP8GVPPPS724eljJWtt+YC0kbfcgqzleX9 U80UPIl3IgWDpnbkK1M3LpyXdU4xcUL//IF8iOky1/3P8BS0lDIIR4RQVlxw5ZpRDvIlBR5TDwx S8jr4QGL+2WuoDBamFs2yXCH8cwNrbJHqqqAQM6XYjRFazksbUg== X-Received: by 2002:a17:906:2307:b0:a43:de9e:69b8 with SMTP id l7-20020a170906230700b00a43de9e69b8mr75558eja.25.1709043986167; Tue, 27 Feb 2024 06:26:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHEe2gYzcoD5hcnzPuGtfMfMrzpRdwQXNtsj3olOPcSmpgyznpSIcoe6+MswU1pOj9nuPyszQ== X-Received: by 2002:a17:906:2307:b0:a43:de9e:69b8 with SMTP id l7-20020a170906230700b00a43de9e69b8mr75547eja.25.1709043985836; Tue, 27 Feb 2024 06:26:25 -0800 (PST) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id t11-20020a170906a10b00b00a42e4b5aaeesm814429ejy.89.2024.02.27.06.26.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Feb 2024 06:26:25 -0800 (PST) Date: Tue, 27 Feb 2024 15:25:51 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 3/6] util: Add write_remainder() helper Message-ID: <20240227152551.72d8744f@elisabeth> In-Reply-To: <20240222055602.1872516-4-david@gibson.dropbear.id.au> References: <20240222055602.1872516-1-david@gibson.dropbear.id.au> <20240222055602.1872516-4-david@gibson.dropbear.id.au> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.36; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: GYK5R3R2HKRCLD67D7F447XB35XDACIB X-Message-ID-Hash: GYK5R3R2HKRCLD67D7F447XB35XDACIB 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: passt-dev@passt.top, Laurent Vivier 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, 22 Feb 2024 16:55:59 +1100 David Gibson wrote: > We have several places where we want to write(2) a buffer or buffers and we > do (or should) handle sort write()s by retrying until everything is After making sure that "handle sorting" doesn't exist (yet): is one between "handle" and "sort" redundant, or is there another meaning to this? > successfully written. Add a helper for this in util.c. > > This version has some differences from the typical write_all() function. > First, take an IO vector rather than a single buffer, because that will be > useful for some of our cases. Second, allow it to take an parameter to > skip the first n bytes of the given buffers. This will be usefl for some > of the cases we want, and also falls out quite naturally from the > implementation. > > Signed-off-by: David Gibson > --- > util.c | 32 ++++++++++++++++++++++++++++++++ > util.h | 1 + > 2 files changed, 33 insertions(+) > > diff --git a/util.c b/util.c > index fb6a0430..a475f2b5 100644 > --- a/util.c > +++ b/util.c > @@ -19,6 +19,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -597,3 +598,34 @@ size_t iov_offset(const struct iovec *iov, size_t n, size_t *offset) > > return i; > } > + > +/* write_remainder() - write the tail of an IO vector to an fd > + * @fd: File descriptor > + * @iov: IO vector > + * @iovcnt: Number of entries in @iov > + * @skip: Number of bytes of the vector to skip writing > + * > + * Return: 0 on success, -1 on error (with errno set) > + * > + * #syscalls write writev > + */ > +int write_remainder(int fd, const struct iovec *iov, int iovcnt, size_t skip) > +{ > + int i; > + > + while ((i = iov_offset(iov, iovcnt, &skip)) < iovcnt) { With my proposal for 1/6 this becomes: while ((i = iov_entry_index(iov, iovcnt, skip, &offset)) < iovcnt) { if (offset) ... which I don't really find brilliant, but at least we don't do if (skip) where 'skip' used to be something completely different. > + ssize_t rc; > + > + if (skip) Curly brackets here for consistency (undecided about readability to be honest). > + rc = write(fd, (char *)iov[i].iov_base + skip, > + iov[i].iov_len - skip); > + else > + rc = writev(fd, &iov[i], iovcnt - i); > + > + if (rc < 0) > + return -1; > + skip += rc; > + } > + > + return 0; > +} > diff --git a/util.h b/util.h > index 62fad6fe..ee380f55 100644 > --- a/util.h > +++ b/util.h > @@ -230,6 +230,7 @@ int __daemon(int pidfile_fd, int devnull_fd); > int fls(unsigned long x); > int write_file(const char *path, const char *buf); > size_t iov_offset(const struct iovec *iov, size_t n, size_t *offset); > +int write_remainder(int fd, const struct iovec *iov, int iovcnt, size_t skip); > > /** > * mod_sub() - Modular arithmetic subtraction -- Stefano