flock: Allow lock directory
With this patch, you can lock directory. Additionally, lockfile opens with O_NOCTTY. Try to open file with O_CREAT flag first, and without it if open fails with EISDIR. Suggested by H. Peter Anvin. Signed-off-by: Alexey Gladkov <legion@altlinux.org>
This commit is contained in:
parent
8da18ddb81
commit
ff5717391c
|
@ -30,6 +30,8 @@ flock \- Manage locks from shell scripts
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
\fBflock\fP [\fB\-sxon\fP] [\fB\-w\fP \fItimeout\fP] \fIlockfile\fP [\fB\-c\fP] \fIcommand...\fP
|
\fBflock\fP [\fB\-sxon\fP] [\fB\-w\fP \fItimeout\fP] \fIlockfile\fP [\fB\-c\fP] \fIcommand...\fP
|
||||||
.PP
|
.PP
|
||||||
|
\fBflock\fP [\fB\-sxon\fP] [\fB\-w\fP \fItimeout\fP] \fIlockdir\fP [\fB\-c\fP] \fIcommand...\fP
|
||||||
|
.PP
|
||||||
\fBflock\fP [\fB\-sxun\fP] [\fB\-w\fP \fItimeout\fP] \fIfd\fP
|
\fBflock\fP [\fB\-sxun\fP] [\fB\-w\fP \fItimeout\fP] \fIfd\fP
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
.PP
|
.PP
|
||||||
|
@ -37,14 +39,14 @@ This utility manages
|
||||||
.BR flock (2)
|
.BR flock (2)
|
||||||
locks from within shell scripts or the command line.
|
locks from within shell scripts or the command line.
|
||||||
.PP
|
.PP
|
||||||
The first form wraps the lock around the executing a command, in a manner similar to
|
The first and second forms wraps the lock around the executing a command, in a manner similar to
|
||||||
.BR su (1)
|
.BR su (1)
|
||||||
or
|
or
|
||||||
.BR newgrp (1).
|
.BR newgrp (1).
|
||||||
It locks a specified file, which is created (assuming appropriate
|
It locks a specified file or directory, which is created (assuming appropriate
|
||||||
permissions), if it does not already exist.
|
permissions), if it does not already exist.
|
||||||
.PP
|
.PP
|
||||||
The second form is convenient inside shell scripts, and is usually
|
The third form is convenient inside shell scripts, and is usually
|
||||||
used the following manner:
|
used the following manner:
|
||||||
.PP
|
.PP
|
||||||
\fC(
|
\fC(
|
||||||
|
|
|
@ -62,6 +62,7 @@ static void usage(int ex)
|
||||||
"flock (%s)\n"
|
"flock (%s)\n"
|
||||||
"Usage: %s [-sxun][-w #] fd#\n"
|
"Usage: %s [-sxun][-w #] fd#\n"
|
||||||
" %s [-sxon][-w #] file [-c] command...\n"
|
" %s [-sxon][-w #] file [-c] command...\n"
|
||||||
|
" %s [-sxon][-w #] directory [-c] command...\n"
|
||||||
" -s --shared Get a shared lock\n"
|
" -s --shared Get a shared lock\n"
|
||||||
" -x --exclusive Get an exclusive lock\n"
|
" -x --exclusive Get an exclusive lock\n"
|
||||||
" -u --unlock Remove a lock\n"
|
" -u --unlock Remove a lock\n"
|
||||||
|
@ -201,7 +202,11 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
filename = argv[optind];
|
filename = argv[optind];
|
||||||
fd = open(filename, O_RDONLY|O_CREAT, 0666);
|
fd = open(filename, O_RDONLY|O_NOCTTY|O_CREAT, 0666);
|
||||||
|
/* Linux doesn't like O_CREAT on a directory, even though it should be a
|
||||||
|
no-op */
|
||||||
|
if (fd < 0 && errno == EISDIR)
|
||||||
|
fd = open(filename, O_RDONLY|O_NOCTTY);
|
||||||
|
|
||||||
if ( fd < 0 ) {
|
if ( fd < 0 ) {
|
||||||
err = errno;
|
err = errno;
|
||||||
|
|
Loading…
Reference in New Issue