Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_vfsops.c revision 1.50
      1 /*	$NetBSD: ptyfs_vfsops.c,v 1.50 2014/04/16 18:55:18 maxv 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.50 2014/04/16 18:55:18 maxv 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 #include <sys/module.h>
     61 
     62 #include <fs/ptyfs/ptyfs.h>
     63 #include <miscfs/genfs/genfs.h>
     64 #include <miscfs/specfs/specdev.h>
     65 
     66 MODULE(MODULE_CLASS_VFS, ptyfs, NULL);
     67 
     68 MALLOC_JUSTDEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
     69 MALLOC_JUSTDEFINE(M_PTYFSTMP, "ptyfs temp", "ptyfs temporary structures");
     70 
     71 VFS_PROTOS(ptyfs);
     72 
     73 static struct sysctllog *ptyfs_sysctl_log;
     74 
     75 static int ptyfs__allocvp(struct mount *, struct lwp *, struct vnode **,
     76     dev_t, char);
     77 static int ptyfs__makename(struct mount *, struct lwp *, char *, size_t,
     78     dev_t, char);
     79 static void ptyfs__getvattr(struct mount *, struct lwp *, struct vattr *);
     80 static int ptyfs__getmp(struct lwp *, struct mount **);
     81 
     82 /*
     83  * ptm glue: When we mount, we make ptm point to us.
     84  */
     85 struct ptm_pty *ptyfs_save_ptm;
     86 static int ptyfs_count;
     87 
     88 static TAILQ_HEAD(, ptyfsmount) ptyfs_head;
     89 
     90 struct ptm_pty ptm_ptyfspty = {
     91 	ptyfs__allocvp,
     92 	ptyfs__makename,
     93 	ptyfs__getvattr,
     94 	ptyfs__getmp,
     95 };
     96 
     97 static int
     98 ptyfs__getmp(struct lwp *l, struct mount **mpp)
     99 {
    100  	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    101  	struct mount *mp;
    102 	struct ptyfsmount *pmnt;
    103 
    104 	TAILQ_FOREACH(pmnt, &ptyfs_head, pmnt_le) {
    105 		mp = pmnt->pmnt_mp;
    106 		if (cwdi->cwdi_rdir == NULL)
    107 			goto ok;
    108 
    109 		if (vn_isunder(mp->mnt_vnodecovered, cwdi->cwdi_rdir, l))
    110 			goto ok;
    111 	}
    112  	*mpp = NULL;
    113  	return EOPNOTSUPP;
    114 ok:
    115 	*mpp = mp;
    116 	return 0;
    117 }
    118 
    119 static const char *
    120 ptyfs__getpath(struct lwp *l, const struct mount *mp)
    121 {
    122 #define MAXBUF (sizeof(mp->mnt_stat.f_mntonname) + 32)
    123 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    124 	char *buf;
    125 	const char *rv;
    126 	size_t len;
    127 	char *bp;
    128 	int error;
    129 
    130 	rv = mp->mnt_stat.f_mntonname;
    131 	if (cwdi->cwdi_rdir == NULL)
    132 		return rv;
    133 
    134 	buf = malloc(MAXBUF, M_TEMP, M_WAITOK);
    135 	bp = buf + MAXBUF;
    136 	*--bp = '\0';
    137 	error = getcwd_common(mp->mnt_vnodecovered, cwdi->cwdi_rdir, &bp,
    138 	    buf, MAXBUF / 2, 0, l);
    139 	if (error) {	/* Mount point is out of rdir */
    140 		rv = NULL;
    141 		goto out;
    142 	}
    143 
    144 	len = strlen(bp);
    145 	if (len < sizeof(mp->mnt_stat.f_mntonname))	/* XXX */
    146 		rv += strlen(rv) - len;
    147 out:
    148 	free(buf, M_TEMP);
    149 	return rv;
    150 }
    151 
    152 static int
    153 ptyfs__makename(struct mount *mp, struct lwp *l, char *tbuf, size_t bufsiz,
    154     dev_t dev, char ms)
    155 {
    156 	size_t len;
    157 	const char *np;
    158 
    159 	switch (ms) {
    160 	case 'p':
    161 		/* We don't provide access to the master, should we? */
    162 		len = snprintf(tbuf, bufsiz, "/dev/null");
    163 		break;
    164 	case 't':
    165 		/*
    166 		 * We support traditional ptys, so we can get here,
    167 		 * if pty had been opened before PTYFS was mounted,
    168 		 * or was opened through /dev/ptyXX devices.
    169 		 * Return it only outside chroot for more security .
    170 		 */
    171 		if (l->l_proc->p_cwdi->cwdi_rdir == NULL
    172 		    && ptyfs_save_ptm != NULL
    173 		    && ptyfs_used_get(PTYFSptc, minor(dev), mp, 0) == NULL)
    174 			return (*ptyfs_save_ptm->makename)(mp, l,
    175 			    tbuf, bufsiz, dev, ms);
    176 
    177 		np = ptyfs__getpath(l, mp);
    178 		if (np == NULL)
    179 			return EOPNOTSUPP;
    180 		len = snprintf(tbuf, bufsiz, "%s/%llu", np,
    181 			(unsigned long long)minor(dev));
    182 		break;
    183 	default:
    184 		return EINVAL;
    185 	}
    186 
    187 	return len >= bufsiz ? ENOSPC : 0;
    188 }
    189 
    190 
    191 static int
    192 /*ARGSUSED*/
    193 ptyfs__allocvp(struct mount *mp, struct lwp *l, struct vnode **vpp,
    194     dev_t dev, char ms)
    195 {
    196 	ptyfstype type;
    197 
    198 	switch (ms) {
    199 	case 'p':
    200 		type = PTYFSptc;
    201 		break;
    202 	case 't':
    203 		type = PTYFSpts;
    204 		break;
    205 	default:
    206 		return EINVAL;
    207 	}
    208 
    209 	return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
    210 }
    211 
    212 
    213 static void
    214 ptyfs__getvattr(struct mount *mp, struct lwp *l, struct vattr *vattr)
    215 {
    216 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
    217 	vattr_null(vattr);
    218 	/* get real uid */
    219 	vattr->va_uid = kauth_cred_getuid(l->l_cred);
    220 	vattr->va_gid = pmnt->pmnt_gid;
    221 	vattr->va_mode = pmnt->pmnt_mode;
    222 }
    223 
    224 
    225 void
    226 ptyfs_init(void)
    227 {
    228 
    229 	TAILQ_INIT(&ptyfs_head);
    230 	malloc_type_attach(M_PTYFSMNT);
    231 	malloc_type_attach(M_PTYFSTMP);
    232 	ptyfs_hashinit();
    233 }
    234 
    235 void
    236 ptyfs_reinit(void)
    237 {
    238 	ptyfs_hashreinit();
    239 }
    240 
    241 void
    242 ptyfs_done(void)
    243 {
    244 
    245 	ptyfs_hashdone();
    246 	malloc_type_detach(M_PTYFSTMP);
    247 	malloc_type_detach(M_PTYFSMNT);
    248 }
    249 
    250 #define OSIZE sizeof(struct { int f; gid_t g; mode_t m; })
    251 /*
    252  * Mount the Pseudo tty params filesystem
    253  */
    254 int
    255 ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    256 {
    257 	struct lwp *l = curlwp;
    258 	int error = 0;
    259 	struct ptyfsmount *pmnt;
    260 	struct ptyfs_args *args = data;
    261 
    262 	if (args == NULL)
    263 		return EINVAL;
    264 	if (*data_len != sizeof *args && *data_len != OSIZE)
    265 		return EINVAL;
    266 
    267 	if (UIO_MX & (UIO_MX - 1)) {
    268 		log(LOG_ERR, "ptyfs: invalid directory entry size");
    269 		return EINVAL;
    270 	}
    271 
    272 	if (mp->mnt_flag & MNT_GETARGS) {
    273 		pmnt = VFSTOPTY(mp);
    274 		if (pmnt == NULL)
    275 			return EIO;
    276 		args->mode = pmnt->pmnt_mode;
    277 		args->gid = pmnt->pmnt_gid;
    278 		if (args->version >= PTYFS_ARGSVERSION) {
    279 			args->flags = pmnt->pmnt_flags;
    280 			*data_len = sizeof *args;
    281 		} else {
    282 			*data_len = OSIZE;
    283 		}
    284 		return 0;
    285 	}
    286 
    287 #if 0
    288 	/* Don't allow more than one mount */
    289 	if (ptyfs_count)
    290 		return EBUSY;
    291 #endif
    292 
    293 	if (mp->mnt_flag & MNT_UPDATE)
    294 		return EOPNOTSUPP;
    295 
    296 	if (args->version > PTYFS_ARGSVERSION)
    297 		return EINVAL;
    298 
    299 	pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK);
    300 
    301 	mp->mnt_data = pmnt;
    302 	pmnt->pmnt_gid = args->gid;
    303 	pmnt->pmnt_mode = args->mode;
    304 	if (args->version >= PTYFS_ARGSVERSION)
    305 		pmnt->pmnt_flags = args->flags;
    306 	else
    307 		pmnt->pmnt_flags = 0;
    308 	mp->mnt_flag |= MNT_LOCAL;
    309 	vfs_getnewfsid(mp);
    310 
    311 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
    312 	    UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
    313 		free(pmnt, M_PTYFSMNT);
    314 		return error;
    315 	}
    316 
    317 	pmnt->pmnt_mp = mp;
    318 	TAILQ_INSERT_TAIL(&ptyfs_head, pmnt, pmnt_le);
    319 	if (ptyfs_count++ == 0) {
    320 		/* Point pty access to us */
    321 		ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
    322 	}
    323 	return 0;
    324 }
    325 
    326 /*ARGSUSED*/
    327 int
    328 ptyfs_start(struct mount *mp, int flags)
    329 {
    330 	return 0;
    331 }
    332 
    333 /*ARGSUSED*/
    334 int
    335 ptyfs_unmount(struct mount *mp, int mntflags)
    336 {
    337 	int error;
    338 	int flags = 0;
    339 	struct ptyfsmount *pmnt;
    340 
    341 	if (mntflags & MNT_FORCE)
    342 		flags |= FORCECLOSE;
    343 
    344 	if ((error = vflush(mp, 0, flags)) != 0)
    345 		return error;
    346 
    347 	ptyfs_count--;
    348 	if (ptyfs_count == 0) {
    349 		/* Restore where pty access was pointing */
    350 		(void)pty_sethandler(ptyfs_save_ptm);
    351 		ptyfs_save_ptm = NULL;
    352 	}
    353 	TAILQ_FOREACH(pmnt, &ptyfs_head, pmnt_le) {
    354 		if (pmnt->pmnt_mp == mp) {
    355 			TAILQ_REMOVE(&ptyfs_head, pmnt, pmnt_le);
    356 			break;
    357 		}
    358  	}
    359 
    360 	/*
    361 	 * Finally, throw away the ptyfsmount structure
    362 	 */
    363 	free(mp->mnt_data, M_PTYFSMNT);
    364 	mp->mnt_data = NULL;
    365 
    366 	return 0;
    367 }
    368 
    369 int
    370 ptyfs_root(struct mount *mp, struct vnode **vpp)
    371 {
    372 	/* setup "." */
    373 	return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
    374 }
    375 
    376 /*ARGSUSED*/
    377 int
    378 ptyfs_sync(struct mount *mp, int waitfor,
    379     kauth_cred_t uc)
    380 {
    381 	return 0;
    382 }
    383 
    384 /*
    385  * Kernfs flat namespace lookup.
    386  * Currently unsupported.
    387  */
    388 /*ARGSUSED*/
    389 int
    390 ptyfs_vget(struct mount *mp, ino_t ino,
    391     struct vnode **vpp)
    392 {
    393 	return EOPNOTSUPP;
    394 }
    395 
    396 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
    397 
    398 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
    399 	&ptyfs_vnodeop_opv_desc,
    400 	NULL,
    401 };
    402 
    403 struct vfsops ptyfs_vfsops = {
    404 	.vfs_name = MOUNT_PTYFS,
    405 	.vfs_min_mount_data = sizeof (struct ptyfs_args),
    406 	.vfs_mount = ptyfs_mount,
    407 	.vfs_start = ptyfs_start,
    408 	.vfs_unmount = ptyfs_unmount,
    409 	.vfs_root = ptyfs_root,
    410 	.vfs_quotactl = (void *)eopnotsupp,
    411 	.vfs_statvfs = genfs_statvfs,
    412 	.vfs_sync = ptyfs_sync,
    413 	.vfs_vget = ptyfs_vget,
    414 	.vfs_fhtovp = (void *)eopnotsupp,
    415 	.vfs_vptofh = (void *)eopnotsupp,
    416 	.vfs_init = ptyfs_init,
    417 	.vfs_reinit = ptyfs_reinit,
    418 	.vfs_done = ptyfs_done,
    419 	.vfs_snapshot = (void *)eopnotsupp,
    420 	.vfs_extattrctl = (void *)eopnotsupp,
    421 	.vfs_suspendctl = (void *)eopnotsupp,
    422 	.vfs_renamelock_enter = genfs_renamelock_enter,
    423 	.vfs_renamelock_exit = genfs_renamelock_exit,
    424 	.vfs_fsync = (void *)eopnotsupp,
    425 	.vfs_opv_descs = ptyfs_vnodeopv_descs
    426 };
    427 
    428 static int
    429 ptyfs_modcmd(modcmd_t cmd, void *arg)
    430 {
    431 	int error;
    432 
    433 	switch (cmd) {
    434 	case MODULE_CMD_INIT:
    435 		error = vfs_attach(&ptyfs_vfsops);
    436 		if (error != 0)
    437 			break;
    438 		sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
    439 			       CTLFLAG_PERMANENT,
    440 			       CTLTYPE_NODE, "ptyfs",
    441 			       SYSCTL_DESCR("Pty file system"),
    442 			       NULL, 0, NULL, 0,
    443 			       CTL_VFS, 23, CTL_EOL);
    444 		/*
    445 		 * XXX the "23" above could be dynamic, thereby eliminating
    446 		 * one more instance of the "number to vfs" mapping problem,
    447 		 * but "23" is the order as taken from sys/mount.h
    448 		 */
    449 		break;
    450 	case MODULE_CMD_FINI:
    451 		error = vfs_detach(&ptyfs_vfsops);
    452 		if (error != 0)
    453 			break;
    454 		sysctl_teardown(&ptyfs_sysctl_log);
    455 		break;
    456 	default:
    457 		error = ENOTTY;
    458 		break;
    459 	}
    460 
    461 	return (error);
    462 }
    463