From patchwork Tue Apr 9 16:45:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony PERARD X-Patchwork-Id: 10891731 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 F376417E0 for ; Tue, 9 Apr 2019 16:47:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFE5C285A5 for ; Tue, 9 Apr 2019 16:47:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D46482888E; Tue, 9 Apr 2019 16:47: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=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 72E49285A5 for ; Tue, 9 Apr 2019 16:47:43 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hDtsl-0008Ex-1B; Tue, 09 Apr 2019 16:45:59 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hDtsj-0008Co-Ol for xen-devel@lists.xenproject.org; Tue, 09 Apr 2019 16:45:57 +0000 X-Inumbo-ID: f12840a6-5ae6-11e9-a08e-bf613d1783e6 Received: from SMTP03.CITRIX.COM (unknown [162.221.156.55]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f12840a6-5ae6-11e9-a08e-bf613d1783e6; Tue, 09 Apr 2019 16:45:54 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.60,330,1549929600"; d="scan'208";a="83137625" From: Anthony PERARD To: Date: Tue, 9 Apr 2019 17:45:36 +0100 Message-ID: <20190409164542.30274-4-anthony.perard@citrix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190409164542.30274-1-anthony.perard@citrix.com> References: <20190409164542.30274-1-anthony.perard@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 3/9] libxl_internal: Split out userdata lock function X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Anthony PERARD , Wei Liu , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP We are going to create a new lock and want to reuse the same machinery. Also, hide the detail of struct libxl__domain_userdata_lock as this is only useful as a pointer by the rest of libxl. No functional changes. Signed-off-by: Anthony PERARD --- tools/libxl/libxl_internal.c | 50 +++++++++++++++++++++++++++++------- tools/libxl/libxl_internal.h | 5 +--- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index f492dae5ff..fa0bbc3960 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -397,21 +397,26 @@ int libxl__device_model_version_running(libxl__gc *gc, uint32_t domid) return value; } +typedef struct { + libxl__carefd *carefd; + char *path; /* path of the lock file itself */ +} libxl__generic_lock; +static void libxl__unlock_generic(libxl__generic_lock * const lock); + /* Portability note: this lock utilises flock(2) so a proper implementation of * flock(2) is required. */ -libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc, - uint32_t domid) +static int libxl__lock_generic(libxl__gc *gc, libxl_domid domid, + libxl__generic_lock * const lock, + const char *lock_name) { - libxl__domain_userdata_lock *lock = NULL; const char *lockfile; int fd; struct stat stab, fstab; - lockfile = libxl__userdata_path(gc, domid, "domain-userdata-lock", "l"); + lockfile = libxl__userdata_path(gc, domid, lock_name, "l"); if (!lockfile) goto out; - lock = libxl__zalloc(NOGC, sizeof(libxl__domain_userdata_lock)); lock->path = libxl__strdup(NOGC, lockfile); while (true) { @@ -464,14 +469,14 @@ libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc, if (libxl_domain_info(CTX, NULL, domid)) goto out; - return lock; + return 0; out: - if (lock) libxl__unlock_domain_userdata(lock); - return NULL; + if (lock) libxl__unlock_generic(lock); + return ERROR_FAIL; } -void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock) +static void libxl__unlock_generic(libxl__generic_lock * const lock) { /* It's important to unlink the file before closing fd to avoid * the following race (if close before unlink): @@ -490,6 +495,33 @@ void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock) if (lock->path) unlink(lock->path); if (lock->carefd) libxl__carefd_close(lock->carefd); free(lock->path); +} + + +struct libxl__domain_userdata_lock { + libxl__generic_lock lock; +}; + +libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc, + uint32_t domid) +{ + libxl__domain_userdata_lock *lock = NULL; + int rc; + + lock = libxl__zalloc(NOGC, sizeof(*lock)); + rc = libxl__lock_generic(gc, domid, &lock->lock, + "domain-userdata-lock"); + if (rc) { + libxl__unlock_domain_userdata(lock); + return NULL; + } + + return lock; +} + +void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock) +{ + libxl__unlock_generic(&lock->lock); free(lock); } diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 702acc6d5d..f1aefaf98a 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -4486,10 +4486,7 @@ static inline int libxl__key_value_list_is_empty(libxl_key_value_list *pkvl) int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl); /* Portability note: a proper flock(2) implementation is required */ -typedef struct { - libxl__carefd *carefd; - char *path; /* path of the lock file itself */ -} libxl__domain_userdata_lock; +typedef struct libxl__domain_userdata_lock libxl__domain_userdata_lock; /* The CTX_LOCK must be held around uses of this lock */ libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc, uint32_t domid);