~ubuntu-branches/ubuntu/precise/xorg-server/precise-updates

« back to all changes in this revision

Viewing changes to hw/xfree86/os-support/linux/lnx_init.c

Tags: 2:1.10.1-2
* Build xserver-xorg-core-udeb on hurd-i386.  Thanks, Samuel Thibault!
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <sys/stat.h>
40
40
 
41
41
static Bool KeepTty = FALSE;
42
 
static int VTnum = -1;
43
42
static Bool VTSwitch = TRUE;
44
43
static Bool ShareVTs = FALSE;
45
44
static int activeVT = -1;
46
45
 
47
 
static int vtPermSave[4];
48
46
static char vtname[11];
49
47
static struct termios tty_attr; /* tty state to restore */
50
48
static int tty_mode; /* kbd mode to restore */
51
49
 
52
 
static int
53
 
saveVtPerms(void)
54
 
{
55
 
    /* We need to use stat to get permissions. */
56
 
    struct stat svtp;
57
 
 
58
 
    /* Do them numerically ordered, hard coded tty0 first. */
59
 
    if (stat("/dev/tty0", &svtp) != 0)
60
 
        return 0;
61
 
    vtPermSave[0] = (int)svtp.st_uid;
62
 
    vtPermSave[1] = (int)svtp.st_gid;
63
 
 
64
 
    /* Now check the console we are dealing with. */
65
 
    if (stat(vtname, &svtp) != 0)
66
 
        return 0;
67
 
    vtPermSave[2] = (int)svtp.st_uid;
68
 
    vtPermSave[3] = (int)svtp.st_gid;
69
 
 
70
 
    return 1;
71
 
}
72
 
 
73
 
static void
74
 
restoreVtPerms(void)
75
 
{
76
 
    if (geteuid() == 0) {
77
 
         /* Set the terminal permissions back to before we started. */
78
 
         (void)chown("/dev/tty0", vtPermSave[0], vtPermSave[1]);
79
 
         (void)chown(vtname, vtPermSave[2], vtPermSave[3]);
80
 
    }
81
 
}
82
 
 
83
50
static void *console_handler;
84
51
 
85
52
static void
86
53
drain_console(int fd, void *closure)
87
54
{
88
 
    tcflush(fd, TCIOFLUSH);
 
55
    errno = 0;
 
56
    if (tcflush(fd, TCIOFLUSH) == -1 && errno == EIO) {
 
57
        xf86RemoveGeneralHandler(console_handler);
 
58
        console_handler = NULL;
 
59
    }
 
60
}
 
61
 
 
62
static void
 
63
switch_to(int vt, const char *from)
 
64
{
 
65
    if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, vt) < 0)
 
66
        FatalError("%s: VT_ACTIVATE failed: %s\n", from, strerror(errno));
 
67
 
 
68
    if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, vt) < 0)
 
69
        FatalError("%s: VT_WAITACTIVE failed: %s\n", from, strerror(errno));
89
70
}
90
71
 
91
72
void
108
89
        /*
109
90
         * setup the virtual terminal manager
110
91
         */
111
 
        if (VTnum != -1) {
112
 
            xf86Info.vtno = VTnum;
 
92
        if (xf86Info.vtno != -1) {
113
93
            from = X_CMDLINE;
114
94
        } else {
115
95
 
175
155
            FatalError("xf86OpenConsole: Cannot open virtual console"
176
156
                       " %d (%s)\n", xf86Info.vtno, strerror(errno));
177
157
 
178
 
        if (!ShareVTs)
179
 
        {
180
 
            /*
181
 
             * Grab the vt ownership before we overwrite it.
182
 
             * Hard coded /dev/tty0 into this function as well for below.
183
 
             */
184
 
            if (!saveVtPerms())
185
 
                xf86Msg(X_WARNING,
186
 
                        "xf86OpenConsole: Could not save ownership of VT\n");
187
 
 
188
 
            if (geteuid() == 0) {
189
 
                    /* change ownership of the vt */
190
 
                    if (chown(vtname, getuid(), getgid()) < 0)
191
 
                            xf86Msg(X_WARNING,"xf86OpenConsole: chown %s failed: %s\n",
192
 
                                    vtname, strerror(errno));
193
 
 
194
 
                    /*
195
 
                     * the current VT device we're running on is not
196
 
                     * "console", we want to grab all consoles too
197
 
                     *
198
 
                     * Why is this needed??
199
 
                     */
200
 
                    if (chown("/dev/tty0", getuid(), getgid()) < 0)
201
 
                            xf86Msg(X_WARNING,"xf86OpenConsole: chown /dev/tty0 failed: %s\n",
202
 
                                    strerror(errno));
203
 
            }
204
 
        }
205
 
 
206
158
        /*
207
159
         * Linux doesn't switch to an active vt after the last close of a vt,
208
160
         * so we do this ourselves by remembering which is active now.
232
184
            /*
233
185
             * now get the VT.  This _must_ succeed, or else fail completely.
234
186
             */
235
 
            if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) < 0)
236
 
                FatalError("xf86OpenConsole: VT_ACTIVATE failed: %s\n",
237
 
                           strerror(errno));
238
 
 
239
 
            if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) < 0)
240
 
                FatalError("xf86OpenConsole: VT_WAITACTIVE failed: %s\n",
241
 
                           strerror(errno));
 
187
            switch_to(xf86Info.vtno, "xf86OpenConsole");
242
188
 
243
189
            if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0)
244
190
                FatalError("xf86OpenConsole: VT_GETMODE failed %s\n",
277
223
            tcsetattr(xf86Info.consoleFd, TCSANOW, &nTty);
278
224
 
279
225
            /* need to keep the buffer clean, else the kernel gets angry */
280
 
            if (xf86Info.allowEmptyInput)
281
 
                console_handler = xf86AddGeneralHandler(xf86Info.consoleFd,
282
 
                                                        drain_console, NULL);
 
226
            xf86SetConsoleHandler(drain_console, NULL);
283
227
 
284
228
            /* we really should have a InitOSInputDevices() function instead
285
229
             * of Init?$#*&Device(). So I just place it here */
287
231
    } else {    /* serverGeneration != 1 */
288
232
        if (!ShareVTs && VTSwitch)
289
233
        {
290
 
            /*
291
 
             * now get the VT
292
 
             */
293
 
            if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) < 0)
294
 
                xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed %s\n",
295
 
                        strerror(errno));
296
 
 
297
 
            if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) < 0)
298
 
                xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed %s\n",
299
 
                        strerror(errno));
 
234
            /* now get the VT */
 
235
            switch_to(xf86Info.vtno, "xf86OpenConsole");
300
236
        }
301
237
    }
302
238
}
341
277
         * Perform a switch back to the active VT when we were started
342
278
         */
343
279
        if (activeVT >= 0) {
344
 
            if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, activeVT) < 0)
345
 
                xf86Msg(X_WARNING, "xf86CloseConsole: VT_ACTIVATE failed: %s\n",
346
 
                        strerror(errno));
347
 
            if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, activeVT) < 0)
348
 
                xf86Msg(X_WARNING,
349
 
                        "xf86CloseConsole: VT_WAITACTIVE failed: %s\n",
350
 
                        strerror(errno));
 
280
            switch_to(activeVT, "xf86CloseConsole");
351
281
            activeVT = -1;
352
282
        }
353
283
    }
354
284
    close(xf86Info.consoleFd);  /* make the vt-manager happy */
355
 
 
356
 
    restoreVtPerms();           /* restore the permissions */
357
285
}
358
286
 
359
287
int
380
308
        }
381
309
        if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
382
310
        {
383
 
                if (sscanf(argv[i], "vt%2d", &VTnum) == 0)
 
311
                if (sscanf(argv[i], "vt%2d", &xf86Info.vtno) == 0)
384
312
                {
385
313
                        UseMsg();
386
 
                        VTnum = -1;
 
314
                        xf86Info.vtno = -1;
387
315
                        return 0;
388
316
                }
389
317
                return 1;