flock: Allow lock directory

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



With this patch, you can lock directory.
Additionally, lockfile opens with O_NOCTTY.

Signed-off-by: Alexey Gladkov <legion@xxxxxxxxxxxx>
---
 sys-utils/flock.1 |    8 +++++---
 sys-utils/flock.c |    7 ++++++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/sys-utils/flock.1 b/sys-utils/flock.1
index 6965ba7..6d60ff7 100644
--- a/sys-utils/flock.1
+++ b/sys-utils/flock.1
@@ -30,6 +30,8 @@ flock \- Manage locks from shell scripts
 .SH SYNOPSIS
 \fBflock\fP [\fB\-sxon\fP] [\fB\-w\fP \fItimeout\fP] \fIlockfile\fP [\fB\-c\fP] \fIcommand...\fP
 .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
 .SH DESCRIPTION
 .PP
@@ -37,14 +39,14 @@ This utility manages
 .BR flock (2)
 locks from within shell scripts or the command line.
 .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)
 or
 .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.
 .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:
 .PP
 \fC(
diff --git a/sys-utils/flock.c b/sys-utils/flock.c
index 6c1acc3..97abc36 100644
--- a/sys-utils/flock.c
+++ b/sys-utils/flock.c
@@ -34,6 +34,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <paths.h>
+#include <fcntl.h>
 #include <sysexits.h>
 #include <sys/types.h>
 #include <sys/file.h>
@@ -62,6 +63,7 @@ static void usage(int ex)
 	  "flock (%s)\n"
 	  "Usage: %s [-sxun][-w #] fd#\n"
 	  "       %s [-sxon][-w #] file [-c] command...\n"
+	  "       %s [-sxon][-w #] directory [-c] command...\n"
 	  "  -s  --shared     Get a shared lock\n"
 	  "  -x  --exclusive  Get an exclusive lock\n"
 	  "  -u  --unlock     Remove a lock\n"
@@ -201,7 +203,10 @@ int main(int argc, char *argv[])
     }
 
     filename = argv[optind];
-    fd = open(filename, O_RDONLY|O_CREAT, 0666);
+#ifdef O_DIRECTORY
+    if ((fd = open(filename, O_RDONLY|O_DIRECTORY)) < 0)
+#endif
+    fd = open(filename, O_RDONLY|O_NOCTTY|O_CREAT, 0666);
 
     if ( fd < 0 ) {
       err = errno;
-- 
Rgrds, legion
--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux