[comp.unix.wizards] Enforcing session timeout under csh

philf@med-isg.stanford.edu (Phil Fernandez) (05/17/88)

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?

Please send mail to me directly; I don't regularly read this
newsgroup.

Thanks!

Phil Fernandez
Stanford University Medical School
Internet:  philf@med-isg.stanford.edu
UUCP:      sun!med-isg.stanford.edu!philf

madd@bu-cs.BU.EDU (Jim Frost) (05/30/88)

In article <21319@labrea.STANFORD.EDU> philf@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@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@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);
  }
}

dieter@nmtsun.nmt.edu (Dieter Muller) (05/31/88)

In article <22987@bu-cs.BU.EDU> madd@bu-it.bu.edu (Jim Frost) writes:
>In article <21319@labrea.STANFORD.EDU> philf@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.
>
>This is actually quite generic and could time out just about anything.
>
[ program that sleeps then kill (cshpid, SIGHUP) ]

There's only one minor little problem with this.  If the user is running
some big hairy huge job that'll run for 5 hours and also changes its
process group, that job doesn't get killed.  In fact, you now are the
proud owner of a process that will randomly splat all over the next
user's terminal.  If the BHHJ also reads, you've got some real problems.

I've been playing with this sort of problem off and on for a while now,
and I can't see any nice way of dealing with it.  Our problem is more
along the lines of restricting certain types of accounts to certain hours,
but the basic principle remains.

The only guaranteed solution I've found is something that, when the time
to nuke arrives, effectively does something along the lines of:

set gleep = `ps x | egrep $WHATEVER | egrep -v egrep | colrm 6`
kill -9 $gleep
unset gleep

(csh & 4.2BSD syntax, if it matters), where WHATEVER is set to the
appropriate criterion (user name, terminal, etc.)

Dieter
-- 
You want coherency, cogency, and literacy all in one note?  Be real.
...{cmcl2, ihnp4}!lanl!unm-la!unmvax!nmtsun!dieter
dieter@nmtsun.nmt.edu