1189328Sdelphij/*	$NetBSD: lockf.c,v 1.3 2008/04/28 20:22:59 martin Exp $	*/
237510Sdt/*-
337510Sdt * Copyright (c) 1997 The NetBSD Foundation, Inc.
437510Sdt * All rights reserved.
537510Sdt *
637510Sdt * This code is derived from software contributed to The NetBSD Foundation
737510Sdt * by Klaus Klein.
837510Sdt *
937510Sdt * Redistribution and use in source and binary forms, with or without
1037510Sdt * modification, are permitted provided that the following conditions
1137510Sdt * are met:
1237510Sdt * 1. Redistributions of source code must retain the above copyright
1337510Sdt *    notice, this list of conditions and the following disclaimer.
1437510Sdt * 2. Redistributions in binary form must reproduce the above copyright
1537510Sdt *    notice, this list of conditions and the following disclaimer in the
1637510Sdt *    documentation and/or other materials provided with the distribution.
1737510Sdt *
1837510Sdt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1937510Sdt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2037510Sdt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2137510Sdt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2237510Sdt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2337510Sdt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2437510Sdt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2537510Sdt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2637510Sdt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2737510Sdt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2837510Sdt * POSSIBILITY OF SUCH DAMAGE.
2937510Sdt */
3037510Sdt
3190039Sobrien#include <sys/cdefs.h>
3290039Sobrien__FBSDID("$FreeBSD$");
3337510Sdt
3471579Sdeischen#include "namespace.h"
3537510Sdt#include <errno.h>
3637510Sdt#include <fcntl.h>
3737510Sdt#include <unistd.h>
3871579Sdeischen#include "un-namespace.h"
39292762Skib#include "libc_private.h"
4037510Sdt
4137510Sdtint
42189328Sdelphijlockf(int filedes, int function, off_t size)
4337510Sdt{
4437510Sdt	struct flock fl;
4537510Sdt	int cmd;
4637510Sdt
4737510Sdt	fl.l_start = 0;
4837510Sdt	fl.l_len = size;
4937510Sdt	fl.l_whence = SEEK_CUR;
5037510Sdt
5137510Sdt	switch (function) {
5237510Sdt	case F_ULOCK:
5337510Sdt		cmd = F_SETLK;
5437510Sdt		fl.l_type = F_UNLCK;
5537510Sdt		break;
5637510Sdt	case F_LOCK:
5737510Sdt		cmd = F_SETLKW;
5837510Sdt		fl.l_type = F_WRLCK;
5937510Sdt		break;
6037510Sdt	case F_TLOCK:
6137510Sdt		cmd = F_SETLK;
6237510Sdt		fl.l_type = F_WRLCK;
6337510Sdt		break;
6437510Sdt	case F_TEST:
6537510Sdt		fl.l_type = F_WRLCK;
66292762Skib		if (((int (*)(int, int, ...))
67292762Skib		    __libc_interposing[INTERPOS_fcntl])(filedes, F_GETLK, &fl)
68292762Skib		    == -1)
6937510Sdt			return (-1);
70292762Skib		if (fl.l_type == F_UNLCK || (fl.l_sysid == 0 &&
71292762Skib		    fl.l_pid == getpid()))
7237510Sdt			return (0);
7337510Sdt		errno = EAGAIN;
7437510Sdt		return (-1);
7537510Sdt		/* NOTREACHED */
7637510Sdt	default:
7737510Sdt		errno = EINVAL;
7837510Sdt		return (-1);
7937510Sdt		/* NOTREACHED */
8037510Sdt	}
8137510Sdt
82292762Skib	return (((int (*)(int, int, ...))
83292762Skib	    __libc_interposing[INTERPOS_fcntl])(filedes, cmd, &fl));
8437510Sdt}
85