1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox stream devices:
|
---|
4 | * Named pipe stream
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #define LOG_GROUP LOG_GROUP_DRV_NAMEDPIPE
|
---|
24 | #include <VBox/pdmdrv.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/file.h>
|
---|
27 | #include <iprt/stream.h>
|
---|
28 | #include <iprt/alloc.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/semaphore.h>
|
---|
31 |
|
---|
32 | #include "Builtins.h"
|
---|
33 |
|
---|
34 | #ifdef RT_OS_WINDOWS
|
---|
35 | #include <windows.h>
|
---|
36 | #else /* !RT_OS_WINDOWS */
|
---|
37 | #include <errno.h>
|
---|
38 | #include <unistd.h>
|
---|
39 | #include <sys/types.h>
|
---|
40 | #include <sys/socket.h>
|
---|
41 | #include <sys/un.h>
|
---|
42 | #endif /* !RT_OS_WINDOWS */
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Defined Constants And Macros *
|
---|
46 | *******************************************************************************/
|
---|
47 |
|
---|
48 | /** Converts a pointer to DRVNAMEDPIPE::IMedia to a PDRVNAMEDPIPE. */
|
---|
49 | #define PDMISTREAM_2_DRVNAMEDPIPE(pInterface) ( (PDRVNAMEDPIPE)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAMEDPIPE, IStream)) )
|
---|
50 |
|
---|
51 | /** Converts a pointer to PDMDRVINS::IBase to a PPDMDRVINS. */
|
---|
52 | #define PDMIBASE_2_DRVINS(pInterface) ( (PPDMDRVINS)((uintptr_t)pInterface - RT_OFFSETOF(PDMDRVINS, IBase)) )
|
---|
53 |
|
---|
54 | /*******************************************************************************
|
---|
55 | * Structures and Typedefs *
|
---|
56 | *******************************************************************************/
|
---|
57 | /**
|
---|
58 | * Named pipe driver instance data.
|
---|
59 | */
|
---|
60 | typedef struct DRVNAMEDPIPE
|
---|
61 | {
|
---|
62 | /** The stream interface. */
|
---|
63 | PDMISTREAM IStream;
|
---|
64 | /** Pointer to the driver instance. */
|
---|
65 | PPDMDRVINS pDrvIns;
|
---|
66 | /** Pointer to the named pipe file name. (Freed by MM) */
|
---|
67 | char *pszLocation;
|
---|
68 | /** Flag whether VirtualBox represents the server or client side. */
|
---|
69 | bool fIsServer;
|
---|
70 | #ifdef RT_OS_WINDOWS
|
---|
71 | /* File handle of the named pipe. */
|
---|
72 | HANDLE NamedPipe;
|
---|
73 | /* Overlapped structure for writes. */
|
---|
74 | OVERLAPPED OverlappedWrite;
|
---|
75 | /* Overlapped structure for reads. */
|
---|
76 | OVERLAPPED OverlappedRead;
|
---|
77 | /* Listen thread wakeup semaphore */
|
---|
78 | RTSEMEVENT ListenSem;
|
---|
79 | #else /* !RT_OS_WINDOWS */
|
---|
80 | /** Socket handle of the local socket for server. */
|
---|
81 | RTSOCKET LocalSocketServer;
|
---|
82 | /** Socket handle of the local socket. */
|
---|
83 | RTSOCKET LocalSocket;
|
---|
84 | #endif /* !RT_OS_WINDOWS */
|
---|
85 | /** Thread for listening for new connections. */
|
---|
86 | RTTHREAD ListenThread;
|
---|
87 | /** Flag to signal listening thread to shut down. */
|
---|
88 | bool fShutdown;
|
---|
89 | } DRVNAMEDPIPE, *PDRVNAMEDPIPE;
|
---|
90 |
|
---|
91 |
|
---|
92 | /*******************************************************************************
|
---|
93 | * Internal Functions *
|
---|
94 | *******************************************************************************/
|
---|
95 |
|
---|
96 |
|
---|
97 | /** @copydoc PDMISTREAM::pfnRead */
|
---|
98 | static DECLCALLBACK(int) drvNamedPipeRead(PPDMISTREAM pInterface, void *pvBuf, size_t *cbRead)
|
---|
99 | {
|
---|
100 | int rc = VINF_SUCCESS;
|
---|
101 | PDRVNAMEDPIPE pData = PDMISTREAM_2_DRVNAMEDPIPE(pInterface);
|
---|
102 | LogFlow(("%s: pvBuf=%p cbRead=%#x (%s)\n", __FUNCTION__, pvBuf, cbRead, pData->pszLocation));
|
---|
103 |
|
---|
104 | Assert(pvBuf);
|
---|
105 | #ifdef RT_OS_WINDOWS
|
---|
106 | if (pData->NamedPipe != INVALID_HANDLE_VALUE)
|
---|
107 | {
|
---|
108 | DWORD cbReallyRead;
|
---|
109 | pData->OverlappedRead.Offset = 0;
|
---|
110 | pData->OverlappedRead.OffsetHigh = 0;
|
---|
111 | if (!ReadFile(pData->NamedPipe, pvBuf, *cbRead, &cbReallyRead, &pData->OverlappedRead))
|
---|
112 | {
|
---|
113 | DWORD uError = GetLastError();
|
---|
114 |
|
---|
115 | if ( uError == ERROR_PIPE_LISTENING
|
---|
116 | || uError == ERROR_PIPE_NOT_CONNECTED)
|
---|
117 | {
|
---|
118 | /* No connection yet/anymore */
|
---|
119 | cbReallyRead = 0;
|
---|
120 |
|
---|
121 | /* wait a bit or else we'll be called right back. */
|
---|
122 | RTThreadSleep(100);
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | if (uError == ERROR_IO_PENDING)
|
---|
127 | {
|
---|
128 | uError = 0;
|
---|
129 |
|
---|
130 | /* Wait for incoming bytes. */
|
---|
131 | if (GetOverlappedResult(pData->NamedPipe, &pData->OverlappedRead, &cbReallyRead, TRUE) == FALSE)
|
---|
132 | uError = GetLastError();
|
---|
133 | }
|
---|
134 |
|
---|
135 | rc = RTErrConvertFromWin32(uError);
|
---|
136 | Log(("drvNamedPipeRead: ReadFile returned %d (%Vrc)\n", uError, rc));
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (VBOX_FAILURE(rc))
|
---|
141 | {
|
---|
142 | Log(("drvNamedPipeRead: FileRead returned %Vrc fShutdown=%d\n", rc, pData->fShutdown));
|
---|
143 | if ( !pData->fShutdown
|
---|
144 | && ( rc == VERR_EOF
|
---|
145 | || rc == VERR_BROKEN_PIPE
|
---|
146 | )
|
---|
147 | )
|
---|
148 |
|
---|
149 | {
|
---|
150 | FlushFileBuffers(pData->NamedPipe);
|
---|
151 | DisconnectNamedPipe(pData->NamedPipe);
|
---|
152 | if (!pData->fIsServer)
|
---|
153 | {
|
---|
154 | CloseHandle(pData->NamedPipe);
|
---|
155 | pData->NamedPipe = INVALID_HANDLE_VALUE;
|
---|
156 | }
|
---|
157 | /* pretend success */
|
---|
158 | rc = VINF_SUCCESS;
|
---|
159 | }
|
---|
160 | cbReallyRead = 0;
|
---|
161 | }
|
---|
162 | *cbRead = (size_t)cbReallyRead;
|
---|
163 | }
|
---|
164 | #else /* !RT_OS_WINDOWS */
|
---|
165 | if (pData->LocalSocket != NIL_RTSOCKET)
|
---|
166 | {
|
---|
167 | ssize_t cbReallyRead;
|
---|
168 | cbReallyRead = recv(pData->LocalSocket, pvBuf, *cbRead, 0);
|
---|
169 | if (cbReallyRead == 0)
|
---|
170 | {
|
---|
171 | RTSOCKET tmp = pData->LocalSocket;
|
---|
172 | pData->LocalSocket = NIL_RTSOCKET;
|
---|
173 | close(tmp);
|
---|
174 | }
|
---|
175 | else if (cbReallyRead == -1)
|
---|
176 | {
|
---|
177 | cbReallyRead = 0;
|
---|
178 | rc = RTErrConvertFromErrno(errno);
|
---|
179 | }
|
---|
180 | *cbRead = cbReallyRead;
|
---|
181 | }
|
---|
182 | #endif /* !RT_OS_WINDOWS */
|
---|
183 | else
|
---|
184 | {
|
---|
185 | RTThreadSleep(100);
|
---|
186 | *cbRead = 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | LogFlow(("%s: cbRead=%d returns %Vrc\n", __FUNCTION__, *cbRead, rc));
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /** @copydoc PDMISTREAM::pfnWrite */
|
---|
195 | static DECLCALLBACK(int) drvNamedPipeWrite(PPDMISTREAM pInterface, const void *pvBuf, size_t *cbWrite)
|
---|
196 | {
|
---|
197 | int rc = VINF_SUCCESS;
|
---|
198 | PDRVNAMEDPIPE pData = PDMISTREAM_2_DRVNAMEDPIPE(pInterface);
|
---|
199 | LogFlow(("%s: pvBuf=%p cbWrite=%#x (%s)\n", __FUNCTION__, pvBuf, cbWrite, pData->pszLocation));
|
---|
200 |
|
---|
201 | Assert(pvBuf);
|
---|
202 | #ifdef RT_OS_WINDOWS
|
---|
203 | if (pData->NamedPipe != INVALID_HANDLE_VALUE)
|
---|
204 | {
|
---|
205 | unsigned cbWritten;
|
---|
206 | pData->OverlappedWrite.Offset = 0;
|
---|
207 | pData->OverlappedWrite.OffsetHigh = 0;
|
---|
208 | if (!WriteFile(pData->NamedPipe, pvBuf, *cbWrite, NULL, &pData->OverlappedWrite))
|
---|
209 | {
|
---|
210 | DWORD uError = GetLastError();
|
---|
211 |
|
---|
212 | if ( uError == ERROR_PIPE_LISTENING
|
---|
213 | || uError == ERROR_PIPE_NOT_CONNECTED)
|
---|
214 | {
|
---|
215 | /* No connection yet/anymore; just discard the write. */
|
---|
216 | cbWritten = *cbWrite;
|
---|
217 | }
|
---|
218 | else
|
---|
219 | if (uError != ERROR_IO_PENDING)
|
---|
220 | {
|
---|
221 | rc = RTErrConvertFromWin32(uError);
|
---|
222 | Log(("drvNamedPipeWrite: WriteFile returned %d (%Vrc)\n", uError, rc));
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | /* Wait for the write to complete. */
|
---|
227 | if (GetOverlappedResult(pData->NamedPipe, &pData->OverlappedWrite, (DWORD *)&cbWritten, TRUE) == FALSE)
|
---|
228 | uError = GetLastError();
|
---|
229 | }
|
---|
230 | }
|
---|
231 | else
|
---|
232 | cbWritten = *cbWrite;
|
---|
233 |
|
---|
234 | if (VBOX_FAILURE(rc))
|
---|
235 | {
|
---|
236 | if ( rc == VERR_EOF
|
---|
237 | || rc == VERR_BROKEN_PIPE)
|
---|
238 | {
|
---|
239 | FlushFileBuffers(pData->NamedPipe);
|
---|
240 | DisconnectNamedPipe(pData->NamedPipe);
|
---|
241 | if (!pData->fIsServer)
|
---|
242 | {
|
---|
243 | CloseHandle(pData->NamedPipe);
|
---|
244 | pData->NamedPipe = INVALID_HANDLE_VALUE;
|
---|
245 | }
|
---|
246 | /* pretend success */
|
---|
247 | rc = VINF_SUCCESS;
|
---|
248 | }
|
---|
249 | cbWritten = 0;
|
---|
250 | }
|
---|
251 | *cbWrite = cbWritten;
|
---|
252 | }
|
---|
253 | #else /* !RT_OS_WINDOWS */
|
---|
254 | if (pData->LocalSocket != NIL_RTSOCKET)
|
---|
255 | {
|
---|
256 | ssize_t cbWritten;
|
---|
257 | cbWritten = send(pData->LocalSocket, pvBuf, *cbWrite, 0);
|
---|
258 | if (cbWritten == 0)
|
---|
259 | {
|
---|
260 | RTSOCKET tmp = pData->LocalSocket;
|
---|
261 | pData->LocalSocket = NIL_RTSOCKET;
|
---|
262 | close(tmp);
|
---|
263 | }
|
---|
264 | else if (cbWritten == -1)
|
---|
265 | {
|
---|
266 | cbWritten = 0;
|
---|
267 | rc = RTErrConvertFromErrno(errno);
|
---|
268 | }
|
---|
269 | *cbWrite = cbWritten;
|
---|
270 | }
|
---|
271 | #endif /* !RT_OS_WINDOWS */
|
---|
272 |
|
---|
273 | LogFlow(("%s: returns %Vrc\n", __FUNCTION__, rc));
|
---|
274 | return rc;
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Queries an interface to the driver.
|
---|
280 | *
|
---|
281 | * @returns Pointer to interface.
|
---|
282 | * @returns NULL if the interface was not supported by the driver.
|
---|
283 | * @param pInterface Pointer to this interface structure.
|
---|
284 | * @param enmInterface The requested interface identification.
|
---|
285 | * @thread Any thread.
|
---|
286 | */
|
---|
287 | static DECLCALLBACK(void *) drvNamedPipeQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
288 | {
|
---|
289 | PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
|
---|
290 | PDRVNAMEDPIPE pDrv = PDMINS2DATA(pDrvIns, PDRVNAMEDPIPE);
|
---|
291 | switch (enmInterface)
|
---|
292 | {
|
---|
293 | case PDMINTERFACE_BASE:
|
---|
294 | return &pDrvIns->IBase;
|
---|
295 | case PDMINTERFACE_STREAM:
|
---|
296 | return &pDrv->IStream;
|
---|
297 | default:
|
---|
298 | return NULL;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /* -=-=-=-=- listen thread -=-=-=-=- */
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Receive thread loop.
|
---|
307 | *
|
---|
308 | * @returns 0 on success.
|
---|
309 | * @param ThreadSelf Thread handle to this thread.
|
---|
310 | * @param pvUser User argument.
|
---|
311 | */
|
---|
312 | static DECLCALLBACK(int) drvNamedPipeListenLoop(RTTHREAD ThreadSelf, void *pvUser)
|
---|
313 | {
|
---|
314 | PDRVNAMEDPIPE pData = (PDRVNAMEDPIPE)pvUser;
|
---|
315 | int rc = VINF_SUCCESS;
|
---|
316 | #ifdef RT_OS_WINDOWS
|
---|
317 | HANDLE NamedPipe = pData->NamedPipe;
|
---|
318 | HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, 0);
|
---|
319 | #endif
|
---|
320 |
|
---|
321 | while (RT_LIKELY(!pData->fShutdown))
|
---|
322 | {
|
---|
323 | #ifdef RT_OS_WINDOWS
|
---|
324 | OVERLAPPED overlapped;
|
---|
325 |
|
---|
326 | memset(&overlapped, 0, sizeof(overlapped));
|
---|
327 | overlapped.hEvent = hEvent;
|
---|
328 |
|
---|
329 | BOOL fConnected = ConnectNamedPipe(NamedPipe, &overlapped);
|
---|
330 | if ( !fConnected
|
---|
331 | && !pData->fShutdown)
|
---|
332 | {
|
---|
333 | DWORD hrc = GetLastError();
|
---|
334 |
|
---|
335 | if (hrc == ERROR_IO_PENDING)
|
---|
336 | {
|
---|
337 | DWORD dummy;
|
---|
338 |
|
---|
339 | hrc = 0;
|
---|
340 | if (GetOverlappedResult(pData->NamedPipe, &overlapped, &dummy, TRUE) == FALSE)
|
---|
341 | hrc = GetLastError();
|
---|
342 |
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (pData->fShutdown)
|
---|
346 | break;
|
---|
347 |
|
---|
348 | if (hrc == ERROR_PIPE_CONNECTED)
|
---|
349 | {
|
---|
350 | RTSemEventWait(pData->ListenSem, 250);
|
---|
351 | }
|
---|
352 | else if (hrc != ERROR_SUCCESS)
|
---|
353 | {
|
---|
354 | rc = RTErrConvertFromWin32(hrc);
|
---|
355 | LogRel(("NamedPipe%d: ConnectNamedPipe failed, rc=%Vrc\n", pData->pDrvIns->iInstance, rc));
|
---|
356 | break;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | #else /* !RT_OS_WINDOWS */
|
---|
360 | if (listen(pData->LocalSocketServer, 0) == -1)
|
---|
361 | {
|
---|
362 | rc = RTErrConvertFromErrno(errno);
|
---|
363 | LogRel(("NamedPipe%d: listen failed, rc=%Vrc\n", pData->pDrvIns->iInstance, rc));
|
---|
364 | break;
|
---|
365 | }
|
---|
366 | int s = accept(pData->LocalSocketServer, NULL, NULL);
|
---|
367 | if (s == -1)
|
---|
368 | {
|
---|
369 | rc = RTErrConvertFromErrno(errno);
|
---|
370 | LogRel(("NamedPipe%d: accept failed, rc=%Vrc\n", pData->pDrvIns->iInstance, rc));
|
---|
371 | break;
|
---|
372 | }
|
---|
373 | else
|
---|
374 | {
|
---|
375 | if (pData->LocalSocket != NIL_RTSOCKET)
|
---|
376 | {
|
---|
377 | LogRel(("NamedPipe%d: only single connection supported\n", pData->pDrvIns->iInstance));
|
---|
378 | close(s);
|
---|
379 | }
|
---|
380 | else
|
---|
381 | pData->LocalSocket = s;
|
---|
382 | }
|
---|
383 | #endif /* !RT_OS_WINDOWS */
|
---|
384 | }
|
---|
385 |
|
---|
386 | #ifdef RT_OS_WINDOWS
|
---|
387 | CloseHandle(hEvent);
|
---|
388 | #endif
|
---|
389 | pData->ListenThread = NIL_RTTHREAD;
|
---|
390 | return VINF_SUCCESS;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Construct a named pipe stream driver instance.
|
---|
396 | *
|
---|
397 | * @returns VBox status.
|
---|
398 | * @param pDrvIns The driver instance data.
|
---|
399 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
400 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
401 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
402 | * iInstance it's expected to be used a bit in this function.
|
---|
403 | */
|
---|
404 | static DECLCALLBACK(int) drvNamedPipeConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
405 | {
|
---|
406 | int rc;
|
---|
407 | char *pszLocation = NULL;
|
---|
408 | PDRVNAMEDPIPE pData = PDMINS2DATA(pDrvIns, PDRVNAMEDPIPE);
|
---|
409 |
|
---|
410 | /*
|
---|
411 | * Init the static parts.
|
---|
412 | */
|
---|
413 | pData->pDrvIns = pDrvIns;
|
---|
414 | pData->pszLocation = NULL;
|
---|
415 | pData->fIsServer = false;
|
---|
416 | #ifdef RT_OS_WINDOWS
|
---|
417 | pData->NamedPipe = INVALID_HANDLE_VALUE;
|
---|
418 | #else /* !RT_OS_WINDOWS */
|
---|
419 | pData->LocalSocketServer = NIL_RTSOCKET;
|
---|
420 | pData->LocalSocket = NIL_RTSOCKET;
|
---|
421 | #endif /* !RT_OS_WINDOWS */
|
---|
422 | pData->ListenThread = NIL_RTTHREAD;
|
---|
423 | pData->fShutdown = false;
|
---|
424 | /* IBase */
|
---|
425 | pDrvIns->IBase.pfnQueryInterface = drvNamedPipeQueryInterface;
|
---|
426 | /* IStream */
|
---|
427 | pData->IStream.pfnRead = drvNamedPipeRead;
|
---|
428 | pData->IStream.pfnWrite = drvNamedPipeWrite;
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * Read the configuration.
|
---|
432 | */
|
---|
433 | if (!CFGMR3AreValuesValid(pCfgHandle, "Location\0IsServer\0"))
|
---|
434 | {
|
---|
435 | rc = VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
436 | goto out;
|
---|
437 | }
|
---|
438 |
|
---|
439 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "Location", &pszLocation);
|
---|
440 | if (VBOX_FAILURE(rc))
|
---|
441 | {
|
---|
442 | AssertMsgFailed(("Configuration error: query \"Location\" resulted in %Vrc.\n", rc));
|
---|
443 | goto out;
|
---|
444 | }
|
---|
445 | pData->pszLocation = pszLocation;
|
---|
446 |
|
---|
447 | bool fIsServer;
|
---|
448 | rc = CFGMR3QueryBool(pCfgHandle, "IsServer", &fIsServer);
|
---|
449 | if (VBOX_FAILURE(rc))
|
---|
450 | {
|
---|
451 | AssertMsgFailed(("Configuration error: query \"IsServer\" resulted in %Vrc.\n", rc));
|
---|
452 | goto out;
|
---|
453 | }
|
---|
454 | pData->fIsServer = fIsServer;
|
---|
455 |
|
---|
456 | #ifdef RT_OS_WINDOWS
|
---|
457 | if (fIsServer)
|
---|
458 | {
|
---|
459 | HANDLE hPipe = CreateNamedPipe(pData->pszLocation, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 32, 32, 10000, NULL);
|
---|
460 | if (hPipe == INVALID_HANDLE_VALUE)
|
---|
461 | {
|
---|
462 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
463 | LogRel(("NamedPipe%d: CreateNamedPipe failed rc=%Vrc\n", pData->pDrvIns->iInstance));
|
---|
464 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to create named pipe %s"), pDrvIns->iInstance, pszLocation);
|
---|
465 | }
|
---|
466 | pData->NamedPipe = hPipe;
|
---|
467 |
|
---|
468 | rc = RTSemEventCreate(&pData->ListenSem);
|
---|
469 | AssertRC(rc);
|
---|
470 |
|
---|
471 | rc = RTThreadCreate(&pData->ListenThread, drvNamedPipeListenLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SerPipe");
|
---|
472 | if VBOX_FAILURE(rc)
|
---|
473 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to create listening thread"), pDrvIns->iInstance);
|
---|
474 |
|
---|
475 | }
|
---|
476 | else
|
---|
477 | {
|
---|
478 | /* Connect to the named pipe. */
|
---|
479 | HANDLE hPipe = CreateFile(pData->pszLocation, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
---|
480 | if (hPipe == INVALID_HANDLE_VALUE)
|
---|
481 | {
|
---|
482 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
483 | LogRel(("NamedPipe%d: CreateFile failed rc=%Vrc\n", pData->pDrvIns->iInstance));
|
---|
484 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to connect to named pipe %s"), pDrvIns->iInstance, pszLocation);
|
---|
485 | }
|
---|
486 | pData->NamedPipe = hPipe;
|
---|
487 | }
|
---|
488 |
|
---|
489 | memset(&pData->OverlappedWrite, 0, sizeof(pData->OverlappedWrite));
|
---|
490 | memset(&pData->OverlappedRead, 0, sizeof(pData->OverlappedRead));
|
---|
491 | pData->OverlappedWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
---|
492 | pData->OverlappedRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
---|
493 | #else /* !RT_OS_WINDOWS */
|
---|
494 | int s;
|
---|
495 | struct sockaddr_un addr;
|
---|
496 |
|
---|
497 | if ((s = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
|
---|
498 | return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS, N_("NamedPipe#%d failed to create local socket"), pDrvIns->iInstance);
|
---|
499 |
|
---|
500 | memset(&addr, 0, sizeof(addr));
|
---|
501 | addr.sun_family = AF_UNIX;
|
---|
502 | strncpy(addr.sun_path, pszLocation, sizeof(addr.sun_path)-1);
|
---|
503 |
|
---|
504 | if (fIsServer)
|
---|
505 | {
|
---|
506 | /* Bind address to the local socket. */
|
---|
507 | RTFileDelete(pszLocation);
|
---|
508 | if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
509 | return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS, N_("NamedPipe#%d failed to bind to local socket %s"), pDrvIns->iInstance, pszLocation);
|
---|
510 | pData->LocalSocketServer = s;
|
---|
511 | rc = RTThreadCreate(&pData->ListenThread, drvNamedPipeListenLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SerPipe");
|
---|
512 | if VBOX_FAILURE(rc)
|
---|
513 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to create listening thread"), pDrvIns->iInstance);
|
---|
514 | }
|
---|
515 | else
|
---|
516 | {
|
---|
517 | /* Connect to the local socket. */
|
---|
518 | if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
519 | return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS, N_("NamedPipe#%d failed to connect to local socket %s"), pDrvIns->iInstance, pszLocation);
|
---|
520 | pData->LocalSocket = s;
|
---|
521 | }
|
---|
522 | #endif /* !RT_OS_WINDOWS */
|
---|
523 |
|
---|
524 | out:
|
---|
525 | if (VBOX_FAILURE(rc))
|
---|
526 | {
|
---|
527 | if (pszLocation)
|
---|
528 | MMR3HeapFree(pszLocation);
|
---|
529 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NamedPipe#%d failed to initialize"), pDrvIns->iInstance);
|
---|
530 | }
|
---|
531 |
|
---|
532 | LogFlow(("drvNamedPipeConstruct: location %s isServer %d\n", pszLocation, fIsServer));
|
---|
533 | LogRel(("NamedPipe: location %s, %s\n", pszLocation, fIsServer ? "server" : "client"));
|
---|
534 | return VINF_SUCCESS;
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * Destruct a named pipe stream driver instance.
|
---|
540 | *
|
---|
541 | * Most VM resources are freed by the VM. This callback is provided so that
|
---|
542 | * any non-VM resources can be freed correctly.
|
---|
543 | *
|
---|
544 | * @param pDrvIns The driver instance data.
|
---|
545 | */
|
---|
546 | static DECLCALLBACK(void) drvNamedPipeDestruct(PPDMDRVINS pDrvIns)
|
---|
547 | {
|
---|
548 | PDRVNAMEDPIPE pData = PDMINS2DATA(pDrvIns, PDRVNAMEDPIPE);
|
---|
549 | LogFlow(("%s: %s\n", __FUNCTION__, pData->pszLocation));
|
---|
550 |
|
---|
551 | if (pData->ListenThread)
|
---|
552 | {
|
---|
553 | RTThreadWait(pData->ListenThread, 250, NULL);
|
---|
554 | if (pData->ListenThread != NIL_RTTHREAD)
|
---|
555 | LogRel(("NamedPipe%d: listen thread did not terminate\n", pDrvIns->iInstance));
|
---|
556 | }
|
---|
557 |
|
---|
558 | if (pData->pszLocation)
|
---|
559 | MMR3HeapFree(pData->pszLocation);
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Power off a named pipe stream driver instance.
|
---|
565 | *
|
---|
566 | * This does most of the destruction work, to avoid ordering dependencies.
|
---|
567 | *
|
---|
568 | * @param pDrvIns The driver instance data.
|
---|
569 | */
|
---|
570 | static DECLCALLBACK(void) drvNamedPipePowerOff(PPDMDRVINS pDrvIns)
|
---|
571 | {
|
---|
572 | PDRVNAMEDPIPE pData = PDMINS2DATA(pDrvIns, PDRVNAMEDPIPE);
|
---|
573 | LogFlow(("%s: %s\n", __FUNCTION__, pData->pszLocation));
|
---|
574 |
|
---|
575 | pData->fShutdown = true;
|
---|
576 |
|
---|
577 | #ifdef RT_OS_WINDOWS
|
---|
578 | if (pData->NamedPipe != INVALID_HANDLE_VALUE)
|
---|
579 | {
|
---|
580 | if (pData->fIsServer)
|
---|
581 | {
|
---|
582 | FlushFileBuffers(pData->NamedPipe);
|
---|
583 | DisconnectNamedPipe(pData->NamedPipe);
|
---|
584 | }
|
---|
585 |
|
---|
586 | CloseHandle(pData->NamedPipe);
|
---|
587 | pData->NamedPipe = INVALID_HANDLE_VALUE;
|
---|
588 | CloseHandle(pData->OverlappedRead.hEvent);
|
---|
589 | CloseHandle(pData->OverlappedWrite.hEvent);
|
---|
590 | }
|
---|
591 | if (pData->fIsServer)
|
---|
592 | {
|
---|
593 | /* Wake up listen thread */
|
---|
594 | RTSemEventSignal(pData->ListenSem);
|
---|
595 | RTSemEventDestroy(pData->ListenSem);
|
---|
596 | }
|
---|
597 | #else /* !RT_OS_WINDOWS */
|
---|
598 | if (pData->fIsServer)
|
---|
599 | {
|
---|
600 | if (pData->LocalSocketServer != NIL_RTSOCKET)
|
---|
601 | close(pData->LocalSocketServer);
|
---|
602 | if (pData->pszLocation)
|
---|
603 | RTFileDelete(pData->pszLocation);
|
---|
604 | }
|
---|
605 | else
|
---|
606 | {
|
---|
607 | if (pData->LocalSocket != NIL_RTSOCKET)
|
---|
608 | close(pData->LocalSocket);
|
---|
609 | }
|
---|
610 | #endif /* !RT_OS_WINDOWS */
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Named pipe driver registration record.
|
---|
616 | */
|
---|
617 | const PDMDRVREG g_DrvNamedPipe =
|
---|
618 | {
|
---|
619 | /* u32Version */
|
---|
620 | PDM_DRVREG_VERSION,
|
---|
621 | /* szDriverName */
|
---|
622 | "NamedPipe",
|
---|
623 | /* pszDescription */
|
---|
624 | "Named Pipe stream driver.",
|
---|
625 | /* fFlags */
|
---|
626 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
627 | /* fClass. */
|
---|
628 | PDM_DRVREG_CLASS_STREAM,
|
---|
629 | /* cMaxInstances */
|
---|
630 | ~0,
|
---|
631 | /* cbInstance */
|
---|
632 | sizeof(DRVNAMEDPIPE),
|
---|
633 | /* pfnConstruct */
|
---|
634 | drvNamedPipeConstruct,
|
---|
635 | /* pfnDestruct */
|
---|
636 | drvNamedPipeDestruct,
|
---|
637 | /* pfnIOCtl */
|
---|
638 | NULL,
|
---|
639 | /* pfnPowerOn */
|
---|
640 | NULL,
|
---|
641 | /* pfnReset */
|
---|
642 | NULL,
|
---|
643 | /* pfnSuspend */
|
---|
644 | NULL,
|
---|
645 | /* pfnResume */
|
---|
646 | NULL,
|
---|
647 | /* pfnDetach */
|
---|
648 | NULL,
|
---|
649 | /* pfnPowerOff */
|
---|
650 | drvNamedPipePowerOff,
|
---|
651 | };
|
---|