借助内核提供的 netlink 机制可以实现内核模块与用户程序之间的通信,逻辑相对比较简单,以下代码摘抄自 stackoverflow 上的一个回答,并做了部分改动。
先看内核模块的代码:
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include <linux/sched.h>
#define NETLINK_USER 31
struct sock *nl_sk = NULL;
extern void flush_tlb_all(void);
static void mymodule_nl_recv_msg(struct sk_buff *skb)
{
struct nlmsghdr *nlh;
int pid;
struct sk_buff *skb_out;
int msg_size;
int res;
struct task_struct *task;
printk(KERN_INFO "Entering: %s\n", __FUNCTION__);
nlh = (struct nlmsghdr *)skb->data;
printk(KERN_INFO "Netlink received msg payload: %s\n", (char *)nlmsg_data(nlh));
pid = nlh->nlmsg_pid; /*pid of sending process */
msg_size = 8;
skb_out = nlmsg_new(msg_size, 0);
if (!skb_out) {
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0);
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
task = pid_task(find_vpid(pid), PIDTYPE_PID);
sprintf(nlmsg_data(nlh), "%d", task->tgid);
res = nlmsg_unicast(nl_sk, skb_out, pid);
if (res < 0)
printk(KERN_INFO "Error while sending bak to user\n");
}
static int __init mymodule_init(void)
{
struct netlink_kernel_cfg cfg = {
.input = mymodule_nl_recv_msg,
};
printk("Entering: %s\n", __FUNCTION__);
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if (!nl_sk) {
printk(KERN_ALERT "Error creating socket.\n");
return -10;
}
return 0;
}
static void __exit mymodule_exit(void)
{
printk(KERN_INFO "exiting mymodule module\n");
netlink_kernel_release(nl_sk);
}
module_init(mymodule_init); module_exit(mymodule_exit);
MODULE_LICENSE("GPL");
Makefile:
obj-m := mymodule.o
KDIR := /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.order
用户侧代码:
#include <linux/netlink.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define NETLINK_USER 31
#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;
int main()
{
char buf[16];
sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
if (sock_fd < 0)
return -1;
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
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 = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
printf("My pid is: %d\n", getpid());
sprintf(buf, "%d", getpid());
strcpy(NLMSG_DATA(nlh), buf);
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("Sending message to kernel\n");
sendmsg(sock_fd, &msg, 0);
printf("Waiting for message from kernel\n");
/* Read message from kernel */
recvmsg(sock_fd, &msg, 0);
printf("Received message payload: %s\n", NLMSG_DATA(nlh));
if (!strcmp(buf, NLMSG_DATA(nlh))) {
printf("communicate successfully!\n");
} else {
printf("communicate failed!\n");
}
close(sock_fd);
}
安装模块(略)
运行用户程序:
$ gcc -o main ./main.c
$ ./main
My pid is: 3987
Sending message to kernel
Waiting for message from kernel
Received message payload: 3987
communicate successfully!
查看模块输出:
$ dmesg |tail
...
[ 2778.165682] Entering: mymodule_init
[ 2808.253696] Entering: mymodule_nl_recv_msg
[ 2808.253698] Netlink received msg payload: 3987