我试图根据这个答案将LKM中的结构发送到用户区:Netlink Sockets in C using the 3.X linux kernel
答案本身的代码是完全可编译的,但是当我尝试发送结构而不是char *时,我会在userland中获得段错误.
这是我改变的:
netlinkKernel.c
我加:
typedef struct test{
int a;
char *b;
} s_test;
并替换
char *msg = "Hello from kernel";
---
msg_size = strlen(msg);
---
strncpy(nlmsg_data(nlh),msg,msg_size);
同
s_test x;
x.a = 42;
x.b = "The answer";
---
msg_size(sizeof(x));
---
memcpy(nlmsg_data(nlh),&x,msg_size);
netlinkUser.c
我添加相同的结构和替换
printf("Received message payload: %s\n",(char *)NLMSG_DATA(nlh));
同
s_test *x = (s_test *)NLMSG_DATA(nlh);
printf("Received message payload: %d - %s\n",x->a,x->b);
问题出在哪儿?
最佳答案
原文链接:https://www.f2er.com/linux/439975.html