Changeset 14430 for misc/runpugs
- Timestamp:
- 10/22/06 01:36:47 (2 years ago)
- Location:
- misc/runpugs
- Files:
-
- 3 added
- 11 modified
-
bin/termdispatcher2.pl (modified) (1 diff)
-
cgi-bin/runpugs (modified) (1 diff)
-
cgi-bin/runpugs2 (added)
-
data/runpugs2.html (added)
-
htdocs/favicon.ico (added)
-
htdocs/runpugs.css (modified) (3 diffs)
-
lib/Web/Terminal/Dispatcher.pm (modified) (2 diffs)
-
lib/Web/Terminal/Msg.pm (modified) (2 diffs)
-
lib/Web/Terminal/Server.pm (modified) (7 diffs)
-
lib/Web/Terminal/Server/Session.pm (modified) (10 diffs)
-
lib/Web/Terminal/Settings.pm (modified) (2 diffs)
-
lib/WebTerminal/Msg.pm (modified) (2 diffs)
-
lib/WebTerminal/Server.pm (modified) (4 diffs)
-
lib/WebTerminal/Server/Terminal.pm (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
misc/runpugs/bin/termdispatcher2.pl
r14366 r14430 14 14 my $cmd=$ARGV[1] || 'my $a='.$id.';say "Hello, $a";'; 15 15 #my $cmd='my $a='.$id.';say "Hello, $a";'; 16 my $prompt='pugs> '; 16 #my $prompt='pugs> '; 17 my $prompt='Prelude> '; 17 18 print "Sending msg $id: $cmd\n"; 18 19 my $ip="127.0.0.1"; 19 my $reply= &Web::Terminal::Dispatcher::send($id,$ip,$prompt.$cmd);20 (my $reply,my $histref) = &Web::Terminal::Dispatcher::send($id,$ip,$prompt.$cmd); 20 21 print $reply; 22 print "\nHistory\n"; 23 for my $entry (@{$histref}) { 24 print "\t$entry\n"; 25 } 21 26 -
misc/runpugs/cgi-bin/runpugs
r14403 r14430 102 102 #NO UNICODE! 103 103 # $cmd=tr/\0-\x{10ffff}/\0-\xff_/; 104 if ($cmd=~/clear/) {$cmd=''} elsif ($cmd!~/^\p{IsASCII}*$/) { 105 $cmd=''; 106 $reply = "Sorry, Unicode is not yet supported.\npugs> "; 104 if ($cmd=~/clear/) { 105 $cmd=''; 106 } elsif ($cmd!~/^\p{IsASCII}*$/) { 107 $cmd=''; 108 $reply = "Sorry, Unicode is not yet supported.\npugs> "; 107 109 } else { 110 if ($cmd=~/>\s+(\:*help)\b/) { 111 $cmd=~s/$1/:h/; 112 } elsif ($cmd=~/>\s+(\:*(quit|bye))\b/) { 113 $cmd=~s/$1/:q/; 114 } 108 115 $reply = &WebTerminal::Dispatcher::send($sessionid,$ip,$cmd); 109 116 $reply="\n".$reply; -
misc/runpugs/htdocs/runpugs.css
r14299 r14430 1 BODY {width:100%;margin-left:10%;margin-right:10%; margin-top: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 1 1pt; line-height: normal; background-color:#FFFFFF;}1 BODY {width:100%;margin-left:10%;margin-right:10%; margin-top: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 12pt; line-height: normal; background-color:#FFFFFF;} 2 2 H1 {font-size: 24pt; 3 3 font-family: Georgia, Serif, Times; … … 9 9 H2 { font-weight: bold; font-size: 12pt} 10 10 LABEL {font-weight: bold; } 11 EM {font-weight: bold;12 font-style: normal;13 text-decoration:none;}14 TT {color: #204a87;15 font-weight: bold;16 }17 11 .copyright {font-size: 9pt} 18 12 .warning { … … 22 16 #mainwindow { 23 17 width: 750px 18 } 19 #hist { 20 width: 750px; 21 border: solid 2px #204a87; 22 font-family: "Andale Mono", courier, fixed, monospace; 23 font-size: 10pt; 24 color: #204a87; 25 background-color: #f2f2f0; 26 padding-bottom: 5px; 24 27 } 25 28 #cmd { -
misc/runpugs/lib/Web/Terminal/Dispatcher.pm
r14366 r14430 28 28 my $port = $Web::Terminal::Settings::port; 29 29 my $cmd=''; 30 # we only consider the last line with a prompt30 # we only consider the last line with a prompt 31 31 my @cmdlines=split("\n",$cmds); 32 32 for my $cmdline (reverse @cmdlines) { … … 70 70 my $rid=$rmesgref->{id}; 71 71 my $reply=$rmesgref->{msg}; 72 my $histref=$rmesgref->{recent}; 72 73 $conn->disconnect(); 73 74 if ( "$id" ne "$rid" ) { 74 die "Terminal server returned wrong id: $rid, should be $id"; 75 # die "Terminal server returned wrong id: $rid, should be $id"; 76 return "Sorry, the pugs session died."; 75 77 } 76 return $reply;78 return ($reply,$histref); 77 79 } 78 80 } -
misc/runpugs/lib/Web/Terminal/Msg.pm
r14366 r14430 249 249 sub _new_client { 250 250 my $sock = $main_socket->accept(); 251 #returns undef on fail. Calling peerhost on undef makes it die. 252 ## so: 253 if (defined $sock) { 254 251 255 my $conn = bless { 252 256 'sock' => $sock, … … 262 266 $conn->disconnect(); 263 267 } 268 } else { 269 return undef; 270 } 264 271 } 265 272 -
misc/runpugs/lib/Web/Terminal/Server.pm
r14374 r14430 32 32 33 33 our %terminals=(); 34 #our %lastcalled=();35 34 our %sessions_per_ip=(); 36 35 … … 42 41 return "Sorry, I can't run any more sessions.\nPlease try again later."; 43 42 } else { 44 # $lastcalled{$id}=time;45 43 if ( exists $terminals{$id} ) { 44 print "$id exists\n"; 45 if ($terminals{$id}->{pid}) { 46 46 $terminals{$id}->{called}=time; 47 47 my $term = $terminals{$id}; 48 push @{$term->{recent}},$cmd; 49 if (scalar @{$term->{recent}}> $Web::Terminal::Settings::nrecent) { 50 shift @{$term->{recent}}; 51 } 48 52 my $lines = $term->write($cmd); 49 53 if ( $cmd eq $Web::Terminal::Settings::quit_command ) { 54 my $pid= $terminals{$id}->{pid}; 55 print "Quit $id ($pid)\n"; 50 56 delete $terminals{$id}; 57 if ($pid) { 58 kill 9,$pid; 59 } 51 60 $sessions_per_ip{$ip}--; 52 } 53 if ($lines=~/Aborted/s) { 61 # } 62 #if ($lines=~/Aborted/s) { 63 } elsif ($terminals{$id}->{error}==1) { 64 my $pid= $terminals{$id}->{pid}; 54 65 delete $terminals{$id}; 66 if ($pid) { 67 kill 9,$pid; 68 } 55 69 $sessions_per_ip{$ip}--; 56 70 } 57 71 return $lines; 72 } else { 73 return "pugs> "; 74 } 58 75 } else { 59 76 if ($sessions_per_ip{$ip}>$Web::Terminal::Settings::nsessions_ip) { 60 return "Sorry, you can't run more than 61 ${Web::Terminal::Settings::nsessions_ip} sessions from one IP 62 address.\n"; 77 print LOG2 "MAX nsessions for $ip reached\n"; 78 return "Sorry, you can't run more than ${Web::Terminal::Settings::nsessions_ip} sessions from one IP address.\n"; 63 79 } else { 80 print "New $id\n"; 64 81 $sessions_per_ip{$ip}++; 65 $terminals{$id} = new Web::Terminal::Server::Session(); 66 $terminals{$id}->{called}=time; 67 $terminals{$id}->{ip}=$ip; 68 my $term = $terminals{$id}; 69 return $term->{'init'}; 82 $terminals{$id} = new Web::Terminal::Server::Session(); 83 $terminals{$id}->{called}=time; 84 $terminals{$id}->{ip}=$ip; 85 my $term = $terminals{$id}; 86 my $init= $term->{'init'}; 87 my $error= $term->{'error'}; 88 if ($error==1) { # Failed to create a new terminal 89 $sessions_per_ip{$ip}--; 90 delete $terminals{$id}; 91 } 92 return $init; 70 93 } 71 94 } … … 79 102 if ( $len > 0 ) { 80 103 # ( my $id, my $ip, my $cmd ) = split( "\n", $msg, 3 ); 81 print "MSG:", $msg;82 104 my $mesgref=YAML::Syck::Load($msg); 83 my $id=$mesgref->{id};84 my $ip=$mesgref->{ip};85 my $cmd=$mesgref->{cmd};105 my $id=$mesgref->{id}; 106 my $ip=$mesgref->{ip}; 107 my $cmd=$mesgref->{cmd}; 86 108 # $cmd=pack("U0C*", unpack("C*",$cmd)); 87 # print "$id($ip): ",$cmd,"\n"; 109 my $pid=0; 110 if(exists $terminals{$id}) { 111 $pid=$terminals{$id}->{pid}; 112 } 113 my $nsess=scalar keys %terminals; 114 print scalar(localtime)," : $nsess : $ip : $id : $pid > ",$cmd,"\n"; 115 print LOG2 scalar(localtime)," : $nsess : $ip : $id : $pid > ",$cmd,"\n"; 88 116 my $lines = &termhandler( $id, $ip, $cmd ); 89 my $replyref=YAML::Syck::Dump({id=>$id,msg=>$lines}); 90 # $conn->send_now("$id\n$lines"); 117 my @history=('--- Recent commands ---'); 118 if (defined $terminals{$id}->{recent}) { 119 @history=@{$terminals{$id}->{recent}}; 120 } 121 my 122 $replyref=YAML::Syck::Dump({id=>$id,msg=>$lines,recent=>\@history}); 91 123 $conn->send_now($replyref); 92 124 … … 105 137 106 138 $SIG{USR1}=\&timeout; 107 #Proc::Daemon::Init; 108 139 if ( $Web::Terminal::Settings::daemon) { 140 Proc::Daemon::Init; 141 } 109 142 # fork/exec by the book: 110 143 use Errno qw(EAGAIN); … … 113 146 if ($pid=fork) { 114 147 #parent here 148 if (-e "/home/andara/apache/data/runpugs2.log") { 149 rename "/home/andara/apache/data/runpugs2.log","/home/andara/apache/data/runpugs2.log.".join("",localtime); 150 } 151 open(LOG2,">/home/andara/apache/data/runpugs2.log"); 115 152 Web::Terminal::Msg->new_server( $host, $port, \&login_proc ); 116 153 Web::Terminal::Msg->event_loop(); … … 122 159 kill 'USR1',getppid(); 123 160 } 161 system("killall $Web::Terminal::Settings::command"); 162 chdir $Web::Terminal::Settings::cgi_path; 163 exec("$Web::Terminal::Settings::perl ../bin/$Web::Terminal::Settings::server"); 164 # chdir "/home/andara/apache/cgi-bin/"; 165 # exec('/usr/bin/perl ../bin/termserv.pl'); 124 166 } elsif ($! == EAGAIN) { 125 167 sleep 5; … … 134 176 my $now=time(); 135 177 for my $id (keys %terminals) { 136 my $then=$terminals{$id} ;178 my $then=$terminals{$id}->{called}; 137 179 if ($now-$then>$Web::Terminal::Settings::timeout_idle) { 138 180 if(exists $terminals{$id}) { 181 my $pid= $terminals{$id}->{pid}; 139 182 my $ip=$terminals{$id}->{ip}; 140 183 $sessions_per_ip{$ip}--; 141 $terminals{$id}->write(':q'); 184 if ($pid) { 185 kill 9,$pid; 186 } 187 # $terminals{$id}->write(':q'); 142 188 delete $terminals{$id}; 189 print LOG2 "Cleaned up $ip : $id : $pid\n"; 143 190 } 144 191 } -
misc/runpugs/lib/Web/Terminal/Server/Session.pm
r14374 r14430 13 13 =cut 14 14 15 $SIG{CHLD}='IGNORE'; 15 16 ## Constructor 16 17 sub new { … … 20 21 # my $prompt = '/\>\ /'; 21 22 my $prompt= '/'.$Web::Terminal::Settings::init_pattern.'/'; 23 $self->{'error'}=0; 24 $self->{'recent'}=[]; 22 25 ## Start pugs 23 26 # $ENV{PUGS_SAFEMODE}=1;# Must be in CGI script! 24 27 ( $self->{'pty'},$self->{'pid'} ) = 25 28 &spawn($Web::Terminal::Settings::command); # spawn() defined below 26 29 if ( $self->{'pty'}==-1 and $self->{'pid'}==0) { 30 $self->{'init'}= "\nThere was a problem starting pugs. Please try again later."; 31 $self->{'error'}=1; 32 } else { 27 33 ## Create a Net::Telnet object to perform I/O on pugs's tty. 28 34 use Net::Telnet; … … 35 41 ); 36 42 #( $self->{'init'}, my $m ) = $self->{'pugs'}->waitfor( 37 ( my $p, my $m ) = $self->{'pugs'}->waitfor( 43 my $error=''; 44 ( my $p, my $m ) = $self->{'pugs'}->waitfor( 38 45 -match => $self->{'pugs'}->prompt, 39 46 -errmode => "return" 40 ) or die "starting pugs failed: ", $self->{'pugs'}->lastline; 41 $self->{'init'}= $p.$m;#$self->{'pugs'}->prompt; 47 ) or do { 48 $self->{'error'}=1; 49 $error="\nThere was a problem starting pugs. Please try again later."; 50 # should close the TTY 51 $self->{'pugs'}->close(); 52 }; 53 #die "starting pugs failed: ", $self->{'pugs'}->lastline; 54 $self->{'init'}= $p.$m.$error;#$self->{'pugs'}->prompt; 55 } 42 56 bless($self,$class); 43 57 return $self; … … 54 68 55 69 if ( $cmd eq $Web::Terminal::Settings::quit_command ) { 70 $obj->{pugs}->close(); 56 71 kill 9, $obj->{'pid'}; 57 72 return "\n$Web::Terminal::Settings::quit_message\n"; … … 64 79 65 80 $pugs->print($cmd); 66 while ( 1) {81 while ($i<$Web::Terminal::Settings::nlines) { 67 82 my $line = $pugs->getline; 68 83 my $msg=$pugs->errmsg; … … 72 87 $pugs->errmsg([]); 73 88 $lline="${Web::Terminal::Settings::prompt} Sorry, that took too long! Aborted.\n"; 89 $pugs->close(); 74 90 $ps=$Web::Terminal::Settings::prompt; 91 $obj->{'error'}=1; 75 92 last; 76 93 } … … 84 101 85 102 #$lline .= "\n$ps>"; 103 if ($i>=$Web::Terminal::Settings::nlines-1) { 104 $obj->{pugs}->close(); 105 kill 9, $obj->{'pid'}; 106 $lline.="Generated output is limited to $Web::Terminal::Settings::nlines lines. Aborted.\npugs"; 107 $obj->{'error'}=1; 108 } 109 86 110 chomp $ps; # a hack! 87 111 $lline .= $ps; … … 92 116 my (@cmd) = @_; 93 117 my ( $pid, $pty, $tty, $tty_fd ); 94 118 my $error=0; 95 119 ## Create a new pseudo terminal. 96 120 use IO::Pty (); 97 121 $pty = new IO::Pty 98 or die $!; 122 or do { 123 return ( -1, 0 ); 124 }; 125 #die $!; 99 126 binmode $pty, ":utf8"; 100 127 ## Execute the program in another process. 101 128 unless ( $pid = fork ) { # child process 102 die "problem spawning program: $!\n" unless defined $pid; 103 129 # die "problem spawning program: $!\n" unless defined $pid; 130 if (not defined $pid) { 131 $pty->close(); 132 $error=1; 133 } else { # all is well 104 134 ## Disassociate process from existing controlling terminal. 105 135 use POSIX (); 106 136 POSIX::setsid 107 or die "setsid failed: $!";137 or ($error=1);#die "setsid failed: $!"; 108 138 109 139 ## Associate process with a new controlling terminal. … … 114 144 115 145 ## Make stdio use the new controlling terminal. 116 open STDIN, "<&$tty_fd" or die $!;117 open STDOUT, ">&$tty_fd" or die $!;118 open STDERR, ">&STDOUT" or die $!;146 open STDIN, "<&$tty_fd" or ($error=1);#die $!; 147 open STDOUT, ">&$tty_fd" or ($error=1);#die $!; 148 open STDERR, ">&STDOUT" or ($error=1);#die $!; 119 149 binmode STDIN, ":utf8"; 120 150 binmode STDOUT, ":utf8"; … … 124 154 ## Execute requested program. 125 155 exec @cmd 126 or die "problem executing $cmd[0]\n"; 156 or ($error=1);#die "problem executing $cmd[0]\n"; 157 } 127 158 } # end child process 159 160 if($error==1) { 161 $pty=-1; 162 $pid=0; 163 } 128 164 129 165 return ( $pty, $pid ); -
misc/runpugs/lib/Web/Terminal/Settings.pm
r14366 r14430 27 27 timeout_idle 28 28 timeout_call 29 nlines 30 daemon 31 perl 32 server 33 cgi_path 34 nrecent 29 35 ); 30 36 31 our $command='/usr/bin/pugs'; 37 our $command='/usr/local/bin/ghci'; 38 our $prompt='Prelude> '; 39 our $prompt_pattern='(^(Prelude)>\s+)'; 40 our $quit_pattern='^Leaving\ GHCi\.'; 41 our $quit_message='Leaving GHCi.'; 32 42 33 our $prompt='pugs> '; 43 44 #our $command='/usr/bin/nice /usr/bin/pugs'; 45 our $server='termserv2.pl'; 46 #our $prompt='pugs> '; 34 47 our $init_pattern='(\>\s+)'; 35 our $prompt_pattern='(^(pugs|\.\.\.\.)>\s+)';48 #our $prompt_pattern='(^(pugs|\.\.\.\.)>\s+)'; 36 49 our $quit_command=':q'; 37 our $quit_pattern='^Leaving\ pugs\.';38 our $quit_message='Leaving pugs.';50 #our $quit_pattern='^Leaving\ pugs\.'; 51 #our $quit_message='Leaving pugs.'; 39 52 40 53 our $filter=0; 41 54 our $filter_pattern=''; 42 55 our $cgi_path='/home/andara/apache/cgi-bin/'; 56 our $daemon=0; 43 57 our $port=2057; 44 58 our $host='localhost'; … … 50 64 our $timeout_call=10; 51 65 our $check_interval=300; 66 our $nlines=250; 67 our $nrecent=10; 68 our $perl='/usr/bin/perl'; 69 -
misc/runpugs/lib/WebTerminal/Msg.pm
r14407 r14430 249 249 sub _new_client { 250 250 my $sock = $main_socket->accept(); 251 #returns undef on fail! 252 ## so: 251 #returns undef on fail. Calling peerhost on undef makes it die. 252 ## so: 253 if (defined $sock) { 253 254 my $conn = bless { 254 255 'sock' => $sock, … … 264 265 $conn->disconnect(); 265 266 } 267 } else { 268 #what? 269 return undef; 270 } 266 271 } 267 272 -
misc/runpugs/lib/WebTerminal/Server.pm
r14407 r14430 46 46 my $lines = $term->write($cmd); 47 47 if ( $cmd eq ':q' ) { 48 my $pid= $terminals{$id}->{pid}; 48 49 delete $terminals{$id}; 49 my $pid= $terminals{$id}->{pid};50 50 if ($pid) { 51 51 kill 9,$pid; … … 54 54 } 55 55 if ($lines=~/Aborted/s) { 56 my $pid= $terminals{$id}->{pid}; 56 57 delete $terminals{$id}; 57 my $pid= $terminals{$id}->{pid};58 58 if ($pid) { 59 59 kill 9,$pid; … … 75 75 $terminals{$id}->{ip}=$ip; 76 76 my $term = $terminals{$id}; 77 return $term->{'init'}; 77 my $init= $term->{'init'}; 78 my $error= $term->{'error'}; 79 if ($error==1) { # Failed to create a new terminal 80 $sessions_per_ip{$ip}--; 81 delete $terminals{$id}; 82 } 83 return $init; 78 84 } 79 85 } … … 111 117 my $port=shift; 112 118 $SIG{USR1}=\&timeout; 113 my $daemon= 1;119 my $daemon=0; 114 120 if ($daemon) { 115 121 Proc::Daemon::Init; -
misc/runpugs/lib/WebTerminal/Server/Terminal.pm
r14407 r14430 17 17 #my $prompt = '/\>\ /'; 18 18 my $prompt = '/>\ /'; 19 $self->{'error'}=0; 19 20 ## Start pugs 20 21 # $ENV{PUGS_SAFEMODE}=1;# Must be in CGI script! 21 ( $self->{'pty'},$self->{'pid'} ) = &spawn("/usr/bin/pugs"); # spawn() defined below 22 22 ( $self->{'pty'},$self->{'pid'} ) = &spawn("/usr/bin/nice /usr/bin/pugs"); # spawn() defined below 23 if ( $self->{'pty'}==-1 and $self->{'pid'}==0) { 24 $self->{'init'}= "\nThere was a problem starting pugs. Please try again later."; 25 $self->{'error'}=1; 26 } else { 23 27 ## Create a Net::Telnet object to perform I/O on pugs's tty. 24 28 use Net::Telnet; … … 38 42 # die "starting pugs failed: ", $self->{'pugs'}->lastline; 39 43 $error="\nThere was a problem starting pugs. Please try again later."; 44 $self->{'error'}=1; 45 # should close the TTY 46 $self->{'pugs'}->close(); 40 47 }; 41 48 $self->{'init'}= $p.$m.$error;#$self->{'pugs'}->prompt; 49 } 42 50 bless($self,$class); 43 51 return $self; … … 66 74 # print "L:",$line,":",$msg; 67 75 if($msg=~/timed/) { 68 $msg=''; 69 $pugs->errmsg([]); 70 $lline="pugs> Sorry, that took too long! Aborted.\n"; 71 $ps='pugs'; 72 last; 76 $msg=''; 77 $pugs->errmsg([]); 78 $lline="pugs> Sorry, that took too long! Aborted.\n"; 79 # $pugs->close(); 80 $ps='pugs'; 81 $obj->{'error'}=1; 82 last; 73 83 } 74 84 $msg=''; … … 78 88 } 79 89 if ($i>=255) { 90 # $pugs->close(); 80 91 kill 9, $obj->{'pid'}; 92 $obj->{'error'}=1; 81 93 $lline.="Generated output is limited to 100 lines. Aborted.\npugs"; 82 94 } … … 90 102 my (@cmd) = @_; 91 103 my ( $pid, $pty, $tty, $tty_fd ); 92 104 my $error=0; 93 105 ## Create a new pseudo terminal.
