First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / solaris / sun_init.c
blob08d35c59c840386c5d4923b9274b44b000d651ff
1 /*
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
22 * OF THIS SOFTWARE.
25 #ifdef HAVE_XORG_CONFIG_H
26 #include <xorg-config.h>
27 #endif
29 #include "xf86.h"
30 #include "xf86Priv.h"
31 #include "xf86_OSlib.h"
32 #if defined(__i386) || defined(__x86)
33 # include <sys/kd.h>
34 #endif
36 static Bool KeepTty = FALSE;
37 static Bool Protect0 = FALSE;
38 #ifdef HAS_USL_VTS
39 static int VTnum = -1;
40 static int xf86StartVT = -1;
41 #endif
43 #if defined(__SOL8__) || !defined(__i386)
44 static char fb_dev[PATH_MAX] = "/dev/fb";
45 #else
46 static char fb_dev[PATH_MAX] = "/dev/console";
47 #endif
49 void
50 xf86OpenConsole(void)
52 #ifdef HAS_USL_VTS
53 int fd, i;
54 struct vt_mode VT;
55 struct vt_stat vtinfo;
56 int FreeVTslot;
57 MessageType from = X_PROBED;
58 #endif
60 if (serverGeneration == 1)
62 /* Check if we're run with euid==0 */
63 if (geteuid() != 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 */
68 if (Protect0)
70 int fd = -1;
72 if ((fd = open("/dev/zero", O_RDONLY, 0)) < 0)
74 xf86Msg(X_WARNING,
75 "xf86OpenConsole: cannot open /dev/zero (%s)\n",
76 strerror(errno));
78 else
80 if ((int)mmap(0, 0x1000, PROT_NONE,
81 MAP_FIXED | MAP_SHARED, fd, 0) == -1)
82 xf86Msg(X_WARNING,
83 "xf86OpenConsole: failed to protect page 0 (%s)\n",
84 strerror(errno));
86 close(fd);
90 #ifdef HAS_USL_VTS
93 * Setup the virtual terminal manager
95 if (VTnum != -1)
97 xf86Info.vtno = VTnum;
98 from = X_CMDLINE;
100 else
102 if ((fd = open("/dev/vt00",O_RDWR,0)) < 0)
103 FatalError("xf86OpenConsole: Cannot open /dev/vt00 (%s)\n",
104 strerror(errno));
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
124 FreeVTslot = 0;
125 for (i = 7; (i >= 0) && !FreeVTslot; i--)
126 if (ioctl(fd, VT_ACTIVATE, i) != 0)
127 FreeVTslot = 1;
129 if (!FreeVTslot ||
130 (ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0) ||
131 (xf86Info.vtno == -1))
132 FatalError("xf86OpenConsole: Cannot find a free VT\n");
134 close(fd);
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 */
143 if (!KeepTty)
144 setpgrp();
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));
150 #ifdef HAS_USL_VTS
152 /* Change ownership of the vt */
153 chown(fb_dev, getuid(), getgid());
156 * Now get the VT
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;
170 VT.relsig = SIGUSR1;
171 VT.acqsig = SIGUSR1;
173 if (ioctl(xf86Info.consoleFd, VT_SETMODE, &VT) < 0)
174 FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n");
175 #endif
176 #ifdef KDSETMODE
177 if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0)
178 FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed\n");
179 #endif
181 else /* serverGeneration != 1 */
183 #ifdef HAS_USL_VTS
185 * Now re-get the VT
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)
199 sleep(5);
201 #endif /* HAS_USL_VTS */
206 void
207 xf86CloseConsole(void)
209 #ifdef HAS_USL_VTS
210 struct vt_mode VT;
211 #endif
212 #if defined(__SOL8__) || !defined(i386)
213 int tmp;
214 #endif
216 #if !defined(i386) && !defined(__x86)
218 if (!xf86DoProbe && !xf86DoConfigure) {
219 int fd;
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) {
229 xf86Msg(X_WARNING,
230 "xf86CloseConsole(): unable to open framebuffer (%s)\n",
231 strerror(errno));
232 } else {
233 struct fbgattr fbattr;
235 if ((ioctl(fd, FBIOGATTR, &fbattr) < 0) &&
236 (ioctl(fd, FBIOGTYPE, &fbattr.fbtype) < 0)) {
237 xf86Msg(X_WARNING,
238 "xf86CloseConsole(): unable to retrieve framebuffer"
239 " attributes (%s)\n", strerror(errno));
240 } else {
241 pointer fbdata;
243 fbdata = mmap(NULL, fbattr.fbtype.fb_size,
244 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
245 if (fbdata == MAP_FAILED) {
246 xf86Msg(X_WARNING,
247 "xf86CloseConsole(): unable to mmap framebuffer"
248 " (%s)\n", strerror(errno));
249 } else {
250 (void)memset(fbdata, 0, fbattr.fbtype.fb_size);
251 (void)munmap(fbdata, fbattr.fbtype.fb_size);
255 close(fd);
259 #endif
261 #ifdef KDSETMODE
262 /* Reset the display back to text mode */
263 ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT);
264 #endif
266 #ifdef HAS_USL_VTS
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.
275 * Doug Anson 11/6/93
276 * danson@lgc.com
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"))
305 KeepTty = TRUE;
306 return 1;
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"))
315 Protect0 = TRUE;
316 return 1;
319 #ifdef HAS_USL_VTS
321 if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
323 if (sscanf(argv[i], "vt%2d", &VTnum) == 0)
325 UseMsg();
326 VTnum = -1;
327 return 0;
330 return 1;
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';
341 return 2;
345 #endif
347 return 0;
350 void xf86UseMsg()
352 #ifdef HAS_USL_VTS
353 ErrorF("vtXX Use the specified VT number\n");
354 #endif
355 #if defined(__SOL8__) || !defined(i386)
356 ErrorF("-dev <fb> Framebuffer device\n");
357 #endif
358 ErrorF("-keeptty Don't detach controlling tty\n");
359 ErrorF(" (for debugging only)\n");