Monday, January 26, 2009

mrxvt hanging or not logging in with XWin ... or Can't read lock file /tmp/.X0-lock

I thought we might get some comments like Jim's. And I'm not sure of what to say about the fonts, but as far as the "hanging" goes, it may be that the mrxvt command is getting executed too soon after the XWin command, and they race to see who starts first, if mrxvt wins the race, then it hangs or does not login.

This compells me to post a short perl script I've written avoid the race:
#!/usr/bin/perl -w

use strict;

# start X
system("XWin -nolock -multiwindow -clipboard -silent-dup-error &");
if ($? == -1) {
print "failed to execute: $!\n";
} elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
} else {
printf "child exited with value %d\n", $? >> 8;
}

# get the Windows process id (not the Cygwin pid)
my $process = "";
my $command = "";

while ($process !~ /\d+/) {
print "Waiting for WINPID ...\n";
$command = "ps -el";
open(PS, "$command 2>&1 |") or die "cant $command: $!";
my($i, $column);
my @line = split /\s+/, <PS>;
for $i ( 0 .. $#line) {
if ($line[$i] eq "WINPID") {
$column = (scalar(@line) - $i) * -1;
}
}
while (<PS>) {
next unless /XWin/;
@_ = split;
$process = $_[$column];
print "WINPID = $process\n";
}
close PS or die "bad $command: $! $?";
}

my $xWinSockets = 0;
while ($xWinSockets < 8) {
sleep 1;
$xWinSockets = getNumberOfXWinSockets();
print "NumberOfXWinSockets = $xWinSockets\n";
}
system("mrxvt -sb -sl 1000 -fn fixed -fg green -bg black -g 150x45 --highlightColor green -ls &");

sub getNumberOfXWinSockets {
my $number = 0;
$command = "netstat -nao -p TCP";
open(STATUS, "$command 2>&1 |") or die "cant $command: $!";
while (<STATUS>) { / ${process}.$/ and do { $number++; } }
close STATUS or die "bad command: $! $?";
return $number;
}
This script has helped me to prevent mrxvt from hanging or racing with XWin. Note the magic number 8 near the end. I hate magic numbers! If you know of the "correct" way to fix this whole issue, email me the details.

Make sure your DISPLAY environment variable is set ... maybe "export DISPLAY=:0". I usually put a line something like that in my .bash_profile.

Here is the typical output from the perl script above:
$ x.pl
child exited with value 0
Waiting for WINPID ...
Waiting for WINPID ...
Waiting for WINPID ...
Waiting for WINPID ...
WINPID = 2404
NumberOfXWinSockets = 0
NumberOfXWinSockets = 0
NumberOfXWinSockets = 0
NumberOfXWinSockets = 0
NumberOfXWinSockets = 0
NumberOfXWinSockets = 2
NumberOfXWinSockets = 2
NumberOfXWinSockets = 8
$
Notice how it was "Waiting for WINPID ..." several times ... not good. Anyone have any ideas about how to fix this?

1 comment:

Vernon Singleton said...

For anyone getting the "Fatal server error:
Can't read lock file /tmp/.X0-lock" ... I have updated the above script to use the "-nolock" option suggested everywhere on the net when you google the error.

Hope that helps.