I'm trying to send a struct from a LKM to userland, based on this answer: Netlink Sockets in C using the 3.X linux kernel
The code from the answer itself is perfectly compilable, but when I try to send a struct
instead of a char *
, I get segfaults in userland.
This is what I change:
netlinkKernel.c
I add:
typedef struct test{
int a;
char *b;
} s_test;
and replace
char *msg = "Hello from kernel";
---
msg_size = strlen(msg);
---
strncpy(nlmsg_data(nlh),msg,msg_size);
with
s_test x;
x.a = 42;
x.b = "The answer";
---
msg_size(sizeof(x));
---
memcpy(nlmsg_data(nlh), &x, msg_size);
netlinkUser.c
I add the same struct and replace
printf("Received message payload: %s\n", (char *)NLMSG_DATA(nlh));
with
s_test *x = (s_test *)NLMSG_DATA(nlh);
printf("Received message payload: %d - %s\n", x->a, x->b);
Where is the problem?
char *
, not achar [N]
. – Apc