Permalink
Browse files

fixes #113 desire flock()

  • Loading branch information...
gdamore committed Nov 12, 2014
1 parent b0352c0 commit c79874d0f161eb999ce69cc8921a0b9a4294a710
@@ -859,6 +859,7 @@ PORTSYS= \
execv.o \
fcntl.o \
fexecve.o \
+ flock.o \
getpagesizes.o \
getpeerucred.o \
inst_sync.o \
@@ -897,6 +897,7 @@ PORTSYS= \
execv.o \
fcntl.o \
fexecve.o \
+ flock.o \
getpagesizes.o \
getpeerucred.o \
inst_sync.o \
@@ -96,6 +96,7 @@ $endif
SYMBOL_VERSION ILLUMOS_0.10 {
protected:
fexecve;
+ flock;
} ILLUMOS_0.9;
SYMBOL_VERSION ILLUMOS_0.9 {
@@ -0,0 +1,70 @@
+/*
+ * This file and its contents are supplied under the terms of the
+ * Common Development and Distribution License ("CDDL"), version 1.0.
+ * You may only use this file in accordance with the terms of version
+ * 1.0 of the CDDL.
+ *
+ * A full copy of the text of the CDDL should have accompanied this
+ * source. A copy of the CDDL is also available via the Internet at
+ * http://www.illumos.org/license/CDDL.
+ */
+
+/*
+ * Copyright 2014 Garrett D'Amore
+ */
+
+/*
+ * This implements the BSD style flock() function. This is
+ * implemented in terms of fcntl(), and the lock is compatible
+ * with both fcntl() and lockf() based locks.
+ */
+
+#include "lint.h"
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/file.h>
+
+int
+flock(int fd, int operation)
+{
+ struct flock64 fl;
+ int cmd;
+
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 0;
+
+ if (operation & LOCK_NB) {
+ cmd = F_SETLK64;
+ } else {
+ cmd = F_SETLKW64;
+ }
+ switch (operation) {
+ case LOCK_SH|LOCK_NB:
+ case LOCK_SH:
+ fl.l_type = F_RDLCK;
+ break;
+
+ case LOCK_EX|LOCK_NB:
+ case LOCK_EX:
+ fl.l_type = F_WRLCK;
+ break;
+
+ case LOCK_UN|LOCK_NB:
+ case LOCK_UN:
+ fl.l_type = F_UNLCK;
+ break;
+ default:
+ errno = EINVAL;
+ return (-1);
+ }
+
+ /*
+ * NB: It is posssible to return more errnos than just
+ * EWOULDBLOCK, EBADF, EINVAL. For example, ENOLCK.
+ */
+
+ return (fcntl(fd, cmd, &fl));
+}
@@ -931,6 +931,7 @@ PORTSYS= \
execv.o \
fcntl.o \
fexecve.o \
+ flock.o \
getpagesizes.o \
getpeerucred.o \
inst_sync.o \
@@ -877,6 +877,7 @@ PORTSYS= \
execv.o \
fcntl.o \
fexecve.o \
+ flock.o \
getpagesizes.o \
getpeerucred.o \
inst_sync.o \
@@ -123,6 +123,7 @@ MANFILES= __fbufsize.3c \
fgetpos.3c \
fgetwc.3c \
floating_to_decimal.3c \
+ flock.3c \
flockfile.3c \
fmtmsg.3c \
fnmatch.3c \
View
@@ -0,0 +1,86 @@
+.\"
+.\" This file and its contents are supplied under the terms of the
+.\" Common Development and Distribution License ("CDDL"), version 1.0.
+.\" You may only use this file in accordance with the terms of version
+.\" 1.0 of the CDDL.
+.\"
+.\" A full copy of the text of the CDDL should have accompanied this
+.\" source. A copy of the CDDL is also available via the Internet at
+.\" http://www.illumos.org/license/CDDL.
+.\"
+.\"
+.\" Copyright 2014 Garrett D'Amore <garrett@damore.org>
+.\"
+.Dd "Nov 13, 2014"
+.Dt FLOCK 3C
+.Os
+.Sh NAME
+.Nm flock
+.Nd BSD-compatible file locking
+.Sh SYNOPSIS
+.In sys/file.h
+.
+.Ft int
+.Fo flock
+.Fa "int fd"
+.Fa "int operation"
+.Fc
+.
+.Sh DESCRIPTION
+The
+.Fn flock
+function provides a BSD compatibility interface for file locking,
+and is implemented in terms of
+.Xr fcntl 2 .
+.
+.Pp
+The
+.Fa fd
+is a file descriptor that references a regular file. The
+.Fa operation
+is one of the following.
+.Bl -tag -width Dv
+.It Dv LOCK_SH
+Acquire a shared (non-exclusive) lock. There may be multiple shared
+locks on a file simultaneously.
+.It Dv LOCK_EX
+Acquire an exclusive lock.
+.It Dv LOCK_UN
+Release the lock.
+.El
+.Pp
+In addition, the value
+.Dv LOCK_NB
+can be added to the operation (bitwise-OR) to indicate that the
+operation should fail immediately rather than blocking if the
+lock cannot be acquired.
+.
+.Sh RETURN VALUES
+.Rv -std
+.Sh ERRORS
+The
+.Fn flock
+function will fail if:
+.Bl -tag -width Er
+.It Er EBADF
+The
+.Fa fd
+argument is not an open file descriptor.
+.
+.It Er EINVAL
+The
+.Fa fd
+argument does not reference a regular file, or an invalid
+.Fa operation
+has been supplied.
+.
+.It Er EWOULDBLOCK
+A lock cannot be immediately acquired due to other conflicting lock(s).
+.El
+.Sh INTERFACE STABILITY
+.Sy Obsolete Committed.
+.Sh MT-LEVEL
+.Sy MT-Safe .
+.Sh SEE ALSO
+.Xr fcntl 2 ,
+.Xr lockf 3C
@@ -119,6 +119,7 @@ file path=usr/share/man/man3c/fgetc.3c
file path=usr/share/man/man3c/fgetpos.3c
file path=usr/share/man/man3c/fgetwc.3c
file path=usr/share/man/man3c/floating_to_decimal.3c
+file path=usr/share/man/man3c/flock.3c
file path=usr/share/man/man3c/flockfile.3c
file path=usr/share/man/man3c/fmtmsg.3c
file path=usr/share/man/man3c/fnmatch.3c
@@ -120,6 +120,17 @@ typedef struct fpollinfo {
#define FCLOEXEC 0x800000 /* O_CLOEXEC = 0x800000 */
#define FDIRECTORY 0x1000000 /* O_DIRECTORY = 0x1000000 */
+#if !defined(_KERNEL) && !defined(_STRICT_SYMBOLS)
+/*
+ * BSD compatible flock implementation. Here for historical reasons.
+ */
+#define LOCK_SH 1 /* shared lock */
+#define LOCK_EX 2 /* exclusive lock */
+#define LOCK_NB 4 /* non-blocking */
+#define LOCK_UN 8 /* unlock */
+extern int flock(int, int);
+#endif /* !_KERNEL && !_STRICT_SYMBOLS */
+
#ifdef _KERNEL
/*

0 comments on commit c79874d

Please sign in to comment.