-for (;;) {
- if ($keepalive) {
- $now= now();
- $thistimeout= $nextsendka-$now;
- if ($thistimeout < 0) {
- defined(send L,"\300",0,$rs)
- or warning("transmit keepalive error: $!");
- $nextsendka= $now+$keepalive;
- $thistimeout= $keepalive;
- }
- } else {
- $thistimeout= undef;
- }
- select($readfds=$wantreadfds,'','',$thistimeout);
- for (;;) {
- if (!defined($r= sysread(UR,$upbuf,$mtu*2+3,length($upbuf)))) {
- $! == EAGAIN || warning("tunnel endpoint read error: $!");
- last;
- }
- if (!$r) {
- quit "tunnel endpoint closed by system";
- }
- while (($p= index($upbuf,"\300")) >= 0) {
- if ($p && !defined(send L,substr($upbuf,0,$p),0,$rs)) {
- warning("transmit error: $!");
- } else {
- if (!$upyet) {
- $upyet= 1;
- debug($downyet ? "tunnel open at this end" : "transmitting");
- }
- if ($keepalive) { $nextsendka= now()+$keepalive; }
- }
- $upbuf= substr($upbuf,$p+1);
- }
+while (keys %procs) {
+ ($c= wait) >0 or
+ fail("wait failed (expecting ". join('; ',keys %procs). ")");
+ warning("unexpected child reaped: pid $c, code $?"), next
+ unless exists $procs{$c};
+ $str= $procs{$c};
+ delete $procs{$c};
+ $? ? warning("subprocess $str failed with code $?")
+ : debug("subprocess $str finished");
+ if ($c==$c_lcmd || $c==$c_fwd || $c==$c_rcmd) {
+ kill 15, grep (exists $procs{$_}, $c_fwd, $c_rcmd);