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>
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
;
72 uint32_t auditing
= AU_AUDITING();
75 switch (filemode
& (FEXEC
|FSEARCH
|FREAD
|FWRITE
)) {
78 if (filemode
& (FAPPEND
|FCREAT
|FTRUNC
|FXATTR
|FXATTRDIROPEN
))
79 return (set_errno(EINVAL
));
86 return (set_errno(EINVAL
));
90 * O_CREAT|O_DIRECTORY is a valid corner-case, because O_CREAT is a
91 * no-op if the file exists; however, since O_DIRECTORY will never
92 * create files, O_EXCL in addition is an invalid combination.
94 if ((filemode
& (FCREAT
|FDIRECTORY
|FEXCL
)) == (FCREAT
|FDIRECTORY
|FEXCL
))
95 return (set_errno(EINVAL
));
97 if (startfd
== AT_FDCWD
) {
104 * We're here via openat()
106 if (copyin(fname
, &startchar
, sizeof (char)))
107 return (set_errno(EFAULT
));
110 * if startchar is / then startfd is ignored
112 if (startchar
== '/')
115 if ((startfp
= getf(startfd
)) == NULL
)
116 return (set_errno(EBADF
));
117 startvp
= startfp
->f_vnode
;
124 * Handle __openattrdirat() requests
126 if (filemode
& FXATTRDIROPEN
) {
127 if (auditing
&& startvp
!= NULL
)
128 audit_setfsat_path(1);
129 if (error
= lookupnameat(fname
, seg
, FOLLOW
,
130 NULLVPP
, &vp
, startvp
))
131 return (set_errno(error
));
139 * Do we need to go into extended attribute space?
141 if (filemode
& FXATTR
) {
142 if (startfd
== AT_FDCWD
) {
143 if (copyin(fname
, &startchar
, sizeof (char)))
144 return (set_errno(EFAULT
));
147 * If startchar == '/' then no extended attributes
150 if (startchar
== '/') {
153 mutex_enter(&p
->p_lock
);
154 startvp
= PTOU(p
)->u_cdir
;
156 mutex_exit(&p
->p_lock
);
161 * Make sure we have a valid extended attribute request.
162 * We must either have a real fd or AT_FDCWD and a relative
165 if (startvp
== NULL
) {
170 if (filemode
& (FXATTR
|FXATTRDIROPEN
)) {
173 if (error
= pn_get(fname
, UIO_USERSPACE
, &pn
)) {
178 * In order to access hidden attribute directory the
179 * user must be able to stat() the file
181 vattr
.va_mask
= AT_ALL
;
182 if (error
= fop_getattr(startvp
, &vattr
, 0, CRED(), NULL
)) {
187 if ((startvp
->v_vfsp
->vfs_flag
& VFS_XATTR
) != 0 ||
188 vfs_has_feature(startvp
->v_vfsp
, VFSFT_SYSATTR_VIEWS
)) {
189 error
= fop_lookup(startvp
, "", &sdvp
, &pn
,
190 (filemode
& FXATTRDIROPEN
) ? LOOKUP_XATTR
:
191 LOOKUP_XATTR
|CREATE_XATTR_DIR
, rootvp
, CRED(),
198 * For __openattrdirat() use "." as filename to open
199 * as part of vn_openat()
201 if (error
== 0 && (filemode
& FXATTRDIROPEN
)) {
215 if ((filemode
& (FREAD
|FWRITE
|FSEARCH
|FEXEC
|FXATTRDIROPEN
)) != 0) {
216 if ((filemode
& (FNONBLOCK
|FNDELAY
)) == (FNONBLOCK
|FNDELAY
))
217 filemode
&= ~FNDELAY
;
218 error
= falloc(NULL
, filemode
, &fp
, &fd
);
220 if (auditing
&& startvp
!= NULL
)
221 audit_setfsat_path(1);
223 * Last arg is a don't-care term if
224 * !(filemode & FCREAT).
226 error
= vn_openat(open_filename
, seg
, filemode
,
227 (int)(createmode
& MODEMASK
),
228 &vp
, CRCREAT
, PTOU(curproc
)->u_cmask
,
234 if ((vp
->v_flag
& VDUP
) == 0) {
236 mutex_exit(&fp
->f_tlock
);
238 * We must now fill in the slot
242 if ((filemode
& FCLOEXEC
) != 0) {
243 f_setfd(fd
, FD_CLOEXEC
);
248 * Special handling for /dev/fd.
249 * Give up the file pointer
250 * and dup the indicated file descriptor
251 * (in v_rdev). This is ugly, but I've
255 dupfd
= getminor(vp
->v_rdev
);
257 mutex_enter(&vp
->v_lock
);
259 mutex_exit(&vp
->v_lock
);
262 return (set_errno(EINVAL
));
263 if ((fp
= getf(dupfd
)) == NULL
) {
265 return (set_errno(EBADF
));
267 mutex_enter(&fp
->f_tlock
);
269 mutex_exit(&fp
->f_tlock
);
271 if ((filemode
& FCLOEXEC
) != 0) {
272 f_setfd(fd
, FD_CLOEXEC
);
280 return (set_errno(error
));
289 return (set_errno(error
));
296 openat(int fd
, char *path
, int omode
, int cmode
)
298 return (copen(fd
, path
, FFLAGS(omode
) |
307 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
309 * Open for large files in 32-bit environment. Sets the FOFFMAX flag.
312 openat64(int fd
, char *path
, int omode
, int cmode
)
314 return (copen(fd
, path
, FFLAGS(omode
) | FOFFMAX
, cmode
));
316 #endif /* _ILP32 || _SYSCALL32_IMPL */
318 #ifdef _SYSCALL32_IMPL
320 * Open for 32-bit compatibility on 64-bit kernel
323 openat32(int fd
, char *path
, int omode
, int cmode
)
325 return (copen(fd
, path
, FFLAGS(omode
), cmode
));
327 #endif /* _SYSCALL32_IMPL */