2016-11-25 8 views
-1

Я хочу обнаружить соединение USB и установить его соответствующий раздел. Я попытался использовать udev.I смог найти устройство в/dev/bus/usb/001 или/002.Но из этого я не могу найти, какой раздел он использует. Поэтому, используя udev, я искал блок-разделы и монтировал, если раздел добавлен..eg/dev/sda1. Но это временное решение. Я хочу, чтобы обнаружить соединение usb с помощью udev или что-то еще, и найти, какой раздел он имеет, и смонтировать его.Попытка монтировать раздел usb в Linux с помощью программы cpp

Мой код:

#include <libudev.h> 
#include <stdio.h> 
#include "dmutil.h" 
#include <pthread.h> 

#include <unistd.h> 

/* 
Thread which detects devices. 
*/ 
void * udev_listener(void * i) 
{ 

    struct udev *udev; 
    udev = udev_new(); 
    if (!udev) { 
     printf("Can't create udev\n"); 
     exit(1); 
    } 

    std::string mount_path_for_udev_listner = DEFAULT_MOUNT_PATH; 
    printf("\n\n **********\nUdev_listner is asked to mount usb to %s ....",mount_path_for_udev_listner.c_str()); 

    struct udev_device *dev; 
    struct udev_monitor *mon; 
    mon = udev_monitor_new_from_netlink(udev, "udev"); 
    assert(mon != NULL); 
    /* int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor,const char *subsystem, const char *devtype); 
     filters to select messages that get delivered to a listener. 
     On Success it returns an integer greater than, or equal to, 0. On failure, a negative error code is returned. 
    */ 
    assert(udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL) >=0); 
    udev_monitor_enable_receiving(mon); 
    /* Get the file descriptor (fd) for the monitor. 
     This fd will get passed to select() */ 

    int fd = udev_monitor_get_fd(mon); 
    /* Begin polling for udev events. Events occur when devices attached to the system are added, removed, or change state. 
     udev_monitor_receive_device() will return a device object representing the device which changed and what type of change occured. 

     The select() system call is used to ensure that the call to udev_monitor_receive_device() will not block. 

     This section will run continuously, calling usleep() at the end of each pass. This is to use udev_monitor in a non-blocking way. */ 
    while (1) 
    { 
     /* 
      int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout); 

      select() allows a program to monitor multiple file descriptors, waiting until one or more of the file descriptors 
      become "ready" for some class of I/O operation. 

      Set up the call to select(). In this case, select() will only operate on a single file descriptor, the one associated 
      with our udev_monitor. Note that the timeval object is set to 0, which will cause select() to not block. */ 
     fd_set fds; 
     struct timeval tv; 
     int ret; 

     FD_ZERO(&fds); //clear fds 
     FD_SET(fd, &fds);// Add fd to fds 
     /* 
      The timeout argument specifies the interval that select() should block waiting for a file descriptor to become ready. 
      This interval will be rounded up to the system clock granularity, and kernel scheduling delays mean that the 
      blocking interval may overrun by a small amount. If both fields of the timeval structure are zero, then select() 
      returns immediately. (This is useful for polling.)If timeout is NULL (no timeout), select() can block indefinitely. 
     */ 
     tv.tv_sec = 0; 
     tv.tv_usec = 0; 
     /* 
      nfds specifies how big the list of file descriptors is because the total number can be vast. 
      So, if you want to monitor file descriptors 24-31, you'd set nfds to 32. 
      man - nfds is the highest-numbered file descriptor in any of the three sets, plus 1. 
     */ 
     ret = select(fd+1, &fds, NULL, NULL, &tv); 

     /* Check if our file descriptor has received data. */ 
     if (ret > 0 && FD_ISSET(fd, &fds)) { 
      printf("\nselect() says there should be data\n"); 

      /* Make the call to receive the device. 
       select() ensured that this will not block. */ 
      dev = udev_monitor_receive_device(mon); 
      if (dev) { 
       printf("Got Device\n"); 
       printf(" Node: %s\n", udev_device_get_devnode(dev)); 
       printf(" Subsystem: %s\n", udev_device_get_subsystem(dev)); 
       printf(" Devtype: %s\n", udev_device_get_devtype(dev)); 
       printf(" syspath:%s\n",udev_device_get_syspath(dev)); 
       printf(" sysname:%s\n",udev_device_get_sysname(dev)); 
       printf(" devpath:%s\n",udev_device_get_devpath(dev)); 
       printf(" subsystem:%s\n",udev_device_get_subsystem(dev)); 
       printf(" Action: %s\n", udev_device_get_action(dev)); 
       std::string devtype=udev_device_get_devtype(dev); 
       std::string action=udev_device_get_action(dev); 
       std::string devnode=udev_device_get_devnode(dev); 
       if(devtype.compare("partition")==0 && action.compare("add") == 0) 
       { 
        printf("A new partition detected at %s\nTrying to mount to %s",devnode.c_str(),mount_path_for_udev_listner.c_str()); 
        int ret = mount_disk(devnode,mount_path_for_udev_listner); 
        if(ret == 0) 
        { 
         printf("\nSystem returns %d, Mounting success\n",ret); 
        } 
        else{ 
          printf("\n*****Error no %d\n",errno); 
        } 
       } 
       if(devtype.compare("partition")==0 && action.compare("remove") == 0) 
       { 
        printf("Partition removal detected, trying to unmount...\n"); 
        int ret=umount_disk(); 
        if(ret==0){ 
         printf("\nSystem returns %d\n",ret); 
         printf("unmount successfull\n"); 
        } 
        else{ 
         printf("\nThe partition unmounting FAILED:\n "); 
        } 
       } 
       udev_device_unref(dev); 
      } 
      else { 
       printf("No Device from receive_device(). An error occured.\n"); 
      }     
     } 
     usleep(250*1000); 
     printf("."); 
     fflush(stdout); 
    } 
    pthread_exit(NULL); 
} 

ответ

0

я получил ответ. Мне просто пришлось добавить еще один фильтр для usb-устройства. если я получу устройство как USB и блок, я могу использовать раздел и смонтировать его.

assert(udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL)>=0); 
assert(udev_monitor_filter_add_match_subsystem_devtype(mon, "usb","usb-device") >=0); 
-1

я скопировал это из https://askubuntu.com/questions/285539/detect-and-mount-devices

sudo lsusbпокажет вам, какие устройства USB обнаруживает Linux. Независимо от того, монтируется или обнаружено запоминающее устройство USB, это отдельные проблемы.sudo lsusb -vдаст подробный вывод, возможно, больше информации, чем вы хотите, если ОС действительно не распознает устройство.

В качестве альтернативы, вы можете сравнить списки устройств в/devдо и после подключения устройства USB. Есть много способов сделать это; Я бы, наверное, просто использовать:

ls -l /dev/* | wc -l

Это даст вам ряд известных устройств. Выполнение этого до и после подключения устройства скажет вам, назначена ли ОС устройству в/dev/.

Другим вариантом является просмотр того, что происходит в dmesg при подключении устройства USB.dmesgможет сообщить вам, как устройство не работает.

Если у устройства USB, у вас возникли проблемы с монтажом, находится в списке lsusb, тогда вы можете попробовать установить устройство. На этом этапе было бы хорошо знать тип файловой системы.sudo fdisk -lсообщит вам тип файловой системы в виде идентификатора. Возможно, вам придется искать идентификационный номер. Для этого есть множество ссылок в Интернете. Как только вы узнаете список устройств, то есть/dev/hda1и тип файловой системы, вы можете попытаться установить устройство вручную с помощью команды mount.

sudo mount /dev/hda1 /home/user/Desktop/whereEver

Вы, возможно, придется убедиться, что место, которые вы хотите установить устройство на существует. Если ОС распознает файловую систему, то mount может работать, только если файловая система не является родной файловой системой; вам может потребоваться указать флаги для монтажа.

Сообщение обратно ваш выход изdmesg(не все это, только со всего, когда устройство USB подключено), иsudo lsusb.

Вы можете найти Linux/UNIX: файлы устройств полезны, если вы пытаетесь определить тип устройства.

Я пишу это, полагая, что все ваши нераспознанные устройства являются блочными устройствами. Существует множество способов решения этой проблемы и множество возможных решений. Для предоставления решения требуется более конкретная информация.

Существует также много графических приложений, которые могут выполнять одно и то же. Вы можете попробовать найти подключенное оборудование в «Disk Utility».

Альтернативный способ обнаружения присоединенные USB устройств является DBus или HAL (однако Hal считается устаревшим), увидеть этот How to detect inserted USB и linux hotplugging

 Смежные вопросы

  • Нет связанных вопросов^_^