1 | /* $Id: HostHardwareLinux.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Classes for handling hardware detection under Linux. Please feel free to
|
---|
4 | * expand these to work for other systems (Solaris!) or to add new ones for
|
---|
5 | * other systems.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008-2010 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 |
|
---|
26 | #include <HostHardwareLinux.h>
|
---|
27 | #include <vector.h>
|
---|
28 |
|
---|
29 | #include <VBox/err.h>
|
---|
30 | #include <VBox/log.h>
|
---|
31 |
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/dir.h>
|
---|
34 | #include <iprt/env.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/thread.h> /* for RTThreadSleep() */
|
---|
41 |
|
---|
42 | #include <linux/cdrom.h>
|
---|
43 | #include <linux/fd.h>
|
---|
44 | #include <linux/major.h>
|
---|
45 | #include <scsi/scsi.h>
|
---|
46 |
|
---|
47 | #include <iprt/linux/sysfs.h>
|
---|
48 |
|
---|
49 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
50 | # ifdef VBOX_USB_WITH_INOTIFY
|
---|
51 | # include <dlfcn.h>
|
---|
52 | # include <fcntl.h>
|
---|
53 | # include <poll.h>
|
---|
54 | # include <signal.h>
|
---|
55 | # include <unistd.h>
|
---|
56 | # endif
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #include <vector>
|
---|
60 |
|
---|
61 | #include <errno.h>
|
---|
62 | #include <dirent.h>
|
---|
63 | #include <limits.h>
|
---|
64 | #include <stdio.h>
|
---|
65 | #include <stdlib.h>
|
---|
66 | #include <sys/types.h>
|
---|
67 |
|
---|
68 | /******************************************************************************
|
---|
69 | * Global Variables *
|
---|
70 | ******************************************************************************/
|
---|
71 |
|
---|
72 | #ifdef TESTCASE
|
---|
73 | static bool testing() { return true; }
|
---|
74 | static bool fNoProbe = false;
|
---|
75 | static bool noProbe() { return fNoProbe; }
|
---|
76 | static void setNoProbe(bool val) { fNoProbe = val; }
|
---|
77 | #else
|
---|
78 | static bool testing() { return false; }
|
---|
79 | static bool noProbe() { return false; }
|
---|
80 | static void setNoProbe(bool val) { (void)val; }
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | /******************************************************************************
|
---|
84 | * Typedefs and Defines *
|
---|
85 | ******************************************************************************/
|
---|
86 |
|
---|
87 | static int getDriveInfoFromEnv(const char *pcszVar, DriveInfoList *pList,
|
---|
88 | bool isDVD, bool *pfSuccess);
|
---|
89 | static int getDriveInfoFromDev(DriveInfoList *pList, bool isDVD,
|
---|
90 | bool *pfSuccess);
|
---|
91 | static int getDriveInfoFromSysfs(DriveInfoList *pList, bool isDVD,
|
---|
92 | bool *pfSuccess);
|
---|
93 |
|
---|
94 | /** Find the length of a string, ignoring trailing non-ascii or control
|
---|
95 | * characters */
|
---|
96 | static size_t strLenStripped(const char *pcsz)
|
---|
97 | {
|
---|
98 | size_t cch = 0;
|
---|
99 | for (size_t i = 0; pcsz[i] != '\0'; ++i)
|
---|
100 | if (pcsz[i] > 32 && pcsz[i] < 127)
|
---|
101 | cch = i;
|
---|
102 | return cch + 1;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Get the name of a floppy drive according to the Linux floppy driver.
|
---|
108 | * @returns true on success, false if the name was not available (i.e. the
|
---|
109 | * device was not readable, or the file name wasn't a PC floppy
|
---|
110 | * device)
|
---|
111 | * @param pcszNode the path to the device node for the device
|
---|
112 | * @param Number the Linux floppy driver number for the drive. Required.
|
---|
113 | * @param pszName where to store the name retrieved
|
---|
114 | */
|
---|
115 | static bool floppyGetName(const char *pcszNode, unsigned Number,
|
---|
116 | floppy_drive_name pszName)
|
---|
117 | {
|
---|
118 | AssertPtrReturn(pcszNode, false);
|
---|
119 | AssertPtrReturn(pszName, false);
|
---|
120 | AssertReturn(Number <= 7, false);
|
---|
121 | RTFILE File;
|
---|
122 | int rc = RTFileOpen(&File, pcszNode, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_NON_BLOCK);
|
---|
123 | if (RT_SUCCESS(rc))
|
---|
124 | {
|
---|
125 | int rcIoCtl;
|
---|
126 | rc = RTFileIoCtl(File, FDGETDRVTYP, pszName, 0, &rcIoCtl);
|
---|
127 | RTFileClose(File);
|
---|
128 | if (RT_SUCCESS(rc) && rcIoCtl >= 0)
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Create a UDI and a description for a floppy drive based on a number and the
|
---|
137 | * driver's name for it. We deliberately return an ugly sequence of
|
---|
138 | * characters as the description rather than an English language string to
|
---|
139 | * avoid translation issues.
|
---|
140 | *
|
---|
141 | * @returns true if we know the device to be valid, false otherwise
|
---|
142 | * @param pcszName the floppy driver name for the device (optional)
|
---|
143 | * @param Number the number of the floppy (0 to 3 on FDC 0, 4 to 7 on
|
---|
144 | * FDC 1)
|
---|
145 | * @param pszDesc where to store the device description (optional)
|
---|
146 | * @param cchDesc the size of the buffer in @a pszDesc
|
---|
147 | * @param pszUdi where to store the device UDI (optional)
|
---|
148 | * @param cchUdi the size of the buffer in @a pszUdi
|
---|
149 | */
|
---|
150 | static void floppyCreateDeviceStrings(const floppy_drive_name pcszName,
|
---|
151 | unsigned Number, char *pszDesc,
|
---|
152 | size_t cchDesc, char *pszUdi,
|
---|
153 | size_t cchUdi)
|
---|
154 | {
|
---|
155 | AssertPtrNullReturnVoid(pcszName);
|
---|
156 | AssertPtrNullReturnVoid(pszDesc);
|
---|
157 | AssertReturnVoid(!pszDesc || cchDesc > 0);
|
---|
158 | AssertPtrNullReturnVoid(pszUdi);
|
---|
159 | AssertReturnVoid(!pszUdi || cchUdi > 0);
|
---|
160 | AssertReturnVoid(Number <= 7);
|
---|
161 | if (pcszName)
|
---|
162 | {
|
---|
163 | const char *pcszSize;
|
---|
164 | switch(pcszName[0])
|
---|
165 | {
|
---|
166 | case 'd': case 'q': case 'h':
|
---|
167 | pcszSize = "5.25\"";
|
---|
168 | break;
|
---|
169 | case 'D': case 'H': case 'E': case 'u':
|
---|
170 | pcszSize = "3.5\"";
|
---|
171 | break;
|
---|
172 | default:
|
---|
173 | pcszSize = "(unknown)";
|
---|
174 | }
|
---|
175 | if (pszDesc)
|
---|
176 | RTStrPrintf(pszDesc, cchDesc, "%s %s K%s", pcszSize, &pcszName[1],
|
---|
177 | Number > 3 ? ", FDC 2" : "");
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | if (pszDesc)
|
---|
182 | RTStrPrintf(pszDesc, cchDesc, "FDD %d%s", (Number & 4) + 1,
|
---|
183 | Number > 3 ? ", FDC 2" : "");
|
---|
184 | }
|
---|
185 | if (pszUdi)
|
---|
186 | RTStrPrintf(pszUdi, cchUdi,
|
---|
187 | "/org/freedesktop/Hal/devices/platform_floppy_%u_storage",
|
---|
188 | Number);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Check whether a device number might correspond to a CD-ROM device according
|
---|
194 | * to Documentation/devices.txt in the Linux kernel source.
|
---|
195 | * @returns true if it might, false otherwise
|
---|
196 | * @param Number the device number (major and minor combination)
|
---|
197 | */
|
---|
198 | static bool isCdromDevNum(dev_t Number)
|
---|
199 | {
|
---|
200 | int major = major(Number);
|
---|
201 | int minor = minor(Number);
|
---|
202 | if ((major == IDE0_MAJOR) && !(minor & 0x3f))
|
---|
203 | return true;
|
---|
204 | if (major == SCSI_CDROM_MAJOR)
|
---|
205 | return true;
|
---|
206 | if (major == CDU31A_CDROM_MAJOR)
|
---|
207 | return true;
|
---|
208 | if (major == GOLDSTAR_CDROM_MAJOR)
|
---|
209 | return true;
|
---|
210 | if (major == OPTICS_CDROM_MAJOR)
|
---|
211 | return true;
|
---|
212 | if (major == SANYO_CDROM_MAJOR)
|
---|
213 | return true;
|
---|
214 | if (major == MITSUMI_X_CDROM_MAJOR)
|
---|
215 | return true;
|
---|
216 | if ((major == IDE1_MAJOR) && !(minor & 0x3f))
|
---|
217 | return true;
|
---|
218 | if (major == MITSUMI_CDROM_MAJOR)
|
---|
219 | return true;
|
---|
220 | if (major == CDU535_CDROM_MAJOR)
|
---|
221 | return true;
|
---|
222 | if (major == MATSUSHITA_CDROM_MAJOR)
|
---|
223 | return true;
|
---|
224 | if (major == MATSUSHITA_CDROM2_MAJOR)
|
---|
225 | return true;
|
---|
226 | if (major == MATSUSHITA_CDROM3_MAJOR)
|
---|
227 | return true;
|
---|
228 | if (major == MATSUSHITA_CDROM4_MAJOR)
|
---|
229 | return true;
|
---|
230 | if (major == AZTECH_CDROM_MAJOR)
|
---|
231 | return true;
|
---|
232 | if (major == 30 /* CM205_CDROM_MAJOR */) /* no #define for some reason */
|
---|
233 | return true;
|
---|
234 | if (major == CM206_CDROM_MAJOR)
|
---|
235 | return true;
|
---|
236 | if ((major == IDE3_MAJOR) && !(minor & 0x3f))
|
---|
237 | return true;
|
---|
238 | if (major == 46 /* Parallel port ATAPI CD-ROM */) /* no #define */
|
---|
239 | return true;
|
---|
240 | if ((major == IDE4_MAJOR) && !(minor & 0x3f))
|
---|
241 | return true;
|
---|
242 | if ((major == IDE5_MAJOR) && !(minor & 0x3f))
|
---|
243 | return true;
|
---|
244 | if ((major == IDE6_MAJOR) && !(minor & 0x3f))
|
---|
245 | return true;
|
---|
246 | if ((major == IDE7_MAJOR) && !(minor & 0x3f))
|
---|
247 | return true;
|
---|
248 | if ((major == IDE8_MAJOR) && !(minor & 0x3f))
|
---|
249 | return true;
|
---|
250 | if ((major == IDE9_MAJOR) && !(minor & 0x3f))
|
---|
251 | return true;
|
---|
252 | if (major == 113 /* VIOCD_MAJOR */)
|
---|
253 | return true;
|
---|
254 | return false;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Send an SCSI INQUIRY command to a device and return selected information.
|
---|
260 | * @returns iprt status code
|
---|
261 | * @returns VERR_TRY_AGAIN if the query failed but might succeed next time
|
---|
262 | * @param pcszNode the full path to the device node
|
---|
263 | * @param pu8Type where to store the SCSI device type on success (optional)
|
---|
264 | * @param pchVendor where to store the vendor id string on success (optional)
|
---|
265 | * @param cchVendor the size of the @a pchVendor buffer
|
---|
266 | * @param pchModel where to store the product id string on success (optional)
|
---|
267 | * @param cchModel the size of the @a pchModel buffer
|
---|
268 | * @note check documentation on the SCSI INQUIRY command and the Linux kernel
|
---|
269 | * SCSI headers included above if you want to understand what is going
|
---|
270 | * on in this method.
|
---|
271 | */
|
---|
272 | static int cdromDoInquiry(const char *pcszNode, uint8_t *pu8Type,
|
---|
273 | char *pchVendor, size_t cchVendor, char *pchModel,
|
---|
274 | size_t cchModel)
|
---|
275 | {
|
---|
276 | LogRelFlowFunc(("pcszNode=%s, pu8Type=%p, pchVendor=%p, cchVendor=%llu, pchModel=%p, cchModel=%llu\n",
|
---|
277 | pcszNode, pu8Type, pchVendor, cchVendor, pchModel,
|
---|
278 | cchModel));
|
---|
279 | AssertPtrReturn(pcszNode, VERR_INVALID_POINTER);
|
---|
280 | AssertPtrNullReturn(pu8Type, VERR_INVALID_POINTER);
|
---|
281 | AssertPtrNullReturn(pchVendor, VERR_INVALID_POINTER);
|
---|
282 | AssertPtrNullReturn(pchModel, VERR_INVALID_POINTER);
|
---|
283 |
|
---|
284 | RTFILE hFile;
|
---|
285 | int rc = RTFileOpen(&hFile, pcszNode, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_NON_BLOCK);
|
---|
286 | if (RT_SUCCESS(rc))
|
---|
287 | {
|
---|
288 | int rcIoCtl = 0;
|
---|
289 | unsigned char u8Response[96] = { 0 };
|
---|
290 | struct cdrom_generic_command CdromCommandReq;
|
---|
291 | RT_ZERO(CdromCommandReq);
|
---|
292 | CdromCommandReq.cmd[0] = INQUIRY;
|
---|
293 | CdromCommandReq.cmd[4] = sizeof(u8Response);
|
---|
294 | CdromCommandReq.buffer = u8Response;
|
---|
295 | CdromCommandReq.buflen = sizeof(u8Response);
|
---|
296 | CdromCommandReq.data_direction = CGC_DATA_READ;
|
---|
297 | CdromCommandReq.timeout = 5000; /* ms */
|
---|
298 | rc = RTFileIoCtl(hFile, CDROM_SEND_PACKET, &CdromCommandReq, 0, &rcIoCtl);
|
---|
299 | if (RT_SUCCESS(rc) && rcIoCtl < 0)
|
---|
300 | rc = RTErrConvertFromErrno(-CdromCommandReq.stat);
|
---|
301 | RTFileClose(hFile);
|
---|
302 |
|
---|
303 | if (RT_SUCCESS(rc))
|
---|
304 | {
|
---|
305 | if (pu8Type)
|
---|
306 | *pu8Type = u8Response[0] & 0x1f;
|
---|
307 | if (pchVendor)
|
---|
308 | RTStrPrintf(pchVendor, cchVendor, "%.8s",
|
---|
309 | &u8Response[8] /* vendor id string */);
|
---|
310 | if (pchModel)
|
---|
311 | RTStrPrintf(pchModel, cchModel, "%.16s",
|
---|
312 | &u8Response[16] /* product id string */);
|
---|
313 | LogRelFlowFunc(("returning success: type=%u, vendor=%.8s, product=%.16s\n",
|
---|
314 | u8Response[0] & 0x1f, &u8Response[8], &u8Response[16]));
|
---|
315 | return VINF_SUCCESS;
|
---|
316 | }
|
---|
317 | }
|
---|
318 | LogRelFlowFunc(("returning %Rrc\n", rc));
|
---|
319 | return rc;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Initialise the device strings (description and UDI) for a DVD drive based on
|
---|
325 | * vendor and model name strings.
|
---|
326 | * @param pcszVendor the vendor ID string
|
---|
327 | * @param pcszModel the product ID string
|
---|
328 | * @param pszDesc where to store the description string (optional)
|
---|
329 | * @param cchDesc the size of the buffer in @pszDesc
|
---|
330 | * @param pszUdi where to store the UDI string (optional)
|
---|
331 | * @param cchUdi the size of the buffer in @pszUdi
|
---|
332 | */
|
---|
333 | /* static */
|
---|
334 | void dvdCreateDeviceStrings(const char *pcszVendor, const char *pcszModel,
|
---|
335 | char *pszDesc, size_t cchDesc, char *pszUdi,
|
---|
336 | size_t cchUdi)
|
---|
337 | {
|
---|
338 | AssertPtrReturnVoid(pcszVendor);
|
---|
339 | AssertPtrReturnVoid(pcszModel);
|
---|
340 | AssertPtrNullReturnVoid(pszDesc);
|
---|
341 | AssertReturnVoid(!pszDesc || cchDesc > 0);
|
---|
342 | AssertPtrNullReturnVoid(pszUdi);
|
---|
343 | AssertReturnVoid(!pszUdi || cchUdi > 0);
|
---|
344 | char szCleaned[128];
|
---|
345 | size_t cchVendor = strLenStripped(pcszVendor);
|
---|
346 | size_t cchModel = strLenStripped(pcszModel);
|
---|
347 |
|
---|
348 | /* Create a cleaned version of the model string for the UDI string. */
|
---|
349 | for (unsigned i = 0; i < sizeof(szCleaned) && pcszModel[i] != '\0'; ++i)
|
---|
350 | if ( (pcszModel[i] >= '0' && pcszModel[i] <= '9')
|
---|
351 | || (pcszModel[i] >= 'A' && pcszModel[i] <= 'z'))
|
---|
352 | szCleaned[i] = pcszModel[i];
|
---|
353 | else
|
---|
354 | szCleaned[i] = '_';
|
---|
355 | szCleaned[RT_MIN(cchModel, sizeof(szCleaned) - 1)] = '\0';
|
---|
356 |
|
---|
357 | /* Construct the description string as "Vendor Product" */
|
---|
358 | if (pszDesc)
|
---|
359 | {
|
---|
360 | if (cchVendor > 0)
|
---|
361 | RTStrPrintf(pszDesc, cchDesc, "%.*s %s", cchVendor, pcszVendor,
|
---|
362 | cchModel > 0 ? pcszModel : "(unknown drive model)");
|
---|
363 | else
|
---|
364 | RTStrPrintf(pszDesc, cchDesc, "%s", pcszModel);
|
---|
365 | }
|
---|
366 | /* Construct the UDI string */
|
---|
367 | if (pszUdi)
|
---|
368 | {
|
---|
369 | if (cchModel > 0)
|
---|
370 | RTStrPrintf(pszUdi, cchUdi,
|
---|
371 | "/org/freedesktop/Hal/devices/storage_model_%s",
|
---|
372 | szCleaned);
|
---|
373 | else
|
---|
374 | pszUdi[0] = '\0';
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Check whether a device node points to a valid device and create a UDI and
|
---|
381 | * a description for it, and store the device number, if it does.
|
---|
382 | * @returns true if the device is valid, false otherwise
|
---|
383 | * @param pcszNode the path to the device node
|
---|
384 | * @param isDVD are we looking for a DVD device (or a floppy device)?
|
---|
385 | * @param pDevice where to store the device node (optional)
|
---|
386 | * @param pszDesc where to store the device description (optional)
|
---|
387 | * @param cchDesc the size of the buffer in @a pszDesc
|
---|
388 | * @param pszUdi where to store the device UDI (optional)
|
---|
389 | * @param cchUdi the size of the buffer in @a pszUdi
|
---|
390 | */
|
---|
391 | static bool devValidateDevice(const char *pcszNode, bool isDVD, dev_t *pDevice,
|
---|
392 | char *pszDesc, size_t cchDesc, char *pszUdi,
|
---|
393 | size_t cchUdi)
|
---|
394 | {
|
---|
395 | AssertPtrReturn(pcszNode, false);
|
---|
396 | AssertPtrNullReturn(pDevice, false);
|
---|
397 | AssertPtrNullReturn(pszDesc, false);
|
---|
398 | AssertReturn(!pszDesc || cchDesc > 0, false);
|
---|
399 | AssertPtrNullReturn(pszUdi, false);
|
---|
400 | AssertReturn(!pszUdi || cchUdi > 0, false);
|
---|
401 | RTFSOBJINFO ObjInfo;
|
---|
402 | if (RT_FAILURE(RTPathQueryInfo(pcszNode, &ObjInfo, RTFSOBJATTRADD_UNIX)))
|
---|
403 | return false;
|
---|
404 | if (!RTFS_IS_DEV_BLOCK(ObjInfo.Attr.fMode))
|
---|
405 | return false;
|
---|
406 | if (pDevice)
|
---|
407 | *pDevice = ObjInfo.Attr.u.Unix.Device;
|
---|
408 | if (isDVD)
|
---|
409 | {
|
---|
410 | char szVendor[128], szModel[128];
|
---|
411 | uint8_t u8Type;
|
---|
412 | if (!isCdromDevNum(ObjInfo.Attr.u.Unix.Device))
|
---|
413 | return false;
|
---|
414 | if (RT_FAILURE(cdromDoInquiry(pcszNode, &u8Type,
|
---|
415 | szVendor, sizeof(szVendor),
|
---|
416 | szModel, sizeof(szModel))))
|
---|
417 | return false;
|
---|
418 | if (u8Type != TYPE_ROM)
|
---|
419 | return false;
|
---|
420 | dvdCreateDeviceStrings(szVendor, szModel, pszDesc, cchDesc,
|
---|
421 | pszUdi, cchUdi);
|
---|
422 | }
|
---|
423 | else
|
---|
424 | {
|
---|
425 | /* Floppies on Linux are legacy devices with hardcoded majors and
|
---|
426 | * minors */
|
---|
427 | unsigned Number;
|
---|
428 | floppy_drive_name szName;
|
---|
429 | if (major(ObjInfo.Attr.u.Unix.Device) != FLOPPY_MAJOR)
|
---|
430 | return false;
|
---|
431 | switch (minor(ObjInfo.Attr.u.Unix.Device))
|
---|
432 | {
|
---|
433 | case 0: case 1: case 2: case 3:
|
---|
434 | Number = minor(ObjInfo.Attr.u.Unix.Device);
|
---|
435 | break;
|
---|
436 | case 128: case 129: case 130: case 131:
|
---|
437 | Number = minor(ObjInfo.Attr.u.Unix.Device) - 128 + 4;
|
---|
438 | break;
|
---|
439 | default:
|
---|
440 | return false;
|
---|
441 | }
|
---|
442 | if (!floppyGetName(pcszNode, Number, szName))
|
---|
443 | return false;
|
---|
444 | floppyCreateDeviceStrings(szName, Number, pszDesc, cchDesc, pszUdi,
|
---|
445 | cchUdi);
|
---|
446 | }
|
---|
447 | return true;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | int VBoxMainDriveInfo::updateDVDs ()
|
---|
452 | {
|
---|
453 | LogFlowThisFunc(("entered\n"));
|
---|
454 | int rc = VINF_SUCCESS;
|
---|
455 | bool success = false; /* Have we succeeded in finding anything yet? */
|
---|
456 | try
|
---|
457 | {
|
---|
458 | mDVDList.clear ();
|
---|
459 | /* Always allow the user to override our auto-detection using an
|
---|
460 | * environment variable. */
|
---|
461 | if (RT_SUCCESS(rc) && (!success || testing()))
|
---|
462 | rc = getDriveInfoFromEnv ("VBOX_CDROM", &mDVDList, true /* isDVD */,
|
---|
463 | &success);
|
---|
464 | setNoProbe(false);
|
---|
465 | if (RT_SUCCESS(rc) && (!success | testing()))
|
---|
466 | rc = getDriveInfoFromSysfs(&mDVDList, true /* isDVD */, &success);
|
---|
467 | if (RT_SUCCESS(rc) && testing())
|
---|
468 | {
|
---|
469 | setNoProbe(true);
|
---|
470 | rc = getDriveInfoFromSysfs(&mDVDList, true /* isDVD */, &success);
|
---|
471 | }
|
---|
472 | /* Walk through the /dev subtree if nothing else has helped. */
|
---|
473 | if (RT_SUCCESS(rc) && (!success | testing()))
|
---|
474 | rc = getDriveInfoFromDev(&mDVDList, true /* isDVD */, &success);
|
---|
475 | }
|
---|
476 | catch(std::bad_alloc &e)
|
---|
477 | {
|
---|
478 | rc = VERR_NO_MEMORY;
|
---|
479 | }
|
---|
480 | LogFlowThisFunc(("rc=%Rrc\n", rc));
|
---|
481 | return rc;
|
---|
482 | }
|
---|
483 |
|
---|
484 | int VBoxMainDriveInfo::updateFloppies ()
|
---|
485 | {
|
---|
486 | LogFlowThisFunc(("entered\n"));
|
---|
487 | int rc = VINF_SUCCESS;
|
---|
488 | bool success = false; /* Have we succeeded in finding anything yet? */
|
---|
489 | try
|
---|
490 | {
|
---|
491 | mFloppyList.clear ();
|
---|
492 | if (RT_SUCCESS(rc) && (!success || testing()))
|
---|
493 | rc = getDriveInfoFromEnv("VBOX_FLOPPY", &mFloppyList,
|
---|
494 | false /* isDVD */, &success);
|
---|
495 | setNoProbe(false);
|
---|
496 | if ( RT_SUCCESS(rc) && (!success || testing()))
|
---|
497 | rc = getDriveInfoFromSysfs(&mFloppyList, false /* isDVD */,
|
---|
498 | &success);
|
---|
499 | if (RT_SUCCESS(rc) && testing())
|
---|
500 | {
|
---|
501 | setNoProbe(true);
|
---|
502 | rc = getDriveInfoFromSysfs(&mFloppyList, false /* isDVD */, &success);
|
---|
503 | }
|
---|
504 | /* Walk through the /dev subtree if nothing else has helped. */
|
---|
505 | if ( RT_SUCCESS(rc) && (!success || testing()))
|
---|
506 | rc = getDriveInfoFromDev(&mFloppyList, false /* isDVD */,
|
---|
507 | &success);
|
---|
508 | }
|
---|
509 | catch(std::bad_alloc &e)
|
---|
510 | {
|
---|
511 | rc = VERR_NO_MEMORY;
|
---|
512 | }
|
---|
513 | LogFlowThisFunc(("rc=%Rrc\n", rc));
|
---|
514 | return rc;
|
---|
515 | }
|
---|
516 |
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Extract the names of drives from an environment variable and add them to a
|
---|
520 | * list if they are valid.
|
---|
521 | * @returns iprt status code
|
---|
522 | * @param pcszVar the name of the environment variable. The variable
|
---|
523 | * value should be a list of device node names, separated
|
---|
524 | * by ':' characters.
|
---|
525 | * @param pList the list to append the drives found to
|
---|
526 | * @param isDVD are we looking for DVD drives or for floppies?
|
---|
527 | * @param pfSuccess this will be set to true if we found at least one drive
|
---|
528 | * and to false otherwise. Optional.
|
---|
529 | */
|
---|
530 | /* static */
|
---|
531 | int getDriveInfoFromEnv(const char *pcszVar, DriveInfoList *pList,
|
---|
532 | bool isDVD, bool *pfSuccess)
|
---|
533 | {
|
---|
534 | AssertPtrReturn(pcszVar, VERR_INVALID_POINTER);
|
---|
535 | AssertPtrReturn(pList, VERR_INVALID_POINTER);
|
---|
536 | AssertPtrNullReturn(pfSuccess, VERR_INVALID_POINTER);
|
---|
537 | LogFlowFunc(("pcszVar=%s, pList=%p, isDVD=%d, pfSuccess=%p\n", pcszVar,
|
---|
538 | pList, isDVD, pfSuccess));
|
---|
539 | int rc = VINF_SUCCESS;
|
---|
540 | bool success = false;
|
---|
541 | char *pszFreeMe = RTEnvDupEx(RTENV_DEFAULT, pcszVar);
|
---|
542 |
|
---|
543 | try
|
---|
544 | {
|
---|
545 | const char *pcszCurrent = pszFreeMe;
|
---|
546 | while (pcszCurrent && *pcszCurrent != '\0')
|
---|
547 | {
|
---|
548 | const char *pcszNext = strchr(pcszCurrent, ':');
|
---|
549 | char szPath[RTPATH_MAX], szReal[RTPATH_MAX];
|
---|
550 | char szDesc[256], szUdi[256];
|
---|
551 | if (pcszNext)
|
---|
552 | RTStrPrintf(szPath, sizeof(szPath), "%.*s",
|
---|
553 | pcszNext - pcszCurrent - 1, pcszCurrent);
|
---|
554 | else
|
---|
555 | RTStrPrintf(szPath, sizeof(szPath), "%s", pcszCurrent);
|
---|
556 | if ( RT_SUCCESS(RTPathReal(szPath, szReal, sizeof(szReal)))
|
---|
557 | && devValidateDevice(szReal, isDVD, NULL, szDesc,
|
---|
558 | sizeof(szDesc), szUdi, sizeof(szUdi)))
|
---|
559 | {
|
---|
560 | pList->push_back(DriveInfo(szReal, szUdi, szDesc));
|
---|
561 | success = true;
|
---|
562 | }
|
---|
563 | pcszCurrent = pcszNext ? pcszNext + 1 : NULL;
|
---|
564 | }
|
---|
565 | if (pfSuccess != NULL)
|
---|
566 | *pfSuccess = success;
|
---|
567 | }
|
---|
568 | catch(std::bad_alloc &e)
|
---|
569 | {
|
---|
570 | rc = VERR_NO_MEMORY;
|
---|
571 | }
|
---|
572 | RTStrFree(pszFreeMe);
|
---|
573 | LogFlowFunc(("rc=%Rrc, success=%d\n", rc, success));
|
---|
574 | return rc;
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | class sysfsBlockDev
|
---|
579 | {
|
---|
580 | public:
|
---|
581 | sysfsBlockDev(const char *pcszName, bool wantDVD)
|
---|
582 | : mpcszName(pcszName), mwantDVD(wantDVD), misConsistent(true),
|
---|
583 | misValid(false)
|
---|
584 | {
|
---|
585 | if (findDeviceNode())
|
---|
586 | {
|
---|
587 | if (mwantDVD)
|
---|
588 | validateAndInitForDVD();
|
---|
589 | else
|
---|
590 | validateAndInitForFloppy();
|
---|
591 | }
|
---|
592 | }
|
---|
593 | private:
|
---|
594 | /** The name of the subdirectory of /sys/block for this device */
|
---|
595 | const char *mpcszName;
|
---|
596 | /** Are we looking for a floppy or a DVD device? */
|
---|
597 | bool mwantDVD;
|
---|
598 | /** The device node for the device */
|
---|
599 | char mszNode[RTPATH_MAX];
|
---|
600 | /** Does the sysfs entry look like we expect it too? This is a canary
|
---|
601 | * for future sysfs ABI changes. */
|
---|
602 | bool misConsistent;
|
---|
603 | /** Is this entry a valid specimen of what we are looking for? */
|
---|
604 | bool misValid;
|
---|
605 | /** Human readable drive description string */
|
---|
606 | char mszDesc[256];
|
---|
607 | /** Unique identifier for the drive. Should be identical to hal's UDI for
|
---|
608 | * the device. May not be unique for two identical drives. */
|
---|
609 | char mszUdi[256];
|
---|
610 | private:
|
---|
611 | /* Private methods */
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Fill in the device node member based on the /sys/block subdirectory.
|
---|
615 | * @returns boolean success value
|
---|
616 | */
|
---|
617 | bool findDeviceNode()
|
---|
618 | {
|
---|
619 | dev_t dev = RTLinuxSysFsReadDevNumFile("block/%s/dev", mpcszName);
|
---|
620 | if (dev == 0)
|
---|
621 | {
|
---|
622 | misConsistent = false;
|
---|
623 | return false;
|
---|
624 | }
|
---|
625 | if (RTLinuxFindDevicePath(dev, RTFS_TYPE_DEV_BLOCK, mszNode,
|
---|
626 | sizeof(mszNode), "%s", mpcszName) < 0)
|
---|
627 | return false;
|
---|
628 | return true;
|
---|
629 | }
|
---|
630 |
|
---|
631 | /** Check whether the sysfs block entry is valid for a DVD device and
|
---|
632 | * initialise the string data members for the object. We try to get all
|
---|
633 | * the information we need from sysfs if possible, to avoid unnecessarily
|
---|
634 | * poking the device, and if that fails we fall back to an SCSI INQUIRY
|
---|
635 | * command. */
|
---|
636 | void validateAndInitForDVD()
|
---|
637 | {
|
---|
638 | char szVendor[128], szModel[128];
|
---|
639 | ssize_t cchVendor, cchModel;
|
---|
640 | int64_t type = RTLinuxSysFsReadIntFile(10, "block/%s/device/type",
|
---|
641 | mpcszName);
|
---|
642 | if (type >= 0 && type != TYPE_ROM)
|
---|
643 | return;
|
---|
644 | if (type == TYPE_ROM)
|
---|
645 | {
|
---|
646 | cchVendor = RTLinuxSysFsReadStrFile(szVendor, sizeof(szVendor),
|
---|
647 | "block/%s/device/vendor",
|
---|
648 | mpcszName);
|
---|
649 | if (cchVendor >= 0)
|
---|
650 | {
|
---|
651 | cchModel = RTLinuxSysFsReadStrFile(szModel, sizeof(szModel),
|
---|
652 | "block/%s/device/model",
|
---|
653 | mpcszName);
|
---|
654 | if (cchModel >= 0)
|
---|
655 | {
|
---|
656 | misValid = true;
|
---|
657 | dvdCreateDeviceStrings(szVendor, szModel,
|
---|
658 | mszDesc, sizeof(mszDesc),
|
---|
659 | mszUdi, sizeof(mszUdi));
|
---|
660 | return;
|
---|
661 | }
|
---|
662 | }
|
---|
663 | }
|
---|
664 | if (!noProbe())
|
---|
665 | probeAndInitForDVD();
|
---|
666 | }
|
---|
667 |
|
---|
668 | /** Try to find out whether a device is a DVD drive by sending it an
|
---|
669 | * SCSI INQUIRY command. If it is, initialise the string and validity
|
---|
670 | * data members for the object based on the returned data.
|
---|
671 | */
|
---|
672 | void probeAndInitForDVD()
|
---|
673 | {
|
---|
674 | AssertReturnVoid(mszNode[0] != '\0');
|
---|
675 | uint8_t u8Type = 0;
|
---|
676 | char szVendor[128] = "";
|
---|
677 | char szModel[128] = "";
|
---|
678 | int rc = cdromDoInquiry(mszNode, &u8Type, szVendor,
|
---|
679 | sizeof(szVendor), szModel,
|
---|
680 | sizeof(szModel));
|
---|
681 | if (RT_SUCCESS(rc) && (u8Type == TYPE_ROM))
|
---|
682 | {
|
---|
683 | misValid = true;
|
---|
684 | dvdCreateDeviceStrings(szVendor, szModel, mszDesc, sizeof(mszDesc),
|
---|
685 | mszUdi, sizeof(mszUdi));
|
---|
686 | }
|
---|
687 | }
|
---|
688 |
|
---|
689 | /** Check whether the sysfs block entry is valid for a floppy device and
|
---|
690 | * initialise the string data members for the object. Since we only
|
---|
691 | * support floppies using the basic "floppy" driver, we check the driver
|
---|
692 | * using the entry name and a driver-specific ioctl. */
|
---|
693 | void validateAndInitForFloppy()
|
---|
694 | {
|
---|
695 | bool haveName = false;
|
---|
696 | floppy_drive_name szName;
|
---|
697 | char szDriver[8];
|
---|
698 | if ( mpcszName[0] != 'f'
|
---|
699 | || mpcszName[1] != 'd'
|
---|
700 | || mpcszName[2] < '0'
|
---|
701 | || mpcszName[2] > '7'
|
---|
702 | || mpcszName[3] != '\0')
|
---|
703 | return;
|
---|
704 | if (!noProbe())
|
---|
705 | haveName = floppyGetName(mszNode, mpcszName[2] - '0', szName);
|
---|
706 | if (RTLinuxSysFsGetLinkDest(szDriver, sizeof(szDriver), "block/%s/%s",
|
---|
707 | mpcszName, "device/driver") >= 0)
|
---|
708 | {
|
---|
709 | if (RTStrCmp(szDriver, "floppy"))
|
---|
710 | return;
|
---|
711 | }
|
---|
712 | else if (!haveName)
|
---|
713 | return;
|
---|
714 | floppyCreateDeviceStrings(haveName ? szName : NULL,
|
---|
715 | mpcszName[2] - '0', mszDesc,
|
---|
716 | sizeof(mszDesc), mszUdi, sizeof(mszUdi));
|
---|
717 | misValid = true;
|
---|
718 | }
|
---|
719 |
|
---|
720 | public:
|
---|
721 | bool isConsistent()
|
---|
722 | {
|
---|
723 | return misConsistent;
|
---|
724 | }
|
---|
725 | bool isValid()
|
---|
726 | {
|
---|
727 | return misValid;
|
---|
728 | }
|
---|
729 | const char *getDesc()
|
---|
730 | {
|
---|
731 | return mszDesc;
|
---|
732 | }
|
---|
733 | const char *getUdi()
|
---|
734 | {
|
---|
735 | return mszUdi;
|
---|
736 | }
|
---|
737 | const char *getNode()
|
---|
738 | {
|
---|
739 | return mszNode;
|
---|
740 | }
|
---|
741 | };
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * Helper function to query the sysfs subsystem for information about DVD
|
---|
745 | * drives attached to the system.
|
---|
746 | * @returns iprt status code
|
---|
747 | * @param pList where to add information about the drives detected
|
---|
748 | * @param isDVD are we looking for DVDs or floppies?
|
---|
749 | * @param pfSuccess Did we find anything?
|
---|
750 | *
|
---|
751 | * @returns IPRT status code
|
---|
752 | */
|
---|
753 | /* static */
|
---|
754 | int getDriveInfoFromSysfs(DriveInfoList *pList, bool isDVD, bool *pfSuccess)
|
---|
755 | {
|
---|
756 | AssertPtrReturn(pList, VERR_INVALID_POINTER);
|
---|
757 | AssertPtrNullReturn(pfSuccess, VERR_INVALID_POINTER); /* Valid or Null */
|
---|
758 | LogFlowFunc (("pList=%p, isDVD=%u, pfSuccess=%p\n",
|
---|
759 | pList, (unsigned) isDVD, pfSuccess));
|
---|
760 | PRTDIR pDir = NULL;
|
---|
761 | int rc;
|
---|
762 | bool fSuccess = false;
|
---|
763 | unsigned cFound = 0;
|
---|
764 |
|
---|
765 | if (!RTPathExists("/sys"))
|
---|
766 | return VINF_SUCCESS;
|
---|
767 | rc = RTDirOpen(&pDir, "/sys/block");
|
---|
768 | /* This might mean that sysfs semantics have changed */
|
---|
769 | AssertReturn(rc != VERR_FILE_NOT_FOUND, VINF_SUCCESS);
|
---|
770 | fSuccess = true;
|
---|
771 | if (RT_SUCCESS(rc))
|
---|
772 | for (;;)
|
---|
773 | {
|
---|
774 | RTDIRENTRY entry;
|
---|
775 | rc = RTDirRead(pDir, &entry, NULL);
|
---|
776 | Assert(rc != VERR_BUFFER_OVERFLOW); /* Should never happen... */
|
---|
777 | if (RT_FAILURE(rc)) /* Including overflow and no more files */
|
---|
778 | break;
|
---|
779 | if (entry.szName[0] == '.')
|
---|
780 | continue;
|
---|
781 | sysfsBlockDev dev(entry.szName, isDVD);
|
---|
782 | /* This might mean that sysfs semantics have changed */
|
---|
783 | AssertBreakStmt(dev.isConsistent(), fSuccess = false);
|
---|
784 | if (!dev.isValid())
|
---|
785 | continue;
|
---|
786 | try
|
---|
787 | {
|
---|
788 | pList->push_back(DriveInfo(dev.getNode(), dev.getUdi(),
|
---|
789 | dev.getDesc()));
|
---|
790 | }
|
---|
791 | catch(std::bad_alloc &e)
|
---|
792 | {
|
---|
793 | rc = VERR_NO_MEMORY;
|
---|
794 | break;
|
---|
795 | }
|
---|
796 | ++cFound;
|
---|
797 | }
|
---|
798 | RTDirClose(pDir);
|
---|
799 | if (rc == VERR_NO_MORE_FILES)
|
---|
800 | rc = VINF_SUCCESS;
|
---|
801 | if (RT_FAILURE(rc))
|
---|
802 | /* Clean up again */
|
---|
803 | for (unsigned i = 0; i < cFound; ++i)
|
---|
804 | pList->pop_back();
|
---|
805 | if (pfSuccess)
|
---|
806 | *pfSuccess = fSuccess;
|
---|
807 | LogFlow (("rc=%Rrc, fSuccess=%u\n", rc, (unsigned) fSuccess));
|
---|
808 | return rc;
|
---|
809 | }
|
---|
810 |
|
---|
811 |
|
---|
812 | /** Structure for holding information about a drive we have found */
|
---|
813 | struct deviceNodeInfo
|
---|
814 | {
|
---|
815 | /** The device number */
|
---|
816 | dev_t Device;
|
---|
817 | /** The device node path */
|
---|
818 | char szPath[RTPATH_MAX];
|
---|
819 | /** The device description */
|
---|
820 | char szDesc[256];
|
---|
821 | /** The device UDI */
|
---|
822 | char szUdi[256];
|
---|
823 | };
|
---|
824 |
|
---|
825 | /** The maximum number of devices we will search for. */
|
---|
826 | enum { MAX_DEVICE_NODES = 8 };
|
---|
827 | /** An array of MAX_DEVICE_NODES devices */
|
---|
828 | typedef struct deviceNodeInfo deviceNodeArray[MAX_DEVICE_NODES];
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * Recursive worker function to walk the /dev tree looking for DVD or floppy
|
---|
832 | * devices.
|
---|
833 | * @returns true if we have already found MAX_DEVICE_NODES devices, false
|
---|
834 | * otherwise
|
---|
835 | * @param pszPath the path to start recursing. The function can modify
|
---|
836 | * this string at and after the terminating zero
|
---|
837 | * @param cchPath the size of the buffer (not the string!) in @a pszPath
|
---|
838 | * @param aDevices where to fill in information about devices that we have
|
---|
839 | * found
|
---|
840 | * @param wantDVD are we looking for DVD devices (or floppies)?
|
---|
841 | */
|
---|
842 | static bool devFindDeviceRecursive(char *pszPath, size_t cchPath,
|
---|
843 | deviceNodeArray aDevices, bool wantDVD)
|
---|
844 | {
|
---|
845 | /*
|
---|
846 | * Check assumptions made by the code below.
|
---|
847 | */
|
---|
848 | size_t const cchBasePath = strlen(pszPath);
|
---|
849 | AssertReturn(cchBasePath < RTPATH_MAX - 10U, false);
|
---|
850 | AssertReturn(pszPath[cchBasePath - 1] != '/', false);
|
---|
851 |
|
---|
852 | PRTDIR pDir;
|
---|
853 | if (RT_FAILURE(RTDirOpen(&pDir, pszPath)))
|
---|
854 | return false;
|
---|
855 | for (;;)
|
---|
856 | {
|
---|
857 | RTDIRENTRY Entry;
|
---|
858 | RTFSOBJINFO ObjInfo;
|
---|
859 | int rc = RTDirRead(pDir, &Entry, NULL);
|
---|
860 | if (RT_FAILURE(rc))
|
---|
861 | break;
|
---|
862 | if (Entry.enmType == RTDIRENTRYTYPE_UNKNOWN)
|
---|
863 | {
|
---|
864 | if (RT_FAILURE(RTPathQueryInfo(pszPath, &ObjInfo,
|
---|
865 | RTFSOBJATTRADD_UNIX)))
|
---|
866 | continue;
|
---|
867 | if (RTFS_IS_SYMLINK(ObjInfo.Attr.fMode))
|
---|
868 | continue;
|
---|
869 | }
|
---|
870 |
|
---|
871 | if (Entry.enmType == RTDIRENTRYTYPE_SYMLINK)
|
---|
872 | continue;
|
---|
873 | pszPath[cchBasePath] = '\0';
|
---|
874 | if (RT_FAILURE(RTPathAppend(pszPath, cchPath, Entry.szName)))
|
---|
875 | break;
|
---|
876 |
|
---|
877 | /* Do the matching. */
|
---|
878 | dev_t DevNode;
|
---|
879 | char szDesc[256], szUdi[256];
|
---|
880 | if (!devValidateDevice(pszPath, wantDVD, &DevNode, szDesc,
|
---|
881 | sizeof(szDesc), szUdi, sizeof(szUdi)))
|
---|
882 | continue;
|
---|
883 | unsigned i;
|
---|
884 | for (i = 0; i < MAX_DEVICE_NODES; ++i)
|
---|
885 | if (!aDevices[i].Device || (aDevices[i].Device == DevNode))
|
---|
886 | break;
|
---|
887 | AssertBreak(i < MAX_DEVICE_NODES);
|
---|
888 | if (aDevices[i].Device)
|
---|
889 | continue;
|
---|
890 | aDevices[i].Device = DevNode;
|
---|
891 | RTStrPrintf(aDevices[i].szPath, sizeof(aDevices[i].szPath),
|
---|
892 | "%s", pszPath);
|
---|
893 | AssertCompile(sizeof(aDevices[i].szDesc) == sizeof(szDesc));
|
---|
894 | strcpy(aDevices[i].szDesc, szDesc);
|
---|
895 | AssertCompile(sizeof(aDevices[i].szUdi) == sizeof(szUdi));
|
---|
896 | strcpy(aDevices[i].szUdi, szUdi);
|
---|
897 | if (i == MAX_DEVICE_NODES - 1)
|
---|
898 | break;
|
---|
899 | continue;
|
---|
900 |
|
---|
901 | /* Recurse into subdirectories. */
|
---|
902 | if ( (Entry.enmType == RTDIRENTRYTYPE_UNKNOWN)
|
---|
903 | && !RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode))
|
---|
904 | continue;
|
---|
905 | if (Entry.enmType != RTDIRENTRYTYPE_DIRECTORY)
|
---|
906 | continue;
|
---|
907 | if (Entry.szName[0] == '.')
|
---|
908 | continue;
|
---|
909 |
|
---|
910 | if (devFindDeviceRecursive(pszPath, cchPath, aDevices, wantDVD))
|
---|
911 | break;
|
---|
912 | }
|
---|
913 | RTDirClose(pDir);
|
---|
914 | return aDevices[MAX_DEVICE_NODES - 1].Device ? true : false;
|
---|
915 | }
|
---|
916 |
|
---|
917 |
|
---|
918 | /**
|
---|
919 | * Recursively walk through the /dev tree and add any DVD or floppy drives we
|
---|
920 | * find and can access to our list. (If we can't access them we can't check
|
---|
921 | * whether or not they are really DVD or floppy drives).
|
---|
922 | * @note this is rather slow (a couple of seconds) for DVD probing on
|
---|
923 | * systems with a static /dev tree, as the current code tries to open
|
---|
924 | * any device node with a major/minor combination that could belong to
|
---|
925 | * a CD-ROM device, and opening a non-existent device can take a non.
|
---|
926 | * negligible time on Linux. If it is ever necessary to improve this
|
---|
927 | * (static /dev trees are no longer very fashionable these days, and
|
---|
928 | * sysfs looks like it will be with us for a while), we could further
|
---|
929 | * reduce the number of device nodes we open by checking whether the
|
---|
930 | * driver is actually loaded in /proc/devices, and by counting the
|
---|
931 | * of currently attached SCSI CD-ROM devices in /proc/scsi/scsi (yes,
|
---|
932 | * there is a race, but it is probably not important for us).
|
---|
933 | * @returns iprt status code
|
---|
934 | * @param pList the list to append the drives found to
|
---|
935 | * @param isDVD are we looking for DVD drives or for floppies?
|
---|
936 | * @param pfSuccess this will be set to true if we found at least one drive
|
---|
937 | * and to false otherwise. Optional.
|
---|
938 | */
|
---|
939 | /* static */
|
---|
940 | int getDriveInfoFromDev(DriveInfoList *pList, bool isDVD, bool *pfSuccess)
|
---|
941 | {
|
---|
942 | AssertPtrReturn(pList, VERR_INVALID_POINTER);
|
---|
943 | AssertPtrNullReturn(pfSuccess, VERR_INVALID_POINTER);
|
---|
944 | LogFlowFunc(("pList=%p, isDVD=%d, pfSuccess=%p\n", pList, isDVD,
|
---|
945 | pfSuccess));
|
---|
946 | int rc = VINF_SUCCESS;
|
---|
947 | bool success = false;
|
---|
948 |
|
---|
949 | char szPath[RTPATH_MAX] = "/dev";
|
---|
950 | deviceNodeArray aDevices;
|
---|
951 | RT_ZERO(aDevices);
|
---|
952 | devFindDeviceRecursive(szPath, sizeof(szPath), aDevices, isDVD);
|
---|
953 | try
|
---|
954 | {
|
---|
955 | for (unsigned i = 0; i < MAX_DEVICE_NODES; ++i)
|
---|
956 | {
|
---|
957 | if (aDevices[i].Device)
|
---|
958 | {
|
---|
959 | pList->push_back(DriveInfo(aDevices[i].szPath,
|
---|
960 | aDevices[i].szUdi, aDevices[i].szDesc));
|
---|
961 | success = true;
|
---|
962 | }
|
---|
963 | }
|
---|
964 | if (pfSuccess != NULL)
|
---|
965 | *pfSuccess = success;
|
---|
966 | }
|
---|
967 | catch(std::bad_alloc &e)
|
---|
968 | {
|
---|
969 | rc = VERR_NO_MEMORY;
|
---|
970 | }
|
---|
971 | LogFlowFunc (("rc=%Rrc, success=%d\n", rc, success));
|
---|
972 | return rc;
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | /** Helper for readFilePathsFromDir(). Adds a path to the vector if it is not
|
---|
977 | * NULL and not a dotfile (".", "..", ".*"). */
|
---|
978 | static int maybeAddPathToVector(const char *pcszPath, const char *pcszEntry,
|
---|
979 | VECTOR_PTR(char *) *pvecpchDevs)
|
---|
980 | {
|
---|
981 | char *pszPath;
|
---|
982 |
|
---|
983 | if (!pcszPath)
|
---|
984 | return 0;
|
---|
985 | if (pcszEntry[0] == '.')
|
---|
986 | return 0;
|
---|
987 | pszPath = RTStrDup(pcszPath);
|
---|
988 | if (!pszPath)
|
---|
989 | return ENOMEM;
|
---|
990 | if (RT_FAILURE(VEC_PUSH_BACK_PTR(pvecpchDevs, char *, pszPath)))
|
---|
991 | return ENOMEM;
|
---|
992 | return 0;
|
---|
993 | }
|
---|
994 |
|
---|
995 | /** Helper for readFilePaths(). Adds the entries from the open directory
|
---|
996 | * @a pDir to the vector @a pvecpchDevs using either the full path or the
|
---|
997 | * realpath() and skipping hidden files and files on which realpath() fails. */
|
---|
998 | static int readFilePathsFromDir(const char *pcszPath, DIR *pDir,
|
---|
999 | VECTOR_PTR(char *) *pvecpchDevs, int withRealPath)
|
---|
1000 | {
|
---|
1001 | struct dirent entry, *pResult;
|
---|
1002 | int err;
|
---|
1003 |
|
---|
1004 | for (err = readdir_r(pDir, &entry, &pResult); pResult;
|
---|
1005 | err = readdir_r(pDir, &entry, &pResult))
|
---|
1006 | {
|
---|
1007 | /* We (implicitly) require that PATH_MAX be defined */
|
---|
1008 | char szPath[PATH_MAX + 1], szRealPath[PATH_MAX + 1], *pszPath;
|
---|
1009 | if (snprintf(szPath, sizeof(szPath), "%s/%s", pcszPath,
|
---|
1010 | entry.d_name) < 0)
|
---|
1011 | return errno;
|
---|
1012 | if (withRealPath)
|
---|
1013 | pszPath = realpath(szPath, szRealPath);
|
---|
1014 | else
|
---|
1015 | pszPath = szPath;
|
---|
1016 | if ((err = maybeAddPathToVector(pszPath, entry.d_name, pvecpchDevs)))
|
---|
1017 | return err;
|
---|
1018 | }
|
---|
1019 | return err;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 |
|
---|
1023 | /**
|
---|
1024 | * Helper for walkDirectory to dump the names of a directory's entries into a
|
---|
1025 | * vector of char pointers.
|
---|
1026 | *
|
---|
1027 | * @returns zero on success or (positive) posix error value.
|
---|
1028 | * @param pcszPath the path to dump.
|
---|
1029 | * @param pvecpchDevs an empty vector of char pointers - must be cleaned up
|
---|
1030 | * by the caller even on failure.
|
---|
1031 | * @param withRealPath whether to canonicalise the filename with realpath
|
---|
1032 | */
|
---|
1033 | static int readFilePaths(const char *pcszPath, VECTOR_PTR(char *) *pvecpchDevs,
|
---|
1034 | int withRealPath)
|
---|
1035 | {
|
---|
1036 | DIR *pDir;
|
---|
1037 | int err;
|
---|
1038 |
|
---|
1039 | AssertPtrReturn(pvecpchDevs, EINVAL);
|
---|
1040 | AssertReturn(VEC_SIZE_PTR(pvecpchDevs) == 0, EINVAL);
|
---|
1041 | AssertPtrReturn(pcszPath, EINVAL);
|
---|
1042 |
|
---|
1043 | pDir = opendir(pcszPath);
|
---|
1044 | if (!pDir)
|
---|
1045 | return RTErrConvertFromErrno(errno);
|
---|
1046 | err = readFilePathsFromDir(pcszPath, pDir, pvecpchDevs, withRealPath);
|
---|
1047 | if (closedir(pDir) < 0 && !err)
|
---|
1048 | err = errno;
|
---|
1049 | return RTErrConvertFromErrno(err);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 |
|
---|
1053 | class hotplugNullImpl : public VBoxMainHotplugWaiterImpl
|
---|
1054 | {
|
---|
1055 | public:
|
---|
1056 | hotplugNullImpl (void) {}
|
---|
1057 | virtual ~hotplugNullImpl (void) {}
|
---|
1058 | /** @copydoc VBoxMainHotplugWaiter::Wait */
|
---|
1059 | virtual int Wait (RTMSINTERVAL)
|
---|
1060 | {
|
---|
1061 | return VERR_NOT_SUPPORTED;
|
---|
1062 | }
|
---|
1063 | /** @copydoc VBoxMainHotplugWaiter::Interrupt */
|
---|
1064 | virtual void Interrupt (void) {}
|
---|
1065 | virtual int getStatus(void)
|
---|
1066 | {
|
---|
1067 | return VERR_NOT_SUPPORTED;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | };
|
---|
1071 |
|
---|
1072 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
1073 | # ifdef VBOX_USB_WITH_INOTIFY
|
---|
1074 | /** Class wrapper around an inotify watch (or a group of them to be precise).
|
---|
1075 | */
|
---|
1076 | typedef struct inotifyWatch
|
---|
1077 | {
|
---|
1078 | /** Pointer to the inotify_add_watch() glibc function/Linux API */
|
---|
1079 | int (*inotify_add_watch)(int, const char *, uint32_t);
|
---|
1080 | /** The native handle of the inotify fd. */
|
---|
1081 | int mhInotify;
|
---|
1082 | } inotifyWatch;
|
---|
1083 |
|
---|
1084 | /** The flags we pass to inotify - modify, create, delete, change permissions
|
---|
1085 | */
|
---|
1086 | #define IN_FLAGS 0x306
|
---|
1087 |
|
---|
1088 | static int iwAddWatch(inotifyWatch *pSelf, const char *pcszPath)
|
---|
1089 | {
|
---|
1090 | errno = 0;
|
---|
1091 | if ( pSelf->inotify_add_watch(pSelf->mhInotify, pcszPath, IN_FLAGS) >= 0
|
---|
1092 | || (errno == EACCES))
|
---|
1093 | return VINF_SUCCESS;
|
---|
1094 | /* Other errors listed in the manpage can be treated as fatal */
|
---|
1095 | return RTErrConvertFromErrno(errno);
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /** Object initialisation */
|
---|
1099 | static int iwInit(inotifyWatch *pSelf)
|
---|
1100 | {
|
---|
1101 | int (*inotify_init)(void);
|
---|
1102 | int fd, flags;
|
---|
1103 | int rc = VINF_SUCCESS;
|
---|
1104 |
|
---|
1105 | AssertPtr(pSelf);
|
---|
1106 | pSelf->mhInotify = -1;
|
---|
1107 | errno = 0;
|
---|
1108 | *(void **)(&inotify_init) = dlsym(RTLD_DEFAULT, "inotify_init");
|
---|
1109 | if (!inotify_init)
|
---|
1110 | return VERR_LDR_IMPORTED_SYMBOL_NOT_FOUND;
|
---|
1111 | *(void **)(&pSelf->inotify_add_watch)
|
---|
1112 | = dlsym(RTLD_DEFAULT, "inotify_add_watch");
|
---|
1113 | if (!pSelf->inotify_add_watch)
|
---|
1114 | return VERR_LDR_IMPORTED_SYMBOL_NOT_FOUND;
|
---|
1115 | fd = inotify_init();
|
---|
1116 | if (fd < 0)
|
---|
1117 | {
|
---|
1118 | Assert(errno > 0);
|
---|
1119 | return RTErrConvertFromErrno(errno);
|
---|
1120 | }
|
---|
1121 | Assert(errno == 0);
|
---|
1122 |
|
---|
1123 | flags = fcntl(fd, F_GETFL, NULL);
|
---|
1124 | if ( flags < 0
|
---|
1125 | || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0
|
---|
1126 | || fcntl(fd, F_SETFD, FD_CLOEXEC) < 0 /* race here */)
|
---|
1127 | {
|
---|
1128 | Assert(errno > 0);
|
---|
1129 | rc = RTErrConvertFromErrno(errno);
|
---|
1130 | }
|
---|
1131 | if (RT_FAILURE(rc))
|
---|
1132 | close(fd);
|
---|
1133 | else
|
---|
1134 | {
|
---|
1135 | Assert(errno == 0);
|
---|
1136 | pSelf->mhInotify = fd;
|
---|
1137 | }
|
---|
1138 | return rc;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | static void iwTerm(inotifyWatch *pSelf)
|
---|
1142 | {
|
---|
1143 | AssertPtrReturnVoid(pSelf);
|
---|
1144 | if (pSelf->mhInotify != -1)
|
---|
1145 | {
|
---|
1146 | close(pSelf->mhInotify);
|
---|
1147 | pSelf->mhInotify = -1;
|
---|
1148 | }
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | static int iwGetFD(inotifyWatch *pSelf)
|
---|
1152 | {
|
---|
1153 | AssertPtrReturn(pSelf, -1);
|
---|
1154 | return pSelf->mhInotify;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | # define SYSFS_USB_DEVICE_PATH "/dev/bus/usb"
|
---|
1158 | # define SYSFS_WAKEUP_STRING "Wake up!"
|
---|
1159 |
|
---|
1160 | class hotplugInotifyImpl : public VBoxMainHotplugWaiterImpl
|
---|
1161 | {
|
---|
1162 | /** Pipe used to interrupt wait(), the read end. */
|
---|
1163 | int mhWakeupPipeR;
|
---|
1164 | /** Pipe used to interrupt wait(), the write end. */
|
---|
1165 | int mhWakeupPipeW;
|
---|
1166 | /** The inotify watch set */
|
---|
1167 | inotifyWatch mWatches;
|
---|
1168 | /** Flag to mark that the Wait() method is currently being called, and to
|
---|
1169 | * ensure that it isn't called multiple times in parallel. */
|
---|
1170 | volatile uint32_t mfWaiting;
|
---|
1171 | /** iprt result code from object initialisation. Should be AssertReturn-ed
|
---|
1172 | * on at the start of all methods. I went this way because I didn't want
|
---|
1173 | * to deal with exceptions. */
|
---|
1174 | int mStatus;
|
---|
1175 | /** ID values associates with the wakeup pipe and the FAM socket for polling
|
---|
1176 | */
|
---|
1177 | enum
|
---|
1178 | {
|
---|
1179 | RPIPE_ID = 0,
|
---|
1180 | INOTIFY_ID,
|
---|
1181 | MAX_POLLID
|
---|
1182 | };
|
---|
1183 |
|
---|
1184 | /** Clean up any resources in use, gracefully skipping over any which have
|
---|
1185 | * not yet been allocated or already cleaned up. Intended to be called
|
---|
1186 | * from the destructor or after a failed initialisation. */
|
---|
1187 | void term(void);
|
---|
1188 |
|
---|
1189 | int drainInotify();
|
---|
1190 |
|
---|
1191 | /** Read the wakeup string from the wakeup pipe */
|
---|
1192 | int drainWakeupPipe(void);
|
---|
1193 | public:
|
---|
1194 | hotplugInotifyImpl(void);
|
---|
1195 | virtual ~hotplugInotifyImpl(void)
|
---|
1196 | {
|
---|
1197 | term();
|
---|
1198 | #ifdef DEBUG
|
---|
1199 | /** The first call to term should mark all resources as freed, so this
|
---|
1200 | * should be a semantic no-op. */
|
---|
1201 | term();
|
---|
1202 | #endif
|
---|
1203 | }
|
---|
1204 | /** Are sysfs and inotify available on this system? If so we expect that
|
---|
1205 | * this implementation will be usable. */
|
---|
1206 | static bool Available(void)
|
---|
1207 | {
|
---|
1208 | return ( RTDirExists(SYSFS_USB_DEVICE_PATH)
|
---|
1209 | && dlsym(RTLD_DEFAULT, "inotify_init") != NULL);
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | virtual int getStatus(void)
|
---|
1213 | {
|
---|
1214 | return mStatus;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | /** @copydoc VBoxMainHotplugWaiter::Wait */
|
---|
1218 | virtual int Wait(RTMSINTERVAL);
|
---|
1219 | /** @copydoc VBoxMainHotplugWaiter::Interrupt */
|
---|
1220 | virtual void Interrupt(void);
|
---|
1221 | };
|
---|
1222 |
|
---|
1223 | /** Simplified version of RTPipeCreate */
|
---|
1224 | static int pipeCreateSimple(int *phPipeRead, int *phPipeWrite)
|
---|
1225 | {
|
---|
1226 | AssertPtrReturn(phPipeRead, VERR_INVALID_POINTER);
|
---|
1227 | AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
|
---|
1228 |
|
---|
1229 | /*
|
---|
1230 | * Create the pipe and set the close-on-exec flag.
|
---|
1231 | */
|
---|
1232 | int aFds[2] = {-1, -1};
|
---|
1233 | if (pipe(aFds))
|
---|
1234 | return RTErrConvertFromErrno(errno);
|
---|
1235 | if ( fcntl(aFds[0], F_SETFD, FD_CLOEXEC) < 0
|
---|
1236 | || fcntl(aFds[1], F_SETFD, FD_CLOEXEC) < 0)
|
---|
1237 | {
|
---|
1238 | int rc = RTErrConvertFromErrno(errno);
|
---|
1239 | close(aFds[0]);
|
---|
1240 | close(aFds[1]);
|
---|
1241 | return rc;
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | *phPipeRead = aFds[0];
|
---|
1245 | *phPipeWrite = aFds[1];
|
---|
1246 |
|
---|
1247 | /*
|
---|
1248 | * Before we leave, make sure to shut up SIGPIPE.
|
---|
1249 | */
|
---|
1250 | signal(SIGPIPE, SIG_IGN);
|
---|
1251 | return VINF_SUCCESS;
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | hotplugInotifyImpl::hotplugInotifyImpl(void) :
|
---|
1255 | mhWakeupPipeR(-1), mhWakeupPipeW(-1), mfWaiting(0),
|
---|
1256 | mStatus(VERR_WRONG_ORDER)
|
---|
1257 | {
|
---|
1258 | # ifdef DEBUG
|
---|
1259 | /* Excercise the code path (term() on a not-fully-initialised object) as
|
---|
1260 | * well as we can. On an uninitialised object this method is a semantic
|
---|
1261 | * no-op. */
|
---|
1262 | mWatches.mhInotify = -1; /* term will access this variable */
|
---|
1263 | term();
|
---|
1264 | /* For now this probing method should only be used if nothing else is
|
---|
1265 | * available */
|
---|
1266 | # endif
|
---|
1267 | int rc;
|
---|
1268 | do {
|
---|
1269 | if (RT_FAILURE(rc = iwInit(&mWatches)))
|
---|
1270 | break;
|
---|
1271 | if (RT_FAILURE(rc = iwAddWatch(&mWatches, SYSFS_USB_DEVICE_PATH)))
|
---|
1272 | break;
|
---|
1273 | if (RT_FAILURE(rc = pipeCreateSimple(&mhWakeupPipeR, &mhWakeupPipeW)))
|
---|
1274 | break;
|
---|
1275 | } while(0);
|
---|
1276 | mStatus = rc;
|
---|
1277 | if (RT_FAILURE(rc))
|
---|
1278 | term();
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | void hotplugInotifyImpl::term(void)
|
---|
1282 | {
|
---|
1283 | /** This would probably be a pending segfault, so die cleanly */
|
---|
1284 | AssertRelease(!mfWaiting);
|
---|
1285 | if (mhWakeupPipeR != -1)
|
---|
1286 | {
|
---|
1287 | close(mhWakeupPipeR);
|
---|
1288 | mhWakeupPipeR = -1;
|
---|
1289 | }
|
---|
1290 | if (mhWakeupPipeW != -1)
|
---|
1291 | {
|
---|
1292 | close(mhWakeupPipeW);
|
---|
1293 | mhWakeupPipeW = -1;
|
---|
1294 | }
|
---|
1295 | iwTerm(&mWatches);
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | int hotplugInotifyImpl::drainInotify()
|
---|
1299 | {
|
---|
1300 | char chBuf[RTPATH_MAX + 256]; /* Should always be big enough */
|
---|
1301 | ssize_t cchRead;
|
---|
1302 |
|
---|
1303 | AssertRCReturn(mStatus, VERR_WRONG_ORDER);
|
---|
1304 | errno = 0;
|
---|
1305 | do {
|
---|
1306 | cchRead = read(iwGetFD(&mWatches), chBuf, sizeof(chBuf));
|
---|
1307 | } while (cchRead > 0);
|
---|
1308 | if (cchRead == 0)
|
---|
1309 | return VINF_SUCCESS;
|
---|
1310 | if (cchRead < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
---|
1311 | return VINF_SUCCESS;
|
---|
1312 | Assert(errno > 0);
|
---|
1313 | return RTErrConvertFromErrno(errno);
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | int hotplugInotifyImpl::drainWakeupPipe(void)
|
---|
1317 | {
|
---|
1318 | char szBuf[sizeof(SYSFS_WAKEUP_STRING)];
|
---|
1319 | ssize_t cbRead;
|
---|
1320 |
|
---|
1321 | AssertRCReturn(mStatus, VERR_WRONG_ORDER);
|
---|
1322 | cbRead = read(mhWakeupPipeR, szBuf, sizeof(szBuf));
|
---|
1323 | Assert(cbRead > 0);
|
---|
1324 | return VINF_SUCCESS;
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | int hotplugInotifyImpl::Wait(RTMSINTERVAL aMillies)
|
---|
1328 | {
|
---|
1329 | int rc;
|
---|
1330 | char **ppszEntry;
|
---|
1331 | VECTOR_PTR(char *) vecpchDevs;
|
---|
1332 |
|
---|
1333 | AssertRCReturn(mStatus, VERR_WRONG_ORDER);
|
---|
1334 | bool fEntered = ASMAtomicCmpXchgU32(&mfWaiting, 1, 0);
|
---|
1335 | AssertReturn(fEntered, VERR_WRONG_ORDER);
|
---|
1336 | VEC_INIT_PTR(&vecpchDevs, char *, RTStrFree);
|
---|
1337 | do {
|
---|
1338 | struct pollfd pollFD[MAX_POLLID];
|
---|
1339 |
|
---|
1340 | rc = readFilePaths(SYSFS_USB_DEVICE_PATH, &vecpchDevs, false);
|
---|
1341 | if (RT_SUCCESS(rc))
|
---|
1342 | VEC_FOR_EACH(&vecpchDevs, char *, ppszEntry)
|
---|
1343 | if (RT_FAILURE(rc = iwAddWatch(&mWatches, *ppszEntry)))
|
---|
1344 | break;
|
---|
1345 | if (RT_FAILURE(rc))
|
---|
1346 | break;
|
---|
1347 | pollFD[RPIPE_ID].fd = mhWakeupPipeR;
|
---|
1348 | pollFD[RPIPE_ID].events = POLLIN;
|
---|
1349 | pollFD[INOTIFY_ID].fd = iwGetFD(&mWatches);
|
---|
1350 | pollFD[INOTIFY_ID].events = POLLIN | POLLERR | POLLHUP;
|
---|
1351 | errno = 0;
|
---|
1352 | int cPolled = poll(pollFD, RT_ELEMENTS(pollFD), aMillies);
|
---|
1353 | if (cPolled < 0)
|
---|
1354 | {
|
---|
1355 | Assert(errno > 0);
|
---|
1356 | rc = RTErrConvertFromErrno(errno);
|
---|
1357 | }
|
---|
1358 | else if (pollFD[RPIPE_ID].revents)
|
---|
1359 | {
|
---|
1360 | rc = drainWakeupPipe();
|
---|
1361 | if (RT_SUCCESS(rc))
|
---|
1362 | rc = VERR_INTERRUPTED;
|
---|
1363 | break;
|
---|
1364 | }
|
---|
1365 | else if (!(pollFD[INOTIFY_ID].revents))
|
---|
1366 | {
|
---|
1367 | AssertBreakStmt(cPolled == 0, rc = VERR_INTERNAL_ERROR);
|
---|
1368 | rc = VERR_TIMEOUT;
|
---|
1369 | }
|
---|
1370 | Assert(errno == 0 || (RT_FAILURE(rc) && rc != VERR_TIMEOUT));
|
---|
1371 | if (RT_FAILURE(rc))
|
---|
1372 | break;
|
---|
1373 | AssertBreakStmt(cPolled == 1, rc = VERR_INTERNAL_ERROR);
|
---|
1374 | if (RT_FAILURE(rc = drainInotify()))
|
---|
1375 | break;
|
---|
1376 | } while (false);
|
---|
1377 | mfWaiting = 0;
|
---|
1378 | VEC_CLEANUP_PTR(&vecpchDevs);
|
---|
1379 | return rc;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | void hotplugInotifyImpl::Interrupt(void)
|
---|
1383 | {
|
---|
1384 | AssertRCReturnVoid(mStatus);
|
---|
1385 | ssize_t cbWritten = write(mhWakeupPipeW, SYSFS_WAKEUP_STRING,
|
---|
1386 | sizeof(SYSFS_WAKEUP_STRING));
|
---|
1387 | if (cbWritten > 0)
|
---|
1388 | fsync(mhWakeupPipeW);
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | # endif /* VBOX_USB_WITH_INOTIFY */
|
---|
1392 | #endif /* VBOX_USB_WTH_SYSFS */
|
---|
1393 |
|
---|
1394 | VBoxMainHotplugWaiter::VBoxMainHotplugWaiter(void)
|
---|
1395 | {
|
---|
1396 | try
|
---|
1397 | {
|
---|
1398 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
1399 | # ifdef VBOX_USB_WITH_INOTIFY
|
---|
1400 | if (hotplugInotifyImpl::Available())
|
---|
1401 | {
|
---|
1402 | mImpl = new hotplugInotifyImpl;
|
---|
1403 | return;
|
---|
1404 | }
|
---|
1405 | # endif /* VBOX_USB_WITH_INOTIFY */
|
---|
1406 | #endif /* VBOX_USB_WITH_SYSFS */
|
---|
1407 | mImpl = new hotplugNullImpl;
|
---|
1408 | }
|
---|
1409 | catch(std::bad_alloc &e)
|
---|
1410 | { }
|
---|
1411 | }
|
---|