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