1*9e66e6d7Sabs /* $NetBSD: lockf.c,v 1.4 2012/06/25 22:32:43 abs Exp $ */
2916831ecSkleink
3916831ecSkleink /*-
4916831ecSkleink * Copyright (c) 1997 The NetBSD Foundation, Inc.
5916831ecSkleink * All rights reserved.
6916831ecSkleink *
7916831ecSkleink * This code is derived from software contributed to The NetBSD Foundation
8916831ecSkleink * by Klaus Klein.
9916831ecSkleink *
10916831ecSkleink * Redistribution and use in source and binary forms, with or without
11916831ecSkleink * modification, are permitted provided that the following conditions
12916831ecSkleink * are met:
13916831ecSkleink * 1. Redistributions of source code must retain the above copyright
14916831ecSkleink * notice, this list of conditions and the following disclaimer.
15916831ecSkleink * 2. Redistributions in binary form must reproduce the above copyright
16916831ecSkleink * notice, this list of conditions and the following disclaimer in the
17916831ecSkleink * documentation and/or other materials provided with the distribution.
18916831ecSkleink *
19916831ecSkleink * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20916831ecSkleink * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21916831ecSkleink * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22916831ecSkleink * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23916831ecSkleink * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24916831ecSkleink * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25916831ecSkleink * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26916831ecSkleink * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27916831ecSkleink * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28916831ecSkleink * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29916831ecSkleink * POSSIBILITY OF SUCH DAMAGE.
30916831ecSkleink */
31916831ecSkleink
32916831ecSkleink #include <sys/cdefs.h>
33916831ecSkleink #if defined(LIBC_SCCS) && !defined(lint)
34*9e66e6d7Sabs __RCSID("$NetBSD: lockf.c,v 1.4 2012/06/25 22:32:43 abs Exp $");
35916831ecSkleink #endif
36916831ecSkleink
37916831ecSkleink #include "namespace.h"
38916831ecSkleink #include <errno.h>
39916831ecSkleink #include <fcntl.h>
40916831ecSkleink #include <unistd.h>
41916831ecSkleink
42916831ecSkleink #ifdef __weak_alias
__weak_alias(lockf,_lockf)4360549036Smycroft __weak_alias(lockf,_lockf)
44916831ecSkleink #endif
45916831ecSkleink
46916831ecSkleink
47916831ecSkleink int
48*9e66e6d7Sabs lockf(int filedes, int function, off_t size)
49916831ecSkleink {
50916831ecSkleink struct flock fl;
51916831ecSkleink int cmd;
52916831ecSkleink
53916831ecSkleink fl.l_start = 0;
54916831ecSkleink fl.l_len = size;
55916831ecSkleink fl.l_whence = SEEK_CUR;
56916831ecSkleink
57916831ecSkleink switch (function) {
58916831ecSkleink case F_ULOCK:
59916831ecSkleink cmd = F_SETLK;
60916831ecSkleink fl.l_type = F_UNLCK;
61916831ecSkleink break;
62916831ecSkleink case F_LOCK:
63916831ecSkleink cmd = F_SETLKW;
64916831ecSkleink fl.l_type = F_WRLCK;
65916831ecSkleink break;
66916831ecSkleink case F_TLOCK:
67916831ecSkleink cmd = F_SETLK;
68916831ecSkleink fl.l_type = F_WRLCK;
69916831ecSkleink break;
70916831ecSkleink case F_TEST:
71916831ecSkleink fl.l_type = F_WRLCK;
72916831ecSkleink if (fcntl(filedes, F_GETLK, &fl) == -1)
73916831ecSkleink return (-1);
74916831ecSkleink if (fl.l_type == F_UNLCK || fl.l_pid == getpid())
75916831ecSkleink return (0);
76916831ecSkleink errno = EAGAIN;
77916831ecSkleink return (-1);
78916831ecSkleink /* NOTREACHED */
79916831ecSkleink default:
80916831ecSkleink errno = EINVAL;
81916831ecSkleink return (-1);
82916831ecSkleink /* NOTREACHED */
83916831ecSkleink }
84916831ecSkleink
85916831ecSkleink return (fcntl(filedes, cmd, &fl));
86916831ecSkleink }
87