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