DiskDeviceWatcher.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "DiskDeviceWatcher.h"
  2. #include <QDebug>
  3. #include <QSocketNotifier>
  4. #include <QByteArray>
  5. #include <QRegExp>
  6. #include <QBuffer>
  7. #include <QCoreApplication>
  8. #include <sys/un.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/socket.h>
  11. #include <linux/types.h>
  12. #include <linux/netlink.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include "../DeviceWatcher.h"
  16. #define UEVENT_BUFFER_SIZE 2048
  17. #define UDEV_MONITOR_KERNEL 1
  18. //#define USE_REGEXP 1
  19. class DiskDeviceWatcherPrivate
  20. {
  21. Q_DISABLE_COPY(DiskDeviceWatcherPrivate)
  22. public:
  23. explicit DiskDeviceWatcherPrivate(DiskDeviceWatcher* parent);
  24. ~DiskDeviceWatcherPrivate();
  25. void initialize();
  26. void parseLine(const QByteArray& line);
  27. QSocketNotifier* m_SocketNotifier;
  28. QBuffer m_Buffer;
  29. int m_NetLinkSocket;
  30. DeviceWatcher* m_DeviceWatcher;
  31. QString m_TypeStr;
  32. private:
  33. DiskDeviceWatcher* m_Parent;
  34. };
  35. DiskDeviceWatcher::DiskDeviceWatcher(QObject *parent)
  36. : QObject(parent)
  37. , m_Private(new DiskDeviceWatcherPrivate(this))
  38. {
  39. }
  40. DiskDeviceWatcher::~DiskDeviceWatcher()
  41. {
  42. }
  43. void DiskDeviceWatcher::onSocketNotifier()
  44. {
  45. QByteArray data;
  46. data.resize(UEVENT_BUFFER_SIZE * 2);
  47. data.fill(0);
  48. size_t len = read(m_Private->m_SocketNotifier->socket(), data.data(), UEVENT_BUFFER_SIZE * 2);
  49. data.resize(len);
  50. data = data.replace(0, '\n').trimmed();
  51. // qDebug()<<"==========data:"<<data;
  52. // if(data.contains("remove@")){
  53. // if(m_Private->m_DeviceWatcher == NULL){
  54. // m_Private->m_DeviceWatcher = (DeviceWatcher*)this->parent()->parent();
  55. // }
  56. // if(m_Private->m_DeviceWatcher != NULL){
  57. // if(m_Private->m_TypeStr.startsWith(QString("/dev/sd"))){
  58. // m_Private->m_DeviceWatcher->stopUsbScanner();
  59. // }
  60. // if(m_Private->m_TypeStr.startsWith(QString("/dev/mmcblk")) && 1){
  61. // m_Private->m_DeviceWatcher->stopSdScanner();
  62. // }
  63. // }
  64. // }
  65. if (m_Private->m_Buffer.isOpen()) {
  66. m_Private->m_Buffer.close();
  67. }
  68. m_Private->m_Buffer.setBuffer(&data);
  69. if (m_Private->m_Buffer.open(QIODevice::ReadOnly)) {
  70. while(m_Private->m_Buffer.canReadLine()) {
  71. m_Private->parseLine(m_Private->m_Buffer.readLine().trimmed());
  72. }
  73. m_Private->m_Buffer.close();
  74. }
  75. }
  76. DiskDeviceWatcherPrivate::DiskDeviceWatcherPrivate(DiskDeviceWatcher *parent)
  77. : m_Parent(parent)
  78. {
  79. m_SocketNotifier = NULL;
  80. m_NetLinkSocket = -1;
  81. m_DeviceWatcher = NULL;
  82. m_TypeStr = "";
  83. initialize();
  84. }
  85. DiskDeviceWatcherPrivate::~DiskDeviceWatcherPrivate()
  86. {
  87. }
  88. void DiskDeviceWatcherPrivate::initialize()
  89. {
  90. //printf("==================%s:%d\n",__func__,__LINE__);
  91. struct sockaddr_nl snl;
  92. const int buffersize = 16 * 1024 * 1024;
  93. int retval;
  94. memset(&snl, 0x00, sizeof(struct sockaddr_nl));
  95. snl.nl_family = AF_NETLINK;
  96. snl.nl_pid = getpid();
  97. snl.nl_groups = UDEV_MONITOR_KERNEL;
  98. m_NetLinkSocket = socket(PF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
  99. if (-1 == m_NetLinkSocket) {
  100. exit(EXIT_FAILURE);
  101. }
  102. /* set receive buffersize */
  103. setsockopt(m_NetLinkSocket, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
  104. retval = bind(m_NetLinkSocket, (struct sockaddr*) &snl, sizeof(struct sockaddr_nl));
  105. if (retval < 0) {
  106. close(m_NetLinkSocket);
  107. m_NetLinkSocket = -1;
  108. exit(EXIT_FAILURE);
  109. } else if (retval == 0) {
  110. struct sockaddr_nl _snl;
  111. socklen_t _addrlen;
  112. _addrlen = sizeof(struct sockaddr_nl);
  113. retval = getsockname(m_NetLinkSocket, (struct sockaddr *)&_snl, &_addrlen);
  114. if (0 == retval) {
  115. snl.nl_pid = _snl.nl_pid;
  116. }
  117. }
  118. m_SocketNotifier = new QSocketNotifier(m_NetLinkSocket, QSocketNotifier::Read, m_Parent);
  119. Qt::ConnectionType type = static_cast<Qt::ConnectionType>(Qt::UniqueConnection | Qt::AutoConnection);
  120. QObject::connect(m_SocketNotifier, SIGNAL(activated(int)),
  121. m_Parent, SLOT(onSocketNotifier()),
  122. type); //will always active
  123. m_SocketNotifier->setEnabled(true);
  124. }
  125. void DiskDeviceWatcherPrivate::parseLine(const QByteArray &line)
  126. {
  127. //qDebug() << line;
  128. if (line.contains("/block/")) { //hotplug
  129. QString action_str = line.left(line.indexOf('@')).toLower();
  130. QString dev = "/dev/" + line.right(line.length() - line.lastIndexOf('/') - 1);
  131. DiskDeviceWatcher::Action action;
  132. // if (QString(qgetenv("PROTOCOL_ID")).contains(QString("yaoxi"))) {
  133. // if (line.contains("musb-ark1680")) {
  134. // if (!line.contains("musb-ark1680.0")) {
  135. // return ;
  136. // }
  137. // }
  138. // }
  139. if (QString("add") == action_str) {
  140. action = DiskDeviceWatcher::A_Add;
  141. m_TypeStr = dev;
  142. emit m_Parent->diskDeviceChange(dev, action);
  143. } else if (QString("remove") == action_str){
  144. action = DiskDeviceWatcher::A_Remove;
  145. m_TypeStr.clear();
  146. emit m_Parent->diskDeviceChange(dev, action);
  147. }
  148. }
  149. }