1 /* $OpenBSD: cons.c,v 1.21 2011/04/19 21:53:36 chl 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/proc.h> 45 #include <sys/buf.h> 46 #include <sys/ioctl.h> 47 #include <sys/tty.h> 48 #include <sys/file.h> 49 #include <sys/conf.h> 50 #include <sys/vnode.h> 51 #include <sys/poll.h> 52 53 #include <dev/cons.h> 54 55 struct tty *constty = NULL; /* virtual console output device */ 56 struct vnode *cn_devvp; /* vnode for underlying device. */ 57 extern struct consdev *cn_tab; /* physical console device info */ 58 59 int 60 cnopen(dev_t dev, int flag, int mode, struct proc *p) 61 { 62 dev_t cndev; 63 64 if (cn_tab == NULL) 65 return (0); 66 67 /* 68 * always open the 'real' console device, so we don't get nailed 69 * later. This follows normal device semantics; they always get 70 * open() calls. 71 */ 72 cndev = cn_tab->cn_dev; 73 if (cndev == NODEV) 74 return (ENXIO); 75 #ifdef DIAGNOSTIC 76 if (cndev == dev) 77 panic("cnopen: recursive"); 78 #endif 79 if (cn_devvp == NULLVP) { 80 /* try to get a reference on its vnode, but fail silently */ 81 cdevvp(cndev, &cn_devvp); 82 } 83 return ((*cdevsw[major(cndev)].d_open)(cndev, flag, mode, p)); 84 } 85 86 int 87 cnclose(dev_t dev, int flag, int mode, struct proc *p) 88 { 89 struct vnode *vp; 90 91 if (cn_tab == NULL) 92 return (0); 93 94 /* 95 * If the real console isn't otherwise open, close it. 96 * If it's otherwise open, don't close it, because that'll 97 * screw up others who have it open. 98 */ 99 dev = cn_tab->cn_dev; 100 if (cn_devvp != NULLVP) { 101 /* release our reference to real dev's vnode */ 102 vrele(cn_devvp); 103 cn_devvp = NULLVP; 104 } 105 if (vfinddev(dev, VCHR, &vp) && vcount(vp)) 106 return (0); 107 return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p)); 108 } 109 110 int 111 cnread(dev_t dev, struct uio *uio, int flag) 112 { 113 114 /* 115 * If we would redirect input, punt. This will keep strange 116 * things from happening to people who are using the real 117 * console. Nothing should be using /dev/console for 118 * input (except a shell in single-user mode, but then, 119 * one wouldn't TIOCCONS then). 120 */ 121 if (constty != NULL) 122 return 0; 123 else if (cn_tab == NULL) 124 return ENXIO; 125 126 dev = cn_tab->cn_dev; 127 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag)); 128 } 129 130 int 131 cnwrite(dev_t dev, struct uio *uio, int flag) 132 { 133 134 /* 135 * Redirect output, if that's appropriate. 136 * If there's no real console, return ENXIO. 137 */ 138 if (constty != NULL) 139 dev = constty->t_dev; 140 else if (cn_tab == NULL) 141 return ENXIO; 142 else 143 dev = cn_tab->cn_dev; 144 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag)); 145 } 146 147 int 148 cnstop(struct tty *tp, int flag) 149 { 150 return (0); 151 } 152 153 int 154 cnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, 155 struct proc *p) 156 { 157 int error; 158 159 /* 160 * Superuser can always use this to wrest control of console 161 * output from the "virtual" console. 162 */ 163 if (cmd == TIOCCONS && constty != NULL) { 164 error = suser(p, SUSER_NOACCT); 165 if (error) 166 return (error); 167 constty = NULL; 168 return (0); 169 } 170 171 /* 172 * Redirect the ioctl, if that's appropriate. 173 * Note that strange things can happen, if a program does 174 * ioctls on /dev/console, then the console is redirected 175 * out from under it. 176 */ 177 if (constty != NULL) 178 dev = constty->t_dev; 179 else if (cn_tab == NULL) 180 return ENXIO; 181 else 182 dev = cn_tab->cn_dev; 183 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 184 } 185 186 /*ARGSUSED*/ 187 int 188 cnpoll(dev_t dev, int rw, struct proc *p) 189 { 190 191 /* 192 * Redirect the poll, if that's appropriate. 193 * I don't want to think of the possible side effects 194 * of console redirection here. 195 */ 196 if (constty != NULL) 197 dev = constty->t_dev; 198 else if (cn_tab == NULL) 199 return POLLERR; 200 else 201 dev = cn_tab->cn_dev; 202 return (ttpoll(dev, rw, p)); 203 } 204 205 206 int 207 cnkqfilter(dev_t dev, struct knote *kn) 208 { 209 210 /* 211 * Redirect output, if that's appropriate. 212 * If there's no real console, return 1. 213 */ 214 if (constty != NULL) 215 dev = constty->t_dev; 216 else if (cn_tab == NULL) 217 return (1); 218 else 219 dev = cn_tab->cn_dev; 220 if (cdevsw[major(dev)].d_flags & D_KQFILTER) 221 return ((*cdevsw[major(dev)].d_kqfilter)(dev, kn)); 222 return (1); 223 } 224 225 int 226 cngetc(void) 227 { 228 229 if (cn_tab == NULL) 230 return (0); 231 return ((*cn_tab->cn_getc)(cn_tab->cn_dev)); 232 } 233 234 void 235 cnputc(int c) 236 { 237 238 if (cn_tab == NULL) 239 return; 240 241 if (c) { 242 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 243 if (c == '\n') 244 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 245 } 246 } 247 248 void 249 cnpollc(int on) 250 { 251 static int refcount = 0; 252 253 if (cn_tab == NULL) 254 return; 255 if (!on) 256 --refcount; 257 if (refcount == 0) 258 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on); 259 if (on) 260 ++refcount; 261 } 262 263 void 264 nullcnpollc(dev_t dev, int on) 265 { 266 267 } 268 269 void 270 cnbell(u_int pitch, u_int period, u_int volume) 271 { 272 if (cn_tab == NULL || cn_tab->cn_bell == NULL) 273 return; 274 275 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume); 276 } 277