timekpr keeps counting time when screen locked or user is switched

Bug #306584 reported by Mark Scott on 2008-12-09
12
This bug affects 1 person
Affects Status Importance Assigned to Milestone
timekpr
Medium
Unassigned

Bug Description

timekpr keeps counting time against the user even when the screen saver is running/locked or when the screen has been switched to another user (ie: Ubuntu). Unfortunately, this makes timekpr kind of worthless for me since I actually want to be able to control computer usage throughout the day not just login time. Otherwise, my son will need to log in & out several times a day (very annoying) just to be able to not use his precious time.

Mark Scott (markscott) wrote :

This link may help (tells how to use Dbus to get notifications of screen idle state changes):

http://live.gnome.org/GnomeScreensaver/FrequentlyAskedQuestions#head-ac43c8f33bc700a5e298e6a82ded0e8bb9b33043

Mark Scott (markscott) wrote :

Here's a small python script that gets the screensaver status. I am not sure how to run it as root & make it talk to a user's dbus session...

Even Nedberg (nedberg) wrote :

The problem is, as you say, to get the status with root.

We will see if we find a way! For now, I think your son has to log out every time he takes a longer break.

Savvas Radevic (medigeek) wrote :

gnome screensaver has a command to check the status:

$ gnome-screensaver-command -q
The screensaver is inactive
The screensaver is not inhibited

I don't know about xfce or kde though. :\

Changed in timekpr:
importance: Undecided → Medium
status: New → Confirmed
Even Nedberg (nedberg) wrote :

It is no problem to get the status with dbus in kde

$qdbus org.kde.screensaver /ScreenSaver GetActive
false

I am sure it is possible in XFCE too.

The problem would be to get this status with root (same problem we hade before we started using the client). The client could provide the info in a file or via a dbus call to timekpr. But a user could make a small script to fake this info and the counter would not run.

true, thanks!! same for gnome:
$ qdbus org.gnome.ScreenSaver /ScreenSaver GetActive
false

I think that xfce uses xscreensaver, it also has xscreensaver-command
but I don't know about dbus

>The client could provide the info in a file or via a dbus call to timekpr.
Correct again, we need dbus integration then (to-do)! :)

Savvas Radevic (medigeek) wrote :

hm.. ok, I checked about xfce, it seems that if favours xscreensaver,
but xubuntu (xubuntu-desktop) depends on gnome-screensaver.

$ apt-cache rdepends gnome-screensaver
gnome-screensaver
Reverse Depends:
  xubuntu-desktop
  xscreensaver-gl-extra
  xfce4-utils

If a user installs xfce4 though, they will install xfce4-utils which
favours xscreensaver:
$ apt-cache show xfce4-utils
Package: xfce4-utils
Recommends: xfwm4, xfce4-panel, thunar, xscreensaver | xlockmore |
xlockmore-gl | gnome-screensaver, dbus-x11, xinput, xdg-user-dirs

We could recommend the use of gnome-screensaver in timekpr though with
a warning for xfce users.
All in all, it seems that in the future, timekpr will have to have 3
different packages to resolve all these dependencies correctly:
timekpr-client-kde, timekpr-client-xfce and timekpr-client-gnome.

But for now, I'll try and read about python dbus server/client, let's
hope I can produce something good. :)

Patrick Turcotte (patrek) wrote :

+1 on this one. Would be much more usefull with time keeping of "active" login time, excluding locked time.

Thanks.

Savvas Radevic (medigeek) wrote :

> +1 on this one. Would be much more usefull with time keeping of "active"
> login time, excluding locked time.

Useful yes, but not safe. :\ Until we figure out a safe way (that the
user can't change), we can't implement this check!

Maybe through the gnome-screensaver process?

$ ps aux | grep gnome-screensaver
forger 28636 0.1 0.1 166348 2156 ? Ss Jun30 1:39
gnome-screensaver

Maybe that "Ss" changes when gnome-screensaver is active?

Even Nedberg (nedberg) wrote :

> Maybe that "Ss" changes when gnome-screensaver is active?

Just tested, no it doesn't.

I agree we should try to fix this, but we will have to make it safe. It is to easy for a kid to fool timekpr if we ask the client. And there is no way (that we know of) for timekpr to get this information without asking the client.

Changed in timekpr:
status: Confirmed → Triaged
Alex S (alex-shillington) wrote :
Download full text (3.1 KiB)

First, thanks to the timekpr team - this is extremely useful. Here is my solution to the 'screen locked/user switched time keeps counting' problem - I hope this will help out someone else. I found the code for an executable named idle at http://coderrr.wordpress.com/2008/04/20/getting-idle-time-in-unix/ - I modified it somewhat to just return the number of seconds of idle time for that display. Here's the C code:

#include <X11/extensions/scrnsaver.h>
#include <stdio.h>

main() {
  XScreenSaverInfo *info = XScreenSaverAllocInfo();
  Display *display = XOpenDisplay(NULL);

  if (display != NULL) {
    XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
    printf("%lu", info->idle/1000);
  } else {
    puts ("error");
  }
}

I compiled that with the command "gcc -o idle idle.c -lXss" and put the resulting binary in /usr/local/sbin/idle. You call that by first exporting the display that you want to query idle time for as $DISPLAY, then you run idle. For example, say that you have users logged in to displays :5 and :6, then you can run the following commands (as root):

export DISPLAY=:5.0;/usr/local/sbin/idle
and
export DISPLAY=:6.0;/usr/local/sbin/idle

to find the number of seconds that those users have been idle (no keyboard/mouse activity.) For timekpr, what we really need to find are the users that are active (the normal users command returns all users that are logged in, active or not.) For this, I wrote a shell script and put that in /usr/local/sbin/active_users.sh - here's the code for that:

#!/bin/bash

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

for i in `w | grep tty | awk '{print $2 $3 ":" $1}' | sed 's/^tty[0-9]*\://g'`; do
    display=`echo $i | awk -F ':' '{print $1}'`
    display=:${display}.0
    user=`echo $i | awk -F ':' '{print $2}'`
    idletime=`export DISPLAY=$display;/usr/local/sbin/idle`
    case "$1" in
        debug)
      echo "$user is on display $display with idletime $idletime"
         ;;
        *)
            if [ $idletime -lt 10 ]; then
      echo -n "$user "
            fi
         ;;
     esac
done
echo

As root, when you run /usr/local/sbin/active_users.sh, you will get only a listing of users that are currently active (in a gnome session only - this doesn't help you are trying to limit time for ssh users...)

Almost done!!!

Now, we have to edit /usr/share/python-support/timekpr/timekpr.py to use /usr/local/sbin/active_users.sh instead of the built in users command. Simply change the line that reads:

u = getcmdoutput('users')
to
u = getcmdoutput('/usr/local/sbin/active_users.sh')

(it's in the getusers function.) Now, just restart the timekpr daemon (/etc/init.d/timekpr restart) and it is ready to go.

I admit, this is a huge hack, but it works for my use case and hopefully can be easily adapted to work for you. There are some problems with this approach - for example, if a time limited user is watching a movie via an app that won't lock the screen, then that user will get extra time. In my case, though, it's all interactive activity so I don't have that problem.

I hope this is useful, and hope even more so that someone i...

Read more...

Savvas Radevic (medigeek) wrote :

> Here is
> my solution to the 'screen locked/user switched time keeps counting'
> problem - I hope this will help out someone else.

Very nice hack/solution indeed. I'll consider it when the time comes. :)

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers