[comp.unix.xenix] auto log out for idle terminal

root@ozdaltx.UUCP (root) (12/08/89)

Here is a quickie I did to watch a caller who might go off and leave
their terminal idle.  After a period of time if the program dosn't see
any activity on their terminal, it will warn them, them log them off
the system. This runs on XENIX 2.2.3 (286) and should run on 386.
compiling is no big deal:  cc -s idleterm.c -o idleterm.
have fun with it....
Scotty
----
AIDS INFORMATION EXCHANGE BBS      (214) 247-2367/247-5609
               "Education is the best weapon"
     {ames,rutgers,texsun,smu}!attctc!ozdaltx!sysop 
------------------------------ CUT HERE ------------------------------
/*
* idleterm.c - monitors the activity on a terminal, actually, the lack of
* and warns the user when their terminal has been idle for more than
* a specified period of time.  If the associated device is not accessed
* within another time period, they are disconnected via kill().
*
* Because I wrote this for use on a BBS, idleterm is run as a background
* process, < idleterm& > and placed at the top of either .profile or a
* shell script which is run by exec.  That ensures the correct PPID.
*
* Do with it what you will, I'd like to know about mods or enhancements.
* Scotty
* {}!attctc!ozdaltx!root
*/

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

main()
{
    int getppid(), ppid;
    long time(), *seconds;
    long devtime, diff, curtime;
    char *tty, *ttyname();
    unsigned sleep();
    struct stat stbuf;

/* not any error checking here - probably should be */

    tty = ttyname(1);	/* get std /dev/ttyname */
    ppid = getppid();	/* get parent process number */

/* forever */
    while(1) {
        curtime = time(&seconds);	/* time in seconds */
        stat(tty, &stbuf);		/* stat the /dev */
        devtime = stbuf.st_atime;	/* time of last access to /dev */
        diff = curtime - devtime;

/* Can be changed - this is set for between 5 and 6 minutes */

        if(diff > 300 && diff <= 360 ){
            fprintf(stderr,"%c\nWARNING:\nTerminal idle more than 5 minutes\n",'\007');
            fprintf(stderr,"You'll be logged off in 1 minute\n");
        }
/* Terminal was idle for more than 6 minutes */
	if(diff > 360){
            fprintf(stderr,"%cLogging you off\n",'\007');
            kill(ppid,SIGHUP);  /* kill 'em */
        }
        sleep(60);
    }
    exit(0);  /* NOTREACHED */
}