Make sure logging handlers are closed when removed
[autotest-zwu.git] / conmux / console
blobf031e5b2c594ce1eb88ad31355a11fc9dfdcc6b2
1 #!/usr/bin/perl
3 # console <host>/<machine> -- interactive client interface
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.
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 $| = 1;
17 our $P = 'console';
19 use FindBin;
20 use POSIX qw(errno_h BUFSIZ);
21 use IO::Socket;
22 use Getopt::Long qw(:config no_auto_abbrev);
24 my $CONMUX = $FindBin::Bin;
25 my $CONMUX = $ENV{'CONMUX_ROOT'} if ($ENV{'CONMUX_ROOT'});
27 # Find our internal libraries.
28 use lib $FindBin::Bin;
29 use lib "$FindBin::Bin/../lib/";
30 use lib "$FindBin::Bin/lib/";
31 use Conmux;
33 # Basic terminal handling.
34 sub termRaw {
35 $termSettings = `stty -g`;
36 system "stty raw -echo opost onlret";
38 sub termRestore {
39 system "stty $termSettings";
42 my $bot;
43 my $list;
44 my $status;
45 GetOptions(
46 'b|bot=s' => \$bot,
47 'l|list' => \$list,
48 's|status' => \$status,
50 sub usage {
51 warn "Usage: $P <service>\n";
52 warn " $P <registry>/<service>\n";
53 warn " $P <host>:<port>\n";
54 warn " $P --status <service>\n";
55 die " $P --list [<registry>]\n";
58 my $id;
59 if ($bot) {
60 $id = 'bot:' . $bot;
61 } else {
62 $id = 'user:' . $ENV{'LOGNAME'};
66 # MODE: registry list.
68 if ($list) {
69 if ($#ARGV == -1) {
70 print Conmux::Registry::list('-');
72 } elsif ($#ARGV == 0) {
73 print Conmux::Registry::list($ARGV[0]);
75 } else {
76 usage();
78 exit 0
82 # COMMAND: payload status command
84 if ($status) {
85 usage() if ($#ARGV != 0);
87 my $sock;
88 eval {
89 $sock = Conmux::connect($ARGV[0]);
91 if ($@) {
92 print "unavailable\n";
93 exit 0
95 my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id,
96 'to' => 'console', 'hide' => 1 });
97 if ($r{'status'} ne 'OK') {
98 print "unavailable\n";
100 } elsif ($r{'state'}) {
101 print "$r{'state'}\n";
103 } else {
104 print "unknown\n";
106 exit 0;
111 # COMMAND: general payload connect.
113 if ($#ARGV != 0) {
114 usage();
117 # Connect to the host/port specified on the command line,
118 # or localhost:23
119 my $sock = Conmux::connect($ARGV[0]);
121 my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id, 'to' => 'console' });
122 die "$P: $ARGV[0]: connect failed - $r{'status'}\n" if ($r{'status'} ne 'OK');
124 print "Connected to $r{'title'} (~\$quit to exit)\n";
125 #display message of the day passed by the server e.g. who is already connected
126 print "$r{'motd'}";
128 my $rin = $win = $ein = '';
129 vec($rin, fileno(STDIN), 1) = 1;
130 vec($rin, fileno($sock), 1) = 1;
131 my $ein = $rin | $win;
133 # We want to buffer output to the terminal. This prevents the program
134 # from blocking if the user hits CTRL-S for example.
135 # XXX ^^^
137 termRaw();
139 # Loop waiting for input from the user, or from the server.
140 while (1) {
141 $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
142 if ($nfound < 0) {
143 if ($! == EINTR || $! == EAGAIN) {
144 next;
146 die "$P: select failed - $!\n";
149 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;
156 die "$P: STDIN: read failed - $!\n";
158 print STDERR "Connection Closed (client)\n";
159 last;
161 print $sock $data;
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;
170 die "$P: server: read failed - $!\n";
172 print STDERR "Connection Closed (server)\n";
173 last;
175 print $data;
179 termRestore();