changeset 14957:5f8ea225e580

fixes #113 desire flock()
author Garrett D'Amore <garrett@damore.org>
date Wed, 12 Nov 2014 08:30:46 -0800
parents fb8e37100d8d
children ad2c7680efd9
files usr/src/lib/libc/amd64/Makefile usr/src/lib/libc/i386/Makefile.com usr/src/lib/libc/port/mapfile-vers usr/src/lib/libc/port/sys/flock.c usr/src/lib/libc/sparc/Makefile.com usr/src/lib/libc/sparcv9/Makefile.com usr/src/man/man3c/Makefile usr/src/man/man3c/flock.3c usr/src/pkg/manifests/system-library.man3c.inc usr/src/uts/common/sys/file.h
diffstat 10 files changed, 174 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/lib/libc/amd64/Makefile	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/lib/libc/amd64/Makefile	Wed Nov 12 08:30:46 2014 -0800
@@ -859,6 +859,7 @@
 	execv.o			\
 	fcntl.o			\
 	fexecve.o		\
+	flock.o			\
 	getpagesizes.o		\
 	getpeerucred.o		\
 	inst_sync.o		\
--- a/usr/src/lib/libc/i386/Makefile.com	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/lib/libc/i386/Makefile.com	Wed Nov 12 08:30:46 2014 -0800
@@ -897,6 +897,7 @@
 	execv.o			\
 	fcntl.o			\
 	fexecve.o		\
+	flock.o			\
 	getpagesizes.o		\
 	getpeerucred.o		\
 	inst_sync.o		\
--- a/usr/src/lib/libc/port/mapfile-vers	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/lib/libc/port/mapfile-vers	Wed Nov 12 08:30:46 2014 -0800
@@ -96,6 +96,7 @@
 SYMBOL_VERSION ILLUMOS_0.10 {
     protected:
 	fexecve;
+	flock;
 } ILLUMOS_0.9;
 
 SYMBOL_VERSION ILLUMOS_0.9 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libc/port/sys/flock.c	Wed Nov 12 08:30:46 2014 -0800
@@ -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));
+}
--- a/usr/src/lib/libc/sparc/Makefile.com	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/lib/libc/sparc/Makefile.com	Wed Nov 12 08:30:46 2014 -0800
@@ -931,6 +931,7 @@
 	execv.o			\
 	fcntl.o			\
 	fexecve.o		\
+	flock.o			\
 	getpagesizes.o		\
 	getpeerucred.o		\
 	inst_sync.o		\
--- a/usr/src/lib/libc/sparcv9/Makefile.com	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/lib/libc/sparcv9/Makefile.com	Wed Nov 12 08:30:46 2014 -0800
@@ -877,6 +877,7 @@
 	execv.o			\
 	fcntl.o			\
 	fexecve.o		\
+	flock.o			\
 	getpagesizes.o		\
 	getpeerucred.o		\
 	inst_sync.o		\
--- a/usr/src/man/man3c/Makefile	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/man/man3c/Makefile	Wed Nov 12 08:30:46 2014 -0800
@@ -123,6 +123,7 @@
 	 	fgetpos.3c					\
 	 	fgetwc.3c					\
 	 	floating_to_decimal.3c				\
+		flock.3c					\
 	 	flockfile.3c					\
 	 	fmtmsg.3c					\
 	 	fnmatch.3c					\
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/man/man3c/flock.3c	Wed Nov 12 08:30:46 2014 -0800
@@ -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
--- a/usr/src/pkg/manifests/system-library.man3c.inc	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/pkg/manifests/system-library.man3c.inc	Wed Nov 12 08:30:46 2014 -0800
@@ -119,6 +119,7 @@
 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
--- a/usr/src/uts/common/sys/file.h	Wed Nov 12 04:51:53 2014 -0800
+++ b/usr/src/uts/common/sys/file.h	Wed Nov 12 08:30:46 2014 -0800
@@ -120,6 +120,17 @@
 #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
 
 /*