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