virt.virt_test_utils: run_autotest - 'tar' needs relative paths to strip the leading '/'
[autotest-zwu.git] / conmux / conmux-attach
blob695d9ff8a1414fa80dc04233b2df8891d7b86533
1 #!/usr/bin/perl
3 # conmux-attach -- attach commands to the console
5 # Attach stand alone commands to a console stream. You can specify
6 # which of the commands file descriptors are connected to which of the
7 # 'steams' in the multiplexor; the console input/output and the user
8 # output.
10 # (C) Copyright IBM Corp. 2004, 2005, 2006
11 # Author: Andy Whitcroft <andyw@uk.ibm.com>
13 # The Console Multiplexor is released under the GNU Public License V2
15 use FindBin;
16 use IO::Socket;
17 use Getopt::Long qw(:config no_auto_abbrev);
19 # Find our internal libraries.
20 use lib $FindBin::Bin;
21 use Conmux;
23 my $P = 'conmux-attach';
24 my ($in, $out, $err);
26 # Usage.
27 GetOptions(
28 'i|stdout=s' => \$in,
29 'o|stdin=s' => \$out,
30 'e|stderr=s' => \$err,
31 'b|bot=s' => \$bot,
32 ) or exit;
34 if ($in eq '' && $out eq '' && $err eq '') {
35 $in = 'client';
36 $out = 'client';
39 if ($#ARGV < 1) {
40 print STDERR "$P: <host:port> <program> ...\n";
41 exit 1;
43 my ($mux, $app) = @ARGV;
44 shift;
45 $app =~ s@.*/@@; $app =~ s@\s.*$@@;
46 $app = $bot if ($bot);
49 # Connect to the client channel.
51 sub conmux_connect {
52 my ($mux, $type) = @_;
54 if (!$cache{$mux, $type}) {
55 my $con = Conmux::connect($mux);
56 my %r = Conmux::sendCmd($con, 'CONNECT', {
57 'id' => 'bot:' . $app,
58 'to' => 'console',
59 'type' => $type } );
60 die "$P: $mux: connect failed - $r{'status'}\n"
61 if ($r{'status'} ne "OK");
63 $cache{$mux, $type} = $con;
66 $cache{$mux, $type};
69 if ($in) {
70 my $to = conmux_connect($mux, $in);
71 open(STDIN, "<&", $to) ||
72 die "$P: unable to hand off socket to stdin - $!\n";
74 if ($out) {
75 my $to = conmux_connect($mux, $out);
76 open(STDOUT, ">&", $to) ||
77 die "$P: unable to hand off socket to stdout - $!\n";
79 if ($err) {
80 my $to = conmux_connect($mux, $err);
81 open(STDERR, ">&", $to) ||
82 die "$P: unable to hand off socket to stderr - $!\n";
84 exec @ARGV;