kern_cons.c revision 48104
11602Srgrimes/* 21602Srgrimes * Copyright (c) 1988 University of Utah. 31602Srgrimes * Copyright (c) 1991 The Regents of the University of California. 41602Srgrimes * All rights reserved. 51602Srgrimes * 61602Srgrimes * This code is derived from software contributed to Berkeley by 71602Srgrimes * the Systems Programming Group of the University of Utah Computer 81602Srgrimes * Science Department. 91602Srgrimes * 101602Srgrimes * Redistribution and use in source and binary forms, with or without 111602Srgrimes * modification, are permitted provided that the following conditions 121602Srgrimes * are met: 131602Srgrimes * 1. Redistributions of source code must retain the above copyright 141602Srgrimes * notice, this list of conditions and the following disclaimer. 151602Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161602Srgrimes * notice, this list of conditions and the following disclaimer in the 171602Srgrimes * documentation and/or other materials provided with the distribution. 181602Srgrimes * 3. All advertising materials mentioning features or use of this software 191602Srgrimes * must display the following acknowledgement: 201602Srgrimes * This product includes software developed by the University of 211602Srgrimes * California, Berkeley and its contributors. 221602Srgrimes * 4. Neither the name of the University nor the names of its contributors 231602Srgrimes * may be used to endorse or promote products derived from this software 241602Srgrimes * without specific prior written permission. 251602Srgrimes * 261602Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271602Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281602Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291602Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301602Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311602Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321602Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331602Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341602Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351602Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361602Srgrimes * SUCH DAMAGE. 371602Srgrimes * 381602Srgrimes * from: @(#)cons.c 7.2 (Berkeley) 5/9/91 391602Srgrimes * $Id: cons.c,v 1.67 1999/05/31 11:25:41 phk Exp $ 401602Srgrimes */ 411602Srgrimes 421602Srgrimes#include "opt_devfs.h" 431603Srgrimes 441602Srgrimes#include <sys/param.h> 451602Srgrimes#ifdef DEVFS 461602Srgrimes#include <sys/devfsext.h> 471602Srgrimes#endif /*DEVFS*/ 481602Srgrimes#include <sys/systm.h> 491602Srgrimes#include <sys/conf.h> 501602Srgrimes#include <sys/kernel.h> 511602Srgrimes#include <sys/reboot.h> 521602Srgrimes#include <sys/sysctl.h> 531602Srgrimes#include <sys/proc.h> 541602Srgrimes#include <sys/tty.h> 551602Srgrimes#include <sys/uio.h> 561602Srgrimes 571602Srgrimes#include <machine/cpu.h> 581602Srgrimes#include <machine/cons.h> 591602Srgrimes 601602Srgrimesstatic d_open_t cnopen; 611602Srgrimesstatic d_close_t cnclose; 621602Srgrimesstatic d_read_t cnread; 631602Srgrimesstatic d_write_t cnwrite; 641603Srgrimesstatic d_ioctl_t cnioctl; 651603Srgrimesstatic d_poll_t cnpoll; 661602Srgrimes 671602Srgrimes#define CDEV_MAJOR 0 681602Srgrimesstatic struct cdevsw cn_cdevsw = { 691603Srgrimes /* open */ cnopen, 701603Srgrimes /* close */ cnclose, 711602Srgrimes /* read */ cnread, 721602Srgrimes /* write */ cnwrite, 731602Srgrimes /* ioctl */ cnioctl, 741602Srgrimes /* stop */ nostop, 751602Srgrimes /* reset */ noreset, 761602Srgrimes /* devtotty */ nodevtotty, 771603Srgrimes /* poll */ cnpoll, 781603Srgrimes /* mmap */ nommap, 791603Srgrimes /* strategy */ nostrategy, 801603Srgrimes /* name */ "console", 811603Srgrimes /* parms */ noparms, 821602Srgrimes /* maj */ CDEV_MAJOR, 831603Srgrimes /* dump */ nodump, 841602Srgrimes /* psize */ nopsize, 851602Srgrimes /* flags */ D_TTY, 861602Srgrimes /* maxio */ 0, 871603Srgrimes /* bmaj */ -1 881602Srgrimes}; 891603Srgrimes 901602Srgrimesstatic dev_t cn_dev_t; /* seems to be never really used */ 911602SrgrimesSYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD, 921603Srgrimes &cn_dev_t, sizeof cn_dev_t, "T,dev_t", ""); 931603Srgrimes 941602Srgrimesstatic int cn_mute; 951603Srgrimes 961602Srgrimesint cons_unavail = 0; /* XXX: 971602Srgrimes * physical console not available for 981603Srgrimes * input (i.e., it is in graphics mode) 991603Srgrimes */ 1001602Srgrimes 1011602Srgrimesstatic u_char cn_is_open; /* nonzero if logical console is open */ 1021602Srgrimesstatic int openmode, openflag; /* how /dev/console was openned */ 1031602Srgrimesstatic u_char cn_phys_is_open; /* nonzero if physical device is open */ 1041602Srgrimesstatic d_close_t *cn_phys_close; /* physical device close function */ 1051603Srgrimesstatic d_open_t *cn_phys_open; /* physical device open function */ 1061603Srgrimesstatic struct consdev *cn_tab; /* physical console device info */ 1071603Srgrimesstatic struct tty *cn_tp; /* physical console tty struct */ 1081602Srgrimes#ifdef DEVFS 1091602Srgrimesstatic void *cn_devfs_token; /* represents the devfs entry */ 1101603Srgrimes#endif /* DEVFS */ 1111603Srgrimes 1121602SrgrimesCONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL); 1131603Srgrimes 1141603Srgrimesvoid 1151602Srgrimescninit() 1161602Srgrimes{ 1171602Srgrimes struct consdev *best_cp, *cp; 1181602Srgrimes const struct consdev **list; 1191602Srgrimes 1201602Srgrimes /* 1211603Srgrimes * Find the first console with the highest priority. 1221602Srgrimes */ 1231602Srgrimes best_cp = NULL; 1241602Srgrimes list = (const struct consdev **)cons_set.ls_items; 1251602Srgrimes while ((cp = *list++) != NULL) { 1261602Srgrimes if (cp->cn_probe == NULL) 1271602Srgrimes continue; 1281603Srgrimes (*cp->cn_probe)(cp); 1291602Srgrimes if (cp->cn_pri > CN_DEAD && 1301602Srgrimes (best_cp == NULL || cp->cn_pri > best_cp->cn_pri)) 1311602Srgrimes best_cp = cp; 1321603Srgrimes } 1331603Srgrimes 1341602Srgrimes /* 1351602Srgrimes * Check if we should mute the console (for security reasons perhaps) 1361602Srgrimes * It can be changes dynamically using sysctl kern.consmute 1371602Srgrimes * once we are up and going. 1381602Srgrimes * 1391602Srgrimes */ 1401603Srgrimes cn_mute = ((boothowto & (RB_MUTE 1411602Srgrimes |RB_SINGLE 1421603Srgrimes |RB_VERBOSE 1431603Srgrimes |RB_ASKNAME 1441603Srgrimes |RB_CONFIG)) == RB_MUTE); 1451602Srgrimes 1461603Srgrimes /* 1471602Srgrimes * If no console, give up. 148 */ 149 if (best_cp == NULL) { 150 if (cn_tab != NULL && cn_tab->cn_term != NULL) 151 (*cn_tab->cn_term)(cn_tab); 152 cn_tab = best_cp; 153 return; 154 } 155 156 /* 157 * Initialize console, then attach to it. This ordering allows 158 * debugging using the previous console, if any. 159 */ 160 (*best_cp->cn_init)(best_cp); 161 if (cn_tab != NULL && cn_tab != best_cp) { 162 /* Turn off the previous console. */ 163 if (cn_tab->cn_term != NULL) 164 (*cn_tab->cn_term)(cn_tab); 165 } 166 cn_tab = best_cp; 167} 168 169void 170cninit_finish() 171{ 172 struct cdevsw *cdp; 173 174 if ((cn_tab == NULL) || cn_mute) 175 return; 176 177 /* 178 * Hook the open and close functions. 179 */ 180 cdp = devsw(cn_tab->cn_dev); 181 cn_phys_close = cdp->d_close; 182 cdp->d_close = cnclose; 183 cn_phys_open = cdp->d_open; 184 cdp->d_open = cnopen; 185 cn_tp = (*cdp->d_devtotty)(cn_tab->cn_dev); 186 cn_dev_t = cn_tp->t_dev; 187} 188 189static void 190cnuninit(void) 191{ 192 struct cdevsw *cdp; 193 194 if (cn_tab == NULL) 195 return; 196 197 /* 198 * Unhook the open and close functions. 199 */ 200 cdp = devsw(cn_tab->cn_dev); 201 cdp->d_close = cn_phys_close; 202 cn_phys_close = NULL; 203 cdp->d_open = cn_phys_open; 204 cn_phys_open = NULL; 205 cn_tp = NULL; 206 cn_dev_t = 0; 207} 208 209/* 210 * User has changed the state of the console muting. 211 * This may require us to open or close the device in question. 212 */ 213static int 214sysctl_kern_consmute SYSCTL_HANDLER_ARGS 215{ 216 int error; 217 int ocn_mute; 218 219 ocn_mute = cn_mute; 220 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 221 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) { 222 if(ocn_mute && !cn_mute) { 223 /* 224 * going from muted to unmuted.. open the physical dev 225 * if the console has been openned 226 */ 227 cninit_finish(); 228 if(cn_is_open) 229 /* XXX curproc is not what we want really */ 230 error = cnopen(cn_dev_t, openflag, 231 openmode, curproc); 232 /* if it failed, back it out */ 233 if ( error != 0) cnuninit(); 234 } else if (!ocn_mute && cn_mute) { 235 /* 236 * going from unmuted to muted.. close the physical dev 237 * if it's only open via /dev/console 238 */ 239 if(cn_is_open) 240 error = cnclose(cn_dev_t, openflag, 241 openmode, curproc); 242 if ( error == 0) cnuninit(); 243 } 244 if (error != 0) { 245 /* 246 * back out the change if there was an error 247 */ 248 cn_mute = ocn_mute; 249 } 250 } 251 return (error); 252} 253 254SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 255 0, sizeof cn_mute, sysctl_kern_consmute, "I", ""); 256 257static int 258cnopen(dev, flag, mode, p) 259 dev_t dev; 260 int flag, mode; 261 struct proc *p; 262{ 263 dev_t cndev, physdev; 264 int retval = 0; 265 266 if (cn_tab == NULL) 267 return (0); 268 cndev = cn_tab->cn_dev; 269 physdev = (major(dev) == major(cndev) ? dev : cndev); 270 /* 271 * If mute is active, then non console opens don't get here 272 * so we don't need to check for that. They 273 * bypass this and go straight to the device. 274 */ 275 if(!cn_mute) 276 retval = (*cn_phys_open)(physdev, flag, mode, p); 277 if (retval == 0) { 278 /* 279 * check if we openned it via /dev/console or 280 * via the physical entry (e.g. /dev/sio0). 281 */ 282 if (dev == cndev) 283 cn_phys_is_open = 1; 284 else if (physdev == cndev) { 285 openmode = mode; 286 openflag = flag; 287 cn_is_open = 1; 288 } 289 } 290 return (retval); 291} 292 293static int 294cnclose(dev, flag, mode, p) 295 dev_t dev; 296 int flag, mode; 297 struct proc *p; 298{ 299 dev_t cndev; 300 301 if (cn_tab == NULL) 302 return (0); 303 cndev = cn_tab->cn_dev; 304 /* 305 * act appropriatly depending on whether it's /dev/console 306 * or the pysical device (e.g. /dev/sio) that's being closed. 307 * in either case, don't actually close the device unless 308 * both are closed. 309 */ 310 if (dev == cndev) { 311 /* the physical device is about to be closed */ 312 cn_phys_is_open = 0; 313 if (cn_is_open) { 314 if (cn_tp) { 315 /* perform a ttyhalfclose() */ 316 /* reset session and proc group */ 317 cn_tp->t_pgrp = NULL; 318 cn_tp->t_session = NULL; 319 } 320 return (0); 321 } 322 } else if (major(dev) != major(cndev)) { 323 /* the logical console is about to be closed */ 324 cn_is_open = 0; 325 if (cn_phys_is_open) 326 return (0); 327 dev = cndev; 328 } 329 if(cn_phys_close) 330 return ((*cn_phys_close)(dev, flag, mode, p)); 331 return (0); 332} 333 334static int 335cnread(dev, uio, flag) 336 dev_t dev; 337 struct uio *uio; 338 int flag; 339{ 340 if ((cn_tab == NULL) || cn_mute) 341 return (0); 342 dev = cn_tab->cn_dev; 343 return ((*devsw(dev)->d_read)(dev, uio, flag)); 344} 345 346static int 347cnwrite(dev, uio, flag) 348 dev_t dev; 349 struct uio *uio; 350 int flag; 351{ 352 if ((cn_tab == NULL) || cn_mute) { 353 uio->uio_resid = 0; /* dump the data */ 354 return (0); 355 } 356 if (constty) 357 dev = constty->t_dev; 358 else 359 dev = cn_tab->cn_dev; 360 return ((*devsw(dev)->d_write)(dev, uio, flag)); 361} 362 363static int 364cnioctl(dev, cmd, data, flag, p) 365 dev_t dev; 366 u_long cmd; 367 caddr_t data; 368 int flag; 369 struct proc *p; 370{ 371 int error; 372 373 if ((cn_tab == NULL) || cn_mute) 374 return (0); 375 /* 376 * Superuser can always use this to wrest control of console 377 * output from the "virtual" console. 378 */ 379 if (cmd == TIOCCONS && constty) { 380 error = suser(p); 381 if (error) 382 return (error); 383 constty = NULL; 384 return (0); 385 } 386 dev = cn_tab->cn_dev; 387 return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, p)); 388} 389 390static int 391cnpoll(dev, events, p) 392 dev_t dev; 393 int events; 394 struct proc *p; 395{ 396 if ((cn_tab == NULL) || cn_mute) 397 return (1); 398 399 dev = cn_tab->cn_dev; 400 401 return ((*devsw(dev)->d_poll)(dev, events, p)); 402} 403 404int 405cngetc() 406{ 407 int c; 408 if ((cn_tab == NULL) || cn_mute) 409 return (-1); 410 c = (*cn_tab->cn_getc)(cn_tab->cn_dev); 411 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 412 return (c); 413} 414 415int 416cncheckc() 417{ 418 if ((cn_tab == NULL) || cn_mute) 419 return (-1); 420 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev)); 421} 422 423void 424cnputc(c) 425 register int c; 426{ 427 if ((cn_tab == NULL) || cn_mute) 428 return; 429 if (c) { 430 if (c == '\n') 431 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 432 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 433 } 434} 435 436static int cn_devsw_installed; 437 438static void 439cn_drvinit(void *unused) 440{ 441 442 if( ! cn_devsw_installed ) { 443 cdevsw_add(&cn_cdevsw); 444 cn_devsw_installed = 1; 445#ifdef DEVFS 446 cn_devfs_token = devfs_add_devswf(&cn_cdevsw, 0, DV_CHR, 447 UID_ROOT, GID_WHEEL, 0600, 448 "console"); 449#endif 450 } 451} 452 453SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL) 454 455 456