1 | /* $Id: USBProxyBackendLinux.cpp 60067 2016-03-16 19:17:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service, Linux Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2012 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/dir.h>
|
---|
35 | #include <iprt/env.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/param.h>
|
---|
40 | #include <iprt/path.h>
|
---|
41 | #include <iprt/pipe.h>
|
---|
42 | #include <iprt/stream.h>
|
---|
43 | #include <iprt/linux/sysfs.h>
|
---|
44 |
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <string.h>
|
---|
47 | #include <stdio.h>
|
---|
48 | #include <errno.h>
|
---|
49 | #include <fcntl.h>
|
---|
50 | #include <unistd.h>
|
---|
51 | #include <sys/statfs.h>
|
---|
52 | #include <sys/poll.h>
|
---|
53 | #ifdef VBOX_WITH_LINUX_COMPILER_H
|
---|
54 | # include <linux/compiler.h>
|
---|
55 | #endif
|
---|
56 | #include <linux/usbdevice_fs.h>
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Initialize data members.
|
---|
61 | */
|
---|
62 | USBProxyBackendLinux::USBProxyBackendLinux(USBProxyService *aUsbProxyService, const com::Utf8Str &strId)
|
---|
63 | : USBProxyBackend(aUsbProxyService, strId), mhFile(NIL_RTFILE), mhWakeupPipeR(NIL_RTPIPE),
|
---|
64 | mhWakeupPipeW(NIL_RTPIPE), mUsingUsbfsDevices(true /* see init */),
|
---|
65 | mUdevPolls(0), mpWaiter(NULL)
|
---|
66 | {
|
---|
67 | LogFlowThisFunc(("aUsbProxyService=%p\n", aUsbProxyService));
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Initializes the object (called right after construction).
|
---|
72 | *
|
---|
73 | * @returns VBox status code.
|
---|
74 | */
|
---|
75 | int USBProxyBackendLinux::init(const com::Utf8Str &strAddress)
|
---|
76 | {
|
---|
77 | NOREF(strAddress);
|
---|
78 |
|
---|
79 | const char *pcszDevicesRoot;
|
---|
80 | int rc = USBProxyLinuxChooseMethod(&mUsingUsbfsDevices, &pcszDevicesRoot);
|
---|
81 | if (RT_SUCCESS(rc))
|
---|
82 | {
|
---|
83 | mDevicesRoot = pcszDevicesRoot;
|
---|
84 | rc = mUsingUsbfsDevices ? initUsbfs() : initSysfs();
|
---|
85 | /* For the day when we have VBoxSVC release logging... */
|
---|
86 | LogRel((RT_SUCCESS(rc) ? "Successfully initialised host USB using %s\n"
|
---|
87 | : "Failed to initialise host USB using %s\n",
|
---|
88 | mUsingUsbfsDevices ? "USBFS" : "sysfs"));
|
---|
89 | }
|
---|
90 |
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Initialization routine for the usbfs based operation.
|
---|
96 | *
|
---|
97 | * @returns iprt status code.
|
---|
98 | */
|
---|
99 | int USBProxyBackendLinux::initUsbfs(void)
|
---|
100 | {
|
---|
101 | Assert(mUsingUsbfsDevices);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Open the devices file.
|
---|
105 | */
|
---|
106 | int rc;
|
---|
107 | char *pszDevices = RTPathJoinA(mDevicesRoot.c_str(), "devices");
|
---|
108 | if (pszDevices)
|
---|
109 | {
|
---|
110 | rc = RTFileOpen(&mhFile, pszDevices, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | rc = RTPipeCreate(&mhWakeupPipeR, &mhWakeupPipeW, 0 /*fFlags*/);
|
---|
114 | if (RT_SUCCESS(rc))
|
---|
115 | {
|
---|
116 | /*
|
---|
117 | * Start the poller thread.
|
---|
118 | */
|
---|
119 | rc = start();
|
---|
120 | if (RT_SUCCESS(rc))
|
---|
121 | {
|
---|
122 | RTStrFree(pszDevices);
|
---|
123 | LogFlowThisFunc(("returns successfully\n"));
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|
127 | RTPipeClose(mhWakeupPipeR);
|
---|
128 | RTPipeClose(mhWakeupPipeW);
|
---|
129 | mhWakeupPipeW = mhWakeupPipeR = NIL_RTPIPE;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | Log(("USBProxyBackendLinux::USBProxyBackendLinux: RTFilePipe failed with rc=%Rrc\n", rc));
|
---|
133 | RTFileClose(mhFile);
|
---|
134 | }
|
---|
135 |
|
---|
136 | RTStrFree(pszDevices);
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | rc = VERR_NO_MEMORY;
|
---|
141 | Log(("USBProxyBackendLinux::USBProxyBackendLinux: out of memory!\n"));
|
---|
142 | }
|
---|
143 |
|
---|
144 | LogFlowThisFunc(("returns failure!!! (rc=%Rrc)\n", rc));
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Initialization routine for the sysfs based operation.
|
---|
151 | *
|
---|
152 | * @returns iprt status code
|
---|
153 | */
|
---|
154 | int USBProxyBackendLinux::initSysfs(void)
|
---|
155 | {
|
---|
156 | Assert(!mUsingUsbfsDevices);
|
---|
157 |
|
---|
158 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
159 | try
|
---|
160 | {
|
---|
161 | mpWaiter = new VBoxMainHotplugWaiter(mDevicesRoot.c_str());
|
---|
162 | }
|
---|
163 | catch(std::bad_alloc &e)
|
---|
164 | {
|
---|
165 | return VERR_NO_MEMORY;
|
---|
166 | }
|
---|
167 | int rc = mpWaiter->getStatus();
|
---|
168 | if (RT_SUCCESS(rc) || rc == VERR_TIMEOUT || rc == VERR_TRY_AGAIN)
|
---|
169 | rc = start();
|
---|
170 | else if (rc == VERR_NOT_SUPPORTED)
|
---|
171 | /* This can legitimately happen if hal or DBus are not running, but of
|
---|
172 | * course we can't start in this case. */
|
---|
173 | rc = VINF_SUCCESS;
|
---|
174 | return rc;
|
---|
175 |
|
---|
176 | #else /* !VBOX_USB_WITH_SYSFS */
|
---|
177 | return VERR_NOT_IMPLEMENTED;
|
---|
178 | #endif /* !VBOX_USB_WITH_SYSFS */
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Stop all service threads and free the device chain.
|
---|
184 | */
|
---|
185 | USBProxyBackendLinux::~USBProxyBackendLinux()
|
---|
186 | {
|
---|
187 | LogFlowThisFunc(("\n"));
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Stop the service.
|
---|
191 | */
|
---|
192 | if (isActive())
|
---|
193 | stop();
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Free resources.
|
---|
197 | */
|
---|
198 | doUsbfsCleanupAsNeeded();
|
---|
199 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
200 | if (mpWaiter)
|
---|
201 | delete mpWaiter;
|
---|
202 | #endif
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * If any Usbfs-related resources are currently allocated, then free them
|
---|
208 | * and mark them as freed.
|
---|
209 | */
|
---|
210 | void USBProxyBackendLinux::doUsbfsCleanupAsNeeded()
|
---|
211 | {
|
---|
212 | /*
|
---|
213 | * Free resources.
|
---|
214 | */
|
---|
215 | RTFileClose(mhFile);
|
---|
216 | mhFile = NIL_RTFILE;
|
---|
217 |
|
---|
218 | RTPipeClose(mhWakeupPipeR);
|
---|
219 | RTPipeClose(mhWakeupPipeW);
|
---|
220 | mhWakeupPipeW = mhWakeupPipeR = NIL_RTPIPE;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | int USBProxyBackendLinux::captureDevice(HostUSBDevice *aDevice)
|
---|
225 | {
|
---|
226 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
227 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
228 |
|
---|
229 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
230 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * Don't think we need to do anything when the device is held... fake it.
|
---|
234 | */
|
---|
235 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
|
---|
236 | devLock.release();
|
---|
237 | interruptWait();
|
---|
238 |
|
---|
239 | return VINF_SUCCESS;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | int USBProxyBackendLinux::releaseDevice(HostUSBDevice *aDevice)
|
---|
244 | {
|
---|
245 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
246 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
247 |
|
---|
248 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
249 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * We're not really holding it atm., just fake it.
|
---|
253 | */
|
---|
254 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
|
---|
255 | devLock.release();
|
---|
256 | interruptWait();
|
---|
257 |
|
---|
258 | return VINF_SUCCESS;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | bool USBProxyBackendLinux::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters,
|
---|
263 | SessionMachine **aIgnoreMachine)
|
---|
264 | {
|
---|
265 | AssertReturn(aDevice, false);
|
---|
266 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), false);
|
---|
267 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
268 | if ( aUSBDevice->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE
|
---|
269 | && aDevice->i_getUsbData()->enmState == USBDEVICESTATE_USED_BY_HOST)
|
---|
270 | LogRel(("USBProxy: Device %04x:%04x (%s) has become accessible.\n",
|
---|
271 | aUSBDevice->idVendor, aUSBDevice->idProduct, aUSBDevice->pszAddress));
|
---|
272 | devLock.release();
|
---|
273 | return updateDeviceStateFake(aDevice, aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * A device was added, we need to adjust mUdevPolls.
|
---|
279 | *
|
---|
280 | * See USBProxyService::deviceAdded for details.
|
---|
281 | */
|
---|
282 | void USBProxyBackendLinux::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines,
|
---|
283 | PUSBDEVICE aUSBDevice)
|
---|
284 | {
|
---|
285 | AssertReturnVoid(aDevice);
|
---|
286 | AssertReturnVoid(!aDevice->isWriteLockOnCurrentThread());
|
---|
287 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
288 | if (aUSBDevice->enmState == USBDEVICESTATE_USED_BY_HOST)
|
---|
289 | {
|
---|
290 | LogRel(("USBProxy: Device %04x:%04x (%s) isn't accessible. giving udev a few seconds to fix this...\n",
|
---|
291 | aUSBDevice->idVendor, aUSBDevice->idProduct, aUSBDevice->pszAddress));
|
---|
292 | mUdevPolls = 10; /* (10 * 500ms = 5s) */
|
---|
293 | }
|
---|
294 |
|
---|
295 | devLock.release();
|
---|
296 | USBProxyBackend::deviceAdded(aDevice, llOpenedMachines, aUSBDevice);
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | int USBProxyBackendLinux::wait(RTMSINTERVAL aMillies)
|
---|
301 | {
|
---|
302 | int rc;
|
---|
303 | if (mUsingUsbfsDevices)
|
---|
304 | rc = waitUsbfs(aMillies);
|
---|
305 | else
|
---|
306 | rc = waitSysfs(aMillies);
|
---|
307 | return rc;
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | /** String written to the wakeup pipe. */
|
---|
312 | #define WAKE_UP_STRING "WakeUp!"
|
---|
313 | /** Length of the string written. */
|
---|
314 | #define WAKE_UP_STRING_LEN ( sizeof(WAKE_UP_STRING) - 1 )
|
---|
315 |
|
---|
316 | int USBProxyBackendLinux::waitUsbfs(RTMSINTERVAL aMillies)
|
---|
317 | {
|
---|
318 | struct pollfd PollFds[2];
|
---|
319 |
|
---|
320 | /* Cap the wait interval if we're polling for udevd changing device permissions. */
|
---|
321 | if (aMillies > 500 && mUdevPolls > 0)
|
---|
322 | {
|
---|
323 | mUdevPolls--;
|
---|
324 | aMillies = 500;
|
---|
325 | }
|
---|
326 |
|
---|
327 | RT_ZERO(PollFds);
|
---|
328 | PollFds[0].fd = RTFileToNative(mhFile);
|
---|
329 | PollFds[0].events = POLLIN;
|
---|
330 | PollFds[1].fd = RTPipeToNative(mhWakeupPipeR);
|
---|
331 | PollFds[1].events = POLLIN | POLLERR | POLLHUP;
|
---|
332 |
|
---|
333 | int rc = poll(&PollFds[0], 2, aMillies);
|
---|
334 | if (rc == 0)
|
---|
335 | return VERR_TIMEOUT;
|
---|
336 | if (rc > 0)
|
---|
337 | {
|
---|
338 | /* drain the pipe */
|
---|
339 | if (PollFds[1].revents & POLLIN)
|
---|
340 | {
|
---|
341 | char szBuf[WAKE_UP_STRING_LEN];
|
---|
342 | rc = RTPipeReadBlocking(mhWakeupPipeR, szBuf, sizeof(szBuf), NULL);
|
---|
343 | AssertRC(rc);
|
---|
344 | }
|
---|
345 | return VINF_SUCCESS;
|
---|
346 | }
|
---|
347 | return RTErrConvertFromErrno(errno);
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | int USBProxyBackendLinux::waitSysfs(RTMSINTERVAL aMillies)
|
---|
352 | {
|
---|
353 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
354 | int rc = mpWaiter->Wait(aMillies);
|
---|
355 | if (rc == VERR_TRY_AGAIN)
|
---|
356 | {
|
---|
357 | RTThreadYield();
|
---|
358 | rc = VINF_SUCCESS;
|
---|
359 | }
|
---|
360 | return rc;
|
---|
361 | #else /* !VBOX_USB_WITH_SYSFS */
|
---|
362 | return USBProxyService::wait(aMillies);
|
---|
363 | #endif /* !VBOX_USB_WITH_SYSFS */
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | int USBProxyBackendLinux::interruptWait(void)
|
---|
368 | {
|
---|
369 | AssertReturn(!isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
370 |
|
---|
371 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
372 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
373 | LogFlowFunc(("mUsingUsbfsDevices=%d\n", mUsingUsbfsDevices));
|
---|
374 | if (!mUsingUsbfsDevices)
|
---|
375 | {
|
---|
376 | mpWaiter->Interrupt();
|
---|
377 | LogFlowFunc(("Returning VINF_SUCCESS\n"));
|
---|
378 | return VINF_SUCCESS;
|
---|
379 | }
|
---|
380 | #endif /* VBOX_USB_WITH_SYSFS */
|
---|
381 | int rc = RTPipeWriteBlocking(mhWakeupPipeW, WAKE_UP_STRING, WAKE_UP_STRING_LEN, NULL);
|
---|
382 | if (RT_SUCCESS(rc))
|
---|
383 | RTPipeFlush(mhWakeupPipeW);
|
---|
384 | LogFlowFunc(("returning %Rrc\n", rc));
|
---|
385 | return rc;
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | PUSBDEVICE USBProxyBackendLinux::getDevices(void)
|
---|
390 | {
|
---|
391 | return USBProxyLinuxGetDevices(mDevicesRoot.c_str(), !mUsingUsbfsDevices);
|
---|
392 | }
|
---|