LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2003, 11:49 AM   #1
alchen1999
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Rep: Reputation: 0
Unhappy 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
 
Old 08-31-2003, 11:51 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Welcome to LQ.

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

Or for more info:
who -u
 
Old 08-31-2003, 12:06 PM   #3
alchen1999
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0
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?
 
Old 08-31-2003, 12:52 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Do you mean you want to run who from a C program?
Code:
#include <stdio.h>

main()
   {
   system ("who");
   }
 
Old 08-31-2003, 01:05 PM   #5
alchen1999
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 08-31-2003, 01:10 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Yes - just download the source for "who" and see exactly how it is done.
 
Old 08-31-2003, 01:23 PM   #7
alchen1999
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0
thank you for your kind reply
 
Old 08-31-2003, 02:42 PM   #8
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
http://www.gnu.org/manual/glibc-2.2....nd-Groups.html
 
Old 08-31-2003, 03:07 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
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.
 
Old 08-31-2003, 07:25 PM   #10
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
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...
 
Old 09-01-2003, 03:43 AM   #11
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
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.
 
  


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
using system call ej25 Programming 9 11-30-2004 11:45 AM
new system call soul2 Linux - General 1 11-03-2004 02:41 PM
Is it possible to use system() and get the return value from the system call newguy21 Programming 1 08-11-2004 01:37 PM
System call dami Linux - General 0 11-18-2003 11:18 AM
call for system date in c mr.moto Programming 2 09-30-2002 03:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:54 AM.

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