dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / kernel / syscall / open.c
blob139d46ab3849f4d58246698f1d4eb199bff83246
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
41 #include <sys/user.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/fcntl.h>
45 #include <sys/stat.h>
46 #include <sys/vnode.h>
47 #include <sys/vfs.h>
48 #include <sys/file.h>
49 #include <sys/mode.h>
50 #include <sys/uio.h>
51 #include <sys/debug.h>
52 #include <c2/audit.h>
55 * Common code for openat(). Check permissions, allocate an open
56 * file structure, and call the device open routine (if any).
59 static int
60 copen(int startfd, char *fname, int filemode, int createmode)
62 struct pathname pn;
63 vnode_t *vp, *sdvp;
64 file_t *fp, *startfp;
65 enum vtype type;
66 int error;
67 int fd, dupfd;
68 vnode_t *startvp;
69 proc_t *p = curproc;
70 uio_seg_t seg = UIO_USERSPACE;
71 char *open_filename = fname;
72 uint32_t auditing = AU_AUDITING();
73 char startchar;
75 switch (filemode & (FEXEC|FSEARCH|FREAD|FWRITE)) {
76 case FEXEC:
77 case FSEARCH:
78 if (filemode & (FAPPEND|FCREAT|FTRUNC|FXATTR|FXATTRDIROPEN))
79 return (set_errno(EINVAL));
80 break;
81 case FREAD:
82 case FWRITE:
83 case (FREAD|FWRITE):
84 break;
85 default:
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) {
99 * Regular open()
101 startvp = NULL;
102 } else {
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 == '/')
113 startvp = NULL;
114 else {
115 if ((startfp = getf(startfd)) == NULL)
116 return (set_errno(EBADF));
117 startvp = startfp->f_vnode;
118 VN_HOLD(startvp);
119 releasef(startfd);
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));
132 if (startvp != NULL)
133 VN_RELE(startvp);
135 startvp = vp;
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
148 * are looked up.
150 if (startchar == '/') {
151 startvp = NULL;
152 } else {
153 mutex_enter(&p->p_lock);
154 startvp = PTOU(p)->u_cdir;
155 VN_HOLD(startvp);
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
163 * pathname.
165 if (startvp == NULL) {
166 goto noxattr;
170 if (filemode & (FXATTR|FXATTRDIROPEN)) {
171 vattr_t vattr;
173 if (error = pn_get(fname, UIO_USERSPACE, &pn)) {
174 goto out;
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)) {
183 pn_free(&pn);
184 goto out;
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(),
192 NULL, NULL, NULL);
193 } else {
194 error = EINVAL;
198 * For __openattrdirat() use "." as filename to open
199 * as part of vn_openat()
201 if (error == 0 && (filemode & FXATTRDIROPEN)) {
202 open_filename = ".";
203 seg = UIO_SYSSPACE;
206 pn_free(&pn);
207 if (error != 0)
208 goto out;
210 VN_RELE(startvp);
211 startvp = sdvp;
214 noxattr:
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);
219 if (error == 0) {
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,
229 startvp, fd);
231 if (startvp != NULL)
232 VN_RELE(startvp);
233 if (error == 0) {
234 if ((vp->v_flag & VDUP) == 0) {
235 fp->f_vnode = vp;
236 mutex_exit(&fp->f_tlock);
238 * We must now fill in the slot
239 * falloc reserved.
241 setf(fd, fp);
242 if ((filemode & FCLOEXEC) != 0) {
243 f_setfd(fd, FD_CLOEXEC);
245 return (fd);
246 } else {
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
252 * seen worse.
254 unfalloc(fp);
255 dupfd = getminor(vp->v_rdev);
256 type = vp->v_type;
257 mutex_enter(&vp->v_lock);
258 vp->v_flag &= ~VDUP;
259 mutex_exit(&vp->v_lock);
260 VN_RELE(vp);
261 if (type != VCHR)
262 return (set_errno(EINVAL));
263 if ((fp = getf(dupfd)) == NULL) {
264 setf(fd, NULL);
265 return (set_errno(EBADF));
267 mutex_enter(&fp->f_tlock);
268 fp->f_count++;
269 mutex_exit(&fp->f_tlock);
270 setf(fd, fp);
271 if ((filemode & FCLOEXEC) != 0) {
272 f_setfd(fd, FD_CLOEXEC);
274 releasef(dupfd);
276 return (fd);
277 } else {
278 setf(fd, NULL);
279 unfalloc(fp);
280 return (set_errno(error));
283 } else {
284 error = EINVAL;
286 out:
287 if (startvp != NULL)
288 VN_RELE(startvp);
289 return (set_errno(error));
293 * Open a file.
296 openat(int fd, char *path, int omode, int cmode)
298 return (copen(fd, path, FFLAGS(omode) |
299 #if defined(_LP64)
300 FOFFMAX,
301 #else
303 #endif
304 cmode));
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 */