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