FreeBSD Bugzilla – Attachment 232656 Details for
Bug 262738
usr.bin/lockf: Add support for locking file descriptors
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Add support for locking FDs to lockf
lockf-fds.diff (text/plain), 4.51 KB, created by
Daniel O'Connor
on 2022-03-23 12:20:57 UTC
(
hide
)
Description:
Add support for locking FDs to lockf
Filename:
MIME Type:
Creator:
Daniel O'Connor
Created:
2022-03-23 12:20:57 UTC
Size:
4.51 KB
patch
obsolete
>diff -ur lockf.orig/Makefile lockf/Makefile >--- lockf.orig/Makefile 2022-03-23 21:44:16.000000000 +1030 >+++ lockf/Makefile 2022-03-23 22:02:30.000000000 +1030 >@@ -1,5 +1,5 @@ > # $FreeBSD: stable/12/usr.bin/lockf/Makefile 90415 2002-02-08 22:31:43Z markm $ > > PROG= lockf >- >+CFLAGS+=-g > .include <bsd.prog.mk> >Only in lockf: lockf >diff -ur lockf.orig/lockf.1 lockf/lockf.1 >--- lockf.orig/lockf.1 2022-03-23 21:44:16.000000000 +1030 >+++ lockf/lockf.1 2022-03-23 22:46:46.000000000 +1030 >@@ -29,19 +29,23 @@ > .Os > .Sh NAME > .Nm lockf >-.Nd execute a command while holding a file lock >+.Nd execute a command while holding a file/fd lock > .Sh SYNOPSIS > .Nm > .Op Fl kns > .Op Fl t Ar seconds >-.Ar file >+.Ar file|fd > .Ar command > .Op Ar arguments >+.Nm >+.Op Fl kns >+.Op Fl t Ar seconds >+.Ar fd > .Sh DESCRIPTION > The > .Nm > utility acquires an exclusive lock on a >-.Ar file , >+.Ar file|fd , > creating it if necessary, > .Bf Em > and removing the file on exit unless explicitly told not to. >@@ -64,7 +68,14 @@ > .Xr flock 2 ; > the mere existence of the > .Ar file >-is not considered to constitute a lock. >+is not considered to constitute a lock. If >+.Ar file|fd >+is an integer greater than 0 then it will be interpreted as >+file descriptor number. This implicitly sets the >+.Fl k >+option. If a file descriptor is used then >+.Ar command >+is optional. This can be used to lock inside a shell script. > .Pp > If the > .Nm >@@ -160,6 +171,31 @@ > did not exit normally, > but may have been signaled or stopped. > .El >+.Sh EXAMPLES >+Lock a file and run a script, return immediately if the lock is not >+available. Do not delete the file afterward so lock order is >+guaranteed. >+.Pp >+.Dl $ lockf -t 0 -k /tmp/my.lock myscript >+.Pp >+Protect a section of a shell script with a lock, wait up to 5 seconds >+for it to become available. In this case >+.Fl k >+is implied. >+.Pp >+.Bd -literal -offset indent >+( >+ lockf -s -t 10 9 >+ if [ $? -ne 0 ]; then >+ echo "Failed to obtain lock" >+ exit 1 >+ fi >+ >+ echo Start >+ # Do some stuff >+ echo End >+) 9>/tmp/my.lock >+.Ed > .Sh SEE ALSO > .Xr flock 2 , > .Xr lockf 3 , >Only in lockf: lockf.1.gz >diff -ur lockf.orig/lockf.c lockf/lockf.c >--- lockf.orig/lockf.c 2022-03-23 21:44:16.000000000 +1030 >+++ lockf/lockf.c 2022-03-23 22:16:05.000000000 +1030 >@@ -50,6 +50,7 @@ > static const char *lockname; > static int lockfd = -1; > static int keep; >+static int fdlock; > static volatile sig_atomic_t timed_out; > > /* >@@ -88,9 +89,17 @@ > usage(); > } > } >- if (argc - optind < 2) >- usage(); >+ > lockname = argv[optind++]; >+ >+ if (atoi(lockname) > 2) >+ fdlock = 1; >+ else >+ fdlock = 0; >+ >+ if (argc - optind < 1 - fdlock) >+ usage(); >+ > argc -= optind; > argv += optind; > if (waitsec > 0) { /* Set up a timeout. */ >@@ -126,7 +135,7 @@ > */ > lockfd = acquire_lock(lockname, flags | O_NONBLOCK); > while (lockfd == -1 && !timed_out && waitsec != 0) { >- if (keep) >+ if (keep || fdlock) > lockfd = acquire_lock(lockname, flags); > else { > wait_for_lock(lockname); >@@ -140,7 +149,13 @@ > exit(EX_TEMPFAIL); > errx(EX_TEMPFAIL, "%s: already locked", lockname); > } >+ > /* At this point, we own the lock. */ >+ >+ /* Nothing else to do for FD lock, just exit */ >+ if (fdlock) >+ return 0; >+ > if (atexit(cleanup) == -1) > err(EX_OSERR, "atexit failed"); > if ((child = fork()) == -1) >@@ -161,7 +176,7 @@ > } > > /* >- * Try to acquire a lock on the given file, creating the file if >+ * Try to acquire a lock on the given file/fd, creating the file if > * necessary. The flags argument is O_NONBLOCK or 0, depending on > * whether we should wait for the lock. Returns an open file descriptor > * on success, or -1 on failure. >@@ -171,12 +186,20 @@ > { > int fd; > >- if ((fd = open(name, O_RDONLY|O_EXLOCK|flags, 0666)) == -1) { >- if (errno == EAGAIN || errno == EINTR) >- return (-1); >- else if (errno == ENOENT && (flags & O_CREAT) == 0) >- err(EX_UNAVAILABLE, "%s", name); >- err(EX_CANTCREAT, "cannot open %s", name); >+ if ((fd = atoi(name)) > 0) { >+ if (flock(fd, LOCK_EX | LOCK_NB) == -1) { >+ if (errno == EAGAIN || errno == EINTR) >+ return (-1); >+ err(EX_CANTCREAT, "cannot lock FD %d", fd); >+ } >+ } else { >+ if ((fd = open(name, O_RDONLY|O_EXLOCK|flags, 0666)) == -1) { >+ if (errno == EAGAIN || errno == EINTR) >+ return (-1); >+ else if (errno == ENOENT && (flags & O_CREAT) == 0) >+ err(EX_UNAVAILABLE, "%s", name); >+ err(EX_CANTCREAT, "cannot open %s", name); >+ } > } > return (fd); > } >@@ -188,7 +211,7 @@ > cleanup(void) > { > >- if (keep) >+ if (keep || lockfd) > flock(lockfd, LOCK_UN); > else > unlink(lockname); >Only in lockf: lockf.o >Only in lockf: test >Only in lockf: test.sh
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 262738
: 232656