kernel: 2.6.18
GCC: 4.1.2
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/netlink.h>
#include <sys/vfs.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define UEVENT_BUFFER_SIZE 1024
static int init_hotplug_sock(void)
{
struct sockaddr_nl snl;
const int buffersize = 16 * 1024;
int retval;
memset(&snl, 0x00, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
snl.nl_pid = getpid();
snl.nl_groups = 1;
int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (hotplug_sock == -1) {
printf("error getting socket: %s", strerror(errno));
return -1;
}
/* set receive buffersize */
setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
if (retval < 0) {
printf("bind failed: %s", strerror(errno));
close(hotplug_sock);
hotplug_sock = -1;
return -1;
}
return hotplug_sock;
}
int main(int argc, char* argv[])
{
int hotplug_sock = init_hotplug_sock();
char buf_cache[UEVENT_BUFFER_SIZE*2] = {0};
while(1) {
memset(buf_cache, 0 ,sizeof(buf_cache));
recv(hotplug_sock,buf_cache, sizeof(buf_cache), 0);
printf("======buf_cache: %s\n", buf_cache);
}
return 0;
}
這個程式一開始使用netlink socket開啟 hotplug_sock,並且註冊netlink family為NETLINK_KOBJECT_UEVENT,這個協定目前我看到的是kernel 2.6.15以後才有,利用這個協定來將kernel space message傳給user space。
然後設定hotplug_sock的緩衝區,SO_RCVBUFFORCE(since Linux 2.6.14)可以設定比rmem_max 更大的buffer,將與位址bind 之後,就可以recv()。如此一來,我們就可以很方便收到從kernel傳來USB熱插拔的訊息。
參考資料:
http://blog.csdn.net/absurd/archive/2007/04/27/1587938.aspx
linux kernel man page