1441eac4des/*	$NetBSD: flock.c,v 1.6 2008/04/28 20:24:12 martin Exp $	*/
2441eac4des
3441eac4des/*-
4441eac4des * Copyright (c) 2001 The NetBSD Foundation, Inc.
5441eac4des * All rights reserved.
6441eac4des *
7441eac4des * This code is derived from software contributed to The NetBSD Foundation
8441eac4des * by Todd Vierling.
9441eac4des *
10441eac4des * Redistribution and use in source and binary forms, with or without
11441eac4des * modification, are permitted provided that the following conditions
12441eac4des * are met:
13441eac4des * 1. Redistributions of source code must retain the above copyright
14441eac4des *    notice, this list of conditions and the following disclaimer.
15441eac4des * 2. Redistributions in binary form must reproduce the above copyright
16441eac4des *    notice, this list of conditions and the following disclaimer in the
17441eac4des *    documentation and/or other materials provided with the distribution.
18441eac4des *
19441eac4des * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20441eac4des * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21441eac4des * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22441eac4des * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23441eac4des * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24441eac4des * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25441eac4des * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26441eac4des * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27441eac4des * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28441eac4des * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29441eac4des * POSSIBILITY OF SUCH DAMAGE.
30441eac4des */
31441eac4des
32441eac4des/*
33441eac4des * Emulate flock() with fcntl(), where available.
34441eac4des * Otherwise, don't do locking; just pretend success.
35441eac4des */
36441eac4des
37441eac4des#include "includes.h"
38441eac4des
39441eac4des#ifndef HAVE_FLOCK
40441eac4des#include <errno.h>
41441eac4des#include <fcntl.h>
42441eac4des
43441eac4desint
44441eac4desflock(int fd, int op)
45441eac4des{
46441eac4des	int rc = 0;
47441eac4des
48441eac4des#if defined(F_SETLK) && defined(F_SETLKW)
49441eac4des	struct flock fl = {0};
50441eac4des
51441eac4des	switch (op & (LOCK_EX|LOCK_SH|LOCK_UN)) {
52441eac4des	case LOCK_EX:
53441eac4des		fl.l_type = F_WRLCK;
54441eac4des		break;
55441eac4des
56441eac4des	case LOCK_SH:
57441eac4des		fl.l_type = F_RDLCK;
58441eac4des		break;
59441eac4des
60441eac4des	case LOCK_UN:
61441eac4des		fl.l_type = F_UNLCK;
62441eac4des		break;
63441eac4des
64441eac4des	default:
65441eac4des		errno = EINVAL;
66441eac4des		return -1;
67441eac4des	}
68441eac4des
69441eac4des	fl.l_whence = SEEK_SET;
70441eac4des	rc = fcntl(fd, op & LOCK_NB ? F_SETLK : F_SETLKW, &fl);
71441eac4des
72441eac4des	if (rc && (errno == EAGAIN))
73441eac4des		errno = EWOULDBLOCK;
74441eac4des#else
75441eac4des	rc = -1;
76441eac4des	errno = ENOSYS;
77441eac4des#endif
78441eac4des
79441eac4des	return rc;
80441eac4des}
81441eac4des#endif
82