2 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany
3 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
4 * Copyright 1999 by David Holland <davidh@iquest.net>
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 copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the names of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 #ifdef HAVE_XORG_CONFIG_H
26 #include <xorg-config.h>
31 #include "xf86_OSlib.h"
32 #if defined(__i386) || defined(__x86)
36 static Bool KeepTty
= FALSE
;
37 static Bool Protect0
= FALSE
;
39 static int VTnum
= -1;
40 static int xf86StartVT
= -1;
43 #if defined(__SOL8__) || !defined(__i386)
44 static char fb_dev
[PATH_MAX
] = "/dev/fb";
46 static char fb_dev
[PATH_MAX
] = "/dev/console";
55 struct vt_stat vtinfo
;
57 MessageType from
= X_PROBED
;
60 if (serverGeneration
== 1)
62 /* Check if we're run with euid==0 */
64 FatalError("xf86OpenConsole: Server must be suid root\n");
66 /* Protect page 0 to help find NULL dereferencing */
67 /* mprotect() doesn't seem to work */
72 if ((fd
= open("/dev/zero", O_RDONLY
, 0)) < 0)
75 "xf86OpenConsole: cannot open /dev/zero (%s)\n",
80 if ((int)mmap(0, 0x1000, PROT_NONE
,
81 MAP_FIXED
| MAP_SHARED
, fd
, 0) == -1)
83 "xf86OpenConsole: failed to protect page 0 (%s)\n",
93 * Setup the virtual terminal manager
97 xf86Info
.vtno
= VTnum
;
102 if ((fd
= open("/dev/vt00",O_RDWR
,0)) < 0)
103 FatalError("xf86OpenConsole: Cannot open /dev/vt00 (%s)\n",
106 if (ioctl(fd
, VT_GETSTATE
, &vtinfo
) < 0)
107 FatalError("xf86OpenConsole: Cannot determine current VT\n");
109 xf86StartVT
= vtinfo
.v_active
;
112 * There is a SEVERE problem with x86's VT's. The VT_OPENQRY
113 * ioctl() will panic the entire system if all 8 (7 VT's+Console)
114 * terminals are used. The only other way I've found to determine
115 * if there is a free VT is to try activating all the the available
116 * VT's and see if they all succeed - if they do, there there is no
117 * free VT, and the Xserver cannot continue without panic'ing the
118 * system. (It's ugly, but it seems to work.) Note there is a
119 * possible race condition here.
121 * David Holland 2/23/94
125 for (i
= 7; (i
>= 0) && !FreeVTslot
; i
--)
126 if (ioctl(fd
, VT_ACTIVATE
, i
) != 0)
130 (ioctl(fd
, VT_OPENQRY
, &xf86Info
.vtno
) < 0) ||
131 (xf86Info
.vtno
== -1))
132 FatalError("xf86OpenConsole: Cannot find a free VT\n");
137 xf86Msg(from
, "using VT number %d\n\n", xf86Info
.vtno
);
139 sprintf(fb_dev
, "/dev/vt%02d", xf86Info
.vtno
); /* Solaris 2.1 x86 */
141 #endif /* HAS_USL_VTS */
146 if (((xf86Info
.consoleFd
= open(fb_dev
, O_RDWR
| O_NDELAY
, 0)) < 0))
147 FatalError("xf86OpenConsole: Cannot open %s (%s)\n",
148 fb_dev
, strerror(errno
));
152 /* Change ownership of the vt */
153 chown(fb_dev
, getuid(), getgid());
158 if (ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, xf86Info
.vtno
) != 0)
159 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_ACTIVATE failed\n");
161 if (ioctl(xf86Info
.consoleFd
, VT_WAITACTIVE
, xf86Info
.vtno
) != 0)
162 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_WAITACTIVE failed\n");
164 if (ioctl(xf86Info
.consoleFd
, VT_GETMODE
, &VT
) < 0)
165 FatalError("xf86OpenConsole: VT_GETMODE failed\n");
167 signal(SIGUSR1
, xf86VTRequest
);
169 VT
.mode
= VT_PROCESS
;
173 if (ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &VT
) < 0)
174 FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n");
177 if (ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_GRAPHICS
) < 0)
178 FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed\n");
181 else /* serverGeneration != 1 */
187 if (ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, xf86Info
.vtno
) != 0)
188 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_ACTIVATE failed\n");
190 if (ioctl(xf86Info
.consoleFd
, VT_WAITACTIVE
, xf86Info
.vtno
) != 0)
191 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_WAITACTIVE failed\n");
194 * If the server doesn't have the VT when the reset occurs,
195 * this is to make sure we don't continue until the activate
196 * signal is received.
198 if (!xf86Screens
[0]->vtSema
)
201 #endif /* HAS_USL_VTS */
207 xf86CloseConsole(void)
212 #if defined(__SOL8__) || !defined(i386)
216 #if !defined(i386) && !defined(__x86)
218 if (!xf86DoProbe
&& !xf86DoConfigure
) {
222 * Wipe out framebuffer just like the non-SI Xsun server does. This
223 * could be improved by saving framebuffer contents in
224 * xf86OpenConsole() above and restoring them here. Also, it's unclear
225 * at this point whether this should be done for all framebuffers in
226 * the system, rather than only the console.
228 if ((fd
= open("/dev/fb", O_RDWR
, 0)) < 0) {
230 "xf86CloseConsole(): unable to open framebuffer (%s)\n",
233 struct fbgattr fbattr
;
235 if ((ioctl(fd
, FBIOGATTR
, &fbattr
) < 0) &&
236 (ioctl(fd
, FBIOGTYPE
, &fbattr
.fbtype
) < 0)) {
238 "xf86CloseConsole(): unable to retrieve framebuffer"
239 " attributes (%s)\n", strerror(errno
));
243 fbdata
= mmap(NULL
, fbattr
.fbtype
.fb_size
,
244 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
245 if (fbdata
== MAP_FAILED
) {
247 "xf86CloseConsole(): unable to mmap framebuffer"
248 " (%s)\n", strerror(errno
));
250 (void)memset(fbdata
, 0, fbattr
.fbtype
.fb_size
);
251 (void)munmap(fbdata
, fbattr
.fbtype
.fb_size
);
262 /* Reset the display back to text mode */
263 ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_TEXT
);
269 * Solaris 2.1 x86 doesn't seem to "switch" back to the console when the VT
270 * is relinquished and its mode is reset to auto. Also, Solaris 2.1 seems
271 * to associate vt00 with the console so I've opened the "console" back up
272 * and made it the active vt again in text mode and then closed it. There
273 * must be a better hack for this but I'm not aware of one at this time.
278 * Fixed - 12/5/93 - David Holland - davidh@dorite.use.com
279 * Did the whole thing similarly to the way linux does it
282 if (ioctl(xf86Info
.consoleFd
, VT_GETMODE
, &VT
) != -1)
284 VT
.mode
= VT_AUTO
; /* Set default vt handling */
285 ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &VT
);
288 /* Activate the VT that X was started on */
289 ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, xf86StartVT
);
291 #endif /* HAS_USL_VTS */
293 close(xf86Info
.consoleFd
);
297 xf86ProcessArgument(int argc
, char **argv
, int i
)
300 * Keep server from detaching from controlling tty. This is useful when
301 * debugging, so the server can receive keyboard signals.
303 if (!strcmp(argv
[i
], "-keeptty"))
310 * Undocumented flag to protect page 0 from read/write to help catch NULL
311 * pointer dereferences. This is purely a debugging flag.
313 if (!strcmp(argv
[i
], "-protect0"))
321 if ((argv
[i
][0] == 'v') && (argv
[i
][1] == 't'))
323 if (sscanf(argv
[i
], "vt%2d", &VTnum
) == 0)
333 #endif /* HAS_USL_VTS */
335 #if defined(__SOL8__) || !defined(i386)
337 if ((i
+ 1) < argc
) {
338 if (!strcmp(argv
[i
], "-dev")) {
339 strncpy(fb_dev
, argv
[i
+1], PATH_MAX
);
340 fb_dev
[PATH_MAX
- 1] = '\0';
353 ErrorF("vtXX Use the specified VT number\n");
355 #if defined(__SOL8__) || !defined(i386)
356 ErrorF("-dev <fb> Framebuffer device\n");
358 ErrorF("-keeptty Don't detach controlling tty\n");
359 ErrorF(" (for debugging only)\n");