kern_cons.c revision 49680
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 * $Id: tty_cons.c,v 1.72 1999/08/09 11:02:43 phk Exp $ 40 */ 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/conf.h> 45#include <sys/kernel.h> 46#include <sys/reboot.h> 47#include <sys/sysctl.h> 48#include <sys/proc.h> 49#include <sys/tty.h> 50#include <sys/uio.h> 51#include <sys/cons.h> 52 53#include <machine/cpu.h> 54 55static d_open_t cnopen; 56static d_close_t cnclose; 57static d_read_t cnread; 58static d_write_t cnwrite; 59static d_ioctl_t cnioctl; 60static d_poll_t cnpoll; 61 62#define CDEV_MAJOR 0 63static struct cdevsw cn_cdevsw = { 64 /* open */ cnopen, 65 /* close */ cnclose, 66 /* read */ cnread, 67 /* write */ cnwrite, 68 /* ioctl */ cnioctl, 69 /* stop */ nostop, 70 /* reset */ noreset, 71 /* devtotty */ nodevtotty, 72 /* poll */ cnpoll, 73 /* mmap */ nommap, 74 /* strategy */ nostrategy, 75 /* name */ "console", 76 /* parms */ noparms, 77 /* maj */ CDEV_MAJOR, 78 /* dump */ nodump, 79 /* psize */ nopsize, 80 /* flags */ D_TTY, 81 /* maxio */ 0, 82 /* bmaj */ -1 83}; 84 85static dev_t cn_dev_t; /* seems to be never really used */ 86static udev_t cn_udev_t; 87SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD, 88 &cn_udev_t, sizeof cn_udev_t, "T,dev_t", ""); 89 90static int cn_mute; 91 92int cons_unavail = 0; /* XXX: 93 * physical console not available for 94 * input (i.e., it is in graphics mode) 95 */ 96 97static u_char cn_is_open; /* nonzero if logical console is open */ 98static int openmode, openflag; /* how /dev/console was openned */ 99static u_char cn_phys_is_open; /* nonzero if physical device is open */ 100static d_close_t *cn_phys_close; /* physical device close function */ 101static d_open_t *cn_phys_open; /* physical device open function */ 102struct consdev *cn_tab; /* physical console device info */ 103static struct tty *cn_tp; /* physical console tty struct */ 104 105CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL); 106 107void 108cninit() 109{ 110 struct consdev **list, *best_cp, *cp; 111 112 /* 113 * Find the first console with the highest priority. 114 */ 115 best_cp = NULL; 116 list = (struct consdev **)cons_set.ls_items; 117 while ((cp = *list++) != NULL) { 118 if (cp->cn_probe == NULL) 119 continue; 120 (*cp->cn_probe)(cp); 121 if (cp->cn_pri > CN_DEAD && 122 (best_cp == NULL || cp->cn_pri > best_cp->cn_pri)) 123 best_cp = cp; 124 } 125 126 /* 127 * Check if we should mute the console (for security reasons perhaps) 128 * It can be changes dynamically using sysctl kern.consmute 129 * once we are up and going. 130 * 131 */ 132 cn_mute = ((boothowto & (RB_MUTE 133 |RB_SINGLE 134 |RB_VERBOSE 135 |RB_ASKNAME 136 |RB_CONFIG)) == RB_MUTE); 137 138 /* 139 * If no console, give up. 140 */ 141 if (best_cp == NULL) { 142 if (cn_tab != NULL && cn_tab->cn_term != NULL) 143 (*cn_tab->cn_term)(cn_tab); 144 cn_tab = best_cp; 145 return; 146 } 147 148 /* 149 * Initialize console, then attach to it. This ordering allows 150 * debugging using the previous console, if any. 151 */ 152 (*best_cp->cn_init)(best_cp); 153 if (cn_tab != NULL && cn_tab != best_cp) { 154 /* Turn off the previous console. */ 155 if (cn_tab->cn_term != NULL) 156 (*cn_tab->cn_term)(cn_tab); 157 } 158 cn_tab = best_cp; 159} 160 161void 162cninit_finish() 163{ 164 struct cdevsw *cdp; 165 166 if ((cn_tab == NULL) || cn_mute) 167 return; 168 169 /* 170 * Hook the open and close functions. 171 */ 172 cdp = devsw(cn_tab->cn_dev); 173 cn_phys_close = cdp->d_close; 174 cdp->d_close = cnclose; 175 cn_phys_open = cdp->d_open; 176 cdp->d_open = cnopen; 177 cn_tp = (*cdp->d_devtotty)(cn_tab->cn_dev); 178 cn_dev_t = cn_tab->cn_dev; 179 cn_udev_t = dev2udev(cn_dev_t); 180} 181 182static void 183cnuninit(void) 184{ 185 struct cdevsw *cdp; 186 187 if (cn_tab == NULL) 188 return; 189 190 /* 191 * Unhook the open and close functions. 192 */ 193 cdp = devsw(cn_tab->cn_dev); 194 cdp->d_close = cn_phys_close; 195 cn_phys_close = NULL; 196 cdp->d_open = cn_phys_open; 197 cn_phys_open = NULL; 198 cn_tp = NULL; 199 cn_dev_t = NODEV; 200 cn_udev_t = NOUDEV; 201} 202 203/* 204 * User has changed the state of the console muting. 205 * This may require us to open or close the device in question. 206 */ 207static int 208sysctl_kern_consmute SYSCTL_HANDLER_ARGS 209{ 210 int error; 211 int ocn_mute; 212 213 ocn_mute = cn_mute; 214 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 215 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) { 216 if(ocn_mute && !cn_mute) { 217 /* 218 * going from muted to unmuted.. open the physical dev 219 * if the console has been openned 220 */ 221 cninit_finish(); 222 if(cn_is_open) 223 /* XXX curproc is not what we want really */ 224 error = cnopen(cn_dev_t, openflag, 225 openmode, curproc); 226 /* if it failed, back it out */ 227 if ( error != 0) cnuninit(); 228 } else if (!ocn_mute && cn_mute) { 229 /* 230 * going from unmuted to muted.. close the physical dev 231 * if it's only open via /dev/console 232 */ 233 if(cn_is_open) 234 error = cnclose(cn_dev_t, openflag, 235 openmode, curproc); 236 if ( error == 0) cnuninit(); 237 } 238 if (error != 0) { 239 /* 240 * back out the change if there was an error 241 */ 242 cn_mute = ocn_mute; 243 } 244 } 245 return (error); 246} 247 248SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 249 0, sizeof cn_mute, sysctl_kern_consmute, "I", ""); 250 251static int 252cnopen(dev, flag, mode, p) 253 dev_t dev; 254 int flag, mode; 255 struct proc *p; 256{ 257 dev_t cndev, physdev; 258 int retval = 0; 259 260 if (cn_tab == NULL) 261 return (0); 262 cndev = cn_tab->cn_dev; 263 physdev = (major(dev) == major(cndev) ? dev : cndev); 264 /* 265 * If mute is active, then non console opens don't get here 266 * so we don't need to check for that. They 267 * bypass this and go straight to the device. 268 */ 269 if(!cn_mute) 270 retval = (*cn_phys_open)(physdev, flag, mode, p); 271 if (retval == 0) { 272 /* 273 * check if we openned it via /dev/console or 274 * via the physical entry (e.g. /dev/sio0). 275 */ 276 if (dev == cndev) 277 cn_phys_is_open = 1; 278 else if (physdev == cndev) { 279 openmode = mode; 280 openflag = flag; 281 cn_is_open = 1; 282 } 283 } 284 return (retval); 285} 286 287static int 288cnclose(dev, flag, mode, p) 289 dev_t dev; 290 int flag, mode; 291 struct proc *p; 292{ 293 dev_t cndev; 294 295 if (cn_tab == NULL) 296 return (0); 297 cndev = cn_tab->cn_dev; 298 /* 299 * act appropriatly depending on whether it's /dev/console 300 * or the pysical device (e.g. /dev/sio) that's being closed. 301 * in either case, don't actually close the device unless 302 * both are closed. 303 */ 304 if (dev == cndev) { 305 /* the physical device is about to be closed */ 306 cn_phys_is_open = 0; 307 if (cn_is_open) { 308 if (cn_tp) { 309 /* perform a ttyhalfclose() */ 310 /* reset session and proc group */ 311 cn_tp->t_pgrp = NULL; 312 cn_tp->t_session = NULL; 313 } 314 return (0); 315 } 316 } else if (major(dev) != major(cndev)) { 317 /* the logical console is about to be closed */ 318 cn_is_open = 0; 319 if (cn_phys_is_open) 320 return (0); 321 dev = cndev; 322 } 323 if(cn_phys_close) 324 return ((*cn_phys_close)(dev, flag, mode, p)); 325 return (0); 326} 327 328static int 329cnread(dev, uio, flag) 330 dev_t dev; 331 struct uio *uio; 332 int flag; 333{ 334 if ((cn_tab == NULL) || cn_mute) 335 return (0); 336 dev = cn_tab->cn_dev; 337 return ((*devsw(dev)->d_read)(dev, uio, flag)); 338} 339 340static int 341cnwrite(dev, uio, flag) 342 dev_t dev; 343 struct uio *uio; 344 int flag; 345{ 346 if ((cn_tab == NULL) || cn_mute) { 347 uio->uio_resid = 0; /* dump the data */ 348 return (0); 349 } 350 if (constty) 351 dev = constty->t_dev; 352 else 353 dev = cn_tab->cn_dev; 354 return ((*devsw(dev)->d_write)(dev, uio, flag)); 355} 356 357static int 358cnioctl(dev, cmd, data, flag, p) 359 dev_t dev; 360 u_long cmd; 361 caddr_t data; 362 int flag; 363 struct proc *p; 364{ 365 int error; 366 367 if ((cn_tab == NULL) || cn_mute) 368 return (0); 369 /* 370 * Superuser can always use this to wrest control of console 371 * output from the "virtual" console. 372 */ 373 if (cmd == TIOCCONS && constty) { 374 error = suser(p); 375 if (error) 376 return (error); 377 constty = NULL; 378 return (0); 379 } 380 dev = cn_tab->cn_dev; 381 return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, p)); 382} 383 384static int 385cnpoll(dev, events, p) 386 dev_t dev; 387 int events; 388 struct proc *p; 389{ 390 if ((cn_tab == NULL) || cn_mute) 391 return (1); 392 393 dev = cn_tab->cn_dev; 394 395 return ((*devsw(dev)->d_poll)(dev, events, p)); 396} 397 398int 399cngetc() 400{ 401 int c; 402 if ((cn_tab == NULL) || cn_mute) 403 return (-1); 404 c = (*cn_tab->cn_getc)(cn_tab->cn_dev); 405 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 406 return (c); 407} 408 409int 410cncheckc() 411{ 412 if ((cn_tab == NULL) || cn_mute) 413 return (-1); 414 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev)); 415} 416 417void 418cnputc(c) 419 register int c; 420{ 421 if ((cn_tab == NULL) || cn_mute) 422 return; 423 if (c) { 424 if (c == '\n') 425 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 426 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 427 } 428} 429 430static void 431cn_drvinit(void *unused) 432{ 433 434 cdevsw_add(&cn_cdevsw); 435 make_dev (&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console"); 436} 437 438SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL) 439 440 441