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