linux c检测网线热插拔(netlink)

linux c检测网线热插拔(netlink)

2016-02-19 09:37 本站整理 浏览(14)
#include <sys/types.h>  
#include <sys/socket.h>  
#include <asm/types.h>  
#include <linux/netlink.h>  
#include <linux/rtnetlink.h>  
#include <unistd.h>
#include <stdlib.h>  
#include <stdio.h>  
#include <sys/ioctl.h>  
#include <linux/if.h>  
#include <string.h>  
  
#define BUFLEN 20480  
  
int main(int argc, char *argv[])  
{  
    int fd, retval;  
    char buf[BUFLEN] = {0};  
    int len = BUFLEN;  
    struct sockaddr_nl addr;  
    struct nlmsghdr *nh;  
    struct ifinfomsg *ifinfo;  
    struct rtattr *attr;  
  
    fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);  
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));  
    memset(&addr, 0, sizeof(addr));  
    addr.nl_family = AF_NETLINK;  
    addr.nl_groups = RTNLGRP_LINK;  
    bind(fd, (struct sockaddr*)&addr, sizeof(addr));  
    while ((retval = read(fd, buf, BUFLEN)) > 0)  
    {  
        for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, retval); nh = NLMSG_NEXT(nh, retval))  
        {  
            if (nh->nlmsg_type == NLMSG_DONE)  
                break;  
            else if (nh->nlmsg_type == NLMSG_ERROR)  
                return -1;  
            else if (nh->nlmsg_type != RTM_NEWLINK)  
                continue;  
            ifinfo = NLMSG_DATA(nh);  
            printf("%u: %s", ifinfo->ifi_index,  
                    (ifinfo->ifi_flags & IFF_LOWER_UP) ? "up" : "down" );  
            attr = (struct rtattr*)(((char*)nh) + NLMSG_SPACE(sizeof(*ifinfo)));  
            len = nh->nlmsg_len - NLMSG_SPACE(sizeof(*ifinfo));  
            for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len))  
            {  
                if (attr->rta_type == IFLA_IFNAME)  
                {  
                    printf(" %s", (char*)RTA_DATA(attr));  
                    break;  
                }  
            }  
            printf("\n");  
        }  
    }  
  
    return 0;  
}

示例输出:
2: down eth0  // 拔出网线
2: up eth0    // 插入网线

源地址:
http://www.cpplive.com/html/1542.html
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: 在 Linux 中,可以使用 UDev(Linux Kernel 中的一个守护程序)监听 USB 设备的插入和拔出事件。通过 UDev ,我们可以使用 Qt 的 QProcess 来与 UDev 通信,并监测 U 盘的热插拔事件。以下是使用 UDev 和 Qt 进行 U 盘热插拔检测的实现步骤: 1. 引入 Qt 的 QProcess 类和 UDev 头文件: ``` c++ #include <QProcess> #include <libudev.h> ``` 2. 定义 UDev 上下文和监听器: ``` c++ struct udev *udev; struct udev_monitor *mon; ``` 3. 初始化 UDev 上下文和监听器: ``` c++ udev = udev_new(); mon = udev_monitor_new_from_netlink(udev, "udev"); udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", "usb_device"); udev_monitor_enable_receiving(mon); ``` 4. 初始化 Qt 的 QProcess 实例用于运行监测程序: ``` c++ QProcess *process = new QProcess(this); ``` 5. 启动监测程序并循环监听 UDev 中的事件: ``` c++ process->start("udevadm monitor --udev -s usb"); while (/*!done*/ true) { fd_set fds; FD_ZERO(&fds); FD_SET(udev_monitor_get_fd(mon), &fds); if (select(udev_monitor_get_fd(mon) + 1, &fds, nullptr, nullptr, nullptr) > 0) { if (FD_ISSET(udev_monitor_get_fd(mon), &fds)) { struct udev_device *dev = udev_monitor_receive_device(mon); // 对设备的属性和信息进行分析 udev_device_unref(dev); } } } ``` 通过以上实现,我们可以在 Linux 中使用 Qt 监测 U 盘的热插拔事件。需要注意的是,在实现中需要对 UDev 中的设备信息进行分析,并且需要在程序结束时释放 UDev 相关的数据结构。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值