1 1.20 christos /* $NetBSD: tty_bsdpty.c,v 1.20 2014/04/04 18:11:58 christos Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.1 christos #include <sys/cdefs.h> 30 1.20 christos __KERNEL_RCSID(0, "$NetBSD: tty_bsdpty.c,v 1.20 2014/04/04 18:11:58 christos Exp $"); 31 1.1 christos 32 1.1 christos #include "opt_ptm.h" 33 1.1 christos 34 1.19 christos #ifndef NO_DEV_PTM 35 1.1 christos #ifdef COMPAT_BSDPTY 36 1.1 christos /* bsd tty implementation for pty multiplexor driver /dev/ptm{,x} */ 37 1.1 christos 38 1.1 christos #include <sys/param.h> 39 1.1 christos #include <sys/systm.h> 40 1.1 christos #include <sys/ioctl.h> 41 1.5 christos #include <sys/lwp.h> 42 1.1 christos #include <sys/tty.h> 43 1.1 christos #include <sys/stat.h> 44 1.1 christos #include <sys/file.h> 45 1.1 christos #include <sys/uio.h> 46 1.1 christos #include <sys/kernel.h> 47 1.1 christos #include <sys/vnode.h> 48 1.1 christos #include <sys/namei.h> 49 1.1 christos #include <sys/signalvar.h> 50 1.1 christos #include <sys/filedesc.h> 51 1.1 christos #include <sys/conf.h> 52 1.1 christos #include <sys/poll.h> 53 1.1 christos #include <sys/pty.h> 54 1.8 elad #include <sys/kauth.h> 55 1.1 christos 56 1.1 christos /* 57 1.1 christos * pts == /dev/tty[pqrs]? 58 1.1 christos * ptc == /dev/pty[pqrs]? 59 1.1 christos */ 60 1.1 christos 61 1.2 christos /* 62 1.2 christos * All this hard-coding is really evil. 63 1.2 christos */ 64 1.3 perry #define TTY_GID 4 65 1.2 christos #define TTY_PERM (S_IRUSR|S_IWUSR|S_IWGRP) 66 1.1 christos #define TTY_TEMPLATE "/dev/XtyXX" 67 1.1 christos #define TTY_NAMESIZE sizeof(TTY_TEMPLATE) 68 1.1 christos #define TTY_LETTERS "pqrstuvwxyzPQRST" 69 1.1 christos #define TTY_OLD_SUFFIX "0123456789abcdef" 70 1.1 christos #define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 71 1.1 christos 72 1.19 christos static int pty_makename(struct mount *, struct lwp *, char *, size_t, dev_t, 73 1.7 christos char); 74 1.19 christos static int pty_allocvp(struct mount *, struct lwp *, struct vnode **, 75 1.1 christos dev_t, char); 76 1.19 christos static void pty_getvattr(struct mount *, struct lwp *, struct vattr *); 77 1.20 christos static int pty__getmp(struct lwp *, struct mount **); 78 1.1 christos 79 1.1 christos struct ptm_pty ptm_bsdpty = { 80 1.1 christos pty_allocvp, 81 1.1 christos pty_makename, 82 1.2 christos pty_getvattr, 83 1.20 christos pty__getmp, 84 1.1 christos }; 85 1.1 christos 86 1.1 christos static int 87 1.1 christos /*ARGSUSED*/ 88 1.19 christos pty_makename(struct mount *mp, struct lwp *l, char *bf, 89 1.10 christos size_t bufsiz, dev_t dev, char c) 90 1.1 christos { 91 1.1 christos size_t nt; 92 1.1 christos dev_t minor = minor(dev); 93 1.6 christos const char *suffix; 94 1.6 christos 95 1.1 christos if (bufsiz < TTY_NAMESIZE) 96 1.1 christos return EINVAL; 97 1.6 christos 98 1.4 christos (void)memcpy(bf, TTY_TEMPLATE, TTY_NAMESIZE); 99 1.1 christos 100 1.1 christos if (minor < 256) { 101 1.6 christos suffix = TTY_OLD_SUFFIX; 102 1.1 christos nt = sizeof(TTY_OLD_SUFFIX) - 1; 103 1.1 christos } else { 104 1.1 christos minor -= 256; 105 1.6 christos suffix = TTY_NEW_SUFFIX; 106 1.6 christos nt = sizeof(TTY_NEW_SUFFIX) - 1; 107 1.1 christos } 108 1.6 christos 109 1.6 christos bf[5] = c; 110 1.6 christos bf[8] = TTY_LETTERS[minor / nt]; 111 1.6 christos bf[9] = suffix[minor % nt]; 112 1.1 christos return 0; 113 1.1 christos } 114 1.1 christos 115 1.1 christos 116 1.1 christos static int 117 1.1 christos /*ARGSUSED*/ 118 1.19 christos pty_allocvp(struct mount *mp, struct lwp *l, struct vnode **vp, dev_t dev, 119 1.1 christos char ms) 120 1.1 christos { 121 1.1 christos int error; 122 1.17 dholland struct pathbuf *pb; 123 1.1 christos struct nameidata nd; 124 1.1 christos char name[TTY_NAMESIZE]; 125 1.1 christos 126 1.19 christos error = pty_makename(NULL, l, name, sizeof(name), dev, ms); 127 1.1 christos if (error) 128 1.1 christos return error; 129 1.1 christos 130 1.17 dholland pb = pathbuf_create(name); 131 1.17 dholland if (pb == NULL) { 132 1.17 dholland return ENOMEM; 133 1.17 dholland } 134 1.17 dholland 135 1.17 dholland NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, pb); 136 1.17 dholland if ((error = namei(&nd)) != 0) { 137 1.17 dholland pathbuf_destroy(pb); 138 1.1 christos return error; 139 1.17 dholland } 140 1.1 christos *vp = nd.ni_vp; 141 1.17 dholland pathbuf_destroy(pb); 142 1.1 christos return 0; 143 1.1 christos } 144 1.2 christos 145 1.2 christos 146 1.2 christos static void 147 1.2 christos /*ARGSUSED*/ 148 1.19 christos pty_getvattr(struct mount *mp, struct lwp *l, struct vattr *vattr) 149 1.2 christos { 150 1.16 pooka vattr_null(vattr); 151 1.2 christos /* get real uid */ 152 1.9 ad vattr->va_uid = kauth_cred_getuid(l->l_cred); 153 1.2 christos vattr->va_gid = TTY_GID; 154 1.2 christos vattr->va_mode = TTY_PERM; 155 1.2 christos } 156 1.20 christos 157 1.20 christos static int 158 1.20 christos pty__getmp(struct lwp *l __unused, struct mount **mpp) 159 1.20 christos { 160 1.20 christos *mpp = 0; 161 1.20 christos return 0; 162 1.20 christos } 163 1.20 christos 164 1.19 christos #endif /* COMPAT_BSDPTY */ 165 1.19 christos #endif /* NO_DEV_PTM */ 166