diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1997-12-22 10:13:58 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1997-12-22 10:13:58 +0000 |
commit | 22fbd8c5f1b252f39bd0ca06b68c920b87cbd775 (patch) | |
tree | 6fd22e8a0704d25db818cb184ce653098e7957a3 | |
parent | 0e4b55891832a731c56c2da441696cfd856ac06c (diff) |
add XPG4.2 lockf; kleink
-rw-r--r-- | include/unistd.h | 12 | ||||
-rw-r--r-- | lib/libc/gen/Makefile.inc | 8 | ||||
-rw-r--r-- | lib/libc/gen/lockf.3 | 247 | ||||
-rw-r--r-- | lib/libc/gen/lockf.c | 89 | ||||
-rw-r--r-- | lib/libc/shlib_version | 2 |
5 files changed, 352 insertions, 6 deletions
diff --git a/include/unistd.h b/include/unistd.h index af7e79ab426..5aab2f3ff7f 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: unistd.h,v 1.14 1997/11/16 01:01:24 deraadt Exp $ */ +/* $OpenBSD: unistd.h,v 1.15 1997/12/22 10:13:50 deraadt Exp $ */ /* $NetBSD: unistd.h,v 1.26.4.1 1996/05/28 02:31:51 mrg Exp $ */ /*- @@ -191,6 +191,16 @@ extern int optreset; int getsubopt __P((char **, char * const *, char **)); extern char *suboptarg; /* getsubopt(3) external variable */ #endif /* !_POSIX_SOURCE */ + +#if (!defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) && \ + !defined(_XOPEN_SOURCE)) || \ + (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE_EXTENDED - 0 == 1) +#define F_ULOCK 0 +#define F_LOCK 1 +#define F_TLOCK 2 +#define F_TEST 3 +int lockf __P((int, int, off_t)); +#endif /* (!defined(_POSIX_SOURCE) && !defined(_XOPEN_SOURCE)) || ... */ __END_DECLS #endif /* !_UNISTD_H_ */ diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index b7bba5cd0fb..24bd548766a 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.15 1997/09/08 09:37:06 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.16 1997/12/22 10:13:55 deraadt Exp $ # gen sources .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/gen ${.CURDIR}/gen @@ -10,8 +10,8 @@ SRCS+= alarm.c assert.c basename.c clock.c closedir.c confstr.c ctermid.c \ getgrouplist.c gethostname.c getloadavg.c getlogin.c getmntinfo.c \ getnetgrent.c getpagesize.c getpass.c getpwent.c \ getttyent.c getusershell.c glob.c initgroups.c isatty.c isctype.c \ - nice.c nlist.c opendir.c pause.c popen.c psignal.c pwcache.c raise.c \ - readdir.c rewinddir.c scandir.c seekdir.c setdomainname.c \ + lockf.c nice.c nlist.c opendir.c pause.c popen.c psignal.c pwcache.c \ + raise.c readdir.c rewinddir.c scandir.c seekdir.c setdomainname.c \ sethostname.c setjmperr.c setmode.c setproctitle.c siginterrupt.c \ siglist.c signal.c signame.c sigsetops.c sleep.c sysconf.c sysctl.c \ syslog.c telldir.c time.c times.c timezone.c tolower_.c ttyname.c \ @@ -49,7 +49,7 @@ MAN+= alarm.3 basename.3 clock.3 confstr.3 ctermid.3 ctype.3 daemon.3 \ getmntinfo.3 getnetgrent.3 getpagesize.3 getpass.3 getpwent.3 \ getttyent.3 getusershell.3 glob.3 initgroups.3 isalnum.3 \ isalpha.3 isascii.3 isblank.3 iscntrl.3 isdigit.3 isgraph.3 isinf.3 \ - islower.3 isprint.3 ispunct.3 isspace.3 isupper.3 isxdigit.3 \ + islower.3 isprint.3 ispunct.3 isspace.3 isupper.3 isxdigit.3 lockf.3 \ ldexp.3 modf.3 nice.3 nlist.3 pause.3 popen.3 psignal.3 pwcache.3 \ raise.3 scandir.3 setjmp.3 setmode.3 setproctitle.3 siginterrupt.3 \ signal.3 sigsetops.3 sleep.3 sysconf.3 sysctl.3 syslog.3 \ diff --git a/lib/libc/gen/lockf.3 b/lib/libc/gen/lockf.3 new file mode 100644 index 00000000000..15e2260ff43 --- /dev/null +++ b/lib/libc/gen/lockf.3 @@ -0,0 +1,247 @@ +.\" $NetBSD: lockf.3,v 1.1 1997/12/20 20:23:17 kleink Exp $ +.\" +.\" Copyright (c) 1997 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Klaus Klein and S.P. Zeidler. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the NetBSD +.\" Foundation, Inc. and its contributors. +.\" 4. Neither the name of The NetBSD Foundation nor the names of its +.\" contributors may be used to endorse or promote products derived +.\" from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd December 19, 1997 +.Dt LOCKF 3 +.Os NetBSD 1.4 +.Sh NAME +.Nm lockf +.Nd record locking on files +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn lockf "int filedes" "int function" "off_t size" +.Sh DESCRIPTION +The +.Fn lockf +function allows sections of a file to be locked with advisory-mode locks. +Calls to +.Fn lockf +from other processes which attempt to lock the locked file section will +either return an error value or block until the section becomes unlocked. +All the locks for a process are removed when the process terminates. +.Pp +The argument +.Fa filedes +is an open file descriptor. +The file descriptor must have been opened either for write-only +.Dv ( O_WRONLY ) +or read/write +.Dv ( O_RDWR ) +operation. +.Pp +The +.Fa function +argument is a control value which specifies the action to be taken. +The permissible values for +.Fa function +are as follows: +.Bl -tag -width F_ULOCKXX -compact -offset indent +.It Sy Function +.Sy Description +.It Dv F_ULOCK +unlock locked sections +.It Dv F_LOCK +lock a section for exclusive use +.It Dv F_TLOCK +test and lock a section for exclusive use +.It Dv F_TEST +test a section for locks by other processes +.El +.Pp +.Dv F_ULOCK +removes locks from a section of the file; +.Dv F_LOCK +and +.Dv F_TLOCK +both lock a section of a file if the section is available; +.Dv F_TEST +detects if a lock by another process is present on the specified section. +.Pp +The +.Fa size +argument is the number of contiguous bytes to be locked or +unlocked. The section to be locked or unlocked starts at the current +offset in the file and extends forward for a positive size or backward +for a negative size (the preceding bytes up to but not including the +current offset). However, it is not permitted to lock a section that +starts or extends before the beginning of the file. +If +.Fa size +is 0, the section from the current offset through the largest possible +file offset is locked (that is, from the current offset through the +present or any future end-of-file). +.Pp +The sections locked with +.Dv F_LOCK +or +.Dv F_TLOCK +may, in whole or in part, contain or be contained by a previously +locked section for the same process. When this occurs, or if adjacent +locked sections would occur, the sections are combined into a single +locked section. If the request would cause the number of locks to +exceed a system-imposed limit, the request will fail. +.Pp +.Dv F_LOCK +and +.Dv F_TLOCK +requests differ only by the action taken if the section is not +available. +.Dv F_LOCK +blocks the calling process until the section is available. +.Dv F_TLOCK +makes the function fail if the section is already locked by another +process. +.Pp +File locks are released on first close by the locking process of any +file descriptor for the file. +.Pp +.Dv F_ULOCK +requests release (wholly or in part) one or more locked sections +controlled by the process. Locked sections will be unlocked starting +at the current file offset through +.Fa size +bytes or to the end of file if size is 0. When all of a locked section +is not released (that is, when the beginning or end of the area to be +unlocked falls within a locked section), the remaining portions of +that section are still locked by the process. Releasing the center +portion of a locked section will cause the remaining locked beginning +and end portions to become two separate locked sections. If the +request would cause the number of locks in the system to exceed a +system-imposed limit, the request will fail. +.Pp +An +.Dv F_ULOCK +request in which size is non-zero and the offset of the last byte of +the requested section is the maximum value for an object of type +off_t, when the process has an existing lock in which size is 0 and +which includes the last byte of the requested section, will be treated +as a request to unlock from the start of the requested section with a +size equal to 0. Otherwise an +.Dv F_ULOCK +request will attempt to unlock only the requested section. +.Pp +A potential for deadlock occurs if a process controlling a locked +region is put to sleep by attempting to lock the locked region of +another process. This implementation detects that sleeping until a +locked region is unlocked would cause a deadlock and fails with an +.Er EDEADLK +error. +.Pp +.Fn lockf , +.Xr fcntl 2 +and +.Xr flock 2 +locks may be safely used concurrently. +.Pp +Blocking on a section is interrupted by any signal. +.Sh RETURN VALUES +If successful, the +.Fn lockf +function returns 0. +Otherwise, it returns -1, sets +.Dv errno +to indicate an error, and existing locks are not changed. +.Sh ERRORS +.Fn lockf +will fail if: +.Bl -tag -width Er +.It Bq Er EAGAIN +The argument +.Fa function +is +.Dv F_TLOCK +or +.Dv F_TEST +and the section is already locked by another process. +.It Bq Er EBADF +The argument +.Fa filedes +is not a valid open file descriptor. +.Pp +The argument +.Fa function +is +.Dv F_LOCK +or +.Dv F_TLOCK , +and +.Fa filedes +is not a valid file descriptor open for writing. +.It Bq Er EDEADLK +The argument +.Fa function +is +.Dv F_LOCK +and a deadlock is detected. +.It Bq Er EINTR +The argument +.Fa function +is F_LOCK +and +.Fn lockf +was interrupted by the delivery of a signal. +.It Bq Er EINVAL +The argument +.Fa function +is not one of +.Dv F_ULOCK , +.Dv F_LOCK , +.Dv F_TLOCK +or +.Dv F_TEST . +.Pp +The argument +.Fa filedes +refers to a file that does not support locking. +.It Bq Er ENOLCK +The argument +.Fa function +is +.Dv F_ULOCK , +.Dv F_LOCK +or +.Dv F_TLOCK , +and satisfying the lock or unlock request would result in the number +of locked regions in the system exceeding a system-imposed limit. +.Sh SEE ALSO +.Xr fcntl 2 , +.Xr flock 2 +.Sh STANDARDS +The +.Fn lockf +function conforms to +.St -xpg4.2 . diff --git a/lib/libc/gen/lockf.c b/lib/libc/gen/lockf.c new file mode 100644 index 00000000000..b3b54d04353 --- /dev/null +++ b/lib/libc/gen/lockf.c @@ -0,0 +1,89 @@ +/* $NetBSD: lockf.c,v 1.1 1997/12/20 20:23:18 kleink Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Klaus Klein. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$NetBSD: lockf.c,v 1.1 1997/12/20 20:23:18 kleink Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +int +lockf(filedes, function, size) + int filedes; + int function; + off_t size; +{ + struct flock fl; + int cmd; + + fl.l_start = 0; + fl.l_len = size; + fl.l_whence = SEEK_CUR; + + switch (function) { + case F_ULOCK: + cmd = F_SETLK; + fl.l_type = F_UNLCK; + break; + case F_LOCK: + cmd = F_SETLKW; + fl.l_type = F_WRLCK; + break; + case F_TLOCK: + cmd = F_SETLK; + fl.l_type = F_WRLCK; + break; + case F_TEST: + fl.l_type = F_WRLCK; + if (fcntl(filedes, F_GETLK, &fl) == -1) + return (-1); + if (fl.l_type == F_UNLCK || fl.l_pid == getpid()) + return (0); + errno = EAGAIN; + return (-1); + /* NOTREACHED */ + default: + errno = EINVAL; + return (-1); + /* NOTREACHED */ + } + + return (fcntl(filedes, cmd, &fl)); +} diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version index 16555489150..cd56ff5f1e5 100644 --- a/lib/libc/shlib_version +++ b/lib/libc/shlib_version @@ -1,2 +1,2 @@ major=17 -minor=5 +minor=6 |