lockf.c revision 189328
11573Srgrimes/* $NetBSD: lockf.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ 21573Srgrimes/*- 31573Srgrimes * Copyright (c) 1997 The NetBSD Foundation, Inc. 41573Srgrimes * All rights reserved. 51573Srgrimes * 61573Srgrimes * This code is derived from software contributed to The NetBSD Foundation 71573Srgrimes * by Klaus Klein. 81573Srgrimes * 91573Srgrimes * Redistribution and use in source and binary forms, with or without 101573Srgrimes * modification, are permitted provided that the following conditions 111573Srgrimes * are met: 121573Srgrimes * 1. Redistributions of source code must retain the above copyright 131573Srgrimes * notice, this list of conditions and the following disclaimer. 141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151573Srgrimes * notice, this list of conditions and the following disclaimer in the 161573Srgrimes * documentation and/or other materials provided with the distribution. 171573Srgrimes * 181573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 191573Srgrimes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 201573Srgrimes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 211573Srgrimes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 221573Srgrimes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 231573Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 241573Srgrimes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 251573Srgrimes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 261573Srgrimes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 271573Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 281573Srgrimes * POSSIBILITY OF SUCH DAMAGE. 291573Srgrimes */ 301573Srgrimes 311573Srgrimes#include <sys/cdefs.h> 321573Srgrimes__FBSDID("$FreeBSD: head/lib/libc/gen/lockf.c 189328 2009-03-04 01:01:26Z delphij $"); 331573Srgrimes 341573Srgrimes#include "namespace.h" 351573Srgrimes#include <errno.h> 361573Srgrimes#include <fcntl.h> 371573Srgrimes#include <unistd.h> 381573Srgrimes#include "un-namespace.h" 391573Srgrimes 4090039Sobrienint 4190039Sobrienlockf(int filedes, int function, off_t size) 421573Srgrimes{ 4371579Sdeischen struct flock fl; 441573Srgrimes int cmd; 451573Srgrimes 461573Srgrimes fl.l_start = 0; 478870Srgrimes fl.l_len = size; 481573Srgrimes fl.l_whence = SEEK_CUR; 491573Srgrimes 501573Srgrimes switch (function) { 511573Srgrimes case F_ULOCK: 521573Srgrimes cmd = F_SETLK; 531573Srgrimes fl.l_type = F_UNLCK; 5471579Sdeischen break; 551573Srgrimes case F_LOCK: 5671579Sdeischen cmd = F_SETLKW; 5771579Sdeischen fl.l_type = F_WRLCK; 581573Srgrimes break; 591573Srgrimes case F_TLOCK: 601573Srgrimes cmd = F_SETLK; 611573Srgrimes fl.l_type = F_WRLCK; 621573Srgrimes break; 631573Srgrimes case F_TEST: 641573Srgrimes fl.l_type = F_WRLCK; 651573Srgrimes if (_fcntl(filedes, F_GETLK, &fl) == -1) 661573Srgrimes return (-1); 671573Srgrimes if (fl.l_type == F_UNLCK || (fl.l_sysid == 0 && fl.l_pid == getpid())) 681573Srgrimes return (0); 691573Srgrimes errno = EAGAIN; 701573Srgrimes return (-1); 711573Srgrimes /* NOTREACHED */ 7290039Sobrien default: 7390039Sobrien errno = EINVAL; 7490039Sobrien return (-1); 751573Srgrimes /* NOTREACHED */ 761573Srgrimes } 771573Srgrimes 781573Srgrimes return (_fcntl(filedes, cmd, &fl)); 791573Srgrimes} 801573Srgrimes