4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
33 * Portions of this source code were derived from Berkeley 4.3 BSD
34 * under license from the Regents of the University of California.
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/fcntl.h>
46 #include <sys/vnode.h>
51 #include <sys/debug.h>
52 #include <sys/pathname.h>
55 * Common code for openat(). Check permissions, allocate an open
56 * file structure, and call the device open routine (if any).
60 copen(int startfd
, char *fname
, int filemode
, int createmode
)
70 uio_seg_t seg
= UIO_USERSPACE
;
71 char *open_filename
= fname
;
74 switch (filemode
& (FEXEC
|FSEARCH
|FREAD
|FWRITE
)) {
77 if (filemode
& (FAPPEND
|FCREAT
|FTRUNC
|FXATTR
|FXATTRDIROPEN
))
78 return (set_errno(EINVAL
));
85 return (set_errno(EINVAL
));
89 * O_CREAT|O_DIRECTORY is a valid corner-case, because O_CREAT is a
90 * no-op if the file exists; however, since O_DIRECTORY will never
91 * create files, O_EXCL in addition is an invalid combination.
93 if ((filemode
& (FCREAT
|FDIRECTORY
|FEXCL
)) == (FCREAT
|FDIRECTORY
|FEXCL
))
94 return (set_errno(EINVAL
));
96 if (startfd
== AT_FDCWD
) {
103 * We're here via openat()
105 if (copyin(fname
, &startchar
, sizeof (char)))
106 return (set_errno(EFAULT
));
109 * if startchar is / then startfd is ignored
111 if (startchar
== '/')
114 if ((startfp
= getf(startfd
)) == NULL
)
115 return (set_errno(EBADF
));
116 startvp
= startfp
->f_vnode
;
123 * Handle __openattrdirat() requests
125 if (filemode
& FXATTRDIROPEN
) {
126 if (error
= lookupnameat(fname
, seg
, FOLLOW
,
127 NULLVPP
, &vp
, startvp
))
128 return (set_errno(error
));
136 * Do we need to go into extended attribute space?
138 if (filemode
& FXATTR
) {
139 if (startfd
== AT_FDCWD
) {
140 if (copyin(fname
, &startchar
, sizeof (char)))
141 return (set_errno(EFAULT
));
144 * If startchar == '/' then no extended attributes
147 if (startchar
== '/') {
150 mutex_enter(&p
->p_lock
);
151 startvp
= PTOU(p
)->u_cdir
;
153 mutex_exit(&p
->p_lock
);
158 * Make sure we have a valid extended attribute request.
159 * We must either have a real fd or AT_FDCWD and a relative
162 if (startvp
== NULL
) {
167 if (filemode
& (FXATTR
|FXATTRDIROPEN
)) {
170 if (error
= pn_get(fname
, UIO_USERSPACE
, &pn
)) {
175 * In order to access hidden attribute directory the
176 * user must be able to stat() the file
178 vattr
.va_mask
= VATTR_ALL
;
179 if (error
= fop_getattr(startvp
, &vattr
, 0, CRED(), NULL
)) {
184 if ((startvp
->v_vfsp
->vfs_flag
& VFS_XATTR
) != 0 ||
185 vfs_has_feature(startvp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
)) {
186 error
= fop_lookup(startvp
, "", &sdvp
, &pn
,
187 (filemode
& FXATTRDIROPEN
) ? LOOKUP_XATTR
:
188 LOOKUP_XATTR
|CREATE_XATTR_DIR
, rootvp
, CRED(),
195 * For __openattrdirat() use "." as filename to open
196 * as part of vn_openat()
198 if (error
== 0 && (filemode
& FXATTRDIROPEN
)) {
212 if ((filemode
& (FREAD
|FWRITE
|FSEARCH
|FEXEC
|FXATTRDIROPEN
)) != 0) {
213 if ((filemode
& (FNONBLOCK
|FNDELAY
)) == (FNONBLOCK
|FNDELAY
))
214 filemode
&= ~FNDELAY
;
215 error
= falloc(NULL
, filemode
, &fp
, &fd
);
218 * Last arg is a don't-care term if
219 * !(filemode & FCREAT).
221 error
= vn_openat(open_filename
, seg
, filemode
,
222 (int)(createmode
& MODEMASK
),
223 &vp
, CRCREAT
, PTOU(curproc
)->u_cmask
,
229 if ((vp
->v_flag
& VDUP
) == 0) {
231 mutex_exit(&fp
->f_tlock
);
233 * We must now fill in the slot
237 if ((filemode
& FCLOEXEC
) != 0) {
238 f_setfd(fd
, FD_CLOEXEC
);
243 * Special handling for /dev/fd.
244 * Give up the file pointer
245 * and dup the indicated file descriptor
246 * (in v_rdev). This is ugly, but I've
250 dupfd
= getminor(vp
->v_rdev
);
252 mutex_enter(&vp
->v_lock
);
254 mutex_exit(&vp
->v_lock
);
257 return (set_errno(EINVAL
));
258 if ((fp
= getf(dupfd
)) == NULL
) {
260 return (set_errno(EBADF
));
262 mutex_enter(&fp
->f_tlock
);
264 mutex_exit(&fp
->f_tlock
);
266 if ((filemode
& FCLOEXEC
) != 0) {
267 f_setfd(fd
, FD_CLOEXEC
);
275 return (set_errno(error
));
284 return (set_errno(error
));
291 openat(int fd
, char *path
, int omode
, int cmode
)
293 return (copen(fd
, path
, FFLAGS(omode
), cmode
));
296 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
298 openat64(int fd
, char *path
, int omode
, int cmode
)
300 return (copen(fd
, path
, FFLAGS(omode
), cmode
));
302 #endif /* _ILP32 || _SYSCALL32_IMPL */