VirtualBox

source: vbox/trunk/src/VBox/Main/linux/HostHardwareLinux.cpp@ 29873

Last change on this file since 29873 was 29352, checked in by vboxsync, 15 years ago

Main/HostHardwareLinux: also check for changed permissions of USB device nodes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 91.7 KB
Line 
1/* $Id: HostHardwareLinux.cpp 29352 2010-05-11 13:23:15Z vboxsync $ */
2/** @file
3 * Classes for handling hardware detection under Linux. Please feel free to
4 * expand these to work for other systems (Solaris!) or to add new ones for
5 * other systems.
6 */
7
8/*
9 * Copyright (C) 2008-2010 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#define LOG_GROUP LOG_GROUP_MAIN
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25
26#include <HostHardwareLinux.h>
27
28#include <VBox/err.h>
29#include <VBox/log.h>
30
31#ifdef VBOX_USB_WITH_DBUS
32# include <VBox/dbus.h>
33#endif
34
35#include <iprt/asm.h>
36#include <iprt/dir.h>
37#include <iprt/env.h>
38#include <iprt/file.h>
39#include <iprt/mem.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/string.h>
43#include <iprt/thread.h> /* for RTThreadSleep() */
44
45#include <linux/cdrom.h>
46#include <linux/fd.h>
47#include <linux/major.h>
48#include <scsi/scsi.h>
49
50#include <iprt/linux/sysfs.h>
51
52#ifdef VBOX_USB_WITH_SYSFS
53# ifdef VBOX_USB_WITH_INOTIFY
54# include <dlfcn.h>
55# include <fcntl.h>
56# include <poll.h>
57# include <signal.h>
58# include <unistd.h>
59# endif
60#endif
61
62#include <vector>
63
64#include <errno.h>
65
66/******************************************************************************
67* Global Variables *
68******************************************************************************/
69
70#ifdef TESTCASE
71static bool testing() { return true; }
72static bool fNoProbe = false;
73static bool noProbe() { return fNoProbe; }
74static void setNoProbe(bool val) { fNoProbe = val; }
75#else
76static bool testing() { return false; }
77static bool noProbe() { return false; }
78static void setNoProbe(bool val) { (void)val; }
79#endif
80
81/******************************************************************************
82* Typedefs and Defines *
83******************************************************************************/
84
85/** When waiting for hotplug events, we currently restart the wait after at
86 * most this many milliseconds. */
87enum { DBUS_POLL_TIMEOUT = 2000 /* ms */ };
88
89static int getDriveInfoFromEnv(const char *pcszVar, DriveInfoList *pList,
90 bool isDVD, bool *pfSuccess);
91static int getDriveInfoFromDev(DriveInfoList *pList, bool isDVD,
92 bool *pfSuccess);
93static int getDriveInfoFromSysfs(DriveInfoList *pList, bool isDVD,
94 bool *pfSuccess);
95#ifdef VBOX_USB_WITH_SYSFS
96# ifdef VBOX_USB_WITH_INOTIFY
97static int getUSBDeviceInfoFromSysfs(USBDeviceInfoList *pList, bool *pfSuccess);
98
99/** Function object to be invoked on filenames from a directory. */
100class pathHandler
101{
102 /** Called on each element of the sysfs directory. Can e.g. store
103 * interesting entries in a list. */
104 virtual bool handle(const char *pcszNode) = 0;
105public:
106 bool doHandle(const char *pcszNode)
107 {
108 AssertPtr(pcszNode);
109 Assert(pcszNode[0] == '/');
110 return handle(pcszNode);
111 }
112};
113
114static int walkDirectory(const char *pcszPath, pathHandler *pHandler,
115 bool useRealPath);
116static int getDeviceInfoFromSysfs(const char *pcszPath, pathHandler *pHandler);
117# endif
118# ifdef VBOX_USB_WITH_DBUS
119/* These must be extern to be usable in the RTMemAutoPtr template */
120extern void VBoxHalShutdown (DBusConnection *pConnection);
121extern void VBoxHalShutdownPrivate (DBusConnection *pConnection);
122extern void VBoxDBusConnectionUnref(DBusConnection *pConnection);
123extern void VBoxDBusConnectionCloseAndUnref(DBusConnection *pConnection);
124extern void VBoxDBusMessageUnref(DBusMessage *pMessage);
125
126static int halInit(RTMemAutoPtr <DBusConnection, VBoxHalShutdown> *pConnection);
127static int halInitPrivate(RTMemAutoPtr <DBusConnection, VBoxHalShutdownPrivate> *pConnection);
128static int halFindDeviceStringMatch (DBusConnection *pConnection,
129 const char *pszKey, const char *pszValue,
130 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> *pMessage);
131/*
132static int halFindDeviceStringMatchVector (DBusConnection *pConnection,
133 const char *pszKey,
134 const char *pszValue,
135 std::vector<iprt::MiniString> *pMatches);
136*/
137static int halGetPropertyStrings (DBusConnection *pConnection,
138 const char *pszUdi, size_t cKeys,
139 const char **papszKeys, char **papszValues,
140 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> *pMessage);
141/*
142static int halGetPropertyStringsVector (DBusConnection *pConnection,
143 const char *pszUdi, size_t cProps,
144 const char **papszKeys,
145 std::vector<iprt::MiniString> *pMatches,
146 bool *pfMatches, bool *pfSuccess);
147*/
148static int getUSBDeviceInfoFromHal(USBDeviceInfoList *pList, bool *pfSuccess);
149static int getOldUSBDeviceInfoFromHal(USBDeviceInfoList *pList, bool *pfSuccess);
150static int getUSBInterfacesFromHal(std::vector <iprt::MiniString> *pList,
151 const char *pcszUdi, bool *pfSuccess);
152static DBusHandlerResult dbusFilterFunction (DBusConnection *pConnection,
153 DBusMessage *pMessage, void *pvUser);
154# endif /* VBOX_USB_WITH_DBUS */
155#endif /* VBOX_USB_WITH_SYSFS */
156
157
158/** Find the length of a string, ignoring trailing non-ascii or control
159 * characters */
160static size_t strLenStripped(const char *pcsz)
161{
162 size_t cch = 0;
163 for (size_t i = 0; pcsz[i] != '\0'; ++i)
164 if (pcsz[i] > 32 && pcsz[i] < 127)
165 cch = i;
166 return cch + 1;
167}
168
169
170/**
171 * Get the name of a floppy drive according to the Linux floppy driver.
172 * @returns true on success, false if the name was not available (i.e. the
173 * device was not readible, or the file name wasn't a PC floppy
174 * device)
175 * @param pcszNode the path to the device node for the device
176 * @param Number the Linux floppy driver number for the drive. Required.
177 * @param pszName where to store the name retreived
178 */
179static bool floppyGetName(const char *pcszNode, unsigned Number,
180 floppy_drive_name pszName)
181{
182 AssertPtrReturn(pcszNode, false);
183 AssertPtrReturn(pszName, false);
184 AssertReturn(Number <= 7, false);
185 RTFILE File;
186 int rc = RTFileOpen(&File, pcszNode, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_NON_BLOCK);
187 if (RT_SUCCESS(rc))
188 {
189 int rcIoCtl;
190 rc = RTFileIoCtl(File, FDGETDRVTYP, pszName, 0, &rcIoCtl);
191 RTFileClose(File);
192 if (RT_SUCCESS(rc) && rcIoCtl >= 0)
193 return true;
194 }
195 return false;
196}
197
198
199/**
200 * Create a UDI and a description for a floppy drive based on a number and the
201 * driver's name for it. We deliberately return an ugly sequence of
202 * characters as the description rather than an English language string to
203 * avoid translation issues.
204 *
205 * @returns true if we know the device to be valid, false otherwise
206 * @param pcszName the floppy driver name for the device (optional)
207 * @param Number the number of the floppy (0 to 3 on FDC 0, 4 to 7 on
208 * FDC 1)
209 * @param pszDesc where to store the device description (optional)
210 * @param cchDesc the size of the buffer in @a pszDesc
211 * @param pszUdi where to store the device UDI (optional)
212 * @param cchUdi the size of the buffer in @a pszUdi
213 */
214static void floppyCreateDeviceStrings(const floppy_drive_name pcszName,
215 unsigned Number, char *pszDesc,
216 size_t cchDesc, char *pszUdi,
217 size_t cchUdi)
218{
219 AssertPtrNullReturnVoid(pcszName);
220 AssertPtrNullReturnVoid(pszDesc);
221 AssertReturnVoid(!pszDesc || cchDesc > 0);
222 AssertPtrNullReturnVoid(pszUdi);
223 AssertReturnVoid(!pszUdi || cchUdi > 0);
224 AssertReturnVoid(Number <= 7);
225 if (pcszName)
226 {
227 const char *pcszSize;
228 switch(pcszName[0])
229 {
230 case 'd': case 'q': case 'h':
231 pcszSize = "5.25\"";
232 break;
233 case 'D': case 'H': case 'E': case 'u':
234 pcszSize = "3.5\"";
235 break;
236 default:
237 pcszSize = "(unknown)";
238 }
239 if (pszDesc)
240 RTStrPrintf(pszDesc, cchDesc, "%s %s K%s", pcszSize, &pcszName[1],
241 Number > 3 ? ", FDC 2" : "");
242 }
243 else
244 {
245 if (pszDesc)
246 RTStrPrintf(pszDesc, cchDesc, "FDD %d%s", (Number & 4) + 1,
247 Number > 3 ? ", FDC 2" : "");
248 }
249 if (pszUdi)
250 RTStrPrintf(pszUdi, cchUdi,
251 "/org/freedesktop/Hal/devices/platform_floppy_%u_storage",
252 Number);
253}
254
255
256/**
257 * Check whether a device number might correspond to a CD-ROM device according
258 * to Documentation/devices.txt in the Linux kernel source.
259 * @returns true if it might, false otherwise
260 * @param Number the device number (major and minor combination)
261 */
262static bool isCdromDevNum(dev_t Number)
263{
264 int major = major(Number);
265 int minor = minor(Number);
266 if ((major == IDE0_MAJOR) && !(minor & 0x3f))
267 return true;
268 if (major == SCSI_CDROM_MAJOR)
269 return true;
270 if (major == CDU31A_CDROM_MAJOR)
271 return true;
272 if (major == GOLDSTAR_CDROM_MAJOR)
273 return true;
274 if (major == OPTICS_CDROM_MAJOR)
275 return true;
276 if (major == SANYO_CDROM_MAJOR)
277 return true;
278 if (major == MITSUMI_X_CDROM_MAJOR)
279 return true;
280 if ((major == IDE1_MAJOR) && !(minor & 0x3f))
281 return true;
282 if (major == MITSUMI_CDROM_MAJOR)
283 return true;
284 if (major == CDU535_CDROM_MAJOR)
285 return true;
286 if (major == MATSUSHITA_CDROM_MAJOR)
287 return true;
288 if (major == MATSUSHITA_CDROM2_MAJOR)
289 return true;
290 if (major == MATSUSHITA_CDROM3_MAJOR)
291 return true;
292 if (major == MATSUSHITA_CDROM4_MAJOR)
293 return true;
294 if (major == AZTECH_CDROM_MAJOR)
295 return true;
296 if (major == 30 /* CM205_CDROM_MAJOR */) /* no #define for some reason */
297 return true;
298 if (major == CM206_CDROM_MAJOR)
299 return true;
300 if ((major == IDE3_MAJOR) && !(minor & 0x3f))
301 return true;
302 if (major == 46 /* Parallel port ATAPI CD-ROM */) /* no #define */
303 return true;
304 if ((major == IDE4_MAJOR) && !(minor & 0x3f))
305 return true;
306 if ((major == IDE5_MAJOR) && !(minor & 0x3f))
307 return true;
308 if ((major == IDE6_MAJOR) && !(minor & 0x3f))
309 return true;
310 if ((major == IDE7_MAJOR) && !(minor & 0x3f))
311 return true;
312 if ((major == IDE8_MAJOR) && !(minor & 0x3f))
313 return true;
314 if ((major == IDE9_MAJOR) && !(minor & 0x3f))
315 return true;
316 if (major == 113 /* VIOCD_MAJOR */)
317 return true;
318 return false;
319}
320
321
322/**
323 * Send an SCSI INQUIRY command to a device and return selected information.
324 * @returns iprt status code
325 * @returns VERR_TRY_AGAIN if the query failed but might succeed next time
326 * @param pcszNode the full path to the device node
327 * @param pu8Type where to store the SCSI device type on success (optional)
328 * @param pchVendor where to store the vendor id string on success (optional)
329 * @param cchVendor the size of the @a pchVendor buffer
330 * @param pchModel where to store the product id string on success (optional)
331 * @param cchModel the size of the @a pchModel buffer
332 * @note check documentation on the SCSI INQUIRY command and the Linux kernel
333 * SCSI headers included above if you want to understand what is going
334 * on in this method.
335 */
336static int cdromDoInquiry(const char *pcszNode, uint8_t *pu8Type,
337 char *pchVendor, size_t cchVendor, char *pchModel,
338 size_t cchModel)
339{
340 LogRelFlowFunc(("pcszNode=%s, pu8Type=%p, pchVendor=%p, cchVendor=%llu, pchModel=%p, cchModel=%llu\n",
341 pcszNode, pu8Type, pchVendor, cchVendor, pchModel,
342 cchModel));
343 AssertPtrReturn(pcszNode, VERR_INVALID_POINTER);
344 AssertPtrNullReturn(pu8Type, VERR_INVALID_POINTER);
345 AssertPtrNullReturn(pchVendor, VERR_INVALID_POINTER);
346 AssertPtrNullReturn(pchModel, VERR_INVALID_POINTER);
347
348 RTFILE hFile;
349 int rc = RTFileOpen(&hFile, pcszNode, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_NON_BLOCK);
350 if (RT_SUCCESS(rc))
351 {
352 int rcIoCtl = 0;
353 unsigned char u8Response[96] = { 0 };
354 struct cdrom_generic_command CdromCommandReq;
355 RT_ZERO(CdromCommandReq);
356 CdromCommandReq.cmd[0] = INQUIRY;
357 CdromCommandReq.cmd[4] = sizeof(u8Response);
358 CdromCommandReq.buffer = u8Response;
359 CdromCommandReq.buflen = sizeof(u8Response);
360 CdromCommandReq.data_direction = CGC_DATA_READ;
361 CdromCommandReq.timeout = 5000; /* ms */
362 rc = RTFileIoCtl(hFile, CDROM_SEND_PACKET, &CdromCommandReq, 0, &rcIoCtl);
363 if (RT_SUCCESS(rc) && rcIoCtl < 0)
364 rc = RTErrConvertFromErrno(-CdromCommandReq.stat);
365 RTFileClose(hFile);
366
367 if (RT_SUCCESS(rc))
368 {
369 if (pu8Type)
370 *pu8Type = u8Response[0] & 0x1f;
371 if (pchVendor)
372 RTStrPrintf(pchVendor, cchVendor, "%.8s",
373 &u8Response[8] /* vendor id string */);
374 if (pchModel)
375 RTStrPrintf(pchModel, cchModel, "%.16s",
376 &u8Response[16] /* product id string */);
377 LogRelFlowFunc(("returning success: type=%u, vendor=%.8s, product=%.16s\n",
378 u8Response[0] & 0x1f, &u8Response[8], &u8Response[16]));
379 return VINF_SUCCESS;
380 }
381 }
382 LogRelFlowFunc(("returning %Rrc\n", rc));
383 return rc;
384}
385
386
387/**
388 * Initialise the device strings (description and UDI) for a DVD drive based on
389 * vendor and model name strings.
390 * @param pcszVendor the vendor ID string
391 * @param pcszModel the product ID string
392 * @param pszDesc where to store the description string (optional)
393 * @param cchDesc the size of the buffer in @pszDesc
394 * @param pszUdi where to store the UDI string (optional)
395 * @param cchUdi the size of the buffer in @pszUdi
396 */
397/* static */
398void dvdCreateDeviceStrings(const char *pcszVendor, const char *pcszModel,
399 char *pszDesc, size_t cchDesc, char *pszUdi,
400 size_t cchUdi)
401{
402 AssertPtrReturnVoid(pcszVendor);
403 AssertPtrReturnVoid(pcszModel);
404 AssertPtrNullReturnVoid(pszDesc);
405 AssertReturnVoid(!pszDesc || cchDesc > 0);
406 AssertPtrNullReturnVoid(pszUdi);
407 AssertReturnVoid(!pszUdi || cchUdi > 0);
408 char szCleaned[128];
409 size_t cchVendor = strLenStripped(pcszVendor);
410 size_t cchModel = strLenStripped(pcszModel);
411
412 /* Create a cleaned version of the model string for the UDI string. */
413 for (unsigned i = 0; pcszModel[i] != '\0' && i < sizeof(szCleaned); ++i)
414 if ( (pcszModel[i] >= '0' && pcszModel[i] <= '9')
415 || (pcszModel[i] >= 'A' && pcszModel[i] <= 'z'))
416 szCleaned[i] = pcszModel[i];
417 else
418 szCleaned[i] = '_';
419 szCleaned[RT_MIN(cchModel, sizeof(szCleaned) - 1)] = '\0';
420
421 /* Construct the description string as "Vendor Product" */
422 if (pszDesc)
423 {
424 if (cchVendor > 0)
425 RTStrPrintf(pszDesc, cchDesc, "%.*s %s", cchVendor, pcszVendor,
426 cchModel > 0 ? pcszModel : "(unknown drive model)");
427 else
428 RTStrPrintf(pszDesc, cchDesc, "%s", pcszModel);
429 }
430 /* Construct the UDI string */
431 if (pszUdi)
432 {
433 if (cchModel > 0)
434 RTStrPrintf(pszUdi, cchUdi,
435 "/org/freedesktop/Hal/devices/storage_model_%s",
436 szCleaned);
437 else
438 pszUdi[0] = '\0';
439 }
440}
441
442
443/**
444 * Check whether a device node points to a valid device and create a UDI and
445 * a description for it, and store the device number, if it does.
446 * @returns true if the device is valid, false otherwise
447 * @param pcszNode the path to the device node
448 * @param isDVD are we looking for a DVD device (or a floppy device)?
449 * @param pDevice where to store the device node (optional)
450 * @param pszDesc where to store the device description (optional)
451 * @param cchDesc the size of the buffer in @a pszDesc
452 * @param pszUdi where to store the device UDI (optional)
453 * @param cchUdi the size of the buffer in @a pszUdi
454 */
455static bool devValidateDevice(const char *pcszNode, bool isDVD, dev_t *pDevice,
456 char *pszDesc, size_t cchDesc, char *pszUdi,
457 size_t cchUdi)
458{
459 AssertPtrReturn(pcszNode, false);
460 AssertPtrNullReturn(pDevice, false);
461 AssertPtrNullReturn(pszDesc, false);
462 AssertReturn(!pszDesc || cchDesc > 0, false);
463 AssertPtrNullReturn(pszUdi, false);
464 AssertReturn(!pszUdi || cchUdi > 0, false);
465 RTFSOBJINFO ObjInfo;
466 if (RT_FAILURE(RTPathQueryInfo(pcszNode, &ObjInfo, RTFSOBJATTRADD_UNIX)))
467 return false;
468 if (!RTFS_IS_DEV_BLOCK(ObjInfo.Attr.fMode))
469 return false;
470 if (pDevice)
471 *pDevice = ObjInfo.Attr.u.Unix.Device;
472 if (isDVD)
473 {
474 char szVendor[128], szModel[128];
475 uint8_t u8Type;
476 if (!isCdromDevNum(ObjInfo.Attr.u.Unix.Device))
477 return false;
478 if (RT_FAILURE(cdromDoInquiry(pcszNode, &u8Type,
479 szVendor, sizeof(szVendor),
480 szModel, sizeof(szModel))))
481 return false;
482 if (u8Type != TYPE_ROM)
483 return false;
484 dvdCreateDeviceStrings(szVendor, szModel, pszDesc, cchDesc,
485 pszUdi, cchUdi);
486 }
487 else
488 {
489 /* Floppies on Linux are legacy devices with hardcoded majors and
490 * minors */
491 unsigned Number;
492 floppy_drive_name szName;
493 if (major(ObjInfo.Attr.u.Unix.Device) != FLOPPY_MAJOR)
494 return false;
495 switch (minor(ObjInfo.Attr.u.Unix.Device))
496 {
497 case 0: case 1: case 2: case 3:
498 Number = minor(ObjInfo.Attr.u.Unix.Device);
499 break;
500 case 128: case 129: case 130: case 131:
501 Number = minor(ObjInfo.Attr.u.Unix.Device) - 128 + 4;
502 break;
503 default:
504 return false;
505 }
506 if (!floppyGetName(pcszNode, Number, szName))
507 return false;
508 floppyCreateDeviceStrings(szName, Number, pszDesc, cchDesc, pszUdi,
509 cchUdi);
510 }
511 return true;
512}
513
514
515int VBoxMainDriveInfo::updateDVDs ()
516{
517 LogFlowThisFunc(("entered\n"));
518 int rc = VINF_SUCCESS;
519 bool success = false; /* Have we succeeded in finding anything yet? */
520 try
521 {
522 mDVDList.clear ();
523 /* Always allow the user to override our auto-detection using an
524 * environment variable. */
525 if (RT_SUCCESS(rc) && (!success || testing()))
526 rc = getDriveInfoFromEnv ("VBOX_CDROM", &mDVDList, true /* isDVD */,
527 &success);
528 setNoProbe(false);
529 if (RT_SUCCESS(rc) && (!success | testing()))
530 rc = getDriveInfoFromSysfs(&mDVDList, true /* isDVD */, &success);
531 if (RT_SUCCESS(rc) && testing())
532 {
533 setNoProbe(true);
534 rc = getDriveInfoFromSysfs(&mDVDList, true /* isDVD */, &success);
535 }
536 /* Walk through the /dev subtree if nothing else has helped. */
537 if (RT_SUCCESS(rc) && (!success | testing()))
538 rc = getDriveInfoFromDev(&mDVDList, true /* isDVD */, &success);
539 }
540 catch(std::bad_alloc &e)
541 {
542 rc = VERR_NO_MEMORY;
543 }
544 LogFlowThisFunc(("rc=%Rrc\n", rc));
545 return rc;
546}
547
548int VBoxMainDriveInfo::updateFloppies ()
549{
550 LogFlowThisFunc(("entered\n"));
551 int rc = VINF_SUCCESS;
552 bool success = false; /* Have we succeeded in finding anything yet? */
553 try
554 {
555 mFloppyList.clear ();
556 if (RT_SUCCESS(rc) && (!success || testing()))
557 rc = getDriveInfoFromEnv("VBOX_FLOPPY", &mFloppyList,
558 false /* isDVD */, &success);
559 setNoProbe(false);
560 if ( RT_SUCCESS(rc) && (!success || testing()))
561 rc = getDriveInfoFromSysfs(&mFloppyList, false /* isDVD */,
562 &success);
563 if (RT_SUCCESS(rc) && testing())
564 {
565 setNoProbe(true);
566 rc = getDriveInfoFromSysfs(&mFloppyList, false /* isDVD */, &success);
567 }
568 /* Walk through the /dev subtree if nothing else has helped. */
569 if ( RT_SUCCESS(rc) && (!success || testing()))
570 rc = getDriveInfoFromDev(&mFloppyList, false /* isDVD */,
571 &success);
572 }
573 catch(std::bad_alloc &e)
574 {
575 rc = VERR_NO_MEMORY;
576 }
577 LogFlowThisFunc(("rc=%Rrc\n", rc));
578 return rc;
579}
580
581
582/**
583 * Extract the names of drives from an environment variable and add them to a
584 * list if they are valid.
585 * @returns iprt status code
586 * @param pcszVar the name of the environment variable. The variable
587 * value should be a list of device node names, separated
588 * by ':' characters.
589 * @param pList the list to append the drives found to
590 * @param isDVD are we looking for DVD drives or for floppies?
591 * @param pfSuccess this will be set to true if we found at least one drive
592 * and to false otherwise. Optional.
593 */
594/* static */
595int getDriveInfoFromEnv(const char *pcszVar, DriveInfoList *pList,
596 bool isDVD, bool *pfSuccess)
597{
598 AssertPtrReturn(pcszVar, VERR_INVALID_POINTER);
599 AssertPtrReturn(pList, VERR_INVALID_POINTER);
600 AssertPtrNullReturn(pfSuccess, VERR_INVALID_POINTER);
601 LogFlowFunc(("pcszVar=%s, pList=%p, isDVD=%d, pfSuccess=%p\n", pcszVar,
602 pList, isDVD, pfSuccess));
603 int rc = VINF_SUCCESS;
604 bool success = false;
605 char *pszFreeMe = RTEnvDupEx(RTENV_DEFAULT, pcszVar);
606
607 try
608 {
609 const char *pcszCurrent = pszFreeMe;
610 while (pcszCurrent && *pcszCurrent != '\0')
611 {
612 const char *pcszNext = strchr(pcszCurrent, ':');
613 char szPath[RTPATH_MAX], szReal[RTPATH_MAX];
614 char szDesc[256], szUdi[256];
615 if (pcszNext)
616 RTStrPrintf(szPath, sizeof(szPath), "%.*s",
617 pcszNext - pcszCurrent - 1, pcszCurrent);
618 else
619 RTStrPrintf(szPath, sizeof(szPath), "%s", pcszCurrent);
620 if ( RT_SUCCESS(RTPathReal(szPath, szReal, sizeof(szReal)))
621 && devValidateDevice(szReal, isDVD, NULL, szDesc,
622 sizeof(szDesc), szUdi, sizeof(szUdi)))
623 {
624 pList->push_back(DriveInfo(szReal, szUdi, szDesc));
625 success = true;
626 }
627 pcszCurrent = pcszNext ? pcszNext + 1 : NULL;
628 }
629 if (pfSuccess != NULL)
630 *pfSuccess = success;
631 }
632 catch(std::bad_alloc &e)
633 {
634 rc = VERR_NO_MEMORY;
635 }
636 RTStrFree(pszFreeMe);
637 LogFlowFunc(("rc=%Rrc, success=%d\n", rc, success));
638 return rc;
639}
640
641
642class sysfsBlockDev
643{
644public:
645 sysfsBlockDev(const char *pcszName, bool wantDVD)
646 : mpcszName(pcszName), mwantDVD(wantDVD), misConsistent(true),
647 misValid(false)
648 {
649 if (findDeviceNode())
650 {
651 if (mwantDVD)
652 validateAndInitForDVD();
653 else
654 validateAndInitForFloppy();
655 }
656 }
657private:
658 /** The name of the subdirectory of /sys/block for this device */
659 const char *mpcszName;
660 /** Are we looking for a floppy or a DVD device? */
661 bool mwantDVD;
662 /** The device node for the device */
663 char mszNode[RTPATH_MAX];
664 /** Does the sysfs entry look like we expect it too? This is a canary
665 * for future sysfs ABI changes. */
666 bool misConsistent;
667 /** Is this entry a valid specimen of what we are looking for? */
668 bool misValid;
669 /** Human readible drive description string */
670 char mszDesc[256];
671 /** Unique identifier for the drive. Should be identical to hal's UDI for
672 * the device. May not be unique for two identical drives. */
673 char mszUdi[256];
674private:
675 /* Private methods */
676
677 /**
678 * Fill in the device node member based on the /sys/block subdirectory.
679 * @returns boolean success value
680 */
681 bool findDeviceNode()
682 {
683 dev_t dev = RTLinuxSysFsReadDevNumFile("block/%s/dev", mpcszName);
684 if (dev == 0)
685 {
686 misConsistent = false;
687 return false;
688 }
689 if (RTLinuxFindDevicePath(dev, RTFS_TYPE_DEV_BLOCK, mszNode,
690 sizeof(mszNode), "%s", mpcszName) < 0)
691 return false;
692 return true;
693 }
694
695 /** Check whether the sysfs block entry is valid for a DVD device and
696 * initialise the string data members for the object. We try to get all
697 * the information we need from sysfs if possible, to avoid unnecessarily
698 * poking the device, and if that fails we fall back to an SCSI INQUIRY
699 * command. */
700 void validateAndInitForDVD()
701 {
702 char szVendor[128], szModel[128];
703 ssize_t cchVendor, cchModel;
704 int64_t type = RTLinuxSysFsReadIntFile(10, "block/%s/device/type",
705 mpcszName);
706 if (type >= 0 && type != TYPE_ROM)
707 return;
708 if (type == TYPE_ROM)
709 {
710 cchVendor = RTLinuxSysFsReadStrFile(szVendor, sizeof(szVendor),
711 "block/%s/device/vendor",
712 mpcszName);
713 if (cchVendor >= 0)
714 {
715 cchModel = RTLinuxSysFsReadStrFile(szModel, sizeof(szModel),
716 "block/%s/device/model",
717 mpcszName);
718 if (cchModel >= 0)
719 {
720 misValid = true;
721 dvdCreateDeviceStrings(szVendor, szModel,
722 mszDesc, sizeof(mszDesc),
723 mszUdi, sizeof(mszUdi));
724 return;
725 }
726 }
727 }
728 if (!noProbe())
729 probeAndInitForDVD();
730 }
731
732 /** Try to find out whether a device is a DVD drive by sending it an
733 * SCSI INQUIRY command. If it is, initialise the string and validity
734 * data members for the object based on the returned data.
735 */
736 void probeAndInitForDVD()
737 {
738 AssertReturnVoid(mszNode[0] != '\0');
739 uint8_t u8Type = 0;
740 char szVendor[128] = "";
741 char szModel[128] = "";
742 int rc = cdromDoInquiry(mszNode, &u8Type, szVendor,
743 sizeof(szVendor), szModel,
744 sizeof(szModel));
745 if (RT_SUCCESS(rc) && (u8Type == TYPE_ROM))
746 {
747 misValid = true;
748 dvdCreateDeviceStrings(szVendor, szModel, mszDesc, sizeof(mszDesc),
749 mszUdi, sizeof(mszUdi));
750 }
751 }
752
753 /** Check whether the sysfs block entry is valid for a floppy device and
754 * initialise the string data members for the object. Since we only
755 * support floppies using the basic "floppy" driver, we check the driver
756 * using the entry name and a driver-specific ioctl. */
757 void validateAndInitForFloppy()
758 {
759 bool haveName = false;
760 floppy_drive_name szName;
761 char szDriver[8];
762 if ( mpcszName[0] != 'f'
763 || mpcszName[1] != 'd'
764 || mpcszName[2] < '0'
765 || mpcszName[2] > '7'
766 || mpcszName[3] != '\0')
767 return;
768 if (!noProbe())
769 haveName = floppyGetName(mszNode, mpcszName[2] - '0', szName);
770 if (RTLinuxSysFsGetLinkDest(szDriver, sizeof(szDriver), "block/%s/%s",
771 mpcszName, "device/driver") >= 0)
772 {
773 if (RTStrCmp(szDriver, "floppy"))
774 return;
775 }
776 else if (!haveName)
777 return;
778 floppyCreateDeviceStrings(haveName ? szName : NULL,
779 mpcszName[2] - '0', mszDesc,
780 sizeof(mszDesc), mszUdi, sizeof(mszUdi));
781 misValid = true;
782 }
783
784public:
785 bool isConsistent()
786 {
787 return misConsistent;
788 }
789 bool isValid()
790 {
791 return misValid;
792 }
793 const char *getDesc()
794 {
795 return mszDesc;
796 }
797 const char *getUdi()
798 {
799 return mszUdi;
800 }
801 const char *getNode()
802 {
803 return mszNode;
804 }
805};
806
807/**
808 * Helper function to query the sysfs subsystem for information about DVD
809 * drives attached to the system.
810 * @returns iprt status code
811 * @param pList where to add information about the drives detected
812 * @param isDVD are we looking for DVDs or floppies?
813 * @param pfSuccess Did we find anything?
814 *
815 * @returns IPRT status code
816 */
817/* static */
818int getDriveInfoFromSysfs(DriveInfoList *pList, bool isDVD, bool *pfSuccess)
819{
820 AssertPtrReturn(pList, VERR_INVALID_POINTER);
821 AssertPtrNullReturn(pfSuccess, VERR_INVALID_POINTER); /* Valid or Null */
822 LogFlowFunc (("pList=%p, isDVD=%u, pfSuccess=%p\n",
823 pList, (unsigned) isDVD, pfSuccess));
824 PRTDIR pDir = NULL;
825 int rc;
826 bool fSuccess = false;
827 unsigned cFound = 0;
828
829 if (!RTPathExists("/sys"))
830 return VINF_SUCCESS;
831 rc = RTDirOpen(&pDir, "/sys/block");
832 /* This might mean that sysfs semantics have changed */
833 AssertReturn(rc != VERR_FILE_NOT_FOUND, VINF_SUCCESS);
834 fSuccess = true;
835 if (RT_SUCCESS(rc))
836 for (;;)
837 {
838 RTDIRENTRY entry;
839 rc = RTDirRead(pDir, &entry, NULL);
840 Assert(rc != VERR_BUFFER_OVERFLOW); /* Should never happen... */
841 if (RT_FAILURE(rc)) /* Including overflow and no more files */
842 break;
843 if (entry.szName[0] == '.')
844 continue;
845 sysfsBlockDev dev(entry.szName, isDVD);
846 /* This might mean that sysfs semantics have changed */
847 AssertBreakStmt(dev.isConsistent(), fSuccess = false);
848 if (!dev.isValid())
849 continue;
850 try
851 {
852 pList->push_back(DriveInfo(dev.getNode(), dev.getUdi(),
853 dev.getDesc()));
854 }
855 catch(std::bad_alloc &e)
856 {
857 rc = VERR_NO_MEMORY;
858 break;
859 }
860 ++cFound;
861 }
862 RTDirClose(pDir);
863 if (rc == VERR_NO_MORE_FILES)
864 rc = VINF_SUCCESS;
865 if (RT_FAILURE(rc))
866 /* Clean up again */
867 for (unsigned i = 0; i < cFound; ++i)
868 pList->pop_back();
869 if (pfSuccess)
870 *pfSuccess = fSuccess;
871 LogFlow (("rc=%Rrc, fSuccess=%u\n", rc, (unsigned) fSuccess));
872 return rc;
873}
874
875
876/** Structure for holding information about a drive we have found */
877struct deviceNodeInfo
878{
879 /** The device number */
880 dev_t Device;
881 /** The device node path */
882 char szPath[RTPATH_MAX];
883 /** The device description */
884 char szDesc[256];
885 /** The device UDI */
886 char szUdi[256];
887};
888
889/** The maximum number of devices we will search for. */
890enum { MAX_DEVICE_NODES = 8 };
891/** An array of MAX_DEVICE_NODES devices */
892typedef struct deviceNodeInfo deviceNodeArray[MAX_DEVICE_NODES];
893
894/**
895 * Recursive worker function to walk the /dev tree looking for DVD or floppy
896 * devices.
897 * @returns true if we have already found MAX_DEVICE_NODES devices, false
898 * otherwise
899 * @param pszPath the path to start recursing. The function can modify
900 * this string at and after the terminating zero
901 * @param cchPath the size of the buffer (not the string!) in @a pszPath
902 * @param aDevices where to fill in information about devices that we have
903 * found
904 * @param wantDVD are we looking for DVD devices (or floppies)?
905 */
906static bool devFindDeviceRecursive(char *pszPath, size_t cchPath,
907 deviceNodeArray aDevices, bool wantDVD)
908{
909 /*
910 * Check assumptions made by the code below.
911 */
912 size_t const cchBasePath = strlen(pszPath);
913 AssertReturn(cchBasePath < RTPATH_MAX - 10U, false);
914 AssertReturn(pszPath[cchBasePath - 1] != '/', false);
915
916 PRTDIR pDir;
917 if (RT_FAILURE(RTDirOpen(&pDir, pszPath)))
918 return false;
919 for (;;)
920 {
921 RTDIRENTRY Entry;
922 RTFSOBJINFO ObjInfo;
923 int rc = RTDirRead(pDir, &Entry, NULL);
924 if (RT_FAILURE(rc))
925 break;
926 if (Entry.enmType == RTDIRENTRYTYPE_UNKNOWN)
927 {
928 if (RT_FAILURE(RTPathQueryInfo(pszPath, &ObjInfo,
929 RTFSOBJATTRADD_UNIX)))
930 continue;
931 if (RTFS_IS_SYMLINK(ObjInfo.Attr.fMode))
932 continue;
933 }
934
935 if (Entry.enmType == RTDIRENTRYTYPE_SYMLINK)
936 continue;
937 pszPath[cchBasePath] = '\0';
938 if (RT_FAILURE(RTPathAppend(pszPath, cchPath, Entry.szName)))
939 break;
940
941 /* Do the matching. */
942 dev_t DevNode;
943 char szDesc[256], szUdi[256];
944 if (!devValidateDevice(pszPath, wantDVD, &DevNode, szDesc,
945 sizeof(szDesc), szUdi, sizeof(szUdi)))
946 continue;
947 unsigned i;
948 for (i = 0; i < MAX_DEVICE_NODES; ++i)
949 if (!aDevices[i].Device || (aDevices[i].Device == DevNode))
950 break;
951 AssertBreak(i < MAX_DEVICE_NODES);
952 if (aDevices[i].Device)
953 continue;
954 aDevices[i].Device = DevNode;
955 RTStrPrintf(aDevices[i].szPath, sizeof(aDevices[i].szPath),
956 "%s", pszPath);
957 AssertCompile(sizeof(aDevices[i].szDesc) == sizeof(szDesc));
958 strcpy(aDevices[i].szDesc, szDesc);
959 AssertCompile(sizeof(aDevices[i].szUdi) == sizeof(szUdi));
960 strcpy(aDevices[i].szUdi, szUdi);
961 if (i == MAX_DEVICE_NODES - 1)
962 break;
963 continue;
964
965 /* Recurse into subdirectories. */
966 if ( (Entry.enmType == RTDIRENTRYTYPE_UNKNOWN)
967 && !RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode))
968 continue;
969 if (Entry.enmType != RTDIRENTRYTYPE_DIRECTORY)
970 continue;
971 if (Entry.szName[0] == '.')
972 continue;
973
974 if (devFindDeviceRecursive(pszPath, cchPath, aDevices, wantDVD))
975 break;
976 }
977 RTDirClose(pDir);
978 return aDevices[MAX_DEVICE_NODES - 1].Device ? true : false;
979}
980
981
982/**
983 * Recursively walk through the /dev tree and add any DVD or floppy drives we
984 * find and can access to our list. (If we can't access them we can't check
985 * whether or not they are really DVD or floppy drives).
986 * @note this is rather slow (a couple of seconds) for DVD probing on
987 * systems with a static /dev tree, as the current code tries to open
988 * any device node with a major/minor combination that could belong to
989 * a CD-ROM device, and opening a non-existent device can take a non.
990 * negligeable time on Linux. If it is ever necessary to improve this
991 * (static /dev trees are no longer very fashionable these days, and
992 * sysfs looks like it will be with us for a while), we could further
993 * reduce the number of device nodes we open by checking whether the
994 * driver is actually loaded in /proc/devices, and by counting the
995 * of currently attached SCSI CD-ROM devices in /proc/scsi/scsi (yes,
996 * there is a race, but it is probably not important for us).
997 * @returns iprt status code
998 * @param pList the list to append the drives found to
999 * @param isDVD are we looking for DVD drives or for floppies?
1000 * @param pfSuccess this will be set to true if we found at least one drive
1001 * and to false otherwise. Optional.
1002 */
1003/* static */
1004int getDriveInfoFromDev(DriveInfoList *pList, bool isDVD, bool *pfSuccess)
1005{
1006 AssertPtrReturn(pList, VERR_INVALID_POINTER);
1007 AssertPtrNullReturn(pfSuccess, VERR_INVALID_POINTER);
1008 LogFlowFunc(("pList=%p, isDVD=%d, pfSuccess=%p\n", pList, isDVD,
1009 pfSuccess));
1010 int rc = VINF_SUCCESS;
1011 bool success = false;
1012
1013 char szPath[RTPATH_MAX] = "/dev";
1014 deviceNodeArray aDevices;
1015 RT_ZERO(aDevices);
1016 devFindDeviceRecursive(szPath, sizeof(szPath), aDevices, isDVD);
1017 try
1018 {
1019 for (unsigned i = 0; i < MAX_DEVICE_NODES; ++i)
1020 {
1021 if (aDevices[i].Device)
1022 {
1023 pList->push_back(DriveInfo(aDevices[i].szPath,
1024 aDevices[i].szUdi, aDevices[i].szDesc));
1025 success = true;
1026 }
1027 }
1028 if (pfSuccess != NULL)
1029 *pfSuccess = success;
1030 }
1031 catch(std::bad_alloc &e)
1032 {
1033 rc = VERR_NO_MEMORY;
1034 }
1035 LogFlowFunc (("rc=%Rrc, success=%d\n", rc, success));
1036 return rc;
1037}
1038
1039
1040int VBoxMainUSBDeviceInfo::UpdateDevices ()
1041{
1042 LogFlowThisFunc(("entered\n"));
1043 int rc = VINF_SUCCESS;
1044 bool success = false; /* Have we succeeded in finding anything yet? */
1045 try
1046 {
1047 mDeviceList.clear();
1048#ifdef VBOX_USB_WITH_SYSFS
1049# ifdef VBOX_USB_WITH_DBUS
1050 bool halSuccess = false;
1051 if ( RT_SUCCESS(rc)
1052 && RT_SUCCESS(RTDBusLoadLib())
1053 && (!success || testing()))
1054 rc = getUSBDeviceInfoFromHal(&mDeviceList, &halSuccess);
1055 /* Try the old API if the new one *succeeded* as only one of them will
1056 * pick up devices anyway. */
1057 if (RT_SUCCESS(rc) && halSuccess && (!success || testing()))
1058 rc = getOldUSBDeviceInfoFromHal(&mDeviceList, &halSuccess);
1059 if (!success)
1060 success = halSuccess;
1061# endif /* VBOX_USB_WITH_DBUS */
1062# ifdef VBOX_USB_WITH_INOTIFY
1063 if ( RT_SUCCESS(rc)
1064 && (!success || testing()))
1065 rc = getUSBDeviceInfoFromSysfs(&mDeviceList, &success);
1066# endif
1067#else /* !VBOX_USB_WITH_SYSFS */
1068 NOREF(success);
1069#endif /* !VBOX_USB_WITH_SYSFS */
1070 }
1071 catch(std::bad_alloc &e)
1072 {
1073 rc = VERR_NO_MEMORY;
1074 }
1075 LogFlowThisFunc(("rc=%Rrc\n", rc));
1076 return rc;
1077}
1078
1079#if defined VBOX_USB_WITH_SYSFS && defined VBOX_USB_WITH_DBUS
1080class hotplugDBusImpl : public VBoxMainHotplugWaiterImpl
1081{
1082 /** The connection to DBus */
1083 RTMemAutoPtr <DBusConnection, VBoxHalShutdownPrivate> mConnection;
1084 /** Semaphore which is set when a device is hotplugged and reset when
1085 * it is read. */
1086 volatile bool mTriggered;
1087 /** A flag to say that we wish to interrupt the current wait. */
1088 volatile bool mInterrupt;
1089 /** The constructor "return code" */
1090 int mStatus;
1091
1092public:
1093 /** Test whether this implementation can be used on the current system */
1094 static bool Available(void)
1095 {
1096 RTMemAutoPtr<DBusConnection, VBoxHalShutdown> dbusConnection;
1097
1098 /* Try to open a test connection to hal */
1099 if (RT_SUCCESS(RTDBusLoadLib()) && RT_SUCCESS(halInit (&dbusConnection)))
1100 return !!dbusConnection;
1101 return false;
1102 }
1103
1104 /** Constructor */
1105 hotplugDBusImpl (void);
1106 virtual ~hotplugDBusImpl (void);
1107 /** @copydoc VBoxMainHotplugWaiter::Wait */
1108 virtual int Wait (RTMSINTERVAL cMillies);
1109 /** @copydoc VBoxMainHotplugWaiter::Interrupt */
1110 virtual void Interrupt (void);
1111 /** @copydoc VBoxMainHotplugWaiter::getStatus */
1112 virtual int getStatus(void)
1113 {
1114 return mStatus;
1115 }
1116};
1117
1118/* This constructor sets up a private connection to the DBus daemon, connects
1119 * to the hal service and installs a filter which sets the mTriggered flag in
1120 * the Context structure when a device (not necessarily USB) is added or
1121 * removed. */
1122hotplugDBusImpl::hotplugDBusImpl (void) : mTriggered(false), mInterrupt(false)
1123{
1124 int rc;
1125
1126 if (RT_SUCCESS(rc = RTDBusLoadLib()))
1127 {
1128 for (unsigned i = 0; RT_SUCCESS(rc) && i < 5 && !mConnection; ++i)
1129 {
1130 rc = halInitPrivate (&mConnection);
1131 }
1132 if (!mConnection)
1133 rc = VERR_NOT_SUPPORTED;
1134 DBusMessage *pMessage;
1135 while ( RT_SUCCESS(rc)
1136 && (pMessage = dbus_connection_pop_message (mConnection.get())) != NULL)
1137 dbus_message_unref (pMessage); /* empty the message queue. */
1138 if ( RT_SUCCESS(rc)
1139 && !dbus_connection_add_filter (mConnection.get(),
1140 dbusFilterFunction,
1141 (void *) &mTriggered, NULL))
1142 rc = VERR_NO_MEMORY;
1143 if (RT_FAILURE(rc))
1144 mConnection.reset();
1145 }
1146 mStatus = rc;
1147}
1148
1149/* Destructor */
1150hotplugDBusImpl::~hotplugDBusImpl ()
1151{
1152 if (!!mConnection)
1153 dbus_connection_remove_filter (mConnection.get(), dbusFilterFunction,
1154 (void *) &mTriggered);
1155}
1156
1157/* Currently this is implemented using a timed out wait on our private DBus
1158 * connection. Because the connection is private we don't have to worry about
1159 * blocking other users. */
1160int hotplugDBusImpl::Wait(RTMSINTERVAL cMillies)
1161{
1162 int rc = VINF_SUCCESS;
1163 if (!mConnection)
1164 rc = VERR_NOT_SUPPORTED;
1165 bool connected = true;
1166 mTriggered = false;
1167 mInterrupt = false;
1168 unsigned cRealMillies;
1169 if (cMillies != RT_INDEFINITE_WAIT)
1170 cRealMillies = cMillies;
1171 else
1172 cRealMillies = DBUS_POLL_TIMEOUT;
1173 while ( RT_SUCCESS(rc) && connected && !mTriggered
1174 && !mInterrupt)
1175 {
1176 connected = dbus_connection_read_write_dispatch (mConnection.get(),
1177 cRealMillies);
1178 if (mInterrupt)
1179 LogFlowFunc(("wait loop interrupted\n"));
1180 if (cMillies != RT_INDEFINITE_WAIT)
1181 mInterrupt = true;
1182 }
1183 if (!connected)
1184 rc = VERR_TRY_AGAIN;
1185 return rc;
1186}
1187
1188/* Set a flag to tell the Wait not to resume next time it times out. */
1189void hotplugDBusImpl::Interrupt()
1190{
1191 LogFlowFunc(("\n"));
1192 mInterrupt = true;
1193}
1194#endif /* VBOX_USB_WITH_SYSFS && VBOX_USB_WITH_DBUS */
1195
1196class hotplugNullImpl : public VBoxMainHotplugWaiterImpl
1197{
1198public:
1199 hotplugNullImpl (void) {}
1200 virtual ~hotplugNullImpl (void) {}
1201 /** @copydoc VBoxMainHotplugWaiter::Wait */
1202 virtual int Wait (RTMSINTERVAL)
1203 {
1204 return VERR_NOT_SUPPORTED;
1205 }
1206 /** @copydoc VBoxMainHotplugWaiter::Interrupt */
1207 virtual void Interrupt (void) {}
1208 virtual int getStatus(void)
1209 {
1210 return VERR_NOT_SUPPORTED;
1211 }
1212
1213};
1214
1215#ifdef VBOX_USB_WITH_SYSFS
1216# ifdef VBOX_USB_WITH_INOTIFY
1217/** Class wrapper around an inotify watch (or a group of them to be precise).
1218 * Inherits from pathHandler so that it can be passed to walkDirectory() to
1219 * easily add all files from a directory. */
1220class inotifyWatch : public pathHandler
1221{
1222 /** Pointer to the inotify_add_watch() glibc function/Linux API */
1223 int (*inotify_add_watch)(int, const char *, uint32_t);
1224 /** The native handle of the inotify fd. */
1225 int mhInotify;
1226 /** Object initialisation status, to save us throwing an exception from
1227 * the constructor if we can't initialise */
1228 int mStatus;
1229
1230 /** Object initialistation */
1231 int initInotify(void);
1232
1233public:
1234 /** Add @a pcszPath to the list of files and directories to be monitored */
1235 virtual bool handle(const char *pcszPath);
1236
1237 inotifyWatch(void) : mhInotify(-1)
1238 {
1239 mStatus = initInotify();
1240 }
1241
1242 ~inotifyWatch(void)
1243 {
1244 if (mhInotify != -1)
1245 {
1246 close(mhInotify);
1247 mhInotify = -1;
1248 }
1249 }
1250
1251 int getStatus(void)
1252 {
1253 return mStatus;
1254 }
1255
1256 int getFD(void)
1257 {
1258 AssertRCReturn(mStatus, -1);
1259 return mhInotify;
1260 }
1261};
1262
1263int inotifyWatch::initInotify(void)
1264{
1265 int (*inotify_init)(void);
1266 int fd, flags;
1267
1268 errno = 0;
1269 *(void **)(&inotify_init) = dlsym(RTLD_DEFAULT, "inotify_init");
1270 if (!inotify_init)
1271 return VERR_LDR_IMPORTED_SYMBOL_NOT_FOUND;
1272 *(void **)(&inotify_add_watch) = dlsym(RTLD_DEFAULT, "inotify_add_watch");
1273 if (!inotify_add_watch)
1274 return VERR_LDR_IMPORTED_SYMBOL_NOT_FOUND;
1275 fd = inotify_init();
1276 if (fd < 0)
1277 {
1278 Assert(errno > 0);
1279 return RTErrConvertFromErrno(errno);
1280 }
1281 Assert(errno == 0);
1282
1283 int rc = VINF_SUCCESS;
1284
1285 flags = fcntl(fd, F_GETFL, NULL);
1286 if ( flags < 0
1287 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
1288 {
1289 Assert(errno > 0);
1290 rc = RTErrConvertFromErrno(errno);
1291 }
1292 if (RT_FAILURE(rc))
1293 close(fd);
1294 else
1295 {
1296 Assert(errno == 0);
1297 mhInotify = fd;
1298 }
1299 return rc;
1300}
1301
1302/** The flags we pass to inotify - modify, create, delete, change permissions
1303 */
1304#define IN_FLAGS 0x306
1305
1306bool inotifyWatch::handle(const char *pcszPath)
1307{
1308 AssertRCReturn(mStatus, false);
1309 errno = 0;
1310 if ( inotify_add_watch(mhInotify, pcszPath, IN_FLAGS) >= 0
1311 || (errno == EACCES))
1312 return true;
1313 /* Other errors listed in the manpage can be treated as fatal */
1314 return false;
1315}
1316
1317# define SYSFS_USB_DEVICE_PATH "/dev/bus/usb"
1318# define SYSFS_WAKEUP_STRING "Wake up!"
1319
1320class hotplugInotifyImpl : public VBoxMainHotplugWaiterImpl
1321{
1322 /** Pipe used to interrupt wait(), the read end. */
1323 int mhWakeupPipeR;
1324 /** Pipe used to interrupt wait(), the write end. */
1325 int mhWakeupPipeW;
1326 /** The inotify watch set */
1327 inotifyWatch mWatches;
1328 /** Flag to mark that the Wait() method is currently being called, and to
1329 * ensure that it isn't called multiple times in parallel. */
1330 volatile uint32_t mfWaiting;
1331 /** iprt result code from object initialisation. Should be AssertReturn-ed
1332 * on at the start of all methods. I went this way because I didn't want
1333 * to deal with exceptions. */
1334 int mStatus;
1335 /** ID values associates with the wakeup pipe and the FAM socket for polling
1336 */
1337 enum
1338 {
1339 RPIPE_ID = 0,
1340 INOTIFY_ID,
1341 MAX_POLLID
1342 };
1343
1344 /** Clean up any resources in use, gracefully skipping over any which have
1345 * not yet been allocated or already cleaned up. Intended to be called
1346 * from the destructor or after a failed initialisation. */
1347 void term(void);
1348
1349 int drainInotify();
1350
1351 /** Read the wakeup string from the wakeup pipe */
1352 int drainWakeupPipe(void);
1353public:
1354 hotplugInotifyImpl(void);
1355 virtual ~hotplugInotifyImpl(void)
1356 {
1357 term();
1358#ifdef DEBUG
1359 /** The first call to term should mark all resources as freed, so this
1360 * should be a semantic no-op. */
1361 term();
1362#endif
1363 }
1364 /** Are sysfs and inotify available on this system? If so we expect that
1365 * this implementation will be usable. */
1366 static bool Available(void)
1367 {
1368 return ( RTDirExists(SYSFS_USB_DEVICE_PATH)
1369 && dlsym(RTLD_DEFAULT, "inotify_init") != NULL);
1370 }
1371
1372 virtual int getStatus(void)
1373 {
1374 return mStatus;
1375 }
1376
1377 /** @copydoc VBoxMainHotplugWaiter::Wait */
1378 virtual int Wait(RTMSINTERVAL);
1379 /** @copydoc VBoxMainHotplugWaiter::Interrupt */
1380 virtual void Interrupt(void);
1381};
1382
1383/** Simplified version of RTPipeCreate */
1384static int pipeCreateSimple(int *phPipeRead, int *phPipeWrite)
1385{
1386 AssertPtrReturn(phPipeRead, VERR_INVALID_POINTER);
1387 AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
1388
1389 /*
1390 * Create the pipe and set the close-on-exec flag if requested.
1391 */
1392 int aFds[2] = {-1, -1};
1393 if (pipe(aFds))
1394 return RTErrConvertFromErrno(errno);
1395
1396 *phPipeRead = aFds[0];
1397 *phPipeWrite = aFds[1];
1398
1399 /*
1400 * Before we leave, make sure to shut up SIGPIPE.
1401 */
1402 signal(SIGPIPE, SIG_IGN);
1403 return VINF_SUCCESS;
1404}
1405
1406hotplugInotifyImpl::hotplugInotifyImpl(void) :
1407 mhWakeupPipeR(-1), mhWakeupPipeW(-1), mfWaiting(0),
1408 mStatus(VERR_WRONG_ORDER)
1409{
1410# ifdef DEBUG
1411 /* Excercise the code path (term() on a not-fully-initialised object) as
1412 * well as we can. On an uninitialised object this method is a sematic
1413 * no-op. */
1414 term();
1415 /* For now this probing method should only be used if nothing else is
1416 * available */
1417 if (!testing())
1418 {
1419# ifdef VBOX_USB_WITH_DBUS
1420 Assert(!hotplugDBusImpl::Available());
1421# endif
1422 }
1423# endif
1424 int rc;
1425 do {
1426 if (RT_FAILURE(rc = mWatches.getStatus()))
1427 break;
1428 mWatches.doHandle(SYSFS_USB_DEVICE_PATH);
1429 if (RT_FAILURE(rc = pipeCreateSimple(&mhWakeupPipeR, &mhWakeupPipeW)))
1430 break;
1431 } while(0);
1432 mStatus = rc;
1433 if (RT_FAILURE(rc))
1434 term();
1435}
1436
1437void hotplugInotifyImpl::term(void)
1438{
1439 /** This would probably be a pending segfault, so die cleanly */
1440 AssertRelease(!mfWaiting);
1441 if (mhWakeupPipeR != -1)
1442 {
1443 close(mhWakeupPipeR);
1444 mhWakeupPipeR = -1;
1445 }
1446 if (mhWakeupPipeW != -1)
1447 {
1448 close(mhWakeupPipeW);
1449 mhWakeupPipeW = -1;
1450 }
1451}
1452
1453int hotplugInotifyImpl::drainInotify()
1454{
1455 char chBuf[RTPATH_MAX + 256]; /* Should always be big enough */
1456 ssize_t cchRead;
1457
1458 AssertRCReturn(mStatus, VERR_WRONG_ORDER);
1459 errno = 0;
1460 do {
1461 cchRead = read(mWatches.getFD(), chBuf, sizeof(chBuf));
1462 } while (cchRead > 0);
1463 if (cchRead == 0)
1464 return VINF_SUCCESS;
1465 if (cchRead < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
1466 return VINF_SUCCESS;
1467 Assert(errno > 0);
1468 return RTErrConvertFromErrno(errno);
1469}
1470
1471int hotplugInotifyImpl::drainWakeupPipe(void)
1472{
1473 char szBuf[sizeof(SYSFS_WAKEUP_STRING)];
1474 ssize_t cbRead;
1475
1476 AssertRCReturn(mStatus, VERR_WRONG_ORDER);
1477 cbRead = read(mhWakeupPipeR, szBuf, sizeof(szBuf));
1478 Assert(cbRead > 0);
1479 return VINF_SUCCESS;
1480}
1481
1482int hotplugInotifyImpl::Wait(RTMSINTERVAL aMillies)
1483{
1484 int rc;
1485
1486 AssertRCReturn(mStatus, VERR_WRONG_ORDER);
1487 bool fEntered = ASMAtomicCmpXchgU32(&mfWaiting, 1, 0);
1488 AssertReturn(fEntered, VERR_WRONG_ORDER);
1489 do {
1490 struct pollfd pollFD[MAX_POLLID];
1491
1492 if (RT_FAILURE(rc = walkDirectory(SYSFS_USB_DEVICE_PATH, &mWatches,
1493 false)))
1494 break;
1495 pollFD[RPIPE_ID].fd = mhWakeupPipeR;
1496 pollFD[RPIPE_ID].events = POLLIN;
1497 pollFD[INOTIFY_ID].fd = mWatches.getFD();
1498 pollFD[INOTIFY_ID].events = POLLIN | POLLERR | POLLHUP;
1499 errno = 0;
1500 int cPolled = poll(pollFD, RT_ELEMENTS(pollFD), aMillies);
1501 if (cPolled < 0)
1502 {
1503 Assert(errno > 0);
1504 rc = RTErrConvertFromErrno(errno);
1505 }
1506 else if (pollFD[RPIPE_ID].revents)
1507 {
1508 rc = drainWakeupPipe();
1509 if (RT_SUCCESS(rc))
1510 rc = VERR_INTERRUPTED;
1511 break;
1512 }
1513 else if (!(pollFD[INOTIFY_ID].revents))
1514 {
1515 AssertBreakStmt(cPolled == 0, rc = VERR_INTERNAL_ERROR);
1516 rc = VERR_TIMEOUT;
1517 }
1518 Assert(errno == 0 || (RT_FAILURE(rc) && rc != VERR_TIMEOUT));
1519 if (RT_FAILURE(rc))
1520 break;
1521 AssertBreakStmt(cPolled == 1, rc = VERR_INTERNAL_ERROR);
1522 if (RT_FAILURE(rc = drainInotify()))
1523 break;
1524 } while (false);
1525 mfWaiting = 0;
1526 return rc;
1527}
1528
1529void hotplugInotifyImpl::Interrupt(void)
1530{
1531 AssertRCReturnVoid(mStatus);
1532 ssize_t cbWritten = write(mhWakeupPipeW, SYSFS_WAKEUP_STRING,
1533 sizeof(SYSFS_WAKEUP_STRING));
1534 if (cbWritten > 0)
1535 fsync(mhWakeupPipeW);
1536}
1537
1538# endif /* VBOX_USB_WITH_INOTIFY */
1539#endif /* VBOX_USB_WTH_SYSFS */
1540
1541VBoxMainHotplugWaiter::VBoxMainHotplugWaiter(void)
1542{
1543 try
1544 {
1545#ifdef VBOX_USB_WITH_SYSFS
1546# ifdef VBOX_WITH_DBUS
1547 if (hotplugDBusImpl::Available())
1548 {
1549 mImpl = new hotplugDBusImpl;
1550 return;
1551 }
1552# endif /* VBOX_WITH_DBUS */
1553# ifdef VBOX_USB_WITH_INOTIFY
1554 if (hotplugInotifyImpl::Available())
1555 {
1556 mImpl = new hotplugInotifyImpl;
1557 return;
1558 }
1559# endif /* VBOX_USB_WITH_INOTIFY */
1560#endif /* VBOX_USB_WITH_SYSFS */
1561 mImpl = new hotplugNullImpl;
1562 }
1563 catch(std::bad_alloc &e)
1564 { }
1565}
1566
1567#ifdef VBOX_USB_WITH_SYSFS
1568# ifdef VBOX_USB_WITH_INOTIFY
1569/**
1570 * Helper function to walk a directory, calling a function object on its files
1571 * @returns iprt status code
1572 * @param pcszPath Directory to walk.
1573 * @param pHandler Handler object which will be invoked on each file
1574 * @param useRealPath Whether to resolve the filename to its real path
1575 * before calling the handler. In this case the target
1576 * must exist.
1577 *
1578 * @returns IPRT status code
1579 */
1580/* static */
1581int walkDirectory(const char *pcszPath, pathHandler *pHandler, bool useRealPath)
1582{
1583 AssertPtrReturn(pcszPath, VERR_INVALID_POINTER);
1584 AssertPtrReturn(pHandler, VERR_INVALID_POINTER);
1585 LogFlowFunc (("pcszPath=%s, pHandler=%p\n", pcszPath, pHandler));
1586 PRTDIR pDir = NULL;
1587 int rc;
1588
1589 rc = RTDirOpen(&pDir, pcszPath);
1590 if (RT_FAILURE(rc))
1591 return rc;
1592 while (RT_SUCCESS(rc))
1593 {
1594 RTDIRENTRY entry;
1595 char szPath[RTPATH_MAX], szAbsPath[RTPATH_MAX];
1596
1597 rc = RTDirRead(pDir, &entry, NULL);
1598 Assert(rc != VERR_BUFFER_OVERFLOW); /* Should never happen... */
1599 /* We break on "no more files" as well as on "real" errors */
1600 if (RT_FAILURE(rc))
1601 break;
1602 if (entry.szName[0] == '.')
1603 continue;
1604 if (RTStrPrintf(szPath, sizeof(szPath), "%s/%s", pcszPath,
1605 entry.szName) >= sizeof(szPath))
1606 rc = VERR_BUFFER_OVERFLOW;
1607 if (RT_FAILURE(rc))
1608 break;
1609 if (useRealPath)
1610 {
1611 rc = RTPathReal(szPath, szAbsPath, sizeof(szAbsPath));
1612 AssertRCBreak(rc); /* sysfs should guarantee that this exists */
1613 if (!pHandler->doHandle(szAbsPath))
1614 break;
1615 }
1616 else
1617 if (!pHandler->doHandle(szPath))
1618 break;
1619 }
1620 RTDirClose(pDir);
1621 if (rc == VERR_NO_MORE_FILES)
1622 rc = VINF_SUCCESS;
1623 LogFlow (("rc=%Rrc\n", rc));
1624 return rc;
1625}
1626
1627
1628/**
1629 * Helper function to walk a sysfs directory for extracting information about
1630 * devices.
1631 * @returns iprt status code
1632 * @param pcszPath Sysfs directory to walk. Must exist.
1633 * @param pHandler Handler object which will be invoked on each directory
1634 * entry
1635 *
1636 * @returns IPRT status code
1637 */
1638/* static */
1639int getDeviceInfoFromSysfs(const char *pcszPath, pathHandler *pHandler)
1640{
1641 return walkDirectory(pcszPath, pHandler, true);
1642}
1643
1644
1645#define USBDEVICE_MAJOR 189
1646
1647/** Deduce the bus that a USB device is plugged into from the device node
1648 * number. See drivers/usb/core/hub.c:usb_new_device as of Linux 2.6.20. */
1649static unsigned usbBusFromDevNum(dev_t devNum)
1650{
1651 AssertReturn(devNum, 0);
1652 AssertReturn(major(devNum) == USBDEVICE_MAJOR, 0);
1653 return (minor(devNum) >> 7) + 1;
1654}
1655
1656
1657/** Deduce the device number of a USB device on the bus from the device node
1658 * number. See drivers/usb/core/hub.c:usb_new_device as of Linux 2.6.20. */
1659static unsigned usbDeviceFromDevNum(dev_t devNum)
1660{
1661 AssertReturn(devNum, 0);
1662 AssertReturn(major(devNum) == USBDEVICE_MAJOR, 0);
1663 return (minor(devNum) & 127) + 1;
1664}
1665
1666
1667/**
1668 * Tell whether a file in /sys/bus/usb/devices is a device rather than an
1669 * interface. To be used with getDeviceInfoFromSysfs().
1670 */
1671class matchUSBDevice : public pathHandler
1672{
1673 USBDeviceInfoList *mList;
1674public:
1675 matchUSBDevice(USBDeviceInfoList *pList) : mList(pList) {}
1676private:
1677 virtual bool handle(const char *pcszNode)
1678 {
1679 const char *pcszFile = strrchr(pcszNode, '/');
1680 if (strchr(pcszFile, ':'))
1681 return true;
1682 dev_t devnum = RTLinuxSysFsReadDevNumFile("%s/dev", pcszNode);
1683 /* Sanity test of our static helpers */
1684 Assert(usbBusFromDevNum(makedev(USBDEVICE_MAJOR, 517)) == 5);
1685 Assert(usbDeviceFromDevNum(makedev(USBDEVICE_MAJOR, 517)) == 6);
1686 AssertReturn (devnum, true);
1687 char szDevPath[RTPATH_MAX];
1688 ssize_t cchDevPath;
1689 cchDevPath = RTLinuxFindDevicePath(devnum, RTFS_TYPE_DEV_CHAR,
1690 szDevPath, sizeof(szDevPath),
1691 "/dev/bus/usb/%.3d/%.3d",
1692 usbBusFromDevNum(devnum),
1693 usbDeviceFromDevNum(devnum));
1694 if (cchDevPath < 0)
1695 return true;
1696 try
1697 {
1698 mList->push_back(USBDeviceInfo(szDevPath, pcszNode));
1699 }
1700 catch(std::bad_alloc &e)
1701 {
1702 return false;
1703 }
1704 return true;
1705 }
1706};
1707
1708/**
1709 * Tell whether a file in /sys/bus/usb/devices is an interface rather than a
1710 * device. To be used with getDeviceInfoFromSysfs().
1711 */
1712class matchUSBInterface : public pathHandler
1713{
1714 USBDeviceInfo *mInfo;
1715public:
1716 /** This constructor is currently used to unit test the class logic in
1717 * debug builds. Since no access is made to anything outside the class,
1718 * this shouldn't cause any slowdown worth mentioning. */
1719 matchUSBInterface(USBDeviceInfo *pInfo) : mInfo(pInfo)
1720 {
1721 Assert(isAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0",
1722 "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
1723 Assert(!isAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-1",
1724 "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
1725 Assert(!isAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0/driver",
1726 "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
1727 }
1728private:
1729 /** The logic for testing whether a sysfs address corresponds to an
1730 * interface of a device. Both must be referenced by their canonical
1731 * sysfs paths. This is not tested, as the test requires file-system
1732 * interaction. */
1733 bool isAnInterfaceOf(const char *pcszIface, const char *pcszDev)
1734 {
1735 size_t cchDev = strlen(pcszDev);
1736
1737 AssertPtr(pcszIface);
1738 AssertPtr(pcszDev);
1739 Assert(pcszIface[0] == '/');
1740 Assert(pcszDev[0] == '/');
1741 Assert(pcszDev[cchDev - 1] != '/');
1742 /* If this passes, pcszIface is at least cchDev long */
1743 if (strncmp(pcszIface, pcszDev, cchDev))
1744 return false;
1745 /* If this passes, pcszIface is longer than cchDev */
1746 if (pcszIface[cchDev] != '/')
1747 return false;
1748 /* In sysfs an interface is an immediate subdirectory of the device */
1749 if (strchr(pcszIface + cchDev + 1, '/'))
1750 return false;
1751 /* And it always has a colon in its name */
1752 if (!strchr(pcszIface + cchDev + 1, ':'))
1753 return false;
1754 /* And hopefully we have now elimitated everything else */
1755 return true;
1756 }
1757
1758 virtual bool handle(const char *pcszNode)
1759 {
1760 if (!isAnInterfaceOf(pcszNode, mInfo->mSysfsPath.c_str()))
1761 return true;
1762 try
1763 {
1764 mInfo->mInterfaces.push_back(pcszNode);
1765 }
1766 catch(std::bad_alloc &e)
1767 {
1768 return false;
1769 }
1770 return true;
1771 }
1772};
1773
1774/**
1775 * Helper function to query the sysfs subsystem for information about USB
1776 * devices attached to the system.
1777 * @returns iprt status code
1778 * @param pList where to add information about the drives detected
1779 * @param pfSuccess Did we find anything?
1780 *
1781 * @returns IPRT status code
1782 */
1783static int getUSBDeviceInfoFromSysfs(USBDeviceInfoList *pList,
1784 bool *pfSuccess)
1785{
1786 AssertPtrReturn(pList, VERR_INVALID_POINTER);
1787 AssertPtrNullReturn(pfSuccess, VERR_INVALID_POINTER); /* Valid or Null */
1788 LogFlowFunc (("pList=%p, pfSuccess=%p\n",
1789 pList, pfSuccess));
1790 size_t cDevices = pList->size();
1791 matchUSBDevice devHandler(pList);
1792 int rc = getDeviceInfoFromSysfs("/sys/bus/usb/devices", &devHandler);
1793 do {
1794 if (RT_FAILURE(rc))
1795 break;
1796 for (USBDeviceInfoList::iterator pInfo = pList->begin();
1797 pInfo != pList->end(); ++pInfo)
1798 {
1799 matchUSBInterface ifaceHandler(&*pInfo);
1800 rc = getDeviceInfoFromSysfs("/sys/bus/usb/devices", &ifaceHandler);
1801 if (RT_FAILURE(rc))
1802 break;
1803 }
1804 } while(0);
1805 if (RT_FAILURE(rc))
1806 /* Clean up again */
1807 while (pList->size() > cDevices)
1808 pList->pop_back();
1809 if (pfSuccess)
1810 *pfSuccess = RT_SUCCESS(rc);
1811 LogFlow (("rc=%Rrc\n", rc));
1812 return rc;
1813}
1814# endif /* VBOX_USB_WITH_INOTIFY */
1815#endif /* VBOX_USB_WITH_SYSFS */
1816
1817#if defined VBOX_USB_WITH_SYSFS && defined VBOX_USB_WITH_DBUS
1818/** Wrapper class around DBusError for automatic cleanup */
1819class autoDBusError
1820{
1821 DBusError mError;
1822public:
1823 autoDBusError () { dbus_error_init (&mError); }
1824 ~autoDBusError ()
1825 {
1826 if (IsSet())
1827 dbus_error_free (&mError);
1828 }
1829 DBusError &get () { return mError; }
1830 bool IsSet ()
1831 {
1832 Assert((mError.name == NULL) == (mError.message == NULL));
1833 return (mError.name != NULL);
1834 }
1835 bool HasName (const char *pcszName)
1836 {
1837 Assert((mError.name == NULL) == (mError.message == NULL));
1838 return (RTStrCmp (mError.name, pcszName) == 0);
1839 }
1840 void FlowLog ()
1841 {
1842 if (IsSet ())
1843 LogFlow(("DBus error %s: %s\n", mError.name, mError.message));
1844 }
1845};
1846
1847/**
1848 * Helper function for setting up a connection to the DBus daemon and
1849 * registering with the hal service.
1850 *
1851 * @note If libdbus is being loaded at runtime then be sure to call
1852 * VBoxDBusCheckPresence before calling this.
1853 * @returns iprt status code
1854 * @param ppConnection where to store the connection handle
1855 */
1856/* static */
1857int halInit (RTMemAutoPtr <DBusConnection, VBoxHalShutdown> *pConnection)
1858{
1859 AssertReturn(VALID_PTR (pConnection), VERR_INVALID_POINTER);
1860 LogFlowFunc (("pConnection=%p\n", pConnection));
1861 int rc = VINF_SUCCESS;
1862 bool halSuccess = true;
1863 autoDBusError dbusError;
1864
1865 RTMemAutoPtr <DBusConnection, VBoxDBusConnectionUnref> dbusConnection;
1866 dbusConnection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbusError.get());
1867 if (!dbusConnection)
1868 halSuccess = false;
1869 if (halSuccess)
1870 {
1871 dbus_connection_set_exit_on_disconnect (dbusConnection.get(), false);
1872 halSuccess = dbus_bus_name_has_owner (dbusConnection.get(),
1873 "org.freedesktop.Hal", &dbusError.get());
1874 }
1875 if (halSuccess)
1876 {
1877 dbus_bus_add_match (dbusConnection.get(),
1878 "type='signal',"
1879 "interface='org.freedesktop.Hal.Manager',"
1880 "sender='org.freedesktop.Hal',"
1881 "path='/org/freedesktop/Hal/Manager'",
1882 &dbusError.get());
1883 halSuccess = !dbusError.IsSet();
1884 }
1885 if (dbusError.HasName (DBUS_ERROR_NO_MEMORY))
1886 rc = VERR_NO_MEMORY;
1887 if (halSuccess)
1888 *pConnection = dbusConnection.release();
1889 LogFlowFunc(("rc=%Rrc, (*pConnection).get()=%p\n", rc, (*pConnection).get()));
1890 dbusError.FlowLog();
1891 return rc;
1892}
1893
1894/**
1895 * Helper function for setting up a private connection to the DBus daemon and
1896 * registering with the hal service. Private connections are considered
1897 * unsociable and should not be used unnecessarily (as per the DBus API docs).
1898 *
1899 * @note If libdbus is being loaded at runtime then be sure to call
1900 * VBoxDBusCheckPresence before calling this.
1901 * @returns iprt status code
1902 * @param pConnection where to store the connection handle
1903 */
1904/* static */
1905int halInitPrivate (RTMemAutoPtr <DBusConnection, VBoxHalShutdownPrivate> *pConnection)
1906{
1907 AssertReturn(VALID_PTR (pConnection), VERR_INVALID_POINTER);
1908 LogFlowFunc (("pConnection=%p\n", pConnection));
1909 int rc = VINF_SUCCESS;
1910 bool halSuccess = true;
1911 autoDBusError dbusError;
1912
1913 RTMemAutoPtr <DBusConnection, VBoxDBusConnectionCloseAndUnref> dbusConnection;
1914 dbusConnection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &dbusError.get());
1915 if (!dbusConnection)
1916 halSuccess = false;
1917 if (halSuccess)
1918 {
1919 dbus_connection_set_exit_on_disconnect (dbusConnection.get(), false);
1920 halSuccess = dbus_bus_name_has_owner (dbusConnection.get(),
1921 "org.freedesktop.Hal", &dbusError.get());
1922 }
1923 if (halSuccess)
1924 {
1925 dbus_bus_add_match (dbusConnection.get(),
1926 "type='signal',"
1927 "interface='org.freedesktop.Hal.Manager',"
1928 "sender='org.freedesktop.Hal',"
1929 "path='/org/freedesktop/Hal/Manager'",
1930 &dbusError.get());
1931 halSuccess = !dbusError.IsSet();
1932 }
1933 if (dbusError.HasName (DBUS_ERROR_NO_MEMORY))
1934 rc = VERR_NO_MEMORY;
1935 if (halSuccess)
1936 *pConnection = dbusConnection.release();
1937 LogFlowFunc(("rc=%Rrc, (*pConnection).get()=%p\n", rc, (*pConnection).get()));
1938 dbusError.FlowLog();
1939 return rc;
1940}
1941
1942/**
1943 * Helper function for shutting down a connection to DBus and hal.
1944 * @param pConnection the connection handle
1945 */
1946/* extern */
1947void VBoxHalShutdown (DBusConnection *pConnection)
1948{
1949 AssertReturnVoid(VALID_PTR (pConnection));
1950 LogFlowFunc (("pConnection=%p\n", pConnection));
1951 autoDBusError dbusError;
1952
1953 dbus_bus_remove_match (pConnection,
1954 "type='signal',"
1955 "interface='org.freedesktop.Hal.Manager',"
1956 "sender='org.freedesktop.Hal',"
1957 "path='/org/freedesktop/Hal/Manager'",
1958 &dbusError.get());
1959 dbus_connection_unref (pConnection);
1960 LogFlowFunc(("returning\n"));
1961 dbusError.FlowLog();
1962}
1963
1964/**
1965 * Helper function for shutting down a private connection to DBus and hal.
1966 * @param pConnection the connection handle
1967 */
1968/* extern */
1969void VBoxHalShutdownPrivate (DBusConnection *pConnection)
1970{
1971 AssertReturnVoid(VALID_PTR (pConnection));
1972 LogFlowFunc (("pConnection=%p\n", pConnection));
1973 autoDBusError dbusError;
1974
1975 dbus_bus_remove_match (pConnection,
1976 "type='signal',"
1977 "interface='org.freedesktop.Hal.Manager',"
1978 "sender='org.freedesktop.Hal',"
1979 "path='/org/freedesktop/Hal/Manager'",
1980 &dbusError.get());
1981 dbus_connection_close (pConnection);
1982 dbus_connection_unref (pConnection);
1983 LogFlowFunc(("returning\n"));
1984 dbusError.FlowLog();
1985}
1986
1987/** Wrapper around dbus_connection_unref. We need this to use it as a real
1988 * function in auto pointers, as a function pointer won't wash here. */
1989/* extern */
1990void VBoxDBusConnectionUnref(DBusConnection *pConnection)
1991{
1992 dbus_connection_unref(pConnection);
1993}
1994
1995/**
1996 * This function closes and unrefs a private connection to dbus. It should
1997 * only be called once no-one else is referencing the connection.
1998 */
1999/* extern */
2000void VBoxDBusConnectionCloseAndUnref(DBusConnection *pConnection)
2001{
2002 dbus_connection_close(pConnection);
2003 dbus_connection_unref(pConnection);
2004}
2005
2006/** Wrapper around dbus_message_unref. We need this to use it as a real
2007 * function in auto pointers, as a function pointer won't wash here. */
2008/* extern */
2009void VBoxDBusMessageUnref(DBusMessage *pMessage)
2010{
2011 dbus_message_unref(pMessage);
2012}
2013
2014/**
2015 * Find the UDIs of hal entries that contain Key=Value property.
2016 * @returns iprt status code. If a non-fatal error occurs, we return success
2017 * but reset pMessage to NULL.
2018 * @param pConnection an initialised connection DBus
2019 * @param pszKey the property key
2020 * @param pszValue the property value
2021 * @param pMessage where to store the return DBus message. This must be
2022 * parsed to get at the UDIs. NOT optional.
2023 */
2024/* static */
2025int halFindDeviceStringMatch (DBusConnection *pConnection, const char *pszKey,
2026 const char *pszValue,
2027 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> *pMessage)
2028{
2029 AssertReturn( VALID_PTR (pConnection) && VALID_PTR (pszKey)
2030 && VALID_PTR (pszValue) && VALID_PTR (pMessage),
2031 VERR_INVALID_POINTER);
2032 LogFlowFunc (("pConnection=%p, pszKey=%s, pszValue=%s, pMessage=%p\n",
2033 pConnection, pszKey, pszValue, pMessage));
2034 int rc = VINF_SUCCESS; /* We set this to failure on fatal errors. */
2035 bool halSuccess = true; /* We set this to false to abort the operation. */
2036 autoDBusError dbusError;
2037
2038 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> message, reply;
2039 if (halSuccess && RT_SUCCESS(rc))
2040 {
2041 message = dbus_message_new_method_call ("org.freedesktop.Hal",
2042 "/org/freedesktop/Hal/Manager",
2043 "org.freedesktop.Hal.Manager",
2044 "FindDeviceStringMatch");
2045 if (!message)
2046 rc = VERR_NO_MEMORY;
2047 }
2048 if (halSuccess && RT_SUCCESS(rc))
2049 {
2050 DBusMessageIter iterAppend;
2051 dbus_message_iter_init_append (message.get(), &iterAppend);
2052 dbus_message_iter_append_basic (&iterAppend, DBUS_TYPE_STRING, &pszKey);
2053 dbus_message_iter_append_basic (&iterAppend, DBUS_TYPE_STRING, &pszValue);
2054 reply = dbus_connection_send_with_reply_and_block (pConnection,
2055 message.get(), -1,
2056 &dbusError.get());
2057 if (!reply)
2058 halSuccess = false;
2059 }
2060 *pMessage = reply.release ();
2061 LogFlowFunc (("rc=%Rrc, *pMessage.value()=%p\n", rc, (*pMessage).get()));
2062 dbusError.FlowLog();
2063 return rc;
2064}
2065
2066/**
2067 * Find the UDIs of hal entries that contain Key=Value property and return the
2068 * result on the end of a vector of iprt::MiniString.
2069 * @returns iprt status code. If a non-fatal error occurs, we return success
2070 * but set *pfSuccess to false.
2071 * @param pConnection an initialised connection DBus
2072 * @param pszKey the property key
2073 * @param pszValue the property value
2074 * @param pMatches pointer to an array of iprt::MiniString to append the
2075 * results to. NOT optional.
2076 * @param pfSuccess will be set to true if the operation succeeds
2077 */
2078/* static */
2079int halFindDeviceStringMatchVector (DBusConnection *pConnection,
2080 const char *pszKey, const char *pszValue,
2081 std::vector<iprt::MiniString> *pMatches,
2082 bool *pfSuccess)
2083{
2084 AssertPtrReturn (pConnection, VERR_INVALID_POINTER);
2085 AssertPtrReturn (pszKey, VERR_INVALID_POINTER);
2086 AssertPtrReturn (pszValue, VERR_INVALID_POINTER);
2087 AssertPtrReturn (pMatches, VERR_INVALID_POINTER);
2088 AssertReturn(pfSuccess == NULL || VALID_PTR (pfSuccess), VERR_INVALID_POINTER);
2089 LogFlowFunc (("pConnection=%p, pszKey=%s, pszValue=%s, pMatches=%p, pfSuccess=%p\n",
2090 pConnection, pszKey, pszValue, pMatches, pfSuccess));
2091 int rc = VINF_SUCCESS; /* We set this to failure on fatal errors. */
2092 bool halSuccess = true; /* We set this to false to abort the operation. */
2093
2094 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> message, replyFind;
2095 DBusMessageIter iterFind, iterUdis;
2096
2097 if (halSuccess && RT_SUCCESS(rc))
2098 {
2099 rc = halFindDeviceStringMatch (pConnection, pszKey, pszValue,
2100 &replyFind);
2101 if (!replyFind)
2102 halSuccess = false;
2103 }
2104 if (halSuccess && RT_SUCCESS(rc))
2105 {
2106 dbus_message_iter_init (replyFind.get(), &iterFind);
2107 if (dbus_message_iter_get_arg_type (&iterFind) != DBUS_TYPE_ARRAY)
2108 halSuccess = false;
2109 }
2110 if (halSuccess && RT_SUCCESS(rc))
2111 dbus_message_iter_recurse (&iterFind, &iterUdis);
2112 for (; halSuccess && RT_SUCCESS(rc)
2113 && dbus_message_iter_get_arg_type (&iterUdis) == DBUS_TYPE_STRING;
2114 dbus_message_iter_next(&iterUdis))
2115 {
2116 /* Now get all UDIs from the iterator */
2117 const char *pszUdi;
2118 dbus_message_iter_get_basic (&iterUdis, &pszUdi);
2119 try
2120 {
2121 pMatches->push_back(pszUdi);
2122 }
2123 catch(std::bad_alloc &e)
2124 {
2125 rc = VERR_NO_MEMORY;
2126 }
2127 }
2128 if (pfSuccess != NULL)
2129 *pfSuccess = halSuccess;
2130 LogFlow (("rc=%Rrc, halSuccess=%d\n", rc, halSuccess));
2131 return rc;
2132}
2133
2134/**
2135 * Read a set of string properties for a device. If some of the properties are
2136 * not of type DBUS_TYPE_STRING or do not exist then a NULL pointer will be
2137 * returned for them.
2138 * @returns iprt status code. If the operation failed for non-fatal reasons
2139 * then we return success and leave pMessage untouched - reset it
2140 * before the call to detect this.
2141 * @param pConnection an initialised connection DBus
2142 * @param pszUdi the Udi of the device
2143 * @param cProps the number of property values to look up
2144 * @param papszKeys the keys of the properties to be looked up
2145 * @param papszValues where to store the values of the properties. The
2146 * strings returned will be valid until the message
2147 * returned in @a ppMessage is freed. Undefined if
2148 * the message is NULL.
2149 * @param pMessage where to store the return DBus message. The caller
2150 * is responsible for freeing this once they have
2151 * finished with the value strings. NOT optional.
2152 */
2153/* static */
2154int halGetPropertyStrings (DBusConnection *pConnection, const char *pszUdi,
2155 size_t cProps, const char **papszKeys,
2156 char **papszValues,
2157 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> *pMessage)
2158{
2159 AssertReturn( VALID_PTR (pConnection) && VALID_PTR (pszUdi)
2160 && VALID_PTR (papszKeys) && VALID_PTR (papszValues)
2161 && VALID_PTR (pMessage),
2162 VERR_INVALID_POINTER);
2163 LogFlowFunc (("pConnection=%p, pszUdi=%s, cProps=%llu, papszKeys=%p, papszValues=%p, pMessage=%p\n",
2164 pConnection, pszUdi, cProps, papszKeys, papszValues, pMessage));
2165 int rc = VINF_SUCCESS; /* We set this to failure on fatal errors. */
2166 bool halSuccess = true; /* We set this to false to abort the operation. */
2167 autoDBusError dbusError;
2168
2169 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> message, reply;
2170 DBusMessageIter iterGet, iterProps;
2171
2172 /* Initialise the return array to NULLs */
2173 for (size_t i = 0; i < cProps; ++i)
2174 papszValues[i] = NULL;
2175
2176 /* Send a GetAllProperties message to hald */
2177 message = dbus_message_new_method_call ("org.freedesktop.Hal", pszUdi,
2178 "org.freedesktop.Hal.Device",
2179 "GetAllProperties");
2180 if (!message)
2181 rc = VERR_NO_MEMORY;
2182 if (halSuccess && RT_SUCCESS(rc))
2183 {
2184 reply = dbus_connection_send_with_reply_and_block (pConnection,
2185 message.get(), -1,
2186 &dbusError.get());
2187 if (!reply)
2188 halSuccess = false;
2189 }
2190
2191 /* Parse the reply */
2192 if (halSuccess && RT_SUCCESS(rc))
2193 {
2194 dbus_message_iter_init (reply.get(), &iterGet);
2195 if ( dbus_message_iter_get_arg_type (&iterGet) != DBUS_TYPE_ARRAY
2196 && dbus_message_iter_get_element_type (&iterGet) != DBUS_TYPE_DICT_ENTRY)
2197 halSuccess = false;
2198 }
2199 if (halSuccess && RT_SUCCESS(rc))
2200 dbus_message_iter_recurse (&iterGet, &iterProps);
2201 /* Go through all entries in the reply and see if any match our keys. */
2202 while ( halSuccess && RT_SUCCESS(rc)
2203 && dbus_message_iter_get_arg_type (&iterProps)
2204 == DBUS_TYPE_DICT_ENTRY)
2205 {
2206 const char *pszKey;
2207 DBusMessageIter iterEntry, iterValue;
2208 dbus_message_iter_recurse (&iterProps, &iterEntry);
2209 dbus_message_iter_get_basic (&iterEntry, &pszKey);
2210 dbus_message_iter_next (&iterEntry);
2211 dbus_message_iter_recurse (&iterEntry, &iterValue);
2212 /* Fill in any matches. */
2213 for (size_t i = 0; i < cProps; ++i)
2214 if (strcmp (pszKey, papszKeys[i]) == 0)
2215 {
2216 if (dbus_message_iter_get_arg_type (&iterValue) == DBUS_TYPE_STRING)
2217 dbus_message_iter_get_basic (&iterValue, &papszValues[i]);
2218 }
2219 dbus_message_iter_next (&iterProps);
2220 }
2221 if (RT_SUCCESS(rc) && halSuccess)
2222 *pMessage = reply.release();
2223 if (dbusError.HasName (DBUS_ERROR_NO_MEMORY))
2224 rc = VERR_NO_MEMORY;
2225 LogFlowFunc (("rc=%Rrc, *pMessage.value()=%p\n", rc, (*pMessage).get()));
2226 dbusError.FlowLog();
2227 return rc;
2228}
2229
2230/**
2231 * Read a set of string properties for a device. If some properties do not
2232 * exist or are not of type DBUS_TYPE_STRING, we will still fetch the others.
2233 * @returns iprt status code. If the operation failed for non-fatal reasons
2234 * then we return success and set *pfSuccess to false.
2235 * @param pConnection an initialised connection DBus
2236 * @param pszUdi the Udi of the device
2237 * @param cProps the number of property values to look up
2238 * @param papszKeys the keys of the properties to be looked up
2239 * @param pMatches pointer to an empty array of iprt::MiniString to append the
2240 * results to. NOT optional.
2241 * @param pfMatches pointer to an array of boolean values indicating
2242 * whether the respective property is a string. If this
2243 * is not supplied then all properties must be strings
2244 * for the operation to be considered successful
2245 * @param pfSuccess will be set to true if the operation succeeds
2246 */
2247/* static */
2248int halGetPropertyStringsVector (DBusConnection *pConnection,
2249 const char *pszUdi, size_t cProps,
2250 const char **papszKeys,
2251 std::vector<iprt::MiniString> *pMatches,
2252 bool *pfMatches, bool *pfSuccess)
2253{
2254 AssertPtrReturn (pConnection, VERR_INVALID_POINTER);
2255 AssertPtrReturn (pszUdi, VERR_INVALID_POINTER);
2256 AssertPtrReturn (papszKeys, VERR_INVALID_POINTER);
2257 AssertPtrReturn (pMatches, VERR_INVALID_POINTER);
2258 AssertReturn((pfMatches == NULL) || VALID_PTR (pfMatches), VERR_INVALID_POINTER);
2259 AssertReturn((pfSuccess == NULL) || VALID_PTR (pfSuccess), VERR_INVALID_POINTER);
2260 AssertReturn(pMatches->empty(), VERR_INVALID_PARAMETER);
2261 LogFlowFunc (("pConnection=%p, pszUdi=%s, cProps=%llu, papszKeys=%p, pMatches=%p, pfMatches=%p, pfSuccess=%p\n",
2262 pConnection, pszUdi, cProps, papszKeys, pMatches, pfMatches, pfSuccess));
2263 RTMemAutoPtr <char *> values(cProps);
2264 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> message;
2265 bool halSuccess = true;
2266 int rc = halGetPropertyStrings (pConnection, pszUdi, cProps, papszKeys,
2267 values.get(), &message);
2268 if (!message)
2269 halSuccess = false;
2270 for (size_t i = 0; RT_SUCCESS(rc) && halSuccess && i < cProps; ++i)
2271 {
2272 bool fMatches = values[i] != NULL;
2273 if (pfMatches != NULL)
2274 pfMatches[i] = fMatches;
2275 else
2276 halSuccess = fMatches;
2277 try
2278 {
2279 pMatches->push_back(fMatches ? values[i] : "");
2280 }
2281 catch(std::bad_alloc &e)
2282 {
2283 rc = VERR_NO_MEMORY;
2284 }
2285 }
2286 if (pfSuccess != NULL)
2287 *pfSuccess = halSuccess;
2288 if (RT_SUCCESS(rc) && halSuccess)
2289 {
2290 Assert(pMatches->size() == cProps);
2291 AssertForEach(j, size_t, 0, cProps, (pfMatches == NULL)
2292 || (pfMatches[j] == true)
2293 || ((pfMatches[j] == false) && (pMatches[j].size() == 0)));
2294 }
2295 LogFlowFunc (("rc=%Rrc, halSuccess=%d\n", rc, halSuccess));
2296 return rc;
2297}
2298
2299
2300/**
2301 * Helper function to query the hal subsystem for information about USB devices
2302 * attached to the system.
2303 * @returns iprt status code
2304 * @param pList where to add information about the devices detected
2305 * @param pfSuccess will be set to true if all interactions with hal
2306 * succeeded and to false otherwise. Optional.
2307 *
2308 * @returns IPRT status code
2309 */
2310/* static */
2311int getUSBDeviceInfoFromHal(USBDeviceInfoList *pList, bool *pfSuccess)
2312{
2313 AssertReturn(VALID_PTR (pList) && (pfSuccess == NULL || VALID_PTR (pfSuccess)),
2314 VERR_INVALID_POINTER);
2315 LogFlowFunc (("pList=%p, pfSuccess=%p\n", pList, pfSuccess));
2316 int rc = VINF_SUCCESS; /* We set this to failure on fatal errors. */
2317 bool halSuccess = true; /* We set this to false to abort the operation. */
2318 autoDBusError dbusError;
2319
2320 RTMemAutoPtr<DBusMessage, VBoxDBusMessageUnref> message, replyFind, replyGet;
2321 RTMemAutoPtr<DBusConnection, VBoxHalShutdown> dbusConnection;
2322 DBusMessageIter iterFind, iterUdis;
2323
2324 /* Connect to hal */
2325 rc = halInit (&dbusConnection);
2326 if (!dbusConnection)
2327 halSuccess = false;
2328 /* Get an array of all devices in the usb_device subsystem */
2329 if (halSuccess && RT_SUCCESS(rc))
2330 {
2331 rc = halFindDeviceStringMatch(dbusConnection.get(), "info.subsystem",
2332 "usb_device", &replyFind);
2333 if (!replyFind)
2334 halSuccess = false;
2335 }
2336 if (halSuccess && RT_SUCCESS(rc))
2337 {
2338 dbus_message_iter_init(replyFind.get(), &iterFind);
2339 if (dbus_message_iter_get_arg_type (&iterFind) != DBUS_TYPE_ARRAY)
2340 halSuccess = false;
2341 }
2342 /* Recurse down into the array and query interesting information about the
2343 * entries. */
2344 if (halSuccess && RT_SUCCESS(rc))
2345 dbus_message_iter_recurse(&iterFind, &iterUdis);
2346 for (; halSuccess && RT_SUCCESS(rc)
2347 && dbus_message_iter_get_arg_type(&iterUdis) == DBUS_TYPE_STRING;
2348 dbus_message_iter_next(&iterUdis))
2349 {
2350 /* Get the device node and the sysfs path for the current entry. */
2351 const char *pszUdi;
2352 dbus_message_iter_get_basic (&iterUdis, &pszUdi);
2353 static const char *papszKeys[] = { "linux.device_file", "linux.sysfs_path" };
2354 char *papszValues[RT_ELEMENTS(papszKeys)];
2355 rc = halGetPropertyStrings(dbusConnection.get(), pszUdi, RT_ELEMENTS(papszKeys),
2356 papszKeys, papszValues, &replyGet);
2357 const char *pszDevice = papszValues[0], *pszSysfsPath = papszValues[1];
2358 /* Get the interfaces. */
2359 if (!!replyGet && pszDevice && pszSysfsPath)
2360 {
2361 USBDeviceInfo info(pszDevice, pszSysfsPath);
2362 bool ifaceSuccess = true; /* If we can't get the interfaces, just
2363 * skip this one device. */
2364 rc = getUSBInterfacesFromHal(&info.mInterfaces, pszUdi, &ifaceSuccess);
2365 if (RT_SUCCESS(rc) && halSuccess && ifaceSuccess)
2366 try
2367 {
2368 pList->push_back(info);
2369 }
2370 catch(std::bad_alloc &e)
2371 {
2372 rc = VERR_NO_MEMORY;
2373 }
2374 }
2375 }
2376 if (dbusError.HasName (DBUS_ERROR_NO_MEMORY))
2377 rc = VERR_NO_MEMORY;
2378 if (pfSuccess != NULL)
2379 *pfSuccess = halSuccess;
2380 LogFlow(("rc=%Rrc, halSuccess=%d\n", rc, halSuccess));
2381 dbusError.FlowLog();
2382 return rc;
2383}
2384
2385/**
2386 * Helper function to query the hal subsystem for information about USB devices
2387 * attached to the system, using the older API.
2388 * @returns iprt status code
2389 * @param pList where to add information about the devices detected
2390 * @param pfSuccess will be set to true if all interactions with hal
2391 * succeeded and to false otherwise. Optional.
2392 *
2393 * @returns IPRT status code
2394 */
2395/* static */
2396int getOldUSBDeviceInfoFromHal(USBDeviceInfoList *pList, bool *pfSuccess)
2397{
2398 AssertReturn(VALID_PTR (pList) && (pfSuccess == NULL || VALID_PTR (pfSuccess)),
2399 VERR_INVALID_POINTER);
2400 LogFlowFunc (("pList=%p, pfSuccess=%p\n", pList, pfSuccess));
2401 int rc = VINF_SUCCESS; /* We set this to failure on fatal errors. */
2402 bool halSuccess = true; /* We set this to false to abort the operation. */
2403 autoDBusError dbusError;
2404
2405 RTMemAutoPtr<DBusMessage, VBoxDBusMessageUnref> message, replyFind, replyGet;
2406 RTMemAutoPtr<DBusConnection, VBoxHalShutdown> dbusConnection;
2407 DBusMessageIter iterFind, iterUdis;
2408
2409 /* Connect to hal */
2410 rc = halInit(&dbusConnection);
2411 if (!dbusConnection)
2412 halSuccess = false;
2413 /* Get an array of all devices in the usb_device subsystem */
2414 if (halSuccess && RT_SUCCESS(rc))
2415 {
2416 rc = halFindDeviceStringMatch(dbusConnection.get(), "info.category",
2417 "usbraw", &replyFind);
2418 if (!replyFind)
2419 halSuccess = false;
2420 }
2421 if (halSuccess && RT_SUCCESS(rc))
2422 {
2423 dbus_message_iter_init(replyFind.get(), &iterFind);
2424 if (dbus_message_iter_get_arg_type(&iterFind) != DBUS_TYPE_ARRAY)
2425 halSuccess = false;
2426 }
2427 /* Recurse down into the array and query interesting information about the
2428 * entries. */
2429 if (halSuccess && RT_SUCCESS(rc))
2430 dbus_message_iter_recurse(&iterFind, &iterUdis);
2431 for (; halSuccess && RT_SUCCESS(rc)
2432 && dbus_message_iter_get_arg_type(&iterUdis) == DBUS_TYPE_STRING;
2433 dbus_message_iter_next(&iterUdis))
2434 {
2435 /* Get the device node and the sysfs path for the current entry. */
2436 const char *pszUdi;
2437 dbus_message_iter_get_basic(&iterUdis, &pszUdi);
2438 static const char *papszKeys[] = { "linux.device_file", "info.parent" };
2439 char *papszValues[RT_ELEMENTS(papszKeys)];
2440 rc = halGetPropertyStrings(dbusConnection.get(), pszUdi, RT_ELEMENTS(papszKeys),
2441 papszKeys, papszValues, &replyGet);
2442 const char *pszDevice = papszValues[0], *pszSysfsPath = papszValues[1];
2443 /* Get the interfaces. */
2444 if (!!replyGet && pszDevice && pszSysfsPath)
2445 {
2446 USBDeviceInfo info(pszDevice, pszSysfsPath);
2447 bool ifaceSuccess = false; /* If we can't get the interfaces, just
2448 * skip this one device. */
2449 rc = getUSBInterfacesFromHal(&info.mInterfaces, pszSysfsPath,
2450 &ifaceSuccess);
2451 if (RT_SUCCESS(rc) && halSuccess && ifaceSuccess)
2452 try
2453 {
2454 pList->push_back(info);
2455 }
2456 catch(std::bad_alloc &e)
2457 {
2458 rc = VERR_NO_MEMORY;
2459 }
2460 }
2461 }
2462 if (dbusError.HasName(DBUS_ERROR_NO_MEMORY))
2463 rc = VERR_NO_MEMORY;
2464 if (pfSuccess != NULL)
2465 *pfSuccess = halSuccess;
2466 LogFlow(("rc=%Rrc, halSuccess=%d\n", rc, halSuccess));
2467 dbusError.FlowLog();
2468 return rc;
2469}
2470
2471
2472/**
2473 * Helper function to query the hal subsystem for information about USB devices
2474 * attached to the system.
2475 * @returns iprt status code
2476 * @param pList where to add information about the devices detected. If
2477 * certain interfaces are not found (@a pfFound is false on
2478 * return) this may contain invalid information.
2479 * @param pcszUdi the hal UDI of the device
2480 * @param pfSuccess will be set to true if the operation succeeds and to
2481 * false if it fails for non-critical reasons. Optional.
2482 *
2483 * @returns IPRT status code
2484 */
2485/* static */
2486int getUSBInterfacesFromHal(std::vector<iprt::MiniString> *pList,
2487 const char *pcszUdi, bool *pfSuccess)
2488{
2489 AssertReturn(VALID_PTR(pList) && VALID_PTR(pcszUdi) &&
2490 (pfSuccess == NULL || VALID_PTR (pfSuccess)),
2491 VERR_INVALID_POINTER);
2492 LogFlowFunc(("pList=%p, pcszUdi=%s, pfSuccess=%p\n", pList, pcszUdi,
2493 pfSuccess));
2494 int rc = VINF_SUCCESS; /* We set this to failure on fatal errors. */
2495 bool halSuccess = true; /* We set this to false to abort the operation. */
2496 autoDBusError dbusError;
2497
2498 RTMemAutoPtr <DBusMessage, VBoxDBusMessageUnref> message, replyFind, replyGet;
2499 RTMemAutoPtr <DBusConnection, VBoxHalShutdown> dbusConnection;
2500 DBusMessageIter iterFind, iterUdis;
2501
2502 rc = halInit(&dbusConnection);
2503 if (!dbusConnection)
2504 halSuccess = false;
2505 if (halSuccess && RT_SUCCESS(rc))
2506 {
2507 /* Look for children of the current UDI. */
2508 rc = halFindDeviceStringMatch(dbusConnection.get(), "info.parent",
2509 pcszUdi, &replyFind);
2510 if (!replyFind)
2511 halSuccess = false;
2512 }
2513 if (halSuccess && RT_SUCCESS(rc))
2514 {
2515 dbus_message_iter_init(replyFind.get(), &iterFind);
2516 if (dbus_message_iter_get_arg_type(&iterFind) != DBUS_TYPE_ARRAY)
2517 halSuccess = false;
2518 }
2519 if (halSuccess && RT_SUCCESS(rc))
2520 dbus_message_iter_recurse(&iterFind, &iterUdis);
2521 for (; halSuccess && RT_SUCCESS(rc)
2522 && dbus_message_iter_get_arg_type(&iterUdis) == DBUS_TYPE_STRING;
2523 dbus_message_iter_next(&iterUdis))
2524 {
2525 /* Now get the sysfs path and the subsystem from the iterator */
2526 const char *pszUdi;
2527 dbus_message_iter_get_basic(&iterUdis, &pszUdi);
2528 static const char *papszKeys[] = { "linux.sysfs_path", "info.subsystem",
2529 "linux.subsystem" };
2530 char *papszValues[RT_ELEMENTS(papszKeys)];
2531 rc = halGetPropertyStrings(dbusConnection.get(), pszUdi, RT_ELEMENTS(papszKeys),
2532 papszKeys, papszValues, &replyGet);
2533 const char *pszSysfsPath = papszValues[0], *pszInfoSubsystem = papszValues[1],
2534 *pszLinuxSubsystem = papszValues[2];
2535 if (!replyGet)
2536 halSuccess = false;
2537 if (!!replyGet && pszSysfsPath == NULL)
2538 halSuccess = false;
2539 if ( halSuccess && RT_SUCCESS(rc)
2540 && RTStrCmp (pszInfoSubsystem, "usb_device") != 0 /* Children of buses can also be devices. */
2541 && RTStrCmp (pszLinuxSubsystem, "usb_device") != 0)
2542 try
2543 {
2544 pList->push_back(pszSysfsPath);
2545 }
2546 catch(std::bad_alloc &e)
2547 {
2548 rc = VERR_NO_MEMORY;
2549 }
2550 }
2551 if (dbusError.HasName(DBUS_ERROR_NO_MEMORY))
2552 rc = VERR_NO_MEMORY;
2553 if (pfSuccess != NULL)
2554 *pfSuccess = halSuccess;
2555 LogFlow(("rc=%Rrc, halSuccess=%d\n", rc, halSuccess));
2556 dbusError.FlowLog();
2557 return rc;
2558}
2559
2560/**
2561 * When it is registered with DBus, this function will be called by
2562 * dbus_connection_read_write_dispatch each time a message is received over the
2563 * DBus connection. We check whether that message was caused by a hal device
2564 * hotplug event, and if so we set a flag. dbus_connection_read_write_dispatch
2565 * will return after calling its filter functions, and its caller should then
2566 * check the status of the flag passed to the filter function.
2567 *
2568 * @param pConnection The DBus connection we are using.
2569 * @param pMessage The DBus message which just arrived.
2570 * @param pvUser A pointer to the flag variable we are to set.
2571 */
2572/* static */
2573DBusHandlerResult dbusFilterFunction(DBusConnection * /* pConnection */,
2574 DBusMessage *pMessage, void *pvUser)
2575{
2576 volatile bool *pTriggered = reinterpret_cast<volatile bool *>(pvUser);
2577 if ( dbus_message_is_signal(pMessage, "org.freedesktop.Hal.Manager",
2578 "DeviceAdded")
2579 || dbus_message_is_signal(pMessage, "org.freedesktop.Hal.Manager",
2580 "DeviceRemoved"))
2581 {
2582 *pTriggered = true;
2583 }
2584 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2585}
2586#endif /* VBOX_USB_WITH_SYSFS && VBOX_USB_WITH_DBUS */
2587
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette