fm/ipmitopo: fix 64-bit compilation
[unleashed.git] / kernel / syscall / open.c
blobadbce3612deac039208854301378613e66a1b242
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 <sys/pathname.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 char startchar;
74 switch (filemode & (FEXEC|FSEARCH|FREAD|FWRITE)) {
75 case FEXEC:
76 case FSEARCH:
77 if (filemode & (FAPPEND|FCREAT|FTRUNC|FXATTR|FXATTRDIROPEN))
78 return (set_errno(EINVAL));
79 break;
80 case FREAD:
81 case FWRITE:
82 case (FREAD|FWRITE):
83 break;
84 default:
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) {
98 * Regular open()
100 startvp = NULL;
101 } else {
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 == '/')
112 startvp = NULL;
113 else {
114 if ((startfp = getf(startfd)) == NULL)
115 return (set_errno(EBADF));
116 startvp = startfp->f_vnode;
117 VN_HOLD(startvp);
118 releasef(startfd);
123 * Handle __openattrdirat() requests
125 if (filemode & FXATTRDIROPEN) {
126 if (error = lookupnameat(fname, seg, FOLLOW,
127 NULLVPP, &vp, startvp))
128 return (set_errno(error));
129 if (startvp != NULL)
130 VN_RELE(startvp);
132 startvp = vp;
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
145 * are looked up.
147 if (startchar == '/') {
148 startvp = NULL;
149 } else {
150 mutex_enter(&p->p_lock);
151 startvp = PTOU(p)->u_cdir;
152 VN_HOLD(startvp);
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
160 * pathname.
162 if (startvp == NULL) {
163 goto noxattr;
167 if (filemode & (FXATTR|FXATTRDIROPEN)) {
168 vattr_t vattr;
170 if (error = pn_get(fname, UIO_USERSPACE, &pn)) {
171 goto out;
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)) {
180 pn_free(&pn);
181 goto out;
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(),
189 NULL, NULL, NULL);
190 } else {
191 error = EINVAL;
195 * For __openattrdirat() use "." as filename to open
196 * as part of vn_openat()
198 if (error == 0 && (filemode & FXATTRDIROPEN)) {
199 open_filename = ".";
200 seg = UIO_SYSSPACE;
203 pn_free(&pn);
204 if (error != 0)
205 goto out;
207 VN_RELE(startvp);
208 startvp = sdvp;
211 noxattr:
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);
216 if (error == 0) {
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,
224 startvp, fd);
226 if (startvp != NULL)
227 VN_RELE(startvp);
228 if (error == 0) {
229 if ((vp->v_flag & VDUP) == 0) {
230 fp->f_vnode = vp;
231 mutex_exit(&fp->f_tlock);
233 * We must now fill in the slot
234 * falloc reserved.
236 setf(fd, fp);
237 if ((filemode & FCLOEXEC) != 0) {
238 f_setfd(fd, FD_CLOEXEC);
240 return (fd);
241 } else {
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
247 * seen worse.
249 unfalloc(fp);
250 dupfd = getminor(vp->v_rdev);
251 type = vp->v_type;
252 mutex_enter(&vp->v_lock);
253 vp->v_flag &= ~VDUP;
254 mutex_exit(&vp->v_lock);
255 VN_RELE(vp);
256 if (type != VCHR)
257 return (set_errno(EINVAL));
258 if ((fp = getf(dupfd)) == NULL) {
259 setf(fd, NULL);
260 return (set_errno(EBADF));
262 mutex_enter(&fp->f_tlock);
263 fp->f_count++;
264 mutex_exit(&fp->f_tlock);
265 setf(fd, fp);
266 if ((filemode & FCLOEXEC) != 0) {
267 f_setfd(fd, FD_CLOEXEC);
269 releasef(dupfd);
271 return (fd);
272 } else {
273 setf(fd, NULL);
274 unfalloc(fp);
275 return (set_errno(error));
278 } else {
279 error = EINVAL;
281 out:
282 if (startvp != NULL)
283 VN_RELE(startvp);
284 return (set_errno(error));
288 * Open a file.
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 */