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