Whamcloud - gitweb
import libsysio for b_newsysio
[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
59 static int
60 _sysio_fcntl(int fd, int cmd, va_list ap, int *rtn)
61 {
62         long arg = va_arg(ap, long);
63
64         *rtn = syscall(SYS_fcntl, fd, cmd, arg);
65         return *rtn == -1 ? -errno : 0;
66 }
67 #endif
68
69 int
70 SYSIO_INTERFACE_NAME(fcntl)(int fd, int cmd, ...)
71 {
72         int     err;
73         int     rtn;
74         struct file *fil;
75         va_list ap;
76         SYSIO_INTERFACE_DISPLAY_BLOCK;
77
78         SYSIO_INTERFACE_ENTER;
79         err = 0;
80         fil = _sysio_fd_find(fd);
81         if (!fil) {
82 #ifdef HAVE_LUSTRE_HACK
83                 va_start(ap, cmd);
84                 err = _sysio_fcntl(fd, cmd, ap, &rtn);
85                 va_end(ap);
86                 goto out;
87 #else
88
89                 rtn = -1;
90                 err = -EBADF;
91                 goto out;
92 #endif
93         }
94
95         switch (cmd) {
96
97             case F_DUPFD:
98                 {
99                         long    newfd;
100
101                         va_start(ap, cmd);
102                         newfd = va_arg(ap, long);
103                         va_end(ap);
104                         if (newfd != (int )newfd || newfd < 0) {
105                                 rtn = -1;
106                                 err = -EBADF;
107                                 goto out;
108                         }
109                         rtn = _sysio_fd_dup(fd, (int )newfd, 0);
110                         if (rtn < 0) {
111                                 err = rtn;
112                                 rtn = -1;
113                         }
114                 }
115                 break;
116             default:
117                 va_start(ap, cmd);
118                 err = fil->f_ino->i_ops.inop_fcntl(fil->f_ino, cmd, ap, &rtn);
119                 va_end(ap);
120                 break;
121         }
122
123 out:
124         SYSIO_INTERFACE_RETURN(rtn, err);
125 }
126
127 #ifdef __GLIBC__
128 #undef __fcntl
129 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(fcntl), 
130                      PREPEND(__, SYSIO_INTERFACE_NAME(fcntl)))
131 #endif
132
133 #ifdef BSD
134 #undef _fcntl
135 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(fcntl), 
136                      PREPEND(_, SYSIO_INTERFACE_NAME(fcntl)))
137 #endif