mirror of
https://git.FreeBSD.org/src.git
synced 2025-01-05 12:56:08 +00:00
Add option to lockf to avoid creating a file if it does not exist.
PR: bin/170775 Submitted by: Matthew Story <matthewstory@gmail.com> Reviewed by: scottl MFC after: 1 week
This commit is contained in:
parent
7a2b450ff8
commit
b5be420a7c
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=250462
@ -32,7 +32,7 @@
|
|||||||
.Nd execute a command while holding a file lock
|
.Nd execute a command while holding a file lock
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl ks
|
.Op Fl kns
|
||||||
.Op Fl t Ar seconds
|
.Op Fl t Ar seconds
|
||||||
.Ar file
|
.Ar file
|
||||||
.Ar command
|
.Ar command
|
||||||
@ -90,6 +90,18 @@ Causes
|
|||||||
.Nm
|
.Nm
|
||||||
to operate silently.
|
to operate silently.
|
||||||
Failure to acquire the lock is indicated only in the exit status.
|
Failure to acquire the lock is indicated only in the exit status.
|
||||||
|
.It Fl n
|
||||||
|
Causes
|
||||||
|
.Nm
|
||||||
|
to fail if the specified lock
|
||||||
|
.Ar file
|
||||||
|
does not exist. If
|
||||||
|
.Fl n
|
||||||
|
is not specified,
|
||||||
|
.Nm
|
||||||
|
will create
|
||||||
|
.Ar file
|
||||||
|
if necessary.
|
||||||
.It Fl t Ar seconds
|
.It Fl t Ar seconds
|
||||||
Specifies a timeout for waiting for the lock.
|
Specifies a timeout for waiting for the lock.
|
||||||
By default,
|
By default,
|
||||||
@ -130,6 +142,10 @@ The
|
|||||||
utility
|
utility
|
||||||
was unable to create the lock file, e.g., because of insufficient access
|
was unable to create the lock file, e.g., because of insufficient access
|
||||||
privileges.
|
privileges.
|
||||||
|
.It Dv EX_UNAVAILABLE
|
||||||
|
The
|
||||||
|
.Fl n
|
||||||
|
option is specified and the specified lock file does not exist.
|
||||||
.It Dv EX_USAGE
|
.It Dv EX_USAGE
|
||||||
There was an error on the
|
There was an error on the
|
||||||
.Nm
|
.Nm
|
||||||
|
@ -56,16 +56,20 @@ static volatile sig_atomic_t timed_out;
|
|||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int ch, silent, status, waitsec;
|
int ch, flags, silent, status, waitsec;
|
||||||
pid_t child;
|
pid_t child;
|
||||||
|
|
||||||
silent = keep = 0;
|
silent = keep = 0;
|
||||||
|
flags = O_CREAT;
|
||||||
waitsec = -1; /* Infinite. */
|
waitsec = -1; /* Infinite. */
|
||||||
while ((ch = getopt(argc, argv, "skt:")) != -1) {
|
while ((ch = getopt(argc, argv, "sknt:")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'k':
|
case 'k':
|
||||||
keep = 1;
|
keep = 1;
|
||||||
break;
|
break;
|
||||||
|
case 'n':
|
||||||
|
flags &= ~O_CREAT;
|
||||||
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
silent = 1;
|
silent = 1;
|
||||||
break;
|
break;
|
||||||
@ -118,13 +122,13 @@ main(int argc, char **argv)
|
|||||||
* avoiding the separate step of waiting for the lock. This
|
* avoiding the separate step of waiting for the lock. This
|
||||||
* yields fairness and improved performance.
|
* yields fairness and improved performance.
|
||||||
*/
|
*/
|
||||||
lockfd = acquire_lock(lockname, O_NONBLOCK);
|
lockfd = acquire_lock(lockname, flags | O_NONBLOCK);
|
||||||
while (lockfd == -1 && !timed_out && waitsec != 0) {
|
while (lockfd == -1 && !timed_out && waitsec != 0) {
|
||||||
if (keep)
|
if (keep)
|
||||||
lockfd = acquire_lock(lockname, 0);
|
lockfd = acquire_lock(lockname, flags);
|
||||||
else {
|
else {
|
||||||
wait_for_lock(lockname);
|
wait_for_lock(lockname);
|
||||||
lockfd = acquire_lock(lockname, O_NONBLOCK);
|
lockfd = acquire_lock(lockname, flags | O_NONBLOCK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (waitsec > 0)
|
if (waitsec > 0)
|
||||||
@ -165,7 +169,7 @@ acquire_lock(const char *name, int flags)
|
|||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if ((fd = open(name, O_RDONLY|O_CREAT|O_EXLOCK|flags, 0666)) == -1) {
|
if ((fd = open(name, flags|O_RDONLY|O_EXLOCK|flags, 0666)) == -1) {
|
||||||
if (errno == EAGAIN || errno == EINTR)
|
if (errno == EAGAIN || errno == EINTR)
|
||||||
return (-1);
|
return (-1);
|
||||||
err(EX_CANTCREAT, "cannot open %s", name);
|
err(EX_CANTCREAT, "cannot open %s", name);
|
||||||
@ -215,7 +219,7 @@ usage(void)
|
|||||||
{
|
{
|
||||||
|
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"usage: lockf [-ks] [-t seconds] file command [arguments]\n");
|
"usage: lockf [-kns] [-t seconds] file command [arguments]\n");
|
||||||
exit(EX_USAGE);
|
exit(EX_USAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user