We see various problems with servers like gconf exiting in a timely
fashion.  The solution I put together removes files from /tmp when the
owner is not logged in.  This doesn't always work because utmp appears
to be unreliable.  However, the behavior with this script seems to be
much better than without it.

I run the script every minute from cron.

-- 
A new cognitive theory of emotion, http://openheartlogic.org
#!/usr/bin/perl

use strict;
use warnings;
use Sys::Syslog;
use Storable;

use constant PENDING_PATH => '/root/pending_tmp.state';  #put on /var ?

our $Pending = {};
eval { $Pending = retrieve PENDING_PATH };

openlog $0, '', 'user';

sub run {
    my $cmd = join ' ', @_;
    system($cmd) == 0
        or die "system $cmd failed: $?"
}

sub usage {
    print STDERR "$0 <user>\n";
    exit;
}

usage if @ARGV > 1;

sub user_to_uid {
    my ($user) = @_;
    return 0 if $user eq 'root';
    my $uid = (getpwnam($user))[2]
        or die "$user not in passwd file";
    $uid;
}

sub uid_to_user {
    my ($num) = @_;
    getpwuid($num) or die "no uid $num";
}

#my $target;
#if (@ARGV == 1) {
#    $target = user_to_uid $ARGV[0];
#}

my %known = ( 0 => 1 );   #root is always known

open my $w, "users|" or die "open $!";
while (defined(my $l = <$w>)) {
    for my $u (split /\s+/, $l) {
        $known{ user_to_uid($u) } = 1;
        delete $Pending->{ user_to_uid($u) };
    }
}

opendir my $dh, '/tmp' or die "opendir $!";
for my $f (readdir $dh) {
    my @s = stat "/tmp/$f";
    if ([EMAIL PROTECTED]) {
      warn "can't stat /tmp/$f";
      next;
    }
    next if $known{ $s[4] };
    my $user = uid_to_user $s[4];
#    print "$user $f\n";
#    next if !defined $target;
#    next if $s[4] != $target;
    my $try = ++$Pending->{ $s[4] };
    if ($try > 1) {
        run "rm -rf /tmp/$f";
        syslog 'crit', "[%s] rm -rf /tmp/%s", $user, $f;
        delete $Pending->{ $s[4] };
    } else {
#       syslog 'crit', "[%s] /tmp/%s sighted %d", $user, $f, $try;
    }
}

store $Pending, PENDING_PATH;

Attachment: signature.asc
Description: Digital signature

Reply via email to