1 | /* $Id: USBProxyServiceLinux.cpp 36418 2011-03-24 22:53:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service, Linux Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include "USBProxyService.h"
|
---|
23 | #include "USBGetDevices.h"
|
---|
24 | #include "Logging.h"
|
---|
25 |
|
---|
26 | #include <VBox/usb.h>
|
---|
27 | #include <VBox/usblib.h>
|
---|
28 | #include <VBox/err.h>
|
---|
29 |
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/alloc.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/ctype.h>
|
---|
34 | #include <iprt/env.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/param.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 | #include <iprt/linux/sysfs.h>
|
---|
42 |
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <errno.h>
|
---|
47 | #include <fcntl.h>
|
---|
48 | #include <unistd.h>
|
---|
49 | #include <sys/statfs.h>
|
---|
50 | #include <sys/poll.h>
|
---|
51 | #ifdef VBOX_WITH_LINUX_COMPILER_H
|
---|
52 | # include <linux/compiler.h>
|
---|
53 | #endif
|
---|
54 | #include <linux/usbdevice_fs.h>
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Initialize data members.
|
---|
59 | */
|
---|
60 | USBProxyServiceLinux::USBProxyServiceLinux(Host *aHost)
|
---|
61 | : USBProxyService(aHost), mFile(NIL_RTFILE), mWakeupPipeR(NIL_RTFILE),
|
---|
62 | mWakeupPipeW(NIL_RTFILE), mUsingUsbfsDevices(true /* see init */),
|
---|
63 | mUdevPolls(0), mpWaiter(NULL)
|
---|
64 | {
|
---|
65 | LogFlowThisFunc(("aHost=%p:{%s}\n", aHost));
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Initializes the object (called right after construction).
|
---|
71 | *
|
---|
72 | * @returns S_OK on success and non-fatal failures, some COM error otherwise.
|
---|
73 | */
|
---|
74 | HRESULT USBProxyServiceLinux::init(void)
|
---|
75 | {
|
---|
76 | /*
|
---|
77 | * Call the superclass method first.
|
---|
78 | */
|
---|
79 | HRESULT hrc = USBProxyService::init();
|
---|
80 | AssertComRCReturn(hrc, hrc);
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * We have two methods available for getting host USB device data - using
|
---|
84 | * USBFS and using sysfs/hal. The default choice depends on build-time
|
---|
85 | * settings and an environment variable; if the default is not available
|
---|
86 | * we fall back to the second.
|
---|
87 | * In the event of both failing, the error from the second method tried
|
---|
88 | * will be presented to the user.
|
---|
89 | */
|
---|
90 | #ifdef VBOX_WITH_SYSFS_BY_DEFAULT
|
---|
91 | bool fUseSysfs = true;
|
---|
92 | #else
|
---|
93 | bool fUseSysfs = false;
|
---|
94 | #endif
|
---|
95 | const char *pcszUsbFromEnv = RTEnvGet("VBOX_USB");
|
---|
96 | const char *pcszUsbRoot = NULL;
|
---|
97 | if (pcszUsbFromEnv)
|
---|
98 | {
|
---|
99 | bool fValidVBoxUSB = true;
|
---|
100 |
|
---|
101 | pcszUsbRoot = RTEnvGet("VBOX_USB_ROOT");
|
---|
102 | if (!RTStrICmp(pcszUsbFromEnv, "USBFS"))
|
---|
103 | {
|
---|
104 | LogRel(("Default USB access method set to \"usbfs\" from environment\n"));
|
---|
105 | fUseSysfs = false;
|
---|
106 | }
|
---|
107 | else if (!RTStrICmp(pcszUsbFromEnv, "SYSFS"))
|
---|
108 | {
|
---|
109 | LogRel(("Default USB method set to \"sysfs\" from environment\n"));
|
---|
110 | fUseSysfs = true;
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | LogRel(("Invalid VBOX_USB environment variable setting \"%s\"\n",
|
---|
115 | pcszUsbFromEnv));
|
---|
116 | fValidVBoxUSB = false;
|
---|
117 | }
|
---|
118 | if (!fValidVBoxUSB && pcszUsbRoot)
|
---|
119 | pcszUsbRoot = NULL;
|
---|
120 | }
|
---|
121 | if (!pcszUsbRoot)
|
---|
122 | {
|
---|
123 | PCUSBDEVTREELOCATION pcLocation;
|
---|
124 | pcLocation = USBProxyLinuxGetDeviceRoot(fUseSysfs);
|
---|
125 | pcszUsbRoot = pcLocation->szDevicesRoot;
|
---|
126 | fUseSysfs = pcLocation->fUseSysfs;
|
---|
127 | }
|
---|
128 | if (pcszUsbRoot)
|
---|
129 | {
|
---|
130 | mUsingUsbfsDevices = !fUseSysfs;
|
---|
131 | mDevicesRoot = pcszUsbRoot;
|
---|
132 | int rc = mUsingUsbfsDevices ? initUsbfs() : initSysfs();
|
---|
133 | /* For the day when we have VBoxSVC release logging... */
|
---|
134 | LogRel((RT_SUCCESS(rc) ? "Successfully initialised host USB using %s\n"
|
---|
135 | : "Failed to initialise host USB using %s\n",
|
---|
136 | mUsingUsbfsDevices ? "USBFS" : "sysfs"));
|
---|
137 | mLastError = rc;
|
---|
138 | }
|
---|
139 | else
|
---|
140 | mLastError = VERR_NOT_FOUND;
|
---|
141 | return S_OK;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Initialization routine for the usbfs based operation.
|
---|
147 | *
|
---|
148 | * @returns iprt status code.
|
---|
149 | */
|
---|
150 | int USBProxyServiceLinux::initUsbfs(void)
|
---|
151 | {
|
---|
152 | Assert(mUsingUsbfsDevices);
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * Open the devices file.
|
---|
156 | */
|
---|
157 | int rc;
|
---|
158 | char *pszDevices = RTPathJoinA(mDevicesRoot.c_str(), "devices");
|
---|
159 | if (pszDevices)
|
---|
160 | {
|
---|
161 | rc = RTFileOpen(&mFile, pszDevices, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | int pipes[2];
|
---|
165 | if (!pipe(pipes))
|
---|
166 | {
|
---|
167 | /* Set close on exec (race here!) */
|
---|
168 | if ( fcntl(pipes[0], F_SETFD, FD_CLOEXEC) >= 0
|
---|
169 | && fcntl(pipes[1], F_SETFD, FD_CLOEXEC) >= 0)
|
---|
170 | {
|
---|
171 | mWakeupPipeR = pipes[0];
|
---|
172 | mWakeupPipeW = pipes[1];
|
---|
173 | /*
|
---|
174 | * Start the poller thread.
|
---|
175 | */
|
---|
176 | rc = start();
|
---|
177 | if (RT_SUCCESS(rc))
|
---|
178 | {
|
---|
179 | RTStrFree(pszDevices);
|
---|
180 | LogFlowThisFunc(("returns successfully - mWakeupPipeR/W=%d/%d\n",
|
---|
181 | mWakeupPipeR, mWakeupPipeW));
|
---|
182 | return VINF_SUCCESS;
|
---|
183 | }
|
---|
184 |
|
---|
185 | RTFileClose(mWakeupPipeR);
|
---|
186 | RTFileClose(mWakeupPipeW);
|
---|
187 | mWakeupPipeW = mWakeupPipeR = NIL_RTFILE;
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | rc = RTErrConvertFromErrno(errno);
|
---|
192 | Log(("USBProxyServiceLinux::USBProxyServiceLinux: fcntl failed, errno=%d\n", errno));
|
---|
193 | close(pipes[0]);
|
---|
194 | close(pipes[1]);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | rc = RTErrConvertFromErrno(errno);
|
---|
200 | Log(("USBProxyServiceLinux::USBProxyServiceLinux: pipe failed, errno=%d\n", errno));
|
---|
201 | }
|
---|
202 | RTFileClose(mFile);
|
---|
203 | }
|
---|
204 |
|
---|
205 | RTStrFree(pszDevices);
|
---|
206 | }
|
---|
207 | else
|
---|
208 | {
|
---|
209 | rc = VERR_NO_MEMORY;
|
---|
210 | Log(("USBProxyServiceLinux::USBProxyServiceLinux: out of memory!\n"));
|
---|
211 | }
|
---|
212 |
|
---|
213 | LogFlowThisFunc(("returns failure!!! (rc=%Rrc)\n", rc));
|
---|
214 | return rc;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Initialization routine for the sysfs based operation.
|
---|
220 | *
|
---|
221 | * @returns iprt status code
|
---|
222 | */
|
---|
223 | int USBProxyServiceLinux::initSysfs(void)
|
---|
224 | {
|
---|
225 | Assert(!mUsingUsbfsDevices);
|
---|
226 |
|
---|
227 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
228 | try
|
---|
229 | {
|
---|
230 | mpWaiter = new VBoxMainHotplugWaiter(mDevicesRoot.c_str());
|
---|
231 | }
|
---|
232 | catch(std::bad_alloc &e)
|
---|
233 | {
|
---|
234 | return VERR_NO_MEMORY;
|
---|
235 | }
|
---|
236 | int rc = mpWaiter->getStatus();
|
---|
237 | if (RT_SUCCESS(rc) || rc == VERR_TIMEOUT || rc == VERR_TRY_AGAIN)
|
---|
238 | rc = start();
|
---|
239 | else if (rc == VERR_NOT_SUPPORTED)
|
---|
240 | /* This can legitimately happen if hal or DBus are not running, but of
|
---|
241 | * course we can't start in this case. */
|
---|
242 | rc = VINF_SUCCESS;
|
---|
243 | return rc;
|
---|
244 |
|
---|
245 | #else /* !VBOX_USB_WITH_SYSFS */
|
---|
246 | return VERR_NOT_IMPLEMENTED;
|
---|
247 | #endif /* !VBOX_USB_WITH_SYSFS */
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Stop all service threads and free the device chain.
|
---|
253 | */
|
---|
254 | USBProxyServiceLinux::~USBProxyServiceLinux()
|
---|
255 | {
|
---|
256 | LogFlowThisFunc(("\n"));
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * Stop the service.
|
---|
260 | */
|
---|
261 | if (isActive())
|
---|
262 | stop();
|
---|
263 |
|
---|
264 | /*
|
---|
265 | * Free resources.
|
---|
266 | */
|
---|
267 | doUsbfsCleanupAsNeeded();
|
---|
268 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
269 | if (mpWaiter)
|
---|
270 | delete mpWaiter;
|
---|
271 | #endif
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * If any Usbfs-related resources are currently allocated, then free them
|
---|
277 | * and mark them as freed.
|
---|
278 | */
|
---|
279 | void USBProxyServiceLinux::doUsbfsCleanupAsNeeded()
|
---|
280 | {
|
---|
281 | /*
|
---|
282 | * Free resources.
|
---|
283 | */
|
---|
284 | if (mFile != NIL_RTFILE)
|
---|
285 | {
|
---|
286 | RTFileClose(mFile);
|
---|
287 | mFile = NIL_RTFILE;
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (mWakeupPipeR != NIL_RTFILE)
|
---|
291 | RTFileClose(mWakeupPipeR);
|
---|
292 | if (mWakeupPipeW != NIL_RTFILE)
|
---|
293 | RTFileClose(mWakeupPipeW);
|
---|
294 | mWakeupPipeW = mWakeupPipeR = NIL_RTFILE;
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | int USBProxyServiceLinux::captureDevice(HostUSBDevice *aDevice)
|
---|
299 | {
|
---|
300 | Log(("USBProxyServiceLinux::captureDevice: %p {%s}\n", aDevice, aDevice->getName().c_str()));
|
---|
301 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
302 | AssertReturn(aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Don't think we need to do anything when the device is held... fake it.
|
---|
306 | */
|
---|
307 | Assert(aDevice->getUnistate() == kHostUSBDeviceState_Capturing);
|
---|
308 | interruptWait();
|
---|
309 |
|
---|
310 | return VINF_SUCCESS;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | int USBProxyServiceLinux::releaseDevice(HostUSBDevice *aDevice)
|
---|
315 | {
|
---|
316 | Log(("USBProxyServiceLinux::releaseDevice: %p\n", aDevice));
|
---|
317 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
318 | AssertReturn(aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * We're not really holding it atm., just fake it.
|
---|
322 | */
|
---|
323 | Assert(aDevice->getUnistate() == kHostUSBDeviceState_ReleasingToHost);
|
---|
324 | interruptWait();
|
---|
325 |
|
---|
326 | return VINF_SUCCESS;
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | bool USBProxyServiceLinux::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
|
---|
331 | {
|
---|
332 | if ( aUSBDevice->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE
|
---|
333 | && aDevice->mUsb->enmState == USBDEVICESTATE_USED_BY_HOST)
|
---|
334 | LogRel(("USBProxy: Device %04x:%04x (%s) has become accessible.\n",
|
---|
335 | aUSBDevice->idVendor, aUSBDevice->idProduct, aUSBDevice->pszAddress));
|
---|
336 | return updateDeviceStateFake(aDevice, aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * A device was added, we need to adjust mUdevPolls.
|
---|
342 | *
|
---|
343 | * See USBProxyService::deviceAdded for details.
|
---|
344 | */
|
---|
345 | void USBProxyServiceLinux::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice)
|
---|
346 | {
|
---|
347 | if (aUSBDevice->enmState == USBDEVICESTATE_USED_BY_HOST)
|
---|
348 | {
|
---|
349 | LogRel(("USBProxy: Device %04x:%04x (%s) isn't accessible. giving udev a few seconds to fix this...\n",
|
---|
350 | aUSBDevice->idVendor, aUSBDevice->idProduct, aUSBDevice->pszAddress));
|
---|
351 | mUdevPolls = 10; /* (10 * 500ms = 5s) */
|
---|
352 | }
|
---|
353 |
|
---|
354 | USBProxyService::deviceAdded(aDevice, llOpenedMachines, aUSBDevice);
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | int USBProxyServiceLinux::wait(RTMSINTERVAL aMillies)
|
---|
359 | {
|
---|
360 | int rc;
|
---|
361 | if (mUsingUsbfsDevices)
|
---|
362 | rc = waitUsbfs(aMillies);
|
---|
363 | else
|
---|
364 | rc = waitSysfs(aMillies);
|
---|
365 | return rc;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /** String written to the wakeup pipe. */
|
---|
370 | #define WAKE_UP_STRING "WakeUp!"
|
---|
371 | /** Length of the string written. */
|
---|
372 | #define WAKE_UP_STRING_LEN ( sizeof(WAKE_UP_STRING) - 1 )
|
---|
373 |
|
---|
374 | int USBProxyServiceLinux::waitUsbfs(RTMSINTERVAL aMillies)
|
---|
375 | {
|
---|
376 | struct pollfd PollFds[2];
|
---|
377 |
|
---|
378 | /* Cap the wait interval if we're polling for udevd changing device permissions. */
|
---|
379 | if (aMillies > 500 && mUdevPolls > 0)
|
---|
380 | {
|
---|
381 | mUdevPolls--;
|
---|
382 | aMillies = 500;
|
---|
383 | }
|
---|
384 |
|
---|
385 | memset(&PollFds, 0, sizeof(PollFds));
|
---|
386 | PollFds[0].fd = mFile;
|
---|
387 | PollFds[0].events = POLLIN;
|
---|
388 | PollFds[1].fd = mWakeupPipeR;
|
---|
389 | PollFds[1].events = POLLIN | POLLERR | POLLHUP;
|
---|
390 |
|
---|
391 | int rc = poll(&PollFds[0], 2, aMillies);
|
---|
392 | if (rc == 0)
|
---|
393 | return VERR_TIMEOUT;
|
---|
394 | if (rc > 0)
|
---|
395 | {
|
---|
396 | /* drain the pipe */
|
---|
397 | if (PollFds[1].revents & POLLIN)
|
---|
398 | {
|
---|
399 | char szBuf[WAKE_UP_STRING_LEN];
|
---|
400 | rc = RTFileRead(mWakeupPipeR, szBuf, sizeof(szBuf), NULL);
|
---|
401 | AssertRC(rc);
|
---|
402 | }
|
---|
403 | return VINF_SUCCESS;
|
---|
404 | }
|
---|
405 | return RTErrConvertFromErrno(errno);
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | int USBProxyServiceLinux::waitSysfs(RTMSINTERVAL aMillies)
|
---|
410 | {
|
---|
411 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
412 | int rc = mpWaiter->Wait(aMillies);
|
---|
413 | if (rc == VERR_TRY_AGAIN)
|
---|
414 | {
|
---|
415 | RTThreadYield();
|
---|
416 | rc = VINF_SUCCESS;
|
---|
417 | }
|
---|
418 | return rc;
|
---|
419 | #else /* !VBOX_USB_WITH_SYSFS */
|
---|
420 | return USBProxyService::wait(aMillies);
|
---|
421 | #endif /* !VBOX_USB_WITH_SYSFS */
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | int USBProxyServiceLinux::interruptWait(void)
|
---|
426 | {
|
---|
427 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
428 | LogFlowFunc(("mUsingUsbfsDevices=%d\n", mUsingUsbfsDevices));
|
---|
429 | if (!mUsingUsbfsDevices)
|
---|
430 | {
|
---|
431 | mpWaiter->Interrupt();
|
---|
432 | LogFlowFunc(("Returning VINF_SUCCESS\n"));
|
---|
433 | return VINF_SUCCESS;
|
---|
434 | }
|
---|
435 | #endif /* VBOX_USB_WITH_SYSFS */
|
---|
436 | int rc = RTFileWrite(mWakeupPipeW, WAKE_UP_STRING, WAKE_UP_STRING_LEN, NULL);
|
---|
437 | if (RT_SUCCESS(rc))
|
---|
438 | RTFileFlush(mWakeupPipeW);
|
---|
439 | LogFlowFunc(("returning %Rrc\n", rc));
|
---|
440 | return rc;
|
---|
441 | }
|
---|
442 |
|
---|
443 |
|
---|
444 | PUSBDEVICE USBProxyServiceLinux::getDevices(void)
|
---|
445 | {
|
---|
446 | return USBProxyLinuxGetDevices(mDevicesRoot.c_str(), !mUsingUsbfsDevices);
|
---|
447 | }
|
---|