Reading files in /var/spool/rwho/whod.*

Fredrik Normann fredrin at ifi.uio.no
Tue Jun 28 05:54:41 EDT 2005


Fredrik Normann wrote:
> Hello,
> 
> I'm trying to read the binary files under /var/spool/rwho/ so I'm 
> wondering if anyone has done that before or could give me some clues on 
> how to read those files. I've tried to use the binascii module without 
> any luck.

A friend of mine made this C-program, that does the trick :)

#include <protocols/rwhod.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>

#include <time.h>
#include <sys/time.h>

#include <sys/types.h>
#include <netinet/in.h>
#include <inttypes.h>

#ifdef SOL
#include <stddef.h>
#else
#include <linux/stddef.h>
#endif

int main (int argc, char **argv)
{

   char *filename, *binary;
   FILE *fp;
   struct whod entry;
   struct outmp who;
   size_t ret;
   size_t cnt = 0;
   char *user = (char*) malloc (8);
   char *timestr = (char*) malloc (512);
   char *hostname = (char*) malloc (512);
   int hostlen, ulen;
   size_t prefix, we_len;

   hostlen = 0;
   ulen = 0;

   struct timeval now;
   struct tm *tptr;
   time_t t;

   binary = *argv;
   ret = gettimeofday (&now, 0);
   if (ret < 0) {
     perror ("Error getting time of day");
     exit (2);
   }

   if (argc < 2) {
     printf ("%s: ERROR, no whod file specified\n", binary);
     exit (1);
   }

   filename = *(argv + 1);

   fp = fopen (filename, "r");
   if (fp == NULL) {
     perror ("Error opening file");
     exit (3);
   }

#ifdef SOL
   ret = fread (&entry, 1, sizeof (entry), fp);
#else
   ret = fread_unlocked (&entry, 1, sizeof (entry), fp);
#endif
   if (ret < 0) {
     perror ("Error reading file");
     exit (4);
   }

   prefix = offsetof (struct whod, wd_we) / sizeof (struct whoent);
   we_len = ret / sizeof (struct whoent) - prefix;

   /* Find lengths of strings */
   for (cnt = 0; cnt < we_len; cnt++) {
     who = entry.wd_we[cnt].we_utmp;
     strncpy (user, who.out_name, 8);
     if (strlen (user) > ulen)
       ulen = strlen (user);
     if ((strlen (entry.wd_hostname) + strlen (who.out_line) + 1) > hostlen)
       hostlen = strlen (entry.wd_hostname) + strlen (who.out_line) + 1;
   }

   for (cnt = 0; cnt < we_len; cnt++) {
     who = entry.wd_we[cnt].we_utmp;
     strncpy (user, who.out_name, 8);
     strncpy (hostname, entry.wd_hostname, 512);
     strcat (hostname, ":");
     strncat (hostname, who.out_line, 8);

     t = now.tv_sec - ntohl (entry.wd_we[cnt].we_idle);

     entry.wd_we[cnt].we_idle = ntohl (entry.wd_we[cnt].we_idle) / 60;

     printf ("%-*s %-*s",
	    ulen, user,
	    hostlen, hostname);
     if (entry.wd_we[cnt].we_idle >= 0) {
       tptr = localtime (&t);
       strftime (timestr, 512, "%a %b %d %H:%m", tptr);

       printf (" %s ", timestr);
       printf ("%5d:%02d",
	      entry.wd_we[cnt].we_idle / 60,
	      entry.wd_we[cnt].we_idle % 60);
     }
     printf ("\n");
   }

   fclose (fp);
}



More information about the Python-list mailing list