lockf.c revision 81586
1284237Sdelphij/* $NetBSD: lockf.c,v 1.1 1997/12/20 20:23:18 kleink Exp $ */ 2267843Sdelphij 3267843Sdelphij/*- 4284237Sdelphij * Copyright (c) 1997 The NetBSD Foundation, Inc. 5267843Sdelphij * All rights reserved. 6267843Sdelphij * 7267843Sdelphij * This code is derived from software contributed to The NetBSD Foundation 8267843Sdelphij * by Klaus Klein. 9267843Sdelphij * 10267843Sdelphij * Redistribution and use in source and binary forms, with or without 11267843Sdelphij * modification, are permitted provided that the following conditions 12267843Sdelphij * are met: 13267843Sdelphij * 1. Redistributions of source code must retain the above copyright 14267843Sdelphij * notice, this list of conditions and the following disclaimer. 15267843Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 16267843Sdelphij * notice, this list of conditions and the following disclaimer in the 17267843Sdelphij * documentation and/or other materials provided with the distribution. 18267843Sdelphij * 3. All advertising materials mentioning features or use of this software 19267843Sdelphij * must display the following acknowledgement: 20284237Sdelphij * This product includes software developed by the NetBSD 21284237Sdelphij * Foundation, Inc. and its contributors. 22284237Sdelphij * 4. Neither the name of The NetBSD Foundation nor the names of its 23284237Sdelphij * contributors may be used to endorse or promote products derived 24284237Sdelphij * from this software without specific prior written permission. 25284237Sdelphij * 26284237Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27284237Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28284237Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29284237Sdelphij * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30284237Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31267843Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32267843Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33267843Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34267843Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35267843Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36267843Sdelphij * POSSIBILITY OF SUCH DAMAGE. 37267843Sdelphij */ 38267843Sdelphij 39267843Sdelphij#if defined(LIBC_SCCS) && !defined(lint) 40267843Sdelphijstatic const char rcsid[]= 41267843Sdelphij "$FreeBSD: head/lib/libc/gen/lockf.c 81586 2001-08-13 14:06:34Z ru $"; 42267843Sdelphij#endif 43267843Sdelphij 44267843Sdelphij#include "namespace.h" 45267843Sdelphij#include <errno.h> 46267843Sdelphij#include <fcntl.h> 47267843Sdelphij#include <unistd.h> 48267843Sdelphij#include "un-namespace.h" 49267843Sdelphij 50267843Sdelphijint 51267843Sdelphijlockf(filedes, function, size) 52267843Sdelphij int filedes; 53267843Sdelphij int function; 54267843Sdelphij off_t size; 55267843Sdelphij{ 56267843Sdelphij struct flock fl; 57267843Sdelphij int cmd; 58267843Sdelphij 59267843Sdelphij fl.l_start = 0; 60267843Sdelphij fl.l_len = size; 61267843Sdelphij fl.l_whence = SEEK_CUR; 62267843Sdelphij 63267843Sdelphij switch (function) { 64267843Sdelphij case F_ULOCK: 65267843Sdelphij cmd = F_SETLK; 66267843Sdelphij fl.l_type = F_UNLCK; 67267843Sdelphij break; 68267843Sdelphij case F_LOCK: 69267843Sdelphij cmd = F_SETLKW; 70267843Sdelphij fl.l_type = F_WRLCK; 71267843Sdelphij break; 72267843Sdelphij case F_TLOCK: 73267843Sdelphij cmd = F_SETLK; 74267843Sdelphij fl.l_type = F_WRLCK; 75267843Sdelphij break; 76267843Sdelphij case F_TEST: 77267843Sdelphij fl.l_type = F_WRLCK; 78267843Sdelphij if (_fcntl(filedes, F_GETLK, &fl) == -1) 79267843Sdelphij return (-1); 80267843Sdelphij if (fl.l_type == F_UNLCK || fl.l_pid == getpid()) 81267843Sdelphij return (0); 82267843Sdelphij errno = EAGAIN; 83267843Sdelphij return (-1); 84267843Sdelphij /* NOTREACHED */ 85267843Sdelphij default: 86267843Sdelphij errno = EINVAL; 87267843Sdelphij return (-1); 88267843Sdelphij /* NOTREACHED */ 89267843Sdelphij } 90267843Sdelphij 91267843Sdelphij return (_fcntl(filedes, cmd, &fl)); 92267843Sdelphij} 93267843Sdelphij