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"
3937510Sdt
4037510Sdtint
41189328Sdelphijlockf(int filedes, int function, off_t size)
4237510Sdt{
4337510Sdt	struct flock fl;
4437510Sdt	int cmd;
4537510Sdt
4637510Sdt	fl.l_start = 0;
4737510Sdt	fl.l_len = size;
4837510Sdt	fl.l_whence = SEEK_CUR;
4937510Sdt
5037510Sdt	switch (function) {
5137510Sdt	case F_ULOCK:
5237510Sdt		cmd = F_SETLK;
5337510Sdt		fl.l_type = F_UNLCK;
5437510Sdt		break;
5537510Sdt	case F_LOCK:
5637510Sdt		cmd = F_SETLKW;
5737510Sdt		fl.l_type = F_WRLCK;
5837510Sdt		break;
5937510Sdt	case F_TLOCK:
6037510Sdt		cmd = F_SETLK;
6137510Sdt		fl.l_type = F_WRLCK;
6237510Sdt		break;
6337510Sdt	case F_TEST:
6437510Sdt		fl.l_type = F_WRLCK;
6556698Sjasone		if (_fcntl(filedes, F_GETLK, &fl) == -1)
6637510Sdt			return (-1);
67177633Sdfr		if (fl.l_type == F_UNLCK || (fl.l_sysid == 0 && fl.l_pid == getpid()))
6837510Sdt			return (0);
6937510Sdt		errno = EAGAIN;
7037510Sdt		return (-1);
7137510Sdt		/* NOTREACHED */
7237510Sdt	default:
7337510Sdt		errno = EINVAL;
7437510Sdt		return (-1);
7537510Sdt		/* NOTREACHED */
7637510Sdt	}
7737510Sdt
7856698Sjasone	return (_fcntl(filedes, cmd, &fl));
7937510Sdt}
80