[net.sources] anti-idle

attila@tekig5.UUCP (Wayne Knapp) (09/28/85)

[]

Here is a c program which shows some interesting uses of system calls
and has little other value :-).  It is a totally benign program doing
no damage to anyone (except perhaps some overly eager system administrators
who may suffer some mental damage).  This program should be run from
your .login file (.profile if you run --heaven forbid-- b-shell).  It
makes it look like your terminal has been idle for random times ranging
from 0 to 99:59 hours to anyone who does w after your terminal has been
idle for a short time.

-----------------------------------------------------------------------



#include <stdio.h>
#include <utmp.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sgtty.h>

#define MICKEY	60		/* sleep loop */

#define MOUSE	;;


char  *pn;

extern char *getlogin();

/*
####################
main (argc, argv)
####################
*/

main ()	 /* beaver dam building program for national guard */ 


{
   register char **p, *q;
   char *tty, *ttyname();
   int i=0;

   signal(SIGTTOU, SIG_IGN);	/* allow writting to control terminals */
   signal(SIGHUP,SIG_DFL);


   if ( (tty = ttyname(fileno(stderr))) == NULL )  /* the name of the tty */
      error("Unable to find the terminal name.\n");


   if (fork())	 /* exit from the parent */
      exit(0);


   /* CHILD */

   for(MOUSE)  /* set idle time and then sleep */
   {
      chkidle(tty);

      sleep(MICKEY);
   }
}



/*
####################
chkidle(tty)
####################
*/

chkidle(tty)  /* check to see if the terminal is idle */

char *tty;

{
   struct stat stbuf;
   long clock;
   int seed;
   char *name;
   time_t timep[2];

   /* terminal hungup, time to go */
   if ( (name =getlogin()) == (char *)NULL || *name == NULL)
      exit(0);

   if ( stat(tty, &stbuf) < 0 )
      error("Unable to stat the tty (%s)\n", tty);
   else
   {
      time(&clock);
      seed = clock & 255;
      timep[0] = clock - (random(seed) % 359940);
      timep[1] = clock - (random(seed) % 359940);
      utime(tty,timep);
   }
}



/*
####################
error(fmt, args)
####################
*/

error(fmt, args)	/* print an error message and exit */

char *fmt;

{
   extern char *pn;
   char namefmt[200];

   sprintf(namefmt, "%s: %s", pn, fmt);
   _doprnt(namefmt, &args, stderr);

   exit(1);
}