Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_vfsops.c revision 1.37.18.1
      1 /*	$NetBSD: ptyfs_vfsops.c,v 1.37.18.1 2014/04/28 16:05:35 sborrill 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.37.18.1 2014/04/28 16:05:35 sborrill 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 ptm_pty *, struct lwp *, struct vnode **,
     76     dev_t, char);
     77 static int ptyfs__makename(struct ptm_pty *, struct lwp *, char *, size_t,
     78     dev_t, char);
     79 static void ptyfs__getvattr(struct ptm_pty *, struct lwp *, struct vattr *);
     80 
     81 /*
     82  * ptm glue: When we mount, we make ptm point to us.
     83  */
     84 struct ptm_pty *ptyfs_save_ptm;
     85 static int ptyfs_count;
     86 
     87 struct ptm_pty ptm_ptyfspty = {
     88 	ptyfs__allocvp,
     89 	ptyfs__makename,
     90 	ptyfs__getvattr,
     91 	NULL
     92 };
     93 
     94 static int
     95 ptyfs__makename(struct ptm_pty *pt, struct lwp *l, char *tbuf, size_t bufsiz,
     96     dev_t dev, char ms)
     97 {
     98 	struct mount *mp = pt->arg;
     99 	size_t len;
    100 
    101 	switch (ms) {
    102 	case 'p':
    103 		/* We don't provide access to the master, should we? */
    104 		len = snprintf(tbuf, bufsiz, "/dev/null");
    105 		break;
    106 	case 't':
    107 		len = snprintf(tbuf, bufsiz, "%s/%d", mp->mnt_stat.f_mntonname,
    108 		    minor(dev));
    109 		break;
    110 	default:
    111 		return EINVAL;
    112 	}
    113 
    114 	return len >= bufsiz ? ENOSPC : 0;
    115 }
    116 
    117 
    118 static int
    119 /*ARGSUSED*/
    120 ptyfs__allocvp(struct ptm_pty *pt, struct lwp *l, struct vnode **vpp,
    121     dev_t dev, char ms)
    122 {
    123 	struct mount *mp = pt->arg;
    124 	ptyfstype type;
    125 
    126 	switch (ms) {
    127 	case 'p':
    128 		type = PTYFSptc;
    129 		break;
    130 	case 't':
    131 		type = PTYFSpts;
    132 		break;
    133 	default:
    134 		return EINVAL;
    135 	}
    136 
    137 	return ptyfs_allocvp(mp, vpp, type, minor(dev), l);
    138 }
    139 
    140 
    141 static void
    142 ptyfs__getvattr(struct ptm_pty *pt, struct lwp *l, struct vattr *vattr)
    143 {
    144 	struct mount *mp = pt->arg;
    145 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
    146 	VATTR_NULL(vattr);
    147 	/* get real uid */
    148 	vattr->va_uid = kauth_cred_getuid(l->l_cred);
    149 	vattr->va_gid = pmnt->pmnt_gid;
    150 	vattr->va_mode = pmnt->pmnt_mode;
    151 }
    152 
    153 
    154 void
    155 ptyfs_init(void)
    156 {
    157 
    158 	malloc_type_attach(M_PTYFSMNT);
    159 	malloc_type_attach(M_PTYFSTMP);
    160 	ptyfs_hashinit();
    161 }
    162 
    163 void
    164 ptyfs_reinit(void)
    165 {
    166 	ptyfs_hashreinit();
    167 }
    168 
    169 void
    170 ptyfs_done(void)
    171 {
    172 
    173 	ptyfs_hashdone();
    174 	malloc_type_detach(M_PTYFSTMP);
    175 	malloc_type_detach(M_PTYFSMNT);
    176 }
    177 
    178 /*
    179  * Mount the Pseudo tty params filesystem
    180  */
    181 int
    182 ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    183 {
    184 	struct lwp *l = curlwp;
    185 	int error = 0;
    186 	struct ptyfsmount *pmnt;
    187 	struct ptyfs_args *args = data;
    188 
    189 	if (args == NULL)
    190 		return EINVAL;
    191 	if (*data_len < sizeof *args)
    192 		return EINVAL;
    193 
    194 	if (UIO_MX & (UIO_MX - 1)) {
    195 		log(LOG_ERR, "ptyfs: invalid directory entry size");
    196 		return EINVAL;
    197 	}
    198 
    199 	if (mp->mnt_flag & MNT_GETARGS) {
    200 		pmnt = VFSTOPTY(mp);
    201 		if (pmnt == NULL)
    202 			return EIO;
    203 		args->version = PTYFS_ARGSVERSION;
    204 		args->mode = pmnt->pmnt_mode;
    205 		args->gid = pmnt->pmnt_gid;
    206 		*data_len = sizeof *args;
    207 		return 0;
    208 	}
    209 
    210 	/* Don't allow more than one mount */
    211 	if (ptyfs_count)
    212 		return EBUSY;
    213 
    214 	if (mp->mnt_flag & MNT_UPDATE)
    215 		return EOPNOTSUPP;
    216 
    217 	if (args->version != PTYFS_ARGSVERSION)
    218 		return EINVAL;
    219 
    220 	pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK);
    221 
    222 	mp->mnt_data = pmnt;
    223 	pmnt->pmnt_gid = args->gid;
    224 	pmnt->pmnt_mode = args->mode;
    225 	mp->mnt_flag |= MNT_LOCAL;
    226 	vfs_getnewfsid(mp);
    227 
    228 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
    229 	    UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
    230 		free(pmnt, M_PTYFSMNT);
    231 		return error;
    232 	}
    233 
    234 	/* Point pty access to us */
    235 
    236 	ptm_ptyfspty.arg = mp;
    237 	ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
    238 	ptyfs_count++;
    239 	return 0;
    240 }
    241 
    242 /*ARGSUSED*/
    243 int
    244 ptyfs_start(struct mount *mp, int flags)
    245 {
    246 	return 0;
    247 }
    248 
    249 /*ARGSUSED*/
    250 int
    251 ptyfs_unmount(struct mount *mp, int mntflags)
    252 {
    253 	int error;
    254 	int flags = 0;
    255 
    256 	if (mntflags & MNT_FORCE)
    257 		flags |= FORCECLOSE;
    258 
    259 	if ((error = vflush(mp, 0, flags)) != 0)
    260 		return (error);
    261 
    262 	/* Restore where pty access was pointing */
    263 	(void)pty_sethandler(ptyfs_save_ptm);
    264 	ptyfs_save_ptm = NULL;
    265 	ptm_ptyfspty.arg = NULL;
    266 
    267 	/*
    268 	 * Finally, throw away the ptyfsmount structure
    269 	 */
    270 	free(mp->mnt_data, M_PTYFSMNT);
    271 	mp->mnt_data = NULL;
    272 	ptyfs_count--;
    273 
    274 	return 0;
    275 }
    276 
    277 int
    278 ptyfs_root(struct mount *mp, struct vnode **vpp)
    279 {
    280 	/* setup "." */
    281 	return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
    282 }
    283 
    284 /*ARGSUSED*/
    285 int
    286 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp)
    287 {
    288 	sbp->f_bsize = DEV_BSIZE;
    289 	sbp->f_frsize = DEV_BSIZE;
    290 	sbp->f_iosize = DEV_BSIZE;
    291 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    292 	sbp->f_bfree = 0;
    293 	sbp->f_bavail = 0;
    294 	sbp->f_bresvd = 0;
    295 	sbp->f_files = 1024;	/* XXX lie */
    296 	sbp->f_ffree = 128;	/* XXX lie */
    297 	sbp->f_favail = 128;	/* XXX lie */
    298 	sbp->f_fresvd = 0;
    299 	sbp->f_namemax = MAXNAMLEN;
    300 	copy_statvfs_info(sbp, mp);
    301 	return 0;
    302 }
    303 
    304 /*ARGSUSED*/
    305 int
    306 ptyfs_sync(struct mount *mp, int waitfor,
    307     kauth_cred_t uc)
    308 {
    309 	return 0;
    310 }
    311 
    312 /*
    313  * Kernfs flat namespace lookup.
    314  * Currently unsupported.
    315  */
    316 /*ARGSUSED*/
    317 int
    318 ptyfs_vget(struct mount *mp, ino_t ino,
    319     struct vnode **vpp)
    320 {
    321 	return EOPNOTSUPP;
    322 }
    323 
    324 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
    325 
    326 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
    327 	&ptyfs_vnodeop_opv_desc,
    328 	NULL,
    329 };
    330 
    331 struct vfsops ptyfs_vfsops = {
    332 	MOUNT_PTYFS,
    333 	sizeof (struct ptyfs_args),
    334 	ptyfs_mount,
    335 	ptyfs_start,
    336 	ptyfs_unmount,
    337 	ptyfs_root,
    338 	(void *)eopnotsupp,		/* vfs_quotactl */
    339 	ptyfs_statvfs,
    340 	ptyfs_sync,
    341 	ptyfs_vget,
    342 	(void *)eopnotsupp,		/* vfs_fhtovp */
    343 	(void *)eopnotsupp,		/* vfs_vptofp */
    344 	ptyfs_init,
    345 	ptyfs_reinit,
    346 	ptyfs_done,
    347 	NULL,				/* vfs_mountroot */
    348 	(void *)eopnotsupp,
    349 	(void *)eopnotsupp,
    350 	(void *)eopnotsupp,		/* vfs_suspendctl */
    351 	genfs_renamelock_enter,
    352 	genfs_renamelock_exit,
    353 	(void *)eopnotsupp,
    354 	ptyfs_vnodeopv_descs,
    355 	0,
    356 	{ NULL, NULL },
    357 };
    358 
    359 static int
    360 ptyfs_modcmd(modcmd_t cmd, void *arg)
    361 {
    362 	int error;
    363 
    364 	switch (cmd) {
    365 	case MODULE_CMD_INIT:
    366 		error = vfs_attach(&ptyfs_vfsops);
    367 		if (error != 0)
    368 			break;
    369 		sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
    370 			       CTLFLAG_PERMANENT,
    371 			       CTLTYPE_NODE, "vfs", NULL,
    372 			       NULL, 0, NULL, 0,
    373 			       CTL_VFS, CTL_EOL);
    374 		sysctl_createv(&ptyfs_sysctl_log, 0, NULL, NULL,
    375 			       CTLFLAG_PERMANENT,
    376 			       CTLTYPE_NODE, "ptyfs",
    377 			       SYSCTL_DESCR("Pty file system"),
    378 			       NULL, 0, NULL, 0,
    379 			       CTL_VFS, 23, CTL_EOL);
    380 		/*
    381 		 * XXX the "23" above could be dynamic, thereby eliminating
    382 		 * one more instance of the "number to vfs" mapping problem,
    383 		 * but "23" is the order as taken from sys/mount.h
    384 		 */
    385 		break;
    386 	case MODULE_CMD_FINI:
    387 		error = vfs_detach(&ptyfs_vfsops);
    388 		if (error != 0)
    389 			break;
    390 		sysctl_teardown(&ptyfs_sysctl_log);
    391 		break;
    392 	default:
    393 		error = ENOTTY;
    394 		break;
    395 	}
    396 
    397 	return (error);
    398 }
    399