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