LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-01-2004, 10:36 PM   #1
appas
Member
 
Registered: Jul 2004
Posts: 72

Rep: Reputation: 15
calling a system function directly from C program


Is it possible to make system calls directly (such sys_open..) from a user application C program.
If yes, could anyone explain how to do this.
 
Old 08-01-2004, 11:32 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
well, yea that's what a system call does.. in a roundabout kinda way. u can't just derefernce the address where sys_open() lives and jump to it tho. in order to move from user space(privilege == 3), to kernel space(privilege == 0), u need to enter thru a call gate. there are limited # of ways to change privilege levels, and for good reason - else it would be hard to run a secure operating system. when u call open(), that is the libC wrapper, which just performs whatever steps are necessary on the given platform to enter a system call. on linux, this involves moving the system call number into EAX, and the args into rest of registers, and then making a software interrupt with int $0x80. the interrupt handler for that slot is invoked, and in system call entry handler the number in EAX is multiplied by the sizeof a pointer, and that number is added to the base address of system call table to find the function pointer for given system call. i dont really understand exactly what it is u want to do tho.. do u mean just not call libC open? in that case, u can use asm and do the work yourself. ie
Code:
movl SYS_CALL_NUM, %eax
/* set up args */
int  $0x80
for a list of call numbers, see /usr/include/asm/unistd.h. u might want to see this as well:
http://www.linuxassembly.org/articles/linasm.html

Last edited by infamous41md; 08-01-2004 at 11:34 PM.
 
Old 08-02-2004, 12:47 AM   #3
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
Thank you for the information,

I would like to explain my scenario in detail. I am writing a proxy file system driver module which intercepts the system call (open, read..) and does my work before calling the original file system call.
Now what i need is to include a function in my proxy file system driver module (kernel space) which can be directly invoked from any c program(user space).
Is there any possible way of doing this.
 
Old 08-02-2004, 01:03 AM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
a couple ways:
1) just register a char driver and use ioctl() calls to do it, for how to do this: http://www.xml.com/ldd/chapter/book/ch05.html#t1
2) add your own system call. i wrote a little tutorial found here:
http://www.1nfamus.netfirms.com/ur_o...calls.txt.html
3) create a /proc entry; and whenever it gets read have your function get called. this is rather dirty and ugly, but it would work. the user app would just open() and read() on the /proc file, and when read() was called u would get to run your function. for how to create proc files, http://www.xml.com/ldd/chapter/book/ch04.html#t2
 
Old 08-02-2004, 01:51 AM   #5
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
I read thro the document and tried to implement the same .

Summary of what i have done.
1. Edited the entry.S and unistd.h as required.
2. Created a new c program which contains the system call to be invoked. ( did not recompile the kernel )
3. Created a c program which invokes the system call. compiled link and executed.

Result :
I am getting errno 38. (Implementation of the function not found)


I have a couple of questions.
1. Is it possible to call the kernel function without recompiling the kernel.
2. Where should (path ) the newly created kernel function be put.
 
Old 08-02-2004, 03:35 AM   #6
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
ahh the joys of insomnia... if u don't recompile the kernel, how could your code changes possibly be noticeable? why don't u just create a char driver and use ioctl() calls; it's simpler and portable. and i don't understand the question #2, unless u mean where do u add the source code?? i wrote that in the paper, u can put it in kernel/sys.c
 
Old 08-02-2004, 04:07 AM   #7
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
Without recompiling the kernel, is it possible to include the kernel function that i want to call from user application (c program)
in a module and dynamically load the module.
Sorry, if the question seems too silly.I hope you understand i am 3 weeks old in c programming
 
Old 08-02-2004, 01:54 PM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
i've already answered your question and provided links as well. MAKE A CHAR DRIVER and just implement open(), close(), and ioctl(). provide some ioctls() that call your function. this way u dont have to recompile kernel. and 3 weeks????!!! whoa, pretty heavy stuff to get into.
 
Old 08-03-2004, 01:50 AM   #9
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
Thanks for your kind efforts for getting me started.
I have started writing char driver and things are moving smooth. I will get back to you incase furthur help required.
 
  


Reply



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
Function pointers, calling, and templates The_Nerd Programming 2 10-09-2005 07:08 PM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
need help with system function in C program veereshai Programming 6 10-26-2004 03:20 PM
Calling my function through MACROS in OpenOfficeWriter mcp_achindra Linux - Software 0 07-05-2004 06:56 AM
C program code calling a Linux system command Linh Programming 1 06-05-2003 01:44 PM

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

All times are GMT -5. The time now is 02:07 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
Open Source Consulting | Domain Registration