xref: /openbsd-src/lib/libc/gen/lockf.c (revision d874cce4b1d9fe6b41c9e4f2117a77d8a4a37b92)
1*d874cce4Sray /*	$OpenBSD: lockf.c,v 1.5 2008/06/26 05:42:05 ray Exp $	*/
2246301a8Sderaadt /*	$NetBSD: lockf.c,v 1.1 1997/12/20 20:23:18 kleink Exp $	*/
3246301a8Sderaadt 
4246301a8Sderaadt /*-
5246301a8Sderaadt  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6246301a8Sderaadt  * All rights reserved.
7246301a8Sderaadt  *
8246301a8Sderaadt  * This code is derived from software contributed to The NetBSD Foundation
9246301a8Sderaadt  * by Klaus Klein.
10246301a8Sderaadt  *
11246301a8Sderaadt  * Redistribution and use in source and binary forms, with or without
12246301a8Sderaadt  * modification, are permitted provided that the following conditions
13246301a8Sderaadt  * are met:
14246301a8Sderaadt  * 1. Redistributions of source code must retain the above copyright
15246301a8Sderaadt  *    notice, this list of conditions and the following disclaimer.
16246301a8Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
17246301a8Sderaadt  *    notice, this list of conditions and the following disclaimer in the
18246301a8Sderaadt  *    documentation and/or other materials provided with the distribution.
19246301a8Sderaadt  *
20246301a8Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21246301a8Sderaadt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22246301a8Sderaadt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23246301a8Sderaadt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24246301a8Sderaadt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25246301a8Sderaadt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26246301a8Sderaadt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27246301a8Sderaadt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28246301a8Sderaadt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29246301a8Sderaadt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30246301a8Sderaadt  * POSSIBILITY OF SUCH DAMAGE.
31246301a8Sderaadt  */
32246301a8Sderaadt 
33246301a8Sderaadt #include <errno.h>
34246301a8Sderaadt #include <fcntl.h>
35246301a8Sderaadt #include <unistd.h>
36246301a8Sderaadt 
37246301a8Sderaadt int
lockf(int filedes,int function,off_t size)38f723aa39Sderaadt lockf(int filedes, int function, off_t size)
39246301a8Sderaadt {
40246301a8Sderaadt 	struct flock fl;
41246301a8Sderaadt 	int cmd;
42246301a8Sderaadt 
43246301a8Sderaadt 	fl.l_start = 0;
44246301a8Sderaadt 	fl.l_len = size;
45246301a8Sderaadt 	fl.l_whence = SEEK_CUR;
46246301a8Sderaadt 
47246301a8Sderaadt 	switch (function) {
48246301a8Sderaadt 	case F_ULOCK:
49246301a8Sderaadt 		cmd = F_SETLK;
50246301a8Sderaadt 		fl.l_type = F_UNLCK;
51246301a8Sderaadt 		break;
52246301a8Sderaadt 	case F_LOCK:
53246301a8Sderaadt 		cmd = F_SETLKW;
54246301a8Sderaadt 		fl.l_type = F_WRLCK;
55246301a8Sderaadt 		break;
56246301a8Sderaadt 	case F_TLOCK:
57246301a8Sderaadt 		cmd = F_SETLK;
58246301a8Sderaadt 		fl.l_type = F_WRLCK;
59246301a8Sderaadt 		break;
60246301a8Sderaadt 	case F_TEST:
61246301a8Sderaadt 		fl.l_type = F_WRLCK;
62246301a8Sderaadt 		if (fcntl(filedes, F_GETLK, &fl) == -1)
63246301a8Sderaadt 			return (-1);
64246301a8Sderaadt 		if (fl.l_type == F_UNLCK || fl.l_pid == getpid())
65246301a8Sderaadt 			return (0);
66246301a8Sderaadt 		errno = EAGAIN;
67246301a8Sderaadt 		return (-1);
68246301a8Sderaadt 		/* NOTREACHED */
69246301a8Sderaadt 	default:
70246301a8Sderaadt 		errno = EINVAL;
71246301a8Sderaadt 		return (-1);
72246301a8Sderaadt 		/* NOTREACHED */
73246301a8Sderaadt 	}
74246301a8Sderaadt 
75246301a8Sderaadt 	return (fcntl(filedes, cmd, &fl));
76246301a8Sderaadt }
77