[关闭]
@zifehng 2017-06-27T10:15:14.000000Z 字数 1352 阅读 2954

uevent_open_socket()浅析

ueventd socket netlink


  1. int uevent_open_socket(int buf_sz, bool passcred)
  2. {
  3. struct sockaddr_nl addr;
  4. int on = passcred;
  5. int s;
  6. memset(&addr, 0, sizeof(addr));
  7. addr.nl_family = AF_NETLINK;
  8. addr.nl_pid = getpid();
  9. addr.nl_groups = 0xffffffff;
  10. s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  11. if(s < 0)
  12. return -1;
  13. setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &buf_sz, sizeof(buf_sz));
  14. setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
  15. if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  16. close(s);
  17. return -1;
  18. }
  19. return s;
  20. }
  1. #include <sys/socket.h>
  2. sockfd = socket(int socket_family, int socket_type, int protocol);

SO_PASSCRED
Enable or disable the receiving of the SCM_CREDENTIALS control message. For more information see unix(7).
SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2). The default value is set by the /proc/sys/net/core/rmem_default file, and the maximum allowed value is set by the /proc/sys/net/core/rmem_max file.The minimum (dou‐bled) value for this option is 256.
SO_RCVBUFFORCE (since Linux 2.6.14)
Using this socket option, a privileged (CAP_NET_ADMIN) process can perform the same task as SO_RCVBUF, but the rmem_max limit can be overridden.

AF_XXX与PF_XXX
AF_前缀表示地址族,PF_前缀表示协议族,最初的设计是:单个协议族可以支持多个地址族,PF_值用于创建套接字,而AF_值用于套接字地址结构。但实际上后来一直没有出现支持多个地址族的协议族,而在头文件中对应相同协议的PF_值与AF_值总是相等,因此两者一直混用。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注