Enforcing session timeout under csh

Jim Frost madd at bu-cs.BU.EDU
Mon May 30 03:48:20 AEST 1988


In article <21319 at labrea.STANFORD.EDU> philf at med-isg.stanford.edu (Phil Fernandez) writes:
|One of my users with a Sun 3/60 wishes to enforce an idle session
|timeout under SunOS 3.5.  That is, after a login session has been idle
|for, say, 10 minutes, he wants the line (most likely a telnet
|connection) to be automatically logged out.  Should work for any
|shell.  Is there a way to do this?  How?

I haven't seen much discussion about this, but here's my solution
(which should work on BSD-ish systems, and definitely works under
SunOS).  Run the following program from .login and it'll idle out
the csh after HOWLONG minutes.

This is actually quite generic and could time out just about anything.

jim frost
madd at bu-it.bu.edu
-- cut here --
/* timeout.c:
 *
 * this program attempts to kill its parent (presumably a shell) when
 * the controlling tty has been idle too long.
 *
 * written by jim frost (madd at bu-it.bu.edu) on 5.27.88.  this program
 * is placed in the public domain.
 */

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

#define HOWLONG 10  /* number of minutes before we kill something */

long ttytime()
{ struct stat stb;

  if (ttyname(0) == NULL)        /* don't know who our tty is */
    exit(1);
  if (stat(ttyname(0),&stb) < 0) /* stat failed -- no device */
    exit(1);
  else
    return(stb.st_mtime);        /* last time since something happened */
}

main(argc,argv)
{ int  cshpid;
  long t;

  /* get parent's (csh's) pid
   */

  cshpid= getppid();

  /* fork off so csh looses us
   */

  switch(fork()) {
    case -1 :
      printf("fork failed!  will not timeout.\n");
      exit(1);
    case 0 :
      break;
    default :
      exit(0);
  }

  /* loop until either ttytime() kills us or our idle time is too large
   */

  for (;;) {
    time(&t);
    if (t - ttytime() > (HOWLONG * 60))

      /* we've been idle too long.  send HUP signal to parent
       */

      if (kill(cshpid,SIGHUP) != -1) {
        printf("\nIdle timeout\n");
        exit(0);
      }

    /* sleep to keep down overhead
     */

    sleep(60);
  }
}



More information about the Comp.unix.wizards mailing list