User Idle Time

Does anyone know how do you determine the user idle time of stdin in order to log the user out for being idle too long. I would like to write a c program to do this but I it is not clear upon how to determine idle time from keyboard input.

Check if you have w command available to you. One of the columns in its output lists idle time.

[yogeshs@localhost ~]$ w
 11:46:04 up  2:06, 5 users,  load average: 0.16, 0.25, 0.21
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
yogeshs  :0       -                09:41   ?xdm?  10:59   0.64s /usr/bin/gnome-session
yogeshs  pts/1    :0.0             09:49    1:55m  0.06s 41.24s gnome-terminal
yogeshs  pts/2    :0.0             09:56    1:48m  0.04s  0.04s bash
yogeshs  pts/3    :0.0             09:59   10:32   0.04s  0.04s bash
yogeshs  pts/4    :0.0             11:45    0.00s  0.03s  0.00s w
[yogeshs@localhost ~]$

I am aware of this command along with the who command, I have looked at the source for these files and they use the time now-last access of tty, I would like to determine idle time by using stdin somehow. Also, Id like to be gangster and write it in C.

At least on my system, the mod time on the tty's device seems to be where this information is coming from. If it works the same on your system, you can use that.

vnix$ ls -l /dev/pts | fgrep era
crw--w---- 1 era tty 136, 0 2008-04-07 15:17 0
crw--w---- 1 era tty 136, 1 2008-04-07 14:22 1
crw--w---- 1 era tty 136, 2 2008-04-07 14:26 2
vnix$ w
 15:17:26 up  1:49,  4 users,  load average: 1.02, 0.45, 0.21
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
era      tty7     :0               13:57    0.00s  3:07m  0.92s x-session-manag
era      pts/0    :0.0             13:57    0.00s  0.30s  0.01s w
era      pts/1    :0.0             14:22   55:09m  0.17s  0.10s ssh -L 4444:loc
era      pts/2    :0.0             14:25   51:26m  0.18s  0.00s tail --follow=n

(Never mind, pointed out related thread but moderator closed the other thread so taking away my pointer there.)

ERA -

To check for idle I:

set an alarm, and then pause();

during the pause() :

I type on the keyboard and it writes to the terminal, but the last access time to stdin or the specific tty are not reflected by my typing. So I have no way to test my program.

Does this make sense? Perhaps I am thinking about this wrong. Sorry for double posting.

I did this in one window:

while true; do ls -l /dev/pts/1; done

Then I started typing in another window, which is connected to /dev/pts/1. The time stamp changed immediately, although then -- the granularity being only one minute in the ls -l output -- seemed stuck. Tried again after the minute changed, and again, it updated basically immediately.

You pause in a program which is running on the terminal you are watching? You want to catch input even when the tty is not actually responding? If I run sleep 600 and then type in something during that time, the tty time stamp does not update. But that would seem to pretty much match a reasonable definition of "idle", from the system's perspective, although certainly from the user's perspective, if you are typoing (oops) at it, you are not idle.

If you pwn the system (I guess you do) then I suppose you could attach a kernel module to the tty driver or something ... sounds like too much work.

UNIX IN THE ENTERPRISE - Killing Idle Logins with idled

If the user is connected over the network (which I guess they will be, on a modern-day server), maybe you can look at the traffic to and from that port, in a more or less similar way.

Here's one way to do it:

Getting idle time in unix coderrr