Skip to contentSkip to conversation

Help !! calling function in module from a user program

DISCUSSION Sudharsan Vijayaraghavan 4 participants 5 messages Feb 18, 2003

lore
Share & tools
lore
#0 lore raw

Hi,

Am a new bee to linux internals.
I am trying to make a simple program witch will call a function from a module. I made a module compiled it and INSMOD-it into kernel, that works fine. I would like to call from my user program a function defined in my kernel module.

Please suggest any method thro' which this could be accomplished.
The only way i did it was by running my new module as insmod mymodule.o and get my job done.

Thanks,
Sudharsan.

#1 lore raw
Quoted 7 lines · Sudharsan Vijayaraghavan

Hi,

Am a new bee to linux internals.
I am trying to make a simple program witch will call a function from a module. I made a module compiled it and INSMOD-it into kernel, that works fine. I would like to call from my user program a function defined in my kernel module.

You never call functions directly from user-mode. You generally use a syscall to interface between user-mode and kernel-mode. Most drivers implement operations that map via these syscalls (i.e. open/close/read/write etc).

What exactly are you trying to achieve?

Signature · 4 lines
Alex, homepage: http://www.bennee.com/~alex/

How much does it cost to entice a dope-smoking UNIX system guru to Dayton?
		-- Brian Boyle, UNIX/WORLD's First Annual Salary Survey
#2 lore raw
In reply to Sudharsan Vijayaraghavan

Hi,
One way of doing this is , by hooking up your function inside the module as a system call.
Here i'm sending two files, module.c and user_space.c. first do "insmod module.o" and then run "./user_space".
As i'm also a newbee, i'm not aware of the disadvantages of this approach.

thanks,
Srinivasu Chinta.

--- Sudharsan Vijayaraghavan <[email protected]>

Quoted 20 lines · wrote:

Hi,

Am a new bee to linux internals.
I am trying to make a simple program witch will call a function from a
module. I made a module compiled it and INSMOD-it into kernel, that works
fine. I would like to call from my user program a function defined in my
kernel module.

Please suggest any method thro' which this could be accomplished.
The only way i did it was by running my new module as insmod mymodule.o and
get my job done.

Thanks,
Sudharsan.

Quoted 7 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

#define MODULE
#define __KERNEL__
#include <linux/module.h>
#include <linux/kernel.h>

extern void *sys_call_table[];
void * org_func;

static void my_func()

{
        printk("Executing my_func...!");
}
int init_module(void)
{
        printk("init_module ...!");
        org_func = sys_call_table[250];
        sys_call_table[250] = my_func;
        return 0;
}
void cleanup_module()
{
        sys_call_table[250] = org_func;
        printk("cleaning up...!");
}

#include <stdio.h>
#include <errno.h>
#include <asm/unistd.h>
#define __NR_my_func 250

_syscall0(void, my_func);

main()

{
        my_func();
}
#3 lore raw
Quoted 8 lines · Srinivas Chinta

Hi,
One way of doing this is , by hooking up your function inside the module as a system call.
Here i'm sending two files, module.c and user_space.c. first do "insmod module.o" and then run "./user_space".
As i'm also a newbee, i'm not aware of the disadvantages of this approach.

The main disadvantage is your driver/module becomes a specialized device that can only be accessed by special syscall incantaion by one application. The Un*x way is about keeping every thing as standard as possible so there is easy interaction between user-mode programs accessing the device even if they don't know the details of the internals.

Most devices can be treated as char devices or block devices and are coded as such. This way you can dump the contents of your hard-drive by doing "cat /dev/hda1 > dump" without cat having intimate knowledge of what a hard drive is and how to access one.

Of course there will always be times when new system calls need to be added but generally this should be done sparinginly.

Signature · 4 lines
Alex, homepage: http://www.bennee.com/~alex/

He missed an invaluable opportunity to hold his tongue.
		-- Andrew Lang
#4 lore raw
In reply to Sudharsan Vijayaraghavan
Quoted 8 lines · Sudharsan Vijayaraghavan

Hi,

Am a new bee to linux internals.
I am trying to make a simple program witch will call a function from a module. I made a module compiled it and INSMOD-it into kernel, that works fine. I would like to call from my user program a function defined in my kernel module.

Quoted 6 lines

Please suggest any method thro' which this could be accomplished. The only way i did it was by running my new module as insmod mymodule.o and get my job done.

Thanks,
Sudharsan.

Unix/Linux uses open() close() read() write() and ioctl() (plus a few others) to interface with modules or any kind of driver. To 'call' some module function from user-mode, you impliment open() and close(). That will provide a file-descriptor for subsequent operations. Then you impliment either read() write() or ioctl() or all, whichever is most appropriate for the function your module is going to provide.

You never 'call' a kernel function directly from user-mode. One of the kernel's primary functions is to make this impossible. Kernel code is protected from direct user-mode access.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). Why is the government concerned about the lunatic fringe? Think about it.