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 |
Welcome to LQ.
If you want to know who is logged it then use who: who Or for more info: who -u |
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? |
Do you mean you want to run who from a C program?
Code:
#include <stdio.h> |
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 |
Yes - just download the source for "who" and see exactly how it is done.
|
thank you for your kind reply
|
|
Re: about system call
Quote:
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 (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. |
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... |
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. |