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