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 537E5B8470 for ; Mon, 4 Dec 2023 14:18:25 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 35BB2DA55 for ; Mon, 4 Dec 2023 14:18:25 +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 ; Mon, 4 Dec 2023 14:18:24 +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 2CF35445EF for ; Mon, 4 Dec 2023 14:18:24 +0100 (CET) From: Gabriel Goller To: pbs-devel@lists.proxmox.com Date: Mon, 4 Dec 2023 14:18:19 +0100 Message-Id: <20231204131820.204423-2-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231204131820.204423-1-g.goller@proxmox.com> References: <20231204131820.204423-1-g.goller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.165 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 - Subject: [pbs-devel] [PATCH 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: Mon, 04 Dec 2023 13:18:25 -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]) [0]: https://man7.org/linux/man-pages/man2/fcntl.2.html Signed-off-by: Gabriel Goller --- proxmox-sys/src/process_locker.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/proxmox-sys/src/process_locker.rs b/proxmox-sys/src/process_locker.rs index 4874da8..ac2a8a2 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); } @@ -132,7 +132,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