X-Git-Url: https://git.jankratochvil.net/?p=nethome.git;a=blobdiff_plain;f=bin%2Ffup;h=ec166d425628bbace3fbe45f384ee45df706476a;hp=05f5d068607e509a6dac4648871100d9588cb967;hb=8104eca7d9cf4376bfa2eddc3c2a71b84b9d9d02;hpb=5e0e8ea3a74884cf242ae2421cf74f3e43f2a28e diff --git a/bin/fup b/bin/fup index 05f5d06..ec166d4 100755 --- a/bin/fup +++ b/bin/fup @@ -13,10 +13,10 @@ use Data::Dumper; my $USER="adsl_303141"; my $USERNAME="Jan Kratochvil"; my $PASSWORD=&_priv_postget("uzivatel.gtsnovera.cz.pwd"); -my $IFDEV="ppp0"; +my $IFDEV="eth0"; my $SECS=4; -my $RX_THRESHOLD=200000; # 226085 -my $TX_THRESHOLD=200000; # 291251 +my $RX_THRESHOLD=210000; # 226085 +my $TX_THRESHOLD=210000; # 291251 my $STABILIZE_OFF=1; my $STABILIZE_ON=4*60; my $INPUT_IDLE=10*60; @@ -117,46 +117,62 @@ my($what,$length)=@_; change($what); } -# CPU0 CPU1 -# 1: 107040 111123 Phys-irq i8042 -# 6: 3 0 Phys-irq floppy -sub input() +use User::Utmp; + +# Users respected for the 'idle' state (see $IdleMax): +my @ValidUsers=qw(root lace jkratoch); + +sub useridle() { - my $fname="/proc/interrupts"; - local *F; - open F,$fname or die "open \"$fname\": $!"; - or die "header \"$fname\"";; - my $r=0; - while () { - chomp; - my @f=split; - last if $f[0]=~/^[A-Z]{3}:$/; # NMI/LOC/ERR/MIS - $f[0]=~/^\s*\d+:$/ or die $f[0]; - next if $f[$#f] ne "i8042"; - for (1..$#f-2) { - $_=$f[$_]; - /^\d+$/ or die $_; - $r+=$_; - } + my %valid_users=map(($_=>1),@ValidUsers); + my($idlebest,$linebest); + for my $utmp (User::Utmp::getut()) { + local $_; + next if defined($_=$utmp->{"ut_type"}) && $_!=&User::Utmp::USER_PROCESS(); + next if defined($_=$utmp->{"ut_user"}) && !$valid_users{$_}; + my $line="/dev/".$utmp->{"ut_line"}; + my $atime=(stat $line)[8]; + my $what="user \"".($utmp->{"ut_user"} || "")."\", line \"$line\""; + warn "Unable to stat $what" and next if !$atime; + my $idle=time()-$atime; + warn "atime in future for $what" and next if $idle<0; + next if defined $idlebest && $idlebest<=$idle; + $idlebest=$idle; + $linebest=$line; } - close F or die "open \"$fname\": $!"; - return $r; + return !wantarray() ? $idlebest : ($idlebest,$linebest); } -my $input=input(); -my $input_last=time()+0; sub input_idle() { - my $input_now=input(); - my $diff=$input_now-$input; - die $diff if $diff<0; - my $time_now=time()+0; - if ($diff) { - $input=$input_now; - $input_last=$time_now; + return useridle() > $INPUT_IDLE; +} + +sub mplayer_running() +{ + local *F; + open F,"/proc/net/unix" or die; + my %unix; + ; + local $_; + while () { + /^(?:\S+\s+){6}(\d+)\s/ or die; + $unix{$1}=1; + } + close F or die; + + local $_; + while () { + (my $fdname=$_)=~s{/exe$}{/fd}; + $_=readlink or next; + m{/mplayer$} or next; + while (<$fdname/*>) { + $_=readlink or next; + my $inode=/^\Qsocket:[\E(\d+)\Q]\E$/ or next; + return 1 if $unix{$1}; + } } - my $time_diff=$time_now-$input_last; - return $time_diff>$INPUT_IDLE; + return; } for (;;) { @@ -171,15 +187,18 @@ for (;;) { $on_now=0 if ($rx-$rx_last) > $RX_THRESHOLD; $on_now=0 if ($tx-$tx_last) > $TX_THRESHOLD; } - $on_now=1 if my $input_idle=input_idle(); + my $print=$on_now; + my $input_idle=input_idle(); + my $mplayer_running=mplayer_running() if $input_idle; + $on_now=1 if $input_idle && !$mplayer_running; $rx_last=$rx; $tx_last=$tx; push @history,$on_now; - my $print=$on_now; - $print=($on_now ? "." : "v") if $input_idle; + $print=($print ? "." : "v") if $input_idle; + $print="M" if $mplayer_running; $print="*" if $ignore_read; print($print); - history(1,($input_idle ? 0 : $STABILIZE_ON)); + history(1,(($input_idle && !$mplayer_running) ? 0 : $STABILIZE_ON)); history(0,$STABILIZE_OFF); $ignore_read=$ignore_write; $ignore_write=0;