参考文章:
内核文档 Documentation/connector/connector.txt
测试用例: 内核模块 Documentation/connector/cn_test.c
用户态程序 Documentation/connector/ucon.c
由于 内核 2.6.14 的 netlink 代码只允许选择一个小于或等于最大组号的组,对于连接器,最大的组号为CN_NETLINK_USERS + 0xf, 即16,因此如果想使用更大的组号,必须修改CN_NETLINK_USERS 到该大值。增加的 0xf 个号码供非内核态用户使用。
因此内核文档中的example程序不能直接运行,
//修改 cn_test.c // static struct cb_id cn_test_id = { 0x123, 0x456 }; static struct cb_id cn_test_id = { 0x7, 0x7 }; //修改 ucon.c //l_local.nl_groups = 0x123; /* bitmask of requested groups */ l_local.nl_groups = 0x7; /* bitmask of requested groups */ #if 1 { int on = 0x7; /* Additional group number */ setsockopt(s, 270, 1, &on, sizeof(on)); } #endif
[注:内核模块编译准备:]
1、安装内核源代码包:
内核版本:kernel-2.6.25.10-86.fc9
下载 kernel-2.6.25.10-86.fc9.src.rpm
# rpm –Uvh kernel-2.6.25.10-86.fc9.src.rpm # cd /usr/src/redhat/SPECS # rpmbuild -bp --target $(uname -m) kernel.spec # cd /usr/src/ # ln -s ./redhat/BUILD/kernel-2.6.25/linux-2.6.25.i686/ linux-2.6.25.i686
准备编译内核:
# cd /usr/src/linux-2.6.25.i686 # make mrproper # make oldconfig # make modules_prepare //修改 /usr/src/linux-2.6.25.i686/Makefile EXTRAVERSION = .10-86.fc9.i686
cn_test.c 模块的Makefile文件:
TARGET=cn_test KDIR=/usr/src/linux-2.6.25.i686 PWD=$(shell pwd) obj-m+=$(TARGET).o default: make -C $(KDIR) M=$(PWD) modules