zoukankan      html  css  js  c++  java
  • udev笔记

    1.udevd的主配置文件是/etc/udev/udev.conf

    2.使用udev来监听U的hot-plug事件

     1 #include <stdio.h>  
     2 #include <stdlib.h>  
     3 #include <string.h>  
     4 #include <ctype.h>  
     5 #include <sys/un.h>  
     6 #include <sys/ioctl.h>  
     7 #include <sys/socket.h>  
     8 #include <linux/types.h>  
     9 #include <linux/netlink.h>  
    10 #include <errno.h>  
    11 #include <unistd.h>  
    12 #include <arpa/inet.h>  
    13 #include <netinet/in.h>
    14 
    15 
    16 static int init_hotplug_sock(void)
    17 {
    18     struct sockaddr_nl snl;
    19     const int buffersize = 16 * 1024 * 1024;
    20     int retval;
    21  
    22     memset(&snl, 0x00, sizeof(struct sockaddr_nl));
    23     snl.nl_family = AF_NETLINK;
    24     snl.nl_pid = getpid();
    25     snl.nl_groups = 1;
    26  
    27     int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    28     if (hotplug_sock == -1) {
    29         printf("error getting socket: %s", strerror(errno));
    30         return -1;
    31     }
    32  
    33     /* set receive buffersize */
    34     setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
    35  
    36     retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
    37     if (retval < 0) {
    38         printf("bind failed: %s", strerror(errno));
    39         close(hotplug_sock);
    40         hotplug_sock = -1;
    41         return -1;
    42     }
    43  
    44     return hotplug_sock;
    45 }
    46  
    47 #define UEVENT_BUFFER_SIZE 2048
    48  
    49 int main(int argc, char* argv[])
    50 {
    51      int hotplug_sock = init_hotplug_sock();
    52     
    53      while(1)
    54      {
    55            char buf[UEVENT_BUFFER_SIZE*2] = {0};
    56            recv(hotplug_sock, &buf, sizeof(buf), 0); 
    57            printf("%s
    ", buf);
    58      }
    59 
    60      return 0;
    61 }

    插上和拔出U盘打印:

    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0
    add@/module/usb_storage
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/scsi_host/host33
    add@/bus/usb/drivers/usb-storage
    add@/module/uas
    add@/bus/usb/drivers/uas
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/scsi_disk/33:0:0:0
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/scsi_device/33:0:0:0
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/scsi_generic/sg2
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/bsg/33:0:0:0
    add@/devices/virtual/bdi/8:16
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/block/sdb
    add@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/block/sdb/sdb1
    add@/module/nls_iso8859_1


    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/bsg/33:0:0:0
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/scsi_generic/sg2
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/scsi_device/33:0:0:0
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/scsi_disk/33:0:0:0
    change@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/block/sdb
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/block/sdb/sdb1
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/block/sdb
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0
    remove@/devices/virtual/bdi/8:16
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/scsi_host/host33
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0
    remove@/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1

    3.利用udev更改网卡的名字

    ①通过ifconfig或者ubuntu:$ cat /sys/class/net/eth0/address来获得网卡的MAC地址:
    00:0c:29:e1:b6:cd
    
    ②创建rules文件
    /etc/udev/rules.d/my-network.rules
    cat my-network.rules:
    SUBSYSTEM=="net", ATTR{address}=="00:0c:29:e1:b6:cd", NAME="my-eth0"
    
    然后重启,发现网卡名字从eth0变为my-eth0

    参考:
    http://blog.chinaunix.net/uid-30415354-id-5754794.html(没看完)

  • 相关阅读:
    字符串的输入输出 附带一道练习题
    NOIP2009 1.多项式输出
    算法--欧几里得
    小程序:2048
    虚函数和多态
    c++学习记录(十五)
    面向对象程序设计寒假作业3
    c++学习记录(十四)
    c++学习记录(十三)
    c++学习记录(十二)
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/9346246.html
Copyright © 2011-2022 走看看