1d7847a8dSBrooks Davis /* $NetBSD: lockf.c,v 1.3 2008/04/28 20:22:59 martin Exp $ */ 2d7847a8dSBrooks Davis /*- 3d7847a8dSBrooks Davis * SPDX-License-Identifier: BSD-2-Clause 4d7847a8dSBrooks Davis * 5d7847a8dSBrooks Davis * Copyright (c) 1997 The NetBSD Foundation, Inc. 6d7847a8dSBrooks Davis * All rights reserved. 7d7847a8dSBrooks Davis * 8d7847a8dSBrooks Davis * This code is derived from software contributed to The NetBSD Foundation 9d7847a8dSBrooks Davis * by Klaus Klein. 10d7847a8dSBrooks Davis * 11d7847a8dSBrooks Davis * Redistribution and use in source and binary forms, with or without 12d7847a8dSBrooks Davis * modification, are permitted provided that the following conditions 13d7847a8dSBrooks Davis * are met: 14d7847a8dSBrooks Davis * 1. Redistributions of source code must retain the above copyright 15d7847a8dSBrooks Davis * notice, this list of conditions and the following disclaimer. 16d7847a8dSBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright 17d7847a8dSBrooks Davis * notice, this list of conditions and the following disclaimer in the 18d7847a8dSBrooks Davis * documentation and/or other materials provided with the distribution. 19d7847a8dSBrooks Davis * 20d7847a8dSBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21d7847a8dSBrooks Davis * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22d7847a8dSBrooks Davis * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23d7847a8dSBrooks Davis * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24d7847a8dSBrooks Davis * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25d7847a8dSBrooks Davis * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26d7847a8dSBrooks Davis * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27d7847a8dSBrooks Davis * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28d7847a8dSBrooks Davis * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29d7847a8dSBrooks Davis * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30d7847a8dSBrooks Davis * POSSIBILITY OF SUCH DAMAGE. 31d7847a8dSBrooks Davis */ 32d7847a8dSBrooks Davis 33d7847a8dSBrooks Davis #include "namespace.h" 34d7847a8dSBrooks Davis #include <errno.h> 35d7847a8dSBrooks Davis #include <fcntl.h> 36d7847a8dSBrooks Davis #include <unistd.h> 37d7847a8dSBrooks Davis #include "un-namespace.h" 38d7847a8dSBrooks Davis #include "libc_private.h" 39d7847a8dSBrooks Davis 40d7847a8dSBrooks Davis int 41d7847a8dSBrooks Davis lockf(int filedes, int function, off_t size) 42d7847a8dSBrooks Davis { 43d7847a8dSBrooks Davis struct flock fl; 44d7847a8dSBrooks Davis int cmd; 45d7847a8dSBrooks Davis 46d7847a8dSBrooks Davis fl.l_start = 0; 47d7847a8dSBrooks Davis fl.l_len = size; 48d7847a8dSBrooks Davis fl.l_whence = SEEK_CUR; 49d7847a8dSBrooks Davis 50d7847a8dSBrooks Davis switch (function) { 51d7847a8dSBrooks Davis case F_ULOCK: 52d7847a8dSBrooks Davis cmd = F_SETLK; 53d7847a8dSBrooks Davis fl.l_type = F_UNLCK; 54d7847a8dSBrooks Davis break; 55d7847a8dSBrooks Davis case F_LOCK: 56d7847a8dSBrooks Davis cmd = F_SETLKW; 57d7847a8dSBrooks Davis fl.l_type = F_WRLCK; 58d7847a8dSBrooks Davis break; 59d7847a8dSBrooks Davis case F_TLOCK: 60d7847a8dSBrooks Davis cmd = F_SETLK; 61d7847a8dSBrooks Davis fl.l_type = F_WRLCK; 62d7847a8dSBrooks Davis break; 63d7847a8dSBrooks Davis case F_TEST: 64d7847a8dSBrooks Davis fl.l_type = F_WRLCK; 65d7847a8dSBrooks Davis if (((int (*)(int, int, ...)) 66d7847a8dSBrooks Davis *(__libc_interposing_slot(INTERPOS_fcntl))) 67d7847a8dSBrooks Davis (filedes, F_GETLK, &fl) == -1) 68d7847a8dSBrooks Davis return (-1); 69d7847a8dSBrooks Davis if (fl.l_type == F_UNLCK || (fl.l_sysid == 0 && 70d7847a8dSBrooks Davis fl.l_pid == getpid())) 71d7847a8dSBrooks Davis return (0); 72d7847a8dSBrooks Davis errno = EAGAIN; 73d7847a8dSBrooks Davis return (-1); 74d7847a8dSBrooks Davis /* NOTREACHED */ 75d7847a8dSBrooks Davis default: 76d7847a8dSBrooks Davis errno = EINVAL; 77d7847a8dSBrooks Davis return (-1); 78d7847a8dSBrooks Davis /* NOTREACHED */ 79d7847a8dSBrooks Davis } 80d7847a8dSBrooks Davis 81*7dd9070eSBrooks Davis return (INTERPOS_SYS(fcntl, filedes, cmd, (intptr_t)&fl)); 82d7847a8dSBrooks Davis } 83