如何从另一个函数调用带有其参数的函数?

时间:2019-11-13 20:17:40

标签: c linux function linux-kernel arguments

我正在开发Linux内核模块。我想调用模块exer_write函数:

exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)

来自另一个名为exer_write_in_thread的函数:

int exer_write_in_thread(void *data)

这是模块:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>  
#include <linux/kernel.h>
#include <linux/uaccess.h>
#include <linux/kthread.h>

MODULE_LICENSE("GPL");      
MODULE_AUTHOR("Gaston");  
MODULE_DESCRIPTION("A simple Linux char driver"); 
MODULE_VERSION("0.1"); 

#define MAX 256
static struct task_struct *thread1;
static char message[MAX] ="";      ///< Memory for the string that is passed from userspace


ssize_t exer_open(struct inode *pinode, struct file *pfile) {

    printk(KERN_INFO "Device has been opened\n");
    return 0;
}


ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {

    if (length > MAX)
        return -EINVAL;

    printk("Thread_fuction is running ...\n");

    if (copy_from_user(message, buffer, length) != 0)
        return -EFAULT;

    printk(KERN_INFO "Received this message : %s ,from the user\n", message);

    return 0;

}   


int exer_write_in_thread(void *data) {
    exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset);
    return 0;
}


struct file_operations exer_file_operations = { 
    .owner = THIS_MODULE,
    .open = exer_open,
    .write = exer_write,    
};


int exer_simple_module_init(void) {

    char our_thread[8]="thread1";

    printk(KERN_INFO "Initializing the LKM\n");
    register_chrdev(240, "Simple Char Drv", &exer_file_operations);

    thread1 = kthread_create(exer_write_in_thread,NULL,our_thread);
    if((thread1))
            {
                printk(KERN_INFO "Thread is created");
                wake_up_process(thread1);
            }

    return 0;
}



void exer_simple_module_exit(void) {

    int ret;    

    unregister_chrdev(240, "Simple Char Drv");

    ret = kthread_stop(thread1);
    if(!ret)
        printk(KERN_INFO "Thread stopped");
}


module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);

我的问题是调用函数exer_write的参数。

如何使用exer_write_in_thread中的函数调用这些参数?

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用其他结构。

struct exr_write_args_s {
    struct file *pfile;
    const char __user *buffer;
    size_t length;
    loff_t *offset;
};

int exer_write_in_thread(void *data) {

    struct exr_write_args_s *const args = data;
    exer_write(args->pfile, args->buffer, args->length, args->offset);
    return 0;
}

int exer_simple_module_init(void) {
...
    struct exr_write_args_s args = {0/*FIXME: init it!*/};
...
    thread1 = kthread_create(exer_write_in_thread, &args, our_thread);
...
}