#include <u.h>
#include <libc.h>
enum {
secperday = 60 * 60 * 24
};
void
logevent(char *msg, long t)
{
int fd;
char *fn, *s, *p;
s = ctime(t);
if((p = strchr(s, '\n')) != nil)
*p = 0;
fn = smprint("/usr/%s/lib/activity", getuser());
fd = open(fn, OWRITE);
free(fn);
if(fd == -1)
return;
seek(fd, 0, 2); // should be append anyway
fprint(fd, "%s %s\n", s, msg);
close(fd);
}
void
main(void)
{
Dir *d;
long mday, mouse, day, now;
day = 0;
mday = 0;
while(1){
now = time(nil);
if((d = dirstat("#m/mouse")) == nil)
sysfatal("#m/mouse - cannot stat - %r\n");
mouse = d->atime;
if(day != now/secperday){
if(day && day == mday)
logevent("logout", mouse);
day = now/secperday;
}
if(mday != mouse/secperday){
if(mday)
logevent("login", mouse);
else
logevent("restart", mouse);
mday = mouse/secperday;
}
sleep(60*5*1000);
}
}
|