| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# conmux-attach -- attach commands to the console |
| 4 |
# |
| 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. |
| 9 |
# |
| 10 |
# (C) Copyright IBM Corp. 2004, 2005, 2006 |
| 11 |
# Author: Andy Whitcroft <[email protected]> |
| 12 |
# |
| 13 |
# The Console Multiplexor is released under the GNU Public License V2 |
| 14 |
# |
| 15 |
use FindBin; |
| 16 |
use IO::Socket; |
| 17 |
use Getopt::Long qw(:config no_auto_abbrev); |
| 18 |
|
| 19 |
# Find our internal libraries. |
| 20 |
use lib $FindBin::Bin; |
| 21 |
use Conmux; |
| 22 |
|
| 23 |
my $P = 'conmux-attach'; |
| 24 |
my ($in, $out, $err); |
| 25 |
|
| 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; |
| 33 |
|
| 34 |
if ($in eq '' && $out eq '' && $err eq '') { |
| 35 |
$in = 'client'; |
| 36 |
$out = 'client'; |
| 37 |
} |
| 38 |
|
| 39 |
if ($#ARGV < 1) { |
| 40 |
print STDERR "$P: <host:port> <program> ...\n"; |
| 41 |
exit 1; |
| 42 |
} |
| 43 |
my ($mux, $app) = @ARGV; |
| 44 |
shift; |
| 45 |
$app =~ s@.*/@@; $app =~ s@\s.*$@@; |
| 46 |
$app = $bot if ($bot); |
| 47 |
|
| 48 |
# |
| 49 |
# Connect to the client channel. |
| 50 |
# |
| 51 |
sub conmux_connect { |
| 52 |
my ($mux, $type) = @_; |
| 53 |
|
| 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"); |
| 62 |
|
| 63 |
$cache{$mux, $type} = $con; |
| 64 |
} |
| 65 |
|
| 66 |
$cache{$mux, $type}; |
| 67 |
} |
| 68 |
|
| 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"; |
| 73 |
} |
| 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"; |
| 78 |
} |
| 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"; |
| 83 |
} |
| 84 |
exec @ARGV; |