kern_cons.c revision 69929
1/* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1991 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department. 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91 39 * $FreeBSD: head/sys/kern/tty_cons.c 69929 2000-12-12 21:18:13Z obrien $ 40 */ 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/conf.h> 45#include <sys/cons.h> 46#include <sys/kernel.h> 47#include <sys/proc.h> 48#include <sys/reboot.h> 49#include <sys/sysctl.h> 50#include <sys/tty.h> 51#include <sys/proc.h> 52#include <sys/uio.h> 53 54#include <machine/cpu.h> 55 56static d_open_t cnopen; 57static d_close_t cnclose; 58static d_read_t cnread; 59static d_write_t cnwrite; 60static d_ioctl_t cnioctl; 61static d_poll_t cnpoll; 62 63#define CDEV_MAJOR 0 64static struct cdevsw cn_cdevsw = { 65 /* open */ cnopen, 66 /* close */ cnclose, 67 /* read */ cnread, 68 /* write */ cnwrite, 69 /* ioctl */ cnioctl, 70 /* poll */ cnpoll, 71 /* mmap */ nommap, 72 /* strategy */ nostrategy, 73 /* name */ "console", 74 /* maj */ CDEV_MAJOR, 75 /* dump */ nodump, 76 /* psize */ nopsize, 77 /* flags */ D_TTY, 78 /* bmaj */ -1 79}; 80 81static dev_t cn_dev_t; /* seems to be never really used */ 82static udev_t cn_udev_t; 83SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD, 84 &cn_udev_t, sizeof cn_udev_t, "T,dev_t", ""); 85 86static int cn_mute; 87 88int cons_unavail = 0; /* XXX: 89 * physical console not available for 90 * input (i.e., it is in graphics mode) 91 */ 92 93static u_char cn_is_open; /* nonzero if logical console is open */ 94static int openmode, openflag; /* how /dev/console was openned */ 95static dev_t cn_devfsdev; /* represents the device private info */ 96static u_char cn_phys_is_open; /* nonzero if physical device is open */ 97static d_close_t *cn_phys_close; /* physical device close function */ 98static d_open_t *cn_phys_open; /* physical device open function */ 99 struct consdev *cn_tab; /* physical console device info */ 100 101CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 102 103void 104cninit() 105{ 106 struct consdev *best_cp, *cp, **list; 107 108 /* 109 * Find the first console with the highest priority. 110 */ 111 best_cp = NULL; 112 list = (struct consdev **)cons_set.ls_items; 113 while ((cp = *list++) != NULL) { 114 if (cp->cn_probe == NULL) 115 continue; 116 (*cp->cn_probe)(cp); 117 if (cp->cn_pri > CN_DEAD && 118 (best_cp == NULL || cp->cn_pri > best_cp->cn_pri)) 119 best_cp = cp; 120 } 121 122 /* 123 * Check if we should mute the console (for security reasons perhaps) 124 * It can be changes dynamically using sysctl kern.consmute 125 * once we are up and going. 126 * 127 */ 128 cn_mute = ((boothowto & (RB_MUTE 129 |RB_SINGLE 130 |RB_VERBOSE 131 |RB_ASKNAME 132 |RB_CONFIG)) == RB_MUTE); 133 134 /* 135 * If no console, give up. 136 */ 137 if (best_cp == NULL) { 138 if (cn_tab != NULL && cn_tab->cn_term != NULL) 139 (*cn_tab->cn_term)(cn_tab); 140 cn_tab = best_cp; 141 return; 142 } 143 144 /* 145 * Initialize console, then attach to it. This ordering allows 146 * debugging using the previous console, if any. 147 */ 148 (*best_cp->cn_init)(best_cp); 149 if (cn_tab != NULL && cn_tab != best_cp) { 150 /* Turn off the previous console. */ 151 if (cn_tab->cn_term != NULL) 152 (*cn_tab->cn_term)(cn_tab); 153 } 154 cn_tab = best_cp; 155} 156 157void 158cninit_finish() 159{ 160 struct cdevsw *cdp; 161 162 if ((cn_tab == NULL) || cn_mute) 163 return; 164 165 /* 166 * Hook the open and close functions. 167 */ 168 cdp = devsw(cn_tab->cn_dev); 169 if (cdp != NULL) { 170 cn_phys_close = cdp->d_close; 171 cdp->d_close = cnclose; 172 cn_phys_open = cdp->d_open; 173 cdp->d_open = cnopen; 174 } 175 cn_dev_t = cn_tab->cn_dev; 176 cn_udev_t = dev2udev(cn_dev_t); 177} 178 179static void 180cnuninit(void) 181{ 182 struct cdevsw *cdp; 183 184 if (cn_tab == NULL) 185 return; 186 187 /* 188 * Unhook the open and close functions. 189 */ 190 cdp = devsw(cn_tab->cn_dev); 191 if (cdp != NULL) { 192 cdp->d_close = cn_phys_close; 193 cdp->d_open = cn_phys_open; 194 } 195 cn_phys_close = NULL; 196 cn_phys_open = NULL; 197 cn_dev_t = NODEV; 198 cn_udev_t = NOUDEV; 199} 200 201/* 202 * User has changed the state of the console muting. 203 * This may require us to open or close the device in question. 204 */ 205static int 206sysctl_kern_consmute(SYSCTL_HANDLER_ARGS) 207{ 208 int error; 209 int ocn_mute; 210 211 ocn_mute = cn_mute; 212 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 213 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) { 214 if(ocn_mute && !cn_mute) { 215 /* 216 * going from muted to unmuted.. open the physical dev 217 * if the console has been openned 218 */ 219 cninit_finish(); 220 if(cn_is_open) 221 /* XXX curproc is not what we want really */ 222 error = cnopen(cn_dev_t, openflag, 223 openmode, curproc); 224 /* if it failed, back it out */ 225 if ( error != 0) cnuninit(); 226 } else if (!ocn_mute && cn_mute) { 227 /* 228 * going from unmuted to muted.. close the physical dev 229 * if it's only open via /dev/console 230 */ 231 if(cn_is_open) 232 error = cnclose(cn_dev_t, openflag, 233 openmode, curproc); 234 if ( error == 0) cnuninit(); 235 } 236 if (error != 0) { 237 /* 238 * back out the change if there was an error 239 */ 240 cn_mute = ocn_mute; 241 } 242 } 243 return (error); 244} 245 246SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 247 0, sizeof cn_mute, sysctl_kern_consmute, "I", ""); 248 249static int 250cnopen(dev, flag, mode, p) 251 dev_t dev; 252 int flag, mode; 253 struct proc *p; 254{ 255 dev_t cndev, physdev; 256 int retval = 0; 257 258 if (cn_tab == NULL || cn_phys_open == NULL) 259 return (0); 260 cndev = cn_tab->cn_dev; 261 physdev = (major(dev) == major(cndev) ? dev : cndev); 262 /* 263 * If mute is active, then non console opens don't get here 264 * so we don't need to check for that. They 265 * bypass this and go straight to the device. 266 */ 267 if(!cn_mute) 268 retval = (*cn_phys_open)(physdev, flag, mode, p); 269 if (retval == 0) { 270 /* 271 * check if we openned it via /dev/console or 272 * via the physical entry (e.g. /dev/sio0). 273 */ 274 if (dev == cndev) 275 cn_phys_is_open = 1; 276 else if (physdev == cndev) { 277 openmode = mode; 278 openflag = flag; 279 cn_is_open = 1; 280 } 281 dev->si_tty = physdev->si_tty; 282 } 283 return (retval); 284} 285 286static int 287cnclose(dev, flag, mode, p) 288 dev_t dev; 289 int flag, mode; 290 struct proc *p; 291{ 292 dev_t cndev; 293 struct tty *cn_tp; 294 295 if (cn_tab == NULL || cn_phys_open == NULL) 296 return (0); 297 cndev = cn_tab->cn_dev; 298 cn_tp = cndev->si_tty; 299 /* 300 * act appropriatly depending on whether it's /dev/console 301 * or the pysical device (e.g. /dev/sio) that's being closed. 302 * in either case, don't actually close the device unless 303 * both are closed. 304 */ 305 if (dev == cndev) { 306 /* the physical device is about to be closed */ 307 cn_phys_is_open = 0; 308 if (cn_is_open) { 309 if (cn_tp) { 310 /* perform a ttyhalfclose() */ 311 /* reset session and proc group */ 312 cn_tp->t_pgrp = NULL; 313 cn_tp->t_session = NULL; 314 } 315 return (0); 316 } 317 } else if (major(dev) != major(cndev)) { 318 /* the logical console is about to be closed */ 319 cn_is_open = 0; 320 if (cn_phys_is_open) 321 return (0); 322 dev = cndev; 323 } 324 if(cn_phys_close) 325 return ((*cn_phys_close)(dev, flag, mode, p)); 326 return (0); 327} 328 329static int 330cnread(dev, uio, flag) 331 dev_t dev; 332 struct uio *uio; 333 int flag; 334{ 335 336 if (cn_tab == NULL || cn_phys_open == NULL) 337 return (0); 338 dev = cn_tab->cn_dev; 339 return ((*devsw(dev)->d_read)(dev, uio, flag)); 340} 341 342static int 343cnwrite(dev, uio, flag) 344 dev_t dev; 345 struct uio *uio; 346 int flag; 347{ 348 349 if (cn_tab == NULL || cn_phys_open == NULL) { 350 uio->uio_resid = 0; /* dump the data */ 351 return (0); 352 } 353 if (constty) 354 dev = constty->t_dev; 355 else 356 dev = cn_tab->cn_dev; 357 return ((*devsw(dev)->d_write)(dev, uio, flag)); 358} 359 360static int 361cnioctl(dev, cmd, data, flag, p) 362 dev_t dev; 363 u_long cmd; 364 caddr_t data; 365 int flag; 366 struct proc *p; 367{ 368 int error; 369 370 if (cn_tab == NULL || cn_phys_open == NULL) 371 return (0); 372 /* 373 * Superuser can always use this to wrest control of console 374 * output from the "virtual" console. 375 */ 376 if (cmd == TIOCCONS && constty) { 377 error = suser(p); 378 if (error) 379 return (error); 380 constty = NULL; 381 return (0); 382 } 383 dev = cn_tab->cn_dev; 384 return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, p)); 385} 386 387static int 388cnpoll(dev, events, p) 389 dev_t dev; 390 int events; 391 struct proc *p; 392{ 393 if ((cn_tab == NULL) || cn_mute) 394 return (1); 395 396 dev = cn_tab->cn_dev; 397 398 return ((*devsw(dev)->d_poll)(dev, events, p)); 399} 400 401int 402cngetc() 403{ 404 int c; 405 if ((cn_tab == NULL) || cn_mute) 406 return (-1); 407 c = (*cn_tab->cn_getc)(cn_tab->cn_dev); 408 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 409 return (c); 410} 411 412int 413cncheckc() 414{ 415 if ((cn_tab == NULL) || cn_mute) 416 return (-1); 417 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev)); 418} 419 420void 421cnputc(c) 422 register int c; 423{ 424 if ((cn_tab == NULL) || cn_mute) 425 return; 426 if (c) { 427 if (c == '\n') 428 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 429 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 430 } 431} 432 433void 434cndbctl(on) 435 int on; 436{ 437 static int refcount; 438 439 if (cn_tab == NULL) 440 return; 441 if (!on) 442 refcount--; 443 if (refcount == 0 && cn_tab->cn_dbctl != NULL) 444 (*cn_tab->cn_dbctl)(cn_tab->cn_dev, on); 445 if (on) 446 refcount++; 447} 448 449static void 450cn_drvinit(void *unused) 451{ 452 453 cn_devfsdev = make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 454 "console"); 455} 456 457SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL) 458