cons.c revision 1.11 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: cons.c,v 1.11 1994/02/01 03:35:06 cgd Exp $
40 */
41
42 #include <sys/param.h>
43 #include <sys/proc.h>
44 #include <sys/user.h>
45 #include <sys/systm.h>
46 #include <sys/buf.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/file.h>
50 #include <sys/conf.h>
51 #include <sys/vnode.h>
52
53 #include <dev/cons.h>
54
55 extern struct consdev constab[];
56
57 struct tty *constty = NULL; /* virtual console output device */
58 struct consdev *cn_tab; /* physical console device info */
59
60 void
61 cninit()
62 {
63 register struct consdev *cp;
64
65 /*
66 * Collect information about all possible consoles
67 * and find the one with highest priority
68 */
69 for (cp = constab; cp->cn_probe; cp++) {
70 (*cp->cn_probe)(cp);
71 if (cp->cn_pri > CN_DEAD &&
72 (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
73 cn_tab = cp;
74 }
75 /*
76 * No console, we can handle it
77 */
78 if ((cp = cn_tab) == NULL)
79 return;
80 /*
81 * Turn on console
82 */
83 (*cp->cn_init)(cp);
84 }
85
86 int
87 cnopen(dev, flag, mode, p)
88 dev_t dev;
89 int flag, mode;
90 struct proc *p;
91 {
92 if (cn_tab == NULL)
93 return (0);
94
95 /*
96 * always open the 'real' console device, so we don't get nailed
97 * later. This follows normal device semantics; they always get
98 * open() calls.
99 */
100 dev = cn_tab->cn_dev;
101 return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
102 }
103
104 int
105 cnclose(dev, flag, mode, p)
106 dev_t dev;
107 int flag, mode;
108 struct proc *p;
109 {
110 struct vnode *vp;
111
112 if (cn_tab == NULL)
113 return (0);
114
115 /*
116 * If the real console isn't otherwise open, close it.
117 * If it's otherwise open, don't close it, because that'll
118 * screw up others who have it open.
119 */
120 dev = cn_tab->cn_dev;
121 if ((vfinddev(dev, VCHR, &vp) == 0) && vcount(vp))
122 return (0);
123 return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
124 }
125
126 int
127 cnread(dev, uio, flag)
128 dev_t dev;
129 struct uio *uio;
130 {
131 /*
132 * If we would redirect input, punt. This will keep strange
133 * things from happening to people who are using the real
134 * console. Nothing should be using /dev/console for
135 * input (except a shell in single-user mode, but then,
136 * one wouldn't TIOCCONS then).
137 */
138 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
139 return 0;
140 else if (cn_tab == NULL)
141 return ENXIO;
142
143 dev = cn_tab->cn_dev;
144 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
145 }
146
147 int
148 cnwrite(dev, uio, flag)
149 dev_t dev;
150 struct uio *uio;
151 {
152 /*
153 * Redirect output, if that's appropriate.
154 * If there's no real console, return ENXIO.
155 */
156 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
157 dev = constty->t_dev;
158 else if (cn_tab == NULL)
159 return ENXIO;
160 else
161 dev = cn_tab->cn_dev;
162 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
163 }
164
165 int
166 cnioctl(dev, cmd, data, flag, p)
167 dev_t dev;
168 caddr_t data;
169 struct proc *p;
170 {
171 int error;
172
173 /*
174 * Superuser can always use this to wrest control of console
175 * output from the "virtual" console.
176 */
177 if (cmd == TIOCCONS && constty != NULL) {
178 error = suser(p->p_ucred, (u_short *) NULL);
179 if (error)
180 return (error);
181 constty = NULL;
182 return (0);
183 }
184
185 /*
186 * Redirect the ioctl, if that's appropriate.
187 * Note that strange things can happen, if a program does
188 * ioctls on /dev/console, then the console is redirected
189 * out from under it.
190 */
191 if (constty != NULL && (cn_tab != NULL || cn_tab->cn_pri != CN_REMOTE))
192 dev = constty->t_dev;
193 else if (cn_tab == NULL)
194 return ENXIO;
195 else
196 dev = cn_tab->cn_dev;
197 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
198 }
199
200 /*ARGSUSED*/
201 int
202 cnselect(dev, rw, p)
203 dev_t dev;
204 int rw;
205 struct proc *p;
206 {
207 /*
208 * Redirect the ioctl, if that's appropriate.
209 * I don't want to think of the possible side effects
210 * of console redirection here.
211 */
212 if (constty != NULL && (cn_tab != NULL || cn_tab->cn_pri != CN_REMOTE))
213 dev = constty->t_dev;
214 else if (cn_tab == NULL)
215 return ENXIO;
216 else
217 dev = cn_tab->cn_dev;
218 return (ttselect(cn_tab->cn_dev, rw, p));
219 }
220
221 int
222 cngetc()
223 {
224 if (cn_tab == NULL)
225 return (0);
226 return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
227 }
228
229 int
230 cnputc(c)
231 register int c;
232 {
233 if (cn_tab == NULL)
234 return;
235 if (c) {
236 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
237 if (c == '\n')
238 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
239 }
240 }
241