1*c8a0e2f4SThomas Veerman /* $NetBSD: flock.c,v 1.6 2008/04/28 20:24:12 martin Exp $ */
2*c8a0e2f4SThomas Veerman
3*c8a0e2f4SThomas Veerman /*-
4*c8a0e2f4SThomas Veerman * Copyright (c) 2001 The NetBSD Foundation, Inc.
5*c8a0e2f4SThomas Veerman * All rights reserved.
6*c8a0e2f4SThomas Veerman *
7*c8a0e2f4SThomas Veerman * This code is derived from software contributed to The NetBSD Foundation
8*c8a0e2f4SThomas Veerman * by Todd Vierling.
9*c8a0e2f4SThomas Veerman *
10*c8a0e2f4SThomas Veerman * Redistribution and use in source and binary forms, with or without
11*c8a0e2f4SThomas Veerman * modification, are permitted provided that the following conditions
12*c8a0e2f4SThomas Veerman * are met:
13*c8a0e2f4SThomas Veerman * 1. Redistributions of source code must retain the above copyright
14*c8a0e2f4SThomas Veerman * notice, this list of conditions and the following disclaimer.
15*c8a0e2f4SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
16*c8a0e2f4SThomas Veerman * notice, this list of conditions and the following disclaimer in the
17*c8a0e2f4SThomas Veerman * documentation and/or other materials provided with the distribution.
18*c8a0e2f4SThomas Veerman *
19*c8a0e2f4SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*c8a0e2f4SThomas Veerman * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*c8a0e2f4SThomas Veerman * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*c8a0e2f4SThomas Veerman * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*c8a0e2f4SThomas Veerman * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*c8a0e2f4SThomas Veerman * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*c8a0e2f4SThomas Veerman * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*c8a0e2f4SThomas Veerman * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*c8a0e2f4SThomas Veerman * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*c8a0e2f4SThomas Veerman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*c8a0e2f4SThomas Veerman * POSSIBILITY OF SUCH DAMAGE.
30*c8a0e2f4SThomas Veerman */
31*c8a0e2f4SThomas Veerman
32*c8a0e2f4SThomas Veerman /*
33*c8a0e2f4SThomas Veerman * Emulate flock() with fcntl(), where available.
34*c8a0e2f4SThomas Veerman * Otherwise, don't do locking; just pretend success.
35*c8a0e2f4SThomas Veerman */
36*c8a0e2f4SThomas Veerman
37*c8a0e2f4SThomas Veerman #include "nbtool_config.h"
38*c8a0e2f4SThomas Veerman
39*c8a0e2f4SThomas Veerman #if !HAVE_FLOCK
40*c8a0e2f4SThomas Veerman #include <errno.h>
41*c8a0e2f4SThomas Veerman #include <fcntl.h>
42*c8a0e2f4SThomas Veerman
flock(int fd,int op)43*c8a0e2f4SThomas Veerman int flock(int fd, int op) {
44*c8a0e2f4SThomas Veerman int rc = 0;
45*c8a0e2f4SThomas Veerman
46*c8a0e2f4SThomas Veerman #if defined(F_SETLK) && defined(F_SETLKW)
47*c8a0e2f4SThomas Veerman struct flock fl = {0};
48*c8a0e2f4SThomas Veerman
49*c8a0e2f4SThomas Veerman switch (op & (LOCK_EX|LOCK_SH|LOCK_UN)) {
50*c8a0e2f4SThomas Veerman case LOCK_EX:
51*c8a0e2f4SThomas Veerman fl.l_type = F_WRLCK;
52*c8a0e2f4SThomas Veerman break;
53*c8a0e2f4SThomas Veerman
54*c8a0e2f4SThomas Veerman case LOCK_SH:
55*c8a0e2f4SThomas Veerman fl.l_type = F_RDLCK;
56*c8a0e2f4SThomas Veerman break;
57*c8a0e2f4SThomas Veerman
58*c8a0e2f4SThomas Veerman case LOCK_UN:
59*c8a0e2f4SThomas Veerman fl.l_type = F_UNLCK;
60*c8a0e2f4SThomas Veerman break;
61*c8a0e2f4SThomas Veerman
62*c8a0e2f4SThomas Veerman default:
63*c8a0e2f4SThomas Veerman errno = EINVAL;
64*c8a0e2f4SThomas Veerman return -1;
65*c8a0e2f4SThomas Veerman }
66*c8a0e2f4SThomas Veerman
67*c8a0e2f4SThomas Veerman fl.l_whence = SEEK_SET;
68*c8a0e2f4SThomas Veerman rc = fcntl(fd, op & LOCK_NB ? F_SETLK : F_SETLKW, &fl);
69*c8a0e2f4SThomas Veerman
70*c8a0e2f4SThomas Veerman if (rc && (errno == EAGAIN))
71*c8a0e2f4SThomas Veerman errno = EWOULDBLOCK;
72*c8a0e2f4SThomas Veerman #endif
73*c8a0e2f4SThomas Veerman
74*c8a0e2f4SThomas Veerman return rc;
75*c8a0e2f4SThomas Veerman }
76*c8a0e2f4SThomas Veerman #endif
77