blob: 244fef1b74d3a274b0e907e6d25b835d34fdf98e [file] [log] [blame]
apw3812c032006-12-07 21:01:14 +00001#!/usr/bin/perl
2#
3# console <host>/<machine> -- interactive client interface
4#
5# The main interactive client interace to conmux. Allows direct
6# interaction with the payload, as well as allowing break out
7# to the conmux menu to envoke defined commands; for example
8# hardreset.
9#
10# (C) Copyright IBM Corp. 2004, 2005, 2006
11# Author: Andy Whitcroft <andyw@uk.ibm.com>
12#
13# The Console Multiplexor is released under the GNU Public License V2
14#
15$| = 1;
16
17our $P = 'console';
18
apwac655042007-02-24 11:09:16 +000019use POSIX qw(errno_h BUFSIZ);
apw3812c032006-12-07 21:01:14 +000020use IO::Socket;
apw3812c032006-12-07 21:01:14 +000021use Getopt::Long qw(:config no_auto_abbrev);
22
23my $CONMUX = '/usr/local/conmux';
24my $CONMUX = $ENV{'CONMUX_ROOT'} if ($ENV{'CONMUX_ROOT'});
25
26# Find our internal libraries.
27###my $lib = $0; $lib =~ s@/[^/]+$@@;
28###push(@INC, $lib);
29###require Conmux;
30##use lib ".";
31##use Conmux;
32use lib "/usr/local/conmux/lib";
33use Conmux;
34
35# Basic terminal handling.
36sub termRaw {
37 $termSettings = `stty -g`;
38 system "stty raw -echo opost onlret";
39}
40sub termRestore {
41 system "stty $termSettings";
42}
43
44my $bot;
45my $list;
46my $status;
47GetOptions(
48 'b|bot=s' => \$bot,
49 'l|list' => \$list,
50 's|status' => \$status,
51);
52sub usage {
53 warn "Usage: $P <service>\n";
54 warn " $P <registry>/<service>\n";
55 warn " $P <host>:<port>\n";
56 warn " $P --status <service>\n";
57 die " $P --list [<registry>]\n";
58}
59
60my $id;
61if ($bot) {
62 $id = 'bot:' . $bot;
63} else {
64 $id = 'user:' . $ENV{'LOGNAME'};
65}
66
67#
68# MODE: registry list.
69#
70if ($list) {
71 if ($#ARGV == -1) {
72 print Conmux::Registry::list('-');
73
74 } elsif ($#ARGV == 0) {
75 print Conmux::Registry::list($ARGV[0]);
76
77 } else {
78 usage();
79 }
80 exit 0
81}
82
83#
84# COMMAND: payload status command
85#
86if ($status) {
87 usage() if ($#ARGV != 0);
88
89 my $sock;
90 eval {
91 $sock = Conmux::connect($ARGV[0]);
92 };
93 if ($@) {
94 print "unavailable\n";
95 exit 0
96 }
97 my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id,
98 'to' => 'console', 'hide' => 1 });
99 if ($r{'status'} ne 'OK') {
100 print "unavailable\n";
101
102 } elsif ($r{'state'}) {
103 print "$r{'state'}\n";
104
105 } else {
106 print "unknown\n";
107 }
108 exit 0;
109}
110
111
112#
113# COMMAND: general payload connect.
114#
115if ($#ARGV != 0) {
116 usage();
117}
118
apw3812c032006-12-07 21:01:14 +0000119# Connect to the host/port specified on the command line,
120# or localhost:23
121my $sock = Conmux::connect($ARGV[0]);
122
123my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id, 'to' => 'console' });
124die "$P: $ARGV[0]: connect failed - $r{'status'}\n" if ($r{'status'} ne 'OK');
125
126print "Connected to $r{'title'} (~\$quit to exit)\n";
127
apwac655042007-02-24 11:09:16 +0000128my $rin = $win = $ein = '';
129vec($rin, fileno(STDIN), 1) = 1;
130vec($rin, fileno($sock), 1) = 1;
131my $ein = $rin | $win;
132
apw3812c032006-12-07 21:01:14 +0000133# We want to buffer output to the terminal. This prevents the program
134# from blocking if the user hits CTRL-S for example.
apwac655042007-02-24 11:09:16 +0000135# XXX ^^^
apw3812c032006-12-07 21:01:14 +0000136
137termRaw();
138
apwac655042007-02-24 11:09:16 +0000139# Loop waiting for input from the user, or from the server.
140while (1) {
141 $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
142 if ($nfound < 0) {
143 if ($! == EINTR || $! == EAGAIN) {
144 next;
145 }
146 die "$P: select failed - $!\n";
147 }
apw3812c032006-12-07 21:01:14 +0000148
apwac655042007-02-24 11:09:16 +0000149 if (vec($rout, fileno(STDIN), 1)) {
150 $len = sysread(STDIN, $data, 4096);
151 if ($len <= 0) {
152 if ($len < 0) {
153 if ($! == EINTR || $! == EAGAIN) {
154 next;
155 }
156 die "$P: STDIN: read failed - $!\n";
157 }
158 print STDERR "Connection Closed (client)\n";
159 last;
160 }
161 print $sock $data;
162 }
163 if (vec($rout, fileno($sock), 1)) {
164 $len = sysread($sock, $data, 4096);
165 if ($len <= 0) {
166 if ($len < 0) {
167 if ($! == EINTR || $! == EAGAIN) {
168 next;
169 }
170 die "$P: server: read failed - $!\n";
171 }
172 print STDERR "Connection Closed (server)\n";
173 last;
174 }
175 print $data;
apw3812c032006-12-07 21:01:14 +0000176 }
177}
178
apwac655042007-02-24 11:09:16 +0000179termRestore();