Reputation: 425
I am developing Linux kernel module that communicate with user space program. This module waits for a message which is being sent from user space in order to print it in kernel mode.
This is the module :
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gaston");
MODULE_DESCRIPTION("A simple Linux char driver");
MODULE_VERSION("0.1");
#define MAX 256
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_read(struct file *pfile, char __user *buffer, size_t length, loff_t *offset) {
return 0;
}
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {
if (length > MAX)
return -EINVAL;
if (copy_from_user(message, buffer, length) != 0)
return -EFAULT;
printk(KERN_INFO "Received %s characters from the user\n", message);
return 0;
}
ssize_t exer_close(struct inode *pinode, struct file *pfile) {
printk(KERN_INFO "Device successfully closed\n");
return 0;
}
struct file_operations exer_file_operations = {
.owner = THIS_MODULE,
.open = exer_open,
.read = exer_read,
.write = exer_write,
.release = exer_close,
};
int exer_simple_module_init(void) {
printk(KERN_INFO "Initializing the LKM\n");
register_chrdev(240, "Simple Char Drv", &exer_file_operations);
return 0;
}
void exer_simple_module_exit(void) {
unregister_chrdev(240, "Simple Char Drv");
}
module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);
This is the user-space program :
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
#define BUFFER_LENGTH 256
int main()
{
int ret, fd;
char stringToSend[BUFFER_LENGTH];
fd = open("/dev/char_device", O_RDWR); // Open the device with read/write access
if (fd < 0)
{
perror("Failed to open the device...");
return errno;
}
printf("Type in a short string to send to the kernel module:\n");
scanf("%s", stringToSend); // Read in a string (with spaces)
printf("Writing message to the device [%s].\n", stringToSend);
ret = write(fd, stringToSend, strlen(stringToSend)); // Send the string to the LKM
if (ret < 0)
{
perror("Failed to write the message to the device.");
return errno;
}
return 0;
}
This works fine and the message that I enter from user-space is printed in kernel space.
Now, I am using this example in order to learn kernel thread programming.
I am trying to make the LKM write function as a function that kernel thread has to execute, so I modified my module like this :
#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;
}
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,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);
The problem here, when I compile the module, an error occurs :
/exer_simple_char_drv.c:63:27: error: passing argument 1 of ‘kthread_create_on_node’ from incompatible pointer type [-Werror=incompatible-pointer-types] thread1 = kthread_create(exer_write,NULL,our_thread);
Searching the net, I find that a thread is created by the call to the function :
struct task_struct *kthread_create(int (*function)(void *data), void *data, const char name[], ...)
This function has as first argument the function that the thread has to execute and has as argument (void *data)
, but, my write kernel function has not the same arguments :
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
Is there a way to solve this problem please ? How can I modify my Linux kernel in order to execute the write
function as a thread ?
Thank you.
Upvotes: 0
Views: 925
Reputation: 58858
You can't. Your thread function has to use this format:
int my_thread_function(void *data)
You can call it whatever you want - it doesn't have to be called my_thread_function
- and the parameter doesn't have to be called data
but it does have to be a void *
.
This will not work:
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
I suggest writing a new function to be your thread function:
int exer_write_in_thread(void *data) {
exer_write(???, ???, ???, ???);
return 0;
}
Obviously you have to figure out what arguments you want to call exer_write
with.
Upvotes: 1