Quote:
Originally posted by alchen1999
But I don't know how and where I can find the related information. Such as I want to get the current login users information?
|
As you may already know, the 'login' program maintaines a set of files: /var/run/utmp and /var/log/wtmp (type "man 1 login" in a terminal to get more info about the login program). The "who" program uses these files to get its infomration from.
So you need some system calls to get information from these files, which are in a specific format to resemble a simple database.
One way to find out which functions there are to do this:
(1) We want to find out about users logged in, so "login" may be good keyword to search for.
Type: "man -k login". This gives a list of man pages containing the word "login" in their description. ( "man -k <keyword>" is the same as "apropos <keyword>", also see "man man" for more info about the "man" command.)
Code:
shell$ man -k login
[..snip..]
utmp (5) - login records
wtmp (5) - login records
(2) So we found out "utmp" may have more info for us.
Type: man 5 utmp. This gives info about the file-format, which header to #include to use structures and #define's for reading the file. We could use this to read the files directly to find out about the users currently logged in. But our program then may not have read-access to, for it is a system-maintained file. Also there may be an aesier way.
(3) At the bottom of "man 5 utmp" there is a section
"See also".
Trying each one of these, or, based on experience, ignore the ones that won't tell us what we are looking for, we find that
man 3 getutent will tell us more about accessing the files that the system ("login") maintains about users logged in. In this case it even has an example program.
Of course there are other ways in Linux to search for a system-call: googling may help. Typing "info libc" provides access to a lot of information about the C-library. (type: "info info" to read more about how the "info" program iself works, or for a primer, just type "info", then press 'h')
Hope this helps.