logout idle users on selected ports

Jay A. Snyder jay at gdx.UUCP
Sun Jan 27 12:07:05 AEST 1991


I wanted to be able to do idle logout for dialins only, but not
console jobs (to keep the dialin lines open).  So I wrote this little
program.  It must be run as root.

This is a simpile little utility that will logout selected terminals
after a specified idle time is reached.

usage:
idleout [time in minutes] list of ttys

--- idleout.c ----
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <utmp.h>

kill_process(pid)
     short pid;
{
  if (kill(pid,SIGHUP)) /* try hangup */
    if (kill(pid,SIGTERM)) /* try term */
      kill(pid,SIGKILL); /* just brute force kill it! */
}

time_t idletime(linename)
     char *linename;
{
  char filename[40];
  struct stat s;
  sprintf(filename,"/dev/%s",linename);
  stat(filename,&s);
  return((time((time_t *) 0)-s.st_mtime)/60);
}

main(argc,argv)
     int argc;
     char *argv[];
{
  struct utmp *u,uu;
  int i;
  time_t t,maxidle;

  if (argc<3)
    {
      fprintf(stderr,"usage: idletime [time] [tty] ...\n");
      exit(-1);
    }
  if (fork()) exit(0); /* run as daemon */
  close(0);
  close(1);
  close(2);
  setpgrp(); /* detach from process group */
  maxidle=atoi(argv[1]);
  uu.ut_user[0]=0;
  uu.ut_id[0]=0;
  uu.ut_line[0]=0;
  uu.ut_pid=0;
  uu.ut_type=0;
  for (;;)
    {
      for (i=2;i<argc;++i)
	{
	  setutent();
	  strcpy(uu.ut_line,argv[i]);
	  u=getutline(&uu);
	  if (u->ut_type==USER_PROCESS)
	    {
	      if (idletime(u->ut_line)>maxidle)
		{
		  kill_process(u->ut_pid);
		}
	    }
	}
      sleep(60);
    }
}



More information about the Alt.sources mailing list