ptyfs_vnops.c revision 1.46.2.1 1 /* $NetBSD: ptyfs_vnops.c,v 1.46.2.1 2014/08/10 06:55:54 tls Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
35 */
36
37 /*
38 * Copyright (c) 1993 Jan-Simon Pendry
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Jan-Simon Pendry.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
72 */
73
74 /*
75 * ptyfs vnode interface
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vnops.c,v 1.46.2.1 2014/08/10 06:55:54 tls Exp $");
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/time.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/filedesc.h>
87 #include <sys/proc.h>
88 #include <sys/vnode.h>
89 #include <sys/namei.h>
90 #include <sys/malloc.h>
91 #include <sys/mount.h>
92 #include <sys/select.h>
93 #include <sys/dirent.h>
94 #include <sys/resourcevar.h>
95 #include <sys/stat.h>
96 #include <sys/conf.h>
97 #include <sys/tty.h>
98 #include <sys/pty.h>
99 #include <sys/kauth.h>
100
101 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
102
103 #include <machine/reg.h>
104
105 #include <fs/ptyfs/ptyfs.h>
106 #include <miscfs/genfs/genfs.h>
107 #include <miscfs/specfs/specdev.h>
108
109 MALLOC_DECLARE(M_PTYFSTMP);
110
111 /*
112 * Vnode Operations.
113 *
114 */
115
116 int ptyfs_lookup (void *);
117 #define ptyfs_create genfs_eopnotsupp
118 #define ptyfs_mknod genfs_eopnotsupp
119 int ptyfs_open (void *);
120 int ptyfs_close (void *);
121 int ptyfs_access (void *);
122 int ptyfs_getattr (void *);
123 int ptyfs_setattr (void *);
124 int ptyfs_read (void *);
125 int ptyfs_write (void *);
126 #define ptyfs_fcntl genfs_fcntl
127 int ptyfs_ioctl (void *);
128 int ptyfs_poll (void *);
129 int ptyfs_kqfilter (void *);
130 #define ptyfs_revoke genfs_revoke
131 #define ptyfs_mmap genfs_eopnotsupp
132 #define ptyfs_fsync genfs_nullop
133 #define ptyfs_seek genfs_nullop
134 #define ptyfs_remove genfs_eopnotsupp
135 #define ptyfs_link genfs_abortop
136 #define ptyfs_rename genfs_eopnotsupp
137 #define ptyfs_mkdir genfs_eopnotsupp
138 #define ptyfs_rmdir genfs_eopnotsupp
139 #define ptyfs_symlink genfs_abortop
140 int ptyfs_readdir (void *);
141 #define ptyfs_readlink genfs_eopnotsupp
142 #define ptyfs_abortop genfs_abortop
143 int ptyfs_reclaim (void *);
144 int ptyfs_inactive (void *);
145 #define ptyfs_lock genfs_lock
146 #define ptyfs_unlock genfs_unlock
147 #define ptyfs_bmap genfs_badop
148 #define ptyfs_strategy genfs_badop
149 int ptyfs_print (void *);
150 int ptyfs_pathconf (void *);
151 #define ptyfs_islocked genfs_islocked
152 int ptyfs_advlock (void *);
153 #define ptyfs_bwrite genfs_eopnotsupp
154 #define ptyfs_putpages genfs_null_putpages
155
156 static int ptyfs_update(struct vnode *, const struct timespec *,
157 const struct timespec *, int);
158 static int ptyfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
159 struct lwp *);
160 static int ptyfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
161 static int atoi(const char *, size_t);
162
163 /*
164 * ptyfs vnode operations.
165 */
166 int (**ptyfs_vnodeop_p)(void *);
167 const struct vnodeopv_entry_desc ptyfs_vnodeop_entries[] = {
168 { &vop_default_desc, vn_default_error },
169 { &vop_lookup_desc, ptyfs_lookup }, /* lookup */
170 { &vop_create_desc, ptyfs_create }, /* create */
171 { &vop_mknod_desc, ptyfs_mknod }, /* mknod */
172 { &vop_open_desc, ptyfs_open }, /* open */
173 { &vop_close_desc, ptyfs_close }, /* close */
174 { &vop_access_desc, ptyfs_access }, /* access */
175 { &vop_getattr_desc, ptyfs_getattr }, /* getattr */
176 { &vop_setattr_desc, ptyfs_setattr }, /* setattr */
177 { &vop_read_desc, ptyfs_read }, /* read */
178 { &vop_write_desc, ptyfs_write }, /* write */
179 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
180 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
181 { &vop_ioctl_desc, ptyfs_ioctl }, /* ioctl */
182 { &vop_fcntl_desc, ptyfs_fcntl }, /* fcntl */
183 { &vop_poll_desc, ptyfs_poll }, /* poll */
184 { &vop_kqfilter_desc, ptyfs_kqfilter }, /* kqfilter */
185 { &vop_revoke_desc, ptyfs_revoke }, /* revoke */
186 { &vop_mmap_desc, ptyfs_mmap }, /* mmap */
187 { &vop_fsync_desc, ptyfs_fsync }, /* fsync */
188 { &vop_seek_desc, ptyfs_seek }, /* seek */
189 { &vop_remove_desc, ptyfs_remove }, /* remove */
190 { &vop_link_desc, ptyfs_link }, /* link */
191 { &vop_rename_desc, ptyfs_rename }, /* rename */
192 { &vop_mkdir_desc, ptyfs_mkdir }, /* mkdir */
193 { &vop_rmdir_desc, ptyfs_rmdir }, /* rmdir */
194 { &vop_symlink_desc, ptyfs_symlink }, /* symlink */
195 { &vop_readdir_desc, ptyfs_readdir }, /* readdir */
196 { &vop_readlink_desc, ptyfs_readlink }, /* readlink */
197 { &vop_abortop_desc, ptyfs_abortop }, /* abortop */
198 { &vop_inactive_desc, ptyfs_inactive }, /* inactive */
199 { &vop_reclaim_desc, ptyfs_reclaim }, /* reclaim */
200 { &vop_lock_desc, ptyfs_lock }, /* lock */
201 { &vop_unlock_desc, ptyfs_unlock }, /* unlock */
202 { &vop_bmap_desc, ptyfs_bmap }, /* bmap */
203 { &vop_strategy_desc, ptyfs_strategy }, /* strategy */
204 { &vop_print_desc, ptyfs_print }, /* print */
205 { &vop_islocked_desc, ptyfs_islocked }, /* islocked */
206 { &vop_pathconf_desc, ptyfs_pathconf }, /* pathconf */
207 { &vop_advlock_desc, ptyfs_advlock }, /* advlock */
208 { &vop_bwrite_desc, ptyfs_bwrite }, /* bwrite */
209 { &vop_putpages_desc, ptyfs_putpages }, /* putpages */
210 { NULL, NULL }
211 };
212 const struct vnodeopv_desc ptyfs_vnodeop_opv_desc =
213 { &ptyfs_vnodeop_p, ptyfs_vnodeop_entries };
214
215 /*
216 * _reclaim is called when getnewvnode()
217 * wants to make use of an entry on the vnode
218 * free list. at this time the filesystem needs
219 * to free any private data and remove the node
220 * from any private lists.
221 */
222 int
223 ptyfs_reclaim(void *v)
224 {
225 struct vop_reclaim_args /* {
226 struct vnode *a_vp;
227 } */ *ap = v;
228 return ptyfs_freevp(ap->a_vp);
229 }
230
231 int
232 ptyfs_inactive(void *v)
233 {
234 struct vop_inactive_args /* {
235 struct vnode *a_vp;
236 bool *a_recycle;
237 } */ *ap = v;
238 struct vnode *vp = ap->a_vp;
239 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
240
241 switch (ptyfs->ptyfs_type) {
242 case PTYFSpts:
243 case PTYFSptc:
244 /* Emulate file deletion for call reclaim(). */
245 *ap->a_recycle = true;
246 break;
247 default:
248 break;
249 }
250 return spec_inactive(v);
251 }
252
253 /*
254 * Return POSIX pathconf information applicable to special devices.
255 */
256 int
257 ptyfs_pathconf(void *v)
258 {
259 struct vop_pathconf_args /* {
260 struct vnode *a_vp;
261 int a_name;
262 register_t *a_retval;
263 } */ *ap = v;
264
265 switch (ap->a_name) {
266 case _PC_LINK_MAX:
267 *ap->a_retval = LINK_MAX;
268 return 0;
269 case _PC_MAX_CANON:
270 *ap->a_retval = MAX_CANON;
271 return 0;
272 case _PC_MAX_INPUT:
273 *ap->a_retval = MAX_INPUT;
274 return 0;
275 case _PC_PIPE_BUF:
276 *ap->a_retval = PIPE_BUF;
277 return 0;
278 case _PC_CHOWN_RESTRICTED:
279 *ap->a_retval = 1;
280 return 0;
281 case _PC_VDISABLE:
282 *ap->a_retval = _POSIX_VDISABLE;
283 return 0;
284 case _PC_SYNC_IO:
285 *ap->a_retval = 1;
286 return 0;
287 default:
288 return EINVAL;
289 }
290 }
291
292 /*
293 * _print is used for debugging.
294 * just print a readable description
295 * of (vp).
296 */
297 int
298 ptyfs_print(void *v)
299 {
300 struct vop_print_args /* {
301 struct vnode *a_vp;
302 } */ *ap = v;
303 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
304
305 printf("tag VT_PTYFS, type %d, pty %d\n",
306 ptyfs->ptyfs_type, ptyfs->ptyfs_pty);
307 return 0;
308 }
309
310 /*
311 * support advisory locking on pty nodes
312 */
313 int
314 ptyfs_advlock(void *v)
315 {
316 struct vop_print_args /* {
317 struct vnode *a_vp;
318 } */ *ap = v;
319 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
320
321 switch (ptyfs->ptyfs_type) {
322 case PTYFSpts:
323 case PTYFSptc:
324 return spec_advlock(v);
325 default:
326 return EOPNOTSUPP;
327 }
328 }
329
330 /*
331 * Invent attributes for ptyfsnode (vp) and store
332 * them in (vap).
333 * Directories lengths are returned as zero since
334 * any real length would require the genuine size
335 * to be computed, and nothing cares anyway.
336 *
337 * this is relatively minimal for ptyfs.
338 */
339 int
340 ptyfs_getattr(void *v)
341 {
342 struct vop_getattr_args /* {
343 struct vnode *a_vp;
344 struct vattr *a_vap;
345 kauth_cred_t a_cred;
346 } */ *ap = v;
347 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
348 struct vattr *vap = ap->a_vap;
349
350 PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
351
352 /* start by zeroing out the attributes */
353 vattr_null(vap);
354
355 /* next do all the common fields */
356 vap->va_type = ap->a_vp->v_type;
357 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
358 vap->va_fileid = ptyfs->ptyfs_fileno;
359 vap->va_gen = 0;
360 vap->va_flags = 0;
361 vap->va_blocksize = PAGE_SIZE;
362
363 vap->va_atime = ptyfs->ptyfs_atime;
364 vap->va_mtime = ptyfs->ptyfs_mtime;
365 vap->va_ctime = ptyfs->ptyfs_ctime;
366 vap->va_birthtime = ptyfs->ptyfs_birthtime;
367 vap->va_mode = ptyfs->ptyfs_mode;
368 vap->va_flags = ptyfs->ptyfs_flags;
369 vap->va_uid = ptyfs->ptyfs_uid;
370 vap->va_gid = ptyfs->ptyfs_gid;
371
372 switch (ptyfs->ptyfs_type) {
373 case PTYFSpts:
374 case PTYFSptc:
375 if (pty_isfree(ptyfs->ptyfs_pty, 1))
376 return ENOENT;
377 vap->va_bytes = vap->va_size = 0;
378 vap->va_rdev = ap->a_vp->v_rdev;
379 vap->va_nlink = 1;
380 break;
381 case PTYFSroot:
382 vap->va_rdev = 0;
383 vap->va_bytes = vap->va_size = DEV_BSIZE;
384 vap->va_nlink = 2;
385 break;
386 default:
387 return EOPNOTSUPP;
388 }
389
390 return 0;
391 }
392
393 /*ARGSUSED*/
394 int
395 ptyfs_setattr(void *v)
396 {
397 struct vop_setattr_args /* {
398 struct vnodeop_desc *a_desc;
399 struct vnode *a_vp;
400 struct vattr *a_vap;
401 kauth_cred_t a_cred;
402 } */ *ap = v;
403 struct vnode *vp = ap->a_vp;
404 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
405 struct vattr *vap = ap->a_vap;
406 kauth_cred_t cred = ap->a_cred;
407 struct lwp *l = curlwp;
408 int error;
409 kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
410 bool changing_sysflags = false;
411
412 if (vap->va_size != VNOVAL) {
413 switch (ptyfs->ptyfs_type) {
414 case PTYFSroot:
415 return EISDIR;
416 case PTYFSpts:
417 case PTYFSptc:
418 break;
419 default:
420 return EINVAL;
421 }
422 }
423
424 if (vap->va_flags != VNOVAL) {
425 if (vp->v_mount->mnt_flag & MNT_RDONLY)
426 return EROFS;
427
428 /* Immutable and append-only flags are not supported on ptyfs. */
429 if (vap->va_flags & (IMMUTABLE | APPEND))
430 return EINVAL;
431
432 /* Snapshot flag cannot be set or cleared */
433 if ((vap->va_flags & SF_SNAPSHOT) != (ptyfs->ptyfs_flags & SF_SNAPSHOT))
434 return EPERM;
435
436 if ((ptyfs->ptyfs_flags & SF_SETTABLE) != (vap->va_flags & SF_SETTABLE)) {
437 changing_sysflags = true;
438 action |= KAUTH_VNODE_WRITE_SYSFLAGS;
439 }
440
441 error = kauth_authorize_vnode(cred, action, vp, NULL,
442 genfs_can_chflags(cred, vp->v_type, ptyfs->ptyfs_uid,
443 changing_sysflags));
444 if (error)
445 return error;
446
447 if (changing_sysflags) {
448 ptyfs->ptyfs_flags = vap->va_flags;
449 } else {
450 ptyfs->ptyfs_flags &= SF_SETTABLE;
451 ptyfs->ptyfs_flags |= (vap->va_flags & UF_SETTABLE);
452 }
453 ptyfs->ptyfs_status |= PTYFS_CHANGE;
454 }
455
456 /*
457 * Go through the fields and update iff not VNOVAL.
458 */
459 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
460 if (vp->v_mount->mnt_flag & MNT_RDONLY)
461 return EROFS;
462 if (ptyfs->ptyfs_type == PTYFSroot)
463 return EPERM;
464 error = ptyfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
465 if (error)
466 return error;
467 }
468
469 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
470 vap->va_birthtime.tv_sec != VNOVAL) {
471 if (vp->v_mount->mnt_flag & MNT_RDONLY)
472 return EROFS;
473 if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0)
474 return EPERM;
475 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
476 NULL, genfs_can_chtimes(vp, vap->va_vaflags,
477 ptyfs->ptyfs_uid, cred));
478 if (error)
479 return (error);
480 if (vap->va_atime.tv_sec != VNOVAL)
481 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
482 ptyfs->ptyfs_status |= PTYFS_ACCESS;
483 if (vap->va_mtime.tv_sec != VNOVAL) {
484 ptyfs->ptyfs_status |= PTYFS_CHANGE | PTYFS_MODIFY;
485 if (vp->v_mount->mnt_flag & MNT_RELATIME)
486 ptyfs->ptyfs_status |= PTYFS_ACCESS;
487 }
488 if (vap->va_birthtime.tv_sec != VNOVAL)
489 ptyfs->ptyfs_birthtime = vap->va_birthtime;
490 ptyfs->ptyfs_status |= PTYFS_CHANGE;
491 error = ptyfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
492 if (error)
493 return error;
494 }
495 if (vap->va_mode != (mode_t)VNOVAL) {
496 if (vp->v_mount->mnt_flag & MNT_RDONLY)
497 return EROFS;
498 if (ptyfs->ptyfs_type == PTYFSroot)
499 return EPERM;
500 if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0 &&
501 (vap->va_mode &
502 (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IXOTH|S_IWOTH)))
503 return EPERM;
504 error = ptyfs_chmod(vp, vap->va_mode, cred, l);
505 if (error)
506 return error;
507 }
508 VN_KNOTE(vp, NOTE_ATTRIB);
509 return 0;
510 }
511
512 /*
513 * Change the mode on a file.
514 * Inode must be locked before calling.
515 */
516 static int
517 ptyfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
518 {
519 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
520 int error;
521
522 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
523 NULL, genfs_can_chmod(vp->v_type, cred, ptyfs->ptyfs_uid,
524 ptyfs->ptyfs_gid, mode));
525 if (error)
526 return (error);
527
528 ptyfs->ptyfs_mode &= ~ALLPERMS;
529 ptyfs->ptyfs_mode |= (mode & ALLPERMS);
530 return 0;
531 }
532
533 /*
534 * Perform chown operation on inode ip;
535 * inode must be locked prior to call.
536 */
537 static int
538 ptyfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
539 struct lwp *l)
540 {
541 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
542 int error;
543
544 if (uid == (uid_t)VNOVAL)
545 uid = ptyfs->ptyfs_uid;
546 if (gid == (gid_t)VNOVAL)
547 gid = ptyfs->ptyfs_gid;
548
549 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
550 NULL, genfs_can_chown(cred, ptyfs->ptyfs_uid, ptyfs->ptyfs_gid,
551 uid, gid));
552 if (error)
553 return (error);
554
555 ptyfs->ptyfs_gid = gid;
556 ptyfs->ptyfs_uid = uid;
557 return 0;
558 }
559
560 /*
561 * implement access checking.
562 *
563 * actually, the check for super-user is slightly
564 * broken since it will allow read access to write-only
565 * objects. this doesn't cause any particular trouble
566 * but does mean that the i/o entry points need to check
567 * that the operation really does make sense.
568 */
569 int
570 ptyfs_access(void *v)
571 {
572 struct vop_access_args /* {
573 struct vnode *a_vp;
574 int a_mode;
575 kauth_cred_t a_cred;
576 } */ *ap = v;
577 struct vattr va;
578 int error;
579
580 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
581 return error;
582
583 return kauth_authorize_vnode(ap->a_cred,
584 KAUTH_ACCESS_ACTION(ap->a_mode, ap->a_vp->v_type, va.va_mode),
585 ap->a_vp, NULL, genfs_can_access(va.va_type, va.va_mode, va.va_uid,
586 va.va_gid, ap->a_mode, ap->a_cred));
587
588 return error;
589 }
590
591 /*
592 * lookup. this is incredibly complicated in the
593 * general case, however for most pseudo-filesystems
594 * very little needs to be done.
595 *
596 * Locking isn't hard here, just poorly documented.
597 *
598 * If we're looking up ".", just vref the parent & return it.
599 *
600 * If we're looking up "..", unlock the parent, and lock "..". If everything
601 * went ok, try to re-lock the parent. We do this to prevent lock races.
602 *
603 * For anything else, get the needed node.
604 *
605 * We try to exit with the parent locked in error cases.
606 */
607 int
608 ptyfs_lookup(void *v)
609 {
610 struct vop_lookup_v2_args /* {
611 struct vnode * a_dvp;
612 struct vnode ** a_vpp;
613 struct componentname * a_cnp;
614 } */ *ap = v;
615 struct componentname *cnp = ap->a_cnp;
616 struct vnode **vpp = ap->a_vpp;
617 struct vnode *dvp = ap->a_dvp;
618 const char *pname = cnp->cn_nameptr;
619 struct ptyfsnode *ptyfs;
620 int pty, error;
621
622 *vpp = NULL;
623
624 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
625 return EROFS;
626
627 if (cnp->cn_namelen == 1 && *pname == '.') {
628 *vpp = dvp;
629 vref(dvp);
630 return 0;
631 }
632
633 ptyfs = VTOPTYFS(dvp);
634 switch (ptyfs->ptyfs_type) {
635 case PTYFSroot:
636 /*
637 * Shouldn't get here with .. in the root node.
638 */
639 if (cnp->cn_flags & ISDOTDOT)
640 return EIO;
641
642 pty = atoi(pname, cnp->cn_namelen);
643
644 if (pty < 0 || pty >= npty || pty_isfree(pty, 1) ||
645 ptyfs_used_get(PTYFSptc, pty, dvp->v_mount, 0) == NULL)
646 break;
647
648 error = ptyfs_allocvp(dvp->v_mount, vpp, PTYFSpts, pty,
649 curlwp);
650 if (error)
651 return error;
652 VOP_UNLOCK(*vpp);
653 return 0;
654
655 default:
656 return ENOTDIR;
657 }
658
659 return cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS;
660 }
661
662 /*
663 * readdir returns directory entries from ptyfsnode (vp).
664 *
665 * the strategy here with ptyfs is to generate a single
666 * directory entry at a time (struct dirent) and then
667 * copy that out to userland using uiomove. a more efficent
668 * though more complex implementation, would try to minimize
669 * the number of calls to uiomove(). for ptyfs, this is
670 * hardly worth the added code complexity.
671 *
672 * this should just be done through read()
673 */
674 int
675 ptyfs_readdir(void *v)
676 {
677 struct vop_readdir_args /* {
678 struct vnode *a_vp;
679 struct uio *a_uio;
680 kauth_cred_t a_cred;
681 int *a_eofflag;
682 off_t **a_cookies;
683 int *a_ncookies;
684 } */ *ap = v;
685 struct uio *uio = ap->a_uio;
686 struct dirent *dp;
687 struct ptyfsnode *ptyfs;
688 off_t i;
689 int error;
690 off_t *cookies = NULL;
691 int ncookies;
692 struct vnode *vp;
693 int nc = 0;
694
695 vp = ap->a_vp;
696 ptyfs = VTOPTYFS(vp);
697
698 if (uio->uio_resid < UIO_MX)
699 return EINVAL;
700 if (uio->uio_offset < 0)
701 return EINVAL;
702
703 dp = malloc(sizeof(struct dirent), M_PTYFSTMP, M_WAITOK | M_ZERO);
704
705 error = 0;
706 i = uio->uio_offset;
707 dp->d_reclen = UIO_MX;
708 ncookies = uio->uio_resid / UIO_MX;
709
710 if (ptyfs->ptyfs_type != PTYFSroot) {
711 error = ENOTDIR;
712 goto out;
713 }
714
715 if (i >= npty)
716 goto out;
717
718 if (ap->a_ncookies) {
719 ncookies = min(ncookies, (npty + 2 - i));
720 cookies = malloc(ncookies * sizeof (off_t),
721 M_TEMP, M_WAITOK);
722 *ap->a_cookies = cookies;
723 }
724
725 for (; i < 2; i++) {
726 /* `.' and/or `..' */
727 dp->d_fileno = PTYFS_FILENO(0, PTYFSroot);
728 dp->d_namlen = i + 1;
729 (void)memcpy(dp->d_name, "..", dp->d_namlen);
730 dp->d_name[i + 1] = '\0';
731 dp->d_type = DT_DIR;
732 if ((error = uiomove(dp, UIO_MX, uio)) != 0)
733 goto out;
734 if (cookies)
735 *cookies++ = i + 1;
736 nc++;
737 }
738 for (; uio->uio_resid >= UIO_MX && i < npty; i++) {
739 /* check for used ptys */
740 if (pty_isfree(i - 2, 1) ||
741 ptyfs_used_get(PTYFSptc, i - 2, vp->v_mount, 0) == NULL)
742 continue;
743
744 dp->d_fileno = PTYFS_FILENO(i - 2, PTYFSpts);
745 dp->d_namlen = snprintf(dp->d_name, sizeof(dp->d_name),
746 "%lld", (long long)(i - 2));
747 dp->d_type = DT_CHR;
748 if ((error = uiomove(dp, UIO_MX, uio)) != 0)
749 goto out;
750 if (cookies)
751 *cookies++ = i + 1;
752 nc++;
753 }
754
755 out:
756 /* not pertinent in error cases */
757 ncookies = nc;
758
759 if (ap->a_ncookies) {
760 if (error) {
761 if (cookies)
762 free(*ap->a_cookies, M_TEMP);
763 *ap->a_ncookies = 0;
764 *ap->a_cookies = NULL;
765 } else
766 *ap->a_ncookies = ncookies;
767 }
768 uio->uio_offset = i;
769 free(dp, M_PTYFSTMP);
770 return error;
771 }
772
773 int
774 ptyfs_open(void *v)
775 {
776 struct vop_open_args /* {
777 struct vnode *a_vp;
778 int a_mode;
779 kauth_cred_t a_cred;
780 } */ *ap = v;
781 struct vnode *vp = ap->a_vp;
782 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
783
784 switch (ptyfs->ptyfs_type) {
785 case PTYFSpts:
786 case PTYFSptc:
787 return spec_open(v);
788 case PTYFSroot:
789 return 0;
790 default:
791 return EINVAL;
792 }
793 }
794
795 int
796 ptyfs_close(void *v)
797 {
798 struct vop_close_args /* {
799 struct vnode *a_vp;
800 int a_fflag;
801 kauth_cred_t a_cred;
802 } */ *ap = v;
803 struct vnode *vp = ap->a_vp;
804 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
805
806 mutex_enter(vp->v_interlock);
807 if (vp->v_usecount > 1)
808 PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
809 mutex_exit(vp->v_interlock);
810
811 switch (ptyfs->ptyfs_type) {
812 case PTYFSpts:
813 case PTYFSptc:
814 return spec_close(v);
815 case PTYFSroot:
816 return 0;
817 default:
818 return EINVAL;
819 }
820 }
821
822 int
823 ptyfs_read(void *v)
824 {
825 struct vop_read_args /* {
826 struct vnode *a_vp;
827 struct uio *a_uio;
828 int a_ioflag;
829 kauth_cred_t a_cred;
830 } */ *ap = v;
831 struct timespec ts;
832 struct vnode *vp = ap->a_vp;
833 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
834 int error;
835
836 if (vp->v_type == VDIR)
837 return EISDIR;
838
839 ptyfs->ptyfs_status |= PTYFS_ACCESS;
840 /* hardclock() resolution is good enough for ptyfs */
841 getnanotime(&ts);
842 (void)ptyfs_update(vp, &ts, &ts, 0);
843
844 switch (ptyfs->ptyfs_type) {
845 case PTYFSpts:
846 case PTYFSptc:
847 VOP_UNLOCK(vp);
848 error = cdev_read(vp->v_rdev, ap->a_uio, ap->a_ioflag);
849 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
850 return error;
851 default:
852 return EOPNOTSUPP;
853 }
854 }
855
856 int
857 ptyfs_write(void *v)
858 {
859 struct vop_write_args /* {
860 struct vnode *a_vp;
861 struct uio *a_uio;
862 int a_ioflag;
863 kauth_cred_t a_cred;
864 } */ *ap = v;
865 struct timespec ts;
866 struct vnode *vp = ap->a_vp;
867 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
868 int error;
869
870 ptyfs->ptyfs_status |= PTYFS_MODIFY;
871 getnanotime(&ts);
872 (void)ptyfs_update(vp, &ts, &ts, 0);
873
874 switch (ptyfs->ptyfs_type) {
875 case PTYFSpts:
876 case PTYFSptc:
877 VOP_UNLOCK(vp);
878 error = cdev_write(vp->v_rdev, ap->a_uio, ap->a_ioflag);
879 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
880 return error;
881 default:
882 return EOPNOTSUPP;
883 }
884 }
885
886 int
887 ptyfs_ioctl(void *v)
888 {
889 struct vop_ioctl_args /* {
890 struct vnode *a_vp;
891 u_long a_command;
892 void *a_data;
893 int a_fflag;
894 kauth_cred_t a_cred;
895 } */ *ap = v;
896 struct vnode *vp = ap->a_vp;
897 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
898
899 switch (ptyfs->ptyfs_type) {
900 case PTYFSpts:
901 case PTYFSptc:
902 return cdev_ioctl(vp->v_rdev, ap->a_command,
903 ap->a_data, ap->a_fflag, curlwp);
904 default:
905 return EOPNOTSUPP;
906 }
907 }
908
909 int
910 ptyfs_poll(void *v)
911 {
912 struct vop_poll_args /* {
913 struct vnode *a_vp;
914 int a_events;
915 } */ *ap = v;
916 struct vnode *vp = ap->a_vp;
917 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
918
919 switch (ptyfs->ptyfs_type) {
920 case PTYFSpts:
921 case PTYFSptc:
922 return cdev_poll(vp->v_rdev, ap->a_events, curlwp);
923 default:
924 return genfs_poll(v);
925 }
926 }
927
928 int
929 ptyfs_kqfilter(void *v)
930 {
931 struct vop_kqfilter_args /* {
932 struct vnode *a_vp;
933 struct knote *a_kn;
934 } */ *ap = v;
935 struct vnode *vp = ap->a_vp;
936 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
937
938 switch (ptyfs->ptyfs_type) {
939 case PTYFSpts:
940 case PTYFSptc:
941 return cdev_kqfilter(vp->v_rdev, ap->a_kn);
942 default:
943 return genfs_kqfilter(v);
944 }
945 }
946
947 static int
948 ptyfs_update(struct vnode *vp, const struct timespec *acc,
949 const struct timespec *mod, int flags)
950 {
951 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
952
953 if (vp->v_mount->mnt_flag & MNT_RDONLY)
954 return 0;
955
956 PTYFS_ITIMES(ptyfs, acc, mod, NULL);
957 return 0;
958 }
959
960 void
961 ptyfs_itimes(struct ptyfsnode *ptyfs, const struct timespec *acc,
962 const struct timespec *mod, const struct timespec *cre)
963 {
964 struct timespec now;
965
966 KASSERT(ptyfs->ptyfs_status & (PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY));
967
968 getnanotime(&now);
969 if (ptyfs->ptyfs_status & PTYFS_ACCESS) {
970 if (acc == NULL)
971 acc = &now;
972 ptyfs->ptyfs_atime = *acc;
973 }
974 if (ptyfs->ptyfs_status & PTYFS_MODIFY) {
975 if (mod == NULL)
976 mod = &now;
977 ptyfs->ptyfs_mtime = *mod;
978 }
979 if (ptyfs->ptyfs_status & PTYFS_CHANGE) {
980 if (cre == NULL)
981 cre = &now;
982 ptyfs->ptyfs_ctime = *cre;
983 }
984 ptyfs->ptyfs_status &= ~(PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY);
985 }
986
987 /*
988 * convert decimal ascii to int
989 */
990 static int
991 atoi(const char *b, size_t len)
992 {
993 int p = 0;
994
995 while (len--) {
996 char c = *b++;
997 if (c < '0' || c > '9')
998 return -1;
999 p = 10 * p + (c - '0');
1000 }
1001
1002 return p;
1003 }
1004