From nobody Sun Apr 13 18:17:56 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612545108; cv=none; d=zohomail.com; s=zohoarc; b=TWHBI4Wo+wgNs5H2ekXacx64dCwSNxPZ6P5o0QVp2hxszWspEnxGMvHV7sVqgJ9tdXrCF43uRXEJLHzOy/oF/DBqwHz7nGMu8YAZHNnI55h53jvWlvyeUSppsKZ2GtRATC+RkocrhLSt6tiostFw1Q/Ai8zQgqihMs6m+m58764= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612545108; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2lrRbdrWGI2XS27dloKIEn7pP33fpAuvfxOU0lrsdRg=; b=iN6EgpxeSf1iZAIL/e5rHM9npzM1Fzf2xnBJAmixqTCTcFOY9r3zQPEPZdT7CIIic4Jwzra7YnH6d3LkxNtYmVQN995ne3KeAXnynyZX0hJkixoERRWgQGl8HaTWLM+/74nUwEQVg5G3aXVgX7UqarioyvPFEql4OKTAlgwM8aw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16125451086111011.3815030852204; Fri, 5 Feb 2021 09:11:48 -0800 (PST) Received: from localhost ([::1]:59360 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l84e3-0002Rw-DQ for importer@patchew.org; Fri, 05 Feb 2021 12:11:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42780) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l84H0-0002ie-Dy for qemu-devel@nongnu.org; Fri, 05 Feb 2021 11:48:06 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56668) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l84Gw-0006yP-7W for qemu-devel@nongnu.org; Fri, 05 Feb 2021 11:47:58 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-325-cKmK_PrQORCxr9VpQCsAQg-1; Fri, 05 Feb 2021 11:47:45 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7D6EC804022; Fri, 5 Feb 2021 16:47:43 +0000 (UTC) Received: from localhost (ovpn-112-54.ams2.redhat.com [10.36.112.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 386B25D9DB; Fri, 5 Feb 2021 16:47:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612543669; 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=2lrRbdrWGI2XS27dloKIEn7pP33fpAuvfxOU0lrsdRg=; b=MsnNw9MNVlvFshBgRQaqHrifaM382hd/ISZmoO0hg77pjMwPdJ+YbbAbNuKVuo+/tXSTZ0 iymIPAQcrSOO7IIqKYE17L0exaXDsJKGMszQF5heXH8hsDZmBJ695pdp3kKvjXik9wBuIA VOYDhNzBBv4OsTZF/eJrmNphN40spyQ= X-MC-Unique: cKmK_PrQORCxr9VpQCsAQg-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL v3 14/27] io: add qio_channel_readv_full_all_eof & qio_channel_readv_full_all helpers Date: Fri, 5 Feb 2021 16:44:46 +0000 Message-Id: <20210205164459.432714-15-stefanha@redhat.com> In-Reply-To: <20210205164459.432714-1-stefanha@redhat.com> References: <20210205164459.432714-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.352, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , John G Johnson , Thomas Huth , Jagannathan Raman , Stefan Hajnoczi , qemu-block@nongnu.org, "Michael S. Tsirkin" , "Denis V. Lunev" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Elena Ufimtseva , Wainer dos Santos Moschetta , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Elena Ufimtseva Adds qio_channel_readv_full_all_eof() and qio_channel_readv_full_all() to read both data and FDs. Refactors existing code to use these helpers. Signed-off-by: Elena Ufimtseva Signed-off-by: John G Johnson Signed-off-by: Jagannathan Raman Acked-by: Daniel P. Berrang=C3=A9 Message-id: b059c4cc0fb741e794d644c144cc21372cad877d.1611938319.git.jag.ram= an@oracle.com Signed-off-by: Stefan Hajnoczi --- include/io/channel.h | 53 +++++++++++++++++++++++ io/channel.c | 101 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 134 insertions(+), 20 deletions(-) diff --git a/include/io/channel.h b/include/io/channel.h index 19e76fc32f..88988979f8 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -777,6 +777,59 @@ void qio_channel_set_aio_fd_handler(QIOChannel *ioc, IOHandler *io_write, void *opaque); =20 +/** + * qio_channel_readv_full_all_eof: + * @ioc: the channel object + * @iov: the array of memory regions to read data to + * @niov: the length of the @iov array + * @fds: an array of file handles to read + * @nfds: number of file handles in @fds + * @errp: pointer to a NULL-initialized error object + * + * + * Performs same function as qio_channel_readv_all_eof. + * Additionally, attempts to read file descriptors shared + * over the channel. The function will wait for all + * requested data to be read, yielding from the current + * coroutine if required. data refers to both file + * descriptors and the iovs. + * + * Returns: 1 if all bytes were read, 0 if end-of-file + * occurs without data, or -1 on error + */ + +int qio_channel_readv_full_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + int **fds, size_t *nfds, + Error **errp); + +/** + * qio_channel_readv_full_all: + * @ioc: the channel object + * @iov: the array of memory regions to read data to + * @niov: the length of the @iov array + * @fds: an array of file handles to read + * @nfds: number of file handles in @fds + * @errp: pointer to a NULL-initialized error object + * + * + * Performs same function as qio_channel_readv_all_eof. + * Additionally, attempts to read file descriptors shared + * over the channel. The function will wait for all + * requested data to be read, yielding from the current + * coroutine if required. data refers to both file + * descriptors and the iovs. + * + * Returns: 0 if all bytes were read, or -1 on error + */ + +int qio_channel_readv_full_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + int **fds, size_t *nfds, + Error **errp); + /** * qio_channel_writev_full_all: * @ioc: the channel object diff --git a/io/channel.c b/io/channel.c index 0d4b8b5160..4555021b62 100644 --- a/io/channel.c +++ b/io/channel.c @@ -91,20 +91,48 @@ int qio_channel_readv_all_eof(QIOChannel *ioc, const struct iovec *iov, size_t niov, Error **errp) +{ + return qio_channel_readv_full_all_eof(ioc, iov, niov, NULL, NULL, errp= ); +} + +int qio_channel_readv_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + Error **errp) +{ + return qio_channel_readv_full_all(ioc, iov, niov, NULL, NULL, errp); +} + +int qio_channel_readv_full_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + int **fds, size_t *nfds, + Error **errp) { int ret =3D -1; struct iovec *local_iov =3D g_new(struct iovec, niov); struct iovec *local_iov_head =3D local_iov; unsigned int nlocal_iov =3D niov; + int **local_fds =3D fds; + size_t *local_nfds =3D nfds; bool partial =3D false; =20 + if (nfds) { + *nfds =3D 0; + } + + if (fds) { + *fds =3D NULL; + } + nlocal_iov =3D iov_copy(local_iov, nlocal_iov, iov, niov, 0, iov_size(iov, niov)); =20 - while (nlocal_iov > 0) { + while ((nlocal_iov > 0) || local_fds) { ssize_t len; - len =3D qio_channel_readv(ioc, local_iov, nlocal_iov, errp); + len =3D qio_channel_readv_full(ioc, local_iov, nlocal_iov, local_f= ds, + local_nfds, errp); if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { if (qemu_in_coroutine()) { qio_channel_yield(ioc, G_IO_IN); @@ -112,20 +140,50 @@ int qio_channel_readv_all_eof(QIOChannel *ioc, qio_channel_wait(ioc, G_IO_IN); } continue; - } else if (len < 0) { - goto cleanup; - } else if (len =3D=3D 0) { - if (partial) { - error_setg(errp, - "Unexpected end-of-file before all bytes were r= ead"); - } else { + } + + if (len =3D=3D 0) { + if (local_nfds && *local_nfds) { + /* + * Got some FDs, but no data yet. This isn't an EOF + * scenario (yet), so carry on to try to read data + * on next loop iteration + */ + goto next_iter; + } else if (!partial) { + /* No fds and no data - EOF before any data read */ ret =3D 0; + goto cleanup; + } else { + len =3D -1; + error_setg(errp, + "Unexpected end-of-file before all data were re= ad"); + /* Fallthrough into len < 0 handling */ + } + } + + if (len < 0) { + /* Close any FDs we previously received */ + if (nfds && fds) { + size_t i; + for (i =3D 0; i < (*nfds); i++) { + close((*fds)[i]); + } + g_free(*fds); + *fds =3D NULL; + *nfds =3D 0; } goto cleanup; } =20 + if (nlocal_iov) { + iov_discard_front(&local_iov, &nlocal_iov, len); + } + +next_iter: partial =3D true; - iov_discard_front(&local_iov, &nlocal_iov, len); + local_fds =3D NULL; + local_nfds =3D NULL; } =20 ret =3D 1; @@ -135,20 +193,23 @@ int qio_channel_readv_all_eof(QIOChannel *ioc, return ret; } =20 -int qio_channel_readv_all(QIOChannel *ioc, - const struct iovec *iov, - size_t niov, - Error **errp) +int qio_channel_readv_full_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + int **fds, size_t *nfds, + Error **errp) { - int ret =3D qio_channel_readv_all_eof(ioc, iov, niov, errp); + int ret =3D qio_channel_readv_full_all_eof(ioc, iov, niov, fds, nfds, = errp); =20 if (ret =3D=3D 0) { - ret =3D -1; - error_setg(errp, - "Unexpected end-of-file before all bytes were read"); - } else if (ret =3D=3D 1) { - ret =3D 0; + error_prepend(errp, + "Unexpected end-of-file before all data were read."); + return -1; } + if (ret =3D=3D 1) { + return 0; + } + return ret; } =20 --=20 2.29.2