[comp.sys.sun] user inactivity and screen blanking

macomber@thoreau.nsc.com (Robert Macomber) (04/14/90)

In article <6348@brazos.Rice.edu>, andrew@ambra.dk (Leif Andrew Rump) writes:
> Is it possible to detect (user) inactivity (no keyboard, mouse or other
> external events) on a unix workstation (more specific a Sun Sparc1)?

One way to check for idle time is by 'stat'ing the tty entry in the "/dev"
directory.  I think that this is how "finger" arrives at user idle times.
Unfortunately, the access times for "/dev/mouse" and "/dev/kbd" don't seem
to get updated.

Here's a quick example show how to collect this information.  It prints
out the number of seconds that each user (tty windows, too) has been
inactive.

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

main()
{
	FILE           *fp;
	struct timeval  now;
	struct utmp     utmp;
	struct stat     then;
	char            tty_path[14];

	if ((fp = fopen("/etc/utmp", "r")) == NULL) {
		printf("can't open /etc/utmp\n");
		exit(1);
	}
	gettimeofday(&now, (struct timeval *) 0);

	printf("UserID      TTY      Idle Seconds\n\n");

	while (fread(&utmp, sizeof(struct utmp), 1, fp) == 1) {

		/*
		 * Ignore null entries
		 */
		if (utmp.ut_name[0] == NULL)
			continue;

		sprintf(tty_path, "/dev/%.8s", utmp.ut_line);

		/*
		 * Get time of last tty I/O
		 */	 
		stat(tty_path, &then);

		printf("%-8.8s %-13.13s %5d\n",
		       utmp.ut_name,
		       tty_path,
		       (now.tv_sec - then.st_atime));
	}
	fclose(fp);
}

			Robert L. Macomber
 		      National Semiconductor
		       South Portland, Maine
		      macomber@thoreau.nsc.com

guy@uunet.uu.net (Guy Harris) (04/15/90)

 >One way to check for idle time is by 'stat'ing the tty entry in the "/dev"
 >directory.  I think that this is how "finger" arrives at user idle times.
 >Unfortunately, the access times for "/dev/mouse" and "/dev/kbd" don't seem
 >to get updated.

Eh?  They sure get updated in 4.x (I remember having to monkey with the
way the mouse device was implemented during 4.0 development in order to
make "finger" and company give the right results).