From nobody Mon Sep 16 18:59:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1721321352; cv=none; d=zohomail.com; s=zohoarc; b=bgmlAkJBqIXy8hQoukX5kOH9meOlDxN0oamfBSerdsX+KWbVl50KEolBzxlvN6l0wMszVp0xdthffEvA49bfqvKJLmBmQfD3M8P4MDt+Tuh7bV0O/mUgzfmGSl/pRaR4xrb9WCn/FWNvAxwMFfQjoVHKKFTil2IIRaN9BUqK2zo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721321352; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=X4BlkI7qVe6JhZnQJA32AL/5m5qn3kLrFdL0fvOwv9E=; b=GKBkVouOL3eR501aOeA1h5cxS46JSzViHchrP6Dfsz/hEAO0P9mgJhSdwrmFVjLhzzyZa6tgyJulg/KSVLBAl2asghGbhLBQd1KUoBuab0UAFhuzrNiB2rPOnKcbDGl746sACfWaupPqUk5Ved3LNwEn7PoRIVnlKlTU75dzTO8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1721321352812768.5971339302018; Thu, 18 Jul 2024 09:49:12 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.760592.1170476 (Exim 4.92) (envelope-from ) id 1sUUJH-0007Sr-Jh; Thu, 18 Jul 2024 16:48:51 +0000 Received: by outflank-mailman (output) from mailman id 760592.1170476; Thu, 18 Jul 2024 16:48:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJH-0007SJ-E1; Thu, 18 Jul 2024 16:48:51 +0000 Received: by outflank-mailman (input) for mailman id 760592; Thu, 18 Jul 2024 16:48:49 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJF-0007P6-PN for xen-devel@lists.xenproject.org; Thu, 18 Jul 2024 16:48:49 +0000 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [2a00:1450:4864:20::62d]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 9a69529b-4525-11ef-8776-851b0ebba9a2; Thu, 18 Jul 2024 18:48:48 +0200 (CEST) Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a77c4309fc8so114537066b.3 for ; Thu, 18 Jul 2024 09:48:48 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820f2bsm576882466b.206.2024.07.18.09.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:48:46 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9a69529b-4525-11ef-8776-851b0ebba9a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1721321327; x=1721926127; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=X4BlkI7qVe6JhZnQJA32AL/5m5qn3kLrFdL0fvOwv9E=; b=QId9VnrsLlQpKF/Si9WMO4UhvwLsgLrJnr/v/bmCWR8VJ3KISHotq04bwGvBPbkf0T EXiEF6NTW2J0EF6Fka82hC3M87mailb9+uJuxDtkNDq0syvyF8HoHRl+veh9RGrV9IO3 QA1ZtfgC2Fkry4IWAyGzAThNkRq5wZNXADtaw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721321327; x=1721926127; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X4BlkI7qVe6JhZnQJA32AL/5m5qn3kLrFdL0fvOwv9E=; b=lNymHf1wZERyxzuG9ONRi1XmtjBKyS0VQVxr+NFSTBxXyK7oUOAtoBUTHNYMNO8Y6O WGPrf8svILfINM3/z4BHmO/h/RMu4XGaX9eMpDw5vDWzR5uvcpYdAzcXLECQO4tNQgC4 GHD9oTPnOv2OSEYkXa5tb7bVggvI9Gd4GdEbfxOqK7DiELMpzZAHOBNIlbi52G3ZwQGC PWh4+mUBwL4nEdU4qrb+6P1yjNeer4Y6YkoyYlJVfQ10g9NiJe/k1z2KkoZwNIfEHTbQ ilJ0mbB5XZmBX+3VAVJk1LwRGuELbytIs616T+lRfw6ukhRvPNhUb+CDuH3xaB8sTCXt 83qA== X-Gm-Message-State: AOJu0YxWlknmIw23gXBHMoGUw/FrtMoHsxQp/KYEWXNJgi9f/xOOaLJR 2johc89AAuvifOSo0SoYtK2PCh6foxMZLEKDP3DaBuVsVVMwldooJTsi8u0sZ/zfh+gZ4htMW/r E X-Google-Smtp-Source: AGHT+IG1xfD5N/kjpMelrT1RzzK/qXkkDG3AdIB0KNVjemyCKq0rzXa1y6vcnYgDZraTZ/yBRKWF5w== X-Received: by 2002:a17:907:366:b0:a77:d773:54ec with SMTP id a640c23a62f3a-a7a01120edamr333454566b.8.1721321326786; Thu, 18 Jul 2024 09:48:46 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross Subject: [PATCH 1/6] tools/libxs: Fix length check in xs_talkv() Date: Thu, 18 Jul 2024 17:48:37 +0100 Message-Id: <20240718164842.3650702-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240718164842.3650702-1-andrew.cooper3@citrix.com> References: <20240718164842.3650702-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1721321353472116600 Content-Type: text/plain; charset="utf-8" If the sum of iov element lengths overflows, the XENSTORE_PAYLOAD_MAX can pass, after which we'll write 4G of data with a good-looking length field, = and the remainder of the payload will be interpreted as subsequent commands. Check each iov element length for XENSTORE_PAYLOAD_MAX before accmulating i= t. Signed-off-by: Andrew Cooper Reviewed-by: Jason Andryuk Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross --- tools/libs/store/xs.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index ec77379ab9bd..81a790cfe60f 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -571,21 +571,24 @@ static void *xs_talkv(struct xs_handle *h, xs_transac= tion_t t, struct xsd_sockmsg msg; void *ret =3D NULL; int saved_errno; - unsigned int i; + unsigned int i, msg_len; struct sigaction ignorepipe, oldact; =20 msg.tx_id =3D t; msg.req_id =3D 0; msg.type =3D type; - msg.len =3D 0; - for (i =3D 0; i < num_vecs; i++) - msg.len +=3D iovec[i].iov_len; =20 - if (msg.len > XENSTORE_PAYLOAD_MAX) { - errno =3D E2BIG; - return 0; + /* Calculate the payload length by summing iovec elements */ + for (i =3D 0, msg_len =3D 0; i < num_vecs; i++) { + if ((iovec[i].iov_len > XENSTORE_PAYLOAD_MAX) || + ((msg_len +=3D iovec[i].iov_len) > XENSTORE_PAYLOAD_MAX)) { + errno =3D E2BIG; + return 0; + } } =20 + msg.len =3D msg_len; + ignorepipe.sa_handler =3D SIG_IGN; sigemptyset(&ignorepipe.sa_mask); ignorepipe.sa_flags =3D 0; --=20 2.39.2 From nobody Mon Sep 16 18:59:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1721321355; cv=none; d=zohomail.com; s=zohoarc; b=aGID1VlRETrOHW5ksyLG1rl/mCBPou4SLCeCdxiETamJxrkdNniwfJ2ZZTHMwdzWPLHfGoyobxfle0WcFKIPPeqn0vDhLTVtYN5iI9BpsqpKWJj/5ERpkcNKuK7pzGxJKRyMscbtUL7BhxX4KDFff7NqHX9DX6WZRarxY3vEUjo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721321355; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0HmR6V9jK0R+Cz/LsyATivEHCG9ldcpQ0RPlbouApH8=; b=a3CxQij12AS6UNHL5Fc1hHC1RrOkMVO5qxFO/hk+I54UOkocWj9ViIcEyIgiiv3EEqmYcaSsZQG5D2kM1lQXGhiainoIk63oA2d4kk3UW+81s3rshvNKLIWZZXCX6TUf12inHUzI9RU5prGXX86bhQzH1OxI8CG/g4uTEY1O98w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1721321355505314.1966091016311; Thu, 18 Jul 2024 09:49:15 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.760593.1170481 (Exim 4.92) (envelope-from ) id 1sUUJH-0007Yz-SH; Thu, 18 Jul 2024 16:48:51 +0000 Received: by outflank-mailman (output) from mailman id 760593.1170481; Thu, 18 Jul 2024 16:48:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJH-0007Y8-Mp; Thu, 18 Jul 2024 16:48:51 +0000 Received: by outflank-mailman (input) for mailman id 760593; Thu, 18 Jul 2024 16:48:50 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJG-0007PV-Ja for xen-devel@lists.xenproject.org; Thu, 18 Jul 2024 16:48:50 +0000 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [2a00:1450:4864:20::62b]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9b65fb3d-4525-11ef-bbfd-fd08da9f4363; Thu, 18 Jul 2024 18:48:49 +0200 (CEST) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a77cb7c106dso111662166b.1 for ; Thu, 18 Jul 2024 09:48:49 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820f2bsm576882466b.206.2024.07.18.09.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:48:47 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9b65fb3d-4525-11ef-bbfd-fd08da9f4363 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1721321328; x=1721926128; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0HmR6V9jK0R+Cz/LsyATivEHCG9ldcpQ0RPlbouApH8=; b=MUZbOyrwIUoWfEkrkGIJ26qKi7ehHwW+c/q5FMoibEuvpwbYDnfd/q5f4Lm+WlGPuh IM+3qSGm5a/2Ne4DwZwO8mVucJpfmPK46V8oA8v8cU5LXOpQXKGgq2PR2Rf1urjYH05K M+on5Q8bslfbwLFebI88/Z9J3AFQ/m7BK4Di0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721321328; x=1721926128; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0HmR6V9jK0R+Cz/LsyATivEHCG9ldcpQ0RPlbouApH8=; b=fRQsrOpa0nQZ1g2etcNQg7HLHO3lN6n0YQfnjROgUYb0Xt0P8IhPJQUMVYkZ91udR5 eg4NosrYFp2kP8chfdIi2La4rw5k9nR5UH4ZiLz788sbav0IYD+uFtXzXjh/DaNBi2vy 4O8X0XtpCrCX+5hvQkEN85Hq/CPvraWQlvWq+YiRzZpOd57nfB9Fbc9PpsnEaAZ/nrgP n8PMdcYHhSVQ3CE40UdZ30lMQA1H33MwU0YKgU496ak85owyTfwd0gHG855IIqvW89Ym fA2YL3OwNkxD89q1BSlXSLKVFALeTLQGBRiAeLHmIBvuXizInxDMa4coERyzac5mHgAE Czkg== X-Gm-Message-State: AOJu0Yy1YUPO2g/nUJJ8G2QMVe/7dhaagqkAuN6au898nKKKd+JniQQ8 ILYvJRNpGoaWrUpNPt6OhxkOowNaI8cC9i6kU6hKWKD8vRYOqXwjdWdsP79efYv8qgJpiYmnNJY d X-Google-Smtp-Source: AGHT+IE6n8Rs0jIbLNVdEx1ZWBkGqND8epNKrtfnBbw0omr3FF26zxYxVCEy3PN/i+QatkEf27EzAA== X-Received: by 2002:a17:906:504a:b0:a72:76c8:ef68 with SMTP id a640c23a62f3a-a7a013553cbmr358012066b.69.1721321328273; Thu, 18 Jul 2024 09:48:48 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross Subject: [PATCH 2/6] tools/libxs: Rework xs_talkv() to take xsd_sockmsg within the iovec Date: Thu, 18 Jul 2024 17:48:38 +0100 Message-Id: <20240718164842.3650702-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240718164842.3650702-1-andrew.cooper3@citrix.com> References: <20240718164842.3650702-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1721321357218116600 Content-Type: text/plain; charset="utf-8" We would like to writev() the whole outgoing message, which is hard given t= he current need to prepend the locally-constructed xsd_sockmsg. Instead, have the caller provide xsd_sockmsg in iovec[0]. This in turn dro= ps the t and type parameters from xs_talkv(). Note that xs_talkv() may alter the iovec structure. This may happen when writev() is really used under the covers, and it's preforable to having the lower levels need to duplciate the iovec to edit it upon encountering a sho= rt write. xs_directory_part() is the only function impacted by this, and it's easy to rearrange. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jason Andryuk Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross --- tools/libs/store/xs.c | 187 ++++++++++++++++++++++++------------------ 1 file changed, 107 insertions(+), 80 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index 81a790cfe60f..1b5a913a1594 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -561,25 +561,32 @@ static void *read_reply( return body; } =20 -/* Send message to xs, get malloc'ed reply. NULL and set errno on error. = */ -static void *xs_talkv(struct xs_handle *h, xs_transaction_t t, - enum xsd_sockmsg_type type, - const struct iovec *iovec, +/* + * Send message to xenstore, get malloc'ed reply. NULL and set errno on e= rror. + * + * @iovec describes the entire outgoing message, starting with the xsd_soc= kmsg + * header. xs_talkv() calculates the outgoing message length, updating + * xsd_sockmsg in element 0. xs_talkv() might edit the iovec structure in + * place (e.g. following short writes). + */ +static void *xs_talkv(struct xs_handle *h, + struct iovec *iovec, unsigned int num_vecs, unsigned int *len) { - struct xsd_sockmsg msg; + struct xsd_sockmsg *msg =3D iovec[0].iov_base; + enum xsd_sockmsg_type reply_type; void *ret =3D NULL; int saved_errno; unsigned int i, msg_len; struct sigaction ignorepipe, oldact; =20 - msg.tx_id =3D t; - msg.req_id =3D 0; - msg.type =3D type; + /* Element 0 must be xsd_sockmsg */ + assert(num_vecs >=3D 1); + assert(iovec[0].iov_len =3D=3D sizeof(*msg)); =20 /* Calculate the payload length by summing iovec elements */ - for (i =3D 0, msg_len =3D 0; i < num_vecs; i++) { + for (i =3D 1, msg_len =3D 0; i < num_vecs; i++) { if ((iovec[i].iov_len > XENSTORE_PAYLOAD_MAX) || ((msg_len +=3D iovec[i].iov_len) > XENSTORE_PAYLOAD_MAX)) { errno =3D E2BIG; @@ -587,7 +594,7 @@ static void *xs_talkv(struct xs_handle *h, xs_transacti= on_t t, } } =20 - msg.len =3D msg_len; + msg->len =3D msg_len; =20 ignorepipe.sa_handler =3D SIG_IGN; sigemptyset(&ignorepipe.sa_mask); @@ -596,28 +603,25 @@ static void *xs_talkv(struct xs_handle *h, xs_transac= tion_t t, =20 mutex_lock(&h->request_mutex); =20 - if (!xs_write_all(h->fd, &msg, sizeof(msg))) - goto fail; - for (i =3D 0; i < num_vecs; i++) if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len)) goto fail; =20 - ret =3D read_reply(h, &msg.type, len); + ret =3D read_reply(h, &reply_type, len); if (!ret) goto fail; =20 mutex_unlock(&h->request_mutex); =20 sigaction(SIGPIPE, &oldact, NULL); - if (msg.type =3D=3D XS_ERROR) { + if (reply_type =3D=3D XS_ERROR) { saved_errno =3D get_error(ret); free(ret); errno =3D saved_errno; return NULL; } =20 - if (msg.type !=3D type) { + if (reply_type !=3D msg->type) { free(ret); saved_errno =3D EBADF; goto close_fd; @@ -650,11 +654,15 @@ static void *xs_single(struct xs_handle *h, xs_transa= ction_t t, const char *string, unsigned int *len) { - struct iovec iovec; + struct xsd_sockmsg msg =3D { .type =3D type, .tx_id =3D t }; + struct iovec iov[2]; + + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D (void *)string; + iov[1].iov_len =3D strlen(string) + 1; =20 - iovec.iov_base =3D (void *)string; - iovec.iov_len =3D strlen(string) + 1; - return xs_talkv(h, t, type, &iovec, 1, len); + return xs_talkv(h, iov, ARRAY_SIZE(iov), len); } =20 static bool xs_bool(char *reply) @@ -691,21 +699,25 @@ static char **xs_directory_common(char *strings, unsi= gned int len, static char **xs_directory_part(struct xs_handle *h, xs_transaction_t t, const char *path, unsigned int *num) { + struct xsd_sockmsg msg =3D { .type =3D XS_DIRECTORY_PART, .tx_id =3D t }; unsigned int off, result_len; char gen[24], offstr[8]; - struct iovec iovec[2]; + struct iovec iov[3]; char *result =3D NULL, *strings =3D NULL; =20 memset(gen, 0, sizeof(gen)); - iovec[0].iov_base =3D (void *)path; - iovec[0].iov_len =3D strlen(path) + 1; =20 for (off =3D 0;;) { snprintf(offstr, sizeof(offstr), "%u", off); - iovec[1].iov_base =3D (void *)offstr; - iovec[1].iov_len =3D strlen(offstr) + 1; - result =3D xs_talkv(h, t, XS_DIRECTORY_PART, iovec, 2, - &result_len); + + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D (void *)path; + iov[1].iov_len =3D strlen(path) + 1; + iov[2].iov_base =3D (void *)offstr; + iov[2].iov_len =3D strlen(offstr) + 1; + + result =3D xs_talkv(h, iov, ARRAY_SIZE(iov), &result_len); =20 /* If XS_DIRECTORY_PART isn't supported return E2BIG. */ if (!result) { @@ -774,15 +786,17 @@ void *xs_read(struct xs_handle *h, xs_transaction_t t, bool xs_write(struct xs_handle *h, xs_transaction_t t, const char *path, const void *data, unsigned int len) { - struct iovec iovec[2]; + struct xsd_sockmsg msg =3D { .type =3D XS_WRITE, .tx_id =3D t }; + struct iovec iov[3]; =20 - iovec[0].iov_base =3D (void *)path; - iovec[0].iov_len =3D strlen(path) + 1; - iovec[1].iov_base =3D (void *)data; - iovec[1].iov_len =3D len; + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D (void *)path; + iov[1].iov_len =3D strlen(path) + 1; + iov[2].iov_base =3D (void *)data; + iov[2].iov_len =3D len; =20 - return xs_bool(xs_talkv(h, t, XS_WRITE, iovec, - ARRAY_SIZE(iovec), NULL)); + return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL)); } =20 /* Create a new directory. @@ -846,34 +860,37 @@ bool xs_set_permissions(struct xs_handle *h, struct xs_permissions *perms, unsigned int num_perms) { + struct xsd_sockmsg msg =3D { .type =3D XS_SET_PERMS, .tx_id =3D t }; unsigned int i; - struct iovec iov[1+num_perms]; + struct iovec iov[2 + num_perms]; + + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D (void *)path; + iov[1].iov_len =3D strlen(path) + 1; =20 - iov[0].iov_base =3D (void *)path; - iov[0].iov_len =3D strlen(path) + 1; -=09 for (i =3D 0; i < num_perms; i++) { char buffer[MAX_STRLEN(unsigned int)+1]; =20 if (!xenstore_perm_to_string(&perms[i], buffer, sizeof(buffer))) goto unwind; =20 - iov[i+1].iov_base =3D strdup(buffer); - iov[i+1].iov_len =3D strlen(buffer) + 1; + iov[i + 2].iov_base =3D strdup(buffer); + iov[i + 2].iov_len =3D strlen(buffer) + 1; if (!iov[i+1].iov_base) goto unwind; } =20 - if (!xs_bool(xs_talkv(h, t, XS_SET_PERMS, iov, 1+num_perms, NULL))) + if (!xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL))) goto unwind; for (i =3D 0; i < num_perms; i++) - free(iov[i+1].iov_base); + free(iov[i + 2].iov_base); return true; =20 unwind: num_perms =3D i; for (i =3D 0; i < num_perms; i++) - free_no_errno(iov[i+1].iov_base); + free_no_errno(iov[i + 2].iov_base); return false; } =20 @@ -890,7 +907,8 @@ bool xs_restrict(struct xs_handle *h, unsigned domid) */ bool xs_watch(struct xs_handle *h, const char *path, const char *token) { - struct iovec iov[2]; + struct xsd_sockmsg msg =3D { .type =3D XS_WATCH }; + struct iovec iov[3]; =20 #ifdef USE_PTHREAD #define DEFAULT_THREAD_STACKSIZE (16 * 1024) @@ -948,13 +966,14 @@ bool xs_watch(struct xs_handle *h, const char *path, = const char *token) mutex_unlock(&h->request_mutex); #endif =20 - iov[0].iov_base =3D (void *)path; - iov[0].iov_len =3D strlen(path) + 1; - iov[1].iov_base =3D (void *)token; - iov[1].iov_len =3D strlen(token) + 1; + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D (void *)path; + iov[1].iov_len =3D strlen(path) + 1; + iov[2].iov_base =3D (void *)token; + iov[2].iov_len =3D strlen(token) + 1; =20 - return xs_bool(xs_talkv(h, XBT_NULL, XS_WATCH, iov, - ARRAY_SIZE(iov), NULL)); + return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL)); } =20 =20 @@ -1067,20 +1086,22 @@ char **xs_read_watch(struct xs_handle *h, unsigned = int *num) */ bool xs_unwatch(struct xs_handle *h, const char *path, const char *token) { - struct iovec iov[2]; + struct xsd_sockmsg sockmsg =3D { .type =3D XS_UNWATCH }; + struct iovec iov[3]; struct xs_stored_msg *msg, *tmsg; bool res; char *s, *p; unsigned int i; char *l_token, *l_path; =20 - iov[0].iov_base =3D (char *)path; - iov[0].iov_len =3D strlen(path) + 1; - iov[1].iov_base =3D (char *)token; - iov[1].iov_len =3D strlen(token) + 1; + iov[0].iov_base =3D &sockmsg; + iov[0].iov_len =3D sizeof(sockmsg); + iov[1].iov_base =3D (char *)path; + iov[1].iov_len =3D strlen(path) + 1; + iov[2].iov_base =3D (char *)token; + iov[2].iov_len =3D strlen(token) + 1; =20 - res =3D xs_bool(xs_talkv(h, XBT_NULL, XS_UNWATCH, iov, - ARRAY_SIZE(iov), NULL)); + res =3D xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL)); =20 if (!h->unwatch_filter) /* Don't filter the watch list */ return res; @@ -1173,43 +1194,47 @@ bool xs_introduce_domain(struct xs_handle *h, unsigned int domid, unsigned long mfn, unsigned int eventchn) { + struct xsd_sockmsg msg =3D { .type =3D XS_INTRODUCE }; char domid_str[MAX_STRLEN(domid)]; char mfn_str[MAX_STRLEN(mfn)]; char eventchn_str[MAX_STRLEN(eventchn)]; - struct iovec iov[3]; + struct iovec iov[4]; =20 snprintf(domid_str, sizeof(domid_str), "%u", domid); snprintf(mfn_str, sizeof(mfn_str), "%lu", mfn); snprintf(eventchn_str, sizeof(eventchn_str), "%u", eventchn); =20 - iov[0].iov_base =3D domid_str; - iov[0].iov_len =3D strlen(domid_str) + 1; - iov[1].iov_base =3D mfn_str; - iov[1].iov_len =3D strlen(mfn_str) + 1; - iov[2].iov_base =3D eventchn_str; - iov[2].iov_len =3D strlen(eventchn_str) + 1; + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D domid_str; + iov[1].iov_len =3D strlen(domid_str) + 1; + iov[2].iov_base =3D mfn_str; + iov[2].iov_len =3D strlen(mfn_str) + 1; + iov[3].iov_base =3D eventchn_str; + iov[3].iov_len =3D strlen(eventchn_str) + 1; =20 - return xs_bool(xs_talkv(h, XBT_NULL, XS_INTRODUCE, iov, - ARRAY_SIZE(iov), NULL)); + return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL)); } =20 bool xs_set_target(struct xs_handle *h, unsigned int domid, unsigned int target) { + struct xsd_sockmsg msg =3D { .type =3D XS_SET_TARGET }; char domid_str[MAX_STRLEN(domid)]; char target_str[MAX_STRLEN(target)]; - struct iovec iov[2]; + struct iovec iov[3]; =20 snprintf(domid_str, sizeof(domid_str), "%u", domid); snprintf(target_str, sizeof(target_str), "%u", target); =20 - iov[0].iov_base =3D domid_str; - iov[0].iov_len =3D strlen(domid_str) + 1; - iov[1].iov_base =3D target_str; - iov[1].iov_len =3D strlen(target_str) + 1; + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D domid_str; + iov[1].iov_len =3D strlen(domid_str) + 1; + iov[2].iov_base =3D target_str; + iov[2].iov_len =3D strlen(target_str) + 1; =20 - return xs_bool(xs_talkv(h, XBT_NULL, XS_SET_TARGET, iov, - ARRAY_SIZE(iov), NULL)); + return xs_bool(xs_talkv(h, iov, ARRAY_SIZE(iov), NULL)); } =20 static void * single_with_domid(struct xs_handle *h, @@ -1305,15 +1330,17 @@ int xs_suspend_evtchn_port(int domid) char *xs_control_command(struct xs_handle *h, const char *cmd, void *data, unsigned int len) { - struct iovec iov[2]; + struct xsd_sockmsg msg =3D { .type =3D XS_CONTROL }; + struct iovec iov[3]; =20 - iov[0].iov_base =3D (void *)cmd; - iov[0].iov_len =3D strlen(cmd) + 1; - iov[1].iov_base =3D data; - iov[1].iov_len =3D len; + iov[0].iov_base =3D &msg; + iov[0].iov_len =3D sizeof(msg); + iov[1].iov_base =3D (void *)cmd; + iov[1].iov_len =3D strlen(cmd) + 1; + iov[2].iov_base =3D data; + iov[2].iov_len =3D len; =20 - return xs_talkv(h, XBT_NULL, XS_CONTROL, iov, - ARRAY_SIZE(iov), NULL); + return xs_talkv(h, iov, ARRAY_SIZE(iov), NULL); } =20 char *xs_debug_command(struct xs_handle *h, const char *cmd, --=20 2.39.2 From nobody Mon Sep 16 18:59:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1721321358; cv=none; d=zohomail.com; s=zohoarc; b=DSwC6+W9qgFk2UA6Dga8nf9ybDX66S5SbrAUJf0QFNx1AsKd7R3KrW+InpTHKvVTCslrrC5CVBmlT/JZIL2VxPMEpqQGhBZBudSOZAfa4UfgZiXY+UmCKQ4Rp5FyghML3ulUtCkJJFEQmNlZiZ6ttsvWlEb6Z3CfHwB5y0DmVt4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721321358; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Xuo7u/zXkhIBsvPwcy6H4PeF57mIJAldQoYHTVo90WI=; b=ecHiHJVAesf55mblLsxyEkmJ4PZyz04urcQFOzM/01bXkW0aMi+Y1yYmVhx4C/9CwZm9ZQAh+xC8hCB9wXGZAVFhBthsdoJgbMum4IQXy4S1dbSdCe4udvzuFe8gp/DGdFS3DtrnAsZmDiEAdXUAx5zrA003pGgY7eBXBNbO0Ro= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1721321358346945.093626050356; Thu, 18 Jul 2024 09:49:18 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.760594.1170490 (Exim 4.92) (envelope-from ) id 1sUUJI-0007iZ-9h; Thu, 18 Jul 2024 16:48:52 +0000 Received: by outflank-mailman (output) from mailman id 760594.1170490; Thu, 18 Jul 2024 16:48:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJI-0007hK-1l; Thu, 18 Jul 2024 16:48:52 +0000 Received: by outflank-mailman (input) for mailman id 760594; Thu, 18 Jul 2024 16:48:50 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJG-0007PV-UA for xen-devel@lists.xenproject.org; Thu, 18 Jul 2024 16:48:50 +0000 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [2a00:1450:4864:20::129]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9bc77dde-4525-11ef-bbfd-fd08da9f4363; Thu, 18 Jul 2024 18:48:50 +0200 (CEST) Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-52ea79e6979so731745e87.2 for ; Thu, 18 Jul 2024 09:48:50 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820f2bsm576882466b.206.2024.07.18.09.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:48:48 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9bc77dde-4525-11ef-bbfd-fd08da9f4363 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1721321329; x=1721926129; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Xuo7u/zXkhIBsvPwcy6H4PeF57mIJAldQoYHTVo90WI=; b=mflds8n3s2rRfb4Muk97juUZW++hDeYqlmL84wvRmuj3kKD2GhAoddkNCgl1PkapCf R+A6G2LDlOttVd8I7OenmyiCAOR6+WEH0zgwf6x7IrVMEVYhS1yqjSMrJR6GENkhpGcG sLINNTRKGkPn0kdTjZXorA3SXXEU833lPcpVU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721321329; x=1721926129; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xuo7u/zXkhIBsvPwcy6H4PeF57mIJAldQoYHTVo90WI=; b=UEKH17Zn7FnN9TdZ0G+accpKZbScqOvGjq3d88OKb3rP7IMWRlJR1p4gT1qWr0imJe Ah/JviVT9fAb/KNfzm00T03K7d3dpF+h9sb3eIR9yBnqVzWcq9afR/fZzp/xQ6Q0oglQ gJDAam9SIntDWVruMRN5EKHqubMiuhXnyHWbFKuIMLWbNAtVMrEYvsUUcPnvGDXY+Fm7 Qb58kNhOhrS/w0uULJQFBhpPe9MFiYnXEQQLYm7D4NK2KpA4fVi8pRNt5Jh9IobEwTKS rFqVvdtZXugHbzelT6cjqKQgU2EI3sEFJQVuTiSeehRQirjVX69Q1XSRkQ+ysw8HfkIE JJ+g== X-Gm-Message-State: AOJu0YyIWj+05o5FZ5gqESj5+1bg5gdJcdsRtPnPb5i+CR82u94EwP1K kH7V6RSmzW6fRaEykhZlnVUZpMQBMgDAzFD6xz9bZS5VlYCng5pvwDcBRjPd21hrcjLHc1JvEXS 9 X-Google-Smtp-Source: AGHT+IH+I4nN2nmQzuvSuwTsqBxTrwMpY51AkGdJw+JG371v4KDIMQo1MbwaSewsmi5uAI1yd9joww== X-Received: by 2002:a05:6512:3ba8:b0:52c:dfe1:44ea with SMTP id 2adb3069b0e04-52ee53a2502mr4486308e87.6.1721321328990; Thu, 18 Jul 2024 09:48:48 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross Subject: [PATCH 3/6] tools/libxs: Rationalise the definition of struct xs_handle Date: Thu, 18 Jul 2024 17:48:39 +0100 Message-Id: <20240718164842.3650702-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240718164842.3650702-1-andrew.cooper3@citrix.com> References: <20240718164842.3650702-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1721321359206116600 Content-Type: text/plain; charset="utf-8" Right now there are two completely different struct xs_handle definitions, depend on #ifdef USE_PTHREAD. One is especially well hidden, and often escapes updates. Rework struct xs_handle using some interior ifdefary. It's slightly longer, but much easier to follow. Importanly, this makes it much harder to forget the !PTHREAD case when adding a "common" variable. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jason Andryuk Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross --- tools/libs/store/xs.c | 77 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index 1b5a913a1594..96ea2b192924 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -40,6 +40,14 @@ #include #include =20 +#ifdef USE_PTHREAD +# include +#endif + +#ifdef USE_DLSYM +# include +#endif + #ifndef O_CLOEXEC #define O_CLOEXEC 0 #endif @@ -54,14 +62,6 @@ struct xs_stored_msg { char *body; }; =20 -#ifdef USE_PTHREAD - -#include - -#ifdef USE_DLSYM -#include -#endif - struct xs_handle { /* Communications channel to xenstore daemon. */ int fd; @@ -71,16 +71,20 @@ struct xs_handle { * A read thread which pulls messages off the comms channel and * signals waiters. */ +#ifdef USE_PTHREAD pthread_t read_thr; int read_thr_exists; +#endif =20 /* * A list of fired watch messages, protected by a mutex. Users can * wait on the conditional variable until a watch is pending. */ XEN_TAILQ_HEAD(, struct xs_stored_msg) watch_list; +#ifdef USE_PTHREAD pthread_mutex_t watch_mutex; pthread_cond_t watch_condvar; +#endif =20 /* Clients can select() on this pipe to wait for a watch to fire. */ int watch_pipe[2]; @@ -93,6 +97,7 @@ struct xs_handle { * conditional variable for its response. */ XEN_TAILQ_HEAD(, struct xs_stored_msg) reply_list; +#ifdef USE_PTHREAD pthread_mutex_t reply_mutex; pthread_cond_t reply_condvar; =20 @@ -112,55 +117,47 @@ struct xs_handle { * reply_mutex * watch_mutex */ +#endif }; =20 -#define mutex_lock(m) pthread_mutex_lock(m) -#define mutex_unlock(m) pthread_mutex_unlock(m) -#define condvar_signal(c) pthread_cond_signal(c) -#define condvar_wait(c,m) pthread_cond_wait(c,m) -#define cleanup_push(f, a) \ - pthread_cleanup_push((void (*)(void *))(f), (void *)(a)) + +#ifdef USE_PTHREAD + +# define mutex_lock(m) pthread_mutex_lock(m) +# define mutex_unlock(m) pthread_mutex_unlock(m) +# define condvar_signal(c) pthread_cond_signal(c) +# define condvar_wait(c, m) pthread_cond_wait(c, m) +# define cleanup_push(f, a) pthread_cleanup_push((void (*)(void *))= (f), (void *)(a)) /* * Some definitions of pthread_cleanup_pop() are a macro starting with an * end-brace. GCC then complains if we immediately precede that with a lab= el. * Hence we insert a dummy statement to appease the compiler in this situa= tion. */ -#define cleanup_pop(run) ((void)0); pthread_cleanup_pop(run) +# define cleanup_pop(run) ((void)0); pthread_cleanup_pop(run) =20 -#define read_thread_exists(h) (h->read_thr_exists) +# define read_thread_exists(h) ((h)->read_thr_exists) =20 /* Because pthread_cleanup_p* are not available when USE_PTHREAD is * disabled, use these macros which convert appropriately. */ -#define cleanup_push_heap(p) cleanup_push(free, p) -#define cleanup_pop_heap(run, p) cleanup_pop((run)) +# define cleanup_push_heap(p) cleanup_push(free, p) +# define cleanup_pop_heap(run, p) cleanup_pop((run)) =20 static void *read_thread(void *arg); =20 -#else /* !defined(USE_PTHREAD) */ +#else /* USE_PTHREAD */ =20 -struct xs_handle { - int fd; - Xentoolcore__Active_Handle tc_ah; /* for restrict */ - XEN_TAILQ_HEAD(, struct xs_stored_msg) reply_list; - XEN_TAILQ_HEAD(, struct xs_stored_msg) watch_list; - /* Clients can select() on this pipe to wait for a watch to fire. */ - int watch_pipe[2]; - /* Filtering watch event in unwatch function? */ - bool unwatch_filter; -}; +# define mutex_lock(m) ((void)0) +# define mutex_unlock(m) ((void)0) +# define condvar_signal(c) ((void)0) +# define condvar_wait(c, m) ((void)0) +# define cleanup_push(f, a) ((void)0) +# define cleanup_pop(run) ((void)0) +# define read_thread_exists(h) (0) +# define cleanup_push_heap(p) ((void)0) +# define cleanup_pop_heap(run, p) do { if ((run)) free(p); } while(0) =20 -#define mutex_lock(m) ((void)0) -#define mutex_unlock(m) ((void)0) -#define condvar_signal(c) ((void)0) -#define condvar_wait(c,m) ((void)0) -#define cleanup_push(f, a) ((void)0) -#define cleanup_pop(run) ((void)0) -#define read_thread_exists(h) (0) +#endif /* !USE_PTHREAD */ =20 -#define cleanup_push_heap(p) ((void)0) -#define cleanup_pop_heap(run, p) do { if ((run)) free(p); } while(0) - -#endif =20 static int read_message(struct xs_handle *h, int nonblocking); =20 --=20 2.39.2 From nobody Mon Sep 16 18:59:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1721321357; cv=none; d=zohomail.com; s=zohoarc; b=Tt+PyM1Va39Cv7iKunfly6wp3K67EqSz6KGsvRDCvPRwWBolHjFhsPbDsnwOncabGzkb9gxCORv9BLwtchpLKVLmhcBi+Xfa9oUv4h7epT3bt5fZbnZ1RroEQL3uo+Uq5tj+e5zytR3UBVNWbFJupv3DmVzwChJaQwGq6FuWcqE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721321357; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=hkTFy1Zu0Gr3zYTHQkG7FDGi6T4zpbFrbx2OTn5XtTQ=; b=HFYtcTgYidozpc78xCs2UtA/rdKbinXzUCMpF1H6L0zUPpMouQ5Knlg7YHSubTN+dZsp0p9+NIrvVQ3Ro2peFdLu3ptNZbQOvjB1EKDHvEqy6JrOILXcMhmxfbmhujjAyIJfs90jnbPQI7mAaAPyXHwlL6NOsnnrT/U3z3cktvc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1721321357605673.0556600367971; Thu, 18 Jul 2024 09:49:17 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.760595.1170510 (Exim 4.92) (envelope-from ) id 1sUUJJ-0008JH-LP; Thu, 18 Jul 2024 16:48:53 +0000 Received: by outflank-mailman (output) from mailman id 760595.1170510; Thu, 18 Jul 2024 16:48:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJJ-0008IC-Fl; Thu, 18 Jul 2024 16:48:53 +0000 Received: by outflank-mailman (input) for mailman id 760595; Thu, 18 Jul 2024 16:48:52 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJI-0007PV-09 for xen-devel@lists.xenproject.org; Thu, 18 Jul 2024 16:48:52 +0000 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [2a00:1450:4864:20::634]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9c7d0fc6-4525-11ef-bbfd-fd08da9f4363; Thu, 18 Jul 2024 18:48:51 +0200 (CEST) Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-a77e85cb9b4so130933866b.0 for ; Thu, 18 Jul 2024 09:48:51 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820f2bsm576882466b.206.2024.07.18.09.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:48:49 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9c7d0fc6-4525-11ef-bbfd-fd08da9f4363 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1721321330; x=1721926130; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hkTFy1Zu0Gr3zYTHQkG7FDGi6T4zpbFrbx2OTn5XtTQ=; b=mUMPKZtcMV/1kx2RfqtvJVAKaULrCxEgcH7W8w9rQsJTm+2Th1VPBHI15BCnG1SR3h 5Y5R2LeTe0+JTns7TA+SripEnTQIleUupBrRv66tWHjX8hxlY/7hF/KHABRyZqbt8y4R rwH69D3UDdWnoBgQ5KNushp8AechEPDnIk8Gs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721321330; x=1721926130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hkTFy1Zu0Gr3zYTHQkG7FDGi6T4zpbFrbx2OTn5XtTQ=; b=IiesVEgBeRmG47r65AMmRksvsgS6bpkXlpJjYnmx27AI+DmouXFASNnJXWXTu4e51e Lo21Mw0L48dwiaLvSIRBZj0ByD7Ybgo4aotg2FUojbGR8/mJxhjok7seT0GqHfw6gBjR UJiv8M1JKJZPRVHT0GMxZCRprV5G3H6Y2UJ6DwQ2293AAwF8HyverDeDBoLBRPf+/oTj EFZ8Ii6dmSJm9kKvTlOJwBzKo3Maz1+KYmMhnucGaAr09ZYC3GomGgkqpqu4qqxPtPb/ 6p4y3NRtVOvuPtSM7nrRmr/g+U1Q+IfNVbtZnezshAGlZPbpedda2zuw8PCUv9VAod0C Mc7w== X-Gm-Message-State: AOJu0Yxy624AuvsObaG31a2h864jp37XmmAeSJ+XiYkneOkxwyd0J8VD 0wbvJ8u2dhUBEE74oYKoIkMyVrSQAbqbNW5XH8u6EY7z5o/rHREnn5eTT8gOvgXcrquTVjv3ycT X X-Google-Smtp-Source: AGHT+IEd3YKoTQS49yqTGntO+HlOOzWaMziAkw4OTK8/nbvgjO2uP5cSTIwTE7Qy4RgevDhVddCB+w== X-Received: by 2002:a17:906:4c51:b0:a77:c0f5:69d1 with SMTP id a640c23a62f3a-a7a01352d79mr392894966b.60.1721321330513; Thu, 18 Jul 2024 09:48:50 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross Subject: [PATCH 4/6] tools/libxs: Track whether we're using a socket or file Date: Thu, 18 Jul 2024 17:48:40 +0100 Message-Id: <20240718164842.3650702-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240718164842.3650702-1-andrew.cooper3@citrix.com> References: <20240718164842.3650702-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1721321359168116600 Content-Type: text/plain; charset="utf-8" It will determine whether to use writev() or sendmsg(). Signed-off-by: Andrew Cooper Reviewed-by: Jason Andryuk Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross --- tools/libs/store/xs.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index 96ea2b192924..f4edeb05f03b 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -65,6 +65,9 @@ struct xs_stored_msg { struct xs_handle { /* Communications channel to xenstore daemon. */ int fd; + + bool is_socket; /* is @fd a file or socket? */ + Xentoolcore__Active_Handle tc_ah; /* for restrict */ =20 /* @@ -305,6 +308,8 @@ static struct xs_handle *get_handle(const char *connect= _to) if (h->fd =3D=3D -1) goto err; =20 + h->is_socket =3D S_ISSOCK(buf.st_mode); + XEN_TAILQ_INIT(&h->reply_list); XEN_TAILQ_INIT(&h->watch_list); =20 --=20 2.39.2 From nobody Mon Sep 16 18:59:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1721321357; cv=none; d=zohomail.com; s=zohoarc; b=Yeog2MyJMJ1lOfJ/rahsIlyLE5RLlomdEgnhQeTPmsHTMXgL6iFBKO0NJNpSK034uk+6sniIIlaJDMbV4SUVkDq6OryDMsjMujKqjurZ/ncxtPK0R5U28473fQBAZ0bigpGzTJU7LtygL9sjnqyQEjeoNoPmlOGUOzRWSbpthrY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721321357; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=yYc4CYlNYiXlhGTE3F3Z17D5dfT00FoaAtahQbVBGvo=; b=fUFASl/GNkHu3BNsYt/5NdveCg56oBgkfDC2x8IQrJg6E0LgFnuOn8/UTHeFWE69CauT9fKQNSVjbSm/vXwZNxDBneDIpNnw3EkXVcq3uB1w21mWZPHeLzsmYApHZw+VN06GYSfft8cro/Ke4akPSJRza38pdOshRUgYstpzTEM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 17213213576966.478570181265013; Thu, 18 Jul 2024 09:49:17 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.760598.1170530 (Exim 4.92) (envelope-from ) id 1sUUJM-0000Rm-63; Thu, 18 Jul 2024 16:48:56 +0000 Received: by outflank-mailman (output) from mailman id 760598.1170530; Thu, 18 Jul 2024 16:48:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJM-0000RT-2j; Thu, 18 Jul 2024 16:48:56 +0000 Received: by outflank-mailman (input) for mailman id 760598; Thu, 18 Jul 2024 16:48:54 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJK-0007P6-OC for xen-devel@lists.xenproject.org; Thu, 18 Jul 2024 16:48:54 +0000 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [2a00:1450:4864:20::230]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 9d8477d4-4525-11ef-8776-851b0ebba9a2; Thu, 18 Jul 2024 18:48:53 +0200 (CEST) Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2eeef45d865so13798371fa.0 for ; Thu, 18 Jul 2024 09:48:53 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820f2bsm576882466b.206.2024.07.18.09.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:48:50 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9d8477d4-4525-11ef-8776-851b0ebba9a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1721321332; x=1721926132; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yYc4CYlNYiXlhGTE3F3Z17D5dfT00FoaAtahQbVBGvo=; b=XwKwEFgd0gPh1dsPy587tpI62YA4S3W96UeQ3afyyO92rhQjwqSJMUxVrwUQolNi0j anD7lc2vVEfHGj2ObLxgDutwMcV8s020d6e1fJY8Pe6xFhu6gPdiJiitU5f8UH0Nd/nv 8y+dt0souw3scDL4bjf+AlES5omp6jHlkOfi0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721321332; x=1721926132; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yYc4CYlNYiXlhGTE3F3Z17D5dfT00FoaAtahQbVBGvo=; b=dIjTrRttSs32iY2q8551YvE8MdqsPB9jnnA1p7ScEeyLK131A5ec1MQfMzz45+yTVK Awf9ixE3msu6fZxZhExFm31cp6k/X3QpgcPaYcHyI6Q0ouWipuNUg9AkkjnqpWq7n3/J t9b/o2o4XbQ9NrU5TpFy2/3/Sbf0gbf+6JTMcPGNSERGig6Sn8qCDPDf5fB2x4XyaS2c Goll1G/RS2H3vy4HuHcRyyA7fvvQEv8rMrWPijEruYH9JM+n6J+LJmcAwc7MOoZ3IogN VdNbPdqA5vC3UzY/jrfDz/1fl2Uo79HNvfylZdtQgS/GsGTF73QVqYn/Em49Zb/d9xM6 QzKA== X-Gm-Message-State: AOJu0Yxr8Qz7qJ91kgFk0/m9k36Batb3NINU9X6QN9TWvNqxds2vmEA3 807jLiVbUme8zflPXHFb/Qqq1Qrn8bjBSmvq8CDkr5M4GJLK/8pQfI40g8lCno8yci1q2Q6Vry9 q X-Google-Smtp-Source: AGHT+IEY1gnYXw/cRpFVXBzT2Q9vxvDB6ROlWIMl2FDXaLt4meEJFMN2uWHJZXsyiVHGgXIQQMBViw== X-Received: by 2002:a05:6512:6cf:b0:52c:905b:ea5f with SMTP id 2adb3069b0e04-52ee545400amr5237900e87.63.1721321331774; Thu, 18 Jul 2024 09:48:51 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross Subject: [PATCH 5/6] tools/libxs: Use writev()/sendmsg() instead of write() Date: Thu, 18 Jul 2024 17:48:41 +0100 Message-Id: <20240718164842.3650702-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240718164842.3650702-1-andrew.cooper3@citrix.com> References: <20240718164842.3650702-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1721321359190116600 Content-Type: text/plain; charset="utf-8" With the input data now conveniently arranged, use writev()/sendmsg() inste= ad of decomposing it into write() calls. This causes all requests to be submitted with a single system call, rather than at least two. While in principle short writes can occur, the chances = of it happening are slim given that most xenbus comms are only a handful of bytes. Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of resubmitting on EINTR or short write. Signed-off-by: Andrew Cooper --- CC: Anthony PERARD CC: Juergen Gross --- tools/libs/store/xs.c | 104 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index f4edeb05f03b..9db5c02661f5 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -563,6 +563,105 @@ static void *read_reply( return body; } =20 +/* + * Wrapper around sendmsg() to resubmit on EINTR or short write. Returns + * @true if all data was transmitted, or @false with errno for an error. + * Note: May alter @iov in place on resubmit. + */ +static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr) +{ + struct msghdr hdr =3D { + .msg_iov =3D iov, + .msg_iovlen =3D nr, + }; + + /* Sanity check first element isn't empty */ + assert(iov->iov_len =3D=3D sizeof(struct xsd_sockmsg)); + + while (hdr.msg_iovlen) { + ssize_t res =3D sendmsg(fd, &hdr, 0); + + if (res < 0 && errno =3D=3D EINTR) + continue; + if (res <=3D 0) + return false; + + while (res) { + if (res >=3D hdr.msg_iov->iov_len) { + /* Skip full element. */ + res -=3D hdr.msg_iov->iov_len; + hdr.msg_iovlen--; + hdr.msg_iov++; + continue; + } + + /* Partial element, adjust base/len. */ + hdr.msg_iov->iov_len -=3D res; + hdr.msg_iov->iov_base +=3D res; + break; + } + + while (hdr.msg_iov->iov_len =3D=3D 0) { + /* Skip empty element. */ + hdr.msg_iovlen--; + hdr.msg_iov++; + } + } + + return true; +} + +/* + * Wrapper around sendmsg() to resubmit on EINTR or short write. Returns + * @true if all data was transmitted, or @false with errno for an error. + * Note: May alter @iov in place on resubmit. + */ +static bool writev_exact(int fd, struct iovec *iov, unsigned int nr) +{ + /* Sanity check first element isn't empty */ + assert(iov->iov_len =3D=3D sizeof(struct xsd_sockmsg)); + + while (nr) { + ssize_t res =3D writev(fd, iov, nr); + + if (res < 0 && errno =3D=3D EINTR) + continue; + if (res <=3D 0) + return false; + + while (res) { + if (res >=3D iov->iov_len) { + /* Skip full element. */ + res -=3D iov->iov_len; + nr--; + iov++; + continue; + } + + /* Partial element, adjust base/len. */ + iov->iov_len -=3D res; + iov->iov_base +=3D res; + break; + } + + while (iov->iov_len =3D=3D 0) { + /* Skip empty element. */ + nr--; + iov++; + } + } + + return true; +} + +static bool write_request(struct xs_handle *h, struct iovec *iov, unsigned= int nr) +{ + if (h->is_socket) + return sendmsg_exact(h->fd, iov, nr); + else + return writev_exact(h->fd, iov, nr); +} + /* * Send message to xenstore, get malloc'ed reply. NULL and set errno on e= rror. * @@ -605,9 +704,8 @@ static void *xs_talkv(struct xs_handle *h, =20 mutex_lock(&h->request_mutex); =20 - for (i =3D 0; i < num_vecs; i++) - if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len)) - goto fail; + if (!write_request(h, iovec, num_vecs)) + goto fail; =20 ret =3D read_reply(h, &reply_type, len); if (!ret) --=20 2.39.2 From nobody Mon Sep 16 18:59:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1721321351; cv=none; d=zohomail.com; s=zohoarc; b=X6THz1bK2I/jf9LKYRIU2NZS+tvoBIwg6q67yMXyK16aPCuNXvNalESikLcKwTweOUfmOj7HSAhzzgtDeDz3MW0zaA+Wiwbk4/E0cg5jIGO7Hku3hrxMf7zYmo0wu45f0z/J3BygEsWHCdsYtyz8CLUgSvADQddqX0IkeGLs8S4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721321351; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NhFAb8PBQU/57PNqveksaa0wxKn4j+nTJ+P+HwWc7e4=; b=cEalktjbQBMsuCxqVkFyXzvZprUp8XSGZoIPKUviiTf3236dWuQb95NUcnlG33zxoByfwpVuKKxWoHykZZuIV9djRBFdl0oZhPquVLRCm+wUywFVczcj9gVeg9wwmb3q3ryNEKjmDYpg+C2LGpCIqIc2rH4rilFgg+tQvdsaQ7U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1721321351495969.1292667206444; Thu, 18 Jul 2024 09:49:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.760597.1170519 (Exim 4.92) (envelope-from ) id 1sUUJK-00009u-TL; Thu, 18 Jul 2024 16:48:54 +0000 Received: by outflank-mailman (output) from mailman id 760597.1170519; Thu, 18 Jul 2024 16:48:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJK-00009h-PU; Thu, 18 Jul 2024 16:48:54 +0000 Received: by outflank-mailman (input) for mailman id 760597; Thu, 18 Jul 2024 16:48:54 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJK-0007PV-77 for xen-devel@lists.xenproject.org; Thu, 18 Jul 2024 16:48:54 +0000 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [2a00:1450:4864:20::134]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9dcf5687-4525-11ef-bbfd-fd08da9f4363; Thu, 18 Jul 2024 18:48:53 +0200 (CEST) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-52ee4e26790so767008e87.0 for ; Thu, 18 Jul 2024 09:48:53 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820f2bsm576882466b.206.2024.07.18.09.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:48:52 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9dcf5687-4525-11ef-bbfd-fd08da9f4363 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1721321332; x=1721926132; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NhFAb8PBQU/57PNqveksaa0wxKn4j+nTJ+P+HwWc7e4=; b=jQ0bWDdjaR9m+QbYmV2C1XVWkpO8CBWMTmAnftexBYwdblB/ZjNxGmjVtoBEwyw9kg 5JhUaXRHF5PAo10LRe7b//GIDDnSgVPXHehkcrYof1CNu66S/BclbP46CRJT+u/54qZU f5IR4d1LRBItOz9doundZ2VakqtDeULWZrvWw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721321332; x=1721926132; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NhFAb8PBQU/57PNqveksaa0wxKn4j+nTJ+P+HwWc7e4=; b=E97GePdMyUjXhmix57GrMWnS2hwxGDY4vph/cvaeWrzcFgCQPRLLN/PDl5u9LVyAOm sLPyMdMDR0pwMk0RoDIlgcOfqN7Q1ukNMXSw2zIpKWVaSMCeV/KJWkiwcbtt2ECtjvDn CiBXUHEUBAXoz0TaD96KQ/WRiN/vkuPn71pNs6BO6dKNZGcG72HcLhMNheKx2r5mvLv2 qfEJlycnxsEJVcx55q+I3QS0llK0SznRPng7jW5+bGy2Jj/i+W2HabKADgiSH0Npllpw TgF8LDlbZo4oKenwmYiSWWRYTq5uhHREkUp4zWTfgWymv5gKeztIoK5eRNx9eQC7SWLg vC2w== X-Gm-Message-State: AOJu0Yz650rppTEa4oQxWM9cY/UI6n2+A6N1Y9hQAsNYsyG4kqv6CPMO X7/BlR1ItU4+zO+ixQ66Wyqx0dD1XFpGL6RnCJpuq5DKEoB2v/Y2VS61AAzlb7AmiBJVBloIpCs F X-Google-Smtp-Source: AGHT+IG4Fe3mXPxhBviT+OHpGVmgrBeFMtY0KoXy8ywzzLjRF6J/wTS1Q3D3OlChZOth7bazYM/HZA== X-Received: by 2002:a05:6512:1082:b0:52b:bee3:dcc6 with SMTP id 2adb3069b0e04-52ee542de4emr3859041e87.51.1721321332536; Thu, 18 Jul 2024 09:48:52 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross Subject: [PATCH 6/6] tools/libxs: Stop playing with SIGPIPE Date: Thu, 18 Jul 2024 17:48:42 +0100 Message-Id: <20240718164842.3650702-7-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240718164842.3650702-1-andrew.cooper3@citrix.com> References: <20240718164842.3650702-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1721321353441116600 Content-Type: text/plain; charset="utf-8" It's very rude for a library to play with signals behind the back of the application, no matter ones views on the default behaviour of SIGPIPE under POSIX. Even if the application doesn't care about the xenstored socket, it= my care about others. This logic has existed since xenstore/xenstored was originally added in com= mit 29c9e570b1ed ("Add xenstore daemon and library") in 2005. It's also unnecessary. Pass MSG_NOSIGNAL when talking to xenstored over a pipe (to avoid sucumbing to SIGPIPE if xenstored has crashed), and forgo any playing with the signal disposition. This has a side benefit of saving 2 syscalls per xenstore request. Signed-off-by: Andrew Cooper Reviewed-by: Jason Andryuk Reviewed-by: Juergen Gross --- CC: Anthony PERARD CC: Juergen Gross --- tools/libs/store/xs.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index 9db5c02661f5..63d754e17014 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -579,7 +579,7 @@ static bool sendmsg_exact(int fd, struct iovec *iov, un= signed int nr) assert(iov->iov_len =3D=3D sizeof(struct xsd_sockmsg)); =20 while (hdr.msg_iovlen) { - ssize_t res =3D sendmsg(fd, &hdr, 0); + ssize_t res =3D sendmsg(fd, &hdr, MSG_NOSIGNAL); =20 if (res < 0 && errno =3D=3D EINTR) continue; @@ -680,7 +680,6 @@ static void *xs_talkv(struct xs_handle *h, void *ret =3D NULL; int saved_errno; unsigned int i, msg_len; - struct sigaction ignorepipe, oldact; =20 /* Element 0 must be xsd_sockmsg */ assert(num_vecs >=3D 1); @@ -697,11 +696,6 @@ static void *xs_talkv(struct xs_handle *h, =20 msg->len =3D msg_len; =20 - ignorepipe.sa_handler =3D SIG_IGN; - sigemptyset(&ignorepipe.sa_mask); - ignorepipe.sa_flags =3D 0; - sigaction(SIGPIPE, &ignorepipe, &oldact); - mutex_lock(&h->request_mutex); =20 if (!write_request(h, iovec, num_vecs)) @@ -713,7 +707,6 @@ static void *xs_talkv(struct xs_handle *h, =20 mutex_unlock(&h->request_mutex); =20 - sigaction(SIGPIPE, &oldact, NULL); if (reply_type =3D=3D XS_ERROR) { saved_errno =3D get_error(ret); free(ret); @@ -732,7 +725,6 @@ static void *xs_talkv(struct xs_handle *h, /* We're in a bad state, so close fd. */ saved_errno =3D errno; mutex_unlock(&h->request_mutex); - sigaction(SIGPIPE, &oldact, NULL); close_fd: close(h->fd); h->fd =3D -1; --=20 2.39.2