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