LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-15-2012, 06:11 AM   #1
GNakul
Member
 
Registered: Oct 2011
Location: Bangalore,India
Posts: 59

Rep: Reputation: Disabled
user login logout time


Hi all,

I just want to know how to do the following.

Whenever a user logs in and logs out, the username and the login time and logout time should be written to a file.

Wrote a program to get it from utmp. It is printing something which i am not able to understand.

Thanks in advance for your help
-Nakul
 
Old 05-15-2012, 09:10 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Have you read 'man 5 utmp' and looked at the record structure? .. actually you're probably safer looking at utmp.h.
 
Old 05-15-2012, 10:41 AM   #3
GNakul
Member
 
Registered: Oct 2011
Location: Bangalore,India
Posts: 59

Original Poster
Rep: Reputation: Disabled
@kbp: It is giving something like
Code:
int main(void) {

while((utmp = getutent()) != NULL) {
printf("%s logged in at %d\n", utmp->ut_user, utmp->ut_time);
}
endutent();
return 0;
}

reboot logged in at 1337084828
runlevel logged in at 1337084828
logged in at 1337065065
logged in at 1337065065
logged in at 1337065065
logged in at 1337065065
LOGIN logged in at 1337065065
LOGIN logged in at 1337065065
logged in at 1337065065
logged in at 1337065065
root logged in at 1337065134
root logged in at 1337070419
root logged in at 1337090131
logged in at 1337075180
root logged in at 1337078936
root logged in at 1337090317
root logged in at 1337090333
 
Old 05-15-2012, 08:54 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
If you look at /usr/include/bits/utmp.h you'll see that ut_time is actually defined as ut_tv.tv_sec:

Quote:
/* Backwards compatibility hacks. */
#define ut_name ut_user
#ifndef _NO_UT_TIME
/* We have a problem here: `ut_time' is also used otherwise. Define
_NO_UT_TIME if the compiler complains. */
# define ut_time ut_tv.tv_sec
#endif
#define ut_xtime ut_tv.tv_sec
#define ut_addr ut_addr_v6[0]
So what you're getting is probably the time in seconds since epoch, you'll need to convert it to a date format.

Ideally you'd change your code to use the new value

Last edited by kbp; 05-15-2012 at 08:55 PM.
 
Old 06-05-2012, 07:13 AM   #5
GNakul
Member
 
Registered: Oct 2011
Location: Bangalore,India
Posts: 59

Original Poster
Rep: Reputation: Disabled
Thanks kbp i achived it
tmp = ut->ut_tv.tv_sec;
printf("%s\n",ctime(&tmp));
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script run as user login or logout in Vsftpd server singhpps Linux - Software 4 11-27-2010 12:06 PM
how to get user login time haplo Linux - Newbie 4 02-05-2009 03:01 AM
notification of user login and logout JudyL Programming 9 08-31-2007 11:47 AM
Which files contain individual user login / logout commands andrewstr Red Hat 5 12-17-2003 06:34 PM


All times are GMT -5. The time now is 05:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration