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