File: threadscan.cpp

package info (click to toggle)
guymager 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 932 kB
  • ctags: 1,580
  • sloc: cpp: 8,522; ansic: 2,571; makefile: 49; sh: 23
file content (566 lines) | stat: -rw-r--r-- 19,971 bytes parent folder | download
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
// ****************************************************************************
//  Project:        GUYMAGER
// ****************************************************************************
//  Programmer:     Guy Voncken
//                  Police Grand-Ducale
//                  Service de Police Judiciaire
//                  Section Nouvelles Technologies
// ****************************************************************************
//  Module:         Thread for scanning the connected devices
// ****************************************************************************

#include <QtCore>
#include <QtDBus/QtDBus>

#include <dlfcn.h>

#include "toolconstants.h"

#include "common.h"
#include "config.h"
#include "device.h"
#include "qtutil.h"
#include "threadscan.h"

// --------------------------
//         Constants
// --------------------------

const int THREADSCAN_WAIT_MAX         = 5000;
const int THREADSCAN_WAIT_GRANULARITY = 100;

#define   LIBPARTED  "libparted.so"

// -----------------------------------
//  Utility functions used by workers
// -----------------------------------

static APIRET ThreadScanMarkLocalDevices (const t_pDeviceList pDeviceList)
{
   QStringList *pLocalDevices;
   t_pDevice    pDevice;
   int           i;

   CHK_EXIT (CfgGetLocalDevices (&pLocalDevices))
   for (i=0; i<pDeviceList->count(); i++)
   {
      pDevice = pDeviceList->at (i);
      pDevice->Local = pLocalDevices->contains (pDevice->SerialNumber) ||
                       pLocalDevices->contains (pDevice->LinuxDevice );
   }
   return NO_ERROR;
}

// --------------------------
//  t_ThreadScanWorkerParted
// --------------------------

// Scan the devices using libparted and bash command to gather device info

typedef  void       (*t_pFnLibPartedProbeAll) (void) ;
typedef  PedDevice* (*t_pFnLibPartedGetNext ) (const PedDevice* pDev);
typedef  void       (*t_pFnLibPartedFreeAll ) (void);

class t_ThreadScanWorkerPartedLocal
{
   public:
      t_ThreadScanWorkerPartedLocal ()
         :LibPartedLoaded      (false), pLibPartedHandle     (NULL),
          pFnLibPartedProbeAll (NULL) ,
          pFnLibPartedGetNext  (NULL) ,
          pFnLibPartedFreeAll  (NULL)
      {
      };

   public:
      bool         LibPartedLoaded;
      void       *pLibPartedHandle;
      t_pFnLibPartedProbeAll pFnLibPartedProbeAll;
      t_pFnLibPartedGetNext  pFnLibPartedGetNext;
      t_pFnLibPartedFreeAll  pFnLibPartedFreeAll;
};


static APIRET ThreadScanGetSerialNumber (char const *pcDeviceName, QString &SerialNumber)
{
   QString Command;
   APIRET  rc;

   Command = CONFIG (CommandGetSerialNumber);
   Command.replace ("%dev", pcDeviceName, Qt::CaseInsensitive);

   rc = QtUtilProcessCommand (Command, SerialNumber);
   if (rc == ERROR_QTUTIL_COMMAND_TIMEOUT)
      return rc;
   CHK(rc)
   SerialNumber = SerialNumber.trimmed();

   return NO_ERROR;
}

t_ThreadScanWorkerParted::t_ThreadScanWorkerParted (APIRET &rc)
{
   char *pErrStr;

   pOwn = new t_ThreadScanWorkerPartedLocal();

   pOwn->pLibPartedHandle = dlopen (LIBPARTED, RTLD_NOW);
   if (pOwn->pLibPartedHandle == NULL)
   {
      LOG_INFO ("%s cannot be found (%s).", LIBPARTED, dlerror());
      rc = ERROR_THREADSCAN_LIBPARTED_NOTWORKING;
      return;
   }

   // GETFN: See dlsym documentation concerning the usage of dlerror for error checking
   #define GETFN(pFn, Type, pFnName)                                           \
      dlerror();                                                               \
      pFn = (Type)dlsym (pOwn->pLibPartedHandle, pFnName);                     \
      pErrStr = dlerror();                                                     \
      if (pErrStr)                                                             \
      {                                                                        \
         LOG_INFO ("Function %s cannot be found (%s).", pFnName, pErrStr);     \
         rc = ERROR_THREADSCAN_LIBPARTED_NOTWORKING;                           \
         return;                                                               \
      }

   GETFN (pOwn->pFnLibPartedProbeAll, t_pFnLibPartedProbeAll, "ped_device_probe_all")
   GETFN (pOwn->pFnLibPartedGetNext , t_pFnLibPartedGetNext , "ped_device_get_next" )
   GETFN (pOwn->pFnLibPartedFreeAll , t_pFnLibPartedFreeAll , "ped_device_free_all" )

   rc = NO_ERROR;
}

t_ThreadScanWorkerParted::~t_ThreadScanWorkerParted (void)
{
   int rc;

   if (pOwn->pLibPartedHandle)
   {
      rc = dlclose (pOwn->pLibPartedHandle);
      if (rc)
         LOG_INFO ("dlclose returned %d (%s).", rc, dlerror());
   }

   delete pOwn;
}


void t_ThreadScanWorkerParted::SlotRescan (void)
{
   t_pDeviceList  pDeviceList;
   t_pDevice      pDevice;
   PedDevice     *pPedDev;
   QString         SerialNumber;
   APIRET          rc;

   emit (SignalScanStarted ());
   LOG_INFO ("Rescanning devices")

   pOwn->pFnLibPartedProbeAll ();

   pDeviceList = new t_DeviceList;
   pPedDev     = NULL;
   while ((pPedDev = pOwn->pFnLibPartedGetNext (pPedDev)))  //lint !e820
   {
      rc = ThreadScanGetSerialNumber (pPedDev->path, SerialNumber);
      if (rc == ERROR_QTUTIL_COMMAND_TIMEOUT)
      {
         LOG_INFO ("Device scan aborted due to timeout while trying to get serial number for %s", pPedDev->path)
         pOwn->pFnLibPartedFreeAll ();
         delete pDeviceList;
         return;
      }
      pDevice = pDeviceList->AppendNew (SerialNumber, pPedDev);
      if (pDevice->LinuxDevice.startsWith ("/dev/fd",  Qt::CaseSensitive)) // Not a good way for checking this, but I don't know how to extract the information from
         pDevice->Removable = true;                                        // PedDevice. BTW, this won't work with other removable devices, for instance memory sticks.
   }
   pOwn->pFnLibPartedFreeAll ();
   CHK_EXIT (ThreadScanMarkLocalDevices (pDeviceList))

   emit (SignalScanFinished (pDeviceList));
}

// --------------------------
//  t_ThreadScanWorkerHAL
// --------------------------

// Scan the devices using DBUS/HAL

#define HAL_SERVICE      "org.freedesktop.Hal"
#define HAL_MANAGER_PATH "/org/freedesktop/Hal/Manager"
#define HAL_MANAGER_IF   "org.freedesktop.Hal.Manager"
#define HAL_DEVICE_IF    "org.freedesktop.Hal.Device"

class t_ThreadScanWorkerHALLocal
{
   public:
      t_ThreadScanWorkerHALLocal (void)
      {
         pDBusConnection = new QDBusConnection (QDBusConnection::systemBus());
         pDBusInterface  = NULL;
      }

     ~t_ThreadScanWorkerHALLocal ()
      {
         QDBusConnection::disconnectFromBus (pDBusConnection->baseService());
         delete pDBusConnection;
         pDBusConnection = NULL;
         pDBusInterface  = NULL;
      }

   public:
      QDBusConnection          *pDBusConnection;
      QDBusConnectionInterface *pDBusInterface;
};


t_ThreadScanWorkerHAL::t_ThreadScanWorkerHAL (APIRET &rc)
{
   pOwn = new t_ThreadScanWorkerHALLocal;
   if (!pOwn->pDBusConnection->isConnected())
   {
      LOG_INFO ("DBus connection failed")
      rc = ERROR_THREADSCAN_DBUSHAL_NOTWORKING;
      return;
   }

   pOwn->pDBusInterface = pOwn->pDBusConnection->interface();
   if (!pOwn->pDBusInterface->isServiceRegistered (HAL_SERVICE))
   {
      LOG_INFO ("HAL not registered")
      rc = ERROR_THREADSCAN_DBUSHAL_NOTWORKING;
      return;
   }
   rc = NO_ERROR;
}

t_ThreadScanWorkerHAL::~t_ThreadScanWorkerHAL (void)
{
   delete pOwn;
}

QList<QVariant> t_ThreadScanWorkerHAL::CallMethod (const QString &Device, const QString &Method, const QString &Argument)
{
   QList<QVariant> Args;
   QDBusMessage    Message = QDBusMessage::createMethodCall (HAL_SERVICE, Device, HAL_DEVICE_IF, Method);
   QDBusMessage    Reply;

   Args += Argument;
   Message.setArguments (Args);
   Reply = pOwn->pDBusConnection->call (Message);

   if (Reply.type() == QDBusMessage::ErrorMessage)
   {
      LOG_ERROR ("DBus returned '%s' for method %s on device %s", QSTR_TO_PSZ (Reply.errorName()),
                                                                  QSTR_TO_PSZ (Method),
                                                                  QSTR_TO_PSZ (Device))
      return QList<QVariant>(); // return empty list
   }

   return Reply.arguments();
}

QVariant t_ThreadScanWorkerHAL::CallMethodSingle (const QString &Device, const QString &Method, const QString &Argument)
{
   QList<QVariant> Results;

   Results = CallMethod (Device, Method, Argument);
   if (Results.first().isNull())
      return QVariant();

   return Results.first();
}

APIRET t_ThreadScanWorkerHAL::GetProperty (const QString &Device, const QString &Property, QList<QVariant> &VarList)
{
   QVariant Exists;

   Exists = CallMethodSingle (Device, "PropertyExists", Property);
   if (Exists.toBool())
        VarList = CallMethod (Device, "GetProperty", Property);
   else VarList = QList<QVariant>();  // Construct an empty, invalid list to show that the property doesn't exist

   return NO_ERROR;
}

APIRET t_ThreadScanWorkerHAL::GetPropertySingle (const QString &Device, const QString &Property, QVariant &Var)
{
   Var = CallMethodSingle (Device, "PropertyExists", Property);
   if (Var.toBool())
        Var = CallMethodSingle (Device, "GetProperty", Property);
   else Var = QVariant();  // Construct an empty, invalid QVariant to show that the property doesn't exist

   return NO_ERROR;
}

bool t_ThreadScanWorkerHAL::PropertyContains (const QString &Device, const QString &Property, const QString &Str)
{
   QList<QVariant> PropertyElements;
   bool            Found = false;

   CHK_EXIT (GetProperty (Device, Property, PropertyElements))

   foreach (QVariant Element, PropertyElements)
   {
      if (Element.isValid())
      {
         switch (Element.type())
         {
            case QVariant::String    : Found = (Element.toString() == Str);                                  break;
            case QVariant::StringList: Found =  Element.toStringList().contains (Str, Qt::CaseInsensitive);  break;
            default                  : break;
         }
      }
      if (Found)
         break;
   }
   return Found;
}

void t_ThreadScanWorkerHAL::SlotRescan (void)
{
   t_pDeviceList   pDeviceList=NULL;
   t_pDevice       pDevice;
   QDBusMessage     Message;
   QDBusMessage     Reply;
   QStringList      DeviceList;
   QVariant         Category;
   QVariant         SerialNumber;
   QVariant         LinuxDevice;
   QVariant         Vendor;
   QVariant         Product;
   QVariant         Size;
   QVariant         Removable;
   QVariant         RemovableAvailable;
   QString          Model;

   if (thread() != QThread::currentThread())
      CHK_EXIT (ERROR_THREADSCAN_CALLED_FROM_WRONG_THREAD) // As Qt's DBus system is quite sensible to this kind of
                                                           // mistake (resulting in many QTimer "cannot be stopped/started
                                                           // from another thread) we prefer to do the check here!
   emit (SignalScanStarted ());
   LOG_INFO ("Device scan started")

   Message = QDBusMessage::createMethodCall (HAL_SERVICE, HAL_MANAGER_PATH, HAL_MANAGER_IF, "GetAllDevices");
   Reply   = pOwn->pDBusConnection->call (Message);
   if (Reply.type() == QDBusMessage::ErrorMessage)
   {
      LOG_ERROR ("DBus GetAllDevices returned %s", QSTR_TO_PSZ(Reply.errorName()))
   }
   else
   {
      pDeviceList = new t_DeviceList;
      DeviceList  = Reply.arguments().first().toStringList();

      //lint -save -e155 -e521
      foreach (QString HalDevice, DeviceList)
      //lint -restore
      {
         // Do not check the info.category any longer. It has been observed, that it may contain many different
         // strings. For example, a phone had "portable_audio_player" in its info.category and was not detected,
         // eventhough it was perfectly readable by dd.
//         CHK_EXIT (GetPropertySingle (HalDevice, "info.category", Category))
//         if (!Category.isValid())
//            continue;
//         if (Category != "storage")
//            continue;

         CHK_EXIT (GetPropertySingle (HalDevice, "block.device", LinuxDevice))
         if (!LinuxDevice.isValid())
            continue;

         if (PropertyContains (HalDevice, "info.capabilities", "volume"))  // we only are interested in complete device (/dev/sda), not in volumes (/dev/sda1)
            continue;

         CHK_EXIT (GetPropertySingle (HalDevice, "storage.removable", Removable))
         if (!Removable.isValid())
         {
            LOG_INFO ("Strange, %s is a block device but has no storage.removable property", QSTR_TO_PSZ(HalDevice))
            continue;
         }
         if (Removable.toBool())
         {
            CHK_EXIT (GetPropertySingle (HalDevice, "storage.removable.media_available", RemovableAvailable))
            if (RemovableAvailable.toBool())
                 CHK_EXIT (GetPropertySingle (HalDevice, "storage.removable.media_size", Size))
            else continue;
         }
         else
         {
            CHK_EXIT (GetPropertySingle (HalDevice, "storage.size", Size))
         }

         CHK_EXIT (GetPropertySingle (HalDevice, "storage.serial", SerialNumber))
         CHK_EXIT (GetPropertySingle (HalDevice, "info.vendor"   , Vendor      ))
         CHK_EXIT (GetPropertySingle (HalDevice, "info.product"  , Product     ))
         if (!SerialNumber.isValid())
            SerialNumber = "";  // Attention: Empty string must be used, else t_DeviceList::MatchDevice doesn't work
         if (!Vendor.isValid() && !Product.isValid())
              Model = "--";
         else Model = Vendor.toString() + " " + Product.toString();
         Model = Model.trimmed();

//         if (SerialNumber.toString().contains("Hitachi", Qt::CaseInsensitive))
//            Size  = QVariant ((qulonglong)1024*1024*1024*32);  // for test purposes
         pDevice = pDeviceList->AppendNew (SerialNumber.toString(), LinuxDevice.toString(), Model,
                                           DEVICE_DEFAULT_SECTOR_SIZE, DEVICE_DEFAULT_SECTOR_SIZE, Size.toULongLong());
         pDevice->Removable = Removable.toBool();
      }
   }
   CHK_EXIT (ThreadScanMarkLocalDevices (pDeviceList))
   LOG_INFO ("Device scan finished")

   emit (SignalScanFinished (pDeviceList));
}

// --------------------------
//        t_ThreadScan
// --------------------------

static APIRET ThreadScanRegisterErrorCodes (void)
{
   static bool Initialised = false;

   if (!Initialised)
   {
      Initialised = true;
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_NOT_STARTED             ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_NOT_STOPPED             ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_EXITCODE_NONZERO        ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_PROCESS_NOTSTARTED      ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_PROCESS_NOTFINISHED     ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_DBUSHAL_NOTWORKING      ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_LIBPARTED_NOTWORKING    ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_PROPERTY_NONEXISTENT    ))
      CHK (TOOL_ERROR_REGISTER_CODE (ERROR_THREADSCAN_CALLED_FROM_WRONG_THREAD))
   }
   return NO_ERROR;
}

t_ThreadScan::t_ThreadScan (void)
{
   CHK_EXIT (ThreadScanRegisterErrorCodes())
   ppoWorker = NULL;  //lint -esym(613, t_ThreadScan::ppoWorker)  Possible use of NULL pointer
}


static APIRET ThreadScanAskUser (bool SwitchToDBusHAL, bool &Ok)
{
   QMessageBox::StandardButton   Button;
   const char                  *pFrom, *pTo;

   pFrom =  SwitchToDBusHAL ? "libparted" : "DBus/HAL";
   pTo   = !SwitchToDBusHAL ? "libparted" : "DBus/HAL";
   LOG_INFO ("%s not accessible, asking user if he wants to switch to %s...", pFrom, pTo)
   Button = QMessageBox::question (NULL, QObject::tr ("%1 not accessible", "Dialog title") .arg(pFrom),
                                         QObject::tr ("Guymager is configured to use %1 for scanning the connected devices, but this "
                                                      "did not work. Alternatively, you may use %2. Do you want to switch to %2?")
                                                      .arg(pFrom) .arg(pTo), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
   Ok = (Button == QMessageBox::Yes);
   if (Ok)
   {
      LOG_INFO ("User switches to %s", pTo)
      CONFIG (ScanUsingDbusHal) = SwitchToDBusHAL;
   }
   else
   {
      LOG_INFO ("User doesn't want to switch. Exiting now.")
   }

   return NO_ERROR;
}

APIRET t_ThreadScan::Start (t_ThreadScanWorker **ppWorker)
{
   int  Wait;
   bool Ok;
   bool SwitchToDBusHAL;

   #define LAUNCH_WORKER                                                                    \
   {                                                                                        \
      *ppWorker   = NULL;                                                                   \
      ppoWorker   = ppWorker;                                                               \
        oWorkerRc = NO_ERROR;                                                               \
      start();                                                                              \
      /* start(QThread::HighPriority); */                                                   \
      for ( Wait =  0;                                                                      \
           (Wait <  THREADSCAN_WAIT_MAX) && (*ppWorker == NULL) && (oWorkerRc==NO_ERROR);   \
            Wait += THREADSCAN_WAIT_GRANULARITY)                                            \
         msleep (THREADSCAN_WAIT_GRANULARITY);                                              \
   }

   LAUNCH_WORKER;

   if ((oWorkerRc == ERROR_THREADSCAN_DBUSHAL_NOTWORKING) ||
       (oWorkerRc == ERROR_THREADSCAN_LIBPARTED_NOTWORKING))
   {
      SwitchToDBusHAL = (oWorkerRc == ERROR_THREADSCAN_LIBPARTED_NOTWORKING);
      CHK (ThreadScanAskUser (SwitchToDBusHAL, Ok))
      if (Ok)
         LAUNCH_WORKER;
   }
   #undef LAUNCH_WORKER

   CHK (oWorkerRc)
   if (*ppWorker == NULL)
      CHK (ERROR_THREADSCAN_NOT_STARTED)

   return NO_ERROR;
}

APIRET t_ThreadScan::Stop ()
{
   time_t BeginT, NowT;
   int    Wait;

   quit(); // This tells t_ThreadScan::run to quit the exec() call

   time (&BeginT);  // As we do not know how much time is spent in the different calls
   do               // to processEvents, we have to measure the time ourselves
   {
      QCoreApplication::processEvents (QEventLoop::ExcludeUserInputEvents, THREADSCAN_WAIT_GRANULARITY);
      time (&NowT);
      Wait = (NowT-BeginT)*MSEC_PER_SECOND;
   } while ((Wait < THREADSCAN_WAIT_MAX) && (*ppoWorker != NULL));

   if (*ppoWorker != NULL)
      CHK (ERROR_THREADSCAN_NOT_STOPPED)

   return NO_ERROR;
}


void t_ThreadScan::run (void)
{
   QTimer *pTimer;
   int      rc;

   LOG_INFO ("Thread Scan started")

   if (CONFIG(ScanUsingDbusHal))
        *ppoWorker = new t_ThreadScanWorkerHAL    (oWorkerRc); // We have to create this object as we want to work with signals
   else *ppoWorker = new t_ThreadScanWorkerParted (oWorkerRc); // and slots in this new thread. t_ThreadScan itself belongs to
                                                               // the main thread and thus can't be used for signals and slots.
   if (oWorkerRc)
      return;

//   pTimer = new QTimer(this); // Do not take "this" as parent, as "this" doesn't belong to the current thread (would lead to the error "Cannot create children for a parent that is in a different thread").
   pTimer = new QTimer();
   CHK_QT_EXIT (connect (pTimer, SIGNAL(timeout()), *ppoWorker, SLOT(SlotRescan())))
   pTimer->start (CONFIG(ScanInterval) * MSEC_PER_SECOND);

   rc = exec(); // Enter event loop
   if (rc)
   {
      LOG_ERROR ("ThreadScan exits with code %d", rc)
      CHK_EXIT (ERROR_THREADSCAN_EXITCODE_NONZERO)
   }
   LOG_INFO ("Thread Scan ended")
   delete pTimer;
   delete *ppoWorker;
   *ppoWorker = NULL;
}