c - 如何使用另一个函数的参数调用一个函数?
问题描述
我正在开发 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
?
解决方案
最简单的方法是使用额外的结构。
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);
...
}
推荐阅读
- r - 我的数据集很大,我不知道如何用数据制作数字
- r - 检查 url 是否存在时识别转发
- scala - 如何将 KafkaAvroSerializer 传递给 Kafka ProducerRecord?
- selenium - 如果两个用户同时登录,将在远程 jenkins 服务器上捕获什么 selenium 屏幕截图?
- c# - 使用 IMessageHandlerContext 发送多条消息
- opendaylight - 192.168.56.3 无法联系遥控器
- angular - How to fix solve Expected a 'for-of' loop instead of a 'for' loop with this simple iteration in angular 8
- android - Android UI 测试:如何点击到 Android 对话框?
- shiny - 我可以在服务器上上传 csv 文件,但它没有显示在主面板或闪亮的仪表板主体面板中
- typescript - 打字稿:在接口中使用枚举值