I have written a kernel module and userspace program which use a netlink socket to communicate (full source here: https://github.com/akshayknarayan/netlink-test ). The kernel module periodically multicasts a message, and the userspace program listens on the socket.
Relevant source snippets:
Kernel module:
43 res = nlmsg_multicast(
44 nl_sk, // @sk: netlink socket to spread messages to
45 skb_out, // @skb: netlink message as socket buffer
46 0, // @portid: own netlink portid to avoid sending to yourself
47 MYMGRP, // @group: multicast group id
48 GFP_KERNEL // @flags: allocation flags
49 );
50 if (res < 0) {
51 printk(KERN_INFO "Error while sending to user: %d\n", res);
52 } else {
53 mod_timer(&timer, jiffies + msecs_to_jiffies(1));
54 printk(KERN_INFO "Send ok\n");
55 }
Userspace program:
39 void nl_recv(int sock) {
...
54 ret = recvmsg(sock, &msg, 0);
55 if (ret < 0)
56 printf("ret < 0.\n");
57 else
58 printf("received message payload: %s\n", (char*) NLMSG_DATA((struct nlmsghdr *) &buffer));
59 }
60
61 int main(int argc, char *argv[]) {
...
69 while (1) {
70 nl_recv(nls);
71 sleep(1);
72 count++;
73 }
If the call to sleep is removed, there is no issue. However, with the call, on kernel 4.10 the kernel hangs after the kernel module is loaded. There's no relevant information in /var/log/syslog after reboot.
It seems that some netlink buffer is getting filled, but in this case I would expect nlmsg_multicast to return ENOBUFS.
Thanks,
Akshay
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@xxxxxxxxxxxxxxxxx https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies