1 | /** $Id: clipboard.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Guest Additions - X11 Shared Clipboard.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2017 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 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <iprt/alloc.h>
|
---|
23 | #include <iprt/asm.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/initterm.h>
|
---|
26 | #include <iprt/mem.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/process.h>
|
---|
29 | #include <iprt/semaphore.h>
|
---|
30 |
|
---|
31 | #include <VBox/log.h>
|
---|
32 | #include <VBox/VBoxGuestLib.h>
|
---|
33 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
34 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
35 |
|
---|
36 | #include "VBoxClient.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Global Variables *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Global clipboard context information.
|
---|
45 | */
|
---|
46 | struct _VBOXCLIPBOARDCONTEXT
|
---|
47 | {
|
---|
48 | /** Client ID for the clipboard subsystem */
|
---|
49 | uint32_t client;
|
---|
50 |
|
---|
51 | /** Pointer to the X11 clipboard backend */
|
---|
52 | CLIPBACKEND *pBackend;
|
---|
53 | };
|
---|
54 |
|
---|
55 | /** Only one client is supported. There seems to be no need for more clients. */
|
---|
56 | static VBOXCLIPBOARDCONTEXT g_ctx;
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Transfer clipboard data from the guest to the host.
|
---|
61 | *
|
---|
62 | * @returns VBox result code
|
---|
63 | * @param u32Format The format of the data being sent
|
---|
64 | * @param pv Pointer to the data being sent
|
---|
65 | * @param cb Size of the data being sent in bytes
|
---|
66 | */
|
---|
67 | static int vboxClipboardSendData(uint32_t u32Format, void *pv, uint32_t cb)
|
---|
68 | {
|
---|
69 | int rc;
|
---|
70 | LogRelFlowFunc(("u32Format=%d, pv=%p, cb=%d\n", u32Format, pv, cb));
|
---|
71 | rc = VbglR3ClipboardWriteData(g_ctx.client, u32Format, pv, cb);
|
---|
72 | LogRelFlowFunc(("rc=%Rrc\n", rc));
|
---|
73 | return rc;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Get clipboard data from the host.
|
---|
79 | *
|
---|
80 | * @returns VBox result code
|
---|
81 | * @param pCtx Our context information
|
---|
82 | * @param u32Format The format of the data being requested
|
---|
83 | * @retval ppv On success and if pcb > 0, this will point to a buffer
|
---|
84 | * to be freed with RTMemFree containing the data read.
|
---|
85 | * @retval pcb On success, this contains the number of bytes of data
|
---|
86 | * returned
|
---|
87 | */
|
---|
88 | int ClipRequestDataForX11(VBOXCLIPBOARDCONTEXT *pCtx, uint32_t u32Format, void **ppv, uint32_t *pcb)
|
---|
89 | {
|
---|
90 | RT_NOREF1(pCtx);
|
---|
91 | int rc = VINF_SUCCESS;
|
---|
92 | uint32_t cb = 1024;
|
---|
93 | void *pv = RTMemAlloc(cb);
|
---|
94 |
|
---|
95 | *ppv = 0;
|
---|
96 | LogRelFlowFunc(("u32Format=%u\n", u32Format));
|
---|
97 | if (RT_UNLIKELY(!pv))
|
---|
98 | rc = VERR_NO_MEMORY;
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | rc = VbglR3ClipboardReadData(g_ctx.client, u32Format, pv, cb, pcb);
|
---|
101 | if (RT_SUCCESS(rc) && (rc != VINF_BUFFER_OVERFLOW))
|
---|
102 | *ppv = pv;
|
---|
103 | /* A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
|
---|
104 | * larger buffer. The size of the buffer needed is placed in *pcb.
|
---|
105 | * So we start all over again. */
|
---|
106 | if (rc == VINF_BUFFER_OVERFLOW)
|
---|
107 | {
|
---|
108 | cb = *pcb;
|
---|
109 | RTMemFree(pv);
|
---|
110 | pv = RTMemAlloc(cb);
|
---|
111 | if (RT_UNLIKELY(!pv))
|
---|
112 | rc = VERR_NO_MEMORY;
|
---|
113 | if (RT_SUCCESS(rc))
|
---|
114 | rc = VbglR3ClipboardReadData(g_ctx.client, u32Format, pv, cb, pcb);
|
---|
115 | if (RT_SUCCESS(rc) && (rc != VINF_BUFFER_OVERFLOW))
|
---|
116 | *ppv = pv;
|
---|
117 | }
|
---|
118 | /* Catch other errors. This also catches the case in which the buffer was
|
---|
119 | * too small a second time, possibly because the clipboard contents
|
---|
120 | * changed half-way through the operation. Since we can't say whether or
|
---|
121 | * not this is actually an error, we just return size 0.
|
---|
122 | */
|
---|
123 | if (RT_FAILURE(rc) || (VINF_BUFFER_OVERFLOW == rc))
|
---|
124 | {
|
---|
125 | *pcb = 0;
|
---|
126 | if (pv != NULL)
|
---|
127 | RTMemFree(pv);
|
---|
128 | }
|
---|
129 | LogRelFlowFunc(("returning %Rrc\n", rc));
|
---|
130 | if (RT_SUCCESS(rc))
|
---|
131 | LogRelFlow((" *pcb=%d\n", *pcb));
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Opaque data structure describing a request from the host for clipboard
|
---|
136 | * data, passed in when the request is forwarded to the X11 backend so that
|
---|
137 | * it can be completed correctly. */
|
---|
138 | struct _CLIPREADCBREQ
|
---|
139 | {
|
---|
140 | /** The data format that was requested. */
|
---|
141 | uint32_t u32Format;
|
---|
142 | };
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Tell the host that new clipboard formats are available.
|
---|
146 | *
|
---|
147 | * @param pCtx Our context information
|
---|
148 | * @param u32Formats The formats to advertise
|
---|
149 | */
|
---|
150 | void ClipReportX11Formats(VBOXCLIPBOARDCONTEXT *pCtx, uint32_t u32Formats)
|
---|
151 | {
|
---|
152 | RT_NOREF1(pCtx);
|
---|
153 | LogRelFlowFunc(("u32Formats=%d\n", u32Formats));
|
---|
154 | int rc = VbglR3ClipboardReportFormats(g_ctx.client, u32Formats);
|
---|
155 | LogRelFlowFunc(("rc=%Rrc\n", rc));
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** This is called by the backend to tell us that a request for data from
|
---|
159 | * X11 has completed.
|
---|
160 | * @param pCtx Our context information
|
---|
161 | * @param rc the iprt result code of the request
|
---|
162 | * @param pReq the request structure that we passed in when we started
|
---|
163 | * the request. We RTMemFree() this in this function.
|
---|
164 | * @param pv the clipboard data returned from X11 if the request
|
---|
165 | * succeeded (see @a rc)
|
---|
166 | * @param cb the size of the data in @a pv
|
---|
167 | */
|
---|
168 | void ClipCompleteDataRequestFromX11(VBOXCLIPBOARDCONTEXT *pCtx, int rc, CLIPREADCBREQ *pReq, void *pv, uint32_t cb)
|
---|
169 | {
|
---|
170 | RT_NOREF1(pCtx);
|
---|
171 | if (RT_SUCCESS(rc))
|
---|
172 | vboxClipboardSendData(pReq->u32Format, pv, cb);
|
---|
173 | else
|
---|
174 | vboxClipboardSendData(0, NULL, 0);
|
---|
175 | RTMemFree(pReq);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Connect the guest clipboard to the host.
|
---|
180 | *
|
---|
181 | * @returns VBox status code
|
---|
182 | */
|
---|
183 | int vboxClipboardConnect(void)
|
---|
184 | {
|
---|
185 | int rc = VINF_SUCCESS;
|
---|
186 | LogRelFlowFunc(("\n"));
|
---|
187 |
|
---|
188 | /* Sanity */
|
---|
189 | AssertReturn(g_ctx.client == 0, VERR_WRONG_ORDER);
|
---|
190 | g_ctx.pBackend = ClipConstructX11(&g_ctx, false);
|
---|
191 | if (!g_ctx.pBackend)
|
---|
192 | rc = VERR_NO_MEMORY;
|
---|
193 | if (RT_SUCCESS(rc))
|
---|
194 | rc = ClipStartX11(g_ctx.pBackend);
|
---|
195 | if (RT_SUCCESS(rc))
|
---|
196 | {
|
---|
197 | rc = VbglR3ClipboardConnect(&g_ctx.client);
|
---|
198 | if (RT_FAILURE(rc))
|
---|
199 | LogRel(("Error connecting to host. rc=%Rrc\n", rc));
|
---|
200 | else if (!g_ctx.client)
|
---|
201 | {
|
---|
202 | LogRel(("Invalid client ID of 0\n"));
|
---|
203 | rc = VERR_NOT_SUPPORTED;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (rc != VINF_SUCCESS && g_ctx.pBackend)
|
---|
208 | ClipDestructX11(g_ctx.pBackend);
|
---|
209 | LogRelFlowFunc(("g_ctx.client=%u rc=%Rrc\n", g_ctx.client, rc));
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * The main loop of our clipboard reader.
|
---|
215 | */
|
---|
216 | int vboxClipboardMain(void)
|
---|
217 | {
|
---|
218 | int rc;
|
---|
219 | LogRelFlowFunc(("Starting guest clipboard service\n"));
|
---|
220 | bool fExiting = false;
|
---|
221 |
|
---|
222 | while (!fExiting)
|
---|
223 | {
|
---|
224 | uint32_t Msg;
|
---|
225 | uint32_t fFormats;
|
---|
226 | rc = VbglR3ClipboardGetHostMsg(g_ctx.client, &Msg, &fFormats);
|
---|
227 | if (RT_SUCCESS(rc))
|
---|
228 | {
|
---|
229 | switch (Msg)
|
---|
230 | {
|
---|
231 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
|
---|
232 | {
|
---|
233 | /* The host has announced available clipboard formats.
|
---|
234 | * Save the information so that it is available for
|
---|
235 | * future requests from guest applications.
|
---|
236 | */
|
---|
237 | LogRelFlowFunc(("VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS fFormats=%x\n", fFormats));
|
---|
238 | ClipAnnounceFormatToX11(g_ctx.pBackend, fFormats);
|
---|
239 | break;
|
---|
240 | }
|
---|
241 |
|
---|
242 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
|
---|
243 | {
|
---|
244 | /* The host needs data in the specified format. */
|
---|
245 | LogRelFlowFunc(("VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA fFormats=%x\n", fFormats));
|
---|
246 | CLIPREADCBREQ *pReq;
|
---|
247 | pReq = (CLIPREADCBREQ *)RTMemAllocZ(sizeof(*pReq));
|
---|
248 | if (!pReq)
|
---|
249 | {
|
---|
250 | rc = VERR_NO_MEMORY;
|
---|
251 | fExiting = true;
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
255 | pReq->u32Format = fFormats;
|
---|
256 | ClipRequestDataFromX11(g_ctx.pBackend, fFormats,
|
---|
257 | pReq);
|
---|
258 | }
|
---|
259 | break;
|
---|
260 | }
|
---|
261 |
|
---|
262 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT:
|
---|
263 | {
|
---|
264 | /* The host is terminating. */
|
---|
265 | LogRelFlowFunc(("VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT\n"));
|
---|
266 | if (RT_SUCCESS(ClipStopX11(g_ctx.pBackend)))
|
---|
267 | ClipDestructX11(g_ctx.pBackend);
|
---|
268 | fExiting = true;
|
---|
269 | break;
|
---|
270 | }
|
---|
271 |
|
---|
272 | default:
|
---|
273 | LogRel2(("Unsupported message from host!!!\n"));
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | LogRelFlow(("processed host event rc = %d\n", rc));
|
---|
278 | }
|
---|
279 | LogRelFlowFunc(("rc=%d\n", rc));
|
---|
280 | return rc;
|
---|
281 | }
|
---|
282 |
|
---|
283 | static const char *getPidFilePath()
|
---|
284 | {
|
---|
285 | return ".vboxclient-clipboard.pid";
|
---|
286 | }
|
---|
287 |
|
---|
288 | static int run(struct VBCLSERVICE **ppInterface, bool fDaemonised)
|
---|
289 | {
|
---|
290 | RT_NOREF2(ppInterface, fDaemonised);
|
---|
291 |
|
---|
292 | /* Initialise the guest library. */
|
---|
293 | int rc = VbglR3InitUser();
|
---|
294 | if (RT_FAILURE(rc))
|
---|
295 | VBClFatalError(("Failed to connect to the VirtualBox kernel service, rc=%Rrc\n", rc));
|
---|
296 | rc = vboxClipboardConnect();
|
---|
297 | /* Not RT_SUCCESS: VINF_PERMISSION_DENIED is host service not present. */
|
---|
298 | if (rc == VINF_SUCCESS)
|
---|
299 | rc = vboxClipboardMain();
|
---|
300 | if (rc == VERR_NOT_SUPPORTED)
|
---|
301 | rc = VINF_SUCCESS; /* Prevent automatic restart. */
|
---|
302 | if (RT_FAILURE(rc))
|
---|
303 | LogRelFunc(("guest clipboard service terminated abnormally: return code %Rrc\n", rc));
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 |
|
---|
307 | static void cleanup(struct VBCLSERVICE **ppInterface)
|
---|
308 | {
|
---|
309 | NOREF(ppInterface);
|
---|
310 | VbglR3Term();
|
---|
311 | }
|
---|
312 |
|
---|
313 | struct VBCLSERVICE vbclClipboardInterface =
|
---|
314 | {
|
---|
315 | getPidFilePath,
|
---|
316 | VBClServiceDefaultHandler, /* init */
|
---|
317 | run,
|
---|
318 | cleanup
|
---|
319 | };
|
---|
320 |
|
---|
321 | struct CLIPBOARDSERVICE
|
---|
322 | {
|
---|
323 | struct VBCLSERVICE *pInterface;
|
---|
324 | };
|
---|
325 |
|
---|
326 | struct VBCLSERVICE **VBClGetClipboardService()
|
---|
327 | {
|
---|
328 | struct CLIPBOARDSERVICE *pService =
|
---|
329 | (struct CLIPBOARDSERVICE *)RTMemAlloc(sizeof(*pService));
|
---|
330 |
|
---|
331 | if (!pService)
|
---|
332 | VBClFatalError(("Out of memory\n"));
|
---|
333 | pService->pInterface = &vbclClipboardInterface;
|
---|
334 | return &pService->pInterface;
|
---|
335 | }
|
---|