186d7f5d3SJohn Marino /* $NetBSD: lockf.c,v 1.1 1997/12/20 20:23:18 kleink Exp $ */
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino /*-
486d7f5d3SJohn Marino * Copyright (c) 1997 The NetBSD Foundation, Inc.
586d7f5d3SJohn Marino * All rights reserved.
686d7f5d3SJohn Marino *
786d7f5d3SJohn Marino * This code is derived from software contributed to The NetBSD Foundation
886d7f5d3SJohn Marino * by Klaus Klein.
986d7f5d3SJohn Marino *
1086d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
1186d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
1286d7f5d3SJohn Marino * are met:
1386d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
1486d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
1586d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1686d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
1786d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
1886d7f5d3SJohn Marino * 3. All advertising materials mentioning features or use of this software
1986d7f5d3SJohn Marino * must display the following acknowledgement:
2086d7f5d3SJohn Marino * This product includes software developed by the NetBSD
2186d7f5d3SJohn Marino * Foundation, Inc. and its contributors.
2286d7f5d3SJohn Marino * 4. Neither the name of The NetBSD Foundation nor the names of its
2386d7f5d3SJohn Marino * contributors may be used to endorse or promote products derived
2486d7f5d3SJohn Marino * from this software without specific prior written permission.
2586d7f5d3SJohn Marino *
2686d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2786d7f5d3SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2886d7f5d3SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2986d7f5d3SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3086d7f5d3SJohn Marino * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3186d7f5d3SJohn Marino * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3286d7f5d3SJohn Marino * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3386d7f5d3SJohn Marino * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3486d7f5d3SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3586d7f5d3SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3686d7f5d3SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
3786d7f5d3SJohn Marino *
3886d7f5d3SJohn Marino * $FreeBSD: src/lib/libc/gen/lockf.c,v 1.5 2000/01/27 23:06:17 jasone Exp $
3986d7f5d3SJohn Marino * $DragonFly: src/lib/libc/gen/lockf.c,v 1.4 2005/11/13 00:07:42 swildner Exp $
4086d7f5d3SJohn Marino */
4186d7f5d3SJohn Marino
4286d7f5d3SJohn Marino #include "namespace.h"
4386d7f5d3SJohn Marino #include <errno.h>
4486d7f5d3SJohn Marino #include <fcntl.h>
4586d7f5d3SJohn Marino #include <unistd.h>
4686d7f5d3SJohn Marino #include "un-namespace.h"
4786d7f5d3SJohn Marino
4886d7f5d3SJohn Marino int
lockf(int filedes,int function,off_t size)4986d7f5d3SJohn Marino lockf(int filedes, int function, off_t size)
5086d7f5d3SJohn Marino {
5186d7f5d3SJohn Marino struct flock fl;
5286d7f5d3SJohn Marino int cmd;
5386d7f5d3SJohn Marino
5486d7f5d3SJohn Marino fl.l_start = 0;
5586d7f5d3SJohn Marino fl.l_len = size;
5686d7f5d3SJohn Marino fl.l_whence = SEEK_CUR;
5786d7f5d3SJohn Marino
5886d7f5d3SJohn Marino switch (function) {
5986d7f5d3SJohn Marino case F_ULOCK:
6086d7f5d3SJohn Marino cmd = F_SETLK;
6186d7f5d3SJohn Marino fl.l_type = F_UNLCK;
6286d7f5d3SJohn Marino break;
6386d7f5d3SJohn Marino case F_LOCK:
6486d7f5d3SJohn Marino cmd = F_SETLKW;
6586d7f5d3SJohn Marino fl.l_type = F_WRLCK;
6686d7f5d3SJohn Marino break;
6786d7f5d3SJohn Marino case F_TLOCK:
6886d7f5d3SJohn Marino cmd = F_SETLK;
6986d7f5d3SJohn Marino fl.l_type = F_WRLCK;
7086d7f5d3SJohn Marino break;
7186d7f5d3SJohn Marino case F_TEST:
7286d7f5d3SJohn Marino fl.l_type = F_WRLCK;
7386d7f5d3SJohn Marino if (_fcntl(filedes, F_GETLK, &fl) == -1)
7486d7f5d3SJohn Marino return (-1);
7586d7f5d3SJohn Marino if (fl.l_type == F_UNLCK || fl.l_pid == getpid())
7686d7f5d3SJohn Marino return (0);
7786d7f5d3SJohn Marino errno = EAGAIN;
7886d7f5d3SJohn Marino return (-1);
7986d7f5d3SJohn Marino /* NOTREACHED */
8086d7f5d3SJohn Marino default:
8186d7f5d3SJohn Marino errno = EINVAL;
8286d7f5d3SJohn Marino return (-1);
8386d7f5d3SJohn Marino /* NOTREACHED */
8486d7f5d3SJohn Marino }
8586d7f5d3SJohn Marino
8686d7f5d3SJohn Marino return (_fcntl(filedes, cmd, &fl));
8786d7f5d3SJohn Marino }
88