aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2025-06-24 15:41:53 +0000
committerKyle Evans <kevans@FreeBSD.org>2025-07-10 17:54:19 +0000
commitdf268d4b03a16869502d6842d40aeb66329db982 (patch)
tree18e831254f4dd48f740b32fc7eedfa9dd0a620c8
parentd78d04b17cb2498186e8fd2681f224a760e75b28 (diff)
-rw-r--r--usr.bin/lockf/lockf.114
-rw-r--r--usr.bin/lockf/lockf.c15
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);