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"
37 * Applications see VT number as consecutive integers starting from 1.
39 * -------------------------------------------------------
40 * 1 : /dev/vt/0 (Alt + Ctrl + F1)
41 * 2 : /dev/vt/2 (Alt + Ctrl + F2)
42 * 3 : /dev/vt/3 (Alt + Ctrl + F3)
45 #define CONSOLE_VTNO 1
46 #define SOL_CONSOLE_DEV "/dev/console"
48 static Bool KeepTty
= FALSE
;
49 static Bool Protect0
= FALSE
;
50 static Bool UseConsole
= FALSE
;
53 static int VTnum
= -1;
54 static int xf86StartVT
= -1;
55 static int vtEnabled
= 0;
56 extern void xf86VTAcquire(int);
57 extern void xf86VTRelease(int);
60 /* Device to open as xf86Info.consoleFd */
61 static char consoleDev
[PATH_MAX
] = "/dev/fb";
63 /* Set by -dev argument on CLI
64 Used by hw/xfree86/common/xf86AutoConfig.c for VIS_GETIDENTIFIER */
65 _X_HIDDEN
char xf86SolarisFbDev
[PATH_MAX
] = "/dev/fb";
68 switch_to(int vt
, const char *from
)
72 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, vt
));
74 xf86Msg(X_WARNING
, "%s: VT_ACTIVATE failed: %s\n",
75 from
, strerror(errno
));
77 SYSCALL(ret
= ioctl(xf86Info
.consoleFd
, VT_WAITACTIVE
, vt
));
79 xf86Msg(X_WARNING
, "%s: VT_WAITACTIVE failed: %s\n",
80 from
, strerror(errno
));
91 struct vt_stat vtinfo
;
92 MessageType from
= X_PROBED
;
95 if (serverGeneration
== 1) {
96 /* Check if we're run with euid==0 */
98 FatalError("xf86OpenConsole: Server must be suid root\n");
100 /* Protect page 0 to help find NULL dereferencing */
101 /* mprotect() doesn't seem to work */
105 if ((fd
= open("/dev/zero", O_RDONLY
, 0)) < 0) {
107 "xf86OpenConsole: cannot open /dev/zero (%s)\n",
111 if (mmap(0, 0x1000, PROT_NONE
,
112 MAP_FIXED
| MAP_SHARED
, fd
, 0) == MAP_FAILED
)
114 "xf86OpenConsole: failed to protect page 0 (%s)\n",
124 * Setup the virtual terminal manager
126 if ((fd
= open("/dev/vt/0", O_RDWR
, 0)) == -1) {
127 xf86ErrorF("xf86OpenConsole: Cannot open /dev/vt/0 (%s)\n",
132 if (ioctl(fd
, VT_ENABLED
, &vtEnabled
) < 0) {
133 xf86ErrorF("xf86OpenConsole: VT_ENABLED failed (%s)\n",
138 #endif /* HAS_USL_VTS */
141 strlcpy(consoleDev
, SOL_CONSOLE_DEV
, sizeof(consoleDev
));
144 xf86Info
.vtno
= CONSOLE_VTNO
;
146 if (vtEnabled
== 0) {
150 if (ioctl(fd
, VT_GETSTATE
, &vtinfo
) < 0)
152 ("xf86OpenConsole: Cannot determine current VT\n");
153 xf86StartVT
= vtinfo
.v_active
;
155 #endif /* HAS_USL_VTS */
160 if (vtEnabled
== 0) {
161 /* VT not enabled - kernel too old or Sparc platforms
162 without visual_io support */
163 xf86Msg(from
, "VT infrastructure is not available\n");
167 strlcpy(consoleDev
, xf86SolarisFbDev
, sizeof(consoleDev
));
171 if (ioctl(fd
, VT_GETSTATE
, &vtinfo
) < 0)
172 FatalError("xf86OpenConsole: Cannot determine current VT\n");
174 xf86StartVT
= vtinfo
.v_active
;
177 xf86Info
.vtno
= VTnum
;
180 else if (xf86Info
.ShareVTs
) {
181 xf86Info
.vtno
= vtinfo
.v_active
;
185 if ((ioctl(fd
, VT_OPENQRY
, &xf86Info
.vtno
) < 0) ||
186 (xf86Info
.vtno
== -1)) {
187 FatalError("xf86OpenConsole: Cannot find a free VT\n");
191 xf86Msg(from
, "using VT number %d\n\n", xf86Info
.vtno
);
192 snprintf(consoleDev
, PATH_MAX
, "/dev/vt/%d", xf86Info
.vtno
);
198 #endif /* HAS_USL_VTS */
204 if (((xf86Info
.consoleFd
= open(consoleDev
, O_RDWR
| O_NDELAY
, 0)) < 0))
205 FatalError("xf86OpenConsole: Cannot open %s (%s)\n",
206 consoleDev
, strerror(errno
));
208 /* Change ownership of the vt or console */
209 chown(consoleDev
, getuid(), getgid());
212 if (xf86Info
.ShareVTs
)
219 switch_to(xf86Info
.vtno
, "xf86OpenConsole");
221 #ifdef VT_SET_CONSUSER /* added in snv_139 */
222 if (strcmp(display
, "0") == 0)
223 if (ioctl(xf86Info
.consoleFd
, VT_SET_CONSUSER
) != 0)
225 "xf86OpenConsole: VT_SET_CONSUSER failed\n");
228 if (ioctl(xf86Info
.consoleFd
, VT_GETMODE
, &VT
) < 0)
229 FatalError("xf86OpenConsole: VT_GETMODE failed\n");
231 OsSignal(SIGUSR1
, xf86VTAcquire
);
232 OsSignal(SIGUSR2
, xf86VTRelease
);
234 VT
.mode
= VT_PROCESS
;
238 if (ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &VT
) < 0)
239 FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n");
241 if (ioctl(xf86Info
.consoleFd
, VT_SETDISPINFO
, atoi(display
)) < 0)
242 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_SETDISPINFO failed\n");
247 SYSCALL(i
= ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_GRAPHICS
));
250 "xf86OpenConsole: KDSETMODE KD_GRAPHICS failed on %s (%s)\n",
251 consoleDev
, strerror(errno
));
255 else { /* serverGeneration != 1 */
258 if (vtEnabled
&& !xf86Info
.ShareVTs
) {
262 if (xf86Info
.autoVTSwitch
)
263 switch_to(xf86Info
.vtno
, "xf86OpenConsole");
265 #ifdef VT_SET_CONSUSER /* added in snv_139 */
266 if (strcmp(display
, "0") == 0)
267 if (ioctl(xf86Info
.consoleFd
, VT_SET_CONSUSER
) != 0)
269 "xf86OpenConsole: VT_SET_CONSUSER failed\n");
273 * If the server doesn't have the VT when the reset occurs,
274 * this is to make sure we don't continue until the activate
275 * signal is received.
280 #endif /* HAS_USL_VTS */
286 xf86CloseConsole(void)
292 #if !defined(__i386__) && !defined(__i386) && !defined(__x86)
294 if (!xf86DoConfigure
) {
298 * Wipe out framebuffer just like the non-SI Xsun server does. This
299 * could be improved by saving framebuffer contents in
300 * xf86OpenConsole() above and restoring them here. Also, it's unclear
301 * at this point whether this should be done for all framebuffers in
302 * the system, rather than only the console.
304 if ((fd
= open(xf86SolarisFbDev
, O_RDWR
, 0)) < 0) {
306 "xf86CloseConsole(): unable to open framebuffer (%s)\n",
310 struct fbgattr fbattr
;
312 if ((ioctl(fd
, FBIOGATTR
, &fbattr
) < 0) &&
313 (ioctl(fd
, FBIOGTYPE
, &fbattr
.fbtype
) < 0)) {
315 "xf86CloseConsole(): unable to retrieve framebuffer"
316 " attributes (%s)\n", strerror(errno
));
321 fbdata
= mmap(NULL
, fbattr
.fbtype
.fb_size
,
322 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
323 if (fbdata
== MAP_FAILED
) {
325 "xf86CloseConsole(): unable to mmap framebuffer"
326 " (%s)\n", strerror(errno
));
329 memset(fbdata
, 0, fbattr
.fbtype
.fb_size
);
330 munmap(fbdata
, fbattr
.fbtype
.fb_size
);
341 /* Reset the display back to text mode */
342 SYSCALL(ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_TEXT
));
347 if (ioctl(xf86Info
.consoleFd
, VT_GETMODE
, &VT
) != -1) {
348 VT
.mode
= VT_AUTO
; /* Set default vt handling */
349 ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &VT
);
352 /* Activate the VT that X was started on */
353 if (xf86Info
.autoVTSwitch
)
354 switch_to(xf86StartVT
, "xf86CloseConsole");
356 #endif /* HAS_USL_VTS */
358 close(xf86Info
.consoleFd
);
362 xf86ProcessArgument(int argc
, char **argv
, int i
)
365 * Keep server from detaching from controlling tty. This is useful when
366 * debugging, so the server can receive keyboard signals.
368 if (!strcmp(argv
[i
], "-keeptty")) {
374 * Undocumented flag to protect page 0 from read/write to help catch NULL
375 * pointer dereferences. This is purely a debugging flag.
377 if (!strcmp(argv
[i
], "-protect0")) {
383 * Use /dev/console as the console device.
385 if (!strcmp(argv
[i
], "-C")) {
392 if ((argv
[i
][0] == 'v') && (argv
[i
][1] == 't')) {
393 if (sscanf(argv
[i
], "vt%d", &VTnum
) == 0) {
402 #endif /* HAS_USL_VTS */
404 if ((i
+ 1) < argc
) {
405 if (!strcmp(argv
[i
], "-dev")) {
406 strlcpy(xf86SolarisFbDev
, argv
[i
+ 1], sizeof(xf86SolarisFbDev
));
418 ErrorF("vtX Use the specified VT number\n");
420 ErrorF("-dev <fb> Framebuffer device\n");
421 ErrorF("-keeptty Don't detach controlling tty\n");
422 ErrorF(" (for debugging only)\n");
423 ErrorF("-C Use /dev/console as the console device\n");