1 | /* $Id: VBoxSharedClipboardSvc.cpp 86912 2020-11-19 07:54:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard Service - Host service entry points.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 | /** @page pg_hostclip The Shared Clipboard Host Service
|
---|
20 | *
|
---|
21 | * The shared clipboard host service is the host half of the clibpoard proxying
|
---|
22 | * between the host and the guest. The guest parts live in VBoxClient, VBoxTray
|
---|
23 | * and VBoxService depending on the OS, with code shared between host and guest
|
---|
24 | * under src/VBox/GuestHost/SharedClipboard/.
|
---|
25 | *
|
---|
26 | * The service is split into a platform-independent core and platform-specific
|
---|
27 | * backends. The service defines two communication protocols - one to
|
---|
28 | * communicate with the clipboard service running on the guest, and one to
|
---|
29 | * communicate with the backend. These will be described in a very skeletal
|
---|
30 | * fashion here.
|
---|
31 | *
|
---|
32 | * r=bird: The "two communication protocols" does not seems to be factual, there
|
---|
33 | * is only one protocol, the first one mentioned. It cannot be backend
|
---|
34 | * specific, because the guest/host protocol is platform and backend agnostic in
|
---|
35 | * nature. You may call it versions, but I take a great dislike to "protocol
|
---|
36 | * versions" here, as you've just extended the existing protocol with a feature
|
---|
37 | * that allows to transfer files and directories too. See @bugref{9437#c39}.
|
---|
38 | *
|
---|
39 | *
|
---|
40 | * @section sec_hostclip_guest_proto The guest communication protocol
|
---|
41 | *
|
---|
42 | * The guest clipboard service communicates with the host service over HGCM
|
---|
43 | * (the host is a HGCM service). HGCM is connection based, so the guest side
|
---|
44 | * has to connect before anything else can be done. (Windows hosts currently
|
---|
45 | * only support one simultaneous connection.) Once it has connected, it can
|
---|
46 | * send messages to the host services, some of which will receive immediate
|
---|
47 | * replies from the host, others which will block till a reply becomes
|
---|
48 | * available. The latter is because HGCM does't allow the host to initiate
|
---|
49 | * communication, it must be guest triggered. The HGCM service is single
|
---|
50 | * threaded, so it doesn't matter if the guest tries to send lots of requests in
|
---|
51 | * parallel, the service will process them one at the time.
|
---|
52 | *
|
---|
53 | * There are currently four messages defined. The first is
|
---|
54 | * VBOX_SHCL_GUEST_FN_MSG_GET / VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT, which waits
|
---|
55 | * for a message from the host. If a host message is sent while the guest is
|
---|
56 | * not waiting, it will be queued until the guest requests it. The host code
|
---|
57 | * only supports a single simultaneous GET call from one client guest.
|
---|
58 | *
|
---|
59 | * The second guest message is VBOX_SHCL_GUEST_FN_REPORT_FORMATS, which tells
|
---|
60 | * the host that the guest has new clipboard data available. The third is
|
---|
61 | * VBOX_SHCL_GUEST_FN_DATA_READ, which asks the host to send its clipboard data
|
---|
62 | * and waits until it arrives. The host supports at most one simultaneous
|
---|
63 | * VBOX_SHCL_GUEST_FN_DATA_READ call from a guest - if a second call is made
|
---|
64 | * before the first has returned, the first will be aborted.
|
---|
65 | *
|
---|
66 | * The last guest message is VBOX_SHCL_GUEST_FN_DATA_WRITE, which is used to
|
---|
67 | * send the contents of the guest clipboard to the host. This call should be
|
---|
68 | * used after the host has requested data from the guest.
|
---|
69 | *
|
---|
70 | *
|
---|
71 | * @section sec_hostclip_backend_proto The communication protocol with the
|
---|
72 | * platform-specific backend
|
---|
73 | *
|
---|
74 | * The initial protocol implementation (called protocol v0) was very simple,
|
---|
75 | * and could only handle simple data (like copied text and so on). It also
|
---|
76 | * was limited to two (2) fixed parameters at all times.
|
---|
77 | *
|
---|
78 | * Since VBox 6.1 a newer protocol (v1) has been established to also support
|
---|
79 | * file transfers. This protocol uses a (per-client) message queue instead
|
---|
80 | * (see VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT vs. VBOX_SHCL_GUEST_FN_GET_HOST_MSG).
|
---|
81 | *
|
---|
82 | * To distinguish the old (legacy) or new(er) protocol, the VBOX_SHCL_GUEST_FN_CONNECT
|
---|
83 | * message has been introduced. If an older guest does not send this message,
|
---|
84 | * an appropriate translation will be done to serve older Guest Additions (< 6.1).
|
---|
85 | *
|
---|
86 | * The protocol also support out-of-order messages by using so-called "context IDs",
|
---|
87 | * which are generated by the host. A context ID consists of a so-called "source event ID"
|
---|
88 | * and a so-called "event ID". Each HGCM client has an own, random, source event ID and
|
---|
89 | * generates non-deterministic event IDs so that the guest side does not known what
|
---|
90 | * comes next; the guest side has to reply with the same conext ID which was sent by
|
---|
91 | * the host request.
|
---|
92 | *
|
---|
93 | * Also see the protocol changelog at VBoxShClSvc.h.
|
---|
94 | *
|
---|
95 | *
|
---|
96 | * @section sec_uri_intro Transferring files
|
---|
97 | *
|
---|
98 | * Since VBox x.x.x transferring files via Shared Clipboard is supported.
|
---|
99 | * See the VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS define for supported / enabled
|
---|
100 | * platforms. This is called "Shared Clipboard transfers".
|
---|
101 | *
|
---|
102 | * Copying files / directories from guest A to guest B requires the host
|
---|
103 | * service to act as a proxy and cache, as we don't allow direct VM-to-VM
|
---|
104 | * communication. Copying from / to the host also is taken into account.
|
---|
105 | *
|
---|
106 | * At the moment a transfer is a all-or-nothing operation, e.g. it either
|
---|
107 | * completes or fails completely. There might be callbacks in the future
|
---|
108 | * to e.g. skip failing entries.
|
---|
109 | *
|
---|
110 | * Known limitations:
|
---|
111 | *
|
---|
112 | * - Support for VRDE (VRDP) is not implemented yet (see #9498).
|
---|
113 | * - Unicode support on Windows hosts / guests is not enabled (yet).
|
---|
114 | * - Symbolic links / Windows junctions are not allowed.
|
---|
115 | * - Windows alternate data streams (ADS) are not allowed.
|
---|
116 | * - No support for ACLs yet.
|
---|
117 | * - No (maybe never) support for NT4.
|
---|
118 |
|
---|
119 | * @section sec_transfer_structure Transfer handling structure
|
---|
120 | *
|
---|
121 | * All structures / classes are designed for running on both, on the guest
|
---|
122 | * (via VBoxTray / VBoxClient) or on the host (host service) to avoid code
|
---|
123 | * duplication where applicable.
|
---|
124 | *
|
---|
125 | * Per HGCM client there is a so-called "transfer context", which in turn can
|
---|
126 | * have one or mulitple so-called "Shared Clipboard transfer" objects. At the
|
---|
127 | * moment we only support on concurrent Shared Clipboard transfer per transfer
|
---|
128 | * context. It's being used for reading from a source or writing to destination,
|
---|
129 | * depening on its direction. An Shared Clipboard transfer can have optional
|
---|
130 | * callbacks which might be needed by various implementations. Also, transfers
|
---|
131 | * optionally can run in an asynchronous thread to prevent blocking the UI while
|
---|
132 | * running.
|
---|
133 | *
|
---|
134 | * @section sec_transfer_providers Transfer providers
|
---|
135 | *
|
---|
136 | * For certain implementations (for example on Windows guests / hosts, using
|
---|
137 | * IDataObject and IStream objects) a more flexible approach reqarding reading /
|
---|
138 | * writing is needed. For this so-called transfer providers abstract the way of how
|
---|
139 | * data is being read / written in the current context (host / guest), while
|
---|
140 | * the rest of the code stays the same.
|
---|
141 | *
|
---|
142 | * @section sec_transfer_protocol Transfer protocol
|
---|
143 | *
|
---|
144 | * The host service issues commands which the guest has to respond with an own
|
---|
145 | * message to. The protocol itself is designed so that it has primitives to list
|
---|
146 | * directories and open/close/read/write file system objects.
|
---|
147 | *
|
---|
148 | * Note that this is different from the DnD approach, as Shared Clipboard transfers
|
---|
149 | * need to be deeper integrated within the host / guest OS (i.e. for progress UI),
|
---|
150 | * and this might require non-monolithic / random access APIs to achieve.
|
---|
151 | *
|
---|
152 | * As there can be multiple file system objects (fs objects) selected for transfer,
|
---|
153 | * a transfer can be queried for its root entries, which then contains the top-level
|
---|
154 | * elements. Based on these elements, (a) (recursive) listing(s) can be performed
|
---|
155 | * to (partially) walk down into directories and query fs object information. The
|
---|
156 | * provider provides appropriate interface for this, even if not all implementations
|
---|
157 | * might need this mechanism.
|
---|
158 | *
|
---|
159 | * An Shared Clipboard transfer has three stages:
|
---|
160 | * - 1. Announcement: An Shared Clipboard transfer-compatible format (currently only one format available)
|
---|
161 | * has been announced, the destination side creates a transfer object, which then,
|
---|
162 | * depending on the actual implementation, can be used to tell the OS that
|
---|
163 | * there is transfer (file) data available.
|
---|
164 | * At this point this just acts as a (kind-of) promise to the OS that we
|
---|
165 | * can provide (file) data at some later point in time.
|
---|
166 | *
|
---|
167 | * - 2. Initialization: As soon as the OS requests the (file) data, mostly triggered
|
---|
168 | * by the user starting a paste operation (CTRL + V), the transfer get initialized
|
---|
169 | * on the destination side, which in turn lets the source know that a transfer
|
---|
170 | * is going to happen.
|
---|
171 | *
|
---|
172 | * - 3. Transfer: At this stage the actual transfer from source to the destination takes
|
---|
173 | * place. How the actual transfer is structurized (e.g. which files / directories
|
---|
174 | * are transferred in which order) depends on the destination implementation. This
|
---|
175 | * is necessary in order to fulfill requirements on the destination side with
|
---|
176 | * regards to ETA calculation or other dependencies.
|
---|
177 | * Both sides can abort or cancel the transfer at any time.
|
---|
178 | */
|
---|
179 |
|
---|
180 |
|
---|
181 | /*********************************************************************************************************************************
|
---|
182 | * Header Files *
|
---|
183 | *********************************************************************************************************************************/
|
---|
184 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
185 | #include <VBox/log.h>
|
---|
186 |
|
---|
187 | #include <VBox/GuestHost/clipboard-helper.h>
|
---|
188 | #include <VBox/HostServices/Service.h>
|
---|
189 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
190 | #include <VBox/HostServices/VBoxClipboardExt.h>
|
---|
191 |
|
---|
192 | #include <VBox/AssertGuest.h>
|
---|
193 | #include <VBox/err.h>
|
---|
194 | #include <VBox/VMMDev.h>
|
---|
195 | #include <VBox/vmm/ssm.h>
|
---|
196 |
|
---|
197 | #include <iprt/mem.h>
|
---|
198 | #include <iprt/string.h>
|
---|
199 | #include <iprt/assert.h>
|
---|
200 | #include <iprt/critsect.h>
|
---|
201 | #include <iprt/rand.h>
|
---|
202 |
|
---|
203 | #include "VBoxSharedClipboardSvc-internal.h"
|
---|
204 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
205 | # include "VBoxSharedClipboardSvc-transfers.h"
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | using namespace HGCM;
|
---|
209 |
|
---|
210 |
|
---|
211 | /*********************************************************************************************************************************
|
---|
212 | * Defined Constants And Macros *
|
---|
213 | *********************************************************************************************************************************/
|
---|
214 | /** @name The saved state versions for the shared clipboard service.
|
---|
215 | *
|
---|
216 | * @note We set bit 31 because prior to version 0x80000002 there would be a
|
---|
217 | * structure size rather than a version number. Setting bit 31 dispells
|
---|
218 | * any possible ambiguity.
|
---|
219 | *
|
---|
220 | * @{ */
|
---|
221 | /** The current saved state version. */
|
---|
222 | #define VBOX_SHCL_SAVED_STATE_VER_CURRENT VBOX_SHCL_SAVED_STATE_LEGACY_CID
|
---|
223 | /** Adds the legacy context ID list. */
|
---|
224 | #define VBOX_SHCL_SAVED_STATE_LEGACY_CID UINT32_C(0x80000005)
|
---|
225 | /** Adds the client's POD state and client state flags.
|
---|
226 | * @since 6.1 RC1 */
|
---|
227 | #define VBOX_SHCL_SAVED_STATE_VER_6_1RC1 UINT32_C(0x80000004)
|
---|
228 | /** First attempt saving state during @bugref{9437} development.
|
---|
229 | * @since 6.1 BETA 2 */
|
---|
230 | #define VBOX_SHCL_SAVED_STATE_VER_6_1B2 UINT32_C(0x80000003)
|
---|
231 | /** First structured version.
|
---|
232 | * @since 3.1 / r53668 */
|
---|
233 | #define VBOX_SHCL_SAVED_STATE_VER_3_1 UINT32_C(0x80000002)
|
---|
234 | /** This was just a state memory dump, including pointers and everything.
|
---|
235 | * @note This is not supported any more. Sorry. */
|
---|
236 | #define VBOX_SHCL_SAVED_STATE_VER_NOT_SUPP (ARCH_BITS == 64 ? UINT32_C(72) : UINT32_C(48))
|
---|
237 | /** @} */
|
---|
238 |
|
---|
239 |
|
---|
240 | /*********************************************************************************************************************************
|
---|
241 | * Global Variables *
|
---|
242 | *********************************************************************************************************************************/
|
---|
243 | PVBOXHGCMSVCHELPERS g_pHelpers;
|
---|
244 |
|
---|
245 | static RTCRITSECT g_CritSect;
|
---|
246 | /** Global Shared Clipboard mode. */
|
---|
247 | static uint32_t g_uMode = VBOX_SHCL_MODE_OFF;
|
---|
248 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
249 | /** Global Shared Clipboard (file) transfer mode. */
|
---|
250 | uint32_t g_fTransferMode = VBOX_SHCL_TRANSFER_MODE_DISABLED;
|
---|
251 | #endif
|
---|
252 |
|
---|
253 | /** Is the clipboard running in headless mode? */
|
---|
254 | static bool g_fHeadless = false;
|
---|
255 |
|
---|
256 | /** Holds the service extension state. */
|
---|
257 | SHCLEXTSTATE g_ExtState = { 0 };
|
---|
258 |
|
---|
259 | /** Global map of all connected clients. */
|
---|
260 | ClipboardClientMap g_mapClients;
|
---|
261 |
|
---|
262 | /** Global list of all clients which are queued up (deferred return) and ready
|
---|
263 | * to process new commands. The key is the (unique) client ID. */
|
---|
264 | ClipboardClientQueue g_listClientsDeferred;
|
---|
265 |
|
---|
266 | /** Host feature mask (VBOX_SHCL_HF_0_XXX) for VBOX_SHCL_GUEST_FN_REPORT_FEATURES
|
---|
267 | * and VBOX_SHCL_GUEST_FN_QUERY_FEATURES. */
|
---|
268 | static uint64_t const g_fHostFeatures0 = VBOX_SHCL_HF_0_CONTEXT_ID
|
---|
269 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
270 | | VBOX_SHCL_HF_0_TRANSFERS
|
---|
271 | #endif
|
---|
272 | ;
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Returns the current Shared Clipboard service mode.
|
---|
277 | *
|
---|
278 | * @returns Current Shared Clipboard service mode.
|
---|
279 | */
|
---|
280 | uint32_t ShClSvcGetMode(void)
|
---|
281 | {
|
---|
282 | return g_uMode;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Getter for headless setting. Also needed by testcase.
|
---|
287 | *
|
---|
288 | * @returns Whether service currently running in headless mode or not.
|
---|
289 | */
|
---|
290 | bool ShClSvcGetHeadless(void)
|
---|
291 | {
|
---|
292 | return g_fHeadless;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static int shClSvcModeSet(uint32_t uMode)
|
---|
296 | {
|
---|
297 | int rc = VERR_NOT_SUPPORTED;
|
---|
298 |
|
---|
299 | switch (uMode)
|
---|
300 | {
|
---|
301 | case VBOX_SHCL_MODE_OFF:
|
---|
302 | RT_FALL_THROUGH();
|
---|
303 | case VBOX_SHCL_MODE_HOST_TO_GUEST:
|
---|
304 | RT_FALL_THROUGH();
|
---|
305 | case VBOX_SHCL_MODE_GUEST_TO_HOST:
|
---|
306 | RT_FALL_THROUGH();
|
---|
307 | case VBOX_SHCL_MODE_BIDIRECTIONAL:
|
---|
308 | {
|
---|
309 | g_uMode = uMode;
|
---|
310 |
|
---|
311 | rc = VINF_SUCCESS;
|
---|
312 | break;
|
---|
313 | }
|
---|
314 |
|
---|
315 | default:
|
---|
316 | {
|
---|
317 | g_uMode = VBOX_SHCL_MODE_OFF;
|
---|
318 | break;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | LogFlowFuncLeaveRC(rc);
|
---|
323 | return rc;
|
---|
324 | }
|
---|
325 |
|
---|
326 | bool ShClSvcLock(void)
|
---|
327 | {
|
---|
328 | return RT_SUCCESS(RTCritSectEnter(&g_CritSect));
|
---|
329 | }
|
---|
330 |
|
---|
331 | void ShClSvcUnlock(void)
|
---|
332 | {
|
---|
333 | int rc2 = RTCritSectLeave(&g_CritSect);
|
---|
334 | AssertRC(rc2);
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Resets a client's state message queue.
|
---|
339 | *
|
---|
340 | * @param pClient Pointer to the client data structure to reset message queue for.
|
---|
341 | * @note Caller enters pClient->CritSect.
|
---|
342 | */
|
---|
343 | void shClSvcMsgQueueReset(PSHCLCLIENT pClient)
|
---|
344 | {
|
---|
345 | Assert(RTCritSectIsOwner(&pClient->CritSect));
|
---|
346 | LogFlowFuncEnter();
|
---|
347 |
|
---|
348 | while (!RTListIsEmpty(&pClient->MsgQueue))
|
---|
349 | {
|
---|
350 | PSHCLCLIENTMSG pMsg = RTListRemoveFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
|
---|
351 | shClSvcMsgFree(pClient, pMsg);
|
---|
352 | }
|
---|
353 | pClient->cMsgAllocated = 0;
|
---|
354 |
|
---|
355 | while (!RTListIsEmpty(&pClient->Legacy.lstCID))
|
---|
356 | {
|
---|
357 | PSHCLCLIENTLEGACYCID pCID = RTListRemoveFirst(&pClient->Legacy.lstCID, SHCLCLIENTLEGACYCID, Node);
|
---|
358 | RTMemFree(pCID);
|
---|
359 | }
|
---|
360 | pClient->Legacy.cCID = 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Allocates a new clipboard message.
|
---|
365 | *
|
---|
366 | * @returns Allocated clipboard message, or NULL on failure.
|
---|
367 | * @param pClient The client which is target of this message.
|
---|
368 | * @param idMsg The message ID (VBOX_SHCL_HOST_MSG_XXX) to use
|
---|
369 | * @param cParms The number of parameters the message takes.
|
---|
370 | */
|
---|
371 | PSHCLCLIENTMSG shClSvcMsgAlloc(PSHCLCLIENT pClient, uint32_t idMsg, uint32_t cParms)
|
---|
372 | {
|
---|
373 | RT_NOREF(pClient);
|
---|
374 | PSHCLCLIENTMSG pMsg = (PSHCLCLIENTMSG)RTMemAllocZ(RT_UOFFSETOF_DYN(SHCLCLIENTMSG, aParms[cParms]));
|
---|
375 | if (pMsg)
|
---|
376 | {
|
---|
377 | uint32_t cAllocated = ASMAtomicIncU32(&pClient->cMsgAllocated);
|
---|
378 | if (cAllocated <= 4096)
|
---|
379 | {
|
---|
380 | RTListInit(&pMsg->ListEntry);
|
---|
381 | pMsg->cParms = cParms;
|
---|
382 | pMsg->idMsg = idMsg;
|
---|
383 | return pMsg;
|
---|
384 | }
|
---|
385 | AssertMsgFailed(("Too many messages allocated for client %u! (%u)\n", pClient->State.uClientID, cAllocated));
|
---|
386 | ASMAtomicDecU32(&pClient->cMsgAllocated);
|
---|
387 | RTMemFree(pMsg);
|
---|
388 | }
|
---|
389 | return NULL;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Frees a formerly allocated clipboard message.
|
---|
394 | *
|
---|
395 | * @param pClient The client which was the target of this message.
|
---|
396 | * @param pMsg Clipboard message to free.
|
---|
397 | */
|
---|
398 | void shClSvcMsgFree(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg)
|
---|
399 | {
|
---|
400 | RT_NOREF(pClient);
|
---|
401 | /** @todo r=bird: Do accounting. */
|
---|
402 | if (pMsg)
|
---|
403 | {
|
---|
404 | pMsg->idMsg = UINT32_C(0xdeadface);
|
---|
405 | RTMemFree(pMsg);
|
---|
406 |
|
---|
407 | uint32_t cAllocated = ASMAtomicDecU32(&pClient->cMsgAllocated);
|
---|
408 | Assert(cAllocated < UINT32_MAX / 2);
|
---|
409 | RT_NOREF(cAllocated);
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Sets the VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT
|
---|
415 | * return parameters.
|
---|
416 | *
|
---|
417 | * @param pMsg Message to set return parameters to.
|
---|
418 | * @param paDstParms The peek parameter vector.
|
---|
419 | * @param cDstParms The number of peek parameters (at least two).
|
---|
420 | * @remarks ASSUMES the parameters has been cleared by clientMsgPeek.
|
---|
421 | */
|
---|
422 | static void shClSvcMsgSetPeekReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
|
---|
423 | {
|
---|
424 | Assert(cDstParms >= 2);
|
---|
425 | if (paDstParms[0].type == VBOX_HGCM_SVC_PARM_32BIT)
|
---|
426 | paDstParms[0].u.uint32 = pMsg->idMsg;
|
---|
427 | else
|
---|
428 | paDstParms[0].u.uint64 = pMsg->idMsg;
|
---|
429 | paDstParms[1].u.uint32 = pMsg->cParms;
|
---|
430 |
|
---|
431 | uint32_t i = RT_MIN(cDstParms, pMsg->cParms + 2);
|
---|
432 | while (i-- > 2)
|
---|
433 | switch (pMsg->aParms[i - 2].type)
|
---|
434 | {
|
---|
435 | case VBOX_HGCM_SVC_PARM_32BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint32_t); break;
|
---|
436 | case VBOX_HGCM_SVC_PARM_64BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint64_t); break;
|
---|
437 | case VBOX_HGCM_SVC_PARM_PTR: paDstParms[i].u.uint32 = pMsg->aParms[i - 2].u.pointer.size; break;
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * Sets the VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT return parameters.
|
---|
443 | *
|
---|
444 | * @returns VBox status code.
|
---|
445 | * @param pMsg The message which parameters to return to the guest.
|
---|
446 | * @param paDstParms The peek parameter vector.
|
---|
447 | * @param cDstParms The number of peek parameters should be exactly two
|
---|
448 | */
|
---|
449 | static int shClSvcMsgSetOldWaitReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
|
---|
450 | {
|
---|
451 | /*
|
---|
452 | * Assert sanity.
|
---|
453 | */
|
---|
454 | AssertPtr(pMsg);
|
---|
455 | AssertPtrReturn(paDstParms, VERR_INVALID_POINTER);
|
---|
456 | AssertReturn(cDstParms >= 2, VERR_INVALID_PARAMETER);
|
---|
457 |
|
---|
458 | Assert(pMsg->cParms == 2);
|
---|
459 | Assert(pMsg->aParms[0].u.uint32 == pMsg->idMsg);
|
---|
460 | switch (pMsg->idMsg)
|
---|
461 | {
|
---|
462 | case VBOX_SHCL_HOST_MSG_READ_DATA:
|
---|
463 | case VBOX_SHCL_HOST_MSG_FORMATS_REPORT:
|
---|
464 | break;
|
---|
465 | default:
|
---|
466 | AssertFailed();
|
---|
467 | }
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Set the parameters.
|
---|
471 | */
|
---|
472 | if (pMsg->cParms > 0)
|
---|
473 | paDstParms[0] = pMsg->aParms[0];
|
---|
474 | if (pMsg->cParms > 1)
|
---|
475 | paDstParms[1] = pMsg->aParms[1];
|
---|
476 | return VINF_SUCCESS;
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Adds a new message to a client'S message queue.
|
---|
481 | *
|
---|
482 | * @param pClient Pointer to the client data structure to add new message to.
|
---|
483 | * @param pMsg Pointer to message to add. The queue then owns the pointer.
|
---|
484 | * @param fAppend Whether to append or prepend the message to the queue.
|
---|
485 | *
|
---|
486 | * @note Caller must enter critical section.
|
---|
487 | */
|
---|
488 | void shClSvcMsgAdd(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg, bool fAppend)
|
---|
489 | {
|
---|
490 | Assert(RTCritSectIsOwned(&pClient->CritSect));
|
---|
491 | AssertPtr(pMsg);
|
---|
492 |
|
---|
493 | LogFlowFunc(("idMsg=%s (%RU32) cParms=%RU32 fAppend=%RTbool\n",
|
---|
494 | ShClHostMsgToStr(pMsg->idMsg), pMsg->idMsg, pMsg->cParms, fAppend));
|
---|
495 |
|
---|
496 | if (fAppend)
|
---|
497 | RTListAppend(&pClient->MsgQueue, &pMsg->ListEntry);
|
---|
498 | else
|
---|
499 | RTListPrepend(&pClient->MsgQueue, &pMsg->ListEntry);
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * Appends a message to the client's queue and wake it up.
|
---|
505 | *
|
---|
506 | * @returns VBox status code, though the message is consumed regardless of what
|
---|
507 | * is returned.
|
---|
508 | * @param pClient The client to queue the message on.
|
---|
509 | * @param pMsg The message to queue. Ownership is always
|
---|
510 | * transfered to the queue.
|
---|
511 | *
|
---|
512 | * @note Caller must enter critical section.
|
---|
513 | */
|
---|
514 | int shClSvcMsgAddAndWakeupClient(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg)
|
---|
515 | {
|
---|
516 | Assert(RTCritSectIsOwned(&pClient->CritSect));
|
---|
517 | AssertPtr(pMsg);
|
---|
518 | AssertPtr(pClient);
|
---|
519 | LogFlowFunc(("idMsg=%s (%u) cParms=%u\n", ShClHostMsgToStr(pMsg->idMsg), pMsg->idMsg, pMsg->cParms));
|
---|
520 |
|
---|
521 | RTListAppend(&pClient->MsgQueue, &pMsg->ListEntry);
|
---|
522 | return shClSvcClientWakeup(pClient);
|
---|
523 | }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Initializes a Shared Clipboard client.
|
---|
527 | *
|
---|
528 | * @param pClient Client to initialize.
|
---|
529 | * @param uClientID HGCM client ID to assign client to.
|
---|
530 | */
|
---|
531 | int shClSvcClientInit(PSHCLCLIENT pClient, uint32_t uClientID)
|
---|
532 | {
|
---|
533 | AssertPtrReturn(pClient, VERR_INVALID_POINTER);
|
---|
534 |
|
---|
535 | /* Assign the client ID. */
|
---|
536 | pClient->State.uClientID = uClientID;
|
---|
537 |
|
---|
538 | RTListInit(&pClient->MsgQueue);
|
---|
539 | pClient->cMsgAllocated = 0;
|
---|
540 |
|
---|
541 | RTListInit(&pClient->Legacy.lstCID);
|
---|
542 | pClient->Legacy.cCID = 0;
|
---|
543 |
|
---|
544 | LogFlowFunc(("[Client %RU32]\n", pClient->State.uClientID));
|
---|
545 |
|
---|
546 | int rc = RTCritSectInit(&pClient->CritSect);
|
---|
547 | if (RT_SUCCESS(rc))
|
---|
548 | {
|
---|
549 | /* Create the client's own event source. */
|
---|
550 | rc = ShClEventSourceCreate(&pClient->EventSrc, 0 /* ID, ignored */);
|
---|
551 | if (RT_SUCCESS(rc))
|
---|
552 | {
|
---|
553 | LogFlowFunc(("[Client %RU32] Using event source %RU32\n", uClientID, pClient->EventSrc.uID));
|
---|
554 |
|
---|
555 | /* Reset the client state. */
|
---|
556 | shclSvcClientStateReset(&pClient->State);
|
---|
557 |
|
---|
558 | /* (Re-)initialize the client state. */
|
---|
559 | rc = shClSvcClientStateInit(&pClient->State, uClientID);
|
---|
560 |
|
---|
561 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
562 | if (RT_SUCCESS(rc))
|
---|
563 | rc = ShClTransferCtxInit(&pClient->TransferCtx);
|
---|
564 | #endif
|
---|
565 | }
|
---|
566 | }
|
---|
567 |
|
---|
568 | LogFlowFuncLeaveRC(rc);
|
---|
569 | return rc;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Destroys a Shared Clipboard client.
|
---|
574 | *
|
---|
575 | * @param pClient Client to destroy.
|
---|
576 | */
|
---|
577 | void shClSvcClientDestroy(PSHCLCLIENT pClient)
|
---|
578 | {
|
---|
579 | AssertPtrReturnVoid(pClient);
|
---|
580 |
|
---|
581 | LogFlowFunc(("[Client %RU32]\n", pClient->State.uClientID));
|
---|
582 |
|
---|
583 | /* Make sure to send a quit message to the guest so that it can terminate gracefully. */
|
---|
584 | RTCritSectEnter(&pClient->CritSect);
|
---|
585 | if (pClient->Pending.uType)
|
---|
586 | {
|
---|
587 | if (pClient->Pending.cParms > 1)
|
---|
588 | HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_QUIT);
|
---|
589 | if (pClient->Pending.cParms > 2)
|
---|
590 | HGCMSvcSetU32(&pClient->Pending.paParms[1], 0);
|
---|
591 | g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
|
---|
592 | pClient->Pending.uType = 0;
|
---|
593 | pClient->Pending.cParms = 0;
|
---|
594 | pClient->Pending.hHandle = NULL;
|
---|
595 | pClient->Pending.paParms = NULL;
|
---|
596 | }
|
---|
597 | RTCritSectLeave(&pClient->CritSect);
|
---|
598 |
|
---|
599 | ShClEventSourceDestroy(&pClient->EventSrc);
|
---|
600 |
|
---|
601 | shClSvcClientStateDestroy(&pClient->State);
|
---|
602 |
|
---|
603 | PSHCLCLIENTLEGACYCID pCidIter, pCidIterNext;
|
---|
604 | RTListForEachSafe(&pClient->Legacy.lstCID, pCidIter, pCidIterNext, SHCLCLIENTLEGACYCID, Node)
|
---|
605 | {
|
---|
606 | RTMemFree(pCidIter);
|
---|
607 | }
|
---|
608 |
|
---|
609 | int rc2 = RTCritSectDelete(&pClient->CritSect);
|
---|
610 | AssertRC(rc2);
|
---|
611 |
|
---|
612 | ClipboardClientMap::iterator itClient = g_mapClients.find(pClient->State.uClientID);
|
---|
613 | if (itClient != g_mapClients.end())
|
---|
614 | g_mapClients.erase(itClient);
|
---|
615 | else
|
---|
616 | AssertFailed();
|
---|
617 |
|
---|
618 | LogFlowFuncLeave();
|
---|
619 | }
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Resets a Shared Clipboard client.
|
---|
623 | *
|
---|
624 | * @param pClient Client to reset.
|
---|
625 | */
|
---|
626 | void shClSvcClientReset(PSHCLCLIENT pClient)
|
---|
627 | {
|
---|
628 | if (!pClient)
|
---|
629 | return;
|
---|
630 |
|
---|
631 | LogFlowFunc(("[Client %RU32]\n", pClient->State.uClientID));
|
---|
632 | RTCritSectEnter(&pClient->CritSect);
|
---|
633 |
|
---|
634 | /* Reset message queue. */
|
---|
635 | shClSvcMsgQueueReset(pClient);
|
---|
636 |
|
---|
637 | /* Reset event source. */
|
---|
638 | ShClEventSourceReset(&pClient->EventSrc);
|
---|
639 |
|
---|
640 | /* Reset pending state. */
|
---|
641 | RT_ZERO(pClient->Pending);
|
---|
642 |
|
---|
643 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
644 | shClSvcClientTransfersReset(pClient);
|
---|
645 | #endif
|
---|
646 |
|
---|
647 | shclSvcClientStateReset(&pClient->State);
|
---|
648 |
|
---|
649 | RTCritSectLeave(&pClient->CritSect);
|
---|
650 | }
|
---|
651 |
|
---|
652 | static int shClSvcClientNegogiateChunkSize(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall,
|
---|
653 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
654 | {
|
---|
655 | /*
|
---|
656 | * Validate the request.
|
---|
657 | */
|
---|
658 | ASSERT_GUEST_RETURN(cParms == VBOX_SHCL_CPARMS_NEGOTIATE_CHUNK_SIZE, VERR_WRONG_PARAMETER_COUNT);
|
---|
659 | ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
660 | uint32_t const cbClientMaxChunkSize = paParms[0].u.uint32;
|
---|
661 | ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
662 | uint32_t const cbClientChunkSize = paParms[1].u.uint32;
|
---|
663 |
|
---|
664 | uint32_t const cbHostMaxChunkSize = VBOX_SHCL_MAX_CHUNK_SIZE; /** @todo Make this configurable. */
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * Do the work.
|
---|
668 | */
|
---|
669 | if (cbClientChunkSize == 0) /* Does the client want us to choose? */
|
---|
670 | {
|
---|
671 | paParms[0].u.uint32 = cbHostMaxChunkSize; /* Maximum */
|
---|
672 | paParms[1].u.uint32 = RT_MIN(pClient->State.cbChunkSize, cbHostMaxChunkSize); /* Preferred */
|
---|
673 |
|
---|
674 | }
|
---|
675 | else /* The client told us what it supports, so update and report back. */
|
---|
676 | {
|
---|
677 | paParms[0].u.uint32 = RT_MIN(cbClientMaxChunkSize, cbHostMaxChunkSize); /* Maximum */
|
---|
678 | paParms[1].u.uint32 = RT_MIN(cbClientMaxChunkSize, pClient->State.cbChunkSize); /* Preferred */
|
---|
679 | }
|
---|
680 |
|
---|
681 | int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
|
---|
682 | if (RT_SUCCESS(rc))
|
---|
683 | {
|
---|
684 | Log(("[Client %RU32] chunk size: %#RU32, max: %#RU32\n",
|
---|
685 | pClient->State.uClientID, paParms[1].u.uint32, paParms[0].u.uint32));
|
---|
686 | }
|
---|
687 | else
|
---|
688 | LogFunc(("pfnCallComplete -> %Rrc\n", rc));
|
---|
689 |
|
---|
690 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
691 | }
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Implements VBOX_SHCL_GUEST_FN_REPORT_FEATURES.
|
---|
695 | *
|
---|
696 | * @returns VBox status code.
|
---|
697 | * @retval VINF_HGCM_ASYNC_EXECUTE on success (we complete the message here).
|
---|
698 | * @retval VERR_ACCESS_DENIED if not master
|
---|
699 | * @retval VERR_INVALID_PARAMETER if bit 63 in the 2nd parameter isn't set.
|
---|
700 | * @retval VERR_WRONG_PARAMETER_COUNT
|
---|
701 | *
|
---|
702 | * @param pClient The client state.
|
---|
703 | * @param hCall The client's call handle.
|
---|
704 | * @param cParms Number of parameters.
|
---|
705 | * @param paParms Array of parameters.
|
---|
706 | */
|
---|
707 | static int shClSvcClientReportFeatures(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall,
|
---|
708 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
709 | {
|
---|
710 | /*
|
---|
711 | * Validate the request.
|
---|
712 | */
|
---|
713 | ASSERT_GUEST_RETURN(cParms == 2, VERR_WRONG_PARAMETER_COUNT);
|
---|
714 | ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
715 | uint64_t const fFeatures0 = paParms[0].u.uint64;
|
---|
716 | ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
717 | uint64_t const fFeatures1 = paParms[1].u.uint64;
|
---|
718 | ASSERT_GUEST_RETURN(fFeatures1 & VBOX_SHCL_GF_1_MUST_BE_ONE, VERR_INVALID_PARAMETER);
|
---|
719 |
|
---|
720 | /*
|
---|
721 | * Do the work.
|
---|
722 | */
|
---|
723 | paParms[0].u.uint64 = g_fHostFeatures0;
|
---|
724 | paParms[1].u.uint64 = 0;
|
---|
725 |
|
---|
726 | int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
|
---|
727 | if (RT_SUCCESS(rc))
|
---|
728 | {
|
---|
729 | pClient->State.fGuestFeatures0 = fFeatures0;
|
---|
730 | pClient->State.fGuestFeatures1 = fFeatures1;
|
---|
731 | Log(("[Client %RU32] features: %#RX64 %#RX64\n", pClient->State.uClientID, fFeatures0, fFeatures1));
|
---|
732 | LogRel2(("Shared Clipboard: Guest reported the following features: %#RX64\n", fFeatures0)); /* fFeatures1 not used yet. */
|
---|
733 | }
|
---|
734 | else
|
---|
735 | LogFunc(("pfnCallComplete -> %Rrc\n", rc));
|
---|
736 |
|
---|
737 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
738 | }
|
---|
739 |
|
---|
740 | /**
|
---|
741 | * Implements VBOX_SHCL_GUEST_FN_QUERY_FEATURES.
|
---|
742 | *
|
---|
743 | * @returns VBox status code.
|
---|
744 | * @retval VINF_HGCM_ASYNC_EXECUTE on success (we complete the message here).
|
---|
745 | * @retval VERR_WRONG_PARAMETER_COUNT
|
---|
746 | *
|
---|
747 | * @param hCall The client's call handle.
|
---|
748 | * @param cParms Number of parameters.
|
---|
749 | * @param paParms Array of parameters.
|
---|
750 | */
|
---|
751 | static int shClSvcClientQueryFeatures(VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
752 | {
|
---|
753 | /*
|
---|
754 | * Validate the request.
|
---|
755 | */
|
---|
756 | ASSERT_GUEST_RETURN(cParms == 2, VERR_WRONG_PARAMETER_COUNT);
|
---|
757 | ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
758 | ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
759 | ASSERT_GUEST(paParms[1].u.uint64 & RT_BIT_64(63));
|
---|
760 |
|
---|
761 | /*
|
---|
762 | * Do the work.
|
---|
763 | */
|
---|
764 | paParms[0].u.uint64 = g_fHostFeatures0;
|
---|
765 | paParms[1].u.uint64 = 0;
|
---|
766 | int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
|
---|
767 | if (RT_FAILURE(rc))
|
---|
768 | LogFunc(("pfnCallComplete -> %Rrc\n", rc));
|
---|
769 |
|
---|
770 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
771 | }
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Implements VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT.
|
---|
775 | *
|
---|
776 | * @returns VBox status code.
|
---|
777 | * @retval VINF_SUCCESS if a message was pending and is being returned.
|
---|
778 | * @retval VERR_TRY_AGAIN if no message pending and not blocking.
|
---|
779 | * @retval VERR_RESOURCE_BUSY if another read already made a waiting call.
|
---|
780 | * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
|
---|
781 | *
|
---|
782 | * @param pClient The client state.
|
---|
783 | * @param hCall The client's call handle.
|
---|
784 | * @param cParms Number of parameters.
|
---|
785 | * @param paParms Array of parameters.
|
---|
786 | * @param fWait Set if we should wait for a message, clear if to return
|
---|
787 | * immediately.
|
---|
788 | *
|
---|
789 | * @note Caller takes and leave the client's critical section.
|
---|
790 | */
|
---|
791 | static int shClSvcClientMsgPeek(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool fWait)
|
---|
792 | {
|
---|
793 | /*
|
---|
794 | * Validate the request.
|
---|
795 | */
|
---|
796 | ASSERT_GUEST_MSG_RETURN(cParms >= 2, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
|
---|
797 |
|
---|
798 | uint64_t idRestoreCheck = 0;
|
---|
799 | uint32_t i = 0;
|
---|
800 | if (paParms[i].type == VBOX_HGCM_SVC_PARM_64BIT)
|
---|
801 | {
|
---|
802 | idRestoreCheck = paParms[0].u.uint64;
|
---|
803 | paParms[0].u.uint64 = 0;
|
---|
804 | i++;
|
---|
805 | }
|
---|
806 | for (; i < cParms; i++)
|
---|
807 | {
|
---|
808 | ASSERT_GUEST_MSG_RETURN(paParms[i].type == VBOX_HGCM_SVC_PARM_32BIT, ("#%u type=%u\n", i, paParms[i].type),
|
---|
809 | VERR_WRONG_PARAMETER_TYPE);
|
---|
810 | paParms[i].u.uint32 = 0;
|
---|
811 | }
|
---|
812 |
|
---|
813 | /*
|
---|
814 | * Check restore session ID.
|
---|
815 | */
|
---|
816 | if (idRestoreCheck != 0)
|
---|
817 | {
|
---|
818 | uint64_t idRestore = g_pHelpers->pfnGetVMMDevSessionId(g_pHelpers);
|
---|
819 | if (idRestoreCheck != idRestore)
|
---|
820 | {
|
---|
821 | paParms[0].u.uint64 = idRestore;
|
---|
822 | LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VERR_VM_RESTORED (%#RX64 -> %#RX64)\n",
|
---|
823 | pClient->State.uClientID, idRestoreCheck, idRestore));
|
---|
824 | return VERR_VM_RESTORED;
|
---|
825 | }
|
---|
826 | Assert(!g_pHelpers->pfnIsCallRestored(hCall));
|
---|
827 | }
|
---|
828 |
|
---|
829 | /*
|
---|
830 | * Return information about the first message if one is pending in the list.
|
---|
831 | */
|
---|
832 | PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
|
---|
833 | if (pFirstMsg)
|
---|
834 | {
|
---|
835 | shClSvcMsgSetPeekReturn(pFirstMsg, paParms, cParms);
|
---|
836 | LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VINF_SUCCESS (idMsg=%s (%u), cParms=%u)\n",
|
---|
837 | pClient->State.uClientID, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
|
---|
838 | return VINF_SUCCESS;
|
---|
839 | }
|
---|
840 |
|
---|
841 | /*
|
---|
842 | * If we cannot wait, fail the call.
|
---|
843 | */
|
---|
844 | if (!fWait)
|
---|
845 | {
|
---|
846 | LogFlowFunc(("[Client %RU32] GUEST_MSG_PEEK_NOWAIT -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
|
---|
847 | return VERR_TRY_AGAIN;
|
---|
848 | }
|
---|
849 |
|
---|
850 | /*
|
---|
851 | * Wait for the host to queue a message for this client.
|
---|
852 | */
|
---|
853 | ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n",
|
---|
854 | pClient->State.uClientID), VERR_RESOURCE_BUSY);
|
---|
855 | pClient->Pending.hHandle = hCall;
|
---|
856 | pClient->Pending.cParms = cParms;
|
---|
857 | pClient->Pending.paParms = paParms;
|
---|
858 | pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT;
|
---|
859 | LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
|
---|
860 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
861 | }
|
---|
862 |
|
---|
863 | /**
|
---|
864 | * Implements VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT.
|
---|
865 | *
|
---|
866 | * @returns VBox status code.
|
---|
867 | * @retval VINF_SUCCESS if a message was pending and is being returned.
|
---|
868 | * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
|
---|
869 | *
|
---|
870 | * @param pClient The client state.
|
---|
871 | * @param hCall The client's call handle.
|
---|
872 | * @param cParms Number of parameters.
|
---|
873 | * @param paParms Array of parameters.
|
---|
874 | *
|
---|
875 | * @note Caller takes and leave the client's critical section.
|
---|
876 | */
|
---|
877 | static int shClSvcClientMsgOldGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
878 | {
|
---|
879 | /*
|
---|
880 | * Validate input.
|
---|
881 | */
|
---|
882 | ASSERT_GUEST_RETURN(cParms == VBOX_SHCL_CPARMS_GET_HOST_MSG_OLD, VERR_WRONG_PARAMETER_COUNT);
|
---|
883 | ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* id32Msg */
|
---|
884 | ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* f32Formats */
|
---|
885 |
|
---|
886 | paParms[0].u.uint32 = 0;
|
---|
887 | paParms[1].u.uint32 = 0;
|
---|
888 |
|
---|
889 | /*
|
---|
890 | * If there is a message pending we can return immediately.
|
---|
891 | */
|
---|
892 | int rc;
|
---|
893 | PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
|
---|
894 | if (pFirstMsg)
|
---|
895 | {
|
---|
896 | LogFlowFunc(("[Client %RU32] uMsg=%s (%RU32), cParms=%RU32\n", pClient->State.uClientID,
|
---|
897 | ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
|
---|
898 |
|
---|
899 | rc = shClSvcMsgSetOldWaitReturn(pFirstMsg, paParms, cParms);
|
---|
900 | AssertPtr(g_pHelpers);
|
---|
901 | rc = g_pHelpers->pfnCallComplete(hCall, rc);
|
---|
902 | if (rc != VERR_CANCELLED)
|
---|
903 | {
|
---|
904 | RTListNodeRemove(&pFirstMsg->ListEntry);
|
---|
905 | shClSvcMsgFree(pClient, pFirstMsg);
|
---|
906 |
|
---|
907 | rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
|
---|
908 | }
|
---|
909 | }
|
---|
910 | /*
|
---|
911 | * Otherwise we must wait.
|
---|
912 | */
|
---|
913 | else
|
---|
914 | {
|
---|
915 | ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n", pClient->State.uClientID),
|
---|
916 | VERR_RESOURCE_BUSY);
|
---|
917 |
|
---|
918 | pClient->Pending.hHandle = hCall;
|
---|
919 | pClient->Pending.cParms = cParms;
|
---|
920 | pClient->Pending.paParms = paParms;
|
---|
921 | pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT;
|
---|
922 |
|
---|
923 | rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
|
---|
924 |
|
---|
925 | LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
|
---|
926 | }
|
---|
927 |
|
---|
928 | LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
|
---|
929 | return rc;
|
---|
930 | }
|
---|
931 |
|
---|
932 | /**
|
---|
933 | * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
|
---|
934 | *
|
---|
935 | * @returns VBox status code.
|
---|
936 | * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
|
---|
937 | * @retval VERR_TRY_AGAIN if no message pending.
|
---|
938 | * @retval VERR_BUFFER_OVERFLOW if a parmeter buffer is too small. The buffer
|
---|
939 | * size was updated to reflect the required size, though this isn't yet
|
---|
940 | * forwarded to the guest. (The guest is better of using peek with
|
---|
941 | * parameter count + 2 parameters to get the sizes.)
|
---|
942 | * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
|
---|
943 | * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
|
---|
944 | *
|
---|
945 | * @param pClient The client state.
|
---|
946 | * @param hCall The client's call handle.
|
---|
947 | * @param cParms Number of parameters.
|
---|
948 | * @param paParms Array of parameters.
|
---|
949 | *
|
---|
950 | * @note Called from within pClient->CritSect.
|
---|
951 | */
|
---|
952 | static int shClSvcClientMsgGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
953 | {
|
---|
954 | /*
|
---|
955 | * Validate the request.
|
---|
956 | */
|
---|
957 | uint32_t const idMsgExpected = cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT ? paParms[0].u.uint32
|
---|
958 | : cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT ? paParms[0].u.uint64
|
---|
959 | : UINT32_MAX;
|
---|
960 |
|
---|
961 | /*
|
---|
962 | * Return information about the first message if one is pending in the list.
|
---|
963 | */
|
---|
964 | PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
|
---|
965 | if (pFirstMsg)
|
---|
966 | {
|
---|
967 | LogFlowFunc(("First message is: %s (%u), cParms=%RU32\n", ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
|
---|
968 |
|
---|
969 | ASSERT_GUEST_MSG_RETURN(pFirstMsg->idMsg == idMsgExpected || idMsgExpected == UINT32_MAX,
|
---|
970 | ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
|
---|
971 | pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->cParms,
|
---|
972 | idMsgExpected, ShClHostMsgToStr(idMsgExpected), cParms),
|
---|
973 | VERR_MISMATCH);
|
---|
974 | ASSERT_GUEST_MSG_RETURN(pFirstMsg->cParms == cParms,
|
---|
975 | ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
|
---|
976 | pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->cParms,
|
---|
977 | idMsgExpected, ShClHostMsgToStr(idMsgExpected), cParms),
|
---|
978 | VERR_WRONG_PARAMETER_COUNT);
|
---|
979 |
|
---|
980 | /* Check the parameter types. */
|
---|
981 | for (uint32_t i = 0; i < cParms; i++)
|
---|
982 | ASSERT_GUEST_MSG_RETURN(pFirstMsg->aParms[i].type == paParms[i].type,
|
---|
983 | ("param #%u: type %u, caller expected %u (idMsg=%u %s)\n", i, pFirstMsg->aParms[i].type,
|
---|
984 | paParms[i].type, pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg)),
|
---|
985 | VERR_WRONG_PARAMETER_TYPE);
|
---|
986 | /*
|
---|
987 | * Copy out the parameters.
|
---|
988 | *
|
---|
989 | * No assertions on buffer overflows, and keep going till the end so we can
|
---|
990 | * communicate all the required buffer sizes.
|
---|
991 | */
|
---|
992 | int rc = VINF_SUCCESS;
|
---|
993 | for (uint32_t i = 0; i < cParms; i++)
|
---|
994 | switch (pFirstMsg->aParms[i].type)
|
---|
995 | {
|
---|
996 | case VBOX_HGCM_SVC_PARM_32BIT:
|
---|
997 | paParms[i].u.uint32 = pFirstMsg->aParms[i].u.uint32;
|
---|
998 | break;
|
---|
999 |
|
---|
1000 | case VBOX_HGCM_SVC_PARM_64BIT:
|
---|
1001 | paParms[i].u.uint64 = pFirstMsg->aParms[i].u.uint64;
|
---|
1002 | break;
|
---|
1003 |
|
---|
1004 | case VBOX_HGCM_SVC_PARM_PTR:
|
---|
1005 | {
|
---|
1006 | uint32_t const cbSrc = pFirstMsg->aParms[i].u.pointer.size;
|
---|
1007 | uint32_t const cbDst = paParms[i].u.pointer.size;
|
---|
1008 | paParms[i].u.pointer.size = cbSrc; /** @todo Check if this is safe in other layers...
|
---|
1009 | * Update: Safe, yes, but VMMDevHGCM doesn't pass it along. */
|
---|
1010 | if (cbSrc <= cbDst)
|
---|
1011 | memcpy(paParms[i].u.pointer.addr, pFirstMsg->aParms[i].u.pointer.addr, cbSrc);
|
---|
1012 | else
|
---|
1013 | {
|
---|
1014 | AssertMsgFailed(("#%u: cbSrc=%RU32 is bigger than cbDst=%RU32\n", i, cbSrc, cbDst));
|
---|
1015 | rc = VERR_BUFFER_OVERFLOW;
|
---|
1016 | }
|
---|
1017 | break;
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | default:
|
---|
1021 | AssertMsgFailed(("#%u: %u\n", i, pFirstMsg->aParms[i].type));
|
---|
1022 | rc = VERR_INTERNAL_ERROR;
|
---|
1023 | break;
|
---|
1024 | }
|
---|
1025 | if (RT_SUCCESS(rc))
|
---|
1026 | {
|
---|
1027 | /*
|
---|
1028 | * Complete the message and remove the pending message unless the
|
---|
1029 | * guest raced us and cancelled this call in the meantime.
|
---|
1030 | */
|
---|
1031 | AssertPtr(g_pHelpers);
|
---|
1032 | rc = g_pHelpers->pfnCallComplete(hCall, rc);
|
---|
1033 |
|
---|
1034 | LogFlowFunc(("[Client %RU32] pfnCallComplete -> %Rrc\n", pClient->State.uClientID, rc));
|
---|
1035 |
|
---|
1036 | if (rc != VERR_CANCELLED)
|
---|
1037 | {
|
---|
1038 | RTListNodeRemove(&pFirstMsg->ListEntry);
|
---|
1039 | shClSvcMsgFree(pClient, pFirstMsg);
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | return VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | LogFlowFunc(("[Client %RU32] Returning %Rrc\n", pClient->State.uClientID, rc));
|
---|
1046 | return rc;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | paParms[0].u.uint32 = 0;
|
---|
1050 | paParms[1].u.uint32 = 0;
|
---|
1051 | LogFlowFunc(("[Client %RU32] -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
|
---|
1052 | return VERR_TRY_AGAIN;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | /**
|
---|
1056 | * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
|
---|
1057 | *
|
---|
1058 | * @returns VBox status code.
|
---|
1059 | * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
|
---|
1060 | * @retval VERR_TRY_AGAIN if no message pending.
|
---|
1061 | * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
|
---|
1062 | * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
|
---|
1063 | *
|
---|
1064 | * @param pClient The client state.
|
---|
1065 | * @param cParms Number of parameters.
|
---|
1066 | *
|
---|
1067 | * @note Called from within pClient->CritSect.
|
---|
1068 | */
|
---|
1069 | static int shClSvcClientMsgCancel(PSHCLCLIENT pClient, uint32_t cParms)
|
---|
1070 | {
|
---|
1071 | /*
|
---|
1072 | * Validate the request.
|
---|
1073 | */
|
---|
1074 | ASSERT_GUEST_MSG_RETURN(cParms == 0, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
|
---|
1075 |
|
---|
1076 | /*
|
---|
1077 | * Execute.
|
---|
1078 | */
|
---|
1079 | if (pClient->Pending.uType != 0)
|
---|
1080 | {
|
---|
1081 | LogFlowFunc(("[Client %RU32] Cancelling waiting thread, isPending=%d, pendingNumParms=%RU32, m_idSession=%x\n",
|
---|
1082 | pClient->State.uClientID, pClient->Pending.uType, pClient->Pending.cParms, pClient->State.uSessionID));
|
---|
1083 |
|
---|
1084 | /*
|
---|
1085 | * The PEEK call is simple: At least two parameters, all set to zero before sleeping.
|
---|
1086 | */
|
---|
1087 | int rcComplete;
|
---|
1088 | if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
|
---|
1089 | {
|
---|
1090 | Assert(pClient->Pending.cParms >= 2);
|
---|
1091 | if (pClient->Pending.paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT)
|
---|
1092 | HGCMSvcSetU64(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
|
---|
1093 | else
|
---|
1094 | HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
|
---|
1095 | rcComplete = VINF_TRY_AGAIN;
|
---|
1096 | }
|
---|
1097 | /*
|
---|
1098 | * The MSG_OLD call is complicated, though we're
|
---|
1099 | * generally here to wake up someone who is peeking and have two parameters.
|
---|
1100 | * If there aren't two parameters, fail the call.
|
---|
1101 | */
|
---|
1102 | else
|
---|
1103 | {
|
---|
1104 | Assert(pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
|
---|
1105 | if (pClient->Pending.cParms > 0)
|
---|
1106 | HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
|
---|
1107 | if (pClient->Pending.cParms > 1)
|
---|
1108 | HGCMSvcSetU32(&pClient->Pending.paParms[1], 0);
|
---|
1109 | rcComplete = pClient->Pending.cParms == 2 ? VINF_SUCCESS : VERR_TRY_AGAIN;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, rcComplete);
|
---|
1113 |
|
---|
1114 | pClient->Pending.hHandle = NULL;
|
---|
1115 | pClient->Pending.paParms = NULL;
|
---|
1116 | pClient->Pending.cParms = 0;
|
---|
1117 | pClient->Pending.uType = 0;
|
---|
1118 | return VINF_SUCCESS;
|
---|
1119 | }
|
---|
1120 | return VWRN_NOT_FOUND;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 |
|
---|
1124 | /**
|
---|
1125 | * Wakes up a pending client (i.e. waiting for new messages).
|
---|
1126 | *
|
---|
1127 | * @returns VBox status code.
|
---|
1128 | * @retval VINF_NO_CHANGE if the client is not in pending mode.
|
---|
1129 | *
|
---|
1130 | * @param pClient Client to wake up.
|
---|
1131 | * @note Caller must enter pClient->CritSect.
|
---|
1132 | */
|
---|
1133 | int shClSvcClientWakeup(PSHCLCLIENT pClient)
|
---|
1134 | {
|
---|
1135 | Assert(RTCritSectIsOwner(&pClient->CritSect));
|
---|
1136 | int rc = VINF_NO_CHANGE;
|
---|
1137 |
|
---|
1138 | if (pClient->Pending.uType != 0)
|
---|
1139 | {
|
---|
1140 | LogFunc(("[Client %RU32] Waking up ...\n", pClient->State.uClientID));
|
---|
1141 |
|
---|
1142 | PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
|
---|
1143 | AssertReturn(pFirstMsg, VERR_INTERNAL_ERROR);
|
---|
1144 |
|
---|
1145 | LogFunc(("[Client %RU32] Current host message is %s (%RU32), cParms=%RU32\n",
|
---|
1146 | pClient->State.uClientID, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
|
---|
1147 |
|
---|
1148 | if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
|
---|
1149 | shClSvcMsgSetPeekReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
|
---|
1150 | else if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT) /* Legacy, Guest Additions < 6.1. */
|
---|
1151 | shClSvcMsgSetOldWaitReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
|
---|
1152 | else
|
---|
1153 | AssertMsgFailedReturn(("pClient->Pending.uType=%u\n", pClient->Pending.uType), VERR_INTERNAL_ERROR_3);
|
---|
1154 |
|
---|
1155 | rc = g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
|
---|
1156 |
|
---|
1157 | if ( rc != VERR_CANCELLED
|
---|
1158 | && pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT)
|
---|
1159 | {
|
---|
1160 | RTListNodeRemove(&pFirstMsg->ListEntry);
|
---|
1161 | shClSvcMsgFree(pClient, pFirstMsg);
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | pClient->Pending.hHandle = NULL;
|
---|
1165 | pClient->Pending.paParms = NULL;
|
---|
1166 | pClient->Pending.cParms = 0;
|
---|
1167 | pClient->Pending.uType = 0;
|
---|
1168 | }
|
---|
1169 | else
|
---|
1170 | LogFunc(("[Client %RU32] Not in pending state, skipping wakeup\n", pClient->State.uClientID));
|
---|
1171 |
|
---|
1172 | return rc;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | /**
|
---|
1176 | * Requests to read clipboard data from the guest.
|
---|
1177 | *
|
---|
1178 | * @returns VBox status code.
|
---|
1179 | * @param pClient Client to request to read data form.
|
---|
1180 | * @param fFormats The formats being requested, OR'ed together (VBOX_SHCL_FMT_XXX).
|
---|
1181 | * @param pidEvent Event ID for waiting for new data. Optional.
|
---|
1182 | * Must be released by the caller with ShClEventRelease() before unregistering then.
|
---|
1183 | */
|
---|
1184 | int ShClSvcGuestDataRequest(PSHCLCLIENT pClient, SHCLFORMATS fFormats, PSHCLEVENTID pidEvent)
|
---|
1185 | {
|
---|
1186 | LogFlowFuncEnter();
|
---|
1187 | if (pidEvent)
|
---|
1188 | *pidEvent = NIL_SHCLEVENTID;
|
---|
1189 | AssertPtrReturn(pClient, VERR_INVALID_POINTER);
|
---|
1190 |
|
---|
1191 | LogFlowFunc(("fFormats=%#x\n", fFormats));
|
---|
1192 |
|
---|
1193 | int rc = VERR_NOT_SUPPORTED;
|
---|
1194 |
|
---|
1195 | SHCLEVENTID idEvent = NIL_SHCLEVENTID;
|
---|
1196 |
|
---|
1197 | /* Generate a separate message for every (valid) format we support. */
|
---|
1198 | while (fFormats)
|
---|
1199 | {
|
---|
1200 | /* Pick the next format to get from the mask: */
|
---|
1201 | /** @todo Make format reporting precedence configurable? */
|
---|
1202 | SHCLFORMAT fFormat;
|
---|
1203 | if (fFormats & VBOX_SHCL_FMT_UNICODETEXT)
|
---|
1204 | fFormat = VBOX_SHCL_FMT_UNICODETEXT;
|
---|
1205 | else if (fFormats & VBOX_SHCL_FMT_BITMAP)
|
---|
1206 | fFormat = VBOX_SHCL_FMT_BITMAP;
|
---|
1207 | else if (fFormats & VBOX_SHCL_FMT_HTML)
|
---|
1208 | fFormat = VBOX_SHCL_FMT_HTML;
|
---|
1209 | else
|
---|
1210 | AssertMsgFailedBreak(("%#x\n", fFormats));
|
---|
1211 |
|
---|
1212 | /* Remove it from the mask. */
|
---|
1213 | fFormats &= ~fFormat;
|
---|
1214 |
|
---|
1215 | /*
|
---|
1216 | * Allocate messages, one for each format.
|
---|
1217 | */
|
---|
1218 | PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient,
|
---|
1219 | pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID
|
---|
1220 | ? VBOX_SHCL_HOST_MSG_READ_DATA_CID : VBOX_SHCL_HOST_MSG_READ_DATA,
|
---|
1221 | 2);
|
---|
1222 | if (pMsg)
|
---|
1223 | {
|
---|
1224 | /*
|
---|
1225 | * Enter the critical section and generate an event.
|
---|
1226 | */
|
---|
1227 | RTCritSectEnter(&pClient->CritSect);
|
---|
1228 |
|
---|
1229 | idEvent = ShClEventIdGenerateAndRegister(&pClient->EventSrc);
|
---|
1230 | if (idEvent != NIL_SHCLEVENTID)
|
---|
1231 | {
|
---|
1232 | LogFlowFunc(("fFormats=%#x -> fFormat=%#x, idEvent=%#x\n", fFormats, fFormat, idEvent));
|
---|
1233 |
|
---|
1234 | const uint64_t uCID = VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID, pClient->EventSrc.uID, idEvent);
|
---|
1235 |
|
---|
1236 | rc = VINF_SUCCESS;
|
---|
1237 |
|
---|
1238 | /* Save the context ID in our legacy cruft if we have to deal with old(er) Guest Additions (< 6.1). */
|
---|
1239 | if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
|
---|
1240 | {
|
---|
1241 | AssertStmt(pClient->Legacy.cCID < 4096, rc = VERR_TOO_MUCH_DATA);
|
---|
1242 | if (RT_SUCCESS(rc))
|
---|
1243 | {
|
---|
1244 | PSHCLCLIENTLEGACYCID pCID = (PSHCLCLIENTLEGACYCID)RTMemAlloc(sizeof(SHCLCLIENTLEGACYCID));
|
---|
1245 | if (pCID)
|
---|
1246 | {
|
---|
1247 | pCID->uCID = uCID;
|
---|
1248 | pCID->enmType = 0; /* Not used yet. */
|
---|
1249 | pCID->uFormat = fFormat;
|
---|
1250 | RTListAppend(&pClient->Legacy.lstCID, &pCID->Node);
|
---|
1251 | pClient->Legacy.cCID++;
|
---|
1252 | }
|
---|
1253 | else
|
---|
1254 | rc = VERR_NO_MEMORY;
|
---|
1255 | }
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | if (RT_SUCCESS(rc))
|
---|
1259 | {
|
---|
1260 | /*
|
---|
1261 | * Format the message.
|
---|
1262 | */
|
---|
1263 | if (pMsg->idMsg == VBOX_SHCL_HOST_MSG_READ_DATA_CID)
|
---|
1264 | HGCMSvcSetU64(&pMsg->aParms[0], uCID);
|
---|
1265 | else
|
---|
1266 | HGCMSvcSetU32(&pMsg->aParms[0], VBOX_SHCL_HOST_MSG_READ_DATA);
|
---|
1267 | HGCMSvcSetU32(&pMsg->aParms[1], fFormat);
|
---|
1268 |
|
---|
1269 | shClSvcMsgAdd(pClient, pMsg, true /* fAppend */);
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 | else
|
---|
1273 | rc = VERR_SHCLPB_MAX_EVENTS_REACHED;
|
---|
1274 |
|
---|
1275 | RTCritSectLeave(&pClient->CritSect);
|
---|
1276 |
|
---|
1277 | if (RT_FAILURE(rc))
|
---|
1278 | shClSvcMsgFree(pClient, pMsg);
|
---|
1279 | }
|
---|
1280 | else
|
---|
1281 | rc = VERR_NO_MEMORY;
|
---|
1282 |
|
---|
1283 | if (RT_FAILURE(rc))
|
---|
1284 | break;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | if (RT_SUCCESS(rc))
|
---|
1288 | {
|
---|
1289 | RTCritSectEnter(&pClient->CritSect);
|
---|
1290 |
|
---|
1291 | /* Retain the last event generated (in case there were multiple clipboard formats)
|
---|
1292 | * if we need to return the event ID to the caller. */
|
---|
1293 | if (pidEvent)
|
---|
1294 | {
|
---|
1295 | ShClEventRetain(&pClient->EventSrc, idEvent);
|
---|
1296 | *pidEvent = idEvent;
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | shClSvcClientWakeup(pClient);
|
---|
1300 |
|
---|
1301 | RTCritSectLeave(&pClient->CritSect);
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | if (RT_FAILURE(rc))
|
---|
1305 | LogRel(("Shared Clipboard: Requesting data in formats %#x from guest failed with %Rrc\n", fFormats, rc));
|
---|
1306 |
|
---|
1307 | LogFlowFuncLeaveRC(rc);
|
---|
1308 | return rc;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | /**
|
---|
1312 | * Signals the host that clipboard data from the guest has been received.
|
---|
1313 | *
|
---|
1314 | * @returns VBox status code. Returns VERR_NOT_FOUND when related event ID was not found.
|
---|
1315 | * @param pClient Client the guest clipboard data was received for.
|
---|
1316 | * @param pCmdCtx Client command context to use.
|
---|
1317 | * @param uFormat Clipboard format of data received.
|
---|
1318 | * @param pvData Pointer to clipboard data received.
|
---|
1319 | * @param cbData Size (in bytes) of clipboard data received.
|
---|
1320 | */
|
---|
1321 | int ShClSvcGuestDataSignal(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
|
---|
1322 | SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
|
---|
1323 | {
|
---|
1324 | AssertPtrReturn(pClient, VERR_INVALID_POINTER);
|
---|
1325 | AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
|
---|
1326 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
1327 |
|
---|
1328 | RT_NOREF(uFormat);
|
---|
1329 |
|
---|
1330 | LogFlowFuncEnter();
|
---|
1331 |
|
---|
1332 | const SHCLEVENTID idEvent = VBOX_SHCL_CONTEXTID_GET_EVENT(pCmdCtx->uContextID);
|
---|
1333 |
|
---|
1334 | AssertMsgReturn(idEvent != NIL_SHCLEVENTID,
|
---|
1335 | ("Event %RU64 empty within supplied context ID\n", idEvent), VERR_WRONG_ORDER);
|
---|
1336 | #ifdef VBOX_STRICT
|
---|
1337 | AssertMsgReturn(ShClEventGet(&pClient->EventSrc, idEvent) != NULL,
|
---|
1338 | ("Event %RU64 not found, even if context ID was around\n", idEvent), VERR_NOT_FOUND);
|
---|
1339 | #endif
|
---|
1340 |
|
---|
1341 | int rc = VINF_SUCCESS;
|
---|
1342 |
|
---|
1343 | PSHCLEVENTPAYLOAD pPayload = NULL;
|
---|
1344 | if (cbData)
|
---|
1345 | rc = ShClPayloadAlloc(idEvent, pvData, cbData, &pPayload);
|
---|
1346 |
|
---|
1347 | if (RT_SUCCESS(rc))
|
---|
1348 | {
|
---|
1349 | RTCritSectEnter(&pClient->CritSect);
|
---|
1350 | rc = ShClEventSignal(&pClient->EventSrc, idEvent, pPayload);
|
---|
1351 | RTCritSectLeave(&pClient->CritSect);
|
---|
1352 | if (RT_FAILURE(rc))
|
---|
1353 | ShClPayloadFree(pPayload);
|
---|
1354 |
|
---|
1355 | /* No one holding a reference to the event anymore? Unregister it. */
|
---|
1356 | if (ShClEventGetRefs(&pClient->EventSrc, idEvent) == 0)
|
---|
1357 | {
|
---|
1358 | int rc2 = ShClEventUnregister(&pClient->EventSrc, idEvent);
|
---|
1359 | if (RT_SUCCESS(rc))
|
---|
1360 | rc = rc2;
|
---|
1361 | }
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | if (RT_FAILURE(rc))
|
---|
1365 | LogRel(("Shared Clipboard: Signalling of guest clipboard data to the host failed with %Rrc\n", rc));
|
---|
1366 |
|
---|
1367 | LogFlowFuncLeaveRC(rc);
|
---|
1368 | return rc;
|
---|
1369 | }
|
---|
1370 |
|
---|
1371 | /**
|
---|
1372 | * Reports available VBox clipboard formats to the guest.
|
---|
1373 | *
|
---|
1374 | * @returns VBox status code.
|
---|
1375 | * @param pClient Client to report clipboard formats to.
|
---|
1376 | * @param fFormats The formats to report (VBOX_SHCL_FMT_XXX), zero
|
---|
1377 | * is okay (empty the clipboard).
|
---|
1378 | */
|
---|
1379 | int ShClSvcHostReportFormats(PSHCLCLIENT pClient, SHCLFORMATS fFormats)
|
---|
1380 | {
|
---|
1381 | AssertPtrReturn(pClient, VERR_INVALID_POINTER);
|
---|
1382 |
|
---|
1383 | LogFlowFunc(("fFormats=%#x\n", fFormats));
|
---|
1384 |
|
---|
1385 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1386 | /*
|
---|
1387 | * If transfer mode is set to disabled, don't report the URI list format to the guest.
|
---|
1388 | */
|
---|
1389 | if (!(g_fTransferMode & VBOX_SHCL_TRANSFER_MODE_ENABLED))
|
---|
1390 | {
|
---|
1391 | LogFlowFunc(("fFormats=%#x -> %#x\n", fFormats, fFormats & ~VBOX_SHCL_FMT_URI_LIST));
|
---|
1392 | fFormats &= ~VBOX_SHCL_FMT_URI_LIST;
|
---|
1393 | }
|
---|
1394 | #endif
|
---|
1395 | LogRel2(("Shared Clipboard: Reporting formats %#x to guest\n", fFormats));
|
---|
1396 |
|
---|
1397 | /*
|
---|
1398 | * Allocate a message, populate parameters and post it to the client.
|
---|
1399 | */
|
---|
1400 | int rc;
|
---|
1401 | PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient, VBOX_SHCL_HOST_MSG_FORMATS_REPORT, 2);
|
---|
1402 | if (pMsg)
|
---|
1403 | {
|
---|
1404 | HGCMSvcSetU32(&pMsg->aParms[0], VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
|
---|
1405 | HGCMSvcSetU32(&pMsg->aParms[1], fFormats);
|
---|
1406 |
|
---|
1407 | RTCritSectEnter(&pClient->CritSect);
|
---|
1408 | shClSvcMsgAddAndWakeupClient(pClient, pMsg);
|
---|
1409 | RTCritSectLeave(&pClient->CritSect);
|
---|
1410 |
|
---|
1411 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1412 | /* If we announce an URI list, create a transfer locally and also tell the guest to create
|
---|
1413 | * a transfer on the guest side. */
|
---|
1414 | if (fFormats & VBOX_SHCL_FMT_URI_LIST)
|
---|
1415 | {
|
---|
1416 | rc = shClSvcTransferStart(pClient, SHCLTRANSFERDIR_TO_REMOTE, SHCLSOURCE_LOCAL,
|
---|
1417 | NULL /* pTransfer */);
|
---|
1418 | if (RT_SUCCESS(rc))
|
---|
1419 | rc = shClSvcSetSource(pClient, SHCLSOURCE_LOCAL);
|
---|
1420 |
|
---|
1421 | if (RT_FAILURE(rc))
|
---|
1422 | LogRel(("Shared Clipboard: Initializing host write transfer failed with %Rrc\n", rc));
|
---|
1423 | }
|
---|
1424 | else
|
---|
1425 | #endif
|
---|
1426 | {
|
---|
1427 | rc = VINF_SUCCESS;
|
---|
1428 | }
|
---|
1429 | }
|
---|
1430 | else
|
---|
1431 | rc = VERR_NO_MEMORY;
|
---|
1432 |
|
---|
1433 | if (RT_FAILURE(rc))
|
---|
1434 | LogRel(("Shared Clipboard: Reporting formats %#x to guest failed with %Rrc\n", fFormats, rc));
|
---|
1435 |
|
---|
1436 | LogFlowFuncLeaveRC(rc);
|
---|
1437 | return rc;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 |
|
---|
1441 | /**
|
---|
1442 | * Handles the VBOX_SHCL_GUEST_FN_REPORT_FORMATS message from the guest.
|
---|
1443 | */
|
---|
1444 | static int shClSvcClientReportFormats(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1445 | {
|
---|
1446 | /*
|
---|
1447 | * Check if the service mode allows this operation and whether the guest is
|
---|
1448 | * supposed to be reading from the host.
|
---|
1449 | */
|
---|
1450 | uint32_t uMode = ShClSvcGetMode();
|
---|
1451 | if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
|
---|
1452 | || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
|
---|
1453 | { /* likely */ }
|
---|
1454 | else
|
---|
1455 | return VERR_ACCESS_DENIED;
|
---|
1456 |
|
---|
1457 | /*
|
---|
1458 | * Digest parameters.
|
---|
1459 | */
|
---|
1460 | ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS
|
---|
1461 | || ( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B
|
---|
1462 | && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
|
---|
1463 | VERR_WRONG_PARAMETER_COUNT);
|
---|
1464 |
|
---|
1465 | uintptr_t iParm = 0;
|
---|
1466 | if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
|
---|
1467 | {
|
---|
1468 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1469 | /* no defined value, so just ignore it */
|
---|
1470 | iParm++;
|
---|
1471 | }
|
---|
1472 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1473 | uint32_t const fFormats = paParms[iParm].u.uint32;
|
---|
1474 | iParm++;
|
---|
1475 | if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
|
---|
1476 | {
|
---|
1477 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1478 | ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
|
---|
1479 | iParm++;
|
---|
1480 | }
|
---|
1481 | Assert(iParm == cParms);
|
---|
1482 |
|
---|
1483 | /*
|
---|
1484 | * Report the formats.
|
---|
1485 | *
|
---|
1486 | * We ignore empty reports if the guest isn't the clipboard owner, this
|
---|
1487 | * prevents a freshly booted guest with an empty clibpoard from clearing
|
---|
1488 | * the host clipboard on startup. Likewise, when a guest shutdown it will
|
---|
1489 | * typically issue an empty report in case it's the owner, we don't want
|
---|
1490 | * that to clear host content either.
|
---|
1491 | */
|
---|
1492 | int rc;
|
---|
1493 | if (!fFormats && pClient->State.enmSource != SHCLSOURCE_REMOTE)
|
---|
1494 | rc = VINF_SUCCESS;
|
---|
1495 | else
|
---|
1496 | {
|
---|
1497 | rc = shClSvcSetSource(pClient, SHCLSOURCE_REMOTE);
|
---|
1498 | if (RT_SUCCESS(rc))
|
---|
1499 | {
|
---|
1500 | if (g_ExtState.pfnExtension)
|
---|
1501 | {
|
---|
1502 | SHCLEXTPARMS parms;
|
---|
1503 | RT_ZERO(parms);
|
---|
1504 | parms.uFormat = fFormats;
|
---|
1505 |
|
---|
1506 | g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof(parms));
|
---|
1507 | }
|
---|
1508 | else
|
---|
1509 | {
|
---|
1510 | rc = ShClBackendFormatAnnounce(pClient, fFormats);
|
---|
1511 | if (RT_FAILURE(rc))
|
---|
1512 | LogRel(("Shared Clipboard: Reporting guest clipboard formats to the host failed with %Rrc\n", rc));
|
---|
1513 | }
|
---|
1514 | }
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 | return rc;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | /**
|
---|
1521 | * Called when the guest wants to read host clipboard data.
|
---|
1522 | * Handles the VBOX_SHCL_GUEST_FN_DATA_READ message.
|
---|
1523 | *
|
---|
1524 | * @returns VBox status code.
|
---|
1525 | * @retval VINF_BUFFER_OVERFLOW if the guest supplied a smaller buffer than needed in order to read the host clipboard data.
|
---|
1526 | * @param pClient Client that wants to read host clipboard data.
|
---|
1527 | * @param cParms Number of HGCM parameters supplied in \a paParms.
|
---|
1528 | * @param paParms Array of HGCM parameters.
|
---|
1529 | */
|
---|
1530 | static int shClSvcClientReadData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1531 | {
|
---|
1532 | LogFlowFuncEnter();
|
---|
1533 |
|
---|
1534 | /*
|
---|
1535 | * Check if the service mode allows this operation and whether the guest is
|
---|
1536 | * supposed to be reading from the host.
|
---|
1537 | */
|
---|
1538 | uint32_t uMode = ShClSvcGetMode();
|
---|
1539 | if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
|
---|
1540 | || uMode == VBOX_SHCL_MODE_HOST_TO_GUEST)
|
---|
1541 | { /* likely */ }
|
---|
1542 | else
|
---|
1543 | return VERR_ACCESS_DENIED;
|
---|
1544 |
|
---|
1545 | /*
|
---|
1546 | * Digest parameters.
|
---|
1547 | *
|
---|
1548 | * We are dragging some legacy here from the 6.1 dev cycle, a 5 parameter
|
---|
1549 | * variant which prepends a 64-bit context ID (RAZ as meaning not defined),
|
---|
1550 | * a 32-bit flag (MBZ, no defined meaning) and switches the last two parameters.
|
---|
1551 | */
|
---|
1552 | ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_DATA_READ
|
---|
1553 | || ( cParms == VBOX_SHCL_CPARMS_DATA_READ_61B
|
---|
1554 | && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
|
---|
1555 | VERR_WRONG_PARAMETER_COUNT);
|
---|
1556 |
|
---|
1557 | uintptr_t iParm = 0;
|
---|
1558 | SHCLCLIENTCMDCTX cmdCtx;
|
---|
1559 | RT_ZERO(cmdCtx);
|
---|
1560 | if (cParms == VBOX_SHCL_CPARMS_DATA_READ_61B)
|
---|
1561 | {
|
---|
1562 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1563 | /* This has no defined meaning and was never used, however the guest passed stuff, so ignore it and leave idContext=0. */
|
---|
1564 | iParm++;
|
---|
1565 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1566 | ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
|
---|
1567 | iParm++;
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
|
---|
1571 | uint32_t cbData = 0;
|
---|
1572 | void *pvData = NULL;
|
---|
1573 |
|
---|
1574 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1575 | uFormat = paParms[iParm].u.uint32;
|
---|
1576 | iParm++;
|
---|
1577 | if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
|
---|
1578 | {
|
---|
1579 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
|
---|
1580 | pvData = paParms[iParm].u.pointer.addr;
|
---|
1581 | cbData = paParms[iParm].u.pointer.size;
|
---|
1582 | iParm++;
|
---|
1583 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
|
---|
1584 | iParm++;
|
---|
1585 | }
|
---|
1586 | else
|
---|
1587 | {
|
---|
1588 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
|
---|
1589 | iParm++;
|
---|
1590 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
|
---|
1591 | pvData = paParms[iParm].u.pointer.addr;
|
---|
1592 | cbData = paParms[iParm].u.pointer.size;
|
---|
1593 | iParm++;
|
---|
1594 | }
|
---|
1595 | Assert(iParm == cParms);
|
---|
1596 |
|
---|
1597 | /*
|
---|
1598 | * For some reason we need to do this (makes absolutely no sense to bird).
|
---|
1599 | */
|
---|
1600 | /** @todo r=bird: I really don't get why you need the State.POD.uFormat
|
---|
1601 | * member. I'm sure there is a reason. Incomplete code? */
|
---|
1602 | if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
|
---|
1603 | {
|
---|
1604 | if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
|
---|
1605 | pClient->State.POD.uFormat = uFormat;
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | LogRel2(("Shared Clipboard: Guest wants to read %RU32 bytes host clipboard data in format %RU32\n", cbData, uFormat));
|
---|
1609 |
|
---|
1610 | /*
|
---|
1611 | * Do the reading.
|
---|
1612 | */
|
---|
1613 | int rc;
|
---|
1614 | uint32_t cbActual = 0;
|
---|
1615 |
|
---|
1616 | /* If there is a service extension active, try reading data from it first. */
|
---|
1617 | if (g_ExtState.pfnExtension)
|
---|
1618 | {
|
---|
1619 | SHCLEXTPARMS parms;
|
---|
1620 | RT_ZERO(parms);
|
---|
1621 |
|
---|
1622 | parms.uFormat = uFormat;
|
---|
1623 | parms.u.pvData = pvData;
|
---|
1624 | parms.cbData = cbData;
|
---|
1625 |
|
---|
1626 | g_ExtState.fReadingData = true;
|
---|
1627 |
|
---|
1628 | /* Read clipboard data from the extension. */
|
---|
1629 | rc = g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ, &parms, sizeof(parms));
|
---|
1630 |
|
---|
1631 | LogRel2(("Shared Clipboard: Read extension clipboard data (fDelayedAnnouncement=%RTbool, fDelayedFormats=%#x, max %RU32 bytes), got %RU32 bytes: rc=%Rrc\n",
|
---|
1632 | g_ExtState.fDelayedAnnouncement, g_ExtState.fDelayedFormats, cbData, parms.cbData, rc));
|
---|
1633 |
|
---|
1634 | /* Did the extension send the clipboard formats yet?
|
---|
1635 | * Otherwise, do this now. */
|
---|
1636 | if (g_ExtState.fDelayedAnnouncement)
|
---|
1637 | {
|
---|
1638 | int rc2 = ShClSvcHostReportFormats(pClient, g_ExtState.fDelayedFormats);
|
---|
1639 | AssertRC(rc2);
|
---|
1640 |
|
---|
1641 | g_ExtState.fDelayedAnnouncement = false;
|
---|
1642 | g_ExtState.fDelayedFormats = 0;
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | g_ExtState.fReadingData = false;
|
---|
1646 |
|
---|
1647 | if (RT_SUCCESS(rc))
|
---|
1648 | cbActual = parms.cbData;
|
---|
1649 | }
|
---|
1650 | else
|
---|
1651 | {
|
---|
1652 | rc = ShClBackendReadData(pClient, &cmdCtx, uFormat, pvData, cbData, &cbActual);
|
---|
1653 | if (RT_SUCCESS(rc))
|
---|
1654 | {
|
---|
1655 | LogRel2(("Shared Clipboard: Read host clipboard data (max %RU32 bytes), got %RU32 bytes\n", cbData, cbActual));
|
---|
1656 | }
|
---|
1657 | else
|
---|
1658 | LogRel(("Shared Clipboard: Reading host clipboard data failed with %Rrc\n", rc));
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | if (RT_SUCCESS(rc))
|
---|
1662 | {
|
---|
1663 | /* Return the actual size required to fullfil the request. */
|
---|
1664 | if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
|
---|
1665 | HGCMSvcSetU32(&paParms[2], cbActual);
|
---|
1666 | else
|
---|
1667 | HGCMSvcSetU32(&paParms[3], cbActual);
|
---|
1668 |
|
---|
1669 | /* If the data to return exceeds the buffer the guest supplies, tell it (and let it try again). */
|
---|
1670 | if (cbActual >= cbData)
|
---|
1671 | rc = VINF_BUFFER_OVERFLOW;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | LogFlowFuncLeaveRC(rc);
|
---|
1675 | return rc;
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | /**
|
---|
1679 | * Called when the guest writes clipboard data to the host.
|
---|
1680 | * Handles the VBOX_SHCL_GUEST_FN_DATA_WRITE message.
|
---|
1681 | *
|
---|
1682 | * @returns VBox status code.
|
---|
1683 | * @param pClient Client that wants to read host clipboard data.
|
---|
1684 | * @param cParms Number of HGCM parameters supplied in \a paParms.
|
---|
1685 | * @param paParms Array of HGCM parameters.
|
---|
1686 | */
|
---|
1687 | int shClSvcClientWriteData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1688 | {
|
---|
1689 | LogFlowFuncEnter();
|
---|
1690 |
|
---|
1691 | /*
|
---|
1692 | * Check if the service mode allows this operation and whether the guest is
|
---|
1693 | * supposed to be reading from the host.
|
---|
1694 | */
|
---|
1695 | uint32_t uMode = ShClSvcGetMode();
|
---|
1696 | if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
|
---|
1697 | || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
|
---|
1698 | { /* likely */ }
|
---|
1699 | else
|
---|
1700 | return VERR_ACCESS_DENIED;
|
---|
1701 |
|
---|
1702 | const bool fReportsContextID = RT_BOOL(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID);
|
---|
1703 |
|
---|
1704 | /*
|
---|
1705 | * Digest parameters.
|
---|
1706 | *
|
---|
1707 | * There are 3 different format here, formatunately no parameters have been
|
---|
1708 | * switch around so it's plain sailing compared to the DATA_READ message.
|
---|
1709 | */
|
---|
1710 | ASSERT_GUEST_RETURN(fReportsContextID
|
---|
1711 | ? cParms == VBOX_SHCL_CPARMS_DATA_WRITE || cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B
|
---|
1712 | : cParms == VBOX_SHCL_CPARMS_DATA_WRITE_OLD,
|
---|
1713 | VERR_WRONG_PARAMETER_COUNT);
|
---|
1714 |
|
---|
1715 | uintptr_t iParm = 0;
|
---|
1716 | SHCLCLIENTCMDCTX cmdCtx;
|
---|
1717 | RT_ZERO(cmdCtx);
|
---|
1718 | if (cParms > VBOX_SHCL_CPARMS_DATA_WRITE_OLD)
|
---|
1719 | {
|
---|
1720 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1721 | cmdCtx.uContextID = paParms[iParm].u.uint64;
|
---|
1722 | iParm++;
|
---|
1723 | }
|
---|
1724 | else
|
---|
1725 | {
|
---|
1726 | /* Older Guest Additions (< 6.1) did not supply a context ID.
|
---|
1727 | * We dig it out from our saved context ID list then a bit down below. */
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
|
---|
1731 | {
|
---|
1732 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
|
---|
1733 | ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
|
---|
1734 | iParm++;
|
---|
1735 | }
|
---|
1736 |
|
---|
1737 | SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
|
---|
1738 | uint32_t cbData = 0;
|
---|
1739 | void *pvData = NULL;
|
---|
1740 |
|
---|
1741 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* Format bit. */
|
---|
1742 | uFormat = paParms[iParm].u.uint32;
|
---|
1743 | iParm++;
|
---|
1744 | if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
|
---|
1745 | {
|
---|
1746 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* "cbData" - duplicates buffer size. */
|
---|
1747 | iParm++;
|
---|
1748 | }
|
---|
1749 | ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
|
---|
1750 | pvData = paParms[iParm].u.pointer.addr;
|
---|
1751 | cbData = paParms[iParm].u.pointer.size;
|
---|
1752 | iParm++;
|
---|
1753 | Assert(iParm == cParms);
|
---|
1754 |
|
---|
1755 | /*
|
---|
1756 | * Handle / check context ID.
|
---|
1757 | */
|
---|
1758 | if (!fReportsContextID) /* Do we have to deal with old(er) GAs (< 6.1) which don't support context IDs? Dig out the context ID then. */
|
---|
1759 | {
|
---|
1760 | PSHCLCLIENTLEGACYCID pCID = NULL;
|
---|
1761 | PSHCLCLIENTLEGACYCID pCIDIter;
|
---|
1762 | RTListForEach(&pClient->Legacy.lstCID, pCIDIter, SHCLCLIENTLEGACYCID, Node) /* Slow, but does the job for now. */
|
---|
1763 | {
|
---|
1764 | if (pCIDIter->uFormat == uFormat)
|
---|
1765 | {
|
---|
1766 | pCID = pCIDIter;
|
---|
1767 | break;
|
---|
1768 | }
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | ASSERT_GUEST_MSG_RETURN(pCID != NULL, ("Context ID for format %#x not found\n", uFormat), VERR_INVALID_CONTEXT);
|
---|
1772 | cmdCtx.uContextID = pCID->uCID;
|
---|
1773 |
|
---|
1774 | /* Not needed anymore; clean up. */
|
---|
1775 | Assert(pClient->Legacy.cCID);
|
---|
1776 | pClient->Legacy.cCID--;
|
---|
1777 | RTListNodeRemove(&pCID->Node);
|
---|
1778 | RTMemFree(pCID);
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | uint64_t const idCtxExpected = VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID, pClient->EventSrc.uID,
|
---|
1782 | VBOX_SHCL_CONTEXTID_GET_EVENT(cmdCtx.uContextID));
|
---|
1783 | ASSERT_GUEST_MSG_RETURN(cmdCtx.uContextID == idCtxExpected,
|
---|
1784 | ("Wrong context ID: %#RX64, expected %#RX64\n", cmdCtx.uContextID, idCtxExpected),
|
---|
1785 | VERR_INVALID_CONTEXT);
|
---|
1786 |
|
---|
1787 | /*
|
---|
1788 | * For some reason we need to do this (makes absolutely no sense to bird).
|
---|
1789 | */
|
---|
1790 | /** @todo r=bird: I really don't get why you need the State.POD.uFormat
|
---|
1791 | * member. I'm sure there is a reason. Incomplete code? */
|
---|
1792 | if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
|
---|
1793 | {
|
---|
1794 | if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
|
---|
1795 | pClient->State.POD.uFormat = uFormat;
|
---|
1796 | }
|
---|
1797 |
|
---|
1798 | LogRel2(("Shared Clipboard: Guest writes %RU32 bytes clipboard data in format %RU32 to host\n", cbData, uFormat));
|
---|
1799 |
|
---|
1800 | /*
|
---|
1801 | * Write the data to the active host side clipboard.
|
---|
1802 | */
|
---|
1803 | int rc;
|
---|
1804 | if (g_ExtState.pfnExtension)
|
---|
1805 | {
|
---|
1806 | SHCLEXTPARMS parms;
|
---|
1807 | RT_ZERO(parms);
|
---|
1808 | parms.uFormat = uFormat;
|
---|
1809 | parms.u.pvData = pvData;
|
---|
1810 | parms.cbData = cbData;
|
---|
1811 |
|
---|
1812 | g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof(parms));
|
---|
1813 | rc = VINF_SUCCESS;
|
---|
1814 | }
|
---|
1815 | else
|
---|
1816 | {
|
---|
1817 | /* Let the backend implementation know. */
|
---|
1818 | rc = ShClBackendWriteData(pClient, &cmdCtx, uFormat, pvData, cbData);
|
---|
1819 | if (RT_FAILURE(rc))
|
---|
1820 | LogRel(("Shared Clipboard: Writing guest clipboard data to the host failed with %Rrc\n", rc));
|
---|
1821 |
|
---|
1822 | int rc2; /* Don't return internals back to the guest. */
|
---|
1823 | rc2 = ShClSvcGuestDataSignal(pClient, &cmdCtx, uFormat, pvData, cbData); /* To complete pending events, if any. */
|
---|
1824 | if (RT_FAILURE(rc2))
|
---|
1825 | LogRel(("Shared Clipboard: Signalling host about guest clipboard data failed with %Rrc\n", rc2));
|
---|
1826 | AssertRC(rc2);
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | LogFlowFuncLeaveRC(rc);
|
---|
1830 | return rc;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | /**
|
---|
1834 | * Gets an error from HGCM service parameters.
|
---|
1835 | *
|
---|
1836 | * @returns VBox status code.
|
---|
1837 | * @param cParms Number of HGCM parameters supplied in \a paParms.
|
---|
1838 | * @param paParms Array of HGCM parameters.
|
---|
1839 | * @param pRc Where to store the received error code.
|
---|
1840 | */
|
---|
1841 | static int shClSvcClientError(uint32_t cParms, VBOXHGCMSVCPARM paParms[], int *pRc)
|
---|
1842 | {
|
---|
1843 | AssertPtrReturn(paParms, VERR_INVALID_PARAMETER);
|
---|
1844 | AssertPtrReturn(pRc, VERR_INVALID_PARAMETER);
|
---|
1845 |
|
---|
1846 | int rc;
|
---|
1847 |
|
---|
1848 | if (cParms == VBOX_SHCL_CPARMS_ERROR)
|
---|
1849 | {
|
---|
1850 | rc = HGCMSvcGetU32(&paParms[1], (uint32_t *)pRc); /** @todo int vs. uint32_t !!! */
|
---|
1851 | }
|
---|
1852 | else
|
---|
1853 | rc = VERR_INVALID_PARAMETER;
|
---|
1854 |
|
---|
1855 | LogFlowFuncLeaveRC(rc);
|
---|
1856 | return rc;
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | /**
|
---|
1860 | * Sets the transfer source type of a Shared Clipboard client.
|
---|
1861 | *
|
---|
1862 | * @returns VBox status code.
|
---|
1863 | * @param pClient Client to set transfer source type for.
|
---|
1864 | * @param enmSource Source type to set.
|
---|
1865 | */
|
---|
1866 | int shClSvcSetSource(PSHCLCLIENT pClient, SHCLSOURCE enmSource)
|
---|
1867 | {
|
---|
1868 | if (!pClient) /* If no client connected (anymore), bail out. */
|
---|
1869 | return VINF_SUCCESS;
|
---|
1870 |
|
---|
1871 | int rc = VINF_SUCCESS;
|
---|
1872 |
|
---|
1873 | if (ShClSvcLock())
|
---|
1874 | {
|
---|
1875 | pClient->State.enmSource = enmSource;
|
---|
1876 |
|
---|
1877 | LogFlowFunc(("Source of client %RU32 is now %RU32\n", pClient->State.uClientID, pClient->State.enmSource));
|
---|
1878 |
|
---|
1879 | ShClSvcUnlock();
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | LogFlowFuncLeaveRC(rc);
|
---|
1883 | return rc;
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 | static int svcInit(void)
|
---|
1887 | {
|
---|
1888 | int rc = RTCritSectInit(&g_CritSect);
|
---|
1889 |
|
---|
1890 | if (RT_SUCCESS(rc))
|
---|
1891 | {
|
---|
1892 | shClSvcModeSet(VBOX_SHCL_MODE_OFF);
|
---|
1893 |
|
---|
1894 | rc = ShClBackendInit();
|
---|
1895 |
|
---|
1896 | /* Clean up on failure, because 'svnUnload' will not be called
|
---|
1897 | * if the 'svcInit' returns an error.
|
---|
1898 | */
|
---|
1899 | if (RT_FAILURE(rc))
|
---|
1900 | {
|
---|
1901 | RTCritSectDelete(&g_CritSect);
|
---|
1902 | }
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 | return rc;
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | static DECLCALLBACK(int) svcUnload(void *)
|
---|
1909 | {
|
---|
1910 | LogFlowFuncEnter();
|
---|
1911 |
|
---|
1912 | ShClBackendDestroy();
|
---|
1913 |
|
---|
1914 | RTCritSectDelete(&g_CritSect);
|
---|
1915 |
|
---|
1916 | return VINF_SUCCESS;
|
---|
1917 | }
|
---|
1918 |
|
---|
1919 | static DECLCALLBACK(int) svcDisconnect(void *, uint32_t u32ClientID, void *pvClient)
|
---|
1920 | {
|
---|
1921 | RT_NOREF(u32ClientID);
|
---|
1922 |
|
---|
1923 | LogFunc(("u32ClientID=%RU32\n", u32ClientID));
|
---|
1924 |
|
---|
1925 | PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
|
---|
1926 | AssertPtr(pClient);
|
---|
1927 |
|
---|
1928 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1929 | shClSvcClientTransfersReset(pClient);
|
---|
1930 | #endif
|
---|
1931 |
|
---|
1932 | ShClBackendDisconnect(pClient);
|
---|
1933 |
|
---|
1934 | shClSvcClientDestroy(pClient);
|
---|
1935 |
|
---|
1936 | return VINF_SUCCESS;
|
---|
1937 | }
|
---|
1938 |
|
---|
1939 | static DECLCALLBACK(int) svcConnect(void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
|
---|
1940 | {
|
---|
1941 | RT_NOREF(fRequestor, fRestoring);
|
---|
1942 |
|
---|
1943 | PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
|
---|
1944 | AssertPtr(pvClient);
|
---|
1945 |
|
---|
1946 | int rc = shClSvcClientInit(pClient, u32ClientID);
|
---|
1947 | if (RT_SUCCESS(rc))
|
---|
1948 | {
|
---|
1949 | rc = ShClBackendConnect(pClient, ShClSvcGetHeadless());
|
---|
1950 | if (RT_SUCCESS(rc))
|
---|
1951 | {
|
---|
1952 | /* Sync the host clipboard content with the client. */
|
---|
1953 | rc = ShClBackendSync(pClient);
|
---|
1954 | if (rc == VINF_NO_CHANGE)
|
---|
1955 | {
|
---|
1956 | /*
|
---|
1957 | * The sync could return VINF_NO_CHANGE if nothing has changed on the host, but older
|
---|
1958 | * Guest Additions rely on the fact that only VINF_SUCCESS indicates a successful connect
|
---|
1959 | * to the host service (instead of using RT_SUCCESS()).
|
---|
1960 | *
|
---|
1961 | * So implicitly set VINF_SUCCESS here to not break older Guest Additions.
|
---|
1962 | */
|
---|
1963 | rc = VINF_SUCCESS;
|
---|
1964 | }
|
---|
1965 |
|
---|
1966 | if (RT_SUCCESS(rc))
|
---|
1967 | {
|
---|
1968 | /* Assign weak pointer to client map .*/
|
---|
1969 | g_mapClients[u32ClientID] = pClient; /** @todo Handle OOM / collisions? */
|
---|
1970 |
|
---|
1971 | /* For now we ASSUME that the first client ever connected is in charge for
|
---|
1972 | * communicating withe the service extension.
|
---|
1973 | *
|
---|
1974 | ** @todo This needs to be fixed ASAP w/o breaking older guest / host combos. */
|
---|
1975 | if (g_ExtState.uClientID == 0)
|
---|
1976 | g_ExtState.uClientID = u32ClientID;
|
---|
1977 | }
|
---|
1978 | }
|
---|
1979 | }
|
---|
1980 |
|
---|
1981 | LogFlowFuncLeaveRC(rc);
|
---|
1982 | return rc;
|
---|
1983 | }
|
---|
1984 |
|
---|
1985 | static DECLCALLBACK(void) svcCall(void *,
|
---|
1986 | VBOXHGCMCALLHANDLE callHandle,
|
---|
1987 | uint32_t u32ClientID,
|
---|
1988 | void *pvClient,
|
---|
1989 | uint32_t u32Function,
|
---|
1990 | uint32_t cParms,
|
---|
1991 | VBOXHGCMSVCPARM paParms[],
|
---|
1992 | uint64_t tsArrival)
|
---|
1993 | {
|
---|
1994 | RT_NOREF(u32ClientID, pvClient, tsArrival);
|
---|
1995 | PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
|
---|
1996 | AssertPtr(pClient);
|
---|
1997 |
|
---|
1998 | #ifdef LOG_ENABLED
|
---|
1999 | LogFunc(("u32ClientID=%RU32, fn=%RU32 (%s), cParms=%RU32, paParms=%p\n",
|
---|
2000 | u32ClientID, u32Function, ShClGuestMsgToStr(u32Function), cParms, paParms));
|
---|
2001 | for (uint32_t i = 0; i < cParms; i++)
|
---|
2002 | {
|
---|
2003 | switch (paParms[i].type)
|
---|
2004 | {
|
---|
2005 | case VBOX_HGCM_SVC_PARM_32BIT:
|
---|
2006 | LogFunc((" paParms[%RU32]: type uint32_t - value %RU32\n", i, paParms[i].u.uint32));
|
---|
2007 | break;
|
---|
2008 | case VBOX_HGCM_SVC_PARM_64BIT:
|
---|
2009 | LogFunc((" paParms[%RU32]: type uint64_t - value %RU64\n", i, paParms[i].u.uint64));
|
---|
2010 | break;
|
---|
2011 | case VBOX_HGCM_SVC_PARM_PTR:
|
---|
2012 | LogFunc((" paParms[%RU32]: type ptr - value 0x%p (%RU32 bytes)\n",
|
---|
2013 | i, paParms[i].u.pointer.addr, paParms[i].u.pointer.size));
|
---|
2014 | break;
|
---|
2015 | case VBOX_HGCM_SVC_PARM_PAGES:
|
---|
2016 | LogFunc((" paParms[%RU32]: type pages - cb=%RU32, cPages=%RU16\n",
|
---|
2017 | i, paParms[i].u.Pages.cb, paParms[i].u.Pages.cPages));
|
---|
2018 | break;
|
---|
2019 | default:
|
---|
2020 | AssertFailed();
|
---|
2021 | }
|
---|
2022 | }
|
---|
2023 | LogFunc(("Client state: fFlags=0x%x, fGuestFeatures0=0x%x, fGuestFeatures1=0x%x\n",
|
---|
2024 | pClient->State.fFlags, pClient->State.fGuestFeatures0, pClient->State.fGuestFeatures1));
|
---|
2025 | #endif
|
---|
2026 |
|
---|
2027 | int rc;
|
---|
2028 | switch (u32Function)
|
---|
2029 | {
|
---|
2030 | case VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT:
|
---|
2031 | RTCritSectEnter(&pClient->CritSect);
|
---|
2032 | rc = shClSvcClientMsgOldGet(pClient, callHandle, cParms, paParms);
|
---|
2033 | RTCritSectLeave(&pClient->CritSect);
|
---|
2034 | break;
|
---|
2035 |
|
---|
2036 | case VBOX_SHCL_GUEST_FN_CONNECT:
|
---|
2037 | LogRel(("Shared Clipboard: 6.1.0 beta or rc Guest Additions detected. Please upgrade!\n"));
|
---|
2038 | rc = VERR_NOT_IMPLEMENTED;
|
---|
2039 | break;
|
---|
2040 |
|
---|
2041 | case VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE:
|
---|
2042 | rc = shClSvcClientNegogiateChunkSize(pClient, callHandle, cParms, paParms);
|
---|
2043 | break;
|
---|
2044 |
|
---|
2045 | case VBOX_SHCL_GUEST_FN_REPORT_FEATURES:
|
---|
2046 | rc = shClSvcClientReportFeatures(pClient, callHandle, cParms, paParms);
|
---|
2047 | break;
|
---|
2048 |
|
---|
2049 | case VBOX_SHCL_GUEST_FN_QUERY_FEATURES:
|
---|
2050 | rc = shClSvcClientQueryFeatures(callHandle, cParms, paParms);
|
---|
2051 | break;
|
---|
2052 |
|
---|
2053 | case VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT:
|
---|
2054 | RTCritSectEnter(&pClient->CritSect);
|
---|
2055 | rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, false /*fWait*/);
|
---|
2056 | RTCritSectLeave(&pClient->CritSect);
|
---|
2057 | break;
|
---|
2058 |
|
---|
2059 | case VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT:
|
---|
2060 | RTCritSectEnter(&pClient->CritSect);
|
---|
2061 | rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, true /*fWait*/);
|
---|
2062 | RTCritSectLeave(&pClient->CritSect);
|
---|
2063 | break;
|
---|
2064 |
|
---|
2065 | case VBOX_SHCL_GUEST_FN_MSG_GET:
|
---|
2066 | RTCritSectEnter(&pClient->CritSect);
|
---|
2067 | rc = shClSvcClientMsgGet(pClient, callHandle, cParms, paParms);
|
---|
2068 | RTCritSectLeave(&pClient->CritSect);
|
---|
2069 | break;
|
---|
2070 |
|
---|
2071 | case VBOX_SHCL_GUEST_FN_MSG_CANCEL:
|
---|
2072 | RTCritSectEnter(&pClient->CritSect);
|
---|
2073 | rc = shClSvcClientMsgCancel(pClient, cParms);
|
---|
2074 | RTCritSectLeave(&pClient->CritSect);
|
---|
2075 | break;
|
---|
2076 |
|
---|
2077 | case VBOX_SHCL_GUEST_FN_REPORT_FORMATS:
|
---|
2078 | rc = shClSvcClientReportFormats(pClient, cParms, paParms);
|
---|
2079 | break;
|
---|
2080 |
|
---|
2081 | case VBOX_SHCL_GUEST_FN_DATA_READ:
|
---|
2082 | rc = shClSvcClientReadData(pClient, cParms, paParms);
|
---|
2083 | break;
|
---|
2084 |
|
---|
2085 | case VBOX_SHCL_GUEST_FN_DATA_WRITE:
|
---|
2086 | rc = shClSvcClientWriteData(pClient, cParms, paParms);
|
---|
2087 | break;
|
---|
2088 |
|
---|
2089 | case VBOX_SHCL_GUEST_FN_ERROR:
|
---|
2090 | {
|
---|
2091 | int rcGuest;
|
---|
2092 | rc = shClSvcClientError(cParms,paParms, &rcGuest);
|
---|
2093 | if (RT_SUCCESS(rc))
|
---|
2094 | {
|
---|
2095 | LogRel(("Shared Clipboard: Error from guest side: %Rrc\n", rcGuest));
|
---|
2096 |
|
---|
2097 | /* Reset client state and start over. */
|
---|
2098 | shclSvcClientStateReset(&pClient->State);
|
---|
2099 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
2100 | shClSvcClientTransfersReset(pClient);
|
---|
2101 | #endif
|
---|
2102 | }
|
---|
2103 | break;
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | default:
|
---|
2107 | {
|
---|
2108 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
2109 | if ( u32Function <= VBOX_SHCL_GUEST_FN_LAST
|
---|
2110 | && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID) )
|
---|
2111 | {
|
---|
2112 | if (g_fTransferMode & VBOX_SHCL_TRANSFER_MODE_ENABLED)
|
---|
2113 | rc = shClSvcTransferHandler(pClient, callHandle, u32Function, cParms, paParms, tsArrival);
|
---|
2114 | else
|
---|
2115 | {
|
---|
2116 | LogRel2(("Shared Clipboard: File transfers are disabled for this VM\n"));
|
---|
2117 | rc = VERR_ACCESS_DENIED;
|
---|
2118 | }
|
---|
2119 | }
|
---|
2120 | else
|
---|
2121 | #endif
|
---|
2122 | {
|
---|
2123 | LogRel2(("Shared Clipboard: Unknown guest function: %u (%#x)\n", u32Function, u32Function));
|
---|
2124 | rc = VERR_NOT_IMPLEMENTED;
|
---|
2125 | }
|
---|
2126 | break;
|
---|
2127 | }
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
|
---|
2131 |
|
---|
2132 | if (rc != VINF_HGCM_ASYNC_EXECUTE)
|
---|
2133 | g_pHelpers->pfnCallComplete(callHandle, rc);
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | /**
|
---|
2137 | * Initializes a Shared Clipboard service's client state.
|
---|
2138 | *
|
---|
2139 | * @returns VBox status code.
|
---|
2140 | * @param pClientState Client state to initialize.
|
---|
2141 | * @param uClientID Client ID (HGCM) to use for this client state.
|
---|
2142 | */
|
---|
2143 | int shClSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID)
|
---|
2144 | {
|
---|
2145 | LogFlowFuncEnter();
|
---|
2146 |
|
---|
2147 | shclSvcClientStateReset(pClientState);
|
---|
2148 |
|
---|
2149 | /* Register the client. */
|
---|
2150 | pClientState->uClientID = uClientID;
|
---|
2151 |
|
---|
2152 | return VINF_SUCCESS;
|
---|
2153 | }
|
---|
2154 |
|
---|
2155 | /**
|
---|
2156 | * Destroys a Shared Clipboard service's client state.
|
---|
2157 | *
|
---|
2158 | * @returns VBox status code.
|
---|
2159 | * @param pClientState Client state to destroy.
|
---|
2160 | */
|
---|
2161 | int shClSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState)
|
---|
2162 | {
|
---|
2163 | RT_NOREF(pClientState);
|
---|
2164 |
|
---|
2165 | LogFlowFuncEnter();
|
---|
2166 |
|
---|
2167 | return VINF_SUCCESS;
|
---|
2168 | }
|
---|
2169 |
|
---|
2170 | /**
|
---|
2171 | * Resets a Shared Clipboard service's client state.
|
---|
2172 | *
|
---|
2173 | * @param pClientState Client state to reset.
|
---|
2174 | */
|
---|
2175 | void shclSvcClientStateReset(PSHCLCLIENTSTATE pClientState)
|
---|
2176 | {
|
---|
2177 | LogFlowFuncEnter();
|
---|
2178 |
|
---|
2179 | pClientState->fGuestFeatures0 = VBOX_SHCL_GF_NONE;
|
---|
2180 | pClientState->fGuestFeatures1 = VBOX_SHCL_GF_NONE;
|
---|
2181 |
|
---|
2182 | pClientState->cbChunkSize = VBOX_SHCL_DEFAULT_CHUNK_SIZE; /** @todo Make this configurable. */
|
---|
2183 | pClientState->enmSource = SHCLSOURCE_INVALID;
|
---|
2184 | pClientState->fFlags = SHCLCLIENTSTATE_FLAGS_NONE;
|
---|
2185 |
|
---|
2186 | pClientState->POD.enmDir = SHCLTRANSFERDIR_UNKNOWN;
|
---|
2187 | pClientState->POD.uFormat = VBOX_SHCL_FMT_NONE;
|
---|
2188 | pClientState->POD.cbToReadWriteTotal = 0;
|
---|
2189 | pClientState->POD.cbReadWritten = 0;
|
---|
2190 |
|
---|
2191 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
2192 | pClientState->Transfers.enmTransferDir = SHCLTRANSFERDIR_UNKNOWN;
|
---|
2193 | #endif
|
---|
2194 | }
|
---|
2195 |
|
---|
2196 | /*
|
---|
2197 | * We differentiate between a function handler for the guest and one for the host.
|
---|
2198 | */
|
---|
2199 | static DECLCALLBACK(int) svcHostCall(void *,
|
---|
2200 | uint32_t u32Function,
|
---|
2201 | uint32_t cParms,
|
---|
2202 | VBOXHGCMSVCPARM paParms[])
|
---|
2203 | {
|
---|
2204 | int rc = VINF_SUCCESS;
|
---|
2205 |
|
---|
2206 | LogFlowFunc(("u32Function=%RU32 (%s), cParms=%RU32, paParms=%p\n",
|
---|
2207 | u32Function, ShClHostFunctionToStr(u32Function), cParms, paParms));
|
---|
2208 |
|
---|
2209 | switch (u32Function)
|
---|
2210 | {
|
---|
2211 | case VBOX_SHCL_HOST_FN_SET_MODE:
|
---|
2212 | {
|
---|
2213 | if (cParms != 1)
|
---|
2214 | {
|
---|
2215 | rc = VERR_INVALID_PARAMETER;
|
---|
2216 | }
|
---|
2217 | else
|
---|
2218 | {
|
---|
2219 | uint32_t u32Mode = VBOX_SHCL_MODE_OFF;
|
---|
2220 |
|
---|
2221 | rc = HGCMSvcGetU32(&paParms[0], &u32Mode);
|
---|
2222 | if (RT_SUCCESS(rc))
|
---|
2223 | rc = shClSvcModeSet(u32Mode);
|
---|
2224 | }
|
---|
2225 |
|
---|
2226 | break;
|
---|
2227 | }
|
---|
2228 |
|
---|
2229 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
2230 | case VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE:
|
---|
2231 | {
|
---|
2232 | if (cParms != 1)
|
---|
2233 | {
|
---|
2234 | rc = VERR_INVALID_PARAMETER;
|
---|
2235 | }
|
---|
2236 | else
|
---|
2237 | {
|
---|
2238 | uint32_t fTransferMode;
|
---|
2239 | rc = HGCMSvcGetU32(&paParms[0], &fTransferMode);
|
---|
2240 | if (RT_SUCCESS(rc))
|
---|
2241 | rc = shClSvcTransferModeSet(fTransferMode);
|
---|
2242 | }
|
---|
2243 | break;
|
---|
2244 | }
|
---|
2245 | #endif
|
---|
2246 | case VBOX_SHCL_HOST_FN_SET_HEADLESS:
|
---|
2247 | {
|
---|
2248 | if (cParms != 1)
|
---|
2249 | {
|
---|
2250 | rc = VERR_INVALID_PARAMETER;
|
---|
2251 | }
|
---|
2252 | else
|
---|
2253 | {
|
---|
2254 | uint32_t uHeadless;
|
---|
2255 | rc = HGCMSvcGetU32(&paParms[0], &uHeadless);
|
---|
2256 | if (RT_SUCCESS(rc))
|
---|
2257 | {
|
---|
2258 | g_fHeadless = RT_BOOL(uHeadless);
|
---|
2259 | LogRel(("Shared Clipboard: Service running in %s mode\n", g_fHeadless ? "headless" : "normal"));
|
---|
2260 | }
|
---|
2261 | }
|
---|
2262 | break;
|
---|
2263 | }
|
---|
2264 |
|
---|
2265 | default:
|
---|
2266 | {
|
---|
2267 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
2268 | rc = shClSvcTransferHostHandler(u32Function, cParms, paParms);
|
---|
2269 | #else
|
---|
2270 | rc = VERR_NOT_IMPLEMENTED;
|
---|
2271 | #endif
|
---|
2272 | break;
|
---|
2273 | }
|
---|
2274 | }
|
---|
2275 |
|
---|
2276 | LogFlowFuncLeaveRC(rc);
|
---|
2277 | return rc;
|
---|
2278 | }
|
---|
2279 |
|
---|
2280 | #ifndef UNIT_TEST
|
---|
2281 |
|
---|
2282 | /**
|
---|
2283 | * SSM descriptor table for the SHCLCLIENTLEGACYCID structure.
|
---|
2284 | *
|
---|
2285 | * @note Saving the ListEntry attribute is not necessary, as this gets used on runtime only.
|
---|
2286 | */
|
---|
2287 | static SSMFIELD const s_aShClSSMClientLegacyCID[] =
|
---|
2288 | {
|
---|
2289 | SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uCID),
|
---|
2290 | SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, enmType),
|
---|
2291 | SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uFormat),
|
---|
2292 | SSMFIELD_ENTRY_TERM()
|
---|
2293 | };
|
---|
2294 |
|
---|
2295 | /**
|
---|
2296 | * SSM descriptor table for the SHCLCLIENTSTATE structure.
|
---|
2297 | *
|
---|
2298 | * @note Saving the session ID not necessary, as they're not persistent across
|
---|
2299 | * state save/restore.
|
---|
2300 | */
|
---|
2301 | static SSMFIELD const s_aShClSSMClientState[] =
|
---|
2302 | {
|
---|
2303 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
|
---|
2304 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
|
---|
2305 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
|
---|
2306 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
|
---|
2307 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, fFlags),
|
---|
2308 | SSMFIELD_ENTRY_TERM()
|
---|
2309 | };
|
---|
2310 |
|
---|
2311 | /**
|
---|
2312 | * VBox 6.1 Beta 1 version of s_aShClSSMClientState (no flags).
|
---|
2313 | */
|
---|
2314 | static SSMFIELD const s_aShClSSMClientState61B1[] =
|
---|
2315 | {
|
---|
2316 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
|
---|
2317 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
|
---|
2318 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
|
---|
2319 | SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
|
---|
2320 | SSMFIELD_ENTRY_TERM()
|
---|
2321 | };
|
---|
2322 |
|
---|
2323 | /**
|
---|
2324 | * SSM descriptor table for the SHCLCLIENTPODSTATE structure.
|
---|
2325 | */
|
---|
2326 | static SSMFIELD const s_aShClSSMClientPODState[] =
|
---|
2327 | {
|
---|
2328 | SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, enmDir),
|
---|
2329 | SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, uFormat),
|
---|
2330 | SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbToReadWriteTotal),
|
---|
2331 | SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbReadWritten),
|
---|
2332 | SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, tsLastReadWrittenMs),
|
---|
2333 | SSMFIELD_ENTRY_TERM()
|
---|
2334 | };
|
---|
2335 |
|
---|
2336 | /**
|
---|
2337 | * SSM descriptor table for the SHCLCLIENTURISTATE structure.
|
---|
2338 | */
|
---|
2339 | static SSMFIELD const s_aShClSSMClientTransferState[] =
|
---|
2340 | {
|
---|
2341 | SSMFIELD_ENTRY(SHCLCLIENTTRANSFERSTATE, enmTransferDir),
|
---|
2342 | SSMFIELD_ENTRY_TERM()
|
---|
2343 | };
|
---|
2344 |
|
---|
2345 | /**
|
---|
2346 | * SSM descriptor table for the header of the SHCLCLIENTMSG structure.
|
---|
2347 | * The actual message parameters will be serialized separately.
|
---|
2348 | */
|
---|
2349 | static SSMFIELD const s_aShClSSMClientMsgHdr[] =
|
---|
2350 | {
|
---|
2351 | SSMFIELD_ENTRY(SHCLCLIENTMSG, idMsg),
|
---|
2352 | SSMFIELD_ENTRY(SHCLCLIENTMSG, cParms),
|
---|
2353 | SSMFIELD_ENTRY_TERM()
|
---|
2354 | };
|
---|
2355 |
|
---|
2356 | /**
|
---|
2357 | * SSM descriptor table for what used to be the VBOXSHCLMSGCTX structure but is
|
---|
2358 | * now part of SHCLCLIENTMSG.
|
---|
2359 | */
|
---|
2360 | static SSMFIELD const s_aShClSSMClientMsgCtx[] =
|
---|
2361 | {
|
---|
2362 | SSMFIELD_ENTRY(SHCLCLIENTMSG, idCtx),
|
---|
2363 | SSMFIELD_ENTRY_TERM()
|
---|
2364 | };
|
---|
2365 | #endif /* !UNIT_TEST */
|
---|
2366 |
|
---|
2367 | static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
|
---|
2368 | {
|
---|
2369 | LogFlowFuncEnter();
|
---|
2370 |
|
---|
2371 | #ifndef UNIT_TEST
|
---|
2372 | /*
|
---|
2373 | * When the state will be restored, pending requests will be reissued
|
---|
2374 | * by VMMDev. The service therefore must save state as if there were no
|
---|
2375 | * pending request.
|
---|
2376 | * Pending requests, if any, will be completed in svcDisconnect.
|
---|
2377 | */
|
---|
2378 | RT_NOREF(u32ClientID);
|
---|
2379 | LogFunc(("u32ClientID=%RU32\n", u32ClientID));
|
---|
2380 |
|
---|
2381 | PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
|
---|
2382 | AssertPtr(pClient);
|
---|
2383 |
|
---|
2384 | /* Write Shared Clipboard saved state version. */
|
---|
2385 | SSMR3PutU32(pSSM, VBOX_SHCL_SAVED_STATE_VER_CURRENT);
|
---|
2386 |
|
---|
2387 | int rc = SSMR3PutStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
|
---|
2388 | AssertRCReturn(rc, rc);
|
---|
2389 |
|
---|
2390 | rc = SSMR3PutStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /*fFlags*/, &s_aShClSSMClientPODState[0], NULL);
|
---|
2391 | AssertRCReturn(rc, rc);
|
---|
2392 |
|
---|
2393 | rc = SSMR3PutStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientTransferState[0], NULL);
|
---|
2394 | AssertRCReturn(rc, rc);
|
---|
2395 |
|
---|
2396 | /* Serialize the client's internal message queue. */
|
---|
2397 | rc = SSMR3PutU64(pSSM, pClient->cMsgAllocated);
|
---|
2398 | AssertRCReturn(rc, rc);
|
---|
2399 |
|
---|
2400 | PSHCLCLIENTMSG pMsg;
|
---|
2401 | RTListForEach(&pClient->MsgQueue, pMsg, SHCLCLIENTMSG, ListEntry)
|
---|
2402 | {
|
---|
2403 | SSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
|
---|
2404 | SSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
|
---|
2405 |
|
---|
2406 | for (uint32_t iParm = 0; iParm < pMsg->cParms; iParm++)
|
---|
2407 | HGCMSvcSSMR3Put(&pMsg->aParms[iParm], pSSM);
|
---|
2408 | }
|
---|
2409 |
|
---|
2410 | rc = SSMR3PutU64(pSSM, pClient->Legacy.cCID);
|
---|
2411 | AssertRCReturn(rc, rc);
|
---|
2412 |
|
---|
2413 | PSHCLCLIENTLEGACYCID pCID;
|
---|
2414 | RTListForEach(&pClient->Legacy.lstCID, pCID, SHCLCLIENTLEGACYCID, Node)
|
---|
2415 | {
|
---|
2416 | rc = SSMR3PutStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /*fFlags*/, &s_aShClSSMClientLegacyCID[0], NULL);
|
---|
2417 | AssertRCReturn(rc, rc);
|
---|
2418 | }
|
---|
2419 | #else /* UNIT_TEST */
|
---|
2420 | RT_NOREF3(u32ClientID, pvClient, pSSM);
|
---|
2421 | #endif /* UNIT_TEST */
|
---|
2422 | return VINF_SUCCESS;
|
---|
2423 | }
|
---|
2424 |
|
---|
2425 | #ifndef UNIT_TEST
|
---|
2426 | static int svcLoadStateV0(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
|
---|
2427 | {
|
---|
2428 | RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
|
---|
2429 |
|
---|
2430 | uint32_t uMarker;
|
---|
2431 | int rc = SSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
|
---|
2432 | AssertRC(rc);
|
---|
2433 | Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
|
---|
2434 |
|
---|
2435 | rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Client ID */
|
---|
2436 | AssertRCReturn(rc, rc);
|
---|
2437 |
|
---|
2438 | bool fValue;
|
---|
2439 | rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgQuit */
|
---|
2440 | AssertRCReturn(rc, rc);
|
---|
2441 |
|
---|
2442 | rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgReadData */
|
---|
2443 | AssertRCReturn(rc, rc);
|
---|
2444 |
|
---|
2445 | rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgFormats */
|
---|
2446 | AssertRCReturn(rc, rc);
|
---|
2447 |
|
---|
2448 | uint32_t fFormats;
|
---|
2449 | rc = SSMR3GetU32(pSSM, &fFormats); /* u32RequestedFormat */
|
---|
2450 | AssertRCReturn(rc, rc);
|
---|
2451 |
|
---|
2452 | rc = SSMR3GetU32(pSSM, &uMarker); /* End marker. */
|
---|
2453 | AssertRCReturn(rc, rc);
|
---|
2454 | Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
|
---|
2455 |
|
---|
2456 | return VINF_SUCCESS;
|
---|
2457 | }
|
---|
2458 | #endif /* UNIT_TEST */
|
---|
2459 |
|
---|
2460 | static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
|
---|
2461 | {
|
---|
2462 | LogFlowFuncEnter();
|
---|
2463 |
|
---|
2464 | #ifndef UNIT_TEST
|
---|
2465 |
|
---|
2466 | RT_NOREF(u32ClientID, uVersion);
|
---|
2467 |
|
---|
2468 | PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
|
---|
2469 | AssertPtr(pClient);
|
---|
2470 |
|
---|
2471 | /* Restore the client data. */
|
---|
2472 | uint32_t lenOrVer;
|
---|
2473 | int rc = SSMR3GetU32(pSSM, &lenOrVer);
|
---|
2474 | AssertRCReturn(rc, rc);
|
---|
2475 |
|
---|
2476 | LogFunc(("u32ClientID=%RU32, lenOrVer=%#RX64\n", u32ClientID, lenOrVer));
|
---|
2477 |
|
---|
2478 | if (lenOrVer == VBOX_SHCL_SAVED_STATE_VER_3_1)
|
---|
2479 | return svcLoadStateV0(u32ClientID, pvClient, pSSM, uVersion);
|
---|
2480 |
|
---|
2481 | if ( lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1B2
|
---|
2482 | && lenOrVer <= VBOX_SHCL_SAVED_STATE_VER_CURRENT)
|
---|
2483 | {
|
---|
2484 | if (lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1RC1)
|
---|
2485 | {
|
---|
2486 | SSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */, &s_aShClSSMClientState[0], NULL);
|
---|
2487 | SSMR3GetStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /* fFlags */,
|
---|
2488 | &s_aShClSSMClientPODState[0], NULL);
|
---|
2489 | }
|
---|
2490 | else
|
---|
2491 | SSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */, &s_aShClSSMClientState61B1[0], NULL);
|
---|
2492 | rc = SSMR3GetStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /* fFlags */,
|
---|
2493 | &s_aShClSSMClientTransferState[0], NULL);
|
---|
2494 | AssertRCReturn(rc, rc);
|
---|
2495 |
|
---|
2496 | /* Load the client's internal message queue. */
|
---|
2497 | uint64_t cMsgs;
|
---|
2498 | rc = SSMR3GetU64(pSSM, &cMsgs);
|
---|
2499 | AssertRCReturn(rc, rc);
|
---|
2500 | AssertLogRelMsgReturn(cMsgs < _16K, ("Too many messages: %u (%x)\n", cMsgs, cMsgs), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
2501 |
|
---|
2502 | for (uint64_t i = 0; i < cMsgs; i++)
|
---|
2503 | {
|
---|
2504 | union
|
---|
2505 | {
|
---|
2506 | SHCLCLIENTMSG Msg;
|
---|
2507 | uint8_t abPadding[RT_UOFFSETOF(SHCLCLIENTMSG, aParms) + sizeof(VBOXHGCMSVCPARM) * 2];
|
---|
2508 | } u;
|
---|
2509 |
|
---|
2510 | SSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
|
---|
2511 | rc = SSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
|
---|
2512 | AssertRCReturn(rc, rc);
|
---|
2513 |
|
---|
2514 | AssertLogRelMsgReturn(u.Msg.cParms <= VMMDEV_MAX_HGCM_PARMS,
|
---|
2515 | ("Too many HGCM message parameters: %u (%#x)\n", u.Msg.cParms, u.Msg.cParms),
|
---|
2516 | VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
2517 |
|
---|
2518 | PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient, u.Msg.idMsg, u.Msg.cParms);
|
---|
2519 | AssertReturn(pMsg, VERR_NO_MEMORY);
|
---|
2520 | pMsg->idCtx = u.Msg.idCtx;
|
---|
2521 |
|
---|
2522 | for (uint32_t p = 0; p < pMsg->cParms; p++)
|
---|
2523 | {
|
---|
2524 | rc = HGCMSvcSSMR3Get(&pMsg->aParms[p], pSSM);
|
---|
2525 | AssertRCReturnStmt(rc, shClSvcMsgFree(pClient, pMsg), rc);
|
---|
2526 | }
|
---|
2527 |
|
---|
2528 | shClSvcMsgAdd(pClient, pMsg, true /* fAppend */);
|
---|
2529 | }
|
---|
2530 |
|
---|
2531 | if (lenOrVer >= VBOX_SHCL_SAVED_STATE_LEGACY_CID)
|
---|
2532 | {
|
---|
2533 | uint64_t cCID;
|
---|
2534 | rc = SSMR3GetU64(pSSM, &cCID);
|
---|
2535 | AssertRCReturn(rc, rc);
|
---|
2536 | AssertLogRelMsgReturn(cCID < _16K, ("Too many context IDs: %u (%x)\n", cCID, cCID), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
2537 |
|
---|
2538 | for (uint64_t i = 0; i < cCID; i++)
|
---|
2539 | {
|
---|
2540 | PSHCLCLIENTLEGACYCID pCID = (PSHCLCLIENTLEGACYCID)RTMemAlloc(sizeof(SHCLCLIENTLEGACYCID));
|
---|
2541 | AssertPtrReturn(pCID, VERR_NO_MEMORY);
|
---|
2542 |
|
---|
2543 | SSMR3GetStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /* fFlags */, &s_aShClSSMClientLegacyCID[0], NULL);
|
---|
2544 | RTListAppend(&pClient->Legacy.lstCID, &pCID->Node);
|
---|
2545 | }
|
---|
2546 | }
|
---|
2547 | }
|
---|
2548 | else
|
---|
2549 | {
|
---|
2550 | LogRel(("Shared Clipboard: Unsupported saved state version (%#x)\n", lenOrVer));
|
---|
2551 | return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
|
---|
2552 | }
|
---|
2553 |
|
---|
2554 | /* Actual host data are to be reported to guest (SYNC). */
|
---|
2555 | ShClBackendSync(pClient);
|
---|
2556 |
|
---|
2557 | #else /* UNIT_TEST */
|
---|
2558 | RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
|
---|
2559 | #endif /* UNIT_TEST */
|
---|
2560 | return VINF_SUCCESS;
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | static DECLCALLBACK(int) extCallback(uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
|
---|
2564 | {
|
---|
2565 | RT_NOREF(pvData, cbData);
|
---|
2566 |
|
---|
2567 | LogFlowFunc(("u32Function=%RU32\n", u32Function));
|
---|
2568 |
|
---|
2569 | int rc = VINF_SUCCESS;
|
---|
2570 |
|
---|
2571 | /* Figure out if the client in charge for the service extension still is connected. */
|
---|
2572 | ClipboardClientMap::const_iterator itClient = g_mapClients.find(g_ExtState.uClientID);
|
---|
2573 | if (itClient != g_mapClients.end())
|
---|
2574 | {
|
---|
2575 | PSHCLCLIENT pClient = itClient->second;
|
---|
2576 | AssertPtr(pClient);
|
---|
2577 |
|
---|
2578 | switch (u32Function)
|
---|
2579 | {
|
---|
2580 | /* The service extension announces formats to the guest. */
|
---|
2581 | case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
|
---|
2582 | {
|
---|
2583 | LogFlowFunc(("VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE: g_ExtState.fReadingData=%RTbool\n", g_ExtState.fReadingData));
|
---|
2584 | if (!g_ExtState.fReadingData)
|
---|
2585 | rc = ShClSvcHostReportFormats(pClient, u32Format);
|
---|
2586 | else
|
---|
2587 | {
|
---|
2588 | g_ExtState.fDelayedAnnouncement = true;
|
---|
2589 | g_ExtState.fDelayedFormats = u32Format;
|
---|
2590 | rc = VINF_SUCCESS;
|
---|
2591 | }
|
---|
2592 | break;
|
---|
2593 | }
|
---|
2594 |
|
---|
2595 | /* The service extension wants read data from the guest. */
|
---|
2596 | case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
|
---|
2597 | rc = ShClSvcGuestDataRequest(pClient, u32Format, NULL /* pidEvent */);
|
---|
2598 | break;
|
---|
2599 |
|
---|
2600 | default:
|
---|
2601 | /* Just skip other messages. */
|
---|
2602 | break;
|
---|
2603 | }
|
---|
2604 | }
|
---|
2605 | else
|
---|
2606 | rc = VERR_NOT_FOUND;
|
---|
2607 |
|
---|
2608 | LogFlowFuncLeaveRC(rc);
|
---|
2609 | return rc;
|
---|
2610 | }
|
---|
2611 |
|
---|
2612 | static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
|
---|
2613 | {
|
---|
2614 | LogFlowFunc(("pfnExtension=%p\n", pfnExtension));
|
---|
2615 |
|
---|
2616 | SHCLEXTPARMS parms;
|
---|
2617 | RT_ZERO(parms);
|
---|
2618 |
|
---|
2619 | if (pfnExtension)
|
---|
2620 | {
|
---|
2621 | /* Install extension. */
|
---|
2622 | g_ExtState.pfnExtension = pfnExtension;
|
---|
2623 | g_ExtState.pvExtension = pvExtension;
|
---|
2624 |
|
---|
2625 | parms.u.pfnCallback = extCallback;
|
---|
2626 |
|
---|
2627 | g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
|
---|
2628 | }
|
---|
2629 | else
|
---|
2630 | {
|
---|
2631 | if (g_ExtState.pfnExtension)
|
---|
2632 | g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
|
---|
2633 |
|
---|
2634 | /* Uninstall extension. */
|
---|
2635 | g_ExtState.pfnExtension = NULL;
|
---|
2636 | g_ExtState.pvExtension = NULL;
|
---|
2637 | }
|
---|
2638 |
|
---|
2639 | return VINF_SUCCESS;
|
---|
2640 | }
|
---|
2641 |
|
---|
2642 | extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
|
---|
2643 | {
|
---|
2644 | int rc = VINF_SUCCESS;
|
---|
2645 |
|
---|
2646 | LogFlowFunc(("pTable=%p\n", pTable));
|
---|
2647 |
|
---|
2648 | if (!VALID_PTR(pTable))
|
---|
2649 | {
|
---|
2650 | rc = VERR_INVALID_PARAMETER;
|
---|
2651 | }
|
---|
2652 | else
|
---|
2653 | {
|
---|
2654 | LogFunc(("pTable->cbSize = %d, ptable->u32Version = 0x%08X\n", pTable->cbSize, pTable->u32Version));
|
---|
2655 |
|
---|
2656 | if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
|
---|
2657 | || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
|
---|
2658 | {
|
---|
2659 | rc = VERR_VERSION_MISMATCH;
|
---|
2660 | }
|
---|
2661 | else
|
---|
2662 | {
|
---|
2663 | g_pHelpers = pTable->pHelpers;
|
---|
2664 |
|
---|
2665 | pTable->cbClient = sizeof(SHCLCLIENT);
|
---|
2666 |
|
---|
2667 | pTable->pfnUnload = svcUnload;
|
---|
2668 | pTable->pfnConnect = svcConnect;
|
---|
2669 | pTable->pfnDisconnect = svcDisconnect;
|
---|
2670 | pTable->pfnCall = svcCall;
|
---|
2671 | pTable->pfnHostCall = svcHostCall;
|
---|
2672 | pTable->pfnSaveState = svcSaveState;
|
---|
2673 | pTable->pfnLoadState = svcLoadState;
|
---|
2674 | pTable->pfnRegisterExtension = svcRegisterExtension;
|
---|
2675 | pTable->pfnNotify = NULL;
|
---|
2676 | pTable->pvService = NULL;
|
---|
2677 |
|
---|
2678 | /* Service specific initialization. */
|
---|
2679 | rc = svcInit();
|
---|
2680 | }
|
---|
2681 | }
|
---|
2682 |
|
---|
2683 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
2684 | return rc;
|
---|
2685 | }
|
---|