views:

47

answers:

1

Or is it possible at all that some process or something else could block a virtual terminal? Or what could be a reason that an application hangs when trying to access the VT1?

It seems, while that is happening, it is hanging in the function ioctl. Esp., this is the code which fails:

int vtno = 1;
const char* vtname = "/dev/tty1";

int fd = open(vtname, O_RDWR|O_NDELAY, 0);

if (ioctl(fd, VT_ACTIVATE, vtno) < 0)
  printf("VT_ACTIVATE failed: %s\n", strerror(errno));

if (ioctl(fd, VT_WAITACTIVE, vtno) < 0)
  printf("VT_WAITACTIVE failed: %s\n", strerror(errno));

It hangs in the second ioctl. When I interrupt it, I get this message:

VT_WAITACTIVE failed: Interrupted system call

Also, while it is waiting there, if I do a chvt 1 from another terminal, that also hangs.

A: 

I found out the problem. Linus Torvalds has described it firstly in a similar situation. It is actually a Race condition.

The problem is as follows: If it happens that right after the first ioctl(fd, VT_ACTIVE, 1) succeeds, i.e. the system switched to the first VT, another separate process switches to another VT, the second ioctl will fail (or just wait forever, i.e. hang) because it waits that we switch to VT1 which we are not going to do anymore (unless the user is doing it).


Edit: Well, that explains it to one part. It doesn't explain why the chvt 1 is hanging also.

Albert