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 B9069B8CA4 for ; Wed, 6 Dec 2023 14:29:10 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9BD9227A1 for ; Wed, 6 Dec 2023 14:28:40 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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 for ; Wed, 6 Dec 2023 14:28:39 +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 D577C42305 for ; Wed, 6 Dec 2023 14:28:38 +0100 (CET) From: Gabriel Goller To: pbs-devel@lists.proxmox.com Date: Wed, 6 Dec 2023 14:28:33 +0100 Message-Id: <20231206132834.240700-2-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231206132834.240700-1-g.goller@proxmox.com> References: <20231206132834.240700-1-g.goller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.159 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [man7.org] Subject: [pbs-devel] [PATCH v2 proxmox 1/2] process_locker: use ofd locking 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, 06 Dec 2023 13:29:10 -0000 Instead of using normal fcntl locking, use the OFD (open file descriptor) flags [0]. This blocks the file on the file-descriptor level, not on the process-level. This solves two problems: - If a process closes any file descriptor referring to a file, then all of the process's locks on that file are released, regardless of the file descriptor(s) on which the locks were obtained. This is bad: it means that a process can lose its locks on a file such as /etc/passwd or /etc/mtab when for some reason a library function decides to open, read, and close the same file. - The threads in a process share locks. In other words, a multithreaded program can't use record locking to ensure that threads don't simultaneously access the same region of a file. (Copied from [0]) Added a `create_dir_all()` call to create the folders if they are not existing. [0]: https://man7.org/linux/man-pages/man2/fcntl.2.html Signed-off-by: Gabriel Goller --- proxmox-sys/src/process_locker.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/proxmox-sys/src/process_locker.rs b/proxmox-sys/src/process_locker.rs index 4874da8..58deacb 100644 --- a/proxmox-sys/src/process_locker.rs +++ b/proxmox-sys/src/process_locker.rs @@ -1,7 +1,7 @@ //! Inter-process reader-writer lock builder. //! //! This implementation uses fcntl record locks with non-blocking -//! F_SETLK command (never blocks). +//! F_OFD_SETLK command (never blocks). //! //! We maintain a map of shared locks with time stamps, so you can get //! the timestamp for the oldest open lock with @@ -13,8 +13,6 @@ use std::sync::{Arc, Mutex}; use anyhow::{bail, Error}; -// fixme: use F_OFD_ locks when implemented with nix::fcntl - // Note: flock lock conversion is not atomic, so we need to use fcntl /// Inter-process reader-writer lock @@ -53,9 +51,10 @@ impl Drop for ProcessLockSharedGuard { l_pid: 0, }; - if let Err(err) = - nix::fcntl::fcntl(data.file.as_raw_fd(), nix::fcntl::FcntlArg::F_SETLKW(&op)) - { + if let Err(err) = nix::fcntl::fcntl( + data.file.as_raw_fd(), + nix::fcntl::FcntlArg::F_OFD_SETLKW(&op), + ) { panic!("unable to drop writer lock - {}", err); } } @@ -93,9 +92,10 @@ impl Drop for ProcessLockExclusiveGuard { l_pid: 0, }; - if let Err(err) = - nix::fcntl::fcntl(data.file.as_raw_fd(), nix::fcntl::FcntlArg::F_SETLKW(&op)) - { + if let Err(err) = nix::fcntl::fcntl( + data.file.as_raw_fd(), + nix::fcntl::FcntlArg::F_OFD_SETLKW(&op), + ) { panic!("unable to drop exclusive lock - {}", err); } @@ -108,6 +108,12 @@ impl ProcessLocker { /// /// This simply creates the file if it does not exist. pub fn new>(lockfile: P) -> Result>, Error> { + let parent = lockfile + .as_ref() + .parent() + .ok_or(anyhow::anyhow!("Failed getting parent dir of locking file"))?; + std::fs::create_dir_all(parent)?; + let file = std::fs::OpenOptions::new() .create(true) .read(true) @@ -132,7 +138,7 @@ impl ProcessLocker { l_pid: 0, }; - nix::fcntl::fcntl(file.as_raw_fd(), nix::fcntl::FcntlArg::F_SETLK(&op))?; + nix::fcntl::fcntl(file.as_raw_fd(), nix::fcntl::FcntlArg::F_OFD_SETLK(&op))?; Ok(()) } -- 2.39.2