summaryrefslogtreecommitdiff
path: root/hw/xfree86/os-support/solaris/sun_init.c
blob: c7382e0e1108c61686731bfc0190ed4531bdb0a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/sunos/sun_init.c,v 1.5 2001/11/25 13:51:24 tsi Exp $ */
/*
 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany
 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
 * Copyright 1999 by David Holland <davidh@iquest.net>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the names of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#ifdef __i386
# include <sys/kd.h>
#endif

static Bool KeepTty = FALSE;
static Bool Protect0 = FALSE;
#ifdef HAS_USL_VTS
static int VTnum = -1;
static int xf86StartVT = -1;
#endif

#if defined(__SOL8__) || !defined(__i386)
static char fb_dev[PATH_MAX] = "/dev/fb";
#else
static char fb_dev[PATH_MAX] = "/dev/console";
#endif

void
xf86OpenConsole(void)
{
#ifdef HAS_USL_VTS
    int fd, i;
    struct vt_mode VT;
    struct vt_stat vtinfo;
    int FreeVTslot;
    MessageType from = X_PROBED;
#endif

    if (serverGeneration == 1)
    {
	/* Check if we're run with euid==0 */
	if (geteuid() != 0)
	    FatalError("xf86OpenConsole: Server must be suid root\n");

	/* Protect page 0 to help find NULL dereferencing */
	/* mprotect() doesn't seem to work */
	if (Protect0)
	{
	    int fd = -1;

	    if ((fd = open("/dev/zero", O_RDONLY, 0)) < 0)
	    {
		xf86Msg(X_WARNING,
			"xf86OpenConsole: cannot open /dev/zero (%s)\n",
			strerror(errno));
	    }
	    else
	    {
		if ((int)mmap(0, 0x1000, PROT_NONE,
			      MAP_FIXED | MAP_SHARED, fd, 0) == -1)
		    xf86Msg(X_WARNING,
			"xf86OpenConsole: failed to protect page 0 (%s)\n",
			strerror(errno));

		close(fd);
	    }
	}

#ifdef HAS_USL_VTS

	/*
	 * Setup the virtual terminal manager
	 */
	if (VTnum != -1)
	{
	    xf86Info.vtno = VTnum;
	    from = X_CMDLINE;
	}
	else
	{
	    if ((fd = open("/dev/vt00",O_RDWR,0)) < 0)
		FatalError("xf86OpenConsole: Cannot open /dev/vt00 (%s)\n",
		    strerror(errno));

	    if (ioctl(fd, VT_GETSTATE, &vtinfo) < 0)
		FatalError("xf86OpenConsole: Cannot determine current VT\n");

	    xf86StartVT = vtinfo.v_active;

	    /*
	     * There is a SEVERE problem with x86's VT's.  The VT_OPENQRY
	     * ioctl() will panic the entire system if all 8 (7 VT's+Console)
	     * terminals are used.  The only other way I've found to determine
	     * if there is a free VT is to try activating all the the available
	     * VT's and see if they all succeed - if they do, there there is no
	     * free VT, and the Xserver cannot continue without panic'ing the
	     * system.  (It's ugly, but it seems to work.)  Note there is a
	     * possible race condition here.
	     *
	     * David Holland 2/23/94
	     */

	    FreeVTslot = 0;
	    for (i = 7; (i >= 0) && !FreeVTslot; i--)
		if (ioctl(fd, VT_ACTIVATE, i) != 0)
		    FreeVTslot = 1;

	    if (!FreeVTslot ||
	        (ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0) ||
		(xf86Info.vtno == -1))
		FatalError("xf86OpenConsole: Cannot find a free VT\n");

	    close(fd);
	}

	xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno);

	sprintf(fb_dev, "/dev/vt%02d", xf86Info.vtno); /* Solaris 2.1 x86 */

#endif /* HAS_USL_VTS */

	if (!KeepTty)
	    setpgrp();

	if (((xf86Info.consoleFd = open(fb_dev, O_RDWR | O_NDELAY, 0)) < 0))
	    FatalError("xf86OpenConsole: Cannot open %s (%s)\n",
		       fb_dev, strerror(errno));

#ifdef HAS_USL_VTS

	/* Change ownership of the vt */
	chown(fb_dev, getuid(), getgid());

	/*
	 * Now get the VT
	 */
	if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");

	if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) != 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");

	if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0)
	    FatalError("xf86OpenConsole: VT_GETMODE failed\n");

	signal(SIGUSR1, xf86VTRequest);

	VT.mode = VT_PROCESS;
	VT.relsig = SIGUSR1;
	VT.acqsig = SIGUSR1;

	if (ioctl(xf86Info.consoleFd, VT_SETMODE, &VT) < 0)
	    FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n");
#endif
#ifdef __i386
	if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0)
	    FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed\n");
#endif
    }
    else /* serverGeneration != 1 */
    {
#ifdef HAS_USL_VTS
	/*
	 * Now re-get the VT
	 */
	if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");

	if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) != 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");

	/*
	 * If the server doesn't have the VT when the reset occurs,
	 * this is to make sure we don't continue until the activate
	 * signal is received.
	 */
	if (!xf86Screens[0]->vtSema)
	    sleep(5);

#endif /* HAS_USL_VTS */

    }
}

void
xf86CloseConsole(void)
{
#ifdef HAS_USL_VTS
    struct vt_mode VT;
#endif
#if defined(__SOL8__) || !defined(i386)
    int tmp;
#endif

#ifndef i386

    if (!xf86DoProbe && !xf86DoConfigure) {
	int fd;

	/*
	 * Wipe out framebuffer just like the non-SI Xsun server does.  This
	 * could be improved by saving framebuffer contents in
	 * xf86OpenConsole() above and restoring them here.  Also, it's unclear
	 * at this point whether this should be done for all framebuffers in
	 * the system, rather than only the console.
	 */
	if ((fd = open("/dev/fb", O_RDWR, 0)) < 0) {
	    xf86Msg(X_WARNING,
		    "xf86CloseConsole():  unable to open framebuffer (%s)\n",
		    strerror(errno));
	} else {
	    struct fbgattr fbattr;

	    if ((ioctl(fd, FBIOGATTR, &fbattr) < 0) &&
		(ioctl(fd, FBIOGTYPE, &fbattr.fbtype) < 0)) {
		xf86Msg(X_WARNING,
			"xf86CloseConsole():  unable to retrieve framebuffer"
			" attributes (%s)\n", strerror(errno));
	    } else {
		pointer fbdata;

		fbdata = mmap(NULL, fbattr.fbtype.fb_size,
			      PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
		if (fbdata == MAP_FAILED) {
		    xf86Msg(X_WARNING,
			    "xf86CloseConsole():  unable to mmap framebuffer"
			    " (%s)\n", strerror(errno));
		} else {
		    (void)memset(fbdata, 0, fbattr.fbtype.fb_size);
		    (void)munmap(fbdata, fbattr.fbtype.fb_size);
		}
	    }

	    close(fd);
	}
    }

#endif

#ifdef __i386
    /* Reset the display back to text mode */
    ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT);
#endif

#ifdef HAS_USL_VTS

    /*
     * Solaris 2.1 x86 doesn't seem to "switch" back to the console when the VT
     * is relinquished and its mode is reset to auto.  Also, Solaris 2.1 seems
     * to associate vt00 with the console so I've opened the "console" back up
     * and made it the active vt again in text mode and then closed it.  There
     * must be a better hack for this but I'm not aware of one at this time.
     *
     * Doug Anson 11/6/93
     * danson@lgc.com
     *
     * Fixed - 12/5/93 - David Holland - davidh@dorite.use.com
     * Did the whole thing similarly to the way linux does it
     */

    if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1)
    {
	VT.mode = VT_AUTO;		/* Set default vt handling */
	ioctl(xf86Info.consoleFd, VT_SETMODE, &VT);
    }

    /* Activate the VT that X was started on */
    ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86StartVT);

#endif /* HAS_USL_VTS */

    close(xf86Info.consoleFd);

#if defined(__SOL8__) || !defined(i386)

    /*
     * This probably shouldn't be here.  However, there is no corresponding
     * xf86CloseKbd() routine - DWH
     */

    /* Set the keyboard into "indirect" mode and turn off even translation */
    tmp = 0;
    (void) ioctl(xf86Info.kbdFd, KIOCSDIRECT, &tmp);
    tmp = TR_ASCII;
    (void) ioctl(xf86Info.kbdFd, KIOCTRANS, &tmp);

    close(xf86Info.kbdFd);

#endif
}

int
xf86ProcessArgument(int argc, char **argv, int i)
{
    /*
     * Keep server from detaching from controlling tty.  This is useful when
     * debugging, so the server can receive keyboard signals.
     */
    if (!strcmp(argv[i], "-keeptty"))
    {
	KeepTty = TRUE;
	return 1;
    }

    /*
     * Undocumented flag to protect page 0 from read/write to help catch NULL
     * pointer dereferences.  This is purely a debugging flag.
     */
    if (!strcmp(argv[i], "-protect0"))
    {
	Protect0 = TRUE;
	return 1;
    }

#ifdef HAS_USL_VTS

    if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
    {
	if (sscanf(argv[i], "vt%2d", &VTnum) == 0)
	{
	    UseMsg();
	    VTnum = -1;
	    return 0;
	}

	return 1;
    }

#endif /* HAS_USL_VTS */

#if defined(__SOL8__) || !defined(i386)

    if ((i + 1) < argc) {
	if (!strcmp(argv[i], "-dev")) {
	    strncpy(fb_dev, argv[i+1], PATH_MAX);
	    fb_dev[PATH_MAX - 1] = '\0';
	    return 2;
	}

	if (!strcmp(argv[i], "-ar1")) {
	    xf86Info.kbdDelay = atoi(argv[i + 1]) * 1000;
	    return 2;
	}

	if (!strcmp(argv[i], "-ar2")) {
	    xf86Info.kbdRate = atoi(argv[i + 1]) * 1000;
	    return 2;
	}
    }

#endif

    return 0;
}

void xf86UseMsg()
{
#ifdef HAS_USL_VTS
    ErrorF("vtXX                   Use the specified VT number\n");
#endif
#if defined(__SOL8__) || !defined(i386)
    ErrorF("-dev <fb>              Framebuffer device\n");
    ErrorF("-ar1 <float>           Set autorepeat initiate time (sec)\n");
    ErrorF("                       (if not using XKB)\n");
    ErrorF("-ar2 <float>           Set autorepeat interval time (sec)\n");
    ErrorF("                       (if not using XKB)\n");
#endif
    ErrorF("-keeptty               Don't detach controlling tty\n");
    ErrorF("                       (for debugging only)\n");
}