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