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