Error while using netlink sockets to communicate between user space application and character device in Linux kernel

StackOverflow https://stackoverflow.com/questions/10170988

Question

I have written a kernel driver "hello_kernel" which registers as a character device on a custom node /dev/hello. In the hello_kernel.c file, after calling register_chrdev(), I tried to create a netlink socket using netlink_kernel_create() ( defined in netlink.h ) with a properly defined function to receive user space messages through this socket.

The code compiled perfectly and I made the device nod /dev/hello using mknod; but when I try to insmod this module ( hello_kernel.ko ), there is an error:

insmod: error inserting 'hello_kernel.ko': -1 No child processes

The dmesg command shows that there was a an error while socket creation, i.e with netlink_kernel_create().

Initially, I was trying to use this module to connect with a user space application solely suing a netlink socket which worked perfectly. My motivation for adding character device functionality was to enable another user application to open it like a regular device file and issue ioctl calls.

What can I do to make this work ? Or does the kernel not support merging both functionalities ?

Code given below:

result = register_chrdev(major_num, "hello", &hello_fops);
if( result < 0 )
{
 printk( KERN_INFO "Could not init hello_kernel" );
 return result;
} 
result=netlink_kernel_create(&init_net, NETLINK_USER, 0, hello_nl_recv_msg,NULL,
THIS_MODULE);
if(!result)
{
 unregister_chrdev(major_num, "memory");
 printk(KERN_ALERT "Error creating socket.\n");
 return -10;
}

Trying to insert module:

$sudo insmod hello_kernel.ko
insmod: error inserting 'hello_kernel.ko': -1 No child processes

Output of dmesg:

[ 1537.809491] Error creating socket.
Was it helpful?

Solution

The problem was with socket creation after registering the device. Now, I create the socket (netlink_kernel_create)before the device registration(register_chrdev) and after rebooting, it works :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top