From nobody Thu Jul 10 17:54:29 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4bdMtG1mDdz61LGc; Thu, 10 Jul 2025 17:54:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bdMtG133qz3JnY; Thu, 10 Jul 2025 17:54:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752170070; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mmcgYoSJKVuxEKhRCsvY6JMTJwYd2DgRjqKn+ED0ewc=; b=mCKDH52yPNlML28Yztt1agfgZAYr2UimMCvoHbg7Phsk0OYJ3gPTtSY8FcMUAC8ix/JL1s SFR+fJWKL9Pb1EDP3vOwdIbPcygZav1eq/Sw3pMG5qHkgYLpfLyjVS0/9yCqwB/Gg29cF2 KckvuKGBvWgPFPzZ3pB6KFpmKDd9oN56XBu6Z1eNJrYMF+IABPxK9pyzSrNxi5cRU9Ofrt P5c48/RwK4/YVcdldg1BS8A2PpG8xAguhijxrrWfCqZb2netPo1qeI77OTorzgbQNqTKAR WrJzcRBGuHP9vmTOu71Qd7zAr0QIEOiZUknim6IVPSc5M4gYNNRKg7vqoPW99A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752170070; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mmcgYoSJKVuxEKhRCsvY6JMTJwYd2DgRjqKn+ED0ewc=; b=iwgwklag3IuEvUW0zwL0tf8PRMKRxcKpPIkej4mh6WvWM6+dJIu5C/sxVarqNNemdphvVo CRIo/Ke0xBuYgoRSfVGaAQSweYnkTXBnYoFlt3H1T44gNSXt5GpmC1qWNfPlHtNrMg2M2u 6UjgIXoZl3hXRYDavixptXUtPPp/k1aV+skwd+czRTrTvSbuCZfrg990nrqX+XiqBsYpai 1r0t4w5BkamlQC3cHm/9BBNBiDDHaxNjwzytYH9p3fI4iCBrT20qCUmscxnfebnx49kbiH D5R+VkKVYn/Z+5dXbHbb6DdgJD37xWHf67o4G0TrbtGNs7AVA055fCHKtOcThA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1752170070; a=rsa-sha256; cv=none; b=YgGBi2gKz4WIU0jHzqv9ozNouSDGA1Ffl4svJe2iiThOg+zOlNXG53axYGTDKqLXIQBaf1 ntwgXS5BBViLjHfxRDTVGqa1y5FSe0DTEDobRNzJfiClShA0GzNVBgaYlYYDL/sxlH8H1f Xvr9/4OdJTPGQuXRhdIhXSI8Q2OHT0dohtMZbaxsEop1Uvhi+yHHWfG+imVok3zyy50vi2 yO1LdE/wFnXo5U7PmADdlvsRMn7OPm89vgjXIydJ56xpFyEoNDF1nbGx5ehVScwyl6Ssbv Kcw1//Bz7IqLOZ6UyoFuU01MvetO/7Y0fAh4xtn1+U2EAxZ/HrDt0dwSQ1G7zQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bdMtG06VPz1GKj; Thu, 10 Jul 2025 17:54:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 56AHsTq8072041; Thu, 10 Jul 2025 17:54:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 56AHsTqf072038; Thu, 10 Jul 2025 17:54:29 GMT (envelope-from git) Date: Thu, 10 Jul 2025 17:54:29 GMT Message-Id: <202507101754.56AHsTqf072038@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: df268d4b03a1 - main - lockf: add a -p mode to write the child's pid List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: df268d4b03a16869502d6842d40aeb66329db982 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=df268d4b03a16869502d6842d40aeb66329db982 commit df268d4b03a16869502d6842d40aeb66329db982 Author: Kyle Evans AuthorDate: 2025-06-24 15:41:53 +0000 Commit: Kyle Evans CommitDate: 2025-07-10 17:54:19 +0000 lockf: add a -p mode to write the child's pid If we're going to hold the lock, it can be useful to scribble down the pid that we spawned off to quickly associate the lock back to the process that's keeping it open. Reviewed by: allanjude (previous version), des Differential Revision: https://reviews.freebsd.org/D51014 --- usr.bin/lockf/lockf.1 | 14 ++++++++++++-- usr.bin/lockf/lockf.c | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/usr.bin/lockf/lockf.1 b/usr.bin/lockf/lockf.1 index d73033101632..5832903246f1 100644 --- a/usr.bin/lockf/lockf.1 +++ b/usr.bin/lockf/lockf.1 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd November 25, 2023 +.Dd June 24, 2025 .Dt LOCKF 1 .Os .Sh NAME @@ -30,7 +30,7 @@ .Nd execute a command while holding a file lock .Sh SYNOPSIS .Nm -.Op Fl knsw +.Op Fl knpsw .Op Fl t Ar seconds .Ar file .Ar command @@ -126,6 +126,16 @@ is not specified, will create .Ar file if necessary. +.It Fl p +Write the pid of the +.Ar command +to +.Ar file . +This option will cause +.Nm +to open +.Ar file +for writing rather than reading. .It Fl t Ar seconds Specifies a timeout for waiting for the lock. By default, diff --git a/usr.bin/lockf/lockf.c b/usr.bin/lockf/lockf.c index 7f88753d1743..93164e30762c 100644 --- a/usr.bin/lockf/lockf.c +++ b/usr.bin/lockf/lockf.c @@ -91,15 +91,15 @@ fdlock_implied(const char *name, long *ofd) int main(int argc, char **argv) { - int ch, flags, silent, status; + int ch, flags, silent, status, writepid; long long waitsec; pid_t child; union lock_subject subj; - silent = keep = 0; + silent = keep = writepid = 0; flags = O_CREAT | O_RDONLY; waitsec = -1; /* Infinite. */ - while ((ch = getopt(argc, argv, "knst:w")) != -1) { + while ((ch = getopt(argc, argv, "knpst:w")) != -1) { switch (ch) { case 'k': keep = 1; @@ -120,6 +120,10 @@ main(int argc, char **argv) "invalid timeout \"%s\"", optarg); } break; + case 'p': + writepid = 1; + flags |= O_TRUNC; + /* FALLTHROUGH */ case 'w': flags = (flags & ~O_RDONLY) | O_WRONLY; break; @@ -249,6 +253,11 @@ main(int argc, char **argv) fclose(stdin); fclose(stdout); fclose(stderr); + + /* Write out the pid before we sleep on it. */ + if (writepid) + (void)dprintf(lockfd, "%d\n", child); + if (waitpid(child, &status, 0) == -1) exit(EX_OSERR); return (WIFEXITED(status) ? WEXITSTATUS(status) : EX_SOFTWARE);