From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A3EC2670C7 for ; Wed, 29 Jul 2020 14:33:52 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9DA47E9BD for ; Wed, 29 Jul 2020 14:33:22 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 68D28E98E for ; Wed, 29 Jul 2020 14:33:21 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 320EE433E7 for ; Wed, 29 Jul 2020 14:33:21 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Wed, 29 Jul 2020 14:33:12 +0200 Message-Id: <20200729123314.10049-4-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200729123314.10049-1-s.reiter@proxmox.com> References: <20200729123314.10049-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.044 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [tools.rs] Subject: [pbs-devel] [PATCH proxmox-backup 3/5] tools: add nonblocking mode to lock_file X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Jul 2020 12:33:52 -0000 Signed-off-by: Stefan Reiter --- src/tools.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/tools.rs b/src/tools.rs index 44db796d..d7b72a73 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -91,6 +91,9 @@ pub fn map_struct_mut(buffer: &mut [u8]) -> Result<&mut T, Error> { /// Create a file lock using fntl. This function allows you to specify /// a timeout if you want to avoid infinite blocking. +/// +/// With timeout set to 0, non-blocking mode is used and the function +/// will fail immediately if the lock can't be acquired. pub fn lock_file( file: &mut F, exclusive: bool, @@ -110,6 +113,16 @@ pub fn lock_file( Some(t) => t, }; + if timeout.as_nanos() == 0 { + let lockarg = if exclusive { + nix::fcntl::FlockArg::LockExclusiveNonblock + } else { + nix::fcntl::FlockArg::LockSharedNonblock + }; + nix::fcntl::flock(file.as_raw_fd(), lockarg)?; + return Ok(()); + } + // unblock the timeout signal temporarily let _sigblock_guard = timer::unblock_timeout_signal(); -- 2.20.1