2 * Copyright 1992 by Orest Zborowski <obz@Kodak.com>
3 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
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
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.
26 #ifdef HAVE_XORG_CONFIG_H
27 #include <xorg-config.h>
37 #include "xf86_OSlib.h"
46 #define KDSKBMUTE 0x4B51
49 static Bool KeepTty
= FALSE
;
50 static int activeVT
= -1;
52 static char vtname
[11];
53 static struct termios tty_attr
; /* tty state to restore */
54 static int tty_mode
; /* kbd mode to restore */
57 drain_console(int fd
, void *closure
)
60 if (tcflush(fd
, TCIOFLUSH
) == -1 && errno
== EIO
) {
61 xf86SetConsoleHandler(NULL
, NULL
);
66 switch_to(int vt
, const char *from
)
70 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, vt
));
72 FatalError("%s: VT_ACTIVATE failed: %s\n", from
, strerror(errno
));
74 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_WAITACTIVE
, vt
));
76 FatalError("%s: VT_WAITACTIVE failed: %s\n", from
, strerror(errno
));
85 MessageType from
= X_PROBED
;
86 char *tty0
[] = { "/dev/tty0", "/dev/vc/0", NULL
};
87 char *vcs
[] = { "/dev/vc/%d", "/dev/tty%d", NULL
};
89 if (serverGeneration
== 1) {
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");
97 * setup the virtual terminal manager
99 if (xf86Info
.vtno
!= -1) {
105 while (tty0
[i
] != NULL
) {
106 if ((fd
= open(tty0
[i
], O_WRONLY
, 0)) >= 0)
112 FatalError("xf86OpenConsole: Cannot open /dev/tty0 (%s)\n",
115 if (xf86Info
.ShareVTs
) {
116 SYSCALL(ret
= ioctl(fd
, VT_GETSTATE
, &vts
));
118 FatalError("xf86OpenConsole: Cannot find the current"
119 " VT (%s)\n", strerror(errno
));
120 xf86Info
.vtno
= vts
.v_active
;
123 SYSCALL(ret
= ioctl(fd
, VT_OPENQRY
, &xf86Info
.vtno
));
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");
133 xf86Msg(from
, "using VT number %d\n\n", xf86Info
.vtno
);
136 pid_t ppid
= getppid();
139 ppgid
= getpgid(ppid
);
142 * change to parent process group that pgid != pid so
143 * that setsid() doesn't fail and we become process
146 if (setpgid(0, ppgid
) < 0)
147 xf86Msg(X_WARNING
, "xf86OpenConsole: setpgid failed: %s\n",
150 /* become process group leader */
152 xf86Msg(X_WARNING
, "xf86OpenConsole: setsid failed: %s\n",
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)
164 if (xf86Info
.consoleFd
< 0)
165 FatalError("xf86OpenConsole: Cannot open virtual console"
166 " %d (%s)\n", xf86Info
.vtno
, strerror(errno
));
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.
172 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_GETSTATE
, &vts
));
174 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_GETSTATE failed: %s\n",
177 activeVT
= vts
.v_active
;
182 * Detach from the controlling tty to avoid char loss
184 if ((i
= open("/dev/tty", O_RDWR
)) >= 0) {
185 SYSCALL(ioctl(i
, TIOCNOTTY
, 0));
191 if (!xf86Info
.ShareVTs
) {
195 * now get the VT. This _must_ succeed, or else fail completely.
197 switch_to(xf86Info
.vtno
, "xf86OpenConsole");
199 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_GETMODE
, &VT
));
201 FatalError("xf86OpenConsole: VT_GETMODE failed %s\n",
204 signal(SIGUSR1
, xf86VTRequest
);
206 VT
.mode
= VT_PROCESS
;
210 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &VT
));
213 ("xf86OpenConsole: VT_SETMODE VT_PROCESS failed: %s\n",
216 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_GRAPHICS
));
218 FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed %s\n",
221 tcgetattr(xf86Info
.consoleFd
, &tty_attr
);
222 SYSCALL(ioctl(xf86Info
.consoleFd
, KDGKBMODE
, &tty_mode
));
224 /* disable kernel special keys and buffering, new style */
225 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, KDSKBMUTE
, 1));
228 /* disable kernel special keys and buffering, old style */
229 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, KDSKBMODE
, K_OFF
));
232 /* fine, just disable special keys */
233 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, KDSKBMODE
, K_RAW
));
235 FatalError("xf86OpenConsole: KDSKBMODE K_RAW failed %s\n",
238 /* ... and drain events, else the kernel gets angry */
239 xf86SetConsoleHandler(drain_console
, NULL
);
244 nTty
.c_iflag
= (IGNPAR
| IGNBRK
) & (~PARMRK
) & (~ISTRIP
);
246 nTty
.c_cflag
= CREAD
| CS8
;
248 nTty
.c_cc
[VTIME
] = 0;
250 cfsetispeed(&nTty
, 9600);
251 cfsetospeed(&nTty
, 9600);
252 tcsetattr(xf86Info
.consoleFd
, TCSANOW
, &nTty
);
255 else { /* serverGeneration != 1 */
256 if (!xf86Info
.ShareVTs
&& xf86Info
.autoVTSwitch
) {
258 switch_to(xf86Info
.vtno
, "xf86OpenConsole");
264 xf86CloseConsole(void)
269 if (xf86Info
.ShareVTs
) {
270 close(xf86Info
.consoleFd
);
275 * unregister the drain_console handler
276 * - what to do if someone else changed it in the meantime?
278 xf86SetConsoleHandler(NULL
, NULL
);
280 /* Back to text mode ... */
281 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_TEXT
));
283 xf86Msg(X_WARNING
, "xf86CloseConsole: KDSETMODE failed: %s\n",
286 SYSCALL(ioctl(xf86Info
.consoleFd
, KDSKBMUTE
, 0));
287 SYSCALL(ioctl(xf86Info
.consoleFd
, KDSKBMODE
, tty_mode
));
288 tcsetattr(xf86Info
.consoleFd
, TCSANOW
, &tty_attr
);
290 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_GETMODE
, &VT
));
292 xf86Msg(X_WARNING
, "xf86CloseConsole: VT_GETMODE failed: %s\n",
295 /* set dflt vt handling */
297 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &VT
));
299 xf86Msg(X_WARNING
, "xf86CloseConsole: VT_SETMODE failed: %s\n",
303 if (xf86Info
.autoVTSwitch
) {
305 * Perform a switch back to the active VT when we were started
308 switch_to(activeVT
, "xf86CloseConsole");
312 close(xf86Info
.consoleFd
); /* make the vt-manager happy */
316 xf86ProcessArgument(int argc
, char *argv
[], int i
)
319 * Keep server from detaching from controlling tty. This is useful
320 * when debugging (so the server can receive keyboard signals.
322 if (!strcmp(argv
[i
], "-keeptty")) {
327 if ((argv
[i
][0] == 'v') && (argv
[i
][1] == 't')) {
328 if (sscanf(argv
[i
], "vt%2d", &xf86Info
.vtno
) == 0) {
341 ErrorF("vtXX use the specified VT number\n");
343 ErrorF("don't detach controlling tty (for debugging only)\n");