1 /* $OpenBSD: cons.c,v 1.29 2020/04/03 08:24:52 mpi Exp $ */ 2 /* $NetBSD: cons.c,v 1.30 1996/04/08 19:57:30 jonathan Exp $ */ 3 4 /* 5 * Copyright (c) 1988 University of Utah. 6 * Copyright (c) 1990, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Systems Programming Group of the University of Utah Computer 11 * Science Department. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * from: Utah $Hdr: cons.c 1.7 92/01/21$ 38 * 39 * @(#)cons.c 8.2 (Berkeley) 1/12/94 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/ioctl.h> 45 #include <sys/tty.h> 46 #include <sys/conf.h> 47 #include <sys/vnode.h> 48 #include <sys/poll.h> 49 50 #include <dev/cons.h> 51 52 struct tty *constty = NULL; /* virtual console output device */ 53 struct vnode *cn_devvp = NULLVP; /* vnode for underlying device. */ 54 55 int 56 cnopen(dev_t dev, int flag, int mode, struct proc *p) 57 { 58 dev_t cndev; 59 60 if (cn_tab == NULL) 61 return (0); 62 63 /* 64 * always open the 'real' console device, so we don't get nailed 65 * later. This follows normal device semantics; they always get 66 * open() calls. 67 */ 68 cndev = cn_tab->cn_dev; 69 if (cndev == NODEV) 70 return (ENXIO); 71 #ifdef DIAGNOSTIC 72 if (cndev == dev) 73 panic("cnopen: recursive"); 74 #endif 75 if (cn_devvp == NULLVP) { 76 /* try to get a reference on its vnode, but fail silently */ 77 cdevvp(cndev, &cn_devvp); 78 } 79 return ((*cdevsw[major(cndev)].d_open)(cndev, flag, mode, p)); 80 } 81 82 int 83 cnclose(dev_t dev, int flag, int mode, struct proc *p) 84 { 85 struct vnode *vp; 86 87 if (cn_tab == NULL) 88 return (0); 89 90 /* 91 * If the real console isn't otherwise open, close it. 92 * If it's otherwise open, don't close it, because that'll 93 * screw up others who have it open. 94 */ 95 dev = cn_tab->cn_dev; 96 if (cn_devvp != NULLVP) { 97 /* release our reference to real dev's vnode */ 98 vrele(cn_devvp); 99 cn_devvp = NULLVP; 100 } 101 if (vfinddev(dev, VCHR, &vp) && vcount(vp)) 102 return (0); 103 return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p)); 104 } 105 106 int 107 cnread(dev_t dev, struct uio *uio, int flag) 108 { 109 110 /* 111 * If we would redirect input, punt. This will keep strange 112 * things from happening to people who are using the real 113 * console. Nothing should be using /dev/console for 114 * input (except a shell in single-user mode, but then, 115 * one wouldn't TIOCCONS then). 116 */ 117 if (constty != NULL) 118 return 0; 119 else if (cn_tab == NULL) 120 return ENXIO; 121 122 dev = cn_tab->cn_dev; 123 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag)); 124 } 125 126 int 127 cnwrite(dev_t dev, struct uio *uio, int flag) 128 { 129 130 /* 131 * Redirect output, if that's appropriate. 132 * If there's no real console, return ENXIO. 133 */ 134 if (constty != NULL) 135 dev = constty->t_dev; 136 else if (cn_tab == NULL) 137 return ENXIO; 138 else 139 dev = cn_tab->cn_dev; 140 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag)); 141 } 142 143 int 144 cnstop(struct tty *tp, int flag) 145 { 146 return (0); 147 } 148 149 int 150 cnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, 151 struct proc *p) 152 { 153 int error; 154 155 /* 156 * Superuser can always use this to wrest control of console 157 * output from the "virtual" console. 158 */ 159 if (cmd == TIOCCONS && constty != NULL) { 160 error = suser(p); 161 if (error) 162 return (error); 163 constty = NULL; 164 return (0); 165 } 166 167 /* 168 * Redirect the ioctl, if that's appropriate. 169 * Note that strange things can happen, if a program does 170 * ioctls on /dev/console, then the console is redirected 171 * out from under it. 172 */ 173 if (constty != NULL) 174 dev = constty->t_dev; 175 else if (cn_tab == NULL) 176 return ENXIO; 177 else 178 dev = cn_tab->cn_dev; 179 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 180 } 181 182 /*ARGSUSED*/ 183 int 184 cnpoll(dev_t dev, int rw, struct proc *p) 185 { 186 187 /* 188 * Redirect the poll, if that's appropriate. 189 * I don't want to think of the possible side effects 190 * of console redirection here. 191 */ 192 if (constty != NULL) 193 dev = constty->t_dev; 194 else if (cn_tab == NULL) 195 return POLLERR; 196 else 197 dev = cn_tab->cn_dev; 198 return (ttpoll(dev, rw, p)); 199 } 200 201 202 int 203 cnkqfilter(dev_t dev, struct knote *kn) 204 { 205 206 /* 207 * Redirect output, if that's appropriate. 208 * If there's no real console, return 1. 209 */ 210 if (constty != NULL) 211 dev = constty->t_dev; 212 else if (cn_tab == NULL) 213 return (ENXIO); 214 else 215 dev = cn_tab->cn_dev; 216 if (cdevsw[major(dev)].d_kqfilter) 217 return ((*cdevsw[major(dev)].d_kqfilter)(dev, kn)); 218 return (EOPNOTSUPP); 219 } 220 221 int 222 cngetc(void) 223 { 224 225 if (cn_tab == NULL) 226 return (0); 227 return ((*cn_tab->cn_getc)(cn_tab->cn_dev)); 228 } 229 230 void 231 cnputc(int c) 232 { 233 234 if (cn_tab == NULL) 235 return; 236 237 if (c) { 238 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 239 if (c == '\n') 240 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 241 } 242 } 243 244 void 245 cnpollc(int on) 246 { 247 static int refcount = 0; 248 249 if (cn_tab == NULL) 250 return; 251 if (!on) 252 --refcount; 253 if (refcount == 0) 254 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on); 255 if (on) 256 ++refcount; 257 } 258 259 void 260 nullcnpollc(dev_t dev, int on) 261 { 262 263 } 264 265 void 266 cnbell(u_int pitch, u_int period, u_int volume) 267 { 268 if (cn_tab == NULL || cn_tab->cn_bell == NULL) 269 return; 270 271 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume); 272 } 273