Whamcloud - gitweb
22e9382d780cfea33854dad34fadd029d60459f2
[fs/lustre-release.git] / libsysio / src / fcntl.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2003 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #include <unistd.h>
45 #include <errno.h>
46 #include <sys/types.h>
47 #include <fcntl.h>
48 #include <sys/queue.h>
49
50 #include "sysio.h"
51 #include "inode.h"
52 #include "file.h"
53
54 #include "sysio-symbols.h"
55
56 #ifdef HAVE_LUSTRE_HACK
57 #include <syscall.h>
58 #include <native.h>
59
60 static int
61 _sysio_fcntl(int fd, int cmd, va_list ap, int *rtn)
62 {
63         long arg = va_arg(ap, long);
64
65         *rtn = syscall(SYSIO_SYS_fcntl, fd, cmd, arg);
66         return *rtn == -1 ? -errno : 0;
67 }
68 #endif
69
70 int
71 SYSIO_INTERFACE_NAME(fcntl)(int fd, int cmd, ...)
72 {
73         int     err;
74         int     rtn;
75         struct file *fil;
76         va_list ap;
77         SYSIO_INTERFACE_DISPLAY_BLOCK;
78
79         SYSIO_INTERFACE_ENTER;
80         err = 0;
81         fil = _sysio_fd_find(fd);
82         if (!fil) {
83 #ifdef HAVE_LUSTRE_HACK
84                 va_start(ap, cmd);
85                 err = _sysio_fcntl(fd, cmd, ap, &rtn);
86                 va_end(ap);
87                 goto out;
88 #else
89
90                 rtn = -1;
91                 err = -EBADF;
92                 goto out;
93 #endif
94         }
95
96         switch (cmd) {
97
98             case F_DUPFD:
99                 {
100                         long    newfd;
101
102                         va_start(ap, cmd);
103                         newfd = va_arg(ap, long);
104                         va_end(ap);
105                         if (newfd != (int )newfd || newfd < 0) {
106                                 rtn = -1;
107                                 err = -EBADF;
108                                 goto out;
109                         }
110                         rtn = _sysio_fd_dup(fd, (int )newfd, 0);
111                         if (rtn < 0) {
112                                 err = rtn;
113                                 rtn = -1;
114                         }
115                 }
116                 break;
117             default:
118                 va_start(ap, cmd);
119                 err = fil->f_ino->i_ops.inop_fcntl(fil->f_ino, cmd, ap, &rtn);
120                 va_end(ap);
121                 break;
122         }
123
124 out:
125         SYSIO_INTERFACE_RETURN(rtn, err);
126 }
127
128 #ifdef __GLIBC__
129 #undef __fcntl
130 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(fcntl), 
131                      PREPEND(__, SYSIO_INTERFACE_NAME(fcntl)))
132 #endif
133
134 #ifdef BSD
135 #undef _fcntl
136 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(fcntl), 
137                      PREPEND(_, SYSIO_INTERFACE_NAME(fcntl)))
138 #endif