summaryrefslogtreecommitdiff
path: root/hw/xfree86/os-support/linux/lnx_init.c
blob: 0bd33aea78cc16314b53bda00f3f1711488d6d1b (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
/* $XdotOrg: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c,v 1.5 2005/04/20 12:25:35 daniels Exp $ */
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c,v 3.14 2001/10/31 22:50:30 tsi Exp $ */
/*
 * Copyright 1992 by Orest Zborowski <obz@Kodak.com>
 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
 *
 * 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 Orest Zborowski and David Wexelblat
 * not be used in advertising or publicity pertaining to distribution of
 * the software without specific, written prior permission.  Orest Zborowski
 * and David Wexelblat make no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * OREST ZBOROWSKI AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD
 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT 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.
 *
 */
/* $XConsortium: lnx_init.c /main/7 1996/10/23 18:46:30 kaleb $ */

#include <X11/X.h>
#include <X11/Xmd.h>

#include "compiler.h"

#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#include "lnx.h"

#include <sys/stat.h>

#ifdef USE_DEV_FB
extern char *getenv(const char *);
#include <linux/fb.h>
char *fb_dev_name;
#endif

static Bool KeepTty = FALSE;
static int VTnum = -1;
static int activeVT = -1;

static int vtPermSave[4];
static char vtname[11];

static int
saveVtPerms(void)
{
    /* We need to use stat to get permissions. */
    struct stat svtp;

    /* Do them numerically ordered, hard coded tty0 first. */
    if (stat("/dev/tty0", &svtp) != 0)
	return 0;
    vtPermSave[0] = (int)svtp.st_uid;
    vtPermSave[1] = (int)svtp.st_gid;

    /* Now check the console we are dealing with. */
    if (stat(vtname, &svtp) != 0)
	return 0;
    vtPermSave[2] = (int)svtp.st_uid;
    vtPermSave[3] = (int)svtp.st_gid;

    return 1;
}

static void
restoreVtPerms(void)
{
    /* Set the terminal permissions back to before we started. */
    chown("/dev/tty0", vtPermSave[0], vtPermSave[1]);
    chown(vtname, vtPermSave[2], vtPermSave[3]);
}

void
xf86OpenConsole(void)
{
    int i, fd = -1;
    struct vt_mode VT;
    struct vt_stat vts;
    MessageType from = X_PROBED;
#ifdef USE_DEV_FB
    struct fb_var_screeninfo var;
    int fbfd;
#endif
    char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
    char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };

    if (serverGeneration == 1) {

	/* when KeepTty check if we're run with euid==0 */
	if (KeepTty && geteuid() != 0) 
	    FatalError("xf86OpenConsole:"
		       " Server must be suid root for option \"KeepTTY\"\n");

	/*
	 * setup the virtual terminal manager
	 */
	if (VTnum != -1) {
	    xf86Info.vtno = VTnum;
	    from = X_CMDLINE;
	} else {

	    i=0;
	    while (tty0[i] != NULL) {
		if ((fd = open(tty0[i],O_WRONLY,0)) >= 0)
		  break;
		i++;
	    }
	    
	    if (fd < 0)
		FatalError(
		    "xf86OpenConsole: Cannot open /dev/tty0 (%s)\n",
		    strerror(errno));

	    if ((ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0) ||
		(xf86Info.vtno == -1)) {
		FatalError("xf86OpenConsole: Cannot find a free VT: %s\n",
			   strerror(errno));
	    }
	    close(fd);
	}

#ifdef USE_DEV_FB
	fb_dev_name=getenv("FRAMEBUFFER");
	if (!fb_dev_name)
	    fb_dev_name="/dev/fb0current";
	
	if ((fbfd = open(fb_dev_name, O_RDONLY)) < 0)
	    FatalError("xf86OpenConsole: Cannot open %s (%s)\n",
		       fb_dev_name, strerror(errno));

	if (ioctl(fbfd, FBIOGET_VSCREENINFO, &var) < 0)
	    FatalError("xf86OpenConsole: Unable to get screen info %s\n",
		       strerror(errno));
#endif
	xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno);

	if (!KeepTty) {
	    pid_t ppid = getppid();
	    pid_t ppgid;
	    ppgid = getpgid(ppid);

	    /*
	     * change to parent process group that pgid != pid so
	     * that setsid() doesn't fail and we become process
	     * group leader
	     */
	    if (setpgid(0,ppgid) < 0)
		xf86Msg(X_WARNING, "xf86OpenConsole: setpgid failed: %s\n",
			strerror(errno));

	    /* become process group leader */
	    if ((setsid() < 0))
		xf86Msg(X_WARNING, "xf86OpenConsole: setsid failed: %s\n",
			strerror(errno));
	}

        i=0;
        while (vcs[i] != NULL) {
            sprintf(vtname, vcs[i], xf86Info.vtno); /* /dev/tty1-64 */
     	    if ((xf86Info.consoleFd = open(vtname, O_RDWR|O_NDELAY, 0)) >= 0)
		break;
            i++;
        }

	if (xf86Info.consoleFd < 0)
	    FatalError("xf86OpenConsole: Cannot open virtual console"
		       " %d (%s)\n", xf86Info.vtno, strerror(errno));

	/*
	 * Grab the vt ownership before we overwrite it.
	 * Hard coded /dev/tty0 into this function as well for below.
	 */
	if (!saveVtPerms())
	    xf86Msg(X_WARNING,
		    "xf86OpenConsole: Could not save ownership of VT\n");

	/* change ownership of the vt */
	if (chown(vtname, getuid(), getgid()) < 0)
	    xf86Msg(X_WARNING,"xf86OpenConsole: chown %s failed: %s\n",
		    vtname, strerror(errno));

	/*
	 * the current VT device we're running on is not "console", we want
	 * to grab all consoles too
	 *
	 * Why is this needed??
	 */
	if (chown("/dev/tty0", getuid(), getgid()) < 0)
	    xf86Msg(X_WARNING,"xf86OpenConsole: chown /dev/tty0 failed: %s\n",
		    strerror(errno));

	/*
	 * Linux doesn't switch to an active vt after the last close of a vt,
	 * so we do this ourselves by remembering which is active now.
	 */
	if (ioctl(xf86Info.consoleFd, VT_GETSTATE, &vts) < 0)
	    xf86Msg(X_WARNING,"xf86OpenConsole: VT_GETSTATE failed: %s\n",
		    strerror(errno));
	else
	    activeVT = vts.v_active;

#if 0
	if (!KeepTty) {
	    /*
	     * Detach from the controlling tty to avoid char loss
	     */
	    if ((i = open("/dev/tty",O_RDWR)) >= 0) {
		ioctl(i, TIOCNOTTY, 0);
		close(i);
	    }
	}
#endif
	    
#if defined(DO_OS_FONTRESTORE)
	lnx_savefont();
#endif
	/*
	 * now get the VT
	 */
	if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) < 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed: %s\n",
		    strerror(errno));

	if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) < 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed: %s\n",
		    strerror(errno));

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

	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: %s\n",
		strerror(errno));
	
	if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0)
	    FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed %s\n",
		       strerror(errno));

	/* we really should have a InitOSInputDevices() function instead
	 * of Init?$#*&Device(). So I just place it here */
	
#ifdef USE_DEV_FB
	/* copy info to new console */
	var.yoffset=0;
	var.xoffset=0;
	if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &var))
	    FatalError("Unable to set screen info\n");
	close(fbfd);
#endif
    } else { 	/* serverGeneration != 1 */
	/*
	 * now get the VT
	 */
	if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) < 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed %s\n",
		    strerror(errno));

	if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) < 0)
	    xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed %s\n",
		    strerror(errno));
    }
    return;
}

void
xf86CloseConsole()
{
    struct vt_mode   VT;
#if defined(DO_OS_FONTRESTORE)
    struct vt_stat vts;
    int vtno = -1;

    if (ioctl(xf86Info.consoleFd, VT_GETSTATE, &vts) < 0)
	xf86Msg(X_WARNING, "xf86CloseConsole: VT_GETSTATE failed: %s\n",
		strerror(errno));
    else
	vtno = vts.v_active;
#endif

    /* Back to text mode ... */
    if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT) < 0)
	xf86Msg(X_WARNING, "xf86CloseConsole: KDSETMODE failed: %s\n",
		strerror(errno));
	
    if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0) 
	xf86Msg(X_WARNING, "xf86CloseConsole: VT_GETMODE failed: %s\n",
		strerror(errno));
    else {
	/* set dflt vt handling */
	VT.mode = VT_AUTO;
	if (ioctl(xf86Info.consoleFd, VT_SETMODE, &VT) < 0) 
	    xf86Msg(X_WARNING, "xf86CloseConsole: VT_SETMODE failed: %s\n",
		    strerror(errno));
    }

    /*
     * Perform a switch back to the active VT when we were started
     */
    if (activeVT >= 0) {
	if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, activeVT) < 0)
	    xf86Msg(X_WARNING, "xf86CloseConsole: VT_ACTIVATE failed: %s\n",
		    strerror(errno));
	activeVT = -1;
    }

#if defined(DO_OS_FONTRESTORE)
    if (xf86Info.vtno == vtno)	/* check if we are active */
	lnx_restorefont();
    lnx_freefontdata();
#endif
    close(xf86Info.consoleFd);	/* make the vt-manager happy */

    restoreVtPerms();		/* restore the permissions */

    return;
}

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

void
xf86UseMsg()
{
	ErrorF("vtXX                   use the specified VT number\n");
	ErrorF("-keeptty               ");
	ErrorF("don't detach controlling tty (for debugging only)\n");
	return;
}