39
39
#include <sys/stat.h>
41
41
static Bool KeepTty = FALSE;
42
static int VTnum = -1;
43
42
static Bool VTSwitch = TRUE;
44
43
static Bool ShareVTs = FALSE;
45
44
static int activeVT = -1;
47
static int vtPermSave[4];
48
46
static char vtname[11];
49
47
static struct termios tty_attr; /* tty state to restore */
50
48
static int tty_mode; /* kbd mode to restore */
55
/* We need to use stat to get permissions. */
58
/* Do them numerically ordered, hard coded tty0 first. */
59
if (stat("/dev/tty0", &svtp) != 0)
61
vtPermSave[0] = (int)svtp.st_uid;
62
vtPermSave[1] = (int)svtp.st_gid;
64
/* Now check the console we are dealing with. */
65
if (stat(vtname, &svtp) != 0)
67
vtPermSave[2] = (int)svtp.st_uid;
68
vtPermSave[3] = (int)svtp.st_gid;
77
/* Set the terminal permissions back to before we started. */
78
(void)chown("/dev/tty0", vtPermSave[0], vtPermSave[1]);
79
(void)chown(vtname, vtPermSave[2], vtPermSave[3]);
83
50
static void *console_handler;
86
53
drain_console(int fd, void *closure)
88
tcflush(fd, TCIOFLUSH);
56
if (tcflush(fd, TCIOFLUSH) == -1 && errno == EIO) {
57
xf86RemoveGeneralHandler(console_handler);
58
console_handler = NULL;
63
switch_to(int vt, const char *from)
65
if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, vt) < 0)
66
FatalError("%s: VT_ACTIVATE failed: %s\n", from, strerror(errno));
68
if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, vt) < 0)
69
FatalError("%s: VT_WAITACTIVE failed: %s\n", from, strerror(errno));
175
155
FatalError("xf86OpenConsole: Cannot open virtual console"
176
156
" %d (%s)\n", xf86Info.vtno, strerror(errno));
181
* Grab the vt ownership before we overwrite it.
182
* Hard coded /dev/tty0 into this function as well for below.
186
"xf86OpenConsole: Could not save ownership of VT\n");
188
if (geteuid() == 0) {
189
/* change ownership of the vt */
190
if (chown(vtname, getuid(), getgid()) < 0)
191
xf86Msg(X_WARNING,"xf86OpenConsole: chown %s failed: %s\n",
192
vtname, strerror(errno));
195
* the current VT device we're running on is not
196
* "console", we want to grab all consoles too
198
* Why is this needed??
200
if (chown("/dev/tty0", getuid(), getgid()) < 0)
201
xf86Msg(X_WARNING,"xf86OpenConsole: chown /dev/tty0 failed: %s\n",
207
159
* Linux doesn't switch to an active vt after the last close of a vt,
208
160
* so we do this ourselves by remembering which is active now.
233
185
* now get the VT. This _must_ succeed, or else fail completely.
235
if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) < 0)
236
FatalError("xf86OpenConsole: VT_ACTIVATE failed: %s\n",
239
if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) < 0)
240
FatalError("xf86OpenConsole: VT_WAITACTIVE failed: %s\n",
187
switch_to(xf86Info.vtno, "xf86OpenConsole");
243
189
if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0)
244
190
FatalError("xf86OpenConsole: VT_GETMODE failed %s\n",
277
223
tcsetattr(xf86Info.consoleFd, TCSANOW, &nTty);
279
225
/* need to keep the buffer clean, else the kernel gets angry */
280
if (xf86Info.allowEmptyInput)
281
console_handler = xf86AddGeneralHandler(xf86Info.consoleFd,
282
drain_console, NULL);
226
xf86SetConsoleHandler(drain_console, NULL);
284
228
/* we really should have a InitOSInputDevices() function instead
285
229
* of Init?$#*&Device(). So I just place it here */
287
231
} else { /* serverGeneration != 1 */
288
232
if (!ShareVTs && VTSwitch)
293
if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) < 0)
294
xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed %s\n",
297
if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) < 0)
298
xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed %s\n",
235
switch_to(xf86Info.vtno, "xf86OpenConsole");
341
277
* Perform a switch back to the active VT when we were started
343
279
if (activeVT >= 0) {
344
if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, activeVT) < 0)
345
xf86Msg(X_WARNING, "xf86CloseConsole: VT_ACTIVATE failed: %s\n",
347
if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, activeVT) < 0)
349
"xf86CloseConsole: VT_WAITACTIVE failed: %s\n",
280
switch_to(activeVT, "xf86CloseConsole");
354
284
close(xf86Info.consoleFd); /* make the vt-manager happy */
356
restoreVtPerms(); /* restore the permissions */
381
309
if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
383
if (sscanf(argv[i], "vt%2d", &VTnum) == 0)
311
if (sscanf(argv[i], "vt%2d", &xf86Info.vtno) == 0)