Communication between Linux kernel processes and user processes 1. Use sockopt to exchange data with the kernel

Source: Internet
Author: User
Tags dmesg
The Linux Kernel provides the copy_from_user ()/copy_to_user () function to copy the kernel state and user State data. However, these two functions cause blocking and cannot be used in hard or soft interruptions. These two special copy functions are generally used in functions similar to system calls.

In the following code, the kernel module registers a set of functions for setting SOCKET options so that the user space process can call this group of functions to read and write kernel state data.

The operation steps and Source Code are as follows:

Header file: imp1.h

/* Imp1.h */
# Ifndef _ imp1_h __
# DEFINE _ imp1_h __

# Define impw.ops_basic 128
# Define imp1_set imp1_ops_basic
# Define imp1_get impw.ops_basic
# Define imp1_max imp1_ops_basic + 1

# Endif

Module:

Compile: gcc-C-d1_kernel _-dmodule imp1_k.c
View module output: Method 1: dmesg

Method 2: tail-F/var/log/messages

/* Imp1_k.c */
# Ifndef _ KERNEL __
# DEFINE _ KERNEL __
# Endif

# Ifndef Module
# Define Module
# Endif

# Include <Linux/module. h>
# Include <Linux/kernel. h>
# Include <Linux/types. h>
# Include <Linux/string. h>
# Include <Linux/netfilter_00004.h>
# Include <Linux/netfilter. h>
# Include <Linux/init. h>
# Include <ASM/uaccess. h>
# Include "imp1.h"

# Define kmsg "A message from kernel/N"
# Define kmsg_len sizeof ("A message from kernel/N ")

Static int data_to_kernel (struct sock * SK, int cmd, void * user,
Unsigned int Len)
{
Switch (CMD)
{
Case imp;set:
{
Char umsg [64];
Memset (umsg, 0, sizeof (char) * 64 );
Copy_from_user (umsg, user, sizeof (char) * 64 );
Printk ("umsg: % s", umsg );
}
Break;
}
Return 0;
}

Static int data_from_kernel (struct sock * SK, int cmd, void * user, int * Len)
{
Switch (CMD)
{
Case imp;get:
{
Copy_to_user (user, kmsg, kmsg_len );
}
Break;
}
Return 0;
}

Static struct nf_sockopt_ops imp1_sockops =
{
. PF = pf_inet,
. Set_optmin = imp1_set,
. Set_optmax = imp1_max,
. Set = data_to_kernel,
. Get_optmin = imp1_get,
. Get_optmax = imp1_max,
. Get = data_from_kernel,
};

Static int _ init Init (void)
{
Return nf_register_sockopt (& imp1_sockops );
}

Static void _ exit Fini (void)
{
Nf_unregister_sockopt (& imp1_sockops );
}

Module_init (init );
Module_exit (fini );

 

 

User Testing Program:

Compile: gcc-o User imp1_u.c

/* Imp1_u.c */
# Include <unistd. h>
# Include <stdio. h>
# Include <sys/socket. h>
# Include <Linux/in. h>
# Include "imp1.h"

# Define umsg "A message from userspace/N"
# Define umsg_len sizeof ("A message from userspace/N ")

Char kmsg [64];

Int main (void)
{
Int sockfd;
Int Len;

Sockfd = socket (af_inet, sock_raw, ipproto_raw );
If (sockfd <0)
{
Printf ("can not create a socket/N ");
Return-1;
}

/* Call function data_to_kernel ()*/
Setsockopt (sockfd, ipproto_ip, imp1_set, umsg, umsg_len );

Len = sizeof (char) * 64;

/* Call function data_from_kernel ()*/
Getsockopt (sockfd, ipproto_ip, imp1_get, kmsg, & Len );
Printf ("kmsg: % s", kmsg );

Close (sockfd );
Return 0;
}

 

 

Use insmod to load a module

Use rmmod to delete a module

Run lsmod to check whether the load is successful.

Dmesg:

Umsg: a message from userspace
Umsg: a message from userspace

[Root @ localhost ipc_sockopt] #./user
Kmsg: a message from Kernel
[Root @ localhost ipc_sockopt] #./user
Kmsg: a message from Kernel

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.