1
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c,v 3.14 2001/10/31 22:50:30 tsi Exp $ */
3
* Copyright 1992 by Orest Zborowski <obz@Kodak.com>
4
* Copyright 1993 by David Wexelblat <dwex@goblin.org>
6
* Permission to use, copy, modify, distribute, and sell this software and its
7
* documentation for any purpose is hereby granted without fee, provided that
8
* the above copyright notice appear in all copies and that both that
9
* copyright notice and this permission notice appear in supporting
10
* documentation, and that the names of Orest Zborowski and David Wexelblat
11
* not be used in advertising or publicity pertaining to distribution of
12
* the software without specific, written prior permission. Orest Zborowski
13
* and David Wexelblat make no representations about the suitability of this
14
* software for any purpose. It is provided "as is" without express or
17
* OREST ZBOROWSKI AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD
18
* TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
* FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT BE LIABLE
20
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26
/* $XConsortium: lnx_init.c /main/7 1996/10/23 18:46:30 kaleb $ */
35
#include "xf86_OSlib.h"
39
extern char *getenv(const char *);
44
static Bool KeepTty = FALSE;
45
static int VTnum = -1;
46
static int activeVT = -1;
56
MessageType from = X_PROBED;
58
struct fb_var_screeninfo var;
61
char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
62
char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
64
if (serverGeneration == 1)
66
/* check if we're run with euid==0 */
69
FatalError("xf86OpenConsole: Server must be suid root\n");
73
* setup the virtual terminal manager
76
xf86Info.vtno = VTnum;
80
while (tty0[i] != NULL)
82
if ((fd = open(tty0[i],O_WRONLY,0)) >= 0)
88
"xf86OpenConsole: Cannot open /dev/tty0 (%s)\n",
90
if ((ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0) ||
91
(xf86Info.vtno == -1)) {
92
FatalError("xf86OpenConsole: Cannot find a free VT\n");
98
fb_dev_name=getenv("FRAMEBUFFER");
100
fb_dev_name="/dev/fb0current";
101
if ((fbfd = open(fb_dev_name, O_RDONLY)) < 0)
102
FatalError("xf86OpenConsole: Cannot open %s (%s)\n",
103
fb_dev_name, strerror(errno));
104
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &var))
105
FatalError("xf86OpenConsole: Unable to get screen info\n");
107
xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno);
114
while (vcs[i] != NULL)
116
sprintf(vtname, vcs[i], xf86Info.vtno); /* /dev/tty1-64 */
117
if ((xf86Info.consoleFd = open(vtname, O_RDWR|O_NDELAY, 0)) >= 0)
122
if (xf86Info.consoleFd < 0) {
123
FatalError("xf86OpenConsole: Cannot open virtual console %d (%s)\n",
124
xf86Info.vtno, strerror(errno));
127
/* change ownership of the vt */
128
chown(vtname, getuid(), getgid());
131
* the current VT device we're running on is not "console", we want
132
* to grab all consoles too
134
* Why is this needed??
136
chown("/dev/tty0", getuid(), getgid());
139
* Linux doesn't switch to an active vt after the last close of a vt,
140
* so we do this ourselves by remembering which is active now.
142
if (ioctl(xf86Info.consoleFd, VT_GETSTATE, &vts) == 0)
144
activeVT = vts.v_active;
150
* Detach from the controlling tty to avoid char loss
152
if ((i = open("/dev/tty",O_RDWR)) >= 0)
154
ioctl(i, TIOCNOTTY, 0);
162
SYSCALL(result = ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno));
165
xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
168
ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno));
171
xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");
173
SYSCALL(result = ioctl(xf86Info.consoleFd, VT_GETMODE, &VT));
176
FatalError("xf86OpenConsole: VT_GETMODE failed\n");
179
signal(SIGUSR1, xf86VTRequest);
181
VT.mode = VT_PROCESS;
184
if (ioctl(xf86Info.consoleFd, VT_SETMODE, &VT) < 0)
186
FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n");
188
if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0)
190
FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed\n");
193
/* we really should have a InitOSInputDevices() function instead
194
* of Init?$#*&Device(). So I just place it here */
197
/* copy info to new console */
200
if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &var))
201
FatalError("Unable to set screen info\n");
207
/* serverGeneration != 1 */
211
SYSCALL(result = ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno));
214
xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
217
ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno));
220
xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");
232
ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno);
233
ioctl(xf86Info.consoleFd, VT_WAITACTIVE, 0);
235
ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT); /* Back to text mode ... */
236
if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1)
239
ioctl(xf86Info.consoleFd, VT_SETMODE, &VT); /* set dflt vt handling */
242
* Perform a switch back to the active VT when we were started
246
ioctl(xf86Info.consoleFd, VT_ACTIVATE, activeVT);
249
close(xf86Info.consoleFd); /* make the vt-manager happy */
254
xf86ProcessArgument(int argc, char *argv[], int i)
257
* Keep server from detaching from controlling tty. This is useful
258
* when debugging (so the server can receive keyboard signals.
260
if (!strcmp(argv[i], "-keeptty"))
265
if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
267
if (sscanf(argv[i], "vt%2d", &VTnum) == 0)
281
ErrorF("vtXX use the specified VT number\n");
283
ErrorF("don't detach controlling tty (for debugging only)\n");