简体   繁体   中英

Keep Netlink Socket open on kernel and User side

After I read some articles on netlink sockets, I tried to adopt this answer here Netlink SO .

I added an endless loop to the userspace program:

while (1) {
  memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));

  iov.iov_base = (void *)nlh;
  iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
  msg.msg_name = (void *)&dest_addr;
  msg.msg_namelen = sizeof(dest_addr);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;

  ret = recvmsg(sock_fd, &msg, 0);
  if (ret == 0) {
    printf("Exit.\n");
    exit(0);
  }
  else if (ret == -1) {
    perror("recvmsg:");
    exit(1);
  }

  printf("%s", NLMSG_DATA(nlh));
}

To get more messages (the program in the linked answer stops after one received message).

I also added a timer in my kernel module, that sends a message every 60 seconds from the kernel module to the userspace program (the next step would be: sending an answer back). This works, since I am not only sending the messages, but also writing to the kernel log with printk, to see if the kernel module is still alive!

My userspace program is now listening to messages (i hope so), but is not getting any of the timed messages sent by the kernel.

So my question is: Is it possible to keep the sockets on both sides open and let the userspace program listen for the messages by the kernel module?

是的,它可以在两侧打开以容纳每一侧。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM