The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=df268d4b03a16869502d6842d40aeb66329db982
commit df268d4b03a16869502d6842d40aeb66329db982 Author: Kyle Evans <kev...@freebsd.org> AuthorDate: 2025-06-24 15:41:53 +0000 Commit: Kyle Evans <kev...@freebsd.org> 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);