LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
LinkBack Search this Thread
Old 11-26-2007, 12:32 AM   #1
SungWon Chung
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Rep: Reputation: 0
How to add a system call in 2.6.x kernel


Hi. I have a problem about adding my own system call.

My system is fedora core 2.6.19.

I did all phases which should be done to add system call.

And I recompiled my kernel.

But I met a problem making an application program.

For using my system call, I wrote codes like this.

==========================================================================================
_syscall3(long, insert_iphash_entry, const uint32_t, category, const uint32_t, ip, const uint32_t, action);
_syscall3(long, modify_iphash_entry, const uint32_t, category, const uint32_t, ip, const uint32_t, action);
_syscall2(long, remove_iphash_entry, const uint32_t, category, const uint32_t, ip);
_syscall1(long, flush_iphash_bucket, const uint32_t, category);
==========================================================================================

Compiling this codes, error was occured like this.

iphash.c:37: error: expected declaration specifiers or '...' before 'insert_ipha sh_entry'
iphash.c:37: error: expected declaration specifiers or '...' before 'category'
iphash.c:37: error: expected declaration specifiers or '...' before 'ip'
iphash.c:37: error: expected declaration specifiers or '...' before 'action'
iphash.c:37: warning: data definition has no type or storage class
iphash.c:37: warning: type defaults to 'int' in declaration of '_syscall3'

I found the reason.

unistd.h which is in my system doesn't have a define like _syscall0, _syscall1 ...

What should I do?

Is this problem related with glibc version?

I don't know what I do.

Please help me.

Last edited by SungWon Chung; 11-26-2007 at 12:35 AM.
 
Old 11-26-2007, 04:53 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 64
Quote:
Originally Posted by SungWon Chung View Post
I did all phases which should be done to add system call.

And I recompiled my kernel.
What exactly were the steps you used?
Quote:
Originally Posted by SungWon Chung View Post
For using my system call, I wrote codes like this.

==========================================================================================
_syscall3(long, insert_iphash_entry, const uint32_t, category, const uint32_t, ip, const uint32_t, action);
_syscall3(long, modify_iphash_entry, const uint32_t, category, const uint32_t, ip, const uint32_t, action);
_syscall2(long, remove_iphash_entry, const uint32_t, category, const uint32_t, ip);
_syscall1(long, flush_iphash_bucket, const uint32_t, category);
==========================================================================================
Well, that’s your problem… There are much better ways to use syscalls from C programs. There is a nice wrapper function called syscall() which comes with glibc (for more information, see “man 2 syscall”). For example (if you use glibc), try something like this for a header file:
Code:
#ifndef _MY_HEADER_H
#define _MY_HEADER_H

#include <sys/syscall.h>
#include <unistd.h>

#define __NR_insert_iphash_entry 300
#define __NR_modify_iphash_entry 301
#define __NR_remove_iphash_entry 302
#define __NR_flush_iphash_bucket 303

static inline int insert_iphash_entry(uint32_t category, uint32_t ip, uint32_t action)
{
	return syscall(__NR_insert_iphash_entry, category, ip, action);
}

static inline int modify_iphash_entry(uint32_t category, uint32_t ip, uint32_t action)
{
	return syscall(__NR_modify_iphash_entry, category, ip, action);
}

static inline int remove_iphash_entry(uint32_t category, uint32_t ip)
{
	return syscall(__NR_remove_iphash_entry, category, ip);
}

static inline int flush_iphash_bucket(uint32_t category)
{
	return syscall(__NR_flush_iphash_bucket, category);
}

#endif /* _MY_HEADER_H */
A few comments:
  1. Obviously, the #defines are supposed to be the system call numbers themselves. If in implementing your system call, you already changed /usr/include/asm/unistd.h then you don’t need to replicate the #defines (in fact if you do, an error should be thrown). If you didn’t do this (or are distributing your program so that editing a system file would require too much permission), you’ll need to duplicate the macros. In fact, you might add some architecture-specific preprocessing since syscall numbers are arch-specific.
  2. You don’t need to const-qualify the function arguments, since in C all arguments are passed by value.
  3. Whereas in kernelspace, your system calls usually return long, in userspace they are interpreted as ints.
  4. The wrapper function will automatically take care of errno for you. I.e., in case of a negative return value from the actual system call, the wrapper will return -1 and set errno accordingly.
  5. Once you include a file with the above code, you may use the functions as if they were C functions instead of direct system calls.
 
Old 11-26-2007, 09:32 PM   #3
SungWon Chung
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up thank you so much

Because of your help, I solved it completely.

I appreciate it.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add system call rakesh_roy25 Linux - General 3 09-14-2007 11:20 AM
I want add my own system call using LKM (loadable kernel module)... raga4223688 Linux - Software 1 03-07-2007 07:21 AM
add new system call linux_lover2005 Programming 2 10-14-2006 01:37 AM
Add New System Call in RH9 kernel-2.4.20-8 kamal_h_mehta Programming 0 04-08-2006 08:51 AM
how to add another system call in to the kernel?? Pratik H Pandya Programming 1 03-26-2006 02:02 AM


All times are GMT -5. The time now is 12:31 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration