X-Git-Url: https://git.jankratochvil.net/?p=nethome.git;a=blobdiff_plain;f=bin%2Ffup;h=ec166d425628bbace3fbe45f384ee45df706476a;hp=c8e9144b35e0e39b6f6f92993273aa75eabbc59e;hb=b8be1b96db19decef6b6b1de6115aa7c5b50bb18;hpb=9d0867d386391086dcf61e0c83fba2d0e58e16ca diff --git a/bin/fup b/bin/fup index c8e9144..ec166d4 100755 --- a/bin/fup +++ b/bin/fup @@ -15,10 +15,11 @@ my $USERNAME="Jan Kratochvil"; my $PASSWORD=&_priv_postget("uzivatel.gtsnovera.cz.pwd"); 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=5*60; +my $STABILIZE_ON=4*60; +my $INPUT_IDLE=10*60; $RX_THRESHOLD=$RX_THRESHOLD*$SECS/8; $TX_THRESHOLD=$TX_THRESHOLD*$SECS/8; @@ -116,6 +117,64 @@ my($what,$length)=@_; change($what); } +use User::Utmp; + +# Users respected for the 'idle' state (see $IdleMax): +my @ValidUsers=qw(root lace jkratoch); + +sub useridle() +{ + 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; + } + return !wantarray() ? $idlebest : ($idlebest,$linebest); +} + +sub input_idle() +{ + 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}; + } + } + return; +} + for (;;) { local *F; open F,"/proc/net/dev" or die; @@ -128,11 +187,18 @@ for (;;) { $on_now=0 if ($rx-$rx_last) > $RX_THRESHOLD; $on_now=0 if ($tx-$tx_last) > $TX_THRESHOLD; } + 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; - print(($ignore_read ? "_" : $on_now)); - history(1,$STABILIZE_ON); + $print=($print ? "." : "v") if $input_idle; + $print="M" if $mplayer_running; + $print="*" if $ignore_read; + print($print); + history(1,(($input_idle && !$mplayer_running) ? 0 : $STABILIZE_ON)); history(0,$STABILIZE_OFF); $ignore_read=$ignore_write; $ignore_write=0;