Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / xfree86 / os-support / linux / lnx_init.c
CommitLineData
a09e091a
JB
1/*
2 * Copyright 1992 by Orest Zborowski <obz@Kodak.com>
3 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the names of Orest Zborowski and David Wexelblat
10 * not be used in advertising or publicity pertaining to distribution of
11 * the software without specific, written prior permission. Orest Zborowski
12 * and David Wexelblat make no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
14 * implied warranty.
15 *
16 * OREST ZBOROWSKI AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD
17 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT BE LIABLE
19 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 *
24 */
25
26#ifdef HAVE_XORG_CONFIG_H
27#include <xorg-config.h>
28#endif
29
30#include <X11/X.h>
31#include <X11/Xmd.h>
32
33#include "compiler.h"
34
35#include "xf86.h"
36#include "xf86Priv.h"
37#include "xf86_OSlib.h"
38
39#include <sys/stat.h>
40
41#ifndef K_OFF
42#define K_OFF 0x4
43#endif
44
45#ifndef KDSKBMUTE
46#define KDSKBMUTE 0x4B51
47#endif
48
49static Bool KeepTty = FALSE;
50static int activeVT = -1;
51
52static char vtname[11];
53static struct termios tty_attr; /* tty state to restore */
54static int tty_mode; /* kbd mode to restore */
55
56static void
57drain_console(int fd, void *closure)
58{
59 errno = 0;
60 if (tcflush(fd, TCIOFLUSH) == -1 && errno == EIO) {
61 xf86SetConsoleHandler(NULL, NULL);
62 }
63}
64
65static void
66switch_to(int vt, const char *from)
67{
68 int ret;
69
70 SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_ACTIVATE, vt));
71 if (ret < 0)
72 FatalError("%s: VT_ACTIVATE failed: %s\n", from, strerror(errno));
73
74 SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_WAITACTIVE, vt));
75 if (ret < 0)
76 FatalError("%s: VT_WAITACTIVE failed: %s\n", from, strerror(errno));
77}
78
79void
80xf86OpenConsole(void)
81{
82 int i, fd = -1, ret;
83 struct vt_mode VT;
84 struct vt_stat vts;
85 MessageType from = X_PROBED;
86 char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
87 char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
88
89 if (serverGeneration == 1) {
90
91 /* when KeepTty check if we're run with euid==0 */
92 if (KeepTty && geteuid() != 0)
93 FatalError("xf86OpenConsole:"
94 " Server must be suid root for option \"KeepTTY\"\n");
95
96 /*
97 * setup the virtual terminal manager
98 */
99 if (xf86Info.vtno != -1) {
100 from = X_CMDLINE;
101 }
102 else {
103
104 i = 0;
105 while (tty0[i] != NULL) {
106 if ((fd = open(tty0[i], O_WRONLY, 0)) >= 0)
107 break;
108 i++;
109 }
110
111 if (fd < 0)
112 FatalError("xf86OpenConsole: Cannot open /dev/tty0 (%s)\n",
113 strerror(errno));
114
115 if (xf86Info.ShareVTs) {
116 SYSCALL(ret = ioctl(fd, VT_GETSTATE, &vts));
117 if (ret < 0)
118 FatalError("xf86OpenConsole: Cannot find the current"
119 " VT (%s)\n", strerror(errno));
120 xf86Info.vtno = vts.v_active;
121 }
122 else {
123 SYSCALL(ret = ioctl(fd, VT_OPENQRY, &xf86Info.vtno));
124 if (ret < 0)
125 FatalError("xf86OpenConsole: Cannot find a free VT: "
126 "%s\n", strerror(errno));
127 if (xf86Info.vtno == -1)
128 FatalError("xf86OpenConsole: Cannot find a free VT\n");
129 }
130 close(fd);
131 }
132
133 xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno);
134
135 if (!KeepTty) {
136 pid_t ppid = getppid();
137 pid_t ppgid;
138
139 ppgid = getpgid(ppid);
140
141 /*
142 * change to parent process group that pgid != pid so
143 * that setsid() doesn't fail and we become process
144 * group leader
145 */
146 if (setpgid(0, ppgid) < 0)
147 xf86Msg(X_WARNING, "xf86OpenConsole: setpgid failed: %s\n",
148 strerror(errno));
149
150 /* become process group leader */
151 if ((setsid() < 0))
152 xf86Msg(X_WARNING, "xf86OpenConsole: setsid failed: %s\n",
153 strerror(errno));
154 }
155
156 i = 0;
157 while (vcs[i] != NULL) {
158 snprintf(vtname, sizeof(vtname), vcs[i], xf86Info.vtno); /* /dev/tty1-64 */
159 if ((xf86Info.consoleFd = open(vtname, O_RDWR | O_NDELAY, 0)) >= 0)
160 break;
161 i++;
162 }
163
164 if (xf86Info.consoleFd < 0)
165 FatalError("xf86OpenConsole: Cannot open virtual console"
166 " %d (%s)\n", xf86Info.vtno, strerror(errno));
167
168 /*
169 * Linux doesn't switch to an active vt after the last close of a vt,
170 * so we do this ourselves by remembering which is active now.
171 */
172 SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_GETSTATE, &vts));
173 if (ret < 0)
174 xf86Msg(X_WARNING, "xf86OpenConsole: VT_GETSTATE failed: %s\n",
175 strerror(errno));
176 else
177 activeVT = vts.v_active;
178
179#if 0
180 if (!KeepTty) {
181 /*
182 * Detach from the controlling tty to avoid char loss
183 */
184 if ((i = open("/dev/tty", O_RDWR)) >= 0) {
185 SYSCALL(ioctl(i, TIOCNOTTY, 0));
186 close(i);
187 }
188 }
189#endif
190
191 if (!xf86Info.ShareVTs) {
192 struct termios nTty;
193
194 /*
195 * now get the VT. This _must_ succeed, or else fail completely.
196 */
197 switch_to(xf86Info.vtno, "xf86OpenConsole");
198
199 SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_GETMODE, &VT));
200 if (ret < 0)
201 FatalError("xf86OpenConsole: VT_GETMODE failed %s\n",
202 strerror(errno));
203
204 signal(SIGUSR1, xf86VTRequest);
205
206 VT.mode = VT_PROCESS;
207 VT.relsig = SIGUSR1;
208 VT.acqsig = SIGUSR1;
209
210 SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_SETMODE, &VT));
211 if (ret < 0)
212 FatalError
213 ("xf86OpenConsole: VT_SETMODE VT_PROCESS failed: %s\n",
214 strerror(errno));
215
216 SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS));
217 if (ret < 0)
218 FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed %s\n",
219 strerror(errno));
220
221 tcgetattr(xf86Info.consoleFd, &tty_attr);
222 SYSCALL(ioctl(xf86Info.consoleFd, KDGKBMODE, &tty_mode));
223
224 /* disable kernel special keys and buffering, new style */
225 SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSKBMUTE, 1));
226 if (ret < 0)
227 {
228 /* disable kernel special keys and buffering, old style */
229 SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSKBMODE, K_OFF));
230 if (ret < 0)
231 {
232 /* fine, just disable special keys */
233 SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSKBMODE, K_RAW));
234 if (ret < 0)
235 FatalError("xf86OpenConsole: KDSKBMODE K_RAW failed %s\n",
236 strerror(errno));
237
238 /* ... and drain events, else the kernel gets angry */
239 xf86SetConsoleHandler(drain_console, NULL);
240 }
241 }
242
243 nTty = tty_attr;
244 nTty.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
245 nTty.c_oflag = 0;
246 nTty.c_cflag = CREAD | CS8;
247 nTty.c_lflag = 0;
248 nTty.c_cc[VTIME] = 0;
249 nTty.c_cc[VMIN] = 1;
250 cfsetispeed(&nTty, 9600);
251 cfsetospeed(&nTty, 9600);
252 tcsetattr(xf86Info.consoleFd, TCSANOW, &nTty);
253 }
254 }
255 else { /* serverGeneration != 1 */
256 if (!xf86Info.ShareVTs && xf86Info.autoVTSwitch) {
257 /* now get the VT */
258 switch_to(xf86Info.vtno, "xf86OpenConsole");
259 }
260 }
261}
262
263void
264xf86CloseConsole(void)
265{
266 struct vt_mode VT;
267 int ret;
268
269 if (xf86Info.ShareVTs) {
270 close(xf86Info.consoleFd);
271 return;
272 }
273
274 /*
275 * unregister the drain_console handler
276 * - what to do if someone else changed it in the meantime?
277 */
278 xf86SetConsoleHandler(NULL, NULL);
279
280 /* Back to text mode ... */
281 SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT));
282 if (ret < 0)
283 xf86Msg(X_WARNING, "xf86CloseConsole: KDSETMODE failed: %s\n",
284 strerror(errno));
285
286 SYSCALL(ioctl(xf86Info.consoleFd, KDSKBMUTE, 0));
287 SYSCALL(ioctl(xf86Info.consoleFd, KDSKBMODE, tty_mode));
288 tcsetattr(xf86Info.consoleFd, TCSANOW, &tty_attr);
289
290 SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_GETMODE, &VT));
291 if (ret < 0)
292 xf86Msg(X_WARNING, "xf86CloseConsole: VT_GETMODE failed: %s\n",
293 strerror(errno));
294 else {
295 /* set dflt vt handling */
296 VT.mode = VT_AUTO;
297 SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_SETMODE, &VT));
298 if (ret < 0)
299 xf86Msg(X_WARNING, "xf86CloseConsole: VT_SETMODE failed: %s\n",
300 strerror(errno));
301 }
302
303 if (xf86Info.autoVTSwitch) {
304 /*
305 * Perform a switch back to the active VT when we were started
306 */
307 if (activeVT >= 0) {
308 switch_to(activeVT, "xf86CloseConsole");
309 activeVT = -1;
310 }
311 }
312 close(xf86Info.consoleFd); /* make the vt-manager happy */
313}
314
315int
316xf86ProcessArgument(int argc, char *argv[], int i)
317{
318 /*
319 * Keep server from detaching from controlling tty. This is useful
320 * when debugging (so the server can receive keyboard signals.
321 */
322 if (!strcmp(argv[i], "-keeptty")) {
323 KeepTty = TRUE;
324 return 1;
325 }
326
327 if ((argv[i][0] == 'v') && (argv[i][1] == 't')) {
328 if (sscanf(argv[i], "vt%2d", &xf86Info.vtno) == 0) {
329 UseMsg();
330 xf86Info.vtno = -1;
331 return 0;
332 }
333 return 1;
334 }
335 return 0;
336}
337
338void
339xf86UseMsg(void)
340{
341 ErrorF("vtXX use the specified VT number\n");
342 ErrorF("-keeptty ");
343 ErrorF("don't detach controlling tty (for debugging only)\n");
344}