首页 > C/C++/Linux > 分区自动挂卸载1—-插拔信息的检测

分区自动挂卸载1—-插拔信息的检测

2009-06-04 17:44 星期四    浏览: 1,325    绿 发表评论 阅读评论

由于没有使用udev那套守护程序,自己做了简单的实现,主要使用下面三个函数来获取socket上的信息实现mount和umount操作:

socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
retval= bind(hotplug_sock, (struct sockaddr*) &snl, sizeof(struct sockaddr_nl));
recv(hotplug_sock, &buf, sizeof(buf), 0);
具体代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/kdev_t.h>
#include <sys/socket.h>
#include <sys/user.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include “mountutil2.h”
static int init_hotplug_sock(void)
{
struct sockaddr_nl snl;
const int buffersize= 16 * 1024 * 1024;
int retval;
memset(&snl, 0×00, 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;
}
#define UEVENT_BUFFER_SIZE 2048
int main(int argc, char* argv[])
{
int hotplug_sock = init_hotplug_sock();
while(1)
{
char buf[UEVENT_BUFFER_SIZE*2] = {0};
recv(hotplug_sock, &buf, sizeof(buf), 0);
printf(“%sn”, buf);
mountumount(buf);//根据内核传递过来的插拔信息进行mount或umount操作
}
return 0;
}
mount或umount操作
void mountumount(const char* buf)
{
if(strstr(buf, “add@/block/mmcblk”))
{
sleep(1);
mountMMCPartions();
}
else if(strstr(buf, “add@/block/sd”))//add@/class/usb_device, add@/block/sdb
{
sleep(4);
mountUSBPartions();
}
else if(strstr(buf, “remove@/block/mmcblk”))
{
umountMMCPartions();
}
else if(strstr(buf, “remove@/class/usb_device”))
{
umountUSBPartions();
}
}
附录:插拔信息log输出:
add@/block p2/mmcblk0
add@/block/mmcblk0/mmcblk0p1
add@/block/mmcblk0/mmcblk0p2
remove@/block/mmcblk0/mmcblk0p2
remove@/block/mmcblk0/mmcblk0p1
remove@/block/mmcblk0
remove@/devices/platform/c2sd/mmc0:13dc
add@/devices/platform/dwc_otg_b/scsi1 : SCSI emulation for USB Mass Storage devices usb2/2-1
add@/devices/platform/usb-storage: device found at 2
add@/class/scsi_host/host1
add@/class/usb_device/usbdev2.2
  Vendor: Generic   Model: USB Flash Disk    Rev: 0.00
  Type:   Direct-Access                      ANSI SCSI revision: 02
add@/devices/platform/dwc_otg_b/SCSI device sdb: 3948544 512-byte hdwr sectors (2022 MB)
add@/block/sdb
remove@/class/scsi_device/1:0:0:0
remove@/block/sdb
remove@/devices/platform/dwc_otg_b/usb2/2-1/2-1:1.0/host1/target1:0:0/1:0:0:0
remove@/class/scsi_host/host1
remove@/devices/platform/dwc_otg_b/usb2/2-1/2-1:1.0
remove@/class/usb_device/usbdev2.2
remove@/devices/platform/dwc_otg_b/usb2/2-1
参考文档:
PF_NETLINK应用实例NETLINK_KOBJECT_UEVENT具体实现--udev实现原理
http://blog.chinaunix.net/u1/38994/showart_1191776.html

本文链接地址: http://blog.redwolf-soft.com/?p=571

原创文章,版权©红狼博客所有, 转载随意,但请注明出处。

    分享到:

相关文章:

  • 无相关文章
分类: C/C++/Linux 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
订阅评论
  欢迎参与讨论,请在这里发表您的看法、交流您的观点。