[ale] Problems with utmp.

Christopher Hamilton chrish at gstv.gsu.edu
Sat Jun 8 16:32:58 EDT 1996


I apologize for posting this to ALE, but I misplaced the sender
of the code that reads the utmp file.

This is the problem, for the most part - it works fine. However,
there have been instances where the ut_type has been like 65442.
A bit strange for a number that is only supposed to be 0-8. here
is the code. The output is something like who -a on some UMIX 
systems.

#!/usr/bin/perl  -w
#
use POSIX;
use subs('utmp_nfo','idle');

print &utmp_nfo;

sub utmp_nfo
{
    $UTMP='/var/run/utmp';	    
    $ut_t='l l a12 a4 l a8 a16 l';	
    $sizeof_ut=length(pack($ut_t,0));

    $getutent=$ut_type=$ut_pid=$ut_line=$ut_id=0;
    $ut_time=$ut_user=$ut_host=$ut_addr=0;

    %proc=('UT_UNKNOWN',    0,
           'RUN_LVL',	    1,
           'BOOT_TIME',	    2,
           'NEW_TIME',	    3,
           'OLD_TIME',	    4,
           'INIT_PROCESS',  5,
           'LOGIN_PROCESS', 6,
           'USER_PROCESS',  7,
           'DEAD_PROCESS',  8);

    open(UTMP,$UTMP) || die $!;
    $getutent='';
    while(read(UTMP,$getutent,$sizeof_ut))
    {
        ($ut_type,$ut_pid,$ut_line,$ut_id,$ut_time,$ut_user,$ut_host,$ut_addr)=
	    unpack($ut_t,$getutent);

#	if($ut_type == $proc{'USER_PROCESS'})
        if($ut_type)
        {
	    $ut_user =~ s/\0//g;
            print $ut_user,' ' x (10 - length($ut_user));

	    $ut_line =~ s/\0//g;
	    (-w "/dev/$ut_line")? print "+ " : print "- ";
	    print "$ut_line",' ' x 3;

	    print strftime("%b %d %H:%M",localtime($ut_time)),' ' x 3;
	    print &idle($ut_line),' ' x 3;

	    $ut_host =~ s/\0//g;
	    (!$ut_host)? print "(console)" : print "($ut_host)";	

            print "($ut_type)\n";
        }
    }
}
sub idle
{
    $time=(time-((stat("/dev/$_[0]"))[8]));
  
    return("00:00") if(!$time);
    return("one day+") if($time/86400 >= 1);

    $hrs=($time/3600) % 24;
    $min=($time/60) % 60;

    return(sprintf("%s%d:%s%d",($hrs < 10)?"0":"",$hrs,
          ($min < 10)?"0":"",$min));
}






More information about the Ale mailing list