From nobody Sun May 19 00:47:39 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1588085947; cv=none; d=zohomail.com; s=zohoarc; b=J/z1yy+dNxT1IfKG3Pru4BZK8oaQR0cD9IriEGZQJKIcgDAyJ542HT1UdZbOmgjhTL1Zpa+cUp3fAfjbGvjeB7LIVLKb0Q9u0ZAjVn6AbyOrbWvHMtgtr1c41rHMxQIWBQ4tALujiK10jzj9tUip122fGRRLYLm1uZ45PnEmBmw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588085947; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=0D3drNXlVQaxmEnhL/2Xp0I1+efZTUQDc6k8m+914c0=; b=MtuoEbRnc2zA3vZN87FhZecYN/OmLirszmBTsNPDk0igE3jbsCkx/QBZpSqQlk+USJTIN0f4EVQnCPF8Pr9q5Epsrm2xWI3Q05VWEVSCqgRtJ3z6PiY826kulR1KzfgVj/Aurz9svYSa5tTOK6w3q11zGG85LBRrxIvQcWu837M= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1588085947106105.49681250027163; Tue, 28 Apr 2020 07:59:07 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jTRh4-0001Hn-JU; Tue, 28 Apr 2020 14:58:42 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jTRh3-0001Hi-4D for xen-devel@lists.xenproject.org; Tue, 28 Apr 2020 14:58:41 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id bebdd44c-8960-11ea-9887-bc764e2007e4; Tue, 28 Apr 2020 14:58:40 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 77637ACC3; Tue, 28 Apr 2020 14:58:38 +0000 (UTC) X-Inumbo-ID: bebdd44c-8960-11ea-9887-bc764e2007e4 X-Virus-Scanned: by amavisd-new at test-mx.suse.de From: Juergen Gross To: xen-devel@lists.xenproject.org Subject: [PATCH] tools/xenstore: simplify socket initialization Date: Tue, 28 Apr 2020 16:58:37 +0200 Message-Id: <20200428145837.6099-1-jgross@suse.com> X-Mailer: git-send-email 2.16.4 X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Ian Jackson , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The setup of file descriptors for the Xenstore sockets is needlessly complicated: the space is allocated dynamically, while two static variables really would do the job. For tearing down the sockets it is easier to widen the scope of the file descriptors from function to file. Signed-off-by: Juergen Gross Reviewed-by: Wei Liu --- tools/xenstore/xenstored_core.c | 69 ++++++++++++++++---------------------= ---- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 551fe38f57..7bd959f28b 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -70,6 +70,9 @@ static struct pollfd *fds; static unsigned int current_array_size; static unsigned int nr_fds; =20 +static int sock =3D -1; +static int ro_sock =3D -1; + #define ROUNDUP(_x, _w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w= ))-1)) =20 static bool verbose =3D false; @@ -310,8 +313,7 @@ fail: return -1; } =20 -static void initialize_fds(int sock, int *p_sock_pollfd_idx, - int ro_sock, int *p_ro_sock_pollfd_idx, +static void initialize_fds(int *p_sock_pollfd_idx, int *p_ro_sock_pollfd_i= dx, int *ptimeout) { struct connection *conn; @@ -1789,43 +1791,29 @@ void corrupt(struct connection *conn, const char *f= mt, ...) check_store(); } =20 - -#ifdef NO_SOCKETS -static void init_sockets(int **psock, int **pro_sock) -{ - static int minus_one =3D -1; - *psock =3D *pro_sock =3D &minus_one; -} -#else -static int destroy_fd(void *_fd) +#ifndef NO_SOCKETS +static void destroy_fds(void) { - int *fd =3D _fd; - close(*fd); - return 0; + if (sock >=3D 0) + close(sock); + if (ro_sock >=3D 0) + close(ro_sock); } =20 -static void init_sockets(int **psock, int **pro_sock) +static void init_sockets(void) { struct sockaddr_un addr; - int *sock, *ro_sock; const char *soc_str =3D xs_daemon_socket(); const char *soc_str_ro =3D xs_daemon_socket_ro(); =20 /* Create sockets for them to listen to. */ - *psock =3D sock =3D talloc(talloc_autofree_context(), int); - if (!sock) - barf_perror("No memory when creating sockets"); - *sock =3D socket(PF_UNIX, SOCK_STREAM, 0); - if (*sock < 0) + atexit(destroy_fds); + sock =3D socket(PF_UNIX, SOCK_STREAM, 0); + if (sock < 0) barf_perror("Could not create socket"); - *pro_sock =3D ro_sock =3D talloc(talloc_autofree_context(), int); - if (!ro_sock) - barf_perror("No memory when creating sockets"); - *ro_sock =3D socket(PF_UNIX, SOCK_STREAM, 0); - if (*ro_sock < 0) + ro_sock =3D socket(PF_UNIX, SOCK_STREAM, 0); + if (ro_sock < 0) barf_perror("Could not create socket"); - talloc_set_destructor(sock, destroy_fd); - talloc_set_destructor(ro_sock, destroy_fd); =20 /* FIXME: Be more sophisticated, don't mug running daemon. */ unlink(soc_str); @@ -1836,24 +1824,21 @@ static void init_sockets(int **psock, int **pro_soc= k) if(strlen(soc_str) >=3D sizeof(addr.sun_path)) barf_perror("socket string '%s' too long", soc_str); strcpy(addr.sun_path, soc_str); - if (bind(*sock, (struct sockaddr *)&addr, sizeof(addr)) !=3D 0) + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) !=3D 0) barf_perror("Could not bind socket to %s", soc_str); =20 if(strlen(soc_str_ro) >=3D sizeof(addr.sun_path)) barf_perror("socket string '%s' too long", soc_str_ro); strcpy(addr.sun_path, soc_str_ro); - if (bind(*ro_sock, (struct sockaddr *)&addr, sizeof(addr)) !=3D 0) + if (bind(ro_sock, (struct sockaddr *)&addr, sizeof(addr)) !=3D 0) barf_perror("Could not bind socket to %s", soc_str_ro); =20 if (chmod(soc_str, 0600) !=3D 0 || chmod(soc_str_ro, 0660) !=3D 0) barf_perror("Could not chmod sockets"); =20 - if (listen(*sock, 1) !=3D 0 - || listen(*ro_sock, 1) !=3D 0) + if (listen(sock, 1) !=3D 0 || listen(ro_sock, 1) !=3D 0) barf_perror("Could not listen on sockets"); - - } #endif =20 @@ -1909,7 +1894,7 @@ int priv_domid =3D 0; =20 int main(int argc, char *argv[]) { - int opt, *sock =3D NULL, *ro_sock =3D NULL; + int opt; int sock_pollfd_idx =3D -1, ro_sock_pollfd_idx =3D -1; bool dofork =3D true; bool outputpid =3D false; @@ -1997,7 +1982,9 @@ int main(int argc, char *argv[]) =20 talloc_enable_null_tracking(); =20 - init_sockets(&sock, &ro_sock); +#ifndef NO_SOCKETS + init_sockets(); +#endif =20 init_pipe(reopen_log_pipe); =20 @@ -2025,8 +2012,7 @@ int main(int argc, char *argv[]) tracefile =3D talloc_strdup(NULL, tracefile); =20 /* Get ready to listen to the tools. */ - initialize_fds(*sock, &sock_pollfd_idx, *ro_sock, &ro_sock_pollfd_idx, - &timeout); + initialize_fds(&sock_pollfd_idx, &ro_sock_pollfd_idx, &timeout); =20 /* Tell the kernel we're up and running. */ xenbus_notify_running(); @@ -2067,7 +2053,7 @@ int main(int argc, char *argv[]) barf_perror("sock poll failed"); break; } else if (fds[sock_pollfd_idx].revents & POLLIN) { - accept_connection(*sock, true); + accept_connection(sock, true); sock_pollfd_idx =3D -1; } } @@ -2077,7 +2063,7 @@ int main(int argc, char *argv[]) barf_perror("ro sock poll failed"); break; } else if (fds[ro_sock_pollfd_idx].revents & POLLIN) { - accept_connection(*ro_sock, false); + accept_connection(ro_sock, false); ro_sock_pollfd_idx =3D -1; } } @@ -2144,8 +2130,7 @@ int main(int argc, char *argv[]) } } =20 - initialize_fds(*sock, &sock_pollfd_idx, *ro_sock, - &ro_sock_pollfd_idx, &timeout); + initialize_fds(&sock_pollfd_idx, &ro_sock_pollfd_idx, &timeout); } } =20 --=20 2.16.4