ioctl() 使用举例
整理一些例子,备忘!
ioctl()
used for control device
#include <sys/ioctl.h>
int ioctl(int d, int request, ...);
所有的request参数及解释 http://goo.gl/gHOh71
第三个参数根据request的类型有5种不同的类型:
- int
- struct ifconf http://linux.die.net/man/7/netdevice
- struct ifreq http://linux.die.net/man/7/netdevice
- struct arpreq http://goo.gl/ZlCFbD
- struct rtentry http://goo.gl/ADwW41
使用举例
-
获取网卡IP地址 http://goo.gl/kfHS78
-
更多操作interfaces的例子 http://goo.gl/f18XkC
-
查询ARP缓存表 http://goo.gl/QOnFfb
需要在ioctl函数调用前增加 strcpy(req.arp_dev, “eth0”);
- 增加、删除路由项 http://goo.gl/2TAcfx
KVM API
The kvm API is a set of ioctls that are issued to control various aspects of a virtual machine. –http://goo.gl/lw4bPm
QEMU userspace process uses kvm.ko driver to execute guest code:
open("/dev/kvm")
ioctl(KVM_CREATE_VM)
ioctl(KVM_CREATE_VCPU)
for (;;) {
ioctl(KVM_RUN)
switch (exit_reason) {
case KVM_EXIT_IO: /* ... */
case KVM_EXIT_HLT: /* ... */
}
}
link: http://goo.gl/qgDTV6
留下评论