我正在尝试根据以下答案将结构从 LKM 发送到用户区: Netlink Sockets in C using the 3.X linux kernel
答案本身的代码是完全可编译的,但是当我尝试发送struct
而不是 achar *
时,我在用户空间中遇到了段错误。
这是我改变的:
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);
问题出在哪里?