Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!rice!sun-spots-request From: macomber@thoreau.nsc.com (Robert Macomber) Newsgroups: comp.sys.sun Subject: Re: user inactivity and screen blanking Keywords: Miscellaneous Message-ID: <6664@brazos.Rice.edu> Date: 13 Apr 90 17:32:23 GMT Sender: root@rice.edu Organization: Sun-Spots Lines: 62 Approved: Sun-Spots@rice.edu X-Refs: Original: v9n107 X-Sun-Spots-Digest: Volume 9, Issue 118, message 16 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 #include #include #include #include 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