Deleted Added
full compact
kern_cons.c (130585) kern_cons.c (130640)
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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
35 */
36
37#include <sys/cdefs.h>
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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/tty_cons.c 130585 2004-06-16 09:47:26Z phk $");
38__FBSDID("$FreeBSD: head/sys/kern/tty_cons.c 130640 2004-06-17 17:16:53Z phk $");
39
40#include "opt_ddb.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/cons.h>
46#include <sys/fcntl.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/msgbuf.h>
50#include <sys/namei.h>
51#include <sys/proc.h>
52#include <sys/queue.h>
53#include <sys/reboot.h>
54#include <sys/sysctl.h>
55#include <sys/tty.h>
56#include <sys/uio.h>
57#include <sys/vnode.h>
58
59#include <ddb/ddb.h>
60
61#include <machine/cpu.h>
62
63static d_open_t cnopen;
64static d_close_t cnclose;
65static d_read_t cnread;
66static d_write_t cnwrite;
67static d_ioctl_t cnioctl;
68static d_poll_t cnpoll;
69static d_kqfilter_t cnkqfilter;
70
71/*
72 * XXX: We really want major #0, but zero here means
73 * XXX: allocate a major number automatically.
74 * XXX: kern_conf.c knows what to do when it sees 256.
75 */
76static struct cdevsw cn_cdevsw = {
77 .d_version = D_VERSION,
78 .d_open = cnopen,
79 .d_close = cnclose,
80 .d_read = cnread,
81 .d_write = cnwrite,
82 .d_ioctl = cnioctl,
83 .d_poll = cnpoll,
84 .d_name = "console",
85 .d_maj = 256,
86 .d_flags = D_TTY | D_NEEDGIANT,
87 .d_kqfilter = cnkqfilter,
88};
89
90struct cn_device {
91 STAILQ_ENTRY(cn_device) cnd_next;
92 struct vnode *cnd_vp;
93 struct consdev *cnd_cn;
94};
95
96#define CNDEVPATHMAX 32
97#define CNDEVTAB_SIZE 4
98static struct cn_device cn_devtab[CNDEVTAB_SIZE];
99static STAILQ_HEAD(, cn_device) cn_devlist =
100 STAILQ_HEAD_INITIALIZER(cn_devlist);
101
102#define CND_INVALID(cnd, td) \
103 (cnd == NULL || cnd->cnd_vp == NULL || \
104 (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1)))
105
39
40#include "opt_ddb.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/cons.h>
46#include <sys/fcntl.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/msgbuf.h>
50#include <sys/namei.h>
51#include <sys/proc.h>
52#include <sys/queue.h>
53#include <sys/reboot.h>
54#include <sys/sysctl.h>
55#include <sys/tty.h>
56#include <sys/uio.h>
57#include <sys/vnode.h>
58
59#include <ddb/ddb.h>
60
61#include <machine/cpu.h>
62
63static d_open_t cnopen;
64static d_close_t cnclose;
65static d_read_t cnread;
66static d_write_t cnwrite;
67static d_ioctl_t cnioctl;
68static d_poll_t cnpoll;
69static d_kqfilter_t cnkqfilter;
70
71/*
72 * XXX: We really want major #0, but zero here means
73 * XXX: allocate a major number automatically.
74 * XXX: kern_conf.c knows what to do when it sees 256.
75 */
76static struct cdevsw cn_cdevsw = {
77 .d_version = D_VERSION,
78 .d_open = cnopen,
79 .d_close = cnclose,
80 .d_read = cnread,
81 .d_write = cnwrite,
82 .d_ioctl = cnioctl,
83 .d_poll = cnpoll,
84 .d_name = "console",
85 .d_maj = 256,
86 .d_flags = D_TTY | D_NEEDGIANT,
87 .d_kqfilter = cnkqfilter,
88};
89
90struct cn_device {
91 STAILQ_ENTRY(cn_device) cnd_next;
92 struct vnode *cnd_vp;
93 struct consdev *cnd_cn;
94};
95
96#define CNDEVPATHMAX 32
97#define CNDEVTAB_SIZE 4
98static struct cn_device cn_devtab[CNDEVTAB_SIZE];
99static STAILQ_HEAD(, cn_device) cn_devlist =
100 STAILQ_HEAD_INITIALIZER(cn_devlist);
101
102#define CND_INVALID(cnd, td) \
103 (cnd == NULL || cnd->cnd_vp == NULL || \
104 (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1)))
105
106static udev_t cn_udev_t;
106static dev_t cn_udev_t;
107SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
108 &cn_udev_t, sizeof cn_udev_t, "T,struct cdev *", "");
109
110int cons_avail_mask = 0; /* Bit mask. Each registered low level console
111 * which is currently unavailable for inpit
112 * (i.e., if it is in graphics mode) will have
113 * this bit cleared.
114 */
115static int cn_mute;
116static int openflag; /* how /dev/console was opened */
117static int cn_is_open;
118static char *consbuf; /* buffer used by `consmsgbuf' */
119static struct callout conscallout; /* callout for outputting to constty */
120struct msgbuf consmsgbuf; /* message buffer for console tty */
121static u_char console_pausing; /* pause after each line during probe */
122static char *console_pausestr=
123"<pause; press any key to proceed to next line or '.' to end pause mode>";
124struct tty *constty; /* pointer to console "window" tty */
125
126static void constty_timeout(void *arg);
127
128CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
129SET_DECLARE(cons_set, struct consdev);
130
131void
132cninit(void)
133{
134 struct consdev *best_cn, *cn, **list;
135
136 /*
137 * Check if we should mute the console (for security reasons perhaps)
138 * It can be changes dynamically using sysctl kern.consmute
139 * once we are up and going.
140 *
141 */
142 cn_mute = ((boothowto & (RB_MUTE
143 |RB_SINGLE
144 |RB_VERBOSE
145 |RB_ASKNAME
146 |RB_CONFIG)) == RB_MUTE);
147
148 /*
149 * Find the first console with the highest priority.
150 */
151 best_cn = NULL;
152 SET_FOREACH(list, cons_set) {
153 cn = *list;
154 cnremove(cn);
155 if (cn->cn_probe == NULL)
156 continue;
157 cn->cn_probe(cn);
158 if (cn->cn_pri == CN_DEAD)
159 continue;
160 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
161 best_cn = cn;
162 if (boothowto & RB_MULTIPLE) {
163 /*
164 * Initialize console, and attach to it.
165 */
166 cnadd(cn);
167 cn->cn_init(cn);
168 }
169 }
170 if (best_cn == NULL)
171 return;
172 if ((boothowto & RB_MULTIPLE) == 0) {
173 cnadd(best_cn);
174 best_cn->cn_init(best_cn);
175 }
176 if (boothowto & RB_PAUSE)
177 console_pausing = 1;
178 /*
179 * Make the best console the preferred console.
180 */
181 cnselect(best_cn);
182}
183
184void
185cninit_finish()
186{
187 console_pausing = 0;
188}
189
190/* add a new physical console to back the virtual console */
191int
192cnadd(struct consdev *cn)
193{
194 struct cn_device *cnd;
195 int i;
196
197 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
198 if (cnd->cnd_cn == cn)
199 return (0);
200 for (i = 0; i < CNDEVTAB_SIZE; i++) {
201 cnd = &cn_devtab[i];
202 if (cnd->cnd_cn == NULL)
203 break;
204 }
205 if (cnd->cnd_cn != NULL)
206 return (ENOMEM);
207 cnd->cnd_cn = cn;
208 if (cn->cn_name[0] == '\0') {
209 /* XXX: it is unclear if/where this print might output */
210 printf("WARNING: console at %p has no name\n", cn);
211 }
212 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
213
214 /* Add device to the active mask. */
215 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
216
217 return (0);
218}
219
220void
221cnremove(struct consdev *cn)
222{
223 struct cn_device *cnd;
224 int i;
225
226 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
227 if (cnd->cnd_cn != cn)
228 continue;
229 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
230 if (cnd->cnd_vp != NULL)
231 vn_close(cnd->cnd_vp, openflag, NOCRED, NULL);
232 cnd->cnd_vp = NULL;
233 cnd->cnd_cn = NULL;
234
235 /* Remove this device from available mask. */
236 for (i = 0; i < CNDEVTAB_SIZE; i++)
237 if (cnd == &cn_devtab[i]) {
238 cons_avail_mask &= ~(1 << i);
239 break;
240 }
241#if 0
242 /*
243 * XXX
244 * syscons gets really confused if console resources are
245 * freed after the system has initialized.
246 */
247 if (cn->cn_term != NULL)
248 cn->cn_term(cn);
249#endif
250 return;
251 }
252}
253
254void
255cnselect(struct consdev *cn)
256{
257 struct cn_device *cnd;
258
259 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
260 if (cnd->cnd_cn != cn)
261 continue;
262 if (cnd == STAILQ_FIRST(&cn_devlist))
263 return;
264 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
265 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
266 return;
267 }
268}
269
270void
271cnavailable(struct consdev *cn, int available)
272{
273 int i;
274
275 for (i = 0; i < CNDEVTAB_SIZE; i++) {
276 if (cn_devtab[i].cnd_cn == cn)
277 break;
278 }
279 if (available) {
280 if (i < CNDEVTAB_SIZE)
281 cons_avail_mask |= (1 << i);
282 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
283 } else {
284 if (i < CNDEVTAB_SIZE)
285 cons_avail_mask &= ~(1 << i);
286 cn->cn_flags |= CN_FLAG_NOAVAIL;
287 }
288}
289
290int
291cnunavailable(void)
292{
293
294 return (cons_avail_mask == 0);
295}
296
297/*
298 * XXX: rewrite to use sbufs instead
299 */
300
301static int
302sysctl_kern_console(SYSCTL_HANDLER_ARGS)
303{
304 struct cn_device *cnd;
305 struct consdev *cp, **list;
306 char *name, *p;
307 int delete, len, error;
308
309 len = 2;
310 SET_FOREACH(list, cons_set) {
311 cp = *list;
312 if (cp->cn_name[0] != '\0')
313 len += strlen(cp->cn_name) + 1;
314 }
315 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
316 len += strlen(cnd->cnd_cn->cn_name) + 1;
317 len = len > CNDEVPATHMAX ? len : CNDEVPATHMAX;
318 MALLOC(name, char *, len, M_TEMP, M_WAITOK | M_ZERO);
319 p = name;
320 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
321 p += sprintf(p, "%s,", cnd->cnd_cn->cn_name);
322 *p++ = '/';
323 SET_FOREACH(list, cons_set) {
324 cp = *list;
325 if (cp->cn_name[0] != '\0')
326 p += sprintf(p, "%s,", cp->cn_name);
327 }
328 error = sysctl_handle_string(oidp, name, len, req);
329 if (error == 0 && req->newptr != NULL) {
330 p = name;
331 error = ENXIO;
332 delete = 0;
333 if (*p == '-') {
334 delete = 1;
335 p++;
336 }
337 SET_FOREACH(list, cons_set) {
338 cp = *list;
339 if (strcmp(p, cp->cn_name) != 0)
340 continue;
341 if (delete) {
342 cnremove(cp);
343 error = 0;
344 } else {
345 error = cnadd(cp);
346 if (error == 0)
347 cnselect(cp);
348 }
349 break;
350 }
351 }
352 FREE(name, M_TEMP);
353 return (error);
354}
355
356SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
357 0, 0, sysctl_kern_console, "A", "Console device control");
358
359/*
360 * User has changed the state of the console muting.
361 * This may require us to open or close the device in question.
362 */
363static int
364sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
365{
366 int error;
367 int ocn_mute;
368
369 ocn_mute = cn_mute;
370 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
371 if (error != 0 || req->newptr == NULL)
372 return (error);
373 if (ocn_mute && !cn_mute && cn_is_open)
107SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
108 &cn_udev_t, sizeof cn_udev_t, "T,struct cdev *", "");
109
110int cons_avail_mask = 0; /* Bit mask. Each registered low level console
111 * which is currently unavailable for inpit
112 * (i.e., if it is in graphics mode) will have
113 * this bit cleared.
114 */
115static int cn_mute;
116static int openflag; /* how /dev/console was opened */
117static int cn_is_open;
118static char *consbuf; /* buffer used by `consmsgbuf' */
119static struct callout conscallout; /* callout for outputting to constty */
120struct msgbuf consmsgbuf; /* message buffer for console tty */
121static u_char console_pausing; /* pause after each line during probe */
122static char *console_pausestr=
123"<pause; press any key to proceed to next line or '.' to end pause mode>";
124struct tty *constty; /* pointer to console "window" tty */
125
126static void constty_timeout(void *arg);
127
128CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
129SET_DECLARE(cons_set, struct consdev);
130
131void
132cninit(void)
133{
134 struct consdev *best_cn, *cn, **list;
135
136 /*
137 * Check if we should mute the console (for security reasons perhaps)
138 * It can be changes dynamically using sysctl kern.consmute
139 * once we are up and going.
140 *
141 */
142 cn_mute = ((boothowto & (RB_MUTE
143 |RB_SINGLE
144 |RB_VERBOSE
145 |RB_ASKNAME
146 |RB_CONFIG)) == RB_MUTE);
147
148 /*
149 * Find the first console with the highest priority.
150 */
151 best_cn = NULL;
152 SET_FOREACH(list, cons_set) {
153 cn = *list;
154 cnremove(cn);
155 if (cn->cn_probe == NULL)
156 continue;
157 cn->cn_probe(cn);
158 if (cn->cn_pri == CN_DEAD)
159 continue;
160 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
161 best_cn = cn;
162 if (boothowto & RB_MULTIPLE) {
163 /*
164 * Initialize console, and attach to it.
165 */
166 cnadd(cn);
167 cn->cn_init(cn);
168 }
169 }
170 if (best_cn == NULL)
171 return;
172 if ((boothowto & RB_MULTIPLE) == 0) {
173 cnadd(best_cn);
174 best_cn->cn_init(best_cn);
175 }
176 if (boothowto & RB_PAUSE)
177 console_pausing = 1;
178 /*
179 * Make the best console the preferred console.
180 */
181 cnselect(best_cn);
182}
183
184void
185cninit_finish()
186{
187 console_pausing = 0;
188}
189
190/* add a new physical console to back the virtual console */
191int
192cnadd(struct consdev *cn)
193{
194 struct cn_device *cnd;
195 int i;
196
197 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
198 if (cnd->cnd_cn == cn)
199 return (0);
200 for (i = 0; i < CNDEVTAB_SIZE; i++) {
201 cnd = &cn_devtab[i];
202 if (cnd->cnd_cn == NULL)
203 break;
204 }
205 if (cnd->cnd_cn != NULL)
206 return (ENOMEM);
207 cnd->cnd_cn = cn;
208 if (cn->cn_name[0] == '\0') {
209 /* XXX: it is unclear if/where this print might output */
210 printf("WARNING: console at %p has no name\n", cn);
211 }
212 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
213
214 /* Add device to the active mask. */
215 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
216
217 return (0);
218}
219
220void
221cnremove(struct consdev *cn)
222{
223 struct cn_device *cnd;
224 int i;
225
226 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
227 if (cnd->cnd_cn != cn)
228 continue;
229 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
230 if (cnd->cnd_vp != NULL)
231 vn_close(cnd->cnd_vp, openflag, NOCRED, NULL);
232 cnd->cnd_vp = NULL;
233 cnd->cnd_cn = NULL;
234
235 /* Remove this device from available mask. */
236 for (i = 0; i < CNDEVTAB_SIZE; i++)
237 if (cnd == &cn_devtab[i]) {
238 cons_avail_mask &= ~(1 << i);
239 break;
240 }
241#if 0
242 /*
243 * XXX
244 * syscons gets really confused if console resources are
245 * freed after the system has initialized.
246 */
247 if (cn->cn_term != NULL)
248 cn->cn_term(cn);
249#endif
250 return;
251 }
252}
253
254void
255cnselect(struct consdev *cn)
256{
257 struct cn_device *cnd;
258
259 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
260 if (cnd->cnd_cn != cn)
261 continue;
262 if (cnd == STAILQ_FIRST(&cn_devlist))
263 return;
264 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
265 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
266 return;
267 }
268}
269
270void
271cnavailable(struct consdev *cn, int available)
272{
273 int i;
274
275 for (i = 0; i < CNDEVTAB_SIZE; i++) {
276 if (cn_devtab[i].cnd_cn == cn)
277 break;
278 }
279 if (available) {
280 if (i < CNDEVTAB_SIZE)
281 cons_avail_mask |= (1 << i);
282 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
283 } else {
284 if (i < CNDEVTAB_SIZE)
285 cons_avail_mask &= ~(1 << i);
286 cn->cn_flags |= CN_FLAG_NOAVAIL;
287 }
288}
289
290int
291cnunavailable(void)
292{
293
294 return (cons_avail_mask == 0);
295}
296
297/*
298 * XXX: rewrite to use sbufs instead
299 */
300
301static int
302sysctl_kern_console(SYSCTL_HANDLER_ARGS)
303{
304 struct cn_device *cnd;
305 struct consdev *cp, **list;
306 char *name, *p;
307 int delete, len, error;
308
309 len = 2;
310 SET_FOREACH(list, cons_set) {
311 cp = *list;
312 if (cp->cn_name[0] != '\0')
313 len += strlen(cp->cn_name) + 1;
314 }
315 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
316 len += strlen(cnd->cnd_cn->cn_name) + 1;
317 len = len > CNDEVPATHMAX ? len : CNDEVPATHMAX;
318 MALLOC(name, char *, len, M_TEMP, M_WAITOK | M_ZERO);
319 p = name;
320 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
321 p += sprintf(p, "%s,", cnd->cnd_cn->cn_name);
322 *p++ = '/';
323 SET_FOREACH(list, cons_set) {
324 cp = *list;
325 if (cp->cn_name[0] != '\0')
326 p += sprintf(p, "%s,", cp->cn_name);
327 }
328 error = sysctl_handle_string(oidp, name, len, req);
329 if (error == 0 && req->newptr != NULL) {
330 p = name;
331 error = ENXIO;
332 delete = 0;
333 if (*p == '-') {
334 delete = 1;
335 p++;
336 }
337 SET_FOREACH(list, cons_set) {
338 cp = *list;
339 if (strcmp(p, cp->cn_name) != 0)
340 continue;
341 if (delete) {
342 cnremove(cp);
343 error = 0;
344 } else {
345 error = cnadd(cp);
346 if (error == 0)
347 cnselect(cp);
348 }
349 break;
350 }
351 }
352 FREE(name, M_TEMP);
353 return (error);
354}
355
356SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
357 0, 0, sysctl_kern_console, "A", "Console device control");
358
359/*
360 * User has changed the state of the console muting.
361 * This may require us to open or close the device in question.
362 */
363static int
364sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
365{
366 int error;
367 int ocn_mute;
368
369 ocn_mute = cn_mute;
370 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
371 if (error != 0 || req->newptr == NULL)
372 return (error);
373 if (ocn_mute && !cn_mute && cn_is_open)
374 error = cnopen(NODEV, openflag, 0, curthread);
374 error = cnopen(NULL, openflag, 0, curthread);
375 else if (!ocn_mute && cn_mute && cn_is_open) {
375 else if (!ocn_mute && cn_mute && cn_is_open) {
376 error = cnclose(NODEV, openflag, 0, curthread);
376 error = cnclose(NULL, openflag, 0, curthread);
377 cn_is_open = 1; /* XXX hack */
378 }
379 return (error);
380}
381
382SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
383 0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
384
385static int
386cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
387{
388 char path[CNDEVPATHMAX];
389 struct nameidata nd;
390 struct vnode *vp;
391 struct cdev *dev;
392 int error;
393
394 if ((vp = cnd->cnd_vp) != NULL) {
395 if (!forceopen && vp->v_type != VBAD) {
396 dev = vp->v_rdev;
397 return ((*devsw(dev)->d_open)(dev, openflag, 0, td));
398 }
399 cnd->cnd_vp = NULL;
400 vn_close(vp, openflag, td->td_ucred, td);
401 }
402 snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_cn->cn_name);
403 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
404 error = vn_open(&nd, &openflag, 0, -1);
405 if (error == 0) {
406 NDFREE(&nd, NDF_ONLY_PNBUF);
407 VOP_UNLOCK(nd.ni_vp, 0, td);
408 if (nd.ni_vp->v_type == VCHR)
409 cnd->cnd_vp = nd.ni_vp;
410 else
411 vn_close(nd.ni_vp, openflag, td->td_ucred, td);
412 }
413 return (cnd->cnd_vp != NULL);
414}
415
416static int
417cnopen(struct cdev *dev, int flag, int mode, struct thread *td)
418{
419 struct cn_device *cnd;
420
421 openflag = flag | FWRITE; /* XXX */
422 cn_is_open = 1; /* console is logically open */
423 if (cn_mute)
424 return (0);
425 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
426 cn_devopen(cnd, td, 0);
427 return (0);
428}
429
430static int
431cnclose(struct cdev *dev, int flag, int mode, struct thread *td)
432{
433 struct cn_device *cnd;
434 struct vnode *vp;
435
436 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
437 if ((vp = cnd->cnd_vp) == NULL)
438 continue;
439 cnd->cnd_vp = NULL;
440 vn_close(vp, openflag, td->td_ucred, td);
441 }
442 cn_is_open = 0;
443 return (0);
444}
445
446static int
447cnread(struct cdev *dev, struct uio *uio, int flag)
448{
449 struct cn_device *cnd;
450
451 cnd = STAILQ_FIRST(&cn_devlist);
452 if (cn_mute || CND_INVALID(cnd, curthread))
453 return (0);
454 dev = cnd->cnd_vp->v_rdev;
455 return ((*devsw(dev)->d_read)(dev, uio, flag));
456}
457
458static int
459cnwrite(struct cdev *dev, struct uio *uio, int flag)
460{
461 struct cn_device *cnd;
462
463 cnd = STAILQ_FIRST(&cn_devlist);
464 if (cn_mute || CND_INVALID(cnd, curthread))
465 goto done;
466 if (constty)
467 dev = constty->t_dev;
468 else
469 dev = cnd->cnd_vp->v_rdev;
470 if (dev != NULL) {
471 log_console(uio);
472 return ((*devsw(dev)->d_write)(dev, uio, flag));
473 }
474done:
475 uio->uio_resid = 0; /* dump the data */
476 return (0);
477}
478
479static int
480cnioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
481{
482 struct cn_device *cnd;
483 int error;
484
485 cnd = STAILQ_FIRST(&cn_devlist);
486 if (cn_mute || CND_INVALID(cnd, td))
487 return (0);
488 /*
489 * Superuser can always use this to wrest control of console
490 * output from the "virtual" console.
491 */
492 if (cmd == TIOCCONS && constty) {
493 error = suser(td);
494 if (error)
495 return (error);
496 constty = NULL;
497 return (0);
498 }
499 dev = cnd->cnd_vp->v_rdev;
500 if (dev != NULL)
501 return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, td));
502 return (0);
503}
504
505/*
506 * XXX
507 * poll/kqfilter do not appear to be correct
508 */
509static int
510cnpoll(struct cdev *dev, int events, struct thread *td)
511{
512 struct cn_device *cnd;
513
514 cnd = STAILQ_FIRST(&cn_devlist);
515 if (cn_mute || CND_INVALID(cnd, td))
516 return (0);
517 dev = cnd->cnd_vp->v_rdev;
518 if (dev != NULL)
519 return ((*devsw(dev)->d_poll)(dev, events, td));
520 return (0);
521}
522
523static int
524cnkqfilter(struct cdev *dev, struct knote *kn)
525{
526 struct cn_device *cnd;
527
528 cnd = STAILQ_FIRST(&cn_devlist);
529 if (cn_mute || CND_INVALID(cnd, curthread))
530 return (1);
531 dev = cnd->cnd_vp->v_rdev;
532 if (dev != NULL)
533 return ((*devsw(dev)->d_kqfilter)(dev, kn));
534 return (1);
535}
536
537/*
538 * Low level console routines.
539 */
540int
541cngetc(void)
542{
543 int c;
544
545 if (cn_mute)
546 return (-1);
547 while ((c = cncheckc()) == -1)
548 ;
549 if (c == '\r')
550 c = '\n'; /* console input is always ICRNL */
551 return (c);
552}
553
554int
555cncheckc(void)
556{
557 struct cn_device *cnd;
558 struct consdev *cn;
559 int c;
560
561 if (cn_mute)
562 return (-1);
563 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
564 cn = cnd->cnd_cn;
565#ifdef DDB
566 if (!db_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
567#endif
568 c = cn->cn_checkc(cn);
569 if (c != -1) {
570 return (c);
571 }
572#ifdef DDB
573 }
574#endif
575 }
576 return (-1);
577}
578
579void
580cnputc(int c)
581{
582 struct cn_device *cnd;
583 struct consdev *cn;
584 char *cp;
585
586 if (cn_mute || c == '\0')
587 return;
588 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
589 cn = cnd->cnd_cn;
590#ifdef DDB
591 if (!db_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
592#endif
593 if (c == '\n')
594 cn->cn_putc(cn, '\r');
595 cn->cn_putc(cn, c);
596#ifdef DDB
597 }
598#endif
599 }
600#ifdef DDB
601 if (console_pausing && !db_active && (c == '\n')) {
602#else
603 if (console_pausing && (c == '\n')) {
604#endif
605 for (cp = console_pausestr; *cp != '\0'; cp++)
606 cnputc(*cp);
607 if (cngetc() == '.')
608 console_pausing = 0;
609 cnputc('\r');
610 for (cp = console_pausestr; *cp != '\0'; cp++)
611 cnputc(' ');
612 cnputc('\r');
613 }
614}
615
616void
617cndbctl(int on)
618{
619 struct cn_device *cnd;
620 struct consdev *cn;
621 static int refcount;
622
623 if (!on)
624 refcount--;
625 if (refcount == 0)
626 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
627 cn = cnd->cnd_cn;
628 if (cn->cn_dbctl != NULL)
629 cn->cn_dbctl(cn, on);
630 }
631 if (on)
632 refcount++;
633}
634
635static int consmsgbuf_size = 8192;
636SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
637 "");
638
639/*
640 * Redirect console output to a tty.
641 */
642void
643constty_set(struct tty *tp)
644{
645 int size;
646
647 KASSERT(tp != NULL, ("constty_set: NULL tp"));
648 if (consbuf == NULL) {
649 size = consmsgbuf_size;
650 consbuf = malloc(size, M_TTYS, M_WAITOK);
651 msgbuf_init(&consmsgbuf, consbuf, size);
652 callout_init(&conscallout, 0);
653 }
654 constty = tp;
655 constty_timeout(NULL);
656}
657
658/*
659 * Disable console redirection to a tty.
660 */
661void
662constty_clear(void)
663{
664 int c;
665
666 constty = NULL;
667 if (consbuf == NULL)
668 return;
669 callout_stop(&conscallout);
670 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
671 cnputc(c);
672 free(consbuf, M_TTYS);
673 consbuf = NULL;
674}
675
676/* Times per second to check for pending console tty messages. */
677static int constty_wakeups_per_second = 5;
678SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
679 &constty_wakeups_per_second, 0, "");
680
681static void
682constty_timeout(void *arg)
683{
684 int c;
685
686 while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) {
687 if (tputchar(c, constty) < 0)
688 constty = NULL;
689 }
690 if (constty != NULL) {
691 callout_reset(&conscallout, hz / constty_wakeups_per_second,
692 constty_timeout, NULL);
693 } else {
694 /* Deallocate the constty buffer memory. */
695 constty_clear();
696 }
697}
698
699static void
700cn_drvinit(void *unused)
701{
702
703 make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console");
704}
705
706SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL)
377 cn_is_open = 1; /* XXX hack */
378 }
379 return (error);
380}
381
382SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
383 0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
384
385static int
386cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
387{
388 char path[CNDEVPATHMAX];
389 struct nameidata nd;
390 struct vnode *vp;
391 struct cdev *dev;
392 int error;
393
394 if ((vp = cnd->cnd_vp) != NULL) {
395 if (!forceopen && vp->v_type != VBAD) {
396 dev = vp->v_rdev;
397 return ((*devsw(dev)->d_open)(dev, openflag, 0, td));
398 }
399 cnd->cnd_vp = NULL;
400 vn_close(vp, openflag, td->td_ucred, td);
401 }
402 snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_cn->cn_name);
403 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
404 error = vn_open(&nd, &openflag, 0, -1);
405 if (error == 0) {
406 NDFREE(&nd, NDF_ONLY_PNBUF);
407 VOP_UNLOCK(nd.ni_vp, 0, td);
408 if (nd.ni_vp->v_type == VCHR)
409 cnd->cnd_vp = nd.ni_vp;
410 else
411 vn_close(nd.ni_vp, openflag, td->td_ucred, td);
412 }
413 return (cnd->cnd_vp != NULL);
414}
415
416static int
417cnopen(struct cdev *dev, int flag, int mode, struct thread *td)
418{
419 struct cn_device *cnd;
420
421 openflag = flag | FWRITE; /* XXX */
422 cn_is_open = 1; /* console is logically open */
423 if (cn_mute)
424 return (0);
425 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
426 cn_devopen(cnd, td, 0);
427 return (0);
428}
429
430static int
431cnclose(struct cdev *dev, int flag, int mode, struct thread *td)
432{
433 struct cn_device *cnd;
434 struct vnode *vp;
435
436 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
437 if ((vp = cnd->cnd_vp) == NULL)
438 continue;
439 cnd->cnd_vp = NULL;
440 vn_close(vp, openflag, td->td_ucred, td);
441 }
442 cn_is_open = 0;
443 return (0);
444}
445
446static int
447cnread(struct cdev *dev, struct uio *uio, int flag)
448{
449 struct cn_device *cnd;
450
451 cnd = STAILQ_FIRST(&cn_devlist);
452 if (cn_mute || CND_INVALID(cnd, curthread))
453 return (0);
454 dev = cnd->cnd_vp->v_rdev;
455 return ((*devsw(dev)->d_read)(dev, uio, flag));
456}
457
458static int
459cnwrite(struct cdev *dev, struct uio *uio, int flag)
460{
461 struct cn_device *cnd;
462
463 cnd = STAILQ_FIRST(&cn_devlist);
464 if (cn_mute || CND_INVALID(cnd, curthread))
465 goto done;
466 if (constty)
467 dev = constty->t_dev;
468 else
469 dev = cnd->cnd_vp->v_rdev;
470 if (dev != NULL) {
471 log_console(uio);
472 return ((*devsw(dev)->d_write)(dev, uio, flag));
473 }
474done:
475 uio->uio_resid = 0; /* dump the data */
476 return (0);
477}
478
479static int
480cnioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
481{
482 struct cn_device *cnd;
483 int error;
484
485 cnd = STAILQ_FIRST(&cn_devlist);
486 if (cn_mute || CND_INVALID(cnd, td))
487 return (0);
488 /*
489 * Superuser can always use this to wrest control of console
490 * output from the "virtual" console.
491 */
492 if (cmd == TIOCCONS && constty) {
493 error = suser(td);
494 if (error)
495 return (error);
496 constty = NULL;
497 return (0);
498 }
499 dev = cnd->cnd_vp->v_rdev;
500 if (dev != NULL)
501 return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, td));
502 return (0);
503}
504
505/*
506 * XXX
507 * poll/kqfilter do not appear to be correct
508 */
509static int
510cnpoll(struct cdev *dev, int events, struct thread *td)
511{
512 struct cn_device *cnd;
513
514 cnd = STAILQ_FIRST(&cn_devlist);
515 if (cn_mute || CND_INVALID(cnd, td))
516 return (0);
517 dev = cnd->cnd_vp->v_rdev;
518 if (dev != NULL)
519 return ((*devsw(dev)->d_poll)(dev, events, td));
520 return (0);
521}
522
523static int
524cnkqfilter(struct cdev *dev, struct knote *kn)
525{
526 struct cn_device *cnd;
527
528 cnd = STAILQ_FIRST(&cn_devlist);
529 if (cn_mute || CND_INVALID(cnd, curthread))
530 return (1);
531 dev = cnd->cnd_vp->v_rdev;
532 if (dev != NULL)
533 return ((*devsw(dev)->d_kqfilter)(dev, kn));
534 return (1);
535}
536
537/*
538 * Low level console routines.
539 */
540int
541cngetc(void)
542{
543 int c;
544
545 if (cn_mute)
546 return (-1);
547 while ((c = cncheckc()) == -1)
548 ;
549 if (c == '\r')
550 c = '\n'; /* console input is always ICRNL */
551 return (c);
552}
553
554int
555cncheckc(void)
556{
557 struct cn_device *cnd;
558 struct consdev *cn;
559 int c;
560
561 if (cn_mute)
562 return (-1);
563 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
564 cn = cnd->cnd_cn;
565#ifdef DDB
566 if (!db_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
567#endif
568 c = cn->cn_checkc(cn);
569 if (c != -1) {
570 return (c);
571 }
572#ifdef DDB
573 }
574#endif
575 }
576 return (-1);
577}
578
579void
580cnputc(int c)
581{
582 struct cn_device *cnd;
583 struct consdev *cn;
584 char *cp;
585
586 if (cn_mute || c == '\0')
587 return;
588 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
589 cn = cnd->cnd_cn;
590#ifdef DDB
591 if (!db_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
592#endif
593 if (c == '\n')
594 cn->cn_putc(cn, '\r');
595 cn->cn_putc(cn, c);
596#ifdef DDB
597 }
598#endif
599 }
600#ifdef DDB
601 if (console_pausing && !db_active && (c == '\n')) {
602#else
603 if (console_pausing && (c == '\n')) {
604#endif
605 for (cp = console_pausestr; *cp != '\0'; cp++)
606 cnputc(*cp);
607 if (cngetc() == '.')
608 console_pausing = 0;
609 cnputc('\r');
610 for (cp = console_pausestr; *cp != '\0'; cp++)
611 cnputc(' ');
612 cnputc('\r');
613 }
614}
615
616void
617cndbctl(int on)
618{
619 struct cn_device *cnd;
620 struct consdev *cn;
621 static int refcount;
622
623 if (!on)
624 refcount--;
625 if (refcount == 0)
626 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
627 cn = cnd->cnd_cn;
628 if (cn->cn_dbctl != NULL)
629 cn->cn_dbctl(cn, on);
630 }
631 if (on)
632 refcount++;
633}
634
635static int consmsgbuf_size = 8192;
636SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
637 "");
638
639/*
640 * Redirect console output to a tty.
641 */
642void
643constty_set(struct tty *tp)
644{
645 int size;
646
647 KASSERT(tp != NULL, ("constty_set: NULL tp"));
648 if (consbuf == NULL) {
649 size = consmsgbuf_size;
650 consbuf = malloc(size, M_TTYS, M_WAITOK);
651 msgbuf_init(&consmsgbuf, consbuf, size);
652 callout_init(&conscallout, 0);
653 }
654 constty = tp;
655 constty_timeout(NULL);
656}
657
658/*
659 * Disable console redirection to a tty.
660 */
661void
662constty_clear(void)
663{
664 int c;
665
666 constty = NULL;
667 if (consbuf == NULL)
668 return;
669 callout_stop(&conscallout);
670 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
671 cnputc(c);
672 free(consbuf, M_TTYS);
673 consbuf = NULL;
674}
675
676/* Times per second to check for pending console tty messages. */
677static int constty_wakeups_per_second = 5;
678SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
679 &constty_wakeups_per_second, 0, "");
680
681static void
682constty_timeout(void *arg)
683{
684 int c;
685
686 while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) {
687 if (tputchar(c, constty) < 0)
688 constty = NULL;
689 }
690 if (constty != NULL) {
691 callout_reset(&conscallout, hz / constty_wakeups_per_second,
692 constty_timeout, NULL);
693 } else {
694 /* Deallocate the constty buffer memory. */
695 constty_clear();
696 }
697}
698
699static void
700cn_drvinit(void *unused)
701{
702
703 make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console");
704}
705
706SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL)