]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2019 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1992, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * This code is derived from software donated to Berkeley by | |
34 | * Jan-Simon Pendry. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. All advertising materials mentioning features or use of this software | |
45 | * must display the following acknowledgement: | |
46 | * This product includes software developed by the University of | |
47 | * California, Berkeley and its contributors. | |
48 | * 4. Neither the name of the University nor the names of its contributors | |
49 | * may be used to endorse or promote products derived from this software | |
50 | * without specific prior written permission. | |
51 | * | |
52 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
53 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
55 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
56 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
57 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
58 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
59 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
60 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
61 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
62 | * SUCH DAMAGE. | |
63 | * | |
64 | * @(#)fdesc_vnops.c 8.17 (Berkeley) 5/22/95 | |
65 | * | |
66 | */ | |
67 | ||
68 | /* | |
69 | * /dev/fd Filesystem | |
70 | */ | |
71 | ||
72 | #include <sys/param.h> | |
73 | #include <sys/systm.h> | |
74 | #include <sys/types.h> | |
75 | #include <sys/time.h> | |
91447636 | 76 | #include <sys/proc_internal.h> |
0a7de745 | 77 | #include <sys/kernel.h> /* boottime */ |
1c79356b A |
78 | #include <sys/resourcevar.h> |
79 | #include <sys/filedesc.h> | |
91447636 A |
80 | #include <sys/kauth.h> |
81 | #include <sys/vnode_internal.h> | |
1c79356b | 82 | #include <sys/malloc.h> |
91447636 | 83 | #include <sys/file_internal.h> |
1c79356b | 84 | #include <sys/stat.h> |
91447636 | 85 | #include <sys/mount_internal.h> |
1c79356b | 86 | #include <sys/namei.h> |
1c79356b A |
87 | #include <sys/dirent.h> |
88 | #include <sys/ubc.h> | |
91447636 A |
89 | #include <sys/socketvar.h> |
90 | #include <sys/pipe.h> | |
91 | #include <sys/uio_internal.h> | |
1c79356b | 92 | #include <vfs/vfs_support.h> |
91447636 | 93 | #include <pexpert/pexpert.h> |
b0d623f7 A |
94 | #include <miscfs/devfs/fdesc.h> |
95 | #include <miscfs/devfs/devfs.h> | |
96 | #include <miscfs/devfs/devfsdefs.h> | |
1c79356b | 97 | |
0a7de745 A |
98 | #define FDL_WANT 0x01 |
99 | #define FDL_LOCKED 0x02 | |
1c79356b A |
100 | static int fdcache_lock; |
101 | ||
1c79356b | 102 | |
0a7de745 A |
103 | #if (FD_STDIN != FD_STDOUT - 1) || (FD_STDOUT != FD_STDERR - 1) |
104 | FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n + 1, n + 2 | |
1c79356b A |
105 | #endif |
106 | ||
0a7de745 | 107 | #define NFDCACHE 3 |
1c79356b A |
108 | |
109 | #define FD_NHASH(ix) \ | |
110 | (&fdhashtbl[(ix) & fdhash]) | |
0a7de745 | 111 | LIST_HEAD(fdhashhead, fdescnode) * fdhashtbl; |
1c79356b A |
112 | u_long fdhash; |
113 | ||
91447636 A |
114 | static int fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context); |
115 | ||
c3c9b80d A |
116 | static LCK_GRP_DECLARE(fdesc_lckgrp, "fdesc"); |
117 | static LCK_MTX_DECLARE(fdesc_mtx, &fdesc_lckgrp); | |
b0d623f7 | 118 | |
0a7de745 | 119 | static void |
b0d623f7 A |
120 | fdesc_lock(void) |
121 | { | |
122 | lck_mtx_lock(&fdesc_mtx); | |
123 | } | |
124 | ||
0a7de745 | 125 | static void |
b0d623f7 A |
126 | fdesc_unlock(void) |
127 | { | |
128 | lck_mtx_unlock(&fdesc_mtx); | |
129 | } | |
130 | ||
91447636 | 131 | |
1c79356b | 132 | /* |
b0d623f7 | 133 | * Initialise cache headers, create the devfs node |
1c79356b | 134 | */ |
91447636 | 135 | int |
b0d623f7 | 136 | devfs_fdesc_init() |
1c79356b | 137 | { |
b0d623f7 A |
138 | int error = 0; |
139 | devnode_t *rootdir = dev_root->de_dnp; | |
140 | devdirent_t *direntp; | |
0a7de745 | 141 | |
b0d623f7 | 142 | /* XXX Make sure you have the right path... */ |
1c79356b | 143 | fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash); |
91447636 | 144 | |
b0d623f7 A |
145 | DEVFS_LOCK(); |
146 | dev_add_entry("fd", rootdir, DEV_DEVFD, NULL, NULL, NULL, &direntp); | |
147 | devfs_fdesc_makelinks(); | |
148 | DEVFS_UNLOCK(); | |
149 | ||
0a7de745 | 150 | return error; |
1c79356b A |
151 | } |
152 | ||
b0d623f7 A |
153 | /* |
154 | * Called during early startup, no need to synchronize | |
155 | */ | |
1c79356b | 156 | int |
b0d623f7 A |
157 | devfs_fdesc_makelinks() |
158 | { | |
159 | int error = 0; | |
160 | devdirent_t *stdin_ent = NULL, *stdout_ent = NULL, *stderr_ent = NULL; | |
161 | devnode_t *root_devnode = dev_root->de_dnp; | |
162 | ||
163 | /* We do this ugliness to get around some "const" warnings */ | |
164 | char in[] = "stdin"; | |
165 | char out[] = "stdout"; | |
166 | char err[] = "stderr"; | |
167 | char zero[] = "fd/0"; | |
168 | char one[] = "fd/1"; | |
169 | char two[] = "fd/2"; | |
170 | ||
171 | if ((error = devfs_make_symlink(root_devnode, in, 0555, zero, &stdin_ent))) { | |
172 | printf("Couldn't make stdin, err %d.\n", error); | |
173 | goto bad; | |
174 | } | |
175 | ||
176 | if ((error = devfs_make_symlink(root_devnode, out, 0555, one, &stdout_ent))) { | |
177 | printf("Couldn't make stdout, err %d.\n", error); | |
178 | goto bad; | |
179 | } | |
180 | ||
181 | if ((error = devfs_make_symlink(root_devnode, err, 0555, two, &stderr_ent))) { | |
182 | printf("Couldn't make stderr, err %d.\n", error); | |
183 | goto bad; | |
184 | } | |
0a7de745 | 185 | |
b0d623f7 A |
186 | return 0; |
187 | ||
188 | bad: | |
189 | if (stdin_ent) { | |
190 | dev_free_name(stdin_ent); | |
191 | } | |
192 | if (stdout_ent) { | |
193 | dev_free_name(stdout_ent); | |
194 | } | |
195 | if (stderr_ent) { | |
196 | dev_free_name(stderr_ent); | |
197 | } | |
198 | ||
199 | return error; | |
200 | } | |
201 | ||
202 | int | |
203 | fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp, enum vtype vtype, int fdno) | |
1c79356b | 204 | { |
1c79356b A |
205 | struct fdhashhead *fc; |
206 | struct fdescnode *fd; | |
207 | int error = 0; | |
91447636 A |
208 | int vid = 0; |
209 | struct vnode_fsparam vfsp; | |
1c79356b | 210 | |
b0d623f7 A |
211 | fdesc_lock(); |
212 | ||
1c79356b A |
213 | fc = FD_NHASH(ix); |
214 | loop: | |
215 | for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) { | |
91447636 | 216 | if (fd->fd_ix == ix && vnode_mount(fd->fd_vnode) == mp) { |
0a7de745 | 217 | vid = vnode_vid(fd->fd_vnode); |
b0d623f7 | 218 | fdesc_unlock(); |
91447636 | 219 | |
39236c6e A |
220 | if (vnode_getwithvid(fd->fd_vnode, vid)) { |
221 | fdesc_lock(); | |
1c79356b | 222 | goto loop; |
39236c6e | 223 | } |
b0d623f7 | 224 | |
1c79356b | 225 | *vpp = fd->fd_vnode; |
f427ee49 | 226 | (*vpp)->v_type = (uint16_t)vtype; |
91447636 | 227 | |
0a7de745 | 228 | return error; |
1c79356b A |
229 | } |
230 | } | |
231 | ||
b0d623f7 | 232 | /* Only one thread can add to the hash at a time */ |
1c79356b A |
233 | if (fdcache_lock & FDL_LOCKED) { |
234 | fdcache_lock |= FDL_WANT; | |
b0d623f7 | 235 | msleep((caddr_t) &fdcache_lock, &fdesc_mtx, PINOD, "fdesc_allocvp", NULL); |
1c79356b A |
236 | goto loop; |
237 | } | |
b0d623f7 | 238 | |
1c79356b | 239 | fdcache_lock |= FDL_LOCKED; |
b0d623f7 | 240 | fdesc_unlock(); |
1c79356b A |
241 | |
242 | MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK); | |
91447636 A |
243 | |
244 | vfsp.vnfs_mp = mp; | |
245 | vfsp.vnfs_vtype = vtype; | |
246 | vfsp.vnfs_str = "fdesc"; | |
2d21ac55 | 247 | vfsp.vnfs_dvp = NULL; |
91447636 | 248 | vfsp.vnfs_fsnode = fd; |
2d21ac55 | 249 | vfsp.vnfs_cnp = NULL; |
91447636 A |
250 | vfsp.vnfs_vops = fdesc_vnodeop_p; |
251 | vfsp.vnfs_rdev = 0; | |
252 | vfsp.vnfs_filesize = 0; | |
253 | vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE; | |
254 | vfsp.vnfs_marksystem = 0; | |
b0d623f7 | 255 | vfsp.vnfs_markroot = 0; |
91447636 A |
256 | |
257 | error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, vpp); | |
1c79356b A |
258 | if (error) { |
259 | FREE(fd, M_TEMP); | |
b0d623f7 | 260 | fdesc_lock(); |
1c79356b A |
261 | goto out; |
262 | } | |
0a7de745 | 263 | |
91447636 | 264 | (*vpp)->v_tag = VT_FDESC; |
1c79356b A |
265 | fd->fd_vnode = *vpp; |
266 | fd->fd_type = ftype; | |
267 | fd->fd_fd = -1; | |
2d21ac55 | 268 | fd->fd_link = NULL; |
1c79356b | 269 | fd->fd_ix = ix; |
b0d623f7 | 270 | fd->fd_fd = fdno; |
0a7de745 | 271 | |
b0d623f7 | 272 | fdesc_lock(); |
1c79356b | 273 | |
b0d623f7 | 274 | LIST_INSERT_HEAD(fc, fd, fd_hash); |
1c79356b | 275 | out: |
b0d623f7 | 276 | /* Hold the lock when we get here */ |
1c79356b A |
277 | fdcache_lock &= ~FDL_LOCKED; |
278 | ||
279 | if (fdcache_lock & FDL_WANT) { | |
280 | fdcache_lock &= ~FDL_WANT; | |
281 | wakeup((caddr_t) &fdcache_lock); | |
282 | } | |
0a7de745 | 283 | |
b0d623f7 | 284 | fdesc_unlock(); |
1c79356b | 285 | |
0a7de745 | 286 | return error; |
1c79356b A |
287 | } |
288 | ||
289 | /* | |
290 | * vp is the current namei directory | |
291 | * ndp is the name to locate in that directory... | |
b0d623f7 A |
292 | * |
293 | * This vnop should only be called on the special directory /dev/fd. | |
1c79356b A |
294 | */ |
295 | int | |
b0d623f7 | 296 | devfs_devfd_lookup(struct vnop_lookup_args *ap) |
1c79356b A |
297 | { |
298 | struct vnode **vpp = ap->a_vpp; | |
299 | struct vnode *dvp = ap->a_dvp; | |
300 | struct componentname *cnp = ap->a_cnp; | |
301 | char *pname = cnp->cn_nameptr; | |
91447636 A |
302 | struct proc *p = vfs_context_proc(ap->a_context); |
303 | int numfiles = p->p_fd->fd_nfiles; | |
304 | int fd; | |
1c79356b A |
305 | int error; |
306 | struct vnode *fvp; | |
1c79356b | 307 | |
1c79356b A |
308 | if (cnp->cn_namelen == 1 && *pname == '.') { |
309 | *vpp = dvp; | |
0a7de745 A |
310 | |
311 | if ((error = vnode_get(dvp))) { | |
c3c9b80d | 312 | goto bad; |
91447636 | 313 | } |
0a7de745 | 314 | return 0; |
1c79356b A |
315 | } |
316 | ||
b0d623f7 A |
317 | fd = 0; |
318 | while (*pname >= '0' && *pname <= '9') { | |
319 | fd = 10 * fd + *pname++ - '0'; | |
0a7de745 | 320 | if (fd >= numfiles) { |
1c79356b | 321 | break; |
0a7de745 | 322 | } |
b0d623f7 | 323 | } |
1c79356b | 324 | |
b0d623f7 A |
325 | if (*pname != '\0') { |
326 | error = ENOENT; | |
327 | goto bad; | |
328 | } | |
1c79356b | 329 | |
b0d623f7 | 330 | if (fd < 0 || fd >= numfiles || |
0a7de745 A |
331 | *fdfile(p, fd) == NULL || |
332 | (*fdflags(p, fd) & UF_RESERVED)) { | |
b0d623f7 A |
333 | error = EBADF; |
334 | goto bad; | |
1c79356b A |
335 | } |
336 | ||
0a7de745 A |
337 | error = fdesc_allocvp(Fdesc, FD_DESC + fd, dvp->v_mount, &fvp, VNON, fd); |
338 | if (error) { | |
b0d623f7 | 339 | goto bad; |
0a7de745 | 340 | } |
b0d623f7 | 341 | *vpp = fvp; |
0a7de745 | 342 | return 0; |
b0d623f7 A |
343 | |
344 | bad: | |
1c79356b | 345 | *vpp = NULL; |
0a7de745 | 346 | return error; |
1c79356b A |
347 | } |
348 | ||
349 | int | |
2d21ac55 | 350 | fdesc_open(struct vnop_open_args *ap) |
1c79356b A |
351 | { |
352 | struct vnode *vp = ap->a_vp; | |
2d21ac55 A |
353 | thread_t thr = vfs_context_thread(ap->a_context); |
354 | uthread_t uu; | |
1c79356b A |
355 | int error = 0; |
356 | ||
0a7de745 A |
357 | if (thr == NULL) { |
358 | return EINVAL; | |
359 | } | |
2d21ac55 A |
360 | |
361 | uu = get_bsdthread_info(thr); | |
362 | ||
1c79356b A |
363 | switch (VTOFDESC(vp)->fd_type) { |
364 | case Fdesc: | |
365 | /* | |
2d21ac55 | 366 | * XXX Kludge: set uu->uu_dupfd to contain the value of the |
0a7de745 | 367 | * the file descriptor being sought for duplication. The error |
1c79356b A |
368 | * return ensures that the vnode for this device will be |
369 | * released by vn_open. Open will detect this special error and | |
370 | * take the actions in dupfdopen. Other callers of vn_open or | |
91447636 | 371 | * vnop_open will simply report the error. |
1c79356b | 372 | */ |
0a7de745 | 373 | uu->uu_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ |
1c79356b A |
374 | error = ENODEV; |
375 | break; | |
0a7de745 | 376 | default: |
b0d623f7 | 377 | panic("Invalid type for fdesc node!"); |
2d21ac55 | 378 | break; |
1c79356b A |
379 | } |
380 | ||
0a7de745 | 381 | return error; |
1c79356b A |
382 | } |
383 | ||
384 | static int | |
91447636 | 385 | fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context) |
1c79356b | 386 | { |
91447636 A |
387 | struct fileproc *fp; |
388 | struct proc *p = vfs_context_proc(a_context); | |
1c79356b A |
389 | struct stat stb; |
390 | int error; | |
391 | ||
0a7de745 A |
392 | if ((error = fp_lookup(p, fd, &fp, 0))) { |
393 | return error; | |
394 | } | |
f427ee49 | 395 | switch (FILEGLOB_DTYPE(fp->fp_glob)) { |
1c79356b | 396 | case DTYPE_VNODE: |
f427ee49 | 397 | if ((error = vnode_getwithref((struct vnode *) fp->fp_glob->fg_data)) != 0) { |
91447636 A |
398 | break; |
399 | } | |
f427ee49 | 400 | if ((error = vnode_authorize((struct vnode *)fp->fp_glob->fg_data, |
0a7de745 A |
401 | NULL, |
402 | KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, | |
403 | a_context)) == 0) { | |
f427ee49 | 404 | error = vnode_getattr((struct vnode *)fp->fp_glob->fg_data, vap, a_context); |
0a7de745 | 405 | } |
1c79356b A |
406 | if (error == 0 && vap->va_type == VDIR) { |
407 | /* | |
408 | * directories can cause loops in the namespace, | |
409 | * so turn off the 'x' bits to avoid trouble. | |
91447636 A |
410 | * |
411 | * XXX ACLs break this, of course | |
1c79356b | 412 | */ |
0a7de745 | 413 | vap->va_mode &= ~((VEXEC) | (VEXEC >> 3) | (VEXEC >> 6)); |
1c79356b | 414 | } |
f427ee49 | 415 | (void)vnode_put((struct vnode *) fp->fp_glob->fg_data); |
1c79356b A |
416 | break; |
417 | ||
418 | case DTYPE_SOCKET: | |
91447636 | 419 | case DTYPE_PIPE: |
2d21ac55 | 420 | #if SOCKETS |
f427ee49 A |
421 | if (FILEGLOB_DTYPE(fp->fp_glob) == DTYPE_SOCKET) { |
422 | error = soo_stat((struct socket *)fp->fp_glob->fg_data, (void *)&stb, 0); | |
0a7de745 | 423 | } else |
2d21ac55 | 424 | #endif /* SOCKETS */ |
f427ee49 | 425 | error = pipe_stat((struct pipe *)fp->fp_glob->fg_data, (void *)&stb, 0); |
91447636 | 426 | |
1c79356b | 427 | if (error == 0) { |
f427ee49 | 428 | if (FILEGLOB_DTYPE(fp->fp_glob) == DTYPE_SOCKET) { |
0a7de745 A |
429 | VATTR_RETURN(vap, va_type, VSOCK); |
430 | } else { | |
431 | VATTR_RETURN(vap, va_type, VFIFO); | |
432 | } | |
91447636 A |
433 | |
434 | VATTR_RETURN(vap, va_mode, stb.st_mode); | |
435 | VATTR_RETURN(vap, va_nlink, stb.st_nlink); | |
436 | VATTR_RETURN(vap, va_uid, stb.st_uid); | |
437 | VATTR_RETURN(vap, va_gid, stb.st_gid); | |
438 | VATTR_RETURN(vap, va_fsid, stb.st_dev); | |
439 | VATTR_RETURN(vap, va_fileid, stb.st_ino); | |
440 | VATTR_RETURN(vap, va_data_size, stb.st_size); | |
441 | VATTR_RETURN(vap, va_access_time, stb.st_atimespec); | |
442 | VATTR_RETURN(vap, va_modify_time, stb.st_mtimespec); | |
443 | VATTR_RETURN(vap, va_change_time, stb.st_ctimespec); | |
444 | VATTR_RETURN(vap, va_gen, stb.st_gen); | |
445 | VATTR_RETURN(vap, va_flags, stb.st_flags); | |
446 | VATTR_RETURN(vap, va_rdev, stb.st_rdev); | |
447 | VATTR_RETURN(vap, va_total_alloc, stb.st_blocks * stb.st_blksize); | |
448 | VATTR_RETURN(vap, va_acl, NULL); | |
1c79356b A |
449 | } |
450 | break; | |
451 | ||
452 | default: | |
91447636 | 453 | error = EBADF; |
1c79356b A |
454 | } |
455 | ||
91447636 | 456 | fp_drop(p, fd, fp, 0); |
0a7de745 | 457 | return error; |
1c79356b A |
458 | } |
459 | ||
460 | int | |
2d21ac55 | 461 | fdesc_getattr(struct vnop_getattr_args *ap) |
1c79356b A |
462 | { |
463 | struct vnode *vp = ap->a_vp; | |
91447636 | 464 | struct vnode_attr *vap = ap->a_vap; |
1c79356b A |
465 | unsigned fd; |
466 | int error = 0; | |
467 | ||
468 | switch (VTOFDESC(vp)->fd_type) { | |
1c79356b A |
469 | case Fdesc: |
470 | fd = VTOFDESC(vp)->fd_fd; | |
91447636 | 471 | error = fdesc_attr(fd, vap, ap->a_context); |
1c79356b A |
472 | break; |
473 | ||
474 | default: | |
b0d623f7 | 475 | panic("Invalid type for an fdesc node!\n"); |
0a7de745 | 476 | break; |
1c79356b | 477 | } |
0a7de745 A |
478 | |
479 | /* | |
b0d623f7 A |
480 | * Yes, we do this without locking, but this value is always just |
481 | * a snapshot. | |
482 | */ | |
1c79356b | 483 | if (error == 0) { |
f427ee49 | 484 | vp->v_type = (uint16_t)vap->va_type; |
0a7de745 | 485 | |
b0d623f7 A |
486 | /* We need an inactive to reset type to VNON */ |
487 | vnode_setneedinactive(vp); | |
1c79356b A |
488 | } |
489 | ||
0a7de745 | 490 | return error; |
1c79356b A |
491 | } |
492 | ||
493 | int | |
2d21ac55 | 494 | fdesc_setattr(struct vnop_setattr_args *ap) |
1c79356b | 495 | { |
91447636 | 496 | struct fileproc *fp; |
1c79356b A |
497 | unsigned fd; |
498 | int error; | |
91447636 | 499 | struct proc * p = vfs_context_proc(ap->a_context); |
1c79356b A |
500 | |
501 | /* | |
502 | * Can't mess with the root vnode | |
503 | */ | |
504 | switch (VTOFDESC(ap->a_vp)->fd_type) { | |
505 | case Fdesc: | |
506 | break; | |
1c79356b | 507 | default: |
b0d623f7 | 508 | panic("Invalid type for an fdesc node!\n"); |
0a7de745 | 509 | return EACCES; |
1c79356b A |
510 | } |
511 | ||
512 | fd = VTOFDESC(ap->a_vp)->fd_fd; | |
0a7de745 A |
513 | if ((error = fp_lookup(vfs_context_proc(ap->a_context), fd, &fp, 0))) { |
514 | return error; | |
515 | } | |
1c79356b A |
516 | |
517 | /* | |
518 | * Can setattr the underlying vnode, but not sockets! | |
519 | */ | |
f427ee49 | 520 | switch (FILEGLOB_DTYPE(fp->fp_glob)) { |
1c79356b | 521 | case DTYPE_VNODE: |
91447636 | 522 | { |
f427ee49 | 523 | if ((error = vnode_getwithref((struct vnode *) fp->fp_glob->fg_data)) != 0) { |
91447636 | 524 | break; |
0a7de745 | 525 | } |
f427ee49 A |
526 | error = vnode_setattr((struct vnode *) fp->fp_glob->fg_data, ap->a_vap, ap->a_context); |
527 | (void)vnode_put((struct vnode *) fp->fp_glob->fg_data); | |
1c79356b | 528 | break; |
91447636 | 529 | } |
1c79356b A |
530 | |
531 | case DTYPE_SOCKET: | |
91447636 | 532 | case DTYPE_PIPE: |
1c79356b A |
533 | error = 0; |
534 | break; | |
535 | ||
536 | default: | |
39236c6e | 537 | error = EBADF; |
1c79356b A |
538 | break; |
539 | } | |
540 | ||
91447636 | 541 | fp_drop(p, fd, fp, 0); |
0a7de745 | 542 | return error; |
1c79356b A |
543 | } |
544 | ||
545 | #define UIO_MX 16 | |
546 | ||
b0d623f7 | 547 | /* |
0a7de745 A |
548 | * static struct dirtmp { |
549 | * u_int32_t d_fileno; | |
550 | * u_short d_reclen; | |
551 | * u_short d_namlen; | |
552 | * char d_name[8]; | |
553 | * } rootent[] = { | |
554 | * { FD_DEVFD, UIO_MX, 2, "fd" }, | |
555 | * { FD_STDIN, UIO_MX, 5, "stdin" }, | |
556 | * { FD_STDOUT, UIO_MX, 6, "stdout" }, | |
557 | * { FD_STDERR, UIO_MX, 6, "stderr" }, | |
558 | * { 0, 0, 0, "" } | |
559 | * }; | |
560 | */ | |
1c79356b | 561 | |
b0d623f7 | 562 | /* Only called on /dev/fd */ |
1c79356b | 563 | int |
b0d623f7 | 564 | devfs_devfd_readdir(struct vnop_readdir_args *ap) |
1c79356b A |
565 | { |
566 | struct uio *uio = ap->a_uio; | |
91447636 | 567 | struct proc *p = current_proc(); |
f427ee49 A |
568 | off_t i; |
569 | int error; | |
1c79356b A |
570 | |
571 | /* | |
572 | * We don't allow exporting fdesc mounts, and currently local | |
573 | * requests do not need cookies. | |
574 | */ | |
0a7de745 A |
575 | if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF)) { |
576 | return EINVAL; | |
577 | } | |
1c79356b | 578 | |
5ba3f43e A |
579 | /* |
580 | * There needs to be space for at least one entry. | |
581 | */ | |
0a7de745 A |
582 | if (uio_resid(uio) < UIO_MX) { |
583 | return EINVAL; | |
584 | } | |
5ba3f43e | 585 | |
1c79356b A |
586 | i = uio->uio_offset / UIO_MX; |
587 | error = 0; | |
5ba3f43e | 588 | while (uio_resid(uio) >= UIO_MX) { |
f427ee49 | 589 | if (i >= p->p_fd->fd_nfiles || i < 0) { |
1c79356b | 590 | break; |
0a7de745 | 591 | } |
1c79356b A |
592 | |
593 | if (*fdfile(p, i) != NULL && !(*fdflags(p, i) & UF_RESERVED)) { | |
594 | struct dirent d; | |
595 | struct dirent *dp = &d; | |
596 | ||
597 | bzero((caddr_t) dp, UIO_MX); | |
598 | ||
f427ee49 A |
599 | dp->d_namlen = (__uint8_t)scnprintf(dp->d_name, sizeof(dp->d_name), |
600 | "%lld", i); | |
1c79356b A |
601 | dp->d_reclen = UIO_MX; |
602 | dp->d_type = DT_UNKNOWN; | |
f427ee49 | 603 | dp->d_fileno = (ino_t)i + FD_STDIN; |
1c79356b A |
604 | /* |
605 | * And ship to userland | |
606 | */ | |
607 | error = uiomove((caddr_t) dp, UIO_MX, uio); | |
0a7de745 | 608 | if (error) { |
1c79356b | 609 | break; |
0a7de745 | 610 | } |
1c79356b A |
611 | } |
612 | i++; | |
613 | } | |
614 | ||
615 | uio->uio_offset = i * UIO_MX; | |
0a7de745 | 616 | return error; |
1c79356b A |
617 | } |
618 | ||
1c79356b | 619 | int |
91447636 | 620 | fdesc_read(__unused struct vnop_read_args *ap) |
1c79356b | 621 | { |
0a7de745 | 622 | return ENOTSUP; |
1c79356b A |
623 | } |
624 | ||
625 | int | |
91447636 | 626 | fdesc_write(__unused struct vnop_write_args *ap) |
1c79356b | 627 | { |
0a7de745 | 628 | return ENOTSUP; |
1c79356b A |
629 | } |
630 | ||
631 | int | |
91447636 | 632 | fdesc_ioctl(__unused struct vnop_ioctl_args *ap) |
1c79356b | 633 | { |
0a7de745 | 634 | return ENOTSUP; |
1c79356b A |
635 | } |
636 | ||
637 | int | |
91447636 | 638 | fdesc_select(__unused struct vnop_select_args *ap) |
1c79356b | 639 | { |
0a7de745 | 640 | return ENOTSUP; |
1c79356b A |
641 | } |
642 | ||
643 | int | |
2d21ac55 | 644 | fdesc_inactive(struct vnop_inactive_args *ap) |
1c79356b A |
645 | { |
646 | struct vnode *vp = ap->a_vp; | |
647 | ||
648 | /* | |
649 | * Clear out the v_type field to avoid | |
650 | * nasty things happening in vgone(). | |
651 | */ | |
1c79356b | 652 | vp->v_type = VNON; |
b0d623f7 | 653 | |
0a7de745 | 654 | return 0; |
1c79356b A |
655 | } |
656 | ||
657 | int | |
2d21ac55 | 658 | fdesc_reclaim(struct vnop_reclaim_args *ap) |
1c79356b A |
659 | { |
660 | struct vnode *vp = ap->a_vp; | |
661 | struct fdescnode *fd = VTOFDESC(vp); | |
662 | ||
b0d623f7 A |
663 | fdesc_lock(); |
664 | ||
1c79356b A |
665 | LIST_REMOVE(fd, fd_hash); |
666 | FREE(vp->v_data, M_TEMP); | |
2d21ac55 | 667 | vp->v_data = NULL; |
0a7de745 | 668 | |
b0d623f7 | 669 | fdesc_unlock(); |
1c79356b | 670 | |
0a7de745 | 671 | return 0; |
1c79356b A |
672 | } |
673 | ||
674 | /* | |
675 | * Return POSIX pathconf information applicable to special devices. | |
676 | */ | |
91447636 | 677 | int |
2d21ac55 | 678 | fdesc_pathconf(struct vnop_pathconf_args *ap) |
1c79356b | 679 | { |
1c79356b A |
680 | switch (ap->a_name) { |
681 | case _PC_LINK_MAX: | |
682 | *ap->a_retval = LINK_MAX; | |
0a7de745 | 683 | return 0; |
1c79356b A |
684 | case _PC_MAX_CANON: |
685 | *ap->a_retval = MAX_CANON; | |
0a7de745 | 686 | return 0; |
1c79356b A |
687 | case _PC_MAX_INPUT: |
688 | *ap->a_retval = MAX_INPUT; | |
0a7de745 | 689 | return 0; |
1c79356b A |
690 | case _PC_PIPE_BUF: |
691 | *ap->a_retval = PIPE_BUF; | |
0a7de745 | 692 | return 0; |
1c79356b | 693 | case _PC_CHOWN_RESTRICTED: |
0a7de745 A |
694 | *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */ |
695 | return 0; | |
1c79356b A |
696 | case _PC_VDISABLE: |
697 | *ap->a_retval = _POSIX_VDISABLE; | |
0a7de745 | 698 | return 0; |
1c79356b | 699 | default: |
0a7de745 | 700 | return EINVAL; |
1c79356b A |
701 | } |
702 | /* NOTREACHED */ | |
703 | } | |
704 | ||
1c79356b A |
705 | /* |
706 | * /dev/fd "should never get here" operation | |
707 | */ | |
708 | int | |
91447636 | 709 | fdesc_badop(void) |
1c79356b | 710 | { |
0a7de745 | 711 | return ENOTSUP; |
1c79356b A |
712 | /* NOTREACHED */ |
713 | } | |
714 | ||
715 | #define VOPFUNC int (*)(void *) | |
716 | ||
91447636 A |
717 | #define fdesc_create (int (*) (struct vnop_create_args *))eopnotsupp |
718 | #define fdesc_mknod (int (*) (struct vnop_mknod_args *))eopnotsupp | |
719 | #define fdesc_close (int (*) (struct vnop_close_args *))nullop | |
720 | #define fdesc_access (int (*) (struct vnop_access_args *))nullop | |
721 | #define fdesc_mmap (int (*) (struct vnop_mmap_args *))eopnotsupp | |
0a7de745 | 722 | #define fdesc_revoke nop_revoke |
91447636 A |
723 | #define fdesc_fsync (int (*) (struct vnop_fsync_args *))nullop |
724 | #define fdesc_remove (int (*) (struct vnop_remove_args *))eopnotsupp | |
725 | #define fdesc_link (int (*) (struct vnop_link_args *))eopnotsupp | |
726 | #define fdesc_rename (int (*) (struct vnop_rename_args *))eopnotsupp | |
727 | #define fdesc_mkdir (int (*) (struct vnop_mkdir_args *))eopnotsupp | |
728 | #define fdesc_rmdir (int (*) (struct vnop_rmdir_args *))eopnotsupp | |
729 | #define fdesc_symlink (int (*) (struct vnop_symlink_args *))eopnotsupp | |
730 | #define fdesc_strategy (int (*) (struct vnop_strategy_args *))fdesc_badop | |
731 | #define fdesc_advlock (int (*) (struct vnop_advlock_args *))eopnotsupp | |
732 | #define fdesc_bwrite (int (*) (struct vnop_bwrite_args *))eopnotsupp | |
733 | #define fdesc_blktooff (int (*) (struct vnop_blktooff_args *))eopnotsupp | |
734 | #define fdesc_offtoblk (int (*) (struct vnop_offtoblk_args *))eopnotsupp | |
735 | #define fdesc_blockmap (int (*) (struct vnop_blockmap_args *))eopnotsupp | |
1c79356b | 736 | |
0a7de745 | 737 | int(**fdesc_vnodeop_p)(void *); |
cb323159 A |
738 | const struct vnodeopv_entry_desc devfs_fdesc_vnodeop_entries[] = { |
739 | { .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)vn_default_error }, | |
740 | { .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)vn_default_error}, /* lookup */ | |
741 | { .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)fdesc_create }, /* create */ | |
742 | { .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)fdesc_mknod }, /* mknod */ | |
743 | { .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)fdesc_open }, /* open */ | |
744 | { .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)fdesc_close }, /* close */ | |
745 | { .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)fdesc_access }, /* access */ | |
746 | { .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)fdesc_getattr }, /* getattr */ | |
747 | { .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)fdesc_setattr }, /* setattr */ | |
748 | { .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)fdesc_read }, /* read */ | |
749 | { .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)fdesc_write }, /* write */ | |
750 | { .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)fdesc_ioctl }, /* ioctl */ | |
751 | { .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)fdesc_select }, /* select */ | |
752 | { .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)fdesc_revoke }, /* revoke */ | |
753 | { .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)fdesc_mmap }, /* mmap */ | |
754 | { .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)fdesc_fsync }, /* fsync */ | |
755 | { .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)fdesc_remove }, /* remove */ | |
756 | { .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)fdesc_link }, /* link */ | |
757 | { .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)fdesc_rename }, /* rename */ | |
758 | { .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)fdesc_mkdir }, /* mkdir */ | |
759 | { .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)fdesc_rmdir }, /* rmdir */ | |
760 | { .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)fdesc_symlink }, /* symlink */ | |
761 | { .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)vn_default_error},/* readdir */ | |
762 | { .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)err_readlink}, /* readlink */ | |
763 | { .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)fdesc_inactive },/* inactive */ | |
764 | { .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)fdesc_reclaim }, /* reclaim */ | |
765 | { .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)fdesc_strategy }, /* strategy */ | |
766 | { .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)fdesc_pathconf }, /* pathconf */ | |
767 | { .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)fdesc_advlock }, /* advlock */ | |
768 | { .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)fdesc_bwrite }, /* bwrite */ | |
769 | { .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein }, /* pagein */ | |
770 | { .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout }, /* pageout */ | |
771 | { .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */ | |
772 | { .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)fdesc_blktooff }, /* blktooff */ | |
773 | { .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)fdesc_offtoblk }, /* offtoblk */ | |
774 | { .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)fdesc_blockmap }, /* blockmap */ | |
775 | { .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (VOPFUNC)NULL } | |
1c79356b | 776 | }; |
b0d623f7 | 777 | |
cb323159 A |
778 | const struct vnodeopv_desc devfs_fdesc_vnodeop_opv_desc = |
779 | { .opv_desc_vector_p = &fdesc_vnodeop_p, .opv_desc_ops = devfs_fdesc_vnodeop_entries }; |