ptyfs_vnops.c revision 1.64 1 /* $NetBSD: ptyfs_vnops.c,v 1.64 2021/06/29 22:34:07 dholland 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.64 2021/06/29 22:34:07 dholland 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_eopnotsupp
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_parsepath_desc, genfs_parsepath }, /* parsepath */
170 { &vop_lookup_desc, ptyfs_lookup }, /* lookup */
171 { &vop_create_desc, ptyfs_create }, /* create */
172 { &vop_mknod_desc, ptyfs_mknod }, /* mknod */
173 { &vop_open_desc, ptyfs_open }, /* open */
174 { &vop_close_desc, ptyfs_close }, /* close */
175 { &vop_access_desc, ptyfs_access }, /* access */
176 { &vop_accessx_desc, genfs_accessx }, /* accessx */
177 { &vop_getattr_desc, ptyfs_getattr }, /* getattr */
178 { &vop_setattr_desc, ptyfs_setattr }, /* setattr */
179 { &vop_read_desc, ptyfs_read }, /* read */
180 { &vop_write_desc, ptyfs_write }, /* write */
181 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
182 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
183 { &vop_ioctl_desc, ptyfs_ioctl }, /* ioctl */
184 { &vop_fcntl_desc, ptyfs_fcntl }, /* fcntl */
185 { &vop_poll_desc, ptyfs_poll }, /* poll */
186 { &vop_kqfilter_desc, ptyfs_kqfilter }, /* kqfilter */
187 { &vop_revoke_desc, ptyfs_revoke }, /* revoke */
188 { &vop_mmap_desc, ptyfs_mmap }, /* mmap */
189 { &vop_fsync_desc, ptyfs_fsync }, /* fsync */
190 { &vop_seek_desc, ptyfs_seek }, /* seek */
191 { &vop_remove_desc, ptyfs_remove }, /* remove */
192 { &vop_link_desc, ptyfs_link }, /* link */
193 { &vop_rename_desc, ptyfs_rename }, /* rename */
194 { &vop_mkdir_desc, ptyfs_mkdir }, /* mkdir */
195 { &vop_rmdir_desc, ptyfs_rmdir }, /* rmdir */
196 { &vop_symlink_desc, ptyfs_symlink }, /* symlink */
197 { &vop_readdir_desc, ptyfs_readdir }, /* readdir */
198 { &vop_readlink_desc, ptyfs_readlink }, /* readlink */
199 { &vop_abortop_desc, ptyfs_abortop }, /* abortop */
200 { &vop_inactive_desc, ptyfs_inactive }, /* inactive */
201 { &vop_reclaim_desc, ptyfs_reclaim }, /* reclaim */
202 { &vop_lock_desc, ptyfs_lock }, /* lock */
203 { &vop_unlock_desc, ptyfs_unlock }, /* unlock */
204 { &vop_bmap_desc, ptyfs_bmap }, /* bmap */
205 { &vop_strategy_desc, ptyfs_strategy }, /* strategy */
206 { &vop_print_desc, ptyfs_print }, /* print */
207 { &vop_islocked_desc, ptyfs_islocked }, /* islocked */
208 { &vop_pathconf_desc, ptyfs_pathconf }, /* pathconf */
209 { &vop_advlock_desc, ptyfs_advlock }, /* advlock */
210 { &vop_bwrite_desc, ptyfs_bwrite }, /* bwrite */
211 { &vop_putpages_desc, ptyfs_putpages }, /* putpages */
212 { NULL, NULL }
213 };
214 const struct vnodeopv_desc ptyfs_vnodeop_opv_desc =
215 { &ptyfs_vnodeop_p, ptyfs_vnodeop_entries };
216
217 /*
218 * free any private data and remove the node
219 * from any private lists.
220 */
221 int
222 ptyfs_reclaim(void *v)
223 {
224 struct vop_reclaim_v2_args /* {
225 struct vnode *a_vp;
226 } */ *ap = v;
227 struct vnode *vp = ap->a_vp;
228
229 VOP_UNLOCK(vp);
230
231 vp->v_data = NULL;
232 return 0;
233 }
234
235 int
236 ptyfs_inactive(void *v)
237 {
238 struct vop_inactive_v2_args /* {
239 struct vnode *a_vp;
240 bool *a_recycle;
241 } */ *ap = v;
242 struct vnode *vp = ap->a_vp;
243 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
244
245 if (ptyfs->ptyfs_type == PTYFSptc)
246 ptyfs_clr_active(vp->v_mount, ptyfs->ptyfs_pty);
247
248 return 0;
249 }
250
251 /*
252 * Return POSIX pathconf information applicable to special devices.
253 */
254 int
255 ptyfs_pathconf(void *v)
256 {
257 struct vop_pathconf_args /* {
258 struct vnode *a_vp;
259 int a_name;
260 register_t *a_retval;
261 } */ *ap = v;
262
263 switch (ap->a_name) {
264 case _PC_LINK_MAX:
265 *ap->a_retval = LINK_MAX;
266 return 0;
267 case _PC_MAX_CANON:
268 *ap->a_retval = MAX_CANON;
269 return 0;
270 case _PC_MAX_INPUT:
271 *ap->a_retval = MAX_INPUT;
272 return 0;
273 case _PC_PIPE_BUF:
274 *ap->a_retval = PIPE_BUF;
275 return 0;
276 case _PC_CHOWN_RESTRICTED:
277 *ap->a_retval = 1;
278 return 0;
279 case _PC_VDISABLE:
280 *ap->a_retval = _POSIX_VDISABLE;
281 return 0;
282 case _PC_SYNC_IO:
283 *ap->a_retval = 1;
284 return 0;
285 default:
286 return genfs_pathconf(ap);
287 }
288 }
289
290 /*
291 * _print is used for debugging.
292 * just print a readable description
293 * of (vp).
294 */
295 int
296 ptyfs_print(void *v)
297 {
298 struct vop_print_args /* {
299 struct vnode *a_vp;
300 } */ *ap = v;
301 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
302
303 printf("tag VT_PTYFS, type %d, pty %d\n",
304 ptyfs->ptyfs_type, ptyfs->ptyfs_pty);
305 return 0;
306 }
307
308 /*
309 * support advisory locking on pty nodes
310 */
311 int
312 ptyfs_advlock(void *v)
313 {
314 struct vop_print_args /* {
315 struct vnode *a_vp;
316 } */ *ap = v;
317 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
318
319 switch (ptyfs->ptyfs_type) {
320 case PTYFSpts:
321 case PTYFSptc:
322 return spec_advlock(v);
323 default:
324 return EOPNOTSUPP;
325 }
326 }
327
328 /*
329 * Invent attributes for ptyfsnode (vp) and store
330 * them in (vap).
331 * Directories lengths are returned as zero since
332 * any real length would require the genuine size
333 * to be computed, and nothing cares anyway.
334 *
335 * this is relatively minimal for ptyfs.
336 */
337 int
338 ptyfs_getattr(void *v)
339 {
340 struct vop_getattr_args /* {
341 struct vnode *a_vp;
342 struct vattr *a_vap;
343 kauth_cred_t a_cred;
344 } */ *ap = v;
345 struct ptyfsnode *ptyfs = VTOPTYFS(ap->a_vp);
346 struct vattr *vap = ap->a_vap;
347
348 PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
349
350 /* start by zeroing out the attributes */
351 vattr_null(vap);
352
353 /* next do all the common fields */
354 vap->va_type = ap->a_vp->v_type;
355 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
356 vap->va_fileid = ptyfs->ptyfs_fileno;
357 vap->va_gen = 0;
358 vap->va_flags = 0;
359 vap->va_blocksize = PAGE_SIZE;
360
361 vap->va_atime = ptyfs->ptyfs_atime;
362 vap->va_mtime = ptyfs->ptyfs_mtime;
363 vap->va_ctime = ptyfs->ptyfs_ctime;
364 vap->va_birthtime = ptyfs->ptyfs_birthtime;
365 vap->va_mode = ptyfs->ptyfs_mode;
366 vap->va_flags = ptyfs->ptyfs_flags;
367 vap->va_uid = ptyfs->ptyfs_uid;
368 vap->va_gid = ptyfs->ptyfs_gid;
369
370 switch (ptyfs->ptyfs_type) {
371 case PTYFSpts:
372 case PTYFSptc:
373 if (pty_isfree(ptyfs->ptyfs_pty, 1))
374 return ENOENT;
375 vap->va_bytes = vap->va_size = 0;
376 vap->va_rdev = ap->a_vp->v_rdev;
377 vap->va_nlink = 1;
378 break;
379 case PTYFSroot:
380 vap->va_rdev = 0;
381 vap->va_bytes = vap->va_size = DEV_BSIZE;
382 vap->va_nlink = 2;
383 break;
384 default:
385 return EOPNOTSUPP;
386 }
387
388 return 0;
389 }
390
391 /*ARGSUSED*/
392 int
393 ptyfs_setattr(void *v)
394 {
395 struct vop_setattr_args /* {
396 struct vnodeop_desc *a_desc;
397 struct vnode *a_vp;
398 struct vattr *a_vap;
399 kauth_cred_t a_cred;
400 } */ *ap = v;
401 struct vnode *vp = ap->a_vp;
402 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
403 struct vattr *vap = ap->a_vap;
404 kauth_cred_t cred = ap->a_cred;
405 struct lwp *l = curlwp;
406 int error;
407 kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
408 bool changing_sysflags = false;
409
410 if (vap->va_size != VNOVALSIZE) {
411 switch (ptyfs->ptyfs_type) {
412 case PTYFSroot:
413 return EISDIR;
414 case PTYFSpts:
415 case PTYFSptc:
416 break;
417 default:
418 return EINVAL;
419 }
420 }
421
422 if (vap->va_flags != VNOVALFLAGS) {
423 if (vp->v_mount->mnt_flag & MNT_RDONLY)
424 return EROFS;
425
426 /* Immutable and append-only flags are not supported on ptyfs. */
427 if (vap->va_flags & (IMMUTABLE | APPEND))
428 return EINVAL;
429
430 /* Snapshot flag cannot be set or cleared */
431 if ((vap->va_flags & SF_SNAPSHOT) != (ptyfs->ptyfs_flags & SF_SNAPSHOT))
432 return EPERM;
433
434 if ((ptyfs->ptyfs_flags & SF_SETTABLE) != (vap->va_flags & SF_SETTABLE)) {
435 changing_sysflags = true;
436 action |= KAUTH_VNODE_WRITE_SYSFLAGS;
437 }
438
439 error = kauth_authorize_vnode(cred, action, vp, NULL,
440 genfs_can_chflags(vp, cred, ptyfs->ptyfs_uid,
441 changing_sysflags));
442 if (error)
443 return error;
444
445 if (changing_sysflags) {
446 ptyfs->ptyfs_flags = vap->va_flags;
447 } else {
448 ptyfs->ptyfs_flags &= SF_SETTABLE;
449 ptyfs->ptyfs_flags |= (vap->va_flags & UF_SETTABLE);
450 }
451 ptyfs->ptyfs_status |= PTYFS_CHANGE;
452 }
453
454 /*
455 * Go through the fields and update iff not VNOVAL.
456 */
457 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
458 if (vp->v_mount->mnt_flag & MNT_RDONLY)
459 return EROFS;
460 error = ptyfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
461 if (error)
462 return error;
463 }
464
465 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
466 vap->va_birthtime.tv_sec != VNOVAL) {
467 if (vp->v_mount->mnt_flag & MNT_RDONLY)
468 return EROFS;
469 if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0)
470 return EPERM;
471 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
472 NULL, genfs_can_chtimes(vp, cred, ptyfs->ptyfs_uid,
473 vap->va_vaflags));
474 if (error)
475 return (error);
476 if (vap->va_atime.tv_sec != VNOVAL)
477 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
478 ptyfs->ptyfs_status |= PTYFS_ACCESS;
479 if (vap->va_mtime.tv_sec != VNOVAL) {
480 ptyfs->ptyfs_status |= PTYFS_CHANGE | PTYFS_MODIFY;
481 if (vp->v_mount->mnt_flag & MNT_RELATIME)
482 ptyfs->ptyfs_status |= PTYFS_ACCESS;
483 }
484 if (vap->va_birthtime.tv_sec != VNOVAL)
485 ptyfs->ptyfs_birthtime = vap->va_birthtime;
486 ptyfs->ptyfs_status |= PTYFS_CHANGE;
487 error = ptyfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
488 if (error)
489 return error;
490 }
491 if (vap->va_mode != (mode_t)VNOVAL) {
492 if (vp->v_mount->mnt_flag & MNT_RDONLY)
493 return EROFS;
494 if ((ptyfs->ptyfs_flags & SF_SNAPSHOT) != 0 &&
495 (vap->va_mode &
496 (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IXOTH|S_IWOTH)))
497 return EPERM;
498 error = ptyfs_chmod(vp, vap->va_mode, cred, l);
499 if (error)
500 return error;
501 }
502 VN_KNOTE(vp, NOTE_ATTRIB);
503 return 0;
504 }
505
506 /*
507 * Change the mode on a file.
508 * Inode must be locked before calling.
509 */
510 static int
511 ptyfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
512 {
513 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
514 int error;
515
516 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
517 NULL, genfs_can_chmod(vp, cred, ptyfs->ptyfs_uid, ptyfs->ptyfs_gid,
518 mode));
519 if (error)
520 return (error);
521
522 ptyfs->ptyfs_mode &= ~ALLPERMS;
523 ptyfs->ptyfs_mode |= (mode & ALLPERMS);
524 return 0;
525 }
526
527 /*
528 * Perform chown operation on inode ip;
529 * inode must be locked prior to call.
530 */
531 static int
532 ptyfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
533 struct lwp *l)
534 {
535 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
536 int error;
537
538 if (uid == (uid_t)VNOVAL)
539 uid = ptyfs->ptyfs_uid;
540 if (gid == (gid_t)VNOVAL)
541 gid = ptyfs->ptyfs_gid;
542
543 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
544 NULL, genfs_can_chown(vp, cred, ptyfs->ptyfs_uid, ptyfs->ptyfs_gid,
545 uid, gid));
546 if (error)
547 return (error);
548
549 ptyfs->ptyfs_gid = gid;
550 ptyfs->ptyfs_uid = uid;
551 return 0;
552 }
553
554 /*
555 * implement access checking.
556 *
557 * actually, the check for super-user is slightly
558 * broken since it will allow read access to write-only
559 * objects. this doesn't cause any particular trouble
560 * but does mean that the i/o entry points need to check
561 * that the operation really does make sense.
562 */
563 int
564 ptyfs_access(void *v)
565 {
566 struct vop_access_args /* {
567 struct vnode *a_vp;
568 accmode_t a_accmode;
569 kauth_cred_t a_cred;
570 } */ *ap = v;
571 struct vattr va;
572 int error;
573
574 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
575 return error;
576
577 return kauth_authorize_vnode(ap->a_cred,
578 KAUTH_ACCESS_ACTION(ap->a_accmode, ap->a_vp->v_type, va.va_mode),
579 ap->a_vp, NULL, genfs_can_access(ap->a_vp, ap->a_cred, va.va_uid,
580 va.va_gid, va.va_mode, NULL, ap->a_accmode));
581 }
582
583 /*
584 * lookup. this is incredibly complicated in the
585 * general case, however for most pseudo-filesystems
586 * very little needs to be done.
587 *
588 * Locking isn't hard here, just poorly documented.
589 *
590 * If we're looking up ".", just vref the parent & return it.
591 *
592 * If we're looking up "..", unlock the parent, and lock "..". If everything
593 * went ok, try to re-lock the parent. We do this to prevent lock races.
594 *
595 * For anything else, get the needed node.
596 *
597 * We try to exit with the parent locked in error cases.
598 */
599 int
600 ptyfs_lookup(void *v)
601 {
602 struct vop_lookup_v2_args /* {
603 struct vnode * a_dvp;
604 struct vnode ** a_vpp;
605 struct componentname * a_cnp;
606 } */ *ap = v;
607 struct componentname *cnp = ap->a_cnp;
608 struct vnode **vpp = ap->a_vpp;
609 struct vnode *dvp = ap->a_dvp;
610 const char *pname = cnp->cn_nameptr;
611 struct ptyfsnode *ptyfs;
612 int pty, error;
613
614 *vpp = NULL;
615
616 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
617 return EROFS;
618
619 if (cnp->cn_namelen == 1 && *pname == '.') {
620 *vpp = dvp;
621 vref(dvp);
622 return 0;
623 }
624
625 ptyfs = VTOPTYFS(dvp);
626 switch (ptyfs->ptyfs_type) {
627 case PTYFSroot:
628 /*
629 * Shouldn't get here with .. in the root node.
630 */
631 if (cnp->cn_flags & ISDOTDOT)
632 return EIO;
633
634 pty = atoi(pname, cnp->cn_namelen);
635 if (pty < 0 || ptyfs_next_active(dvp->v_mount, pty) != pty)
636 break;
637 error = ptyfs_allocvp(dvp->v_mount, vpp, PTYFSpts, pty);
638 if (error)
639 return error;
640 if (ptyfs_next_active(dvp->v_mount, pty) != pty) {
641 vrele(*vpp);
642 *vpp = NULL;
643 return ENOENT;
644 }
645 return 0;
646
647 default:
648 return ENOTDIR;
649 }
650
651 return cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS;
652 }
653
654 /*
655 * readdir returns directory entries from ptyfsnode (vp).
656 *
657 * the strategy here with ptyfs is to generate a single
658 * directory entry at a time (struct dirent) and then
659 * copy that out to userland using uiomove. a more efficent
660 * though more complex implementation, would try to minimize
661 * the number of calls to uiomove(). for ptyfs, this is
662 * hardly worth the added code complexity.
663 *
664 * this should just be done through read()
665 */
666 int
667 ptyfs_readdir(void *v)
668 {
669 struct vop_readdir_args /* {
670 struct vnode *a_vp;
671 struct uio *a_uio;
672 kauth_cred_t a_cred;
673 int *a_eofflag;
674 off_t **a_cookies;
675 int *a_ncookies;
676 } */ *ap = v;
677 struct uio *uio = ap->a_uio;
678 struct dirent *dp;
679 struct ptyfsnode *ptyfs;
680 off_t i;
681 int error;
682 off_t *cookies = NULL;
683 int ncookies;
684 struct vnode *vp;
685 int n, nc = 0;
686
687 vp = ap->a_vp;
688 ptyfs = VTOPTYFS(vp);
689
690 if (uio->uio_resid < UIO_MX)
691 return EINVAL;
692 if (uio->uio_offset < 0)
693 return EINVAL;
694
695 dp = malloc(sizeof(struct dirent), M_PTYFSTMP, M_WAITOK | M_ZERO);
696
697 error = 0;
698 i = uio->uio_offset;
699 dp->d_reclen = UIO_MX;
700 ncookies = uio->uio_resid / UIO_MX;
701
702 if (ptyfs->ptyfs_type != PTYFSroot) {
703 error = ENOTDIR;
704 goto out;
705 }
706
707 if (i >= npty)
708 goto out;
709
710 if (ap->a_ncookies) {
711 ncookies = uimin(ncookies, (npty + 2 - i));
712 cookies = malloc(ncookies * sizeof (off_t),
713 M_TEMP, M_WAITOK);
714 *ap->a_cookies = cookies;
715 }
716
717 for (; i < 2; i++) {
718 /* `.' and/or `..' */
719 dp->d_fileno = PTYFS_FILENO(PTYFSroot, 0);
720 dp->d_namlen = i + 1;
721 (void)memcpy(dp->d_name, "..", dp->d_namlen);
722 dp->d_name[i + 1] = '\0';
723 dp->d_type = DT_DIR;
724 if ((error = uiomove(dp, UIO_MX, uio)) != 0)
725 goto out;
726 if (cookies)
727 *cookies++ = i + 1;
728 nc++;
729 }
730 while (uio->uio_resid >= UIO_MX) {
731 /* check for used ptys */
732 n = ptyfs_next_active(vp->v_mount, i - 2);
733 if (n < 0)
734 break;
735 dp->d_fileno = PTYFS_FILENO(PTYFSpts, n);
736 dp->d_namlen = snprintf(dp->d_name, sizeof(dp->d_name),
737 "%lld", (long long)(n));
738 dp->d_type = DT_CHR;
739 if ((error = uiomove(dp, UIO_MX, uio)) != 0)
740 goto out;
741 i = n + 3;
742 if (cookies)
743 *cookies++ = i;
744 nc++;
745 }
746
747 out:
748 /* not pertinent in error cases */
749 ncookies = nc;
750
751 if (ap->a_ncookies) {
752 if (error) {
753 if (cookies)
754 free(*ap->a_cookies, M_TEMP);
755 *ap->a_ncookies = 0;
756 *ap->a_cookies = NULL;
757 } else
758 *ap->a_ncookies = ncookies;
759 }
760 uio->uio_offset = i;
761 free(dp, M_PTYFSTMP);
762 return error;
763 }
764
765 int
766 ptyfs_open(void *v)
767 {
768 struct vop_open_args /* {
769 struct vnode *a_vp;
770 int a_mode;
771 kauth_cred_t a_cred;
772 } */ *ap = v;
773 struct vnode *vp = ap->a_vp;
774 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
775
776 switch (ptyfs->ptyfs_type) {
777 case PTYFSpts:
778 case PTYFSptc:
779 return spec_open(v);
780 case PTYFSroot:
781 return 0;
782 default:
783 return EINVAL;
784 }
785 }
786
787 int
788 ptyfs_close(void *v)
789 {
790 struct vop_close_args /* {
791 struct vnode *a_vp;
792 int a_fflag;
793 kauth_cred_t a_cred;
794 } */ *ap = v;
795 struct vnode *vp = ap->a_vp;
796 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
797
798 mutex_enter(vp->v_interlock);
799 if (vrefcnt(vp) > 1)
800 PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
801 mutex_exit(vp->v_interlock);
802
803 switch (ptyfs->ptyfs_type) {
804 case PTYFSpts:
805 case PTYFSptc:
806 return spec_close(v);
807 case PTYFSroot:
808 return 0;
809 default:
810 return EINVAL;
811 }
812 }
813
814 int
815 ptyfs_read(void *v)
816 {
817 struct vop_read_args /* {
818 struct vnode *a_vp;
819 struct uio *a_uio;
820 int a_ioflag;
821 kauth_cred_t a_cred;
822 } */ *ap = v;
823 struct timespec ts;
824 struct vnode *vp = ap->a_vp;
825 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
826 int error;
827
828 if (vp->v_type == VDIR)
829 return EISDIR;
830
831 ptyfs->ptyfs_status |= PTYFS_ACCESS;
832 /* hardclock() resolution is good enough for ptyfs */
833 getnanotime(&ts);
834 (void)ptyfs_update(vp, &ts, &ts, 0);
835
836 switch (ptyfs->ptyfs_type) {
837 case PTYFSpts:
838 case PTYFSptc:
839 VOP_UNLOCK(vp);
840 error = cdev_read(vp->v_rdev, ap->a_uio, ap->a_ioflag);
841 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
842 return error;
843 default:
844 return EOPNOTSUPP;
845 }
846 }
847
848 int
849 ptyfs_write(void *v)
850 {
851 struct vop_write_args /* {
852 struct vnode *a_vp;
853 struct uio *a_uio;
854 int a_ioflag;
855 kauth_cred_t a_cred;
856 } */ *ap = v;
857 struct timespec ts;
858 struct vnode *vp = ap->a_vp;
859 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
860 int error;
861
862 ptyfs->ptyfs_status |= PTYFS_MODIFY;
863 getnanotime(&ts);
864 (void)ptyfs_update(vp, &ts, &ts, 0);
865
866 switch (ptyfs->ptyfs_type) {
867 case PTYFSpts:
868 case PTYFSptc:
869 VOP_UNLOCK(vp);
870 error = cdev_write(vp->v_rdev, ap->a_uio, ap->a_ioflag);
871 vn_lock(vp, LK_RETRY|LK_EXCLUSIVE);
872 return error;
873 default:
874 return EOPNOTSUPP;
875 }
876 }
877
878 int
879 ptyfs_ioctl(void *v)
880 {
881 struct vop_ioctl_args /* {
882 struct vnode *a_vp;
883 u_long a_command;
884 void *a_data;
885 int a_fflag;
886 kauth_cred_t a_cred;
887 } */ *ap = v;
888 struct vnode *vp = ap->a_vp;
889 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
890
891 switch (ptyfs->ptyfs_type) {
892 case PTYFSpts:
893 case PTYFSptc:
894 return cdev_ioctl(vp->v_rdev, ap->a_command,
895 ap->a_data, ap->a_fflag, curlwp);
896 default:
897 return EOPNOTSUPP;
898 }
899 }
900
901 int
902 ptyfs_poll(void *v)
903 {
904 struct vop_poll_args /* {
905 struct vnode *a_vp;
906 int a_events;
907 } */ *ap = v;
908 struct vnode *vp = ap->a_vp;
909 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
910
911 switch (ptyfs->ptyfs_type) {
912 case PTYFSpts:
913 case PTYFSptc:
914 return cdev_poll(vp->v_rdev, ap->a_events, curlwp);
915 default:
916 return genfs_poll(v);
917 }
918 }
919
920 int
921 ptyfs_kqfilter(void *v)
922 {
923 struct vop_kqfilter_args /* {
924 struct vnode *a_vp;
925 struct knote *a_kn;
926 } */ *ap = v;
927 struct vnode *vp = ap->a_vp;
928 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
929
930 switch (ptyfs->ptyfs_type) {
931 case PTYFSpts:
932 case PTYFSptc:
933 return cdev_kqfilter(vp->v_rdev, ap->a_kn);
934 default:
935 return genfs_kqfilter(v);
936 }
937 }
938
939 static int
940 ptyfs_update(struct vnode *vp, const struct timespec *acc,
941 const struct timespec *mod, int flags)
942 {
943 struct ptyfsnode *ptyfs = VTOPTYFS(vp);
944
945 if (vp->v_mount->mnt_flag & MNT_RDONLY)
946 return 0;
947
948 PTYFS_ITIMES(ptyfs, acc, mod, NULL);
949 return 0;
950 }
951
952 void
953 ptyfs_itimes(struct ptyfsnode *ptyfs, const struct timespec *acc,
954 const struct timespec *mod, const struct timespec *cre)
955 {
956 struct timespec now;
957
958 KASSERT(ptyfs->ptyfs_status & (PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY));
959
960 getnanotime(&now);
961 if (ptyfs->ptyfs_status & PTYFS_ACCESS) {
962 if (acc == NULL)
963 acc = &now;
964 ptyfs->ptyfs_atime = *acc;
965 }
966 if (ptyfs->ptyfs_status & PTYFS_MODIFY) {
967 if (mod == NULL)
968 mod = &now;
969 ptyfs->ptyfs_mtime = *mod;
970 }
971 if (ptyfs->ptyfs_status & PTYFS_CHANGE) {
972 if (cre == NULL)
973 cre = &now;
974 ptyfs->ptyfs_ctime = *cre;
975 }
976 ptyfs->ptyfs_status &= ~(PTYFS_ACCESS|PTYFS_CHANGE|PTYFS_MODIFY);
977 }
978
979 /*
980 * convert decimal ascii to int
981 */
982 static int
983 atoi(const char *b, size_t len)
984 {
985 int p = 0;
986
987 while (len--) {
988 char c = *b++;
989 if (c < '0' || c > '9')
990 return -1;
991 p = 10 * p + (c - '0');
992 }
993
994 return p;
995 }
996