[PATCH] linux: Clean up filenames used in Linux VT init.

Cody Maloney cmaloney at theoreticalchaos.com
Mon May 16 14:42:16 PDT 2011


On Mon, May 16, 2011 at 5:27 AM, Timothy Meade <zt.tmzt at gmail.com> wrote:
>
> On May 14, 2011 3:39 PM, "Cody Maloney" <cmaloney at theoreticalchaos.com>
> wrote:
>>
>> According to the comment in the Linux kernel source file
>> drivers/tty/vt/vt_ioctl.c /dev/tty0 points to /dev/console,
>> so we should just go to /dev/console, and virtual terminals
>> are always labeled /dev/ttyN where N is 1..MAX_NR_CONSOLES,
>> so only check /dev/ttyN.
>>
>> Signed-off-by: Cody Maloney <cmaloney at theoreticalchaos.com>
>> ---
>>  hw/xfree86/os-support/linux/lnx_init.c |   39
>> ++++++-------------------------
>>  1 files changed, 8 insertions(+), 31 deletions(-)
>>
>> diff --git a/hw/xfree86/os-support/linux/lnx_init.c
>> b/hw/xfree86/os-support/linux/lnx_init.c
>> index 77dfb2f..04518f3 100644
>> --- a/hw/xfree86/os-support/linux/lnx_init.c
>> +++ b/hw/xfree86/os-support/linux/lnx_init.c
>> @@ -76,17 +76,17 @@ switch_to(int vt, const char *from)
>>  void
>>  xf86OpenConsole(void)
>>  {
>> -    int i, fd = -1, ret;
>> +    int fd = -1, ret;
>>     struct vt_mode VT;
>>     struct vt_stat vts;
>>     MessageType from = X_PROBED;
>> -    char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
>> -    char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
>> +    const char *console_fname = "/dev/console";
>> +    char *vcs = "/dev/tty%d";
>>
>>     if (serverGeneration == 1) {
>>
>>        /* when KeepTty check if we're run with euid==0 */
>> -       if (KeepTty && geteuid() != 0)
>> +       if (KeepTty && geteuid() != 0)
>>            FatalError("xf86OpenConsole:"
>>                       " Server must be suid root for option
>> \"KeepTTY\"\n");
>>
>> @@ -96,17 +96,11 @@ xf86OpenConsole(void)
>>        if (xf86Info.vtno != -1) {
>>            from = X_CMDLINE;
>>        } else {
>> +           fd = open(console_fname, O_WRONLY, 0);
>>
>> -           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",
>> +                   "xf86OpenConsole: Cannot open /dev/console to use VT
>> ioctls (%s)\n",
>>                    strerror(errno));
>>
>>             if (ShareVTs)
>> @@ -149,13 +143,8 @@ xf86OpenConsole(void)
>>                        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++;
>> -        }
>> +        sprintf(vtname, vcs, xf86Info.vtno); /* /dev/tty1-64 */
>> +        xf86Info.consoleFd = open(vtname, O_RDWR|O_NDELAY, 0);
>>
>>        if (xf86Info.consoleFd < 0)
>>            FatalError("xf86OpenConsole: Cannot open virtual console"
>> @@ -172,18 +161,6 @@ xf86OpenConsole(void)
>>        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) {
>> -               SYSCALL(ioctl(i, TIOCNOTTY, 0));
>> -               close(i);
>> -           }
>> -       }
>> -#endif
>> -
>>         if (!ShareVTs)
>>         {
>>             struct termios nTty;
>> --
>> 1.7.5.1
>>
>> _______________________________________________
>> xorg-devel at lists.x.org: X.Org development
>> Archives: http://lists.x.org/archives/xorg-devel
>> Info: http://lists.x.org/mailman/listinfo/xorg-devel
>
> This may be different when operating under mdev, devfs, android init,
> devtmpfs (devfs replacement, not sure that's what it's called), especially
> /dev/vc/*
>
> Timothy Meade (tmzt)

I just checked and in android, there is no /dev/tty0, /dev/vc/*, but
there is /dev/tty and /dev/console, so the old code would have missed
completely anyways. devtmpfs doesn't add any devices directly, relying
on what the kernel creates (I'll come back to what the kernel creates
later). devfs was killed off a while ago (replaced with udev), it
maintains /dev/tty[0-9]+, as well as /dev/vc/[0-9]+. All major distros
have switched off of it in new versions (versions which contain new
Xorg). Devtmpfs serves as a container for a minimal /dev for embedded
devices such as android, and only has devices documented in
doc/devices.txt added to it. For mdev, the directory name in /sys that
mdev finds the 'dev' file is located is taken to be the filename used
for the device name, this means it could never add subdirectories, so
/dev/vc/0 could never exist.

Grepping through the kernel source, the only place where
/dev/vc/[0-9]+ is mentioned is one line:
linux-2.6.38.6/drivers/tty/vt/vt.c:	
register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)

Looking at the source code, this may or may not be created depending
on whether or not the if exits early. /dev/tty0 is always created. If
you look at the updated patch I sent
(http://lists.freedesktop.org/archives/xorg-devel/2011-May/022225.html),
I use /dev/tty0 instead of /dev/console because, as Julien pointed
out, /dev/console might not point to a virtual terminal, and as such
might not be valid for the virtual terminal ioctl calls.

Cody


More information about the xorg-devel mailing list