1/*-
2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <fcntl.h>
32#include <stdarg.h>
33#include <sys/types.h>
34#include <sys/syscall.h>
35#include "libc_private.h"
36
37#pragma weak fcntl
38int
39fcntl(int fd, int cmd, ...)
40{
41	va_list args;
42	long arg;
43
44	va_start(args, cmd);
45	arg = va_arg(args, long);
46	va_end(args);
47
48	return (((int (*)(int, int, ...))
49	    __libc_interposing[INTERPOS_fcntl])(fd, cmd, arg));
50}
51
52#ifdef SYSCALL_COMPAT
53__weak_reference(__fcntl_compat, __fcntl);
54
55int
56__fcntl_compat(int fd, int cmd, ...)
57{
58	va_list args;
59	long arg;
60	struct __oflock ofl;
61	struct flock *flp;
62	int res;
63
64	va_start(args, cmd);
65	arg = va_arg(args, long);
66	va_end(args);
67
68	if (__getosreldate() >= 800028) {
69		return (__sys_fcntl(fd, cmd, arg));
70	} else {
71		if (cmd == F_GETLK || cmd == F_SETLK || cmd == F_SETLKW) {
72			/*
73			 * Convert new-style struct flock (which
74			 * includes l_sysid) to old-style.
75			 */
76			flp = (struct flock *) (uintptr_t) arg;
77			ofl.l_start = flp->l_start;
78			ofl.l_len = flp->l_len;
79			ofl.l_pid = flp->l_pid;
80			ofl.l_type = flp->l_type;
81			ofl.l_whence = flp->l_whence;
82
83			switch (cmd) {
84			case F_GETLK:
85				res = __sys_fcntl(fd, F_OGETLK, &ofl);
86				if (res >= 0) {
87					flp->l_start = ofl.l_start;
88					flp->l_len = ofl.l_len;
89					flp->l_pid = ofl.l_pid;
90					flp->l_type = ofl.l_type;
91					flp->l_whence = ofl.l_whence;
92					flp->l_sysid = 0;
93				}
94				return (res);
95
96			case F_SETLK:
97				return (__sys_fcntl(fd, F_OSETLK, &ofl));
98
99			case F_SETLKW:
100				return (__sys_fcntl(fd, F_OSETLKW, &ofl));
101			}
102		}
103		return (__sys_fcntl(fd, cmd, arg));
104	}
105}
106#endif
107