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