aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xfree86/os-support/solaris/sun_init.c
blob: ae9adee4bddbc5e43ea3d20c91ab88739af3c1d9 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * 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.
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

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

/*
 * Applications see VT number as consecutive integers starting from 1.
 * VT number			VT device
 * -------------------------------------------------------
 *     1             :          /dev/vt/0 (Alt + Ctrl + F1)
 *     2             :          /dev/vt/2 (Alt + Ctrl + F2)
 *     3             :          /dev/vt/3 (Alt + Ctrl + F3)
 *  ... ...
 */
#define	CONSOLE_VTNO	1
#define	SOL_CONSOLE_DEV	"/dev/console"

static Bool KeepTty = FALSE;
static Bool Protect0 = FALSE;
static Bool UseConsole = FALSE;
#ifdef HAS_USL_VTS
static int VTnum = -1;
static int xf86StartVT = -1;
static int vtEnabled = 0;
extern void xf86VTAcquire(int);
extern void xf86VTRelease(int);
#endif

/* Device to open as xf86Info.consoleFd */
static char consoleDev[PATH_MAX] = "/dev/fb";

/* Set by -dev argument on CLI
   Used by hw/xfree86/common/xf86AutoConfig.c for VIS_GETIDENTIFIER */
_X_HIDDEN char xf86SolarisFbDev[PATH_MAX] = "/dev/fb";

void
xf86OpenConsole(void)
{
    int i;
#ifdef HAS_USL_VTS
    int fd;
    struct vt_mode VT;
    struct vt_stat vtinfo;
    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 (mmap(0, 0x1000, PROT_NONE,
			 MAP_FIXED | MAP_SHARED, fd, 0) == MAP_FAILED)
		    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 ((fd = open("/dev/vt/0",O_RDWR,0)) == -1)
	{
	    xf86ErrorF("xf86OpenConsole: Cannot open /dev/vt/0 (%s)\n",
		       strerror(errno));
	    vtEnabled = 0;
	}
	else
	{
	    if (ioctl(fd, VT_ENABLED, &vtEnabled) < 0)
	    {
		xf86ErrorF("xf86OpenConsole: VT_ENABLED failed (%s)\n",
			   strerror(errno));
		vtEnabled = 0;
	    }
	}
#endif /*  HAS_USL_VTS */

	if (UseConsole)
	{
	    strlcpy(consoleDev, SOL_CONSOLE_DEV, sizeof(consoleDev));

#ifdef HAS_USL_VTS
	    xf86Info.vtno = CONSOLE_VTNO;

	    if (vtEnabled == 0)
	    {
		xf86StartVT = 0;
	    }
	    else
	    {
		if (ioctl(fd, VT_GETSTATE, &vtinfo) < 0)
		    FatalError("xf86OpenConsole: Cannot determine current VT\n");
		xf86StartVT = vtinfo.v_active;
	    }
#endif /*  HAS_USL_VTS */
	    goto OPENCONSOLE;
	}

#ifdef HAS_USL_VTS
	if (vtEnabled == 0)
	{
	    /* VT not enabled - kernel too old or Sparc platforms
	       without visual_io support */
	    xf86Msg(from, "VT infrastructure is not available\n");

	    xf86StartVT = 0;
	    xf86Info.vtno = 0;
	    strlcpy(consoleDev, xf86SolarisFbDev, sizeof(consoleDev));
	    goto OPENCONSOLE;
	}

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

	xf86StartVT = vtinfo.v_active;

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

	xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno);
	snprintf(consoleDev, PATH_MAX, "/dev/vt/%d", xf86Info.vtno);

	if (fd != -1)
	{
	    close(fd);
	}

#endif /* HAS_USL_VTS */

OPENCONSOLE:
	if (!KeepTty)
	    setpgrp();

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

	/* Change ownership of the vt or console */
	chown(consoleDev, getuid(), getgid());

#ifdef HAS_USL_VTS
	if (vtEnabled)
	{
	    /*
	     * 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");

#ifdef VT_SET_CONSUSER /* added in snv_139 */
	    if (strcmp(display, "0") == 0)
		if (ioctl(xf86Info.consoleFd, VT_SET_CONSUSER) != 0)
		    xf86Msg(X_WARNING,
			"xf86OpenConsole: VT_SET_CONSUSER failed\n");
#endif

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

	    OsSignal(SIGUSR1, xf86VTAcquire);
	    OsSignal(SIGUSR2, xf86VTRelease);

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

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

	    if (ioctl(xf86Info.consoleFd, VT_SETDISPINFO, atoi(display)) < 0)
		xf86Msg(X_WARNING, "xf86OpenConsole: VT_SETDISPINFO failed\n");
	}
#endif

#ifdef KDSETMODE
	SYSCALL(i = ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS));
	if (i < 0) {
	    xf86Msg(X_WARNING,
		    "xf86OpenConsole: KDSETMODE KD_GRAPHICS failed on %s (%s)\n",
		    consoleDev, strerror(errno));
	}
#endif
    }
    else /* serverGeneration != 1 */
    {
#ifdef HAS_USL_VTS
	if (vtEnabled)
	{
	    /*
	     * 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");

#ifdef VT_SET_CONSUSER /* added in snv_139 */
	    if (strcmp(display, "0") == 0)
		if (ioctl(xf86Info.consoleFd, VT_SET_CONSUSER) != 0)
		    xf86Msg(X_WARNING,
			"xf86OpenConsole: VT_SET_CONSUSER failed\n");
#endif

	    /*
	     * 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(__i386__) && !defined(__i386) && !defined(__x86)

    if (!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(xf86SolarisFbDev, 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 {
		    memset(fbdata, 0, fbattr.fbtype.fb_size);
		    munmap(fbdata, fbattr.fbtype.fb_size);
		}
	    }

	    close(fd);
	}
    }

#endif

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

#ifdef HAS_USL_VTS
    if (vtEnabled)
    {
	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);
}

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;
    }

    /*
     * Use /dev/console as the console device.
     */
    if (!strcmp(argv[i], "-C"))
    {
	UseConsole = TRUE;
	return 1;
    }

#ifdef HAS_USL_VTS

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

	return 1;
    }

#endif /* HAS_USL_VTS */

    if ((i + 1) < argc) {
	if (!strcmp(argv[i], "-dev")) {
	    strlcpy(xf86SolarisFbDev, argv[i+1], sizeof(xf86SolarisFbDev));
	    return 2;
	}
    }

    return 0;
}

void xf86UseMsg(void)
{
#ifdef HAS_USL_VTS
    ErrorF("vtX                    Use the specified VT number\n");
#endif
    ErrorF("-dev <fb>              Framebuffer device\n");
    ErrorF("-keeptty               Don't detach controlling tty\n");
    ErrorF("                       (for debugging only)\n");
    ErrorF("-C                     Use /dev/console as the console device\n");
}