1*ce099b40Smartin /* $NetBSD: flock.c,v 1.6 2008/04/28 20:24:12 martin Exp $ */
29d5d28c2Stv
39d5d28c2Stv /*-
49d5d28c2Stv * Copyright (c) 2001 The NetBSD Foundation, Inc.
59d5d28c2Stv * All rights reserved.
69d5d28c2Stv *
79d5d28c2Stv * This code is derived from software contributed to The NetBSD Foundation
89d5d28c2Stv * by Todd Vierling.
99d5d28c2Stv *
109d5d28c2Stv * Redistribution and use in source and binary forms, with or without
119d5d28c2Stv * modification, are permitted provided that the following conditions
129d5d28c2Stv * are met:
139d5d28c2Stv * 1. Redistributions of source code must retain the above copyright
149d5d28c2Stv * notice, this list of conditions and the following disclaimer.
159d5d28c2Stv * 2. Redistributions in binary form must reproduce the above copyright
169d5d28c2Stv * notice, this list of conditions and the following disclaimer in the
179d5d28c2Stv * documentation and/or other materials provided with the distribution.
189d5d28c2Stv *
199d5d28c2Stv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209d5d28c2Stv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219d5d28c2Stv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229d5d28c2Stv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239d5d28c2Stv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249d5d28c2Stv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259d5d28c2Stv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269d5d28c2Stv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279d5d28c2Stv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289d5d28c2Stv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299d5d28c2Stv * POSSIBILITY OF SUCH DAMAGE.
309d5d28c2Stv */
319d5d28c2Stv
3220046385Stv /*
3320046385Stv * Emulate flock() with fcntl(), where available.
34f8af6468Smason * Otherwise, don't do locking; just pretend success.
3520046385Stv */
369d5d28c2Stv
37171d6532Slukem #include "nbtool_config.h"
389d5d28c2Stv
3920046385Stv #if !HAVE_FLOCK
409d5d28c2Stv #include <errno.h>
419d5d28c2Stv #include <fcntl.h>
429d5d28c2Stv
flock(int fd,int op)439d5d28c2Stv int flock(int fd, int op) {
449d5d28c2Stv int rc = 0;
459d5d28c2Stv
469d5d28c2Stv #if defined(F_SETLK) && defined(F_SETLKW)
479d5d28c2Stv struct flock fl = {0};
489d5d28c2Stv
499d5d28c2Stv switch (op & (LOCK_EX|LOCK_SH|LOCK_UN)) {
509d5d28c2Stv case LOCK_EX:
519d5d28c2Stv fl.l_type = F_WRLCK;
529d5d28c2Stv break;
539d5d28c2Stv
549d5d28c2Stv case LOCK_SH:
559d5d28c2Stv fl.l_type = F_RDLCK;
569d5d28c2Stv break;
579d5d28c2Stv
589d5d28c2Stv case LOCK_UN:
599d5d28c2Stv fl.l_type = F_UNLCK;
609d5d28c2Stv break;
619d5d28c2Stv
629d5d28c2Stv default:
639d5d28c2Stv errno = EINVAL;
649d5d28c2Stv return -1;
659d5d28c2Stv }
669d5d28c2Stv
679d5d28c2Stv fl.l_whence = SEEK_SET;
68d6c76879Stv rc = fcntl(fd, op & LOCK_NB ? F_SETLK : F_SETLKW, &fl);
699d5d28c2Stv
709d5d28c2Stv if (rc && (errno == EAGAIN))
719d5d28c2Stv errno = EWOULDBLOCK;
729d5d28c2Stv #endif
739d5d28c2Stv
749d5d28c2Stv return rc;
759d5d28c2Stv }
769d5d28c2Stv #endif
77