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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9BF126B3ED for ; Thu, 18 Feb 2021 15:40:32 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7EFB2F657 for ; Thu, 18 Feb 2021 15:40:32 +0100 (CET) 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 4AC47F62B for ; Thu, 18 Feb 2021 15:40:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D55A446272 for ; Thu, 18 Feb 2021 15:40:30 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 18 Feb 2021 15:40:20 +0100 Message-Id: <20210218144030.16778-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.217 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 Subject: [pbs-devel] [PATCH proxmox-backup 01/11] tape/drive: add test_device_path_lock 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: Thu, 18 Feb 2021 14:40:32 -0000 this makes it possible to detect if the drive was locked in a non-blocking manner Signed-off-by: Dominik Csapak --- src/tape/drive/mod.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/tape/drive/mod.rs b/src/tape/drive/mod.rs index e44e0e36..faa89953 100644 --- a/src/tape/drive/mod.rs +++ b/src/tape/drive/mod.rs @@ -496,3 +496,28 @@ fn lock_device_path(device_path: &str) -> Result { Ok(DeviceLockGuard(file)) } + +// Same logic as lock_device_path, but uses a timeout of 0, making it +// non-blocking, and returning if the file is locked or not +fn test_device_path_lock(device_path: &str) -> Result { + + let lock_name = crate::tools::systemd::escape_unit(device_path, true); + + let mut path = std::path::PathBuf::from("/var/lock"); + path.push(lock_name); + + let timeout = std::time::Duration::new(0, 0); + let mut file = std::fs::OpenOptions::new().create(true).append(true).open(path)?; + match proxmox::tools::fs::lock_file(&mut file, true, Some(timeout)) { + // file was not locked, continue + Ok(()) => {}, + // file was locked, return true + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => return Ok(true), + Err(err) => bail!("{}", err), + } + + let backup_user = crate::backup::backup_user()?; + fchown(file.as_raw_fd(), Some(backup_user.uid), Some(backup_user.gid))?; + + Ok(false) +} -- 2.20.1