使用netlink机制在内核进程和用户空间进程通信

内核模块代码:

netlink_drv.c



#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <net/sock.h>
#include <net/netlink.h>


#define NETLINK_TEST 21


struct sock *nl_sk = NULL;
EXPORT_SYMBOL_GPL(nl_sk);


void nl_data_ready (struct sk_buff *__skb)
{
struct sk_buff *skb;
struct nlmsghdr *nlh;
   u32 pid;
   int rc;
   int len = NLMSG_SPACE(1200);
   char str[100];


   //printk("net_link: data is ready to read.\n");
   skb = skb_get(__skb);
  
   if (skb->len >= NLMSG_SPACE(0)) {
     nlh = nlmsg_hdr(skb);
     //printk("net_link: recv %s.\n", (char *)NLMSG_DATA(nlh));
        
memset(str, 0, sizeof(str));
memcpy(str,NLMSG_DATA(nlh), sizeof(str)); 
    
pid = nlh->nlmsg_pid; /*pid of sending process */
     //printk("Usrspace sender pid is %d, message is %s \n", pid, (char *)NLMSG_DATA(nlh));
    
kfree_skb(skb);


     skb = alloc_skb(len, GFP_ATOMIC);
     if (!skb){
       printk(KERN_ERR "net_link: allocate failed.\n");
       return;
     }
     nlh = nlmsg_put(skb,0,0,0,1200,0);
     NETLINK_CB(skb).pid = 0; /* from kernel */
        
if (strstr(str, "open")) {
printk("@@@ Kernel receive open command from Userspace then send open to Userspace \n");
memcpy(NLMSG_DATA(nlh), "kernel receive open command", sizeof("kernel receive open command"));
} else if (strstr(str, "close")) {
printk("@@@ Kernel receive close command from Userspace then send close to Userspace \n");
memcpy(NLMSG_DATA(nlh), "kernel receive close command", sizeof("kernel receive close command"));
}
    
     //printk("net_link: going to send.\n");
    
rc = netlink_unicast(nl_sk, skb, pid, MSG_DONTWAIT);
     if (rc < 0) {
       printk(KERN_ERR "net_link: can not unicast skb (%d)\n", rc);
     }
     //printk("net_link: send is ok.\n");


   }
   return;
}


static int test_netlink(void) 
{
   nl_sk = netlink_kernel_create(&init_net, NETLINK_TEST, 0, nl_data_ready, NULL, THIS_MODULE);


   if (!nl_sk) {
     printk(KERN_ERR "net_link: Can not create netlink socket.\n");
     return -EIO;
   }
   printk("net_link: create socket ok.\n");
   return 0;
}


int my_module_init(void)
{
   test_netlink();
   return 0;
}


void my_module_exit(void)
{
   if (nl_sk != NULL){
     sock_release(nl_sk->sk_socket);
   }
   printk("net_link: remove ok.\n");
}


MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alex Xia");
module_init(my_module_init); 
module_exit(my_module_exit);


用户空间应用程序代码:

netlink_app.c

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/socket.h>


#define MAX_PAYLOAD 1024 /* maximum payload size*/
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;


char str[100];


int main(int argc, char* argv[])
{   
char c;
    sock_fd = socket(PF_NETLINK, SOCK_RAW, 21);


nlh=(struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
    
while(1) {
memset(str, 0, sizeof(str));  
memset(&msg, 0, sizeof(msg));

memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
src_addr.nl_groups = 0; /* not in mcast groups */

bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));

memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */


nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid(); /* self pid */
nlh->nlmsg_flags = 0;


   iov.iov_base = (void *)nlh;
        iov.iov_len = nlh->nlmsg_len;
        
msg.msg_name = (void *)&dest_addr;
        msg.msg_namelen = sizeof(dest_addr);
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
        
printf("Please input a char, which o:open, c:close, k:killed, e:exit \n");
c = getchar();
if ('o' == c) {
strcpy(NLMSG_DATA(nlh), "open");
printf("Sending open message ...\n");
sendmsg(sock_fd, &msg, 0);


memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
recvmsg(sock_fd, &msg, 0);
printf("Received message payload: %s\n",NLMSG_DATA(nlh));
memcpy(str, NLMSG_DATA(nlh), sizeof(str));
if (strstr(str, "please kill your process")) {
printf("<<< Userspace receive kill process message from kernel, please input k \n");
}
} else if ('c' == c) {
printf("Sending close message ...\n");
   strcpy(NLMSG_DATA(nlh), "close");
sendmsg(sock_fd, &msg, 0);
printf("Close return to userspace \n");
} else if ('k' == c) {
   printf("Sending killed message ...\n");
strcpy(NLMSG_DATA(nlh), "killed");
sendmsg(sock_fd, &msg, 0);
printf("Killed return to userspace \n");
} else if ('e' == c) {
break;
}
}
        
/* Close Netlink Socket */
    close(sock_fd);
}



如此可以再内核进程和用户空间进程间直接实现进程通信。

你可能感兴趣的:(struct,socket,Module,command,kill,input)