From patchwork Fri Jun 7 17:01:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 10982425 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CFB1614E5 for ; Fri, 7 Jun 2019 17:01:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BDBFF28B5F for ; Fri, 7 Jun 2019 17:01:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B1B2E28B65; Fri, 7 Jun 2019 17:01:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C98328B68 for ; Fri, 7 Jun 2019 17:01:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731098AbfFGRBk (ORCPT ); Fri, 7 Jun 2019 13:01:40 -0400 Received: from foss.arm.com ([217.140.110.172]:44506 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728974AbfFGRBk (ORCPT ); Fri, 7 Jun 2019 13:01:40 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DCA33499; Fri, 7 Jun 2019 10:01:39 -0700 (PDT) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.197.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 51ED43F71A; Fri, 7 Jun 2019 10:01:39 -0700 (PDT) From: Andre Przywara To: Will Deacon Cc: kvm@vger.kernel.org Subject: [PATCH kvmtool v2 1/2] list: Clean up ghost socket files Date: Fri, 7 Jun 2019 18:01:20 +0100 Message-Id: <20190607170121.16557-2-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190607170121.16557-1-andre.przywara@arm.com> References: <20190607170121.16557-1-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When kvmtool (or the host kernel) crashes or gets killed, we cannot automatically remove the socket file we created for that VM. A later call of "lkvm list" iterates over all those files and complains about those "ghost socket files", as there is no one listening on the other side. Also sometimes the automatic guest name generation happens to generate the same name again, so an unrelated "lkvm run" later complains and stops, which is bad for automation. As the only code doing a listen() on this socket is kvmtool upon VM *creation*, such an orphaned socket file will never come back to life, so we can as well unlink() those sockets in the code. This spares the user from doing it herself. We keep the message in the code to notify the user of this. Signed-off-by: Andre Przywara --- kvm-ipc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kvm-ipc.c b/kvm-ipc.c index e07ad105..29ea498a 100644 --- a/kvm-ipc.c +++ b/kvm-ipc.c @@ -101,9 +101,9 @@ int kvm__get_sock_by_instance(const char *name) r = connect(s, (struct sockaddr *)&local, len); if (r < 0 && errno == ECONNREFUSED) { - /* Tell the user clean ghost socket file */ - pr_err("\"%s\" could be a ghost socket file, please remove it", - sock_file); + /* Clean up the ghost socket file */ + unlink(local.sun_path); + pr_info("Removed ghost socket file \"%s\".", sock_file); return r; } else if (r < 0) { return r; From patchwork Fri Jun 7 17:01:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 10982427 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5590D14B6 for ; Fri, 7 Jun 2019 17:01:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4369228B5F for ; Fri, 7 Jun 2019 17:01:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3779828B67; Fri, 7 Jun 2019 17:01:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C509628B5F for ; Fri, 7 Jun 2019 17:01:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731108AbfFGRBl (ORCPT ); Fri, 7 Jun 2019 13:01:41 -0400 Received: from foss.arm.com ([217.140.110.172]:44512 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731099AbfFGRBl (ORCPT ); Fri, 7 Jun 2019 13:01:41 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A722C80D; Fri, 7 Jun 2019 10:01:40 -0700 (PDT) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.197.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1BC2A3F71A; Fri, 7 Jun 2019 10:01:40 -0700 (PDT) From: Andre Przywara To: Will Deacon Cc: kvm@vger.kernel.org Subject: [PATCH kvmtool v2 2/2] run: Check for ghost socket file upon VM creation Date: Fri, 7 Jun 2019 18:01:21 +0100 Message-Id: <20190607170121.16557-3-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190607170121.16557-1-andre.przywara@arm.com> References: <20190607170121.16557-1-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Kvmtool creates a (debug) UNIX socket file for each VM, using its (possibly auto-generated) name as the filename. There is a check using access(), which bails out with an error message if a socket with that name already exists. Aside from this check being unnecessary, as the bind() call later would complain as well, this is also racy. But more annoyingly the bail out is not needed most of the time: an existing socket inode is most likely just an orphaned leftover from a previous kvmtool run, which just failed to remove that file, because of a crash, for instance. Upon finding such a collision, let's first try to connect to that socket, to detect if there is still a kvmtool instance listening on the other end. If that fails, this socket will never come back to life, so we can safely clean it up and reuse the name for the new guest. However if the connect() succeeds, there is an actual live kvmtool instance using this name, so not proceeding is the only option. This should never happen with the (PID based) automatically generated names, though. This avoids an annoying (and not helpful) error message and helps automated kvmtool runs to proceed in more cases. Signed-off-by: Andre Przywara --- kvm-ipc.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/kvm-ipc.c b/kvm-ipc.c index 29ea498a..23f7b12e 100644 --- a/kvm-ipc.c +++ b/kvm-ipc.c @@ -43,10 +43,6 @@ static int kvm__create_socket(struct kvm *kvm) snprintf(full_name, sizeof(full_name), "%s/%s%s", kvm__get_dir(), kvm->cfg.guest_name, KVM_SOCK_SUFFIX); - if (access(full_name, F_OK) == 0) { - pr_err("Socket file %s already exist", full_name); - return -EEXIST; - } s = socket(AF_UNIX, SOCK_STREAM, 0); if (s < 0) { @@ -58,6 +54,33 @@ static int kvm__create_socket(struct kvm *kvm) strlcpy(local.sun_path, full_name, sizeof(local.sun_path)); len = strlen(local.sun_path) + sizeof(local.sun_family); r = bind(s, (struct sockaddr *)&local, len); + /* Check for an existing socket file */ + if (r < 0 && errno == EADDRINUSE) { + r = connect(s, (struct sockaddr *)&local, len); + if (r == 0) { + /* + * If we could connect, there is already a guest + * using this same name. This should not happen + * for PID derived names, but could happen for user + * provided guest names. + */ + pr_err("Guest socket file %s already exists.", + full_name); + r = -EEXIST; + goto fail; + } + if (errno == ECONNREFUSED) { + /* + * This is a ghost socket file, with no-one listening + * on the other end. Since kvmtool will only bind + * above when creating a new guest, there is no + * danger in just removing the file and re-trying. + */ + unlink(full_name); + pr_info("Removed ghost socket file \"%s\".", full_name); + r = bind(s, (struct sockaddr *)&local, len); + } + } if (r < 0) { perror("bind"); goto fail;