LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   about system call (https://www.linuxquestions.org/questions/programming-9/about-system-call-87627/)

alchen1999 08-31-2003 11:49 AM

about system call
 
hi, all
i am new in Linux. I'd like to do something by calling system call.
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?
i need your help. thanks

david_ross 08-31-2003 11:51 AM

Welcome to LQ.

If you want to know who is logged it then use who:
who

Or for more info:
who -u

alchen1999 08-31-2003 12:06 PM

thanks, i know the command "who" to get the information.
actually, i want to write c/c++ program in liunx,
but i don't know how to do it? which system call i can use?

david_ross 08-31-2003 12:52 PM

Do you mean you want to run who from a C program?
Code:

#include <stdio.h>

main()
  {
  system ("who");
  }


alchen1999 08-31-2003 01:05 PM

i want to wirte C program instead of who command.
not call who command in c...
when the c program run, it's like who command.
print out all login users.
can i do that in c program?
thanks

david_ross 08-31-2003 01:10 PM

Yes - just download the source for "who" and see exactly how it is done.

alchen1999 08-31-2003 01:23 PM

thank you for your kind reply

kev82 08-31-2003 02:42 PM

http://www.gnu.org/manual/glibc-2.2....nd-Groups.html

Hko 08-31-2003 03:07 PM

Re: about system call
 
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.

shishir 08-31-2003 07:25 PM

yeah ...

infact if you do an strace on who, youll see the program trying to open the file /var/run/utmp, which it reads line by line.

the structure utmp is in file utmp.h:
for your reference :
struct utmp
{
short int ut_type; /* Type of login. */
pid_t ut_pid; /* Process ID of login process. */
char ut_line[UT_LINESIZE]; /* Devicename. */
char ut_id[4]; /* Inittab ID. */
char ut_user[UT_NAMESIZE]; /* Username. */
char ut_host[UT_HOSTSIZE]; /* Hostname for remote login. */
struct exit_status ut_exit; /* Exit status of a process marked
as DEAD_PROCESS. */
long int ut_session; /* Session ID, used for windowing. */
struct timeval ut_tv; /* Time entry was made. */
int32_t ut_addr_v6[4]; /* Internet address of remote host. */
char __unused[20]; /* Reserved for future use. */
};


do man getutent..try to get some hints frm the sample code there in the man page...

hope this has been of some help...

vanquisher 09-01-2003 03:43 AM

I'd suggest a book Advanced programming in UNIX environment by W. Richard Stevens. If you seriously want to do some stuff, get that book. It should help.


All times are GMT -5. The time now is 06:48 AM.