ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/shell.pl
Revision: 1.22
Committed: Thu Feb 12 22:18:27 2004 UTC (20 years, 5 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_5, rel-4_22, rel-4_21, rel-4_0, rel-4_3, rel-3_41, rel-4_13, rel-4_11, rel-3_55, rel-3_51, rel-4_01, rel-4_03, rel-4_02, rel-2_0, rel-2_1, rel-1_1, rel-1_0, rel-1_9, rel-1_2, rel-3_6, rel-3_62, rel-3_63, rel-3_61, rel-1_5, rel-1_4, rel-1_7, rel-1_6, rel-3_4, rel-3_1, rel-3_5, rel-3_3, rel-3_2, rel-3_0, rel-3_01, rel-3_11, rel-1_31, rel-4_1, rel-4_2, stack_sharing, rel-3_501, rel-4_31
Changes since 1.21: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 # a server command shell
2    
3     use Coro;
4     use Coro::Handle;
5     use Coro::Socket;
6     use Event;
7 root 1.8 use Time::HiRes 'time';
8    
9 root 1.10 use Text::Abbrev;
10    
11 root 1.8 my $last_ts = time;
12 root 1.1
13 root 1.10 my %complete;
14 root 1.20 my @commands = qw(quit squit refresh country restart block info print clrdiridx);
15 root 1.10
16     abbrev \%complete, @commands;
17    
18 root 1.1 sub shell {
19     my $fh = shift;
20    
21     while (defined (print $fh "cmd> "), $_ = <$fh>) {
22 root 1.2 s/\015?\012$//;
23 root 1.10 if (s/^(\S+)\s*// && (my $cmd = $complete{$1})) {
24     if ($cmd eq "quit") {
25 root 1.13 print $fh "bye bye.\n";#d#
26 root 1.10 last;
27     } elsif ($cmd eq "squit") {
28 root 1.13 print $fh "server quit.\n";#d#
29 root 1.10 Event::unloop;
30 root 1.11 last;
31 root 1.12 } elsif ($cmd eq "print") {
32 root 1.11 my @res = eval $_;
33     print $fh "eval: $@\n" if $@;
34     print $fh "RES = ", (join " : ", @res), "\n";
35 root 1.10 } elsif ($cmd eq "block") {
36     print "blocked '$_'\n";#d#
37 root 1.21 $conn::blocked{$_} = [time + $::BLOCKTIME, "blocked by operator"];
38 root 1.10 } elsif ($cmd eq "info") {
39     $::NOW = time+1e-6;
40     my @data;
41     for (values %conn::conn) {
42     for (values %$_) {
43     next unless $_;
44     my $rate = sprintf "%.1f", $_->{written} / ($::NOW - $_->{time});
45     push @data, "$_->{country}/$_->{remote_addr} $_->{written} $rate $_->{method} $_->{uri}\n";
46     }
47 root 1.1 }
48 root 1.10 print $fh sort @data;
49     print $fh scalar@data, " ($::conns) connections\n";#d#
50 root 1.19 print $fh "$::written bytes written in the last ", $::NOW - $last_ts, " seconds\n";
51 root 1.10 printf $fh "(%.1f bytes/s)\n", $::written / ($::NOW - $last_ts);
52     ($last_ts, $::written) = ($::NOW, 0);
53     } elsif ($cmd eq "refresh") {
54 root 1.11 do "config.pl";
55     print $fh "config.pl: $@\n" if $@;
56 root 1.16 %statdata_cache = ();
57 root 1.18 conn::read_blockuri;
58     conn::read_blockref;
59 root 1.20 } elsif ($cmd eq "clrdiridx") {
60     %statdata_cache = ();
61 pcg 1.22 delete $diridx{$_} for keys %diridx; # server error on %diridx = ();
62 root 1.10 } elsif ($cmd eq "restart") {
63     $::RESTART = 1;
64     unloop;
65     print $fh "restarting, cu!\n";
66     last;
67     } elsif ($cmd eq "country") {
68 root 1.17 print $fh netgeo::ip_request($_), "\n";
69 root 1.1 }
70     } else {
71 root 1.10 print $fh "try one of @commands\n";
72 root 1.1 }
73     }
74     }
75    
76     # bind to tcp port
77     if ($CMDSHELL_PORT) {
78     my $port = new Coro::Socket
79 root 1.14 #LocalAddr => "127.0.0.1",
80 root 1.1 LocalPort => $CMDSHELL_PORT,
81     ReuseAddr => 1,
82     Listen => 1,
83     or die "unable to bind cmdshell port: $!";
84 root 1.2
85     push @listen_sockets, $port;
86 root 1.1
87     async {
88 root 1.7 while () {
89     async \&shell, scalar $port->accept;
90     }
91 root 1.1 };
92     }
93    
94     # bind to stdin (debug)
95     if (1) {
96     my $tty;
97     open $tty, "+</dev/tty"
98     and async \&shell, unblock $tty;
99     }
100