linux 实现监听热插拔事件

在一些跑linux 系统的平台上,比如故事机,会支持sdcard 热插拔的功能,也就是在sdcard 插入后,把sdcard mount到文件系统中,sdcard拔出后移除。上层应用要怎么知道这个事件呢,应用层可以注册监听uevent 事件即可,下面给出对应的实现。

​
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <sys/mount.h>


#define EPOLL_FD_SIZE     3   //需要监听的fd个数
#define EPOLL_EVENT_SIZE  2
#define EPOLL_TIMEOUT     1000  //等待的超时时间,1000ms 也就是1秒

#ifndef KERNEL_UEVENT_LEN
#define KERNEL_UEVENT_LEN      (4*1024)
#endif

typedef struct
{
    int epoll_fd;              //epoll 对应的fd
    int uevent_fd;             //热插拔节点的sock 句柄
    pthread_t hotplug_thread;  //对应的线程
    int is_start;              //线程是否已经创建
    int is_running;            //是否在while循环中运行
} hotplug_context_t;


static hotplug_context_t hotplug = 
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值