$project_root = "XPROJECT_ROOT";
$command = $project_root . "/bin/xclock -update 1";
$xfindproxy = $project_root . "/bin/xfindproxy";
$xauth = $project_root . "/bin/xauth";
$proxymngr = "XPROXYMNGR";
select(STDERR); $| = 1;
select(STDOUT); $| = 1;
print "Content-type: text/plain\r\n\r\n";
sub handler { local($sig) = @_;
print "1\n";
print "Error: Caught a SIG$sig -- Oops!\n";
system "rm -f /tmp/*$$";
exit(0);
}
$SIG{'INT'} = 'handler';
$SIG{'QUIT'} = 'handler';
$SIG{'TERM'} = 'handler';
$SIG{'KILL'} = 'handler';
$SIG{'STOP'} = 'handler';
$SIG{'PIPE'} = 'handler';
sub parse_url {
local($input, *proto_ret, *display_ret, *param_ret) = @_;
($sub_url, $param_ret) = split(/;/, $input, 2);
($proto_ret, $display_ret) = split(/:/, $sub_url, 2);
}
sub parse_auth {
local($input, *name_ret, *key_ret) = @_;
if ($input) {
($param_name, $param_value) = split(/=/, $input, 2);
if ($param_name eq "auth") {
($name_ret, $key_ret) = split(/:/, $param_value, 2);
}
}
}
sub parse_lbx_param {
local($input, *lbx_ret, *lbx_auth_name_ret, *lbx_auth_key_ret) = @_;
($lbx_ret, $lbxparam) = split(/;/, $input, 2);
if ($lbx_ret eq "YES") {
&parse_auth($lbxparam, *lbx_auth_name_ret, *lbx_auth_key_ret);
}
}
sub setup_lbx_proxy {
local(*display, $auth_name, $auth_key) = @_;
if ($auth_name && $auth_key) {
$proxy_input = "/tmp/xlbxauth.$$";
open(PROXYINPUT, ">$proxy_input");
print PROXYINPUT "$auth_name\n$auth_key\n";
close(PROXYINPUT);
$findproxy_param = " -auth <$proxy_input";
} else {
$findproxy_param = "";
}
($host, $tmp) = split(/:/, $display);
($dpy, $screen) = split(/\./, $tmp);
$server = $host . ":" . $dpy;
open(PROXY, "$xfindproxy -manager $proxymngr -server $server -name LBX $findproxy_param|");
while (<PROXY>) {
chop;
($proxy_dpy, $proxy_port) = split(/:/, $_);
if ($proxy_dpy && $proxy_port) {
$display = $proxy_dpy . ":" . $proxy_port;
if ($screen) {
$display .= "." . $screen;
}
last;
}
}
close(PROXY);
if ($proxy_input) {
system "rm -f $proxy_input";
}
}
sub add_auth {
local($display, $auth_name, $auth_key) = @_;
system "$xauth add $display $auth_name $auth_key";
}
if ($ENV{'QUERY_STRING'})
{
$query = $ENV{'QUERY_STRING'};
} else {
$query = $ARGV[0];
}
if ($query)
{
$cleanup = "";
%params = split(/\?/, $query);
foreach $param (split(/\?/, $query)) {
($name, $value) = split(/=/, $param, 2);
if ($name eq "WIDTH") {
$width = $value;
} elsif ($name eq "HEIGHT") {
$height = $value;
} elsif ($name eq "UI") {
&parse_url($value, *proto, *display, *ui_param);
if ($proto eq 'x11') {
$xdisplay = $display;
} else {
}
&parse_auth($ui_param, *xui_auth_name, *xui_auth_key);
} elsif ($name eq "X-UI-LBX") {
&parse_lbx_param($value, *xui_lbx,
*xui_lbx_auth_name, *xui_lbx_auth_key);
}
}
$ENV{'XAUTHORITY'} = "/tmp/xauth.$$";
$cleanup = "rm -f $ENV{'XAUTHORITY'}";
if ($xdisplay) {
if ($xui_lbx eq "YES") {
&setup_lbx_proxy(*xdisplay, $xui_lbx_auth_name, $xui_lbx_auth_key);
}
if ($xui_auth_name && $xui_auth_key) {
&add_auth($xdisplay, $xui_auth_name, $xui_auth_key);
}
$command .= " -display $xdisplay";
$ENV{'DISPLAY'} = $xdisplay;
}
if ($width && $height) {
$command .= " -geometry ${width}x${height}";
}
$LOCK= "/tmp/lock.$$";
$LOG= "/tmp/log.$$";
$LOG2 = "/tmp/log2.$$";
system "(touch $LOCK; _s=true; $command >$LOG 2>&1 || _s=false; if \$_s; then rm $LOG; else rm $LOCK; fi; if [ -f $LOG2 ]; then rm $LOG2; fi; $cleanup) >/dev/null 2>&1 </dev/null &";
sleep(5);
if (open(LOCK, "<$LOCK")) {
close(LOCK);
system "rm $LOCK; mv $LOG $LOG2";
print "0\n";
} else {
print "1\n";
system "cat $LOG; rm $LOG";
}
} else {
print "This script requires to be given the proper RX arguments
to run successfully.
";
}