1 /* $NetBSD: ptyfs_vfsops.c,v 1.35 2008/05/13 08:31:12 simonb 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.35 2008/05/13 08:31:12 simonb 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 int ptyfs__allocvp(struct ptm_pty *, struct lwp *, struct vnode **, 74 dev_t, char); 75 static int ptyfs__makename(struct ptm_pty *, struct lwp *, char *, size_t, 76 dev_t, char); 77 static void ptyfs__getvattr(struct ptm_pty *, struct lwp *, struct vattr *); 78 79 /* 80 * ptm glue: When we mount, we make ptm point to us. 81 */ 82 struct ptm_pty *ptyfs_save_ptm; 83 static int ptyfs_count; 84 85 struct ptm_pty ptm_ptyfspty = { 86 ptyfs__allocvp, 87 ptyfs__makename, 88 ptyfs__getvattr, 89 NULL 90 }; 91 92 static const char * 93 ptyfs__getpath(struct lwp *l, const struct mount *mp) 94 { 95 #define MAXBUF (sizeof(mp->mnt_stat.f_mntonname) + 32) 96 struct cwdinfo *cwdi = l->l_proc->p_cwdi; 97 char *buf; 98 const char *rv; 99 size_t len; 100 char *bp; 101 int error; 102 103 rv = mp->mnt_stat.f_mntonname; 104 if (cwdi->cwdi_rdir == NULL) 105 return rv; 106 107 buf = malloc(MAXBUF, M_TEMP, M_WAITOK); 108 bp = buf + MAXBUF; 109 *--bp = '\0'; 110 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, 111 buf, MAXBUF / 2, 0, l); 112 if (error) /* XXX */ 113 goto out; 114 115 len = strlen(bp); 116 if (len < sizeof(mp->mnt_stat.f_mntonname)) /* XXX */ 117 rv += len; 118 out: 119 free(buf, M_TEMP); 120 return rv; 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 lwp *l, 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(l->l_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 187 malloc_type_attach(M_PTYFSMNT); 188 malloc_type_attach(M_PTYFSTMP); 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 202 ptyfs_hashdone(); 203 malloc_type_detach(M_PTYFSTMP); 204 malloc_type_detach(M_PTYFSMNT); 205 } 206 207 /* 208 * Mount the Pseudo tty params filesystem 209 */ 210 int 211 ptyfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 212 { 213 struct lwp *l = curlwp; 214 int error = 0; 215 struct ptyfsmount *pmnt; 216 struct ptyfs_args *args = data; 217 218 if (*data_len < sizeof *args) 219 return EINVAL; 220 221 if (UIO_MX & (UIO_MX - 1)) { 222 log(LOG_ERR, "ptyfs: invalid directory entry size"); 223 return EINVAL; 224 } 225 226 if (mp->mnt_flag & MNT_GETARGS) { 227 pmnt = VFSTOPTY(mp); 228 if (pmnt == NULL) 229 return EIO; 230 args->version = PTYFS_ARGSVERSION; 231 args->mode = pmnt->pmnt_mode; 232 args->gid = pmnt->pmnt_gid; 233 *data_len = sizeof *args; 234 return 0; 235 } 236 237 /* Don't allow more than one mount */ 238 if (ptyfs_count) 239 return EBUSY; 240 241 if (mp->mnt_flag & MNT_UPDATE) 242 return EOPNOTSUPP; 243 244 if (args->version != PTYFS_ARGSVERSION) 245 return EINVAL; 246 247 pmnt = malloc(sizeof(struct ptyfsmount), M_PTYFSMNT, M_WAITOK); 248 249 mp->mnt_data = pmnt; 250 pmnt->pmnt_gid = args->gid; 251 pmnt->pmnt_mode = args->mode; 252 mp->mnt_flag |= MNT_LOCAL; 253 vfs_getnewfsid(mp); 254 255 if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs", 256 UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) { 257 free(pmnt, M_PTYFSMNT); 258 return error; 259 } 260 261 /* Point pty access to us */ 262 263 ptm_ptyfspty.arg = mp; 264 ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty); 265 ptyfs_count++; 266 return 0; 267 } 268 269 /*ARGSUSED*/ 270 int 271 ptyfs_start(struct mount *mp, int flags) 272 { 273 return 0; 274 } 275 276 /*ARGSUSED*/ 277 int 278 ptyfs_unmount(struct mount *mp, int mntflags) 279 { 280 int error; 281 int flags = 0; 282 283 if (mntflags & MNT_FORCE) 284 flags |= FORCECLOSE; 285 286 if ((error = vflush(mp, 0, flags)) != 0) 287 return (error); 288 289 /* Restore where pty access was pointing */ 290 (void)pty_sethandler(ptyfs_save_ptm); 291 ptyfs_save_ptm = NULL; 292 ptm_ptyfspty.arg = NULL; 293 294 /* 295 * Finally, throw away the ptyfsmount structure 296 */ 297 free(mp->mnt_data, M_PTYFSMNT); 298 mp->mnt_data = NULL; 299 ptyfs_count--; 300 301 return 0; 302 } 303 304 int 305 ptyfs_root(struct mount *mp, struct vnode **vpp) 306 { 307 /* setup "." */ 308 return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL); 309 } 310 311 /*ARGSUSED*/ 312 int 313 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp) 314 { 315 sbp->f_bsize = DEV_BSIZE; 316 sbp->f_frsize = DEV_BSIZE; 317 sbp->f_iosize = DEV_BSIZE; 318 sbp->f_blocks = 2; /* 1K to keep df happy */ 319 sbp->f_bfree = 0; 320 sbp->f_bavail = 0; 321 sbp->f_bresvd = 0; 322 sbp->f_files = 1024; /* XXX lie */ 323 sbp->f_ffree = 128; /* XXX lie */ 324 sbp->f_favail = 128; /* XXX lie */ 325 sbp->f_fresvd = 0; 326 sbp->f_namemax = MAXNAMLEN; 327 copy_statvfs_info(sbp, mp); 328 return 0; 329 } 330 331 /*ARGSUSED*/ 332 int 333 ptyfs_sync(struct mount *mp, int waitfor, 334 kauth_cred_t uc) 335 { 336 return 0; 337 } 338 339 /* 340 * Kernfs flat namespace lookup. 341 * Currently unsupported. 342 */ 343 /*ARGSUSED*/ 344 int 345 ptyfs_vget(struct mount *mp, ino_t ino, 346 struct vnode **vpp) 347 { 348 return EOPNOTSUPP; 349 } 350 351 SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup") 352 { 353 354 sysctl_createv(clog, 0, NULL, NULL, 355 CTLFLAG_PERMANENT, 356 CTLTYPE_NODE, "vfs", NULL, 357 NULL, 0, NULL, 0, 358 CTL_VFS, CTL_EOL); 359 sysctl_createv(clog, 0, NULL, NULL, 360 CTLFLAG_PERMANENT, 361 CTLTYPE_NODE, "ptyfs", 362 SYSCTL_DESCR("Pty file system"), 363 NULL, 0, NULL, 0, 364 CTL_VFS, 23, CTL_EOL); 365 /* 366 * XXX the "23" above could be dynamic, thereby eliminating 367 * one more instance of the "number to vfs" mapping problem, 368 * but "23" is the order as taken from sys/mount.h 369 */ 370 } 371 372 373 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc; 374 375 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = { 376 &ptyfs_vnodeop_opv_desc, 377 NULL, 378 }; 379 380 struct vfsops ptyfs_vfsops = { 381 MOUNT_PTYFS, 382 sizeof (struct ptyfs_args), 383 ptyfs_mount, 384 ptyfs_start, 385 ptyfs_unmount, 386 ptyfs_root, 387 (void *)eopnotsupp, /* vfs_quotactl */ 388 ptyfs_statvfs, 389 ptyfs_sync, 390 ptyfs_vget, 391 (void *)eopnotsupp, /* vfs_fhtovp */ 392 (void *)eopnotsupp, /* vfs_vptofp */ 393 ptyfs_init, 394 ptyfs_reinit, 395 ptyfs_done, 396 NULL, /* vfs_mountroot */ 397 (void *)eopnotsupp, 398 (void *)eopnotsupp, 399 (void *)eopnotsupp, /* vfs_suspendctl */ 400 genfs_renamelock_enter, 401 genfs_renamelock_exit, 402 (void *)eopnotsupp, 403 ptyfs_vnodeopv_descs, 404 0, 405 { NULL, NULL }, 406 }; 407 408 static int 409 ptyfs_modcmd(modcmd_t cmd, void *arg) 410 { 411 412 switch (cmd) { 413 case MODULE_CMD_INIT: 414 return vfs_attach(&ptyfs_vfsops); 415 case MODULE_CMD_FINI: 416 return vfs_detach(&ptyfs_vfsops); 417 default: 418 return ENOTTY; 419 } 420 } 421