ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/shell.pl
Revision: 1.23
Committed: Sat Dec 8 21:01:17 2007 UTC (16 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_91, rel-4_748, rel-4_8, rel-4_9, rel-4_741, rel-4_743, rel-4_742, rel-4_744, rel-4_747, rel-4_74, rel-4_71, rel-4_72, rel-4_73, rel-4_802, rel-4_803, rel-4_801, rel-4_804, rel-4_479, rel-4_50, rel-4_51, rel-4_4, rel-4_45, rel-4_745, rel-4_901, rel-4_49, rel-4_48, rel-4_746, rel-4_47, rel-4_46, rel-4_7, rel-4_911, rel-4_912, rel-4_32, rel-4_33, rel-4_34, rel-4_35, rel-4_36, rel-4_37
Changes since 1.22: +4 -3 lines
Log Message:
convert from Event to EV

File Contents

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