MythTV  master
mediamonitor-unix.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 #include "config.h"
3 
4 // Standard C headers
5 #include <cstdio>
6 
7 // POSIX headers
8 #include <dirent.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #ifndef ANDROID
12 #include <fstab.h>
13 #endif
14 
15 // UNIX System headers
16 #include <sys/file.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/wait.h>
20 #include <sys/param.h>
21 
22 // C++ headers
23 #include <iostream>
24 
25 using namespace std;
26 
27 // Qt headers
28 #if CONFIG_QTDBUS
29 #include <QtDBus>
30 #include <QDBusConnection>
31 #endif
32 #include <QList>
33 #include <QTextStream>
34 #include <QDir>
35 #include <QFile>
36 
37 // MythTV headers
38 #include "mythmediamonitor.h"
39 #include "mediamonitor-unix.h"
40 #include "mythconfig.h"
41 #include "mythcdrom.h"
42 #include "mythhdd.h"
43 #include "mythlogging.h"
44 #include "mythsystemlegacy.h"
45 #include "exitcodes.h"
46 
47 #if HAVE_LIBUDEV
48 extern "C" {
49  #include <libudev.h>
50 }
51 #endif
52 
53 
54 #ifndef MNTTYPE_ISO9660
55 #ifdef linux
56 #define MNTTYPE_ISO9660 "iso9660"
57 #elif defined(__FreeBSD__) || CONFIG_DARWIN || defined(__OpenBSD__)
58 #define MNTTYPE_ISO9660 "cd9660"
59 #endif
60 #endif
61 
62 #ifndef MNTTYPE_UDF
63 #define MNTTYPE_UDF "udf"
64 #endif
65 
66 #ifndef MNTTYPE_AUTO
67 #define MNTTYPE_AUTO "auto"
68 #endif
69 
70 #ifndef MNTTYPE_SUPERMOUNT
71 #define MNTTYPE_SUPERMOUNT "supermount"
72 #endif
73 #define SUPER_OPT_DEV "dev="
74 
75 #if CONFIG_QTDBUS
76 // DBus UDisk service - http://hal.freedesktop.org/docs/udisks/
77 #define UDISKS_SVC "org.freedesktop.UDisks"
78 #define UDISKS_PATH "/org/freedesktop/UDisks"
79 #define UDISKS_IFACE "org.freedesktop.UDisks"
80 #define UDISKS_DEVADD "DeviceAdded"
81 #define UDISKS_DEVRMV "DeviceRemoved"
82 #define UDISKS_DEVSIG "o" // OBJECT_PATH
83 // DBus UDisks2 service - https://udisks.freedesktop.org/
84 #define UDISKS2_SVC "org.freedesktop.UDisks2"
85 #define UDISKS2_PATH "/org/freedesktop/UDisks2"
86 #define UDISKS2_IFACE "org.freedesktop.UDisks2.Drive"
87 #endif
88 
89 const char * MediaMonitorUnix::kUDEV_FIFO = "/tmp/mythtv_media";
90 
91 
92 // Some helpers for debugging:
93 
94 static const QString LOC = QString("MMUnix:");
95 
96 #ifndef Q_OS_ANDROID
97 // TODO: are these used?
98 static void fstabError(const QString &methodName)
99 {
100  LOG(VB_GENERAL, LOG_ALERT,
101  LOC + methodName + " Error: failed to open " + _PATH_FSTAB +
102  " for reading, " + ENO);
103 }
104 #endif
105 
106 static void statError(const QString &methodName, const QString &devPath)
107 {
108  LOG(VB_GENERAL, LOG_ALERT,
109  LOC + methodName + " Error: failed to stat " + devPath +
110  ", " + ENO);
111 }
112 
114 // MediaMonitor
115 
116 
118  unsigned long interval, bool allowEject)
119  : MediaMonitor(par, interval, allowEject)
120 {
122  CheckMountable();
123 
124  LOG(VB_MEDIA, LOG_INFO, "Initial device list...\n" + listDevices());
125 }
126 
127 
128 #if !CONFIG_QTDBUS
130 {
131  if (m_fifo >= 0)
132  {
133  close(m_fifo);
134  m_fifo = -1;
135  unlink(kUDEV_FIFO);
136  }
138 }
139 #endif // !CONFIG_QTDBUS
140 
141 
142 // Loop through the file system table and add any supported devices.
144 {
145 #ifndef Q_OS_ANDROID
146  struct fstab * mep = nullptr;
147 
148  // Attempt to open the file system descriptor entry.
149  if (!setfsent())
150  {
151  fstabError(":CheckFileSystemTable()");
152  return false;
153  }
154 
155  // Add all the entries
156  while ((mep = getfsent()) != nullptr)
157  AddDevice(mep);
158 
159  endfsent();
160 
161  return !m_Devices.isEmpty();
162 #else
163  return false;
164 #endif
165 }
166 
167 #if CONFIG_QTDBUS
168 // Get a device property by name
169 static QVariant DeviceProperty(const QDBusObjectPath& o, const char kszProperty[])
170 {
171  QVariant v;
172 
173  QDBusInterface iface(UDISKS_SVC, o.path(), UDISKS_IFACE".Device",
174  QDBusConnection::systemBus() );
175  if (iface.isValid())
176  v = iface.property(kszProperty);
177 
178  return v;
179 }
180 #endif
181 
194 {
195 #if CONFIG_QTDBUS
196  for (int i = 0; i < 10; ++i, usleep(500000))
197  {
198  // Connect to UDisks. This can sometimes fail if mythfrontend
199  // is started during system init
200  QDBusInterface iface(UDISKS_SVC, UDISKS_PATH, UDISKS_IFACE,
201  QDBusConnection::systemBus() );
202  if (!iface.isValid())
203  {
204  LOG(VB_GENERAL, LOG_ALERT, LOC +
205  "CheckMountable: DBus interface error: " +
206  iface.lastError().message() );
207  QDBusInterface iface2(UDISKS2_SVC, UDISKS2_PATH, UDISKS2_IFACE,
208  QDBusConnection::systemBus() );
209  if (iface2.isValid()) {
210  // We have udisks2 service on this system, give up quickly
211  // TODO: Implement device monitoring via udisks2 service
212  LOG(VB_GENERAL, LOG_WARNING, LOC +
213  "UDisks2 service found. Media Monitor does not support this yet!");
214  return false;
215  }
216  continue;
217  }
218 
219  // Enumerate devices
220  typedef QList<QDBusObjectPath> QDBusObjectPathList;
221  QDBusReply<QDBusObjectPathList> reply = iface.call("EnumerateDevices");
222  if (!reply.isValid())
223  {
224  LOG(VB_GENERAL, LOG_ALERT, LOC +
225  "CheckMountable DBus EnumerateDevices error: " +
226  reply.error().message() );
227  continue;
228  }
229 
230  // Listen on DBus for UDisk add/remove device messages
231  (void)QDBusConnection::systemBus().connect(
232  UDISKS_SVC, UDISKS_PATH, UDISKS_IFACE, UDISKS_DEVADD, UDISKS_DEVSIG,
233  this, SLOT(deviceAdded(QDBusObjectPath)) );
234  (void)QDBusConnection::systemBus().connect(
235  UDISKS_SVC, UDISKS_PATH, UDISKS_IFACE, UDISKS_DEVRMV, UDISKS_DEVSIG,
236  this, SLOT(deviceRemoved(QDBusObjectPath)) );
237 
238  // Parse the returned device array
239  const QDBusObjectPathList& list(reply.value());
240  for (QDBusObjectPathList::const_iterator it = list.begin();
241  it != list.end(); ++it)
242  {
243  if (!DeviceProperty(*it, "DeviceIsSystemInternal").toBool() &&
244  !DeviceProperty(*it, "DeviceIsPartitionTable").toBool() )
245  {
246  QString dev = DeviceProperty(*it, "DeviceFile").toString();
247 
248  // ignore floppies, too slow
249  if (dev.startsWith("/dev/fd"))
250  continue;
251 
252  MythMediaDevice* pDevice;
253  if (DeviceProperty(*it, "DeviceIsRemovable").toBool())
254  pDevice = MythCDROM::get(this, dev.toLatin1(), false, m_AllowEject);
255  else
256  pDevice = MythHDD::Get(this, dev.toLatin1(), false, false);
257 
258  if (pDevice && !MediaMonitorUnix::AddDevice(pDevice))
259  pDevice->deleteLater();
260  }
261  }
262 
263  // Success
264  return true;
265  }
266 
267  // Timed out
268  return false;
269 
270 #elif defined linux
271  // NB needs script in /etc/udev/rules.d
272  mkfifo(kUDEV_FIFO, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
273  m_fifo = open(kUDEV_FIFO, O_RDONLY | O_NONBLOCK);
274 
275  QDir sysfs("/sys/block");
276  sysfs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
277 
278  QStringList devices = sysfs.entryList();
279 
280  for (QStringList::iterator it = devices.begin(); it != devices.end(); ++it)
281  {
282  // ignore floppies, too slow
283  if ((*it).startsWith("fd"))
284  continue;
285 
286  sysfs.cd(*it);
287  QString path = sysfs.absolutePath();
288  if (CheckRemovable(path))
289  FindPartitions(path, true);
290  sysfs.cdUp();
291  }
292  return true;
293 #else // linux
294  return false;
295 #endif
296 }
297 
298 #if !CONFIG_QTDBUS
299 
302 bool MediaMonitorUnix::CheckRemovable(const QString &dev)
303 {
304 #ifdef linux
305  QString removablePath = dev + "/removable";
306  QFile removable(removablePath);
307  if (removable.exists() && removable.open(QIODevice::ReadOnly))
308  {
309  char c = 0;
310  QString msg = LOC + ":CheckRemovable(" + dev + ")/removable ";
311  bool ok = removable.getChar(&c);
312  removable.close();
313 
314  if (ok)
315  {
316  LOG(VB_MEDIA, LOG_DEBUG, msg + c);
317  if (c == '1')
318  return true;
319  }
320  else
321  {
322  LOG(VB_GENERAL, LOG_ALERT, msg + "failed");
323  }
324  }
325  return false;
326 #else // if !linux
327  return false;
328 #endif // !linux
329 }
330 
336 QString MediaMonitorUnix::GetDeviceFile(const QString &sysfs)
337 {
338  QString msg = LOC + ":GetDeviceFile(" + sysfs + ")";
339  QString ret = sysfs;
340 
341  // In case of error, a working default? (device names usually match)
342  ret.replace(QRegExp(".*/"), "/dev/");
343 
344 #ifdef linux
345  #if HAVE_LIBUDEV
346  // Use libudev to determine the name
347  ret.clear();
348  struct udev *udev = udev_new();
349  if (udev != nullptr)
350  {
351  struct udev_device *device =
352  udev_device_new_from_syspath(udev, sysfs.toLatin1().constData());
353  if (device != nullptr)
354  {
355  const char *name = udev_device_get_devnode(device);
356 
357  if (name != nullptr)
358  ret = tr(name);
359  else
360  {
361  // This can happen when udev sends an AddDevice for a block
362  // device with partitions. FindPartition locates a partition
363  // in sysfs but udev hasn't created the devnode for it yet.
364  // Udev will send another AddDevice for the partition later.
365  LOG(VB_MEDIA, LOG_DEBUG, msg + " devnode not (yet) known");
366  }
367 
368  udev_device_unref(device);
369  }
370  else
371  {
372  LOG(VB_GENERAL, LOG_ALERT,
373  msg + " udev_device_new_from_syspath returned NULL");
374  ret = "";
375  }
376 
377  udev_unref(udev);
378  }
379  else
380  LOG(VB_GENERAL, LOG_ALERT,
381  "MediaMonitorUnix::GetDeviceFile udev_new failed");
382  #else // HAVE_LIBUDEV
383  // Use udevadm info to determine the name
384  QStringList args;
385  args << "info" << "-q" << "name"
386  << "-rp" << sysfs;
387 
388  uint flags = kMSStdOut;
389  if (VERBOSE_LEVEL_CHECK(VB_MEDIA, LOG_DEBUG))
390  flags |= kMSStdErr;
391 
392  // TODO: change this to a MythSystemLegacy on the stack?
393  MythSystemLegacy *udevinfo = new MythSystemLegacy("udevinfo", args, flags);
394  udevinfo->Run(4);
395  if( udevinfo->Wait() != GENERIC_EXIT_OK )
396  {
397  delete udevinfo;
398  return ret;
399  }
400 
401  if (VERBOSE_LEVEL_CHECK(VB_MEDIA, LOG_DEBUG))
402  {
403  QTextStream estream(udevinfo->ReadAllErr());
404  while( !estream.atEnd() )
405  LOG(VB_MEDIA, LOG_DEBUG,
406  msg + " - udevadm info error...\n" + estream.readLine());
407  }
408 
409  QTextStream ostream(udevinfo->ReadAll());
410  QString udevLine = ostream.readLine();
411  if (!udevLine.startsWith("device not found in database") )
412  ret = udevLine;
413 
414  delete udevinfo;
415  #endif // HAVE_LIBUDEV
416 #endif // linux
417 
418  LOG(VB_MEDIA, LOG_INFO, msg + "->'" + ret + "'");
419  return ret;
420 }
421 #endif // !CONFIG_QTDBUS
422 
423 /*
424  * \brief Reads the list devices known to be CD or DVD devices.
425  * \return list of CD and DVD device names.
426  */
427 // pure virtual
429 {
430  QStringList l;
431 
432 #if CONFIG_QTDBUS
433  QDBusInterface iface(UDISKS_SVC, UDISKS_PATH, UDISKS_IFACE,
434  QDBusConnection::systemBus() );
435  if (iface.isValid())
436  {
437  // Enumerate devices
438  typedef QList<QDBusObjectPath> QDBusObjectPathList;
439  QDBusReply<QDBusObjectPathList> reply = iface.call("EnumerateDevices");
440  if (reply.isValid())
441  {
442  const QDBusObjectPathList& list(reply.value());
443  for (QDBusObjectPathList::const_iterator it = list.begin();
444  it != list.end(); ++it)
445  {
446  if (DeviceProperty(*it, "DeviceIsRemovable").toBool())
447  {
448  QString dev = DeviceProperty(*it, "DeviceFile").toString();
449  if (dev.startsWith("/dev/"))
450  dev.remove(0,5);
451  l.push_back(dev);
452  }
453  }
454  }
455  }
456 
457 #elif defined linux
458  QFile file("/proc/sys/dev/cdrom/info");
459  if (file.open(QIODevice::ReadOnly))
460  {
461  QString line;
462  QTextStream stream(&file);
463  do
464  {
465  line = stream.readLine();
466  if (line.startsWith("drive name:"))
467  {
468  l = line.split('\t', QString::SkipEmptyParts);
469  l.pop_front(); // Remove 'drive name:' field
470  break; // file should only contain one drive table?
471  }
472  }
473  while (!stream.atEnd());
474  file.close();
475  }
476 #endif // linux
477 
478  LOG(VB_MEDIA, LOG_DEBUG,
479  LOC + ":GetCDROMBlockDevices()->'" + l.join(", ") + "'");
480  return l;
481 }
482 
483 static void LookupModel(MythMediaDevice* device)
484 {
485  QString desc;
486 
487 #if CONFIG_QTDBUS
488  QDBusInterface iface(UDISKS_SVC, UDISKS_PATH, UDISKS_IFACE,
489  QDBusConnection::systemBus() );
490  if (iface.isValid())
491  {
492  QDBusReply<QDBusObjectPath> reply = iface.call(
493  "FindDeviceByDeviceFile", device->getRealDevice());
494  if (reply.isValid())
495  {
496  desc = DeviceProperty(reply, "DriveVendor").toString();
497  if (!desc.isEmpty())
498  desc += " ";
499  desc += DeviceProperty(reply, "DriveModel").toString();
500  }
501  }
502 
503 #elif defined linux
504 
505  // Given something like /dev/hda1, extract hda1
506  QString devname = device->getRealDevice().mid(5,5);
507 
508  if (devname.startsWith("hd")) // IDE drive
509  {
510  QFile file("/proc/ide/" + devname.left(3) + "/model");
511  if (file.open(QIODevice::ReadOnly))
512  {
513  QTextStream stream(&file);
514 
515  desc.append(stream.readLine());
516  file.close();
517  }
518  }
519 
520  if (devname.startsWith("scd")) // scd0 doesn't appear in /sys/block,
521  devname.replace("scd", "sr"); // use sr0 instead
522 
523  if (devname.startsWith("sd") // SATA/USB/FireWire
524  || devname.startsWith("sr")) // SCSI CD-ROM?
525  {
526  QString path = devname.prepend("/sys/block/");
527  path.append("/device/");
528 
529  QFile file(path + "vendor");
530  if (file.open(QIODevice::ReadOnly))
531  {
532  QTextStream stream(&file);
533 
534  desc.append(stream.readLine());
535  desc.append(' ');
536  file.close();
537  }
538 
539  file.setFileName(path + "model");
540  if (file.open(QIODevice::ReadOnly))
541  {
542  QTextStream stream(&file);
543 
544  desc.append(stream.readLine());
545  desc.append(' ');
546  file.close();
547  }
548  }
549 #endif
550 
551  LOG(VB_MEDIA, LOG_DEBUG, QString("LookupModel '%1' -> '%2'")
552  .arg(device->getRealDevice()).arg(desc) );
553  device->setDeviceModel(desc.toLatin1().constData());
554 }
555 
560 {
561  if ( ! pDevice )
562  {
563  LOG(VB_GENERAL, LOG_ERR, "MediaMonitorUnix::AddDevice(null)");
564  return false;
565  }
566 
567  // If the user doesn't want this device to be monitored, stop now:
568  if (shouldIgnore(pDevice))
569  return false;
570 
571  QString path = pDevice->getDevicePath();
572  if (!path.length())
573  {
574  LOG(VB_GENERAL, LOG_ALERT,
575  "MediaMonitorUnix::AddDevice() - empty device path.");
576  return false;
577  }
578 
579  dev_t new_rdev;
580  struct stat sb;
581 
582  if (stat(path.toLocal8Bit().constData(), &sb) < 0)
583  {
584  statError(":AddDevice()", path);
585  return false;
586  }
587  new_rdev = sb.st_rdev;
588 
589  //
590  // Check if this is a duplicate of a device we have already added
591  //
592  QList<MythMediaDevice*>::const_iterator itr = m_Devices.begin();
593  for (; itr != m_Devices.end(); ++itr)
594  {
595  if (stat((*itr)->getDevicePath().toLocal8Bit().constData(), &sb) < 0)
596  {
597  statError(":AddDevice()", (*itr)->getDevicePath());
598  return false;
599  }
600 
601  if (sb.st_rdev == new_rdev)
602  {
603  LOG(VB_MEDIA, LOG_INFO,
604  LOC + ":AddDevice() - not adding " + path +
605  "\n "
606  "because it appears to be a duplicate of " +
607  (*itr)->getDevicePath());
608  return false;
609  }
610  }
611 
612  LookupModel(pDevice);
613 
614  QMutexLocker locker(&m_DevicesLock);
615 
616  connect(pDevice, SIGNAL(statusChanged(MythMediaStatus, MythMediaDevice*)),
618  m_Devices.push_back( pDevice );
619  m_UseCount[pDevice] = 0;
620  LOG(VB_MEDIA, LOG_INFO, LOC + ":AddDevice() - Added " + path);
621 
622  return true;
623 }
624 
625 // Given a fstab entry to a media device determine what type of device it is
626 bool MediaMonitorUnix::AddDevice(struct fstab * mep)
627 {
628  if (!mep)
629  return false;
630 
631 #ifndef Q_OS_ANDROID
632  QString devicePath( mep->fs_spec );
633 #if 0
634  LOG(VB_GENERAL, LOG_DEBUG, "AddDevice - " + devicePath);
635 #endif
636 
637  MythMediaDevice* pDevice = nullptr;
638  struct stat sbuf;
639 
640  bool is_supermount = false;
641  bool is_cdrom = false;
642 
643  if (stat(mep->fs_spec, &sbuf) < 0)
644  return false;
645 
646  // Can it be mounted?
647  if ( ! ( ((strstr(mep->fs_mntops, "owner") &&
648  (sbuf.st_mode & S_IRUSR)) || strstr(mep->fs_mntops, "user")) &&
649  (strstr(mep->fs_vfstype, MNTTYPE_ISO9660) ||
650  strstr(mep->fs_vfstype, MNTTYPE_UDF) ||
651  strstr(mep->fs_vfstype, MNTTYPE_AUTO)) ) )
652  {
653  if (strstr(mep->fs_mntops, MNTTYPE_ISO9660) &&
654  strstr(mep->fs_vfstype, MNTTYPE_SUPERMOUNT))
655  {
656  is_supermount = true;
657  }
658  else
659  {
660  return false;
661  }
662  }
663 
664  if (strstr(mep->fs_mntops, MNTTYPE_ISO9660) ||
665  strstr(mep->fs_vfstype, MNTTYPE_ISO9660) ||
666  strstr(mep->fs_vfstype, MNTTYPE_UDF) ||
667  strstr(mep->fs_vfstype, MNTTYPE_AUTO))
668  {
669  is_cdrom = true;
670 #if 0
671  LOG(VB_GENERAL, LOG_DEBUG, "Device is a CDROM");
672 #endif
673  }
674 
675  if (!is_supermount)
676  {
677  if (is_cdrom)
678  pDevice = MythCDROM::get(this, mep->fs_spec,
679  is_supermount, m_AllowEject);
680  }
681  else
682  {
683  char *dev = nullptr;
684  int len = 0;
685  dev = strstr(mep->fs_mntops, SUPER_OPT_DEV);
686  if (dev == nullptr)
687  return false;
688 
689  dev += sizeof(SUPER_OPT_DEV)-1;
690  while (dev[len] != ',' && dev[len] != ' ' && dev[len] != 0)
691  len++;
692 
693  if (dev[len] != 0)
694  {
695  char devstr[256];
696  strncpy(devstr, dev, len);
697  devstr[len] = 0;
698  if (is_cdrom)
699  pDevice = MythCDROM::get(this, devstr,
700  is_supermount, m_AllowEject);
701  }
702  else
703  return false;
704  }
705 
706  if (pDevice)
707  {
708  pDevice->setMountPath(mep->fs_file);
709  if (pDevice->testMedia() == MEDIAERR_OK)
710  {
711  if (MediaMonitorUnix::AddDevice(pDevice))
712  return true;
713  }
714  pDevice->deleteLater();
715  }
716 #endif
717 
718  return false;
719 }
720 
721 #if CONFIG_QTDBUS
722 /*
723  * DBus UDisk AddDevice handler
724  */
725 void MediaMonitorUnix::deviceAdded( const QDBusObjectPath& o)
726 {
727  LOG(VB_MEDIA, LOG_INFO, LOC + ":deviceAdded " + o.path());
728 
729  // Don't add devices with partition tables, just the partitions
730  if (!DeviceProperty(o, "DeviceIsPartitionTable").toBool())
731  {
732  QString dev = DeviceProperty(o, "DeviceFile").toString();
733 
734  MythMediaDevice* pDevice;
735  if (DeviceProperty(o, "DeviceIsRemovable").toBool())
736  pDevice = MythCDROM::get(this, dev.toLatin1(), false, m_AllowEject);
737  else
738  pDevice = MythHDD::Get(this, dev.toLatin1(), false, false);
739 
740  if (pDevice && !AddDevice(pDevice))
741  pDevice->deleteLater();
742  }
743 }
744 
745 /*
746  * DBus UDisk RemoveDevice handler
747  */
748 void MediaMonitorUnix::deviceRemoved( const QDBusObjectPath& o)
749 {
750  LOG(VB_MEDIA, LOG_INFO, LOC + "deviceRemoved " + o.path());
751 #if 0 // This fails because the DeviceFile has just been deleted
752  QString dev = DeviceProperty(o, "DeviceFile");
753  if (!dev.isEmpty())
754  RemoveDevice(dev);
755 #else
756  QString dev = QFileInfo(o.path()).baseName();
757  dev.prepend("/dev/");
758  RemoveDevice(dev);
759 #endif
760 }
761 
762 #else //CONFIG_QTDBUS
763 
780 bool MediaMonitorUnix::FindPartitions(const QString &dev, bool checkPartitions)
781 {
782  LOG(VB_MEDIA, LOG_DEBUG,
783  LOC + ":FindPartitions(" + dev +
784  QString(",%1").arg(checkPartitions ? " true" : " false" ) + ")");
785  MythMediaDevice* pDevice = nullptr;
786 
787  if (checkPartitions)
788  {
789  // check for partitions
790  QDir sysfs(dev);
791  sysfs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
792 
793  bool found_partitions = false;
794  QStringList parts = sysfs.entryList();
795  for (QStringList::iterator pit = parts.begin();
796  pit != parts.end(); ++pit)
797  {
798  // skip some sysfs dirs that are _not_ sub-partitions
799  if (*pit == "device" || *pit == "holders" || *pit == "queue"
800  || *pit == "slaves" || *pit == "subsystem"
801  || *pit == "bdi" || *pit == "power")
802  continue;
803 
804  found_partitions |= FindPartitions(
805  sysfs.absoluteFilePath(*pit), false);
806  }
807 
808  // no partitions on block device, use main device
809  if (!found_partitions)
810  found_partitions |= FindPartitions(sysfs.absolutePath(), false);
811 
812  return found_partitions;
813  }
814 
815  QString device_file = GetDeviceFile(dev);
816 
817  if (device_file.isEmpty())
818  return false;
819 
820  QStringList cdroms = GetCDROMBlockDevices();
821 
822  if (cdroms.contains(dev.section('/', -1)))
823  {
824  // found cdrom device
825  pDevice = MythCDROM::get(
826  this, device_file.toLatin1().constData(), false, m_AllowEject);
827  }
828  else
829  {
830  // found block or partition device
831  pDevice = MythHDD::Get(
832  this, device_file.toLatin1().constData(), false, false);
833  }
834 
835  if (AddDevice(pDevice))
836  return true;
837 
838  if (pDevice)
839  pDevice->deleteLater();
840 
841  return false;
842 }
843 
850 {
851  char buffer[256];
852  QString qBuffer;
853 
854  if (m_fifo == -1)
855  return;
856 
857  int size = read(m_fifo, buffer, 255);
858  while (size > 0)
859  {
860  // append buffer to QString
861  buffer[size] = '\0';
862  qBuffer.append(buffer);
863  size = read(m_fifo, buffer, 255);
864  }
865  const QStringList list = qBuffer.split('\n', QString::SkipEmptyParts);
866 
867  QStringList::const_iterator it = list.begin();
868  for (; it != list.end(); ++it)
869  {
870  if ((*it).startsWith("add"))
871  {
872  QString dev = (*it).section(' ', 1, 1);
873  LOG(VB_MEDIA, LOG_INFO, "Udev add " + dev);
874 
875  if (CheckRemovable(dev))
876  FindPartitions(dev, true);
877  }
878  else if ((*it).startsWith("remove"))
879  {
880  QString dev = (*it).section(' ', 2, 2);
881  LOG(VB_MEDIA, LOG_INFO, "Udev remove " + dev);
882  RemoveDevice(dev);
883  }
884  }
885 }
886 #endif
static MythHDD * Get(QObject *par, const char *devicePath, bool SuperMount, bool AllowEject)
Helper function used to create a new instance of a hard disk device.
Definition: mythhdd.cpp:15
#define mkfifo(path, mode)
Definition: compat.h:227
void Run(time_t timeout=0)
Runs a command inside the /bin/sh shell. Returns immediately.
const QString & getDevicePath() const
Definition: mythmedia.h:61
QMap< MythMediaDevice *, int > m_UseCount
void setDeviceModel(const char *model)
Definition: mythmedia.h:68
allow access to stdout
Definition: mythsystem.h:39
#define O_NONBLOCK
Definition: mythmedia.cpp:25
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
static void LookupModel(MythMediaDevice *device)
bool shouldIgnore(const MythMediaDevice *device)
Check user preferences to see if this device should be monitored.
unsigned int uint
Definition: compat.h:140
#define SUPER_OPT_DEV
QString GetDeviceFile(const QString &sysfs)
Returns the device special file associated with the /sys/block node.
virtual MythMediaError testMedia()
Definition: mythmedia.h:95
def read(device=None, features=[])
Definition: disc.py:35
virtual void deleteLater(void)
bool AddDevice(MythMediaDevice *pDevice) override
Given a media device, add it to our collection.
QList< MythMediaDevice * > m_Devices
#define S_IROTH
Definition: compat.h:224
#define close
Definition: compat.h:16
static void fstabError(const QString &methodName)
#define VERBOSE_LEVEL_CHECK(_MASK_, _LEVEL_)
Definition: mythlogging.h:24
bool FindPartitions(const QString &dev, bool checkPartitions)
Creates MythMedia instances for sysfs removable media devices.
void mediaStatusChanged(MythMediaStatus oldStatus, MythMediaDevice *pMedia)
Slot which is called when the device status changes and posts a media event to the mainwindow.
QByteArray & ReadAll()
void deleteLater(void) override
#define S_IWGRP
Definition: replex.c:54
const char * name
Definition: ParseText.cpp:328
#define MNTTYPE_UDF
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:99
const QString & getRealDevice() const
Definition: mythmedia.h:63
allow access to stderr
Definition: mythsystem.h:40
bool CheckMountable(void)
Search /sys/block for valid removable media devices.
bool CheckRemovable(const QString &dev)
Is /sys/block/dev a removable device?
static MythCDROM * get(QObject *par, const char *devicePath, bool SuperMount, bool AllowEject)
Definition: mythcdrom.cpp:35
uint Wait(time_t timeout=0)
#define S_IRGRP
Definition: compat.h:223
bool CheckFileSystemTable(void)
void CheckDeviceNotifications(void) override
Checks the named pipe, kUDEV_FIFO, for hotplug events from the udev system.
bool RemoveDevice(const QString &dev)
Remove a device from the media monitor.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
#define MNTTYPE_SUPERMOUNT
#define S_IWOTH
Definition: replex.c:56
static void statError(const QString &methodName, const QString &devPath)
QString listDevices(void)
A string summarising the current devices, for debugging.
QByteArray & ReadAllErr()
static const QString LOC
#define MNTTYPE_AUTO
MythMediaStatus
Definition: mythmedia.h:12
MediaMonitorUnix(QObject *par, unsigned long interval, bool allowEject)
QStringList GetCDROMBlockDevices(void) override
void setMountPath(const char *path)
Definition: mythmedia.h:59
static const char * kUDEV_FIFO