Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_vfsops.c revision 1.15
      1 /*	$NetBSD: ptyfs_vfsops.c,v 1.15 2006/05/14 21:31:52 elad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software donated 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  */
     35 
     36 /*
     37  * Pseudo-tty Filesystem
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vfsops.c,v 1.15 2006/05/14 21:31:52 elad Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/sysctl.h>
     46 #include <sys/conf.h>
     47 #include <sys/proc.h>
     48 #include <sys/vnode.h>
     49 #include <sys/mount.h>
     50 #include <sys/namei.h>
     51 #include <sys/stat.h>
     52 #include <sys/dirent.h>
     53 #include <sys/malloc.h>
     54 #include <sys/syslog.h>
     55 #include <sys/select.h>
     56 #include <sys/filedesc.h>
     57 #include <sys/tty.h>
     58 #include <sys/pty.h>
     59 #include <sys/kauth.h>
     60 
     61 #include <fs/ptyfs/ptyfs.h>
     62 #include <miscfs/specfs/specdev.h>
     63 
     64 MALLOC_DEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
     65 
     66 void	ptyfs_init(void);
     67 void	ptyfs_reinit(void);
     68 void	ptyfs_done(void);
     69 int	ptyfs_mount(struct mount *, const char *, void *, struct nameidata *,
     70     struct lwp *);
     71 int	ptyfs_start(struct mount *, int, struct lwp *);
     72 int	ptyfs_unmount(struct mount *, int, struct lwp *);
     73 int	ptyfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
     74 int	ptyfs_quotactl(struct mount *, int, uid_t, void *, struct lwp *);
     75 int	ptyfs_sync(struct mount *, int, kauth_cred_t, struct lwp *);
     76 int	ptyfs_vget(struct mount *, ino_t, struct vnode **);
     77 
     78 static int ptyfs__allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
     79     dev_t, char);
     80 static int ptyfs__makename(struct ptm_pty *, struct lwp *, char *, size_t,
     81     dev_t, char);
     82 static void ptyfs__getvattr(struct ptm_pty *, struct proc *, struct vattr *);
     83 
     84 /*
     85  * ptm glue: When we mount, we make ptm point to us.
     86  */
     87 struct ptm_pty *ptyfs_save_ptm;
     88 static int ptyfs_count;
     89 
     90 struct ptm_pty ptm_ptyfspty = {
     91 	ptyfs__allocvp,
     92 	ptyfs__makename,
     93 	ptyfs__getvattr,
     94 	NULL
     95 };
     96 
     97 static const char *
     98 ptyfs__getpath(struct lwp *l, const struct mount *mp)
     99 {
    100 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    101 	char buf[sizeof(mp->mnt_stat.f_mntonname) + 32];
    102 	size_t len;
    103 	char *bp;
    104 	int error;
    105 
    106 	if (cwdi->cwdi_rdir == NULL)
    107 		return mp->mnt_stat.f_mntonname;
    108 
    109 	bp = buf + sizeof(buf);
    110 	*--bp = '\0';
    111 	error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp,
    112 	    buf, sizeof(buf) / 2, 0, l);
    113 	if (error)	/* XXX */
    114 		return mp->mnt_stat.f_mntonname;
    115 
    116 	len = strlen(bp);
    117 	if (len >= sizeof(mp->mnt_stat.f_mntonname))	/* XXX */
    118 		return mp->mnt_stat.f_mntonname;
    119 	else
    120 		return &mp->mnt_stat.f_mntonname[len];
    121 }
    122 
    123 static int
    124 ptyfs__makename(struct ptm_pty *pt, struct lwp *l, char *tbuf, size_t bufsiz,
    125     dev_t dev, char ms)
    126 {
    127 	struct mount *mp = pt->arg;
    128 	size_t len;
    129 
    130 	switch (ms) {
    131 	case 'p':
    132 		/* We don't provide access to the master, should we? */
    133 		len = snprintf(tbuf, bufsiz, "/dev/null");
    134 		break;
    135 	case 't':
    136 		len = snprintf(tbuf, bufsiz, "%s/%d", ptyfs__getpath(l, mp),
    137 		    minor(dev));
    138 		break;
    139 	default:
    140 		return EINVAL;
    141 	}
    142 
    143 	return len >= bufsiz ? ENOSPC : 0;
    144 }
    145 
    146 
    147 static int
    148 /*ARGSUSED*/
    149 ptyfs__allocvp(struct ptm_pty *pt, struct lwp *l, struct vnode **vpp,
    150     dev_t dev, char ms)
    151 {
    152 	struct mount *mp = pt->arg;
    153 	ptyfstype type;
    154 
    155 	switch (ms) {
    156 	case 'p':
    157 		type = PTYFSptc;
    158 		break;
    159 	case 't':
    160 		type = PTYFSpts;
    161 		break;
    162 	default:
    163 		return EINVAL;
    164 	}
    165 
    166 	return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
    167 }
    168 
    169 
    170 static void
    171 ptyfs__getvattr(struct ptm_pty *pt, struct proc *p, struct vattr *vattr)
    172 {
    173 	struct mount *mp = pt->arg;
    174 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
    175 	VATTR_NULL(vattr);
    176 	/* get real uid */
    177 	vattr->va_uid = kauth_cred_getuid(p->p_cred);
    178 	vattr->va_gid = pmnt->pmnt_gid;
    179 	vattr->va_mode = pmnt->pmnt_mode;
    180 }
    181 
    182 
    183 void
    184 ptyfs_init(void)
    185 {
    186 #ifdef _LKM
    187 	malloc_type_attach(M_PTYFSMNT);
    188 #endif
    189 	ptyfs_hashinit();
    190 }
    191 
    192 void
    193 ptyfs_reinit(void)
    194 {
    195 	ptyfs_hashreinit();
    196 }
    197 
    198 void
    199 ptyfs_done(void)
    200 {
    201 #ifdef _LKM
    202 	malloc_type_detach(M_PTYFSMNT);
    203 #endif
    204 	ptyfs_hashdone();
    205 }
    206 
    207 /*
    208  * Mount the Pseudo tty params filesystem
    209  */
    210 int
    211 ptyfs_mount(struct mount *mp, const char *path, void *data,
    212     struct nameidata *ndp, struct lwp *l)
    213 {
    214 	int error = 0;
    215 	struct ptyfsmount *pmnt;
    216 	struct ptyfs_args args;
    217 
    218 	if (UIO_MX & (UIO_MX - 1)) {
    219 		log(LOG_ERR, "ptyfs: invalid directory entry size");
    220 		return EINVAL;
    221 	}
    222 
    223 	if (mp->mnt_flag & MNT_GETARGS) {
    224 		pmnt = VFSTOPTY(mp);
    225 		if (pmnt == NULL)
    226 			return EIO;
    227 		args.version = PTYFS_ARGSVERSION;
    228 		args.mode = pmnt->pmnt_mode;
    229 		args.gid = pmnt->pmnt_gid;
    230 		return copyout(&args, data, sizeof(args));
    231 	}
    232 
    233 	/* Don't allow more than one mount */
    234 	if (ptyfs_count)
    235 		return EBUSY;
    236 
    237 	if (mp->mnt_flag & MNT_UPDATE)
    238 		return EOPNOTSUPP;
    239 
    240 	if (data != NULL) {
    241 		error = copyin(data, &args, sizeof args);
    242 		if (error != 0)
    243 			return error;
    244 
    245 		if (args.version != PTYFS_ARGSVERSION)
    246 			return EINVAL;
    247 	} else {
    248 		/*
    249 		 * Arguments are mandatory.
    250 		 */
    251 		return EINVAL;
    252 	}
    253 
    254 	pmnt = malloc(sizeof(struct ptyfsmount), M_UFSMNT, M_WAITOK);
    255 
    256 	mp->mnt_data = pmnt;
    257 	pmnt->pmnt_gid = args.gid;
    258 	pmnt->pmnt_mode = args.mode;
    259 	mp->mnt_flag |= MNT_LOCAL;
    260 	vfs_getnewfsid(mp);
    261 
    262 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
    263 	    UIO_SYSSPACE, mp, l)) != 0) {
    264 		free(pmnt, M_UFSMNT);
    265 		return error;
    266 	}
    267 
    268 	/* Point pty access to us */
    269 
    270 	ptm_ptyfspty.arg = mp;
    271 	ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
    272 	ptyfs_count++;
    273 	return 0;
    274 }
    275 
    276 /*ARGSUSED*/
    277 int
    278 ptyfs_start(struct mount *mp, int flags, struct lwp *p)
    279 {
    280 	return 0;
    281 }
    282 
    283 /*ARGSUSED*/
    284 int
    285 ptyfs_unmount(struct mount *mp, int mntflags, struct lwp *p)
    286 {
    287 	int error;
    288 	int flags = 0;
    289 
    290 	if (mntflags & MNT_FORCE)
    291 		flags |= FORCECLOSE;
    292 
    293 	if ((error = vflush(mp, 0, flags)) != 0)
    294 		return (error);
    295 
    296 	/* Restore where pty access was pointing */
    297 	(void)pty_sethandler(ptyfs_save_ptm);
    298 	ptyfs_save_ptm = NULL;
    299 	ptm_ptyfspty.arg = NULL;
    300 
    301 	/*
    302 	 * Finally, throw away the ptyfsmount structure
    303 	 */
    304 	free(mp->mnt_data, M_UFSMNT);
    305 	mp->mnt_data = 0;
    306 	ptyfs_count--;
    307 
    308 	return 0;
    309 }
    310 
    311 int
    312 ptyfs_root(struct mount *mp, struct vnode **vpp)
    313 {
    314 	/* setup "." */
    315 	return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
    316 }
    317 
    318 /*ARGSUSED*/
    319 int
    320 ptyfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *p)
    321 {
    322 	return EOPNOTSUPP;
    323 }
    324 
    325 /*ARGSUSED*/
    326 int
    327 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *p)
    328 {
    329 	sbp->f_bsize = DEV_BSIZE;
    330 	sbp->f_frsize = DEV_BSIZE;
    331 	sbp->f_iosize = DEV_BSIZE;
    332 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    333 	sbp->f_bfree = 0;
    334 	sbp->f_bavail = 0;
    335 	sbp->f_bresvd = 0;
    336 	sbp->f_files = 1024;	/* XXX lie */
    337 	sbp->f_ffree = 128;	/* XXX lie */
    338 	sbp->f_favail = 128;	/* XXX lie */
    339 	sbp->f_fresvd = 0;
    340 	sbp->f_namemax = MAXNAMLEN;
    341 	copy_statvfs_info(sbp, mp);
    342 	return 0;
    343 }
    344 
    345 /*ARGSUSED*/
    346 int
    347 ptyfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc, struct lwp *p)
    348 {
    349 	return 0;
    350 }
    351 
    352 /*
    353  * Kernfs flat namespace lookup.
    354  * Currently unsupported.
    355  */
    356 /*ARGSUSED*/
    357 int
    358 ptyfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    359 {
    360 	return EOPNOTSUPP;
    361 }
    362 
    363 SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup")
    364 {
    365 
    366 	sysctl_createv(clog, 0, NULL, NULL,
    367 		       CTLFLAG_PERMANENT,
    368 		       CTLTYPE_NODE, "vfs", NULL,
    369 		       NULL, 0, NULL, 0,
    370 		       CTL_VFS, CTL_EOL);
    371 	sysctl_createv(clog, 0, NULL, NULL,
    372 		       CTLFLAG_PERMANENT,
    373 		       CTLTYPE_NODE, "ptyfs",
    374 		       SYSCTL_DESCR("Pty file system"),
    375 		       NULL, 0, NULL, 0,
    376 		       CTL_VFS, 23, CTL_EOL);
    377 	/*
    378 	 * XXX the "23" above could be dynamic, thereby eliminating
    379 	 * one more instance of the "number to vfs" mapping problem,
    380 	 * but "23" is the order as taken from sys/mount.h
    381 	 */
    382 }
    383 
    384 
    385 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
    386 
    387 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
    388 	&ptyfs_vnodeop_opv_desc,
    389 	NULL,
    390 };
    391 
    392 struct vfsops ptyfs_vfsops = {
    393 	MOUNT_PTYFS,
    394 	ptyfs_mount,
    395 	ptyfs_start,
    396 	ptyfs_unmount,
    397 	ptyfs_root,
    398 	ptyfs_quotactl,
    399 	ptyfs_statvfs,
    400 	ptyfs_sync,
    401 	ptyfs_vget,
    402 	NULL,				/* vfs_fhtovp */
    403 	NULL,				/* vfs_vptofp */
    404 	ptyfs_init,
    405 	ptyfs_reinit,
    406 	ptyfs_done,
    407 	NULL,				/* vfs_mountroot */
    408 	(int (*)(struct mount *, struct vnode *, struct timespec *))eopnotsupp,
    409 	(int (*)(struct mount *, int, struct vnode *, int, const char *,
    410 	    struct lwp *))eopnotsupp,
    411 	ptyfs_vnodeopv_descs,
    412 };
    413 VFS_ATTACH(ptyfs_vfsops);
    414