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