我的实际调用语句如下,倒数第2个参数是NULL
nl_sk = netlink_kernel_create(NETLINK_TEST, 0, nl_data_ready, NULL, THIS_MODULE);
另外,在仍然有此警告的情况下,内核模块和用户态程序之间的通信过程成功了,在此我将代码完整贴出来,供大家参考,并请斑竹帮我解决刚才那个警告问题
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7
8 #define NETLINK_TEST 17
9 #define MAX_PAYLOAD 1024
10
11 struct sock *nl_sk = NULL;
12
13 void nl_data_ready (struct sock *sk, int len)
14 {
15 wake_up_interruptible(sk->sk_sleep);
16 }
17
18 void netlink_test()
19 {
20 struct sk_buff *skb = NULL;
21 struct nlmsghdr *nlh = NULL;
22 int err;
23 unsigned long pid;
24
25 nl_sk = netlink_kernel_create(NETLINK_TEST, 0, nl_data_ready, NULL, THIS_MODULE);
26 /* wait for message coming down from user-space */
27 skb = skb_recv_datagram(nl_sk, 0, 0, &err);
28
29 nlh = (struct nlmsghdr *)skb->data;
30 printk("%s: received netlink message payload:%s\n",
31 __FUNCTION__, (char *)NLMSG_DATA(nlh));
32
33 pid = nlh->nlmsg_pid; /*pid of sending process */
34 NETLINK_CB(skb).pid = pid;
35 NETLINK_CB(skb).dst_group = 0; /* unicast */
36 netlink_unicast(nl_sk, skb, pid, MSG_DONTWAIT);
37 sock_release(nl_sk->sk_socket);
38 }
39
40 static int __init hello_start(void)
41 {
42 printk("Loading netlink demo module...\n");
43
44 netlink_test();
45 return 0;
46 }
47
48 static void __exit hello_end(void)
49 {
50 printk("Exiting netlink demo module...\n");
51 }
52
53 module_init(hello_start);
54 module_exit(hello_end);
用户态程序:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7
8 #define NETLINK_TEST 17
9 #define MAX_PAYLOAD 1024 /* maximum payload size*/
10
11 struct sockaddr_nl src_addr, dest_addr;
12 struct nlmsghdr *nlh = NULL;
13 struct iovec iov;
14 struct msghdr msg;
15 int sock_fd;
16
17 int main() {
18 sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_TEST);
19
20 memset(&src_addr, 0, sizeof(src_addr));
21 src_addr.nl_family = AF_NETLINK;
22 src_addr.nl_pid = getpid(); /* self pid */
23 src_addr.nl_groups = 0; /* not in mcast groups */
24 bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
25
26 memset(&dest_addr, 0, sizeof(dest_addr));
27 dest_addr.nl_family = AF_NETLINK;
28 dest_addr.nl_pid = 0; /* For Linux Kernel */
29 dest_addr.nl_groups = 0; /* unicast */
30
31 nlh=(struct nlmsghdr *)malloc(
32 NLMSG_SPACE(MAX_PAYLOAD));
33 /* Fill the netlink message header */
34 nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
35 nlh->nlmsg_pid = getpid(); /* self pid */
36 nlh->nlmsg_flags = 0;
37 /* Fill in the netlink message payload */
38 strcpy(NLMSG_DATA(nlh), "Hello you!");
39
40 iov.iov_base = (void *)nlh;
41 iov.iov_len = nlh->nlmsg_len;
42 msg.msg_name = (void *)&dest_addr;
43 msg.msg_namelen = sizeof(dest_addr);
44 msg.msg_iov = &iov;
45 msg.msg_iovlen = 1;
46
47 sendmsg(sock_fd, &msg, 0);
48
49 /* Read message from kernel */
50 memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
51 recvmsg(sock_fd, &msg, 0);
52 printf(" Received message payload: %s\n", NLMSG_DATA(nlh));
53
54 /* Close Netlink Socket */
55 close(sock_fd);
56
57 return 0;
58 }
Makefile:
1 obj-m = netlink_k.o
2 KVERSION = $(shell uname -r)
3 all:
4 make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
5 gcc netlink_u.c -o netlink_u
6 clean:
7 make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
阅读(875) | 评论(0) | 转发(0) |