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