From patchwork Tue Jun 6 21:26:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Hesse X-Patchwork-Id: 192 X-Patchwork-Delegate: andrew.gregory.8@gmail.com Return-Path: Delivered-To: patchwork@archlinux.org Received: from nymeria.archlinux.org by nymeria.archlinux.org (Dovecot) with LMTP id H4XLKBQeN1klbgAAtiB/HQ for ; Tue, 06 Jun 2017 23:26:44 +0200 Received: from nymeria.archlinux.org (localhost.localdomain [127.0.0.1]) by nymeria.archlinux.org (Postfix) with ESMTP id A050040241; Tue, 6 Jun 2017 23:26:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on nymeria.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.5 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.1 Received: from luna.archlinux.org (luna.archlinux.org [IPv6:2a01:4f8:160:3033::2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by nymeria.archlinux.org (Postfix) with ESMTPS; Tue, 6 Jun 2017 23:26:43 +0200 (CEST) Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 60AAE2C0CD; Tue, 6 Jun 2017 21:26:43 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=eworm.de header.i=@eworm.de header.b=kpQLVmYg Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 67D882C0A5 for ; Tue, 6 Jun 2017 21:26:41 +0000 (UTC) Received: from nymeria.archlinux.org (nymeria.archlinux.org [IPv6:2a00:1828:2000:547::2]) by luna.archlinux.org (Postfix) with ESMTPS for ; Tue, 6 Jun 2017 21:26:41 +0000 (UTC) Received: from nymeria.archlinux.org (localhost.localdomain [127.0.0.1]) by nymeria.archlinux.org (Postfix) with ESMTP id 111EE40241 for ; Tue, 6 Jun 2017 23:26:40 +0200 (CEST) Received: from mx.mylinuxtime.de (mx.mylinuxtime.de [148.251.109.235]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by nymeria.archlinux.org (Postfix) with ESMTPS for ; Tue, 6 Jun 2017 23:26:39 +0200 (CEST) Received: from leda.eworm.de (unknown [10.10.1.2]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx.mylinuxtime.de (Postfix) with ESMTPSA id 19B1124EB4; Tue, 6 Jun 2017 23:26:39 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.10.3 mx.mylinuxtime.de 19B1124EB4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eworm.de; s=mail; t=1496784399; bh=oR8YgIVQhl36FkGCZqh1wj2Vck6vGKgD5jMoTeA4N10=; h=From:To:Cc:Subject:Date; b=kpQLVmYg9s3gwofuiEfybRpbFeDS0w1M9CX8P1C88lndpZ8Pl9E7gB2yMEFzhWJ7D PtteBH3wZ7Uww1lsAG6GXK+0g76qK21vKnsIrhWrVfBOzBqPatXB0trSNXfAsGd5WP iOeojEdW79XT6BW/xm0CuY1xPjV+wJIhO0ugkOHo= Received: by leda.eworm.de (Postfix, from userid 1000) id 3437910147E; Tue, 6 Jun 2017 23:26:33 +0200 (CEST) From: Christian Hesse To: Discussion list for pacman development Date: Tue, 6 Jun 2017 23:26:28 +0200 Message-Id: <20170606212628.26466-1-list@eworm.de> X-Mailer: git-send-email 2.13.1 Subject: [pacman-dev] [PATCH 1/1] alpm: use flock() for db lock X-BeenThere: pacman-dev@archlinux.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Discussion list for pacman development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Discussion list for pacman development Cc: Christian Hesse Errors-To: pacman-dev-bounces@archlinux.org Sender: "pacman-dev" X-UID: 450 Status: X-Keywords: Content-Length: 2375 From: Christian Hesse We used to check for file existens, but that suffers from stale lock files caused by unexpected events like crash, shutdown, etc. Instead use flock() to lock the file. It does not matter whether or not the file exists but whether an exclusive lock can be obtained. Also remove the hint about removing the file from pacman. Signed-off-by: Christian Hesse --- lib/libalpm/handle.c | 17 +++++++++++++++-- src/pacman/util.c | 5 ----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index b6b27881..bb5cc907 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -120,10 +121,20 @@ int _alpm_handle_lock(alpm_handle_t *handle) FREE(dir); do { - handle->lockfd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0000); + handle->lockfd = open(handle->lockfile, O_WRONLY | O_CREAT | O_CLOEXEC, 0000); } while(handle->lockfd == -1 && errno == EINTR); - return (handle->lockfd >= 0 ? 0 : -1); + if (handle->lockfd == -1) + return -1; + + /* try to get an exclusive lock */ + if (flock(handle->lockfd, LOCK_EX | LOCK_NB) == -1) { + close(handle->lockfd); + handle->lockfd = -1; + return -1; + } + + return 0; } /** Remove the database lock file @@ -137,6 +148,8 @@ int SYMEXPORT alpm_unlock(alpm_handle_t *handle) ASSERT(handle->lockfile != NULL, return 0); ASSERT(handle->lockfd >= 0, return 0); + flock(handle->lockfd, LOCK_UN); + close(handle->lockfd); handle->lockfd = -1; diff --git a/src/pacman/util.c b/src/pacman/util.c index ae8a74d3..c3f9d3ba 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -81,13 +81,8 @@ void trans_init_error(void) pm_printf(ALPM_LOG_ERROR, _("failed to init transaction (%s)\n"), alpm_strerror(err)); if(err == ALPM_ERR_HANDLE_LOCK) { - const char *lockfile = alpm_option_get_lockfile(config->handle); pm_printf(ALPM_LOG_ERROR, _("could not lock database: %s\n"), strerror(errno)); - if(access(lockfile, F_OK) == 0) { - fprintf(stderr, _(" if you're sure a package manager is not already\n" - " running, you can remove %s\n"), lockfile); - } } }