xref: /freebsd/lib/libc/sys/fcntl.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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 __weak_reference(__fcntl_compat, fcntl);
38 
39 int
40 __fcntl_compat(int fd, int cmd, ...)
41 {
42 	va_list args;
43 	long arg;
44 	struct __oflock ofl;
45 	struct flock *flp;
46 	int res;
47 
48 	va_start(args, cmd);
49 	arg = va_arg(args, long);
50 	va_end(args);
51 
52 	if (__getosreldate() >= 800028) {
53 		return (__sys_fcntl(fd, cmd, arg));
54 	} else {
55 		if (cmd == F_GETLK || cmd == F_SETLK || cmd == F_SETLKW) {
56 			/*
57 			 * Convert new-style struct flock (which
58 			 * includes l_sysid) to old-style.
59 			 */
60 			flp = (struct flock *) (uintptr_t) arg;
61 			ofl.l_start = flp->l_start;
62 			ofl.l_len = flp->l_len;
63 			ofl.l_pid = flp->l_pid;
64 			ofl.l_type = flp->l_type;
65 			ofl.l_whence = flp->l_whence;
66 
67 			switch (cmd) {
68 			case F_GETLK:
69 				res = __sys_fcntl(fd, F_OGETLK, &ofl);
70 				if (res >= 0) {
71 					flp->l_start = ofl.l_start;
72 					flp->l_len = ofl.l_len;
73 					flp->l_pid = ofl.l_pid;
74 					flp->l_type = ofl.l_type;
75 					flp->l_whence = ofl.l_whence;
76 					flp->l_sysid = 0;
77 				}
78 				return (res);
79 
80 			case F_SETLK:
81 				return (__sys_fcntl(fd, F_OSETLK, &ofl));
82 
83 			case F_SETLKW:
84 				return (__sys_fcntl(fd, F_OSETLKW, &ofl));
85 			}
86 		}
87 		return (__sys_fcntl(fd, cmd, arg));
88 	}
89 }
90