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: src/sys/kern/tty_cons.c,v 1.81.2.4 2001/12/17 18:44:41 guido Exp $ 40 * $DragonFly: src/sys/kern/tty_cons.c,v 1.21 2007/05/07 05:21:41 dillon Exp $ 41 */ 42 43 #include "opt_ddb.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/conf.h> 48 #include <sys/cons.h> 49 #include <sys/kernel.h> 50 #include <sys/proc.h> 51 #include <sys/priv.h> 52 #include <sys/reboot.h> 53 #include <sys/sysctl.h> 54 #include <sys/tty.h> 55 #include <sys/uio.h> 56 #include <sys/msgport.h> 57 #include <sys/msgport2.h> 58 #include <sys/device.h> 59 60 #include <ddb/ddb.h> 61 62 #include <machine/cpu.h> 63 64 static d_open_t cnopen; 65 static d_close_t cnclose; 66 static d_read_t cnread; 67 static d_write_t cnwrite; 68 static d_ioctl_t cnioctl; 69 static d_poll_t cnpoll; 70 static d_kqfilter_t cnkqfilter; 71 72 static int cnintercept(struct dev_generic_args *ap); 73 74 #define CDEV_MAJOR 0 75 static struct dev_ops cn_ops = { 76 { "console", CDEV_MAJOR, D_TTY | D_KQFILTER }, 77 .d_open = cnopen, 78 .d_close = cnclose, 79 .d_read = cnread, 80 .d_write = cnwrite, 81 .d_ioctl = cnioctl, 82 .d_poll = cnpoll, 83 .d_kqfilter = cnkqfilter, 84 }; 85 86 static struct dev_ops cn_iops = { 87 { "intercept", CDEV_MAJOR, D_TTY | D_KQFILTER }, 88 .d_default = cnintercept 89 }; 90 91 static struct dev_ops *cn_fwd_ops; 92 static cdev_t cn_dev; 93 static udev_t cn_udev; 94 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD, 95 &cn_udev, sizeof cn_udev, "T,udev_t", ""); 96 97 static int cn_mute; 98 99 int cons_unavail = 0; /* XXX: 100 * physical console not available for 101 * input (i.e., it is in graphics mode) 102 */ 103 104 static u_char cn_is_open; /* nonzero if logical console is open */ 105 static int openmode, openflag; /* how /dev/console was openned */ 106 static cdev_t cn_devfsdev; /* represents the device private info */ 107 static u_char cn_phys_is_open; /* nonzero if physical device is open */ 108 static u_char console_pausing; /* pause after each line during probe */ 109 static char *console_pausestr= 110 "<pause; press any key to proceed to next line or '.' to end pause mode>"; 111 112 struct consdev *cn_tab; /* physical console device info */ 113 struct consdev *gdb_tab; /* physical gdb debugger device info */ 114 115 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 116 SET_DECLARE(cons_set, struct consdev); 117 118 void 119 cninit(void) 120 { 121 struct consdev *best_cp, *cp, **list; 122 123 /* 124 * Find the first console with the highest priority. 125 */ 126 best_cp = NULL; 127 SET_FOREACH(list, cons_set) { 128 cp = *list; 129 if (cp->cn_probe == NULL) 130 continue; 131 (*cp->cn_probe)(cp); 132 if (cp->cn_pri > CN_DEAD && cp->cn_probegood && 133 (best_cp == NULL || cp->cn_pri > best_cp->cn_pri)) 134 best_cp = cp; 135 } 136 137 /* 138 * Check if we should mute the console (for security reasons perhaps) 139 * It can be changes dynamically using sysctl kern.consmute 140 * once we are up and going. 141 * 142 */ 143 cn_mute = ((boothowto & (RB_MUTE 144 |RB_SINGLE 145 |RB_VERBOSE 146 |RB_ASKNAME 147 |RB_CONFIG)) == RB_MUTE); 148 149 /* 150 * If no console, give up. 151 */ 152 if (best_cp == NULL) { 153 if (cn_tab != NULL && cn_tab->cn_term != NULL) 154 (*cn_tab->cn_term)(cn_tab); 155 cn_tab = best_cp; 156 return; 157 } 158 159 /* 160 * Initialize console, then attach to it. This ordering allows 161 * debugging using the previous console, if any. 162 */ 163 (*best_cp->cn_init)(best_cp); 164 if (cn_tab != NULL && cn_tab != best_cp) { 165 /* Turn off the previous console. */ 166 if (cn_tab->cn_term != NULL) 167 (*cn_tab->cn_term)(cn_tab); 168 } 169 if (boothowto & RB_PAUSE) 170 console_pausing = 1; 171 cn_tab = best_cp; 172 } 173 174 /* 175 * Hook the open and close functions on the selected device. 176 */ 177 void 178 cninit_finish(void) 179 { 180 if ((cn_tab == NULL) || cn_mute) 181 return; 182 183 if (cn_tab->cn_dev == NULL) { 184 cn_tab->cn_init_fini(cn_tab); 185 } 186 if (cn_tab->cn_dev == NULL) { 187 kprintf("Unable to hook console open and close! cn_tab %p\n", 188 cn_tab); 189 return; 190 } 191 192 /* 193 * Hook the open and close functions. XXX bad hack. 194 */ 195 if (dev_is_good(cn_tab->cn_dev)) { 196 cn_fwd_ops = dev_ops_intercept(cn_tab->cn_dev, &cn_iops); 197 } 198 cn_dev = cn_tab->cn_dev; 199 cn_udev = dev2udev(cn_dev); 200 console_pausing = 0; 201 } 202 203 static void 204 cnuninit(void) 205 { 206 if (cn_tab == NULL) 207 return; 208 209 /* 210 * Unhook the open and close functions. XXX bad hack 211 */ 212 if (cn_fwd_ops) 213 dev_ops_restore(cn_tab->cn_dev, cn_fwd_ops); 214 cn_fwd_ops = NULL; 215 cn_dev = NULL; 216 cn_udev = NOUDEV; 217 } 218 219 /* 220 * User has changed the state of the console muting. 221 * This may require us to open or close the device in question. 222 */ 223 static int 224 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS) 225 { 226 int error; 227 int ocn_mute; 228 229 ocn_mute = cn_mute; 230 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 231 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) { 232 if(ocn_mute && !cn_mute) { 233 /* 234 * going from muted to unmuted.. open the physical dev 235 * if the console has been openned 236 */ 237 cninit_finish(); 238 if (cn_is_open) { 239 /* XXX curproc is not what we want really */ 240 error = dev_dopen(cn_dev, openflag, 241 openmode, curproc->p_ucred); 242 } 243 /* if it failed, back it out */ 244 if ( error != 0) cnuninit(); 245 } else if (!ocn_mute && cn_mute) { 246 /* 247 * going from unmuted to muted.. close the physical dev 248 * if it's only open via /dev/console 249 */ 250 if (cn_is_open) { 251 error = dev_dclose(cn_dev, openflag, 252 openmode); 253 } 254 if (error == 0) 255 cnuninit(); 256 } 257 if (error != 0) { 258 /* 259 * back out the change if there was an error 260 */ 261 cn_mute = ocn_mute; 262 } 263 } 264 return (error); 265 } 266 267 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 268 0, sizeof cn_mute, sysctl_kern_consmute, "I", ""); 269 270 /* 271 * We intercept the OPEN and CLOSE calls on the original device, and 272 * forward the rest through. 273 */ 274 static int 275 cnintercept(struct dev_generic_args *ap) 276 { 277 int error; 278 279 if (ap->a_desc == &dev_open_desc) { 280 error = cnopen((struct dev_open_args *)ap); 281 } else if (ap->a_desc == &dev_close_desc) { 282 error = cnclose((struct dev_close_args *)ap); 283 } else if (cn_fwd_ops) { 284 error = dev_doperate_ops(cn_fwd_ops, ap); 285 } else { 286 error = ENXIO; 287 } 288 return (error); 289 } 290 291 /* 292 * cnopen() is called as an intercept function (dev will be that of the 293 * actual physical device representing our console), and also called from 294 * the muting code and from the /dev/console switch (dev will have the 295 * console's cdevsw). 296 */ 297 static int 298 cnopen(struct dev_open_args *ap) 299 { 300 cdev_t dev = ap->a_head.a_dev; 301 int flag = ap->a_oflags; 302 int mode = ap->a_devtype; 303 cdev_t cndev, physdev; 304 int retval = 0; 305 306 if (cn_tab == NULL || cn_fwd_ops == NULL) 307 return (0); 308 cndev = cn_tab->cn_dev; 309 physdev = (major(dev) == major(cndev) ? dev : cndev); 310 311 /* 312 * If mute is active, then non console opens don't get here 313 * so we don't need to check for that. They bypass this and go 314 * straight to the device. 315 * 316 * It is important to note that due to our intercept and the fact 317 * that we might be called via the original (intercepted) device, 318 * the original device's ops may point to us, so to avoid an 319 * infinite recursion we have to forward through cn_fwd_ops. 320 * This is a severe hack that really needs to be fixed XXX. 321 * 322 * XXX at the moment we assume that the port forwarding function 323 * is synchronous for open. 324 */ 325 if (!cn_mute) { 326 ap->a_head.a_dev = physdev; 327 retval = dev_doperate_ops(cn_fwd_ops, &ap->a_head); 328 } 329 if (retval == 0) { 330 /* 331 * check if we openned it via /dev/console or 332 * via the physical entry (e.g. /dev/sio0). 333 */ 334 if (dev == cndev) 335 cn_phys_is_open = 1; 336 else if (physdev == cndev) { 337 openmode = mode; 338 openflag = flag; 339 cn_is_open = 1; 340 } 341 dev->si_tty = physdev->si_tty; 342 } 343 return (retval); 344 } 345 346 /* 347 * cnclose() is called as a port intercept function (dev will be that of the 348 * actual physical device representing our console), and also called from 349 * the muting code and from the /dev/console switch (dev will have the 350 * console's cdevsw). 351 */ 352 static int 353 cnclose(struct dev_close_args *ap) 354 { 355 cdev_t dev = ap->a_head.a_dev; 356 cdev_t cndev; 357 struct tty *cn_tp; 358 359 if (cn_tab == NULL || cn_fwd_ops == NULL) 360 return (0); 361 cndev = cn_tab->cn_dev; 362 cn_tp = cndev->si_tty; 363 /* 364 * act appropriatly depending on whether it's /dev/console 365 * or the pysical device (e.g. /dev/sio) that's being closed. 366 * in either case, don't actually close the device unless 367 * both are closed. 368 */ 369 if (dev == cndev) { 370 /* the physical device is about to be closed */ 371 cn_phys_is_open = 0; 372 if (cn_is_open) { 373 if (cn_tp) { 374 /* perform a ttyhalfclose() */ 375 /* reset session and proc group */ 376 ttyclearsession(cn_tp); 377 } 378 return (0); 379 } 380 } else if (major(dev) != major(cndev)) { 381 /* the logical console is about to be closed */ 382 cn_is_open = 0; 383 if (cn_phys_is_open) 384 return (0); 385 dev = cndev; 386 } 387 if (cn_fwd_ops) { 388 ap->a_head.a_dev = dev; 389 return (dev_doperate_ops(cn_fwd_ops, &ap->a_head)); 390 } 391 return (0); 392 } 393 394 /* 395 * The following functions are dispatched solely from the /dev/console 396 * device. Their job is primarily to forward the request through. 397 * If the console is not attached to anything then write()'s are sunk 398 * to null and reads return 0 (mostly). 399 */ 400 static int 401 cnread(struct dev_read_args *ap) 402 { 403 if (cn_tab == NULL || cn_fwd_ops == NULL) 404 return (0); 405 ap->a_head.a_dev = cn_tab->cn_dev; 406 return (dev_doperate(&ap->a_head)); 407 } 408 409 static int 410 cnwrite(struct dev_write_args *ap) 411 { 412 struct uio *uio = ap->a_uio; 413 cdev_t dev; 414 415 if (cn_tab == NULL || cn_fwd_ops == NULL) { 416 uio->uio_resid = 0; /* dump the data */ 417 return (0); 418 } 419 if (constty) 420 dev = constty->t_dev; 421 else 422 dev = cn_tab->cn_dev; 423 log_console(uio); 424 ap->a_head.a_dev = dev; 425 return (dev_doperate(&ap->a_head)); 426 } 427 428 static int 429 cnioctl(struct dev_ioctl_args *ap) 430 { 431 int error; 432 433 if (cn_tab == NULL || cn_fwd_ops == NULL) 434 return (0); 435 KKASSERT(curproc != NULL); 436 /* 437 * Superuser can always use this to wrest control of console 438 * output from the "virtual" console. 439 */ 440 if (ap->a_cmd == TIOCCONS && constty) { 441 if (ap->a_cred) { 442 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); 443 if (error) 444 return (error); 445 } 446 constty = NULL; 447 return (0); 448 } 449 ap->a_head.a_dev = cn_tab->cn_dev; 450 return (dev_doperate(&ap->a_head)); 451 } 452 453 static int 454 cnpoll(struct dev_poll_args *ap) 455 { 456 if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL) 457 return (1); 458 ap->a_head.a_dev = cn_tab->cn_dev; 459 return (dev_doperate(&ap->a_head)); 460 } 461 462 static int 463 cnkqfilter(struct dev_kqfilter_args *ap) 464 { 465 if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL) 466 return (1); 467 ap->a_head.a_dev = cn_tab->cn_dev; 468 return (dev_doperate(&ap->a_head)); 469 } 470 471 /* 472 * These synchronous functions are primarily used the kernel needs to 473 * access the keyboard (e.g. when running the debugger), or output data 474 * directly to the console. 475 */ 476 int 477 cngetc(void) 478 { 479 int c; 480 if ((cn_tab == NULL) || cn_mute) 481 return (-1); 482 c = (*cn_tab->cn_getc)(cn_tab->cn_dev); 483 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 484 return (c); 485 } 486 487 int 488 cncheckc(void) 489 { 490 if ((cn_tab == NULL) || cn_mute) 491 return (-1); 492 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev)); 493 } 494 495 void 496 cnputc(int c) 497 { 498 char *cp; 499 500 if ((cn_tab == NULL) || cn_mute) 501 return; 502 if (c) { 503 if (c == '\n') 504 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 505 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 506 #ifdef DDB 507 if (console_pausing && !db_active && (c == '\n')) { 508 #else 509 if (console_pausing && (c == '\n')) { 510 #endif 511 for(cp=console_pausestr; *cp != '\0'; cp++) 512 (*cn_tab->cn_putc)(cn_tab->cn_dev, *cp); 513 if (cngetc() == '.') 514 console_pausing = 0; 515 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 516 for(cp=console_pausestr; *cp != '\0'; cp++) 517 (*cn_tab->cn_putc)(cn_tab->cn_dev, ' '); 518 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 519 } 520 } 521 } 522 523 void 524 cndbctl(int on) 525 { 526 static int refcount; 527 528 if (cn_tab == NULL) 529 return; 530 if (!on) 531 refcount--; 532 if (refcount == 0 && cn_tab->cn_dbctl != NULL) 533 (*cn_tab->cn_dbctl)(cn_tab->cn_dev, on); 534 if (on) 535 refcount++; 536 } 537 538 static void 539 cn_drvinit(void *unused) 540 { 541 dev_ops_add(&cn_ops, 0, 0); 542 cn_devfsdev = make_dev(&cn_ops, 0, UID_ROOT, GID_WHEEL, 543 0600, "console"); 544 } 545 546 SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL) 547