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