Callback function from kernel module

Jeshwanth Kumar N K Jeshu jeshkumar555 at gmail.com
Sat Nov 10 03:17:37 EST 2012


Hello Santosh and Anand,

Thanks for the reply and very good suggestions.

As Anand Moon suggestedhttp://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.html

link in http://lists.kernelnewbies.org/pipermail/kernelnewbies/2012-November/006489.html
previous thread,

I read all the kernel to user space communication mechanism ( Except
Upcall  <http://people.ee.ethz.ch/%7Earkeller/linux/multi/kernel_user_space_howto.html#toc7>,
ll read it). As of now for my application,

the signals passing from kernel to user space is enough. I was trying
the sample code provided in  signals section of above link.

1. In the code he used *find_task_by_pid_type, *but
*find_task_by_pid_type *not available in sched.h. So I have changed it
to
*find_task_by_vpid(pid)*, is this right way ?

2. I tried to install the module but it is not finding the symbol, but
the symbol is present in */proc/kallsyms.
*
Anything I am missing while compiling ? as I am getting warning.



*Compile:*

jeshwanth at jeshwanth:~/linux/kernel_user_space/code/signals$ make
make -C /lib/modules/3.0.0-26-generic-pae/build
M=/home/jeshwanth/linux/kernel_user_space/code/signals modules
make[1]: Entering directory `/usr/src/linux-headers-3.0.0-26-generic-pae'
  Building modules, stage 2.
  MODPOST 1 modules
*WARNING:* "find_task_by_vpid"
[/home/jeshwanth/linux/kernel_user_space/code/signals/signal_kernel.ko]
undefined!
make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-26-generic-pae'
[1]+  Done                    gedit signal_kernel.c

*install Command:*
root at jeshwanth:/home/jeshwanth/linux/kernel_user_space/code/signals# insmod
signal_kernel.ko
insmod: error inserting 'signal_kernel.ko': -1 Unknown symbol in module

*/proc/kallsyms:*

00000000 T find_task_by_pid_ns
00000000 T find_task_by_vpid

*dmesg:*
[15012.556740] signal_kernel: Unknown symbol find_task_by_vpid (err 0)

*Code:*
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/siginfo.h>    //siginfo
#include <linux/rcupdate.h>    //rcu_read_lock
#include <linux/sched.h>    //find_task_by_pid_type
#include <linux/debugfs.h>
#include <linux/uaccess.h>


#define SIG_TEST 44    // we choose 44 as our signal number (real-time
signals are in the range of 33 to 64)

struct dentry *file;

static ssize_t write_pid(struct file *file, const char __user *buf,
                                size_t count, loff_t *ppos)
{
    char mybuf[10];
    int pid = 0;
    int ret;
    struct siginfo info;
    struct task_struct *t;
    /* read the value from user space */
    if(count > 10)
        return -EINVAL;
    copy_from_user(mybuf, buf, count);
    sscanf(mybuf, "%d", &pid);
    printk("pid = %d\n", pid);

    /* send the signal */
    memset(&info, 0, sizeof(struct siginfo));
    info.si_signo = SIG_TEST;
    info.si_code = SI_QUEUE;    // this is bit of a trickery: SI_QUEUE is
normally used by sigqueue from user space,
                    // and kernel space should use SI_KERNEL. But if
SI_KERNEL is used the real_time data
                    // is not delivered to the user space signal handler
function.
    info.si_int = 1234;          //real time signals may have 32 bits of
data.

    rcu_read_lock();
    //t = find_task_by_pid_type(PIDTYPE_PID, pid);  //find the task_struct
associated with this pid
    t = find_task_by_vpid(pid);
    if(t == NULL){
        printk("no such pid\n");
        rcu_read_unlock();
        return -ENODEV;
    }
    rcu_read_unlock();
    ret = send_sig_info(SIG_TEST, &info, t);    //send the signal
    if (ret < 0) {
        printk("error sending signal\n");
        return ret;
    }
    return count;
}

static const struct file_operations my_fops = {
    .write = write_pid,
};

static int __init signalexample_module_init(void)
{
    /* we need to know the pid of the user space process
      * -> we use debugfs for this. As soon as a pid is written to
      * this file, a signal is sent to that pid
      */
    /* only root can write to this file (no read) */
    file = debugfs_create_file("signalconfpid", 0200, NULL, NULL, &my_fops);
    return 0;
}
static void __exit signalexample_module_exit(void)
{
    debugfs_remove(file);

}

module_init(signalexample_module_init);
module_exit(signalexample_module_exit);
MODULE_LICENSE("GPL");


On Thu, Nov 8, 2012 at 11:33 AM, SaNtosh kuLkarni <
santosh.yesoptus at gmail.com> wrote:

> I think you are looking for Upcall Functionality
>
> upcall functionality allows a kernel module to invoke a function in user
> space. It is possible to start a program in user space, and give it some
> command line arguments, as well as setting environment variables.
>
>
> **
> int call_usermodehelper  (char * *path*, char ** *argv*, char ** *envp*,
> int *wait*);
>
> Runs a user-space application. The application is started asynchronously
> if wait is not set, and runs as a child of keventd. (ie. it runs with full
> root capabilities).
>
> Must be called from process context. Returns a negative error code if
> program was not execed successfully, or 0.
>
> **man pages
>
> Regards
>
> Santosh
>
>
>
> On Wed, Nov 7, 2012 at 8:30 PM, Bernd Petrovitsch <
> bernd at petrovitsch.priv.at> wrote:
>
>> Hi!
>>
>> On Sam, 2012-11-03 at 19:14 +0530, Jeshwanth Kumar N K Jeshu wrote:
>> [...]
>> > Can I call userspace function from kernel module ? Actually I need to
>> > process some data in user space for every event occured in kernel
>> module.
>>
>> The usual way to implement this with a character device. The userspace
>> application opens the character device. It then read()s the events from
>> it, handles it and write()s results back (if needed).
>> The kernel part handles to IRQs, puts that into a buffer where it waits
>> for the read() from user space.
>>
>> You can use netlink sockets for this which may save some code though or
>> implement a character device directly.
>>
>> Kind regards,
>>         Bernd
>> --
>> Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
>>                      LUGA : http://www.luga.at
>>
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>
>
>
>
> --
> *Regards,
> Santosh*
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>


-- 
Regards
Jeshwanth Kumar N K
+91-7411483498
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20121110/92e1c2d6/attachment.html 


More information about the Kernelnewbies mailing list