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 = nullptr;
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  struct stat sb {};
580  if (stat(path.toLocal8Bit().constData(), &sb) < 0)
581  {
582  statError(":AddDevice()", path);
583  return false;
584  }
585  dev_t new_rdev = sb.st_rdev;
586 
587  //
588  // Check if this is a duplicate of a device we have already added
589  //
590  QList<MythMediaDevice*>::const_iterator itr = m_Devices.begin();
591  for (; itr != m_Devices.end(); ++itr)
592  {
593  if (stat((*itr)->getDevicePath().toLocal8Bit().constData(), &sb) < 0)
594  {
595  statError(":AddDevice()", (*itr)->getDevicePath());
596  return false;
597  }
598 
599  if (sb.st_rdev == new_rdev)
600  {
601  LOG(VB_MEDIA, LOG_INFO,
602  LOC + ":AddDevice() - not adding " + path +
603  "\n "
604  "because it appears to be a duplicate of " +
605  (*itr)->getDevicePath());
606  return false;
607  }
608  }
609 
610  LookupModel(pDevice);
611 
612  QMutexLocker locker(&m_DevicesLock);
613 
614  connect(pDevice, SIGNAL(statusChanged(MythMediaStatus, MythMediaDevice*)),
616  m_Devices.push_back( pDevice );
617  m_UseCount[pDevice] = 0;
618  LOG(VB_MEDIA, LOG_INFO, LOC + ":AddDevice() - Added " + path);
619 
620  return true;
621 }
622 
623 // Given a fstab entry to a media device determine what type of device it is
624 bool MediaMonitorUnix::AddDevice(struct fstab * mep)
625 {
626  if (!mep)
627  return false;
628 
629 #ifndef Q_OS_ANDROID
630  QString devicePath( mep->fs_spec );
631 #if 0
632  LOG(VB_GENERAL, LOG_DEBUG, "AddDevice - " + devicePath);
633 #endif
634 
635  MythMediaDevice* pDevice = nullptr;
636  struct stat sbuf {};
637 
638  bool is_supermount = false;
639  bool is_cdrom = false;
640 
641  if (stat(mep->fs_spec, &sbuf) < 0)
642  return false;
643 
644  // Can it be mounted?
645  if ( ! ( ((strstr(mep->fs_mntops, "owner") &&
646  (sbuf.st_mode & S_IRUSR)) || strstr(mep->fs_mntops, "user")) &&
647  (strstr(mep->fs_vfstype, MNTTYPE_ISO9660) ||
648  strstr(mep->fs_vfstype, MNTTYPE_UDF) ||
649  strstr(mep->fs_vfstype, MNTTYPE_AUTO)) ) )
650  {
651  if (strstr(mep->fs_mntops, MNTTYPE_ISO9660) &&
652  strstr(mep->fs_vfstype, MNTTYPE_SUPERMOUNT))
653  {
654  is_supermount = true;
655  }
656  else
657  {
658  return false;
659  }
660  }
661 
662  if (strstr(mep->fs_mntops, MNTTYPE_ISO9660) ||
663  strstr(mep->fs_vfstype, MNTTYPE_ISO9660) ||
664  strstr(mep->fs_vfstype, MNTTYPE_UDF) ||
665  strstr(mep->fs_vfstype, MNTTYPE_AUTO))
666  {
667  is_cdrom = true;
668 #if 0
669  LOG(VB_GENERAL, LOG_DEBUG, "Device is a CDROM");
670 #endif
671  }
672 
673  if (!is_supermount)
674  {
675  if (is_cdrom)
676  pDevice = MythCDROM::get(this, mep->fs_spec,
677  is_supermount, m_AllowEject);
678  }
679  else
680  {
681  char *dev = nullptr;
682  int len = 0;
683  dev = strstr(mep->fs_mntops, SUPER_OPT_DEV);
684  if (dev == nullptr)
685  return false;
686 
687  dev += sizeof(SUPER_OPT_DEV)-1;
688  while (dev[len] != ',' && dev[len] != ' ' && dev[len] != 0)
689  len++;
690 
691  if (dev[len] != 0)
692  {
693  char devstr[256];
694  strncpy(devstr, dev, len);
695  devstr[len] = 0;
696  if (is_cdrom)
697  pDevice = MythCDROM::get(this, devstr,
698  is_supermount, m_AllowEject);
699  }
700  else
701  return false;
702  }
703 
704  if (pDevice)
705  {
706  pDevice->setMountPath(mep->fs_file);
707  if (pDevice->testMedia() == MEDIAERR_OK)
708  {
709  if (MediaMonitorUnix::AddDevice(pDevice))
710  return true;
711  }
712  pDevice->deleteLater();
713  }
714 #endif
715 
716  return false;
717 }
718 
719 #if CONFIG_QTDBUS
720 /*
721  * DBus UDisk AddDevice handler
722  */
723 void MediaMonitorUnix::deviceAdded( const QDBusObjectPath& o)
724 {
725  LOG(VB_MEDIA, LOG_INFO, LOC + ":deviceAdded " + o.path());
726 
727  // Don't add devices with partition tables, just the partitions
728  if (!DeviceProperty(o, "DeviceIsPartitionTable").toBool())
729  {
730  QString dev = DeviceProperty(o, "DeviceFile").toString();
731 
732  MythMediaDevice* pDevice = nullptr;
733  if (DeviceProperty(o, "DeviceIsRemovable").toBool())
734  pDevice = MythCDROM::get(this, dev.toLatin1(), false, m_AllowEject);
735  else
736  pDevice = MythHDD::Get(this, dev.toLatin1(), false, false);
737 
738  if (pDevice && !AddDevice(pDevice))
739  pDevice->deleteLater();
740  }
741 }
742 
743 /*
744  * DBus UDisk RemoveDevice handler
745  */
746 void MediaMonitorUnix::deviceRemoved( const QDBusObjectPath& o)
747 {
748  LOG(VB_MEDIA, LOG_INFO, LOC + "deviceRemoved " + o.path());
749 #if 0 // This fails because the DeviceFile has just been deleted
750  QString dev = DeviceProperty(o, "DeviceFile");
751  if (!dev.isEmpty())
752  RemoveDevice(dev);
753 #else
754  QString dev = QFileInfo(o.path()).baseName();
755  dev.prepend("/dev/");
756  RemoveDevice(dev);
757 #endif
758 }
759 
760 #else //CONFIG_QTDBUS
761 
778 bool MediaMonitorUnix::FindPartitions(const QString &dev, bool checkPartitions)
779 {
780  LOG(VB_MEDIA, LOG_DEBUG,
781  LOC + ":FindPartitions(" + dev +
782  QString(",%1").arg(checkPartitions ? " true" : " false" ) + ")");
783  MythMediaDevice* pDevice = nullptr;
784 
785  if (checkPartitions)
786  {
787  // check for partitions
788  QDir sysfs(dev);
789  sysfs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
790 
791  bool found_partitions = false;
792  QStringList parts = sysfs.entryList();
793  for (QStringList::iterator pit = parts.begin();
794  pit != parts.end(); ++pit)
795  {
796  // skip some sysfs dirs that are _not_ sub-partitions
797  if (*pit == "device" || *pit == "holders" || *pit == "queue"
798  || *pit == "slaves" || *pit == "subsystem"
799  || *pit == "bdi" || *pit == "power")
800  continue;
801 
802  found_partitions |= FindPartitions(
803  sysfs.absoluteFilePath(*pit), false);
804  }
805 
806  // no partitions on block device, use main device
807  if (!found_partitions)
808  found_partitions |= FindPartitions(sysfs.absolutePath(), false);
809 
810  return found_partitions;
811  }
812 
813  QString device_file = GetDeviceFile(dev);
814 
815  if (device_file.isEmpty())
816  return false;
817 
818  QStringList cdroms = GetCDROMBlockDevices();
819 
820  if (cdroms.contains(dev.section('/', -1)))
821  {
822  // found cdrom device
823  pDevice = MythCDROM::get(
824  this, device_file.toLatin1().constData(), false, m_AllowEject);
825  }
826  else
827  {
828  // found block or partition device
829  pDevice = MythHDD::Get(
830  this, device_file.toLatin1().constData(), false, false);
831  }
832 
833  if (AddDevice(pDevice))
834  return true;
835 
836  if (pDevice)
837  pDevice->deleteLater();
838 
839  return false;
840 }
841 
848 {
849  char buffer[256];
850  QString qBuffer;
851 
852  if (m_fifo == -1)
853  return;
854 
855  int size = read(m_fifo, buffer, 255);
856  while (size > 0)
857  {
858  // append buffer to QString
859  buffer[size] = '\0';
860  qBuffer.append(buffer);
861  size = read(m_fifo, buffer, 255);
862  }
863  const QStringList list = qBuffer.split('\n', QString::SkipEmptyParts);
864 
865  QStringList::const_iterator it = list.begin();
866  for (; it != list.end(); ++it)
867  {
868  if ((*it).startsWith("add"))
869  {
870  QString dev = (*it).section(' ', 1, 1);
871  LOG(VB_MEDIA, LOG_INFO, "Udev add " + dev);
872 
873  if (CheckRemovable(dev))
874  FindPartitions(dev, true);
875  }
876  else if ((*it).startsWith("remove"))
877  {
878  QString dev = (*it).section(' ', 2, 2);
879  LOG(VB_MEDIA, LOG_INFO, "Udev remove " + dev);
880  RemoveDevice(dev);
881  }
882  }
883 }
884 #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