fcntl.c revision 179434
1177911Sdfr/*-
2177911Sdfr * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3177911Sdfr * Authors: Doug Rabson <dfr@rabson.org>
4177911Sdfr * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
5177911Sdfr *
6177911Sdfr * Redistribution and use in source and binary forms, with or without
7177911Sdfr * modification, are permitted provided that the following conditions
8177911Sdfr * are met:
9177911Sdfr * 1. Redistributions of source code must retain the above copyright
10177911Sdfr *    notice, this list of conditions and the following disclaimer.
11177911Sdfr * 2. Redistributions in binary form must reproduce the above copyright
12177911Sdfr *    notice, this list of conditions and the following disclaimer in the
13177911Sdfr *    documentation and/or other materials provided with the distribution.
14177911Sdfr *
15177911Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16177911Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17177911Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18177911Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19177911Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20177911Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21177911Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22177911Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23177911Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24177911Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25177911Sdfr * SUCH DAMAGE.
26177911Sdfr */
27177911Sdfr
28177911Sdfr#include <sys/cdefs.h>
29177911Sdfr__FBSDID("$FreeBSD: head/lib/libc/sys/fcntl.c 179434 2008-05-30 14:47:42Z dfr $");
30177911Sdfr
31177911Sdfr#include <fcntl.h>
32177911Sdfr#include <stdarg.h>
33177911Sdfr#include <sys/types.h>
34177911Sdfr#include <sys/syscall.h>
35177911Sdfr#include "libc_private.h"
36177911Sdfr
37179434Sdfr__weak_reference(__fcntl_compat, fcntl);
38179358Sdfr
39177911Sdfrint
40179434Sdfr__fcntl_compat(int fd, int cmd, ...)
41177911Sdfr{
42177911Sdfr	va_list args;
43177911Sdfr	long arg;
44177911Sdfr	struct oflock ofl;
45177911Sdfr	struct flock *flp;
46177911Sdfr	int res;
47177911Sdfr
48177911Sdfr	va_start(args, cmd);
49177911Sdfr	arg = va_arg(args, long);
50177911Sdfr	va_end(args);
51177911Sdfr
52177911Sdfr	if (__getosreldate() >= 800028) {
53177911Sdfr		return (__sys_fcntl(fd, cmd, arg));
54177911Sdfr	} else {
55177911Sdfr		if (cmd == F_GETLK || cmd == F_SETLK || cmd == F_SETLKW) {
56177911Sdfr			/*
57177911Sdfr			 * Convert new-style struct flock (which
58177911Sdfr			 * includes l_sysid) to old-style.
59177911Sdfr			 */
60177911Sdfr			flp = (struct flock *) (uintptr_t) arg;
61177911Sdfr			ofl.l_start = flp->l_start;
62177911Sdfr			ofl.l_len = flp->l_len;
63177911Sdfr			ofl.l_pid = flp->l_pid;
64177911Sdfr			ofl.l_type = flp->l_type;
65177911Sdfr			ofl.l_whence = flp->l_whence;
66177911Sdfr
67177911Sdfr			switch (cmd) {
68177911Sdfr			case F_GETLK:
69177911Sdfr				res = __sys_fcntl(fd, F_OGETLK, &ofl);
70177911Sdfr				if (res >= 0) {
71177911Sdfr					flp->l_start = ofl.l_start;
72177911Sdfr					flp->l_len = ofl.l_len;
73177911Sdfr					flp->l_pid = ofl.l_pid;
74177911Sdfr					flp->l_type = ofl.l_type;
75177911Sdfr					flp->l_whence = ofl.l_whence;
76177911Sdfr					flp->l_sysid = 0;
77177911Sdfr				}
78177911Sdfr				return (res);
79177911Sdfr
80177911Sdfr			case F_SETLK:
81177911Sdfr				return (__sys_fcntl(fd, F_OSETLK, &ofl));
82177911Sdfr
83177911Sdfr			case F_SETLKW:
84177911Sdfr				return (__sys_fcntl(fd, F_OSETLKW, &ofl));
85177911Sdfr			}
86177911Sdfr		}
87177911Sdfr		return (__sys_fcntl(fd, cmd, arg));
88177911Sdfr	}
89177911Sdfr}
90