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  using QDBusObjectPathList = QList<QDBusObjectPath>;
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  foreach (const auto & entry, list)
241  {
242  if (!DeviceProperty(entry, "DeviceIsSystemInternal").toBool() &&
243  !DeviceProperty(entry, "DeviceIsPartitionTable").toBool() )
244  {
245  QString dev = DeviceProperty(entry, "DeviceFile").toString();
246 
247  // ignore floppies, too slow
248  if (dev.startsWith("/dev/fd"))
249  continue;
250 
251  MythMediaDevice* pDevice = nullptr;
252  if (DeviceProperty(entry, "DeviceIsRemovable").toBool())
253  pDevice = MythCDROM::get(this, dev.toLatin1(), false, m_AllowEject);
254  else
255  pDevice = MythHDD::Get(this, dev.toLatin1(), false, false);
256 
257  if (pDevice && !MediaMonitorUnix::AddDevice(pDevice))
258  pDevice->deleteLater();
259  }
260  }
261 
262  // Success
263  return true;
264  }
265 
266  // Timed out
267  return false;
268 
269 #elif defined linux
270  // NB needs script in /etc/udev/rules.d
271  mkfifo(kUDEV_FIFO, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
272  m_fifo = open(kUDEV_FIFO, O_RDONLY | O_NONBLOCK);
273 
274  QDir sysfs("/sys/block");
275  sysfs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
276 
277  QStringList devices = sysfs.entryList();
278  foreach (auto & device, devices)
279  {
280  // ignore floppies, too slow
281  if (device.startsWith("fd"))
282  continue;
283 
284  sysfs.cd(device);
285  QString path = sysfs.absolutePath();
286  if (CheckRemovable(path))
287  FindPartitions(path, true);
288  sysfs.cdUp();
289  }
290  return true;
291 #else // linux
292  return false;
293 #endif
294 }
295 
296 #if !CONFIG_QTDBUS
297 
300 bool MediaMonitorUnix::CheckRemovable(const QString &dev)
301 {
302 #ifdef linux
303  QString removablePath = dev + "/removable";
304  QFile removable(removablePath);
305  if (removable.exists() && removable.open(QIODevice::ReadOnly))
306  {
307  char c = 0;
308  QString msg = LOC + ":CheckRemovable(" + dev + ")/removable ";
309  bool ok = removable.getChar(&c);
310  removable.close();
311 
312  if (ok)
313  {
314  LOG(VB_MEDIA, LOG_DEBUG, msg + c);
315  if (c == '1')
316  return true;
317  }
318  else
319  {
320  LOG(VB_GENERAL, LOG_ALERT, msg + "failed");
321  }
322  }
323  return false;
324 #else // if !linux
325  return false;
326 #endif // !linux
327 }
328 
334 QString MediaMonitorUnix::GetDeviceFile(const QString &sysfs)
335 {
336  QString msg = LOC + ":GetDeviceFile(" + sysfs + ")";
337  QString ret = sysfs;
338 
339  // In case of error, a working default? (device names usually match)
340  ret.replace(QRegExp(".*/"), "/dev/");
341 
342 #ifdef linux
343  #if HAVE_LIBUDEV
344  // Use libudev to determine the name
345  ret.clear();
346  struct udev *udev = udev_new();
347  if (udev != nullptr)
348  {
349  struct udev_device *device =
350  udev_device_new_from_syspath(udev, sysfs.toLatin1().constData());
351  if (device != nullptr)
352  {
353  const char *name = udev_device_get_devnode(device);
354 
355  if (name != nullptr)
356  ret = tr(name);
357  else
358  {
359  // This can happen when udev sends an AddDevice for a block
360  // device with partitions. FindPartition locates a partition
361  // in sysfs but udev hasn't created the devnode for it yet.
362  // Udev will send another AddDevice for the partition later.
363  LOG(VB_MEDIA, LOG_DEBUG, msg + " devnode not (yet) known");
364  }
365 
366  udev_device_unref(device);
367  }
368  else
369  {
370  LOG(VB_GENERAL, LOG_ALERT,
371  msg + " udev_device_new_from_syspath returned NULL");
372  ret = "";
373  }
374 
375  udev_unref(udev);
376  }
377  else
378  LOG(VB_GENERAL, LOG_ALERT,
379  "MediaMonitorUnix::GetDeviceFile udev_new failed");
380  #else // HAVE_LIBUDEV
381  // Use udevadm info to determine the name
382  QStringList args;
383  args << "info" << "-q" << "name"
384  << "-rp" << sysfs;
385 
386  uint flags = kMSStdOut;
387  if (VERBOSE_LEVEL_CHECK(VB_MEDIA, LOG_DEBUG))
388  flags |= kMSStdErr;
389 
390  // TODO: change this to a MythSystemLegacy on the stack?
391  MythSystemLegacy *udevinfo = new MythSystemLegacy("udevinfo", args, flags);
392  udevinfo->Run(4);
393  if( udevinfo->Wait() != GENERIC_EXIT_OK )
394  {
395  delete udevinfo;
396  return ret;
397  }
398 
399  if (VERBOSE_LEVEL_CHECK(VB_MEDIA, LOG_DEBUG))
400  {
401  QTextStream estream(udevinfo->ReadAllErr());
402  while( !estream.atEnd() )
403  LOG(VB_MEDIA, LOG_DEBUG,
404  msg + " - udevadm info error...\n" + estream.readLine());
405  }
406 
407  QTextStream ostream(udevinfo->ReadAll());
408  QString udevLine = ostream.readLine();
409  if (!udevLine.startsWith("device not found in database") )
410  ret = udevLine;
411 
412  delete udevinfo;
413  #endif // HAVE_LIBUDEV
414 #endif // linux
415 
416  LOG(VB_MEDIA, LOG_INFO, msg + "->'" + ret + "'");
417  return ret;
418 }
419 #endif // !CONFIG_QTDBUS
420 
421 /*
422  * \brief Reads the list devices known to be CD or DVD devices.
423  * \return list of CD and DVD device names.
424  */
425 // pure virtual
427 {
428  QStringList l;
429 
430 #if CONFIG_QTDBUS
431  QDBusInterface iface(UDISKS_SVC, UDISKS_PATH, UDISKS_IFACE,
432  QDBusConnection::systemBus() );
433  if (iface.isValid())
434  {
435  // Enumerate devices
436  using QDBusObjectPathList = QList<QDBusObjectPath>;
437  QDBusReply<QDBusObjectPathList> reply = iface.call("EnumerateDevices");
438  if (reply.isValid())
439  {
440  const QDBusObjectPathList& list(reply.value());
441  foreach (const auto & entry, list)
442  {
443  if (DeviceProperty(entry, "DeviceIsRemovable").toBool())
444  {
445  QString dev = DeviceProperty(entry, "DeviceFile").toString();
446  if (dev.startsWith("/dev/"))
447  dev.remove(0,5);
448  l.push_back(dev);
449  }
450  }
451  }
452  }
453 
454 #elif defined linux
455  QFile file("/proc/sys/dev/cdrom/info");
456  if (file.open(QIODevice::ReadOnly))
457  {
458  QString line;
459  QTextStream stream(&file);
460  do
461  {
462  line = stream.readLine();
463  if (line.startsWith("drive name:"))
464  {
465  l = line.split('\t', QString::SkipEmptyParts);
466  l.pop_front(); // Remove 'drive name:' field
467  break; // file should only contain one drive table?
468  }
469  }
470  while (!stream.atEnd());
471  file.close();
472  }
473 #endif // linux
474 
475  LOG(VB_MEDIA, LOG_DEBUG,
476  LOC + ":GetCDROMBlockDevices()->'" + l.join(", ") + "'");
477  return l;
478 }
479 
480 static void LookupModel(MythMediaDevice* device)
481 {
482  QString desc;
483 
484 #if CONFIG_QTDBUS
485  QDBusInterface iface(UDISKS_SVC, UDISKS_PATH, UDISKS_IFACE,
486  QDBusConnection::systemBus() );
487  if (iface.isValid())
488  {
489  QDBusReply<QDBusObjectPath> reply = iface.call(
490  "FindDeviceByDeviceFile", device->getRealDevice());
491  if (reply.isValid())
492  {
493  desc = DeviceProperty(reply, "DriveVendor").toString();
494  if (!desc.isEmpty())
495  desc += " ";
496  desc += DeviceProperty(reply, "DriveModel").toString();
497  }
498  }
499 
500 #elif defined linux
501 
502  // Given something like /dev/hda1, extract hda1
503  QString devname = device->getRealDevice().mid(5,5);
504 
505  if (devname.startsWith("hd")) // IDE drive
506  {
507  QFile file("/proc/ide/" + devname.left(3) + "/model");
508  if (file.open(QIODevice::ReadOnly))
509  {
510  QTextStream stream(&file);
511 
512  desc.append(stream.readLine());
513  file.close();
514  }
515  }
516 
517  if (devname.startsWith("scd")) // scd0 doesn't appear in /sys/block,
518  devname.replace("scd", "sr"); // use sr0 instead
519 
520  if (devname.startsWith("sd") // SATA/USB/FireWire
521  || devname.startsWith("sr")) // SCSI CD-ROM?
522  {
523  QString path = devname.prepend("/sys/block/");
524  path.append("/device/");
525 
526  QFile file(path + "vendor");
527  if (file.open(QIODevice::ReadOnly))
528  {
529  QTextStream stream(&file);
530 
531  desc.append(stream.readLine());
532  desc.append(' ');
533  file.close();
534  }
535 
536  file.setFileName(path + "model");
537  if (file.open(QIODevice::ReadOnly))
538  {
539  QTextStream stream(&file);
540 
541  desc.append(stream.readLine());
542  desc.append(' ');
543  file.close();
544  }
545  }
546 #endif
547 
548  LOG(VB_MEDIA, LOG_DEBUG, QString("LookupModel '%1' -> '%2'")
549  .arg(device->getRealDevice()).arg(desc) );
550  device->setDeviceModel(desc.toLatin1().constData());
551 }
552 
557 {
558  if ( ! pDevice )
559  {
560  LOG(VB_GENERAL, LOG_ERR, "MediaMonitorUnix::AddDevice(null)");
561  return false;
562  }
563 
564  // If the user doesn't want this device to be monitored, stop now:
565  if (shouldIgnore(pDevice))
566  return false;
567 
568  QString path = pDevice->getDevicePath();
569  if (!path.length())
570  {
571  LOG(VB_GENERAL, LOG_ALERT,
572  "MediaMonitorUnix::AddDevice() - empty device path.");
573  return false;
574  }
575 
576  struct stat sb {};
577  if (stat(path.toLocal8Bit().constData(), &sb) < 0)
578  {
579  statError(":AddDevice()", path);
580  return false;
581  }
582  dev_t new_rdev = sb.st_rdev;
583 
584  //
585  // Check if this is a duplicate of a device we have already added
586  //
587  foreach (auto & device, m_Devices)
588  {
589  if (stat(device->getDevicePath().toLocal8Bit().constData(), &sb) < 0)
590  {
591  statError(":AddDevice()", device->getDevicePath());
592  return false;
593  }
594 
595  if (sb.st_rdev == new_rdev)
596  {
597  LOG(VB_MEDIA, LOG_INFO,
598  LOC + ":AddDevice() - not adding " + path +
599  "\n "
600  "because it appears to be a duplicate of " +
601  device->getDevicePath());
602  return false;
603  }
604  }
605 
606  LookupModel(pDevice);
607 
608  QMutexLocker locker(&m_DevicesLock);
609 
610  connect(pDevice, SIGNAL(statusChanged(MythMediaStatus, MythMediaDevice*)),
612  m_Devices.push_back( pDevice );
613  m_UseCount[pDevice] = 0;
614  LOG(VB_MEDIA, LOG_INFO, LOC + ":AddDevice() - Added " + path);
615 
616  return true;
617 }
618 
619 // Given a fstab entry to a media device determine what type of device it is
620 bool MediaMonitorUnix::AddDevice(struct fstab * mep)
621 {
622  if (!mep)
623  return false;
624 
625 #ifndef Q_OS_ANDROID
626  QString devicePath( mep->fs_spec );
627 #if 0
628  LOG(VB_GENERAL, LOG_DEBUG, "AddDevice - " + devicePath);
629 #endif
630 
631  MythMediaDevice* pDevice = nullptr;
632  struct stat sbuf {};
633 
634  bool is_supermount = false;
635  bool is_cdrom = false;
636 
637  if (stat(mep->fs_spec, &sbuf) < 0)
638  return false;
639 
640  // Can it be mounted?
641  if ( ! ( ((strstr(mep->fs_mntops, "owner") &&
642  (sbuf.st_mode & S_IRUSR)) || strstr(mep->fs_mntops, "user")) &&
643  (strstr(mep->fs_vfstype, MNTTYPE_ISO9660) ||
644  strstr(mep->fs_vfstype, MNTTYPE_UDF) ||
645  strstr(mep->fs_vfstype, MNTTYPE_AUTO)) ) )
646  {
647  if (strstr(mep->fs_mntops, MNTTYPE_ISO9660) &&
648  strstr(mep->fs_vfstype, MNTTYPE_SUPERMOUNT))
649  {
650  is_supermount = true;
651  }
652  else
653  {
654  return false;
655  }
656  }
657 
658  if (strstr(mep->fs_mntops, MNTTYPE_ISO9660) ||
659  strstr(mep->fs_vfstype, MNTTYPE_ISO9660) ||
660  strstr(mep->fs_vfstype, MNTTYPE_UDF) ||
661  strstr(mep->fs_vfstype, MNTTYPE_AUTO))
662  {
663  is_cdrom = true;
664 #if 0
665  LOG(VB_GENERAL, LOG_DEBUG, "Device is a CDROM");
666 #endif
667  }
668 
669  if (!is_supermount)
670  {
671  if (is_cdrom)
672  pDevice = MythCDROM::get(this, mep->fs_spec,
673  is_supermount, m_AllowEject);
674  }
675  else
676  {
677  char *dev = nullptr;
678  int len = 0;
679  dev = strstr(mep->fs_mntops, SUPER_OPT_DEV);
680  if (dev == nullptr)
681  return false;
682 
683  dev += sizeof(SUPER_OPT_DEV)-1;
684  while (dev[len] != ',' && dev[len] != ' ' && dev[len] != 0)
685  len++;
686 
687  if (dev[len] != 0)
688  {
689  char devstr[256];
690  strncpy(devstr, dev, len);
691  devstr[len] = 0;
692  if (is_cdrom)
693  pDevice = MythCDROM::get(this, devstr,
694  is_supermount, m_AllowEject);
695  }
696  else
697  return false;
698  }
699 
700  if (pDevice)
701  {
702  pDevice->setMountPath(mep->fs_file);
703  if (pDevice->testMedia() == MEDIAERR_OK)
704  {
705  if (MediaMonitorUnix::AddDevice(pDevice))
706  return true;
707  }
708  pDevice->deleteLater();
709  }
710 #endif
711 
712  return false;
713 }
714 
715 #if CONFIG_QTDBUS
716 /*
717  * DBus UDisk AddDevice handler
718  */
719 void MediaMonitorUnix::deviceAdded( const QDBusObjectPath& o)
720 {
721  LOG(VB_MEDIA, LOG_INFO, LOC + ":deviceAdded " + o.path());
722 
723  // Don't add devices with partition tables, just the partitions
724  if (!DeviceProperty(o, "DeviceIsPartitionTable").toBool())
725  {
726  QString dev = DeviceProperty(o, "DeviceFile").toString();
727 
728  MythMediaDevice* pDevice = nullptr;
729  if (DeviceProperty(o, "DeviceIsRemovable").toBool())
730  pDevice = MythCDROM::get(this, dev.toLatin1(), false, m_AllowEject);
731  else
732  pDevice = MythHDD::Get(this, dev.toLatin1(), false, false);
733 
734  if (pDevice && !AddDevice(pDevice))
735  pDevice->deleteLater();
736  }
737 }
738 
739 /*
740  * DBus UDisk RemoveDevice handler
741  */
742 void MediaMonitorUnix::deviceRemoved( const QDBusObjectPath& o)
743 {
744  LOG(VB_MEDIA, LOG_INFO, LOC + "deviceRemoved " + o.path());
745 #if 0 // This fails because the DeviceFile has just been deleted
746  QString dev = DeviceProperty(o, "DeviceFile");
747  if (!dev.isEmpty())
748  RemoveDevice(dev);
749 #else
750  QString dev = QFileInfo(o.path()).baseName();
751  dev.prepend("/dev/");
752  RemoveDevice(dev);
753 #endif
754 }
755 
756 #else //CONFIG_QTDBUS
757 
774 bool MediaMonitorUnix::FindPartitions(const QString &dev, bool checkPartitions)
775 {
776  LOG(VB_MEDIA, LOG_DEBUG,
777  LOC + ":FindPartitions(" + dev +
778  QString(",%1").arg(checkPartitions ? " true" : " false" ) + ")");
779  MythMediaDevice* pDevice = nullptr;
780 
781  if (checkPartitions)
782  {
783  // check for partitions
784  QDir sysfs(dev);
785  sysfs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
786 
787  bool found_partitions = false;
788  QStringList parts = sysfs.entryList();
789  foreach (auto & part, parts)
790  {
791  // skip some sysfs dirs that are _not_ sub-partitions
792  if (part == "device" || part == "holders" || part == "queue"
793  || part == "slaves" || part == "subsystem"
794  || part == "bdi" || part == "power")
795  continue;
796 
797  found_partitions |= FindPartitions(
798  sysfs.absoluteFilePath(part), false);
799  }
800 
801  // no partitions on block device, use main device
802  if (!found_partitions)
803  found_partitions |= FindPartitions(sysfs.absolutePath(), false);
804 
805  return found_partitions;
806  }
807 
808  QString device_file = GetDeviceFile(dev);
809 
810  if (device_file.isEmpty())
811  return false;
812 
813  QStringList cdroms = GetCDROMBlockDevices();
814 
815  if (cdroms.contains(dev.section('/', -1)))
816  {
817  // found cdrom device
818  pDevice = MythCDROM::get(
819  this, device_file.toLatin1().constData(), false, m_AllowEject);
820  }
821  else
822  {
823  // found block or partition device
824  pDevice = MythHDD::Get(
825  this, device_file.toLatin1().constData(), false, false);
826  }
827 
828  if (AddDevice(pDevice))
829  return true;
830 
831  if (pDevice)
832  pDevice->deleteLater();
833 
834  return false;
835 }
836 
843 {
844  char buffer[256];
845  QString qBuffer;
846 
847  if (m_fifo == -1)
848  return;
849 
850  int size = read(m_fifo, buffer, 255);
851  while (size > 0)
852  {
853  // append buffer to QString
854  buffer[size] = '\0';
855  qBuffer.append(buffer);
856  size = read(m_fifo, buffer, 255);
857  }
858  const QStringList list = qBuffer.split('\n', QString::SkipEmptyParts);
859  foreach (const auto & notif, list)
860  {
861  if (notif.startsWith("add"))
862  {
863  QString dev = notif.section(' ', 1, 1);
864  LOG(VB_MEDIA, LOG_INFO, "Udev add " + dev);
865 
866  if (CheckRemovable(dev))
867  FindPartitions(dev, true);
868  }
869  else if (notif.startsWith("remove"))
870  {
871  QString dev = notif.section(' ', 2, 2);
872  LOG(VB_MEDIA, LOG_INFO, "Udev remove " + dev);
873  RemoveDevice(dev);
874  }
875  }
876 }
877 #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
#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.
#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
unsigned int uint
Definition: compat.h:140
#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
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
allow access to stderr
Definition: mythsystem.h:40
allow access to stdout
Definition: mythsystem.h:39
static const char * kUDEV_FIFO