问题描述
我正在尝试从用户空间向内核模块发送一个字符串,内核将反向返回的字符串返回。我遵循一个简单的示例code,并在string.h标头中定义的内核代码中添加了strrev()函数。但这给了我一个错误“严重错误:string.h:没有这样的文件或目录”,也没有其他错误。用户代码与链接中的示例代码相同。谁能解释为什么会这样?
用户代码
#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 str[20];
sock_fd = socket(AF_NETLINK,SOCK_RAW,NETLINK_USER);
if (sock_fd < 0)
return -1;
memset(&src_addr,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,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,NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
printf("Enter a string for reversal:- ");
scanf("%s",str);
strcpy(NLMSG_DATA(nlh),str);
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("\nSending message to kernel\n");
sendmsg(sock_fd,&msg,0);
printf("Waiting for message from kernel\n");
/* Read message from kernel */
recvmsg(sock_fd,0);
printf("Received message payload: %s\n",NLMSG_DATA(nlh));
close(sock_fd);
return 0;
}
内核代码
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include<string.h>
#define NETLINK_USER 31
struct sock *nl_sk = NULL;
static void hello_nl_recv_msg(struct sk_buff *skb)
{
struct nlmsghdr *nlh;
int pid;
struct sk_buff *skb_out;
int msg_size;
char msg[20];
int res;
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 */
strcpy(msg,strrev((char *)nlmsg_data(nlh)));
msg_size = strlen(msg);
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,NLMSG_DONE,msg_size,0);
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
strncpy(nlmsg_data(nlh),msg,msg_size);
res = nlmsg_unicast(nl_sk,skb_out,pid);
if (res < 0)
printk(KERN_INFO "Error while sending bak to user\n");
}
static int __init hello_init(void)
{
printk("Entering: %s\n",__FUNCTION__);
//nl_sk = netlink_kernel_create(&init_net,NETLINK_USER,hello_nl_recv_msg,NULL,THIS_MODULE);
struct netlink_kernel_cfg cfg = {
.input = hello_nl_recv_msg,};
nl_sk = netlink_kernel_create(&init_net,&cfg);
if (!nl_sk) {
printk(KERN_ALERT "Error creating socket.\n");
return -10;
}
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_INFO "exiting hello module\n");
netlink_kernel_release(nl_sk);
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
终端窗口
make -C /lib/modules/4.15.0-1096-oem/build/ M=/home/Username/Documents/Task_KtoUcom modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-1096-oem'
CC [M] /home/Username/Documents/Task_KtoUcom/kernl.o
/home/Username/Documents/Task_KtoUcom/kernl.c:5:9: Fatal error: string.h: No such file or directory
#include<string.h>
^~~~~~~~~~
compilation terminated.
scripts/Makefile.build:337: recipe for target '/home/Username/Documents/Task_KtoUcom/kernl.o' Failed
make[2]: *** [/home/Username/Documents/Task_KtoUcom/kernl.o] Error 1
Makefile:1585: recipe for target '_module_/home/Username/Documents/Task_KtoUcom' Failed
make[1]: *** [_module_/home/Username/Documents/Task_KtoUcom] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-1096-oem'
Makefile:4: recipe for target 'all' Failed
make: *** [all] Error 2
解决方法
Linux内核在linux/string.h
标头中提供了字符串函数:
#include <linux/string.h>
故意选择了不同的标头名称:Linux内核<linux/string.h>
提供的一组功能,与标准<string.h>
提供的一组功能有所不同
头文件<linux/string.h>
提供了大多数标准的字符串函数,并替代了其他一些字符串函数。
例如函数strlen
,strcpy
和strncpy
由该标头提供,并具有与C标准中相同的语义。