How to call a module's function? - Linux

This is a discussion on How to call a module's function? - Linux ; Hello *! I'm new in module programming, so my apologize if it's a stupid question. I would like to know how to call a function declared within a module from a process. For instance, I have in a module something ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: How to call a module's function?

  1. How to call a module's function?

    Hello *!

    I'm new in module programming, so my apologize if it's a stupid question.

    I would like to know how to call a function declared within a module
    from a process.
    For instance, I have in a module something like:

    static int status;

    static int get_status();
    static void set_status(int status);

    I would like to set and get the status from another program. How to do
    it? Is it through ioctl? then how?

    Well, it's not really obvious how the module interact with the kernel
    and other process. How modules interact each other and so on.

    So if someone can answer my question and give me a quick overview of
    module interaction, then I thank him in advance!

    See you,

    John

  2. Re: How to call a module's function?

    On Fri, 29 Sep 2006 17:46:36 +0200
    John wrote:

    > Hello *!
    >
    > I'm new in module programming, so my apologize if it's a stupid
    > question.
    >
    > I would like to know how to call a function declared within a module
    > from a process.
    > For instance, I have in a module something like:
    >
    > static int status;
    >
    > static int get_status();
    > static void set_status(int status);
    >
    > I would like to set and get the status from another program. How to do
    > it? Is it through ioctl? then how?
    >

    You can't actually call directly those functions from another program as
    they're static functions. Perhaps you can call them indirectly using
    other functions from the module, though. Take a look on the other
    functions within the same .c file.

  3. Re: How to call a module's function?

    John wrote:
    > I'm new in module programming, so my apologize if it's a stupid question.


    > I would like to know how to call a function declared within a module
    > from a process.
    > For instance, I have in a module something like:


    > static int status;


    > static int get_status();
    > static void set_status(int status);


    > I would like to set and get the status from another program. How to do
    > it? Is it through ioctl? then how?


    > Well, it's not really obvious how the module interact with the kernel
    > and other process. How modules interact each other and so on.


    > So if someone can answer my question and give me a quick overview of
    > module interaction, then I thank him in advance!


    Since the function is in a module that becomes part of the kernels
    code once the module has been loaded you can't call it directly
    from some userland process, these can't execute random functions
    within the kernel. Instead, you need to use a well-defined interface
    to the kernel. And the ioctl() system call seems to be what you
    typically would use here.

    (A module may call functions in another module - as long as they
    aren't marked as non-callable from other modules - since all the
    loaded modules are part of the same space, the kernel.)

    In order to be able to use ioctl(() your module first of all needs
    a handler for ioctl() calls, i.e. a function that gets invoked on
    an ioctl() call from userland. In that function you look for some
    unique integral value it gets from the caller, associated with the
    function you want to have executed (i.e. either get_status() or
    set_status()) and than branches to that function accordingly,
    possibly passing on to the function a pointer to some memory in
    user space, also received from the caller (that's where you store
    the argument(s) for the function).

    The address of the modules ioctl handler must be set in the
    'struct file_operations' of the module (called that way because
    the ioctl() sysem call operates on the device file associated
    with the module).

    Then in a header file, which gets included during the compilation
    of the module a well as when your userland program is compiled,
    you also need to define the constants associated with the func-
    tions to be called via an ioctl() call (please note that you
    can't use some random numbers but that there's a set of macros
    to create numbers that then also carry information if the func-
    tions to be called in the end accepts arguments and/or returns
    a value).

    Once you have that infrastructure in place you than can do an
    ioctl() call in your program to have the corresponding function
    executed by the kernel.

    Well, that's a rough "theoretical" overview. For the practical
    part it would probably best to have a look at e.g. the book
    "Linux Device Drivers" by Corbet, Rubini and Kroah-Hartmann
    from O'Reilly, but you also can read the book online at

    http://lwn.net/Kernel/LDD3/

    Especially look for chapter 6 where ioctl is discussed in length.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  4. Re: How to call a module's function?

    Waaahhh!

    What a nice answer! Thanks a lot Jens!

    I'll read the Linux Device Drivers' book.

    Actually, What I'm doing is adding new methods to an existing driver.
    The driver is dedicated to a wifi card and I would like to implement a
    status method and a statistical method. The goal would be to call them
    from an external process...

    Regards,

    John

    Jens Toerring wrote:
    > John wrote:
    >> I'm new in module programming, so my apologize if it's a stupid question.

    >
    >> I would like to know how to call a function declared within a module
    >> from a process.
    >> For instance, I have in a module something like:

    >
    >> static int status;

    >
    >> static int get_status();
    >> static void set_status(int status);

    >
    >> I would like to set and get the status from another program. How to do
    >> it? Is it through ioctl? then how?

    >
    >> Well, it's not really obvious how the module interact with the kernel
    >> and other process. How modules interact each other and so on.

    >
    >> So if someone can answer my question and give me a quick overview of
    >> module interaction, then I thank him in advance!

    >
    > Since the function is in a module that becomes part of the kernels
    > code once the module has been loaded you can't call it directly
    > from some userland process, these can't execute random functions
    > within the kernel. Instead, you need to use a well-defined interface
    > to the kernel. And the ioctl() system call seems to be what you
    > typically would use here.
    >
    > (A module may call functions in another module - as long as they
    > aren't marked as non-callable from other modules - since all the
    > loaded modules are part of the same space, the kernel.)
    >
    > In order to be able to use ioctl(() your module first of all needs
    > a handler for ioctl() calls, i.e. a function that gets invoked on
    > an ioctl() call from userland. In that function you look for some
    > unique integral value it gets from the caller, associated with the
    > function you want to have executed (i.e. either get_status() or
    > set_status()) and than branches to that function accordingly,
    > possibly passing on to the function a pointer to some memory in
    > user space, also received from the caller (that's where you store
    > the argument(s) for the function).
    >
    > The address of the modules ioctl handler must be set in the
    > 'struct file_operations' of the module (called that way because
    > the ioctl() sysem call operates on the device file associated
    > with the module).
    >
    > Then in a header file, which gets included during the compilation
    > of the module a well as when your userland program is compiled,
    > you also need to define the constants associated with the func-
    > tions to be called via an ioctl() call (please note that you
    > can't use some random numbers but that there's a set of macros
    > to create numbers that then also carry information if the func-
    > tions to be called in the end accepts arguments and/or returns
    > a value).
    >
    > Once you have that infrastructure in place you than can do an
    > ioctl() call in your program to have the corresponding function
    > executed by the kernel.
    >
    > Well, that's a rough "theoretical" overview. For the practical
    > part it would probably best to have a look at e.g. the book
    > "Linux Device Drivers" by Corbet, Rubini and Kroah-Hartmann
    > from O'Reilly, but you also can read the book online at
    >
    > http://lwn.net/Kernel/LDD3/
    >
    > Especially look for chapter 6 where ioctl is discussed in length.
    >
    > Regards, Jens
    > --
    > \ Jens Thoms Toerring ___ jt@toerring.de
    > \__________________________ http://toerring.de


  5. Re: How to call a module's function?

    Jens Toerring wrote:
    > John wrote:
    >
    >>I'm new in module programming, so my apologize if it's a stupid question.

    >
    >
    >>I would like to know how to call a function declared within a module
    >>from a process.
    >>For instance, I have in a module something like:

    >
    >
    >>static int status;

    >
    >
    >>static int get_status();
    >>static void set_status(int status);

    >
    >
    >>I would like to set and get the status from another program. How to do
    >>it? Is it through ioctl? then how?

    >
    >
    >>Well, it's not really obvious how the module interact with the kernel
    >>and other process. How modules interact each other and so on.

    >
    >
    >>So if someone can answer my question and give me a quick overview of
    >>module interaction, then I thank him in advance!

    >
    >
    > Since the function is in a module that becomes part of the kernels
    > code once the module has been loaded you can't call it directly
    > from some userland process, these can't execute random functions
    > within the kernel. Instead, you need to use a well-defined interface
    > to the kernel. And the ioctl() system call seems to be what you
    > typically would use here.


    Beg to differ.
    If all John wants to do is get/set a status, an entry in /proc or /sys
    might be a better solution.
    It has the additional advantage of allowing access from script
    languages, e.g. shells. The disadvantage would be that no additional
    information can be passed: failure to "set status" might go undetected.
    However, since "set_status" is void anyway, this would not be a problem.

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  6. Re: How to call a module's function?

    Josef Moellers wrote:
    > Beg to differ.
    > If all John wants to do is get/set a status, an entry in /proc or /sys
    > might be a better solution.


    Definitely, that would be a potentially better alternative and the OP
    should seriously consider doing it that way. I answered under the im-
    pression that the OP was interested in getting some specific functions
    in the module called, not just looking for a way to be able to monitor
    or manipulate some value within the module.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

+ Reply to Thread