1 /* $NetBSD: ptyfs_vfsops.c,v 1.29 2007/09/24 00:42:14 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.29 2007/09/24 00:42:14 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 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 struct lwp *l) 209 { 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 struct lwp *p) 269 { 270 return 0; 271 } 272 273 /*ARGSUSED*/ 274 int 275 ptyfs_unmount(struct mount *mp, int mntflags, struct lwp *p) 276 { 277 int error; 278 int flags = 0; 279 280 if (mntflags & MNT_FORCE) 281 flags |= FORCECLOSE; 282 283 if ((error = vflush(mp, 0, flags)) != 0) 284 return (error); 285 286 /* Restore where pty access was pointing */ 287 (void)pty_sethandler(ptyfs_save_ptm); 288 ptyfs_save_ptm = NULL; 289 ptm_ptyfspty.arg = NULL; 290 291 /* 292 * Finally, throw away the ptyfsmount structure 293 */ 294 free(mp->mnt_data, M_UFSMNT); 295 mp->mnt_data = 0; 296 ptyfs_count--; 297 298 return 0; 299 } 300 301 int 302 ptyfs_root(struct mount *mp, struct vnode **vpp) 303 { 304 /* setup "." */ 305 return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL); 306 } 307 308 /*ARGSUSED*/ 309 int 310 ptyfs_quotactl(struct mount *mp, int cmd, uid_t uid, 311 void *arg, struct lwp *p) 312 { 313 return EOPNOTSUPP; 314 } 315 316 /*ARGSUSED*/ 317 int 318 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *p) 319 { 320 sbp->f_bsize = DEV_BSIZE; 321 sbp->f_frsize = DEV_BSIZE; 322 sbp->f_iosize = DEV_BSIZE; 323 sbp->f_blocks = 2; /* 1K to keep df happy */ 324 sbp->f_bfree = 0; 325 sbp->f_bavail = 0; 326 sbp->f_bresvd = 0; 327 sbp->f_files = 1024; /* XXX lie */ 328 sbp->f_ffree = 128; /* XXX lie */ 329 sbp->f_favail = 128; /* XXX lie */ 330 sbp->f_fresvd = 0; 331 sbp->f_namemax = MAXNAMLEN; 332 copy_statvfs_info(sbp, mp); 333 return 0; 334 } 335 336 /*ARGSUSED*/ 337 int 338 ptyfs_sync(struct mount *mp, int waitfor, 339 kauth_cred_t uc, struct lwp *p) 340 { 341 return 0; 342 } 343 344 /* 345 * Kernfs flat namespace lookup. 346 * Currently unsupported. 347 */ 348 /*ARGSUSED*/ 349 int 350 ptyfs_vget(struct mount *mp, ino_t ino, 351 struct vnode **vpp) 352 { 353 return EOPNOTSUPP; 354 } 355 356 SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup") 357 { 358 359 sysctl_createv(clog, 0, NULL, NULL, 360 CTLFLAG_PERMANENT, 361 CTLTYPE_NODE, "vfs", NULL, 362 NULL, 0, NULL, 0, 363 CTL_VFS, CTL_EOL); 364 sysctl_createv(clog, 0, NULL, NULL, 365 CTLFLAG_PERMANENT, 366 CTLTYPE_NODE, "ptyfs", 367 SYSCTL_DESCR("Pty file system"), 368 NULL, 0, NULL, 0, 369 CTL_VFS, 23, CTL_EOL); 370 /* 371 * XXX the "23" above could be dynamic, thereby eliminating 372 * one more instance of the "number to vfs" mapping problem, 373 * but "23" is the order as taken from sys/mount.h 374 */ 375 } 376 377 378 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc; 379 380 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = { 381 &ptyfs_vnodeop_opv_desc, 382 NULL, 383 }; 384 385 struct vfsops ptyfs_vfsops = { 386 MOUNT_PTYFS, 387 sizeof (struct ptyfs_args), 388 ptyfs_mount, 389 ptyfs_start, 390 ptyfs_unmount, 391 ptyfs_root, 392 ptyfs_quotactl, 393 ptyfs_statvfs, 394 ptyfs_sync, 395 ptyfs_vget, 396 (void *)eopnotsupp, /* vfs_fhtovp */ 397 (void *)eopnotsupp, /* vfs_vptofp */ 398 ptyfs_init, 399 ptyfs_reinit, 400 ptyfs_done, 401 NULL, /* vfs_mountroot */ 402 (int (*)(struct mount *, struct vnode *, struct timespec *))eopnotsupp, 403 (int (*)(struct mount *, int, struct vnode *, int, const char *, 404 struct lwp *))eopnotsupp, 405 (void *)eopnotsupp, /* vfs_suspendctl */ 406 ptyfs_vnodeopv_descs, 407 0, 408 { NULL, NULL }, 409 }; 410 VFS_ATTACH(ptyfs_vfsops); 411