Skip to content

Instantly share code, notes, and snippets.

@petr999
Created October 28, 2010 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petr999/651700 to your computer and use it in GitHub Desktop.
Save petr999/651700 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
package main;
use strict;
use warnings;
use POSIX qw/setsid/;
use Readonly;
my $cmd = @ARGV > 0 ? join ' ', @ARGV : 'wmaker';
my Readonly $clientsDomainSuffix = 'localdomain';
my Readonly @clientsHostnames = qw/host00 host01/;
my Readonly $xauth = '/usr/local/bin/xauth';
my Readonly $ssh= 'prefix/bin/ssh user@host';
my Readonly $X= 'prefix/bin/X';
srand( time );
sub xKeys{
my $cmdServer = 'prefix/bin/xauth -f $HOME/.serverauth';
foreach my $jail ( @clientsHostnames ){
my $cookie = unpack "H*", join( "", map {chr int rand 256} (0..15) );
my $cmdParam = "add $jail$clientsDomainSuffix/unix:0 . $cookie\n";
my $cmdClient = "$xauth -f \$HOME/".".$jail-authority";
`$cmdServer $cmdParam`;
my $errCode = $? >> 8; die "$errCode: $!" if $errCode;
`$cmdClient $cmdParam`;
$errCode = $? >> 8; die "$errCode: $!" if $errCode;
}
}
sub xClient{
system "$ssh $cmd";
}
sub sig_handle{
my( $sig, $pid ) = @_;
return sub{
$sig = ( $sig eq 'INT' ) ? 'TERM' : $sig ;
kill $sig, $pid ;
};
}
sub xServer{
my $xPid = open my $xFh, "$X vt9 -keeptty -retro -logverbose 9 -audit 4 -auth \$HOME/.serverauth |";
die "open X: $xPid $!" unless defined( $xPid ) and $xPid > 0;
map{ my $signal = $_; $SIG{ $signal } = sig_handle( $signal, $xPid );
} qw/INT HUP TERM/;
while(<$xFh>){ print; }
}
&xKeys;
my $ppid = $$;
my $pid = fork;
if( defined $pid ){
if( $pid > 0 ){
xServer();
} elsif( $pid == 0 ){
xClient();
kill TERM => $ppid;
}
} else {
die "Can not fork: $pid ($!)";
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment