user inactivity and screen blanking

Robert Macomber macomber at thoreau.nsc.com
Sat Apr 14 03:32:23 AEST 1990


In article <6348 at brazos.Rice.edu>, andrew at 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 at thoreau.nsc.com



More information about the Comp.sys.sun mailing list