kern_cons.c revision 116663
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 */ 40 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/sys/kern/tty_cons.c 116663 2003-06-22 02:54:33Z iedowse $"); 43 44#include "opt_ddb.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/conf.h> 49#include <sys/cons.h> 50#include <sys/fcntl.h> 51#include <sys/kernel.h> 52#include <sys/malloc.h> 53#include <sys/msgbuf.h> 54#include <sys/namei.h> 55#include <sys/proc.h> 56#include <sys/queue.h> 57#include <sys/reboot.h> 58#include <sys/sysctl.h> 59#include <sys/tty.h> 60#include <sys/uio.h> 61#include <sys/vnode.h> 62 63#include <ddb/ddb.h> 64 65#include <machine/cpu.h> 66 67static d_open_t cnopen; 68static d_close_t cnclose; 69static d_read_t cnread; 70static d_write_t cnwrite; 71static d_ioctl_t cnioctl; 72static d_poll_t cnpoll; 73static d_kqfilter_t cnkqfilter; 74 75static struct cdevsw cn_cdevsw = { 76 .d_open = cnopen, 77 .d_close = cnclose, 78 .d_read = cnread, 79 .d_write = cnwrite, 80 .d_ioctl = cnioctl, 81 .d_poll = cnpoll, 82 .d_name = "console", 83 .d_maj = 256, 84 /* 85 * XXX: We really want major #0, but zero here means 86 * XXX: allocate a major number automatically. 87 * XXX: kern_conf.c knows what to do when it sees 256. 88 */ 89 .d_flags = D_TTY, 90 .d_kqfilter = cnkqfilter, 91}; 92 93struct cn_device { 94 STAILQ_ENTRY(cn_device) cnd_next; 95 char cnd_name[16]; 96 struct vnode *cnd_vp; 97 struct consdev *cnd_cn; 98}; 99 100#define CNDEVPATHMAX 32 101#define CNDEVTAB_SIZE 4 102static struct cn_device cn_devtab[CNDEVTAB_SIZE]; 103static STAILQ_HEAD(, cn_device) cn_devlist = 104 STAILQ_HEAD_INITIALIZER(cn_devlist); 105 106#define CND_INVALID(cnd, td) \ 107 (cnd == NULL || cnd->cnd_vp == NULL || \ 108 (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1))) 109 110static udev_t cn_udev_t; 111SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD, 112 &cn_udev_t, sizeof cn_udev_t, "T,dev_t", ""); 113 114int cons_unavail = 0; /* XXX: 115 * physical console not available for 116 * input (i.e., it is in graphics mode) 117 */ 118static int cn_mute; 119static int openflag; /* how /dev/console was opened */ 120static int cn_is_open; 121static char *consbuf; /* buffer used by `consmsgbuf' */ 122static struct callout conscallout; /* callout for outputting to constty */ 123struct msgbuf consmsgbuf; /* message buffer for console tty */ 124static u_char console_pausing; /* pause after each line during probe */ 125static char *console_pausestr= 126"<pause; press any key to proceed to next line or '.' to end pause mode>"; 127struct tty *constty; /* pointer to console "window" tty */ 128 129void cndebug(char *); 130static void constty_timeout(void *arg); 131 132CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 133SET_DECLARE(cons_set, struct consdev); 134 135void 136cninit(void) 137{ 138 struct consdev *best_cn, *cn, **list; 139 140 /* 141 * Check if we should mute the console (for security reasons perhaps) 142 * It can be changes dynamically using sysctl kern.consmute 143 * once we are up and going. 144 * 145 */ 146 cn_mute = ((boothowto & (RB_MUTE 147 |RB_SINGLE 148 |RB_VERBOSE 149 |RB_ASKNAME 150 |RB_CONFIG)) == RB_MUTE); 151 152 /* 153 * Find the first console with the highest priority. 154 */ 155 best_cn = NULL; 156 SET_FOREACH(list, cons_set) { 157 cn = *list; 158 cnremove(cn); 159 if (cn->cn_probe == NULL) 160 continue; 161 cn->cn_probe(cn); 162 if (cn->cn_pri == CN_DEAD) 163 continue; 164 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri) 165 best_cn = cn; 166 if (boothowto & RB_MULTIPLE) { 167 /* 168 * Initialize console, and attach to it. 169 */ 170 cnadd(cn); 171 cn->cn_init(cn); 172 } 173 } 174 if (best_cn == NULL) 175 return; 176 if ((boothowto & RB_MULTIPLE) == 0) { 177 cnadd(best_cn); 178 best_cn->cn_init(best_cn); 179 } 180 if (boothowto & RB_PAUSE) 181 console_pausing = 1; 182 /* 183 * Make the best console the preferred console. 184 */ 185 cnselect(best_cn); 186} 187 188void 189cninit_finish() 190{ 191 console_pausing = 0; 192} 193 194/* add a new physical console to back the virtual console */ 195int 196cnadd(struct consdev *cn) 197{ 198 struct cn_device *cnd; 199 int i; 200 201 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) 202 if (cnd->cnd_cn == cn) 203 return (0); 204 for (i = 0; i < CNDEVTAB_SIZE; i++) { 205 cnd = &cn_devtab[i]; 206 if (cnd->cnd_cn == NULL) 207 break; 208 } 209 if (cnd->cnd_cn != NULL) 210 return (ENOMEM); 211 cnd->cnd_cn = cn; 212 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next); 213 return (0); 214} 215 216void 217cnremove(struct consdev *cn) 218{ 219 struct cn_device *cnd; 220 221 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 222 if (cnd->cnd_cn != cn) 223 continue; 224 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next); 225 if (cnd->cnd_vp != NULL) 226 vn_close(cnd->cnd_vp, openflag, NOCRED, NULL); 227 cnd->cnd_vp = NULL; 228 cnd->cnd_cn = NULL; 229 cnd->cnd_name[0] = '\0'; 230#if 0 231 /* 232 * XXX 233 * syscons gets really confused if console resources are 234 * freed after the system has initialized. 235 */ 236 if (cn->cn_term != NULL) 237 cn->cn_term(cn); 238#endif 239 return; 240 } 241} 242 243void 244cnselect(struct consdev *cn) 245{ 246 struct cn_device *cnd; 247 248 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 249 if (cnd->cnd_cn != cn) 250 continue; 251 if (cnd == STAILQ_FIRST(&cn_devlist)) 252 return; 253 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next); 254 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next); 255 return; 256 } 257} 258 259void 260cndebug(char *str) 261{ 262 int i, len; 263 264 len = strlen(str); 265 cnputc('>'); cnputc('>'); cnputc('>'); cnputc(' '); 266 for (i = 0; i < len; i++) 267 cnputc(str[i]); 268 cnputc('\n'); 269} 270 271static int 272sysctl_kern_console(SYSCTL_HANDLER_ARGS) 273{ 274 struct cn_device *cnd; 275 struct consdev *cp, **list; 276 char *name, *p; 277 int delete, len, error; 278 279 len = 2; 280 SET_FOREACH(list, cons_set) { 281 cp = *list; 282 if (cp->cn_dev != NULL) 283 len += strlen(devtoname(cp->cn_dev)) + 1; 284 } 285 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) 286 len += strlen(devtoname(cnd->cnd_cn->cn_dev)) + 1; 287 len = len > CNDEVPATHMAX ? len : CNDEVPATHMAX; 288 MALLOC(name, char *, len, M_TEMP, M_WAITOK | M_ZERO); 289 p = name; 290 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) 291 p += sprintf(p, "%s,", devtoname(cnd->cnd_cn->cn_dev)); 292 *p++ = '/'; 293 SET_FOREACH(list, cons_set) { 294 cp = *list; 295 if (cp->cn_dev != NULL) 296 p += sprintf(p, "%s,", devtoname(cp->cn_dev)); 297 } 298 error = sysctl_handle_string(oidp, name, len, req); 299 if (error == 0 && req->newptr != NULL) { 300 p = name; 301 error = ENXIO; 302 delete = 0; 303 if (*p == '-') { 304 delete = 1; 305 p++; 306 } 307 SET_FOREACH(list, cons_set) { 308 cp = *list; 309 if (cp->cn_dev == NULL || 310 strcmp(p, devtoname(cp->cn_dev)) != 0) 311 continue; 312 if (delete) { 313 cnremove(cp); 314 error = 0; 315 } else { 316 error = cnadd(cp); 317 if (error == 0) 318 cnselect(cp); 319 } 320 break; 321 } 322 } 323 FREE(name, M_TEMP); 324 return (error); 325} 326 327SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW, 328 0, 0, sysctl_kern_console, "A", "Console device control"); 329 330/* 331 * User has changed the state of the console muting. 332 * This may require us to open or close the device in question. 333 */ 334static int 335sysctl_kern_consmute(SYSCTL_HANDLER_ARGS) 336{ 337 int error; 338 int ocn_mute; 339 340 ocn_mute = cn_mute; 341 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 342 if (error != 0 || req->newptr == NULL) 343 return (error); 344 if (ocn_mute && !cn_mute && cn_is_open) 345 error = cnopen(NODEV, openflag, 0, curthread); 346 else if (!ocn_mute && cn_mute && cn_is_open) { 347 error = cnclose(NODEV, openflag, 0, curthread); 348 cn_is_open = 1; /* XXX hack */ 349 } 350 return (error); 351} 352 353SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 354 0, sizeof(cn_mute), sysctl_kern_consmute, "I", ""); 355 356static int 357cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen) 358{ 359 char path[CNDEVPATHMAX]; 360 struct nameidata nd; 361 struct vnode *vp; 362 dev_t dev; 363 int error; 364 365 if ((vp = cnd->cnd_vp) != NULL) { 366 if (!forceopen && vp->v_type != VBAD) { 367 dev = vp->v_rdev; 368 return ((*devsw(dev)->d_open)(dev, openflag, 0, td)); 369 } 370 cnd->cnd_vp = NULL; 371 vn_close(vp, openflag, td->td_ucred, td); 372 } 373 if (cnd->cnd_name[0] == '\0') { 374 strlcpy(cnd->cnd_name, devtoname(cnd->cnd_cn->cn_dev), 375 sizeof(cnd->cnd_name)); 376 } 377 snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_name); 378 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td); 379 error = vn_open(&nd, &openflag, 0); 380 if (error == 0) { 381 NDFREE(&nd, NDF_ONLY_PNBUF); 382 VOP_UNLOCK(nd.ni_vp, 0, td); 383 if (nd.ni_vp->v_type == VCHR) 384 cnd->cnd_vp = nd.ni_vp; 385 else 386 vn_close(nd.ni_vp, openflag, td->td_ucred, td); 387 } 388 return (cnd->cnd_vp != NULL); 389} 390 391static int 392cnopen(dev_t dev, int flag, int mode, struct thread *td) 393{ 394 struct cn_device *cnd; 395 396 openflag = flag | FWRITE; /* XXX */ 397 cn_is_open = 1; /* console is logically open */ 398 if (cn_mute) 399 return (0); 400 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) 401 cn_devopen(cnd, td, 0); 402 return (0); 403} 404 405static int 406cnclose(dev_t dev, int flag, int mode, struct thread *td) 407{ 408 struct cn_device *cnd; 409 struct vnode *vp; 410 411 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 412 if ((vp = cnd->cnd_vp) == NULL) 413 continue; 414 cnd->cnd_vp = NULL; 415 vn_close(vp, openflag, td->td_ucred, td); 416 } 417 cn_is_open = 0; 418 return (0); 419} 420 421static int 422cnread(dev_t dev, struct uio *uio, int flag) 423{ 424 struct cn_device *cnd; 425 426 cnd = STAILQ_FIRST(&cn_devlist); 427 if (cn_mute || CND_INVALID(cnd, curthread)) 428 return (0); 429 dev = cnd->cnd_vp->v_rdev; 430 return ((*devsw(dev)->d_read)(dev, uio, flag)); 431} 432 433static int 434cnwrite(dev_t dev, struct uio *uio, int flag) 435{ 436 struct cn_device *cnd; 437 438 cnd = STAILQ_FIRST(&cn_devlist); 439 if (cn_mute || CND_INVALID(cnd, curthread)) 440 goto done; 441 if (constty) 442 dev = constty->t_dev; 443 else 444 dev = cnd->cnd_vp->v_rdev; 445 if (dev != NULL) { 446 log_console(uio); 447 return ((*devsw(dev)->d_write)(dev, uio, flag)); 448 } 449done: 450 uio->uio_resid = 0; /* dump the data */ 451 return (0); 452} 453 454static int 455cnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 456{ 457 struct cn_device *cnd; 458 int error; 459 460 cnd = STAILQ_FIRST(&cn_devlist); 461 if (cn_mute || CND_INVALID(cnd, td)) 462 return (0); 463 /* 464 * Superuser can always use this to wrest control of console 465 * output from the "virtual" console. 466 */ 467 if (cmd == TIOCCONS && constty) { 468 error = suser(td); 469 if (error) 470 return (error); 471 constty = NULL; 472 return (0); 473 } 474 dev = cnd->cnd_vp->v_rdev; 475 if (dev != NULL) 476 return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, td)); 477 return (0); 478} 479 480/* 481 * XXX 482 * poll/kqfilter do not appear to be correct 483 */ 484static int 485cnpoll(dev_t dev, int events, struct thread *td) 486{ 487 struct cn_device *cnd; 488 489 cnd = STAILQ_FIRST(&cn_devlist); 490 if (cn_mute || CND_INVALID(cnd, td)) 491 return (0); 492 dev = cnd->cnd_vp->v_rdev; 493 if (dev != NULL) 494 return ((*devsw(dev)->d_poll)(dev, events, td)); 495 return (0); 496} 497 498static int 499cnkqfilter(dev_t dev, struct knote *kn) 500{ 501 struct cn_device *cnd; 502 503 cnd = STAILQ_FIRST(&cn_devlist); 504 if (cn_mute || CND_INVALID(cnd, curthread)) 505 return (1); 506 dev = cnd->cnd_vp->v_rdev; 507 if (dev != NULL) 508 return ((*devsw(dev)->d_kqfilter)(dev, kn)); 509 return (1); 510} 511 512/* 513 * Low level console routines. 514 */ 515int 516cngetc(void) 517{ 518 int c; 519 520 if (cn_mute) 521 return (-1); 522 while ((c = cncheckc()) == -1) 523 ; 524 if (c == '\r') 525 c = '\n'; /* console input is always ICRNL */ 526 return (c); 527} 528 529int 530cncheckc(void) 531{ 532 struct cn_device *cnd; 533 struct consdev *cn; 534 int c; 535 536 if (cn_mute) 537 return (-1); 538 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 539 cn = cnd->cnd_cn; 540 c = cn->cn_checkc(cn); 541 if (c != -1) { 542 return (c); 543 } 544 } 545 return (-1); 546} 547 548void 549cnputc(int c) 550{ 551 struct cn_device *cnd; 552 struct consdev *cn; 553 char *cp; 554 555 if (cn_mute || c == '\0') 556 return; 557 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 558 cn = cnd->cnd_cn; 559 if (c == '\n') 560 cn->cn_putc(cn, '\r'); 561 cn->cn_putc(cn, c); 562 } 563#ifdef DDB 564 if (console_pausing && !db_active && (c == '\n')) { 565#else 566 if (console_pausing && (c == '\n')) { 567#endif 568 for (cp = console_pausestr; *cp != '\0'; cp++) 569 cnputc(*cp); 570 if (cngetc() == '.') 571 console_pausing = 0; 572 cnputc('\r'); 573 for (cp = console_pausestr; *cp != '\0'; cp++) 574 cnputc(' '); 575 cnputc('\r'); 576 } 577} 578 579void 580cndbctl(int on) 581{ 582 struct cn_device *cnd; 583 struct consdev *cn; 584 static int refcount; 585 586 if (!on) 587 refcount--; 588 if (refcount == 0) 589 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 590 cn = cnd->cnd_cn; 591 if (cn->cn_dbctl != NULL) 592 cn->cn_dbctl(cn, on); 593 } 594 if (on) 595 refcount++; 596} 597 598static int consmsgbuf_size = 8192; 599SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0, 600 ""); 601 602/* 603 * Redirect console output to a tty. 604 */ 605void 606constty_set(struct tty *tp) 607{ 608 int size; 609 610 KASSERT(tp != NULL, ("constty_set: NULL tp")); 611 if (consbuf == NULL) { 612 size = consmsgbuf_size; 613 consbuf = malloc(size, M_TTYS, M_WAITOK); 614 msgbuf_init(&consmsgbuf, consbuf, size); 615 callout_init(&conscallout, 0); 616 } 617 constty = tp; 618 constty_timeout(NULL); 619} 620 621/* 622 * Disable console redirection to a tty. 623 */ 624void 625constty_clear(void) 626{ 627 int c; 628 629 constty = NULL; 630 if (consbuf == NULL) 631 return; 632 callout_stop(&conscallout); 633 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) 634 cnputc(c); 635 free(consbuf, M_TTYS); 636 consbuf = NULL; 637} 638 639/* Times per second to check for pending console tty messages. */ 640static int constty_wakeups_per_second = 5; 641SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW, 642 &constty_wakeups_per_second, 0, ""); 643 644static void 645constty_timeout(void *arg) 646{ 647 int c; 648 649 while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) { 650 if (tputchar(c, constty) < 0) 651 constty = NULL; 652 } 653 if (constty != NULL) { 654 callout_reset(&conscallout, hz / constty_wakeups_per_second, 655 constty_timeout, NULL); 656 } else { 657 /* Deallocate the constty buffer memory. */ 658 constty_clear(); 659 } 660} 661 662static void 663cn_drvinit(void *unused) 664{ 665 666 make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console"); 667} 668 669SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL) 670