1 | /* $Id: DBGCIoProvIpc.cpp 96865 2022-09-26 14:45:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGC - Debugger Console, IPC I/O provider.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2020-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <VBox/dbg.h>
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 |
|
---|
36 | #include <iprt/localipc.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 |
|
---|
40 | #include "DBGCIoProvInternal.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | /**
|
---|
47 | * Debug console IPC connection data.
|
---|
48 | */
|
---|
49 | typedef struct DBGCIPCCON
|
---|
50 | {
|
---|
51 | /** The I/O callback table for the console. */
|
---|
52 | DBGCIO Io;
|
---|
53 | /** The socket of the connection. */
|
---|
54 | RTLOCALIPCSESSION hSession;
|
---|
55 | /** Connection status. */
|
---|
56 | bool fAlive;
|
---|
57 | } DBGCIPCCON;
|
---|
58 | /** Pointer to the instance data of the console IPC backend. */
|
---|
59 | typedef DBGCIPCCON *PDBGCIPCCON;
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Internal Functions *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @interface_method_impl{DBGCIO,pfnDestroy}
|
---|
68 | */
|
---|
69 | static DECLCALLBACK(void) dbgcIoProvIpcIoDestroy(PCDBGCIO pIo)
|
---|
70 | {
|
---|
71 | PDBGCIPCCON pIpcCon = RT_FROM_MEMBER(pIo, DBGCIPCCON, Io);
|
---|
72 | RTLocalIpcSessionClose(pIpcCon->hSession);
|
---|
73 | pIpcCon->fAlive =false;
|
---|
74 | RTMemFree(pIpcCon);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @interface_method_impl{DBGCIO,pfnInput}
|
---|
80 | */
|
---|
81 | static DECLCALLBACK(bool) dbgcIoProvIpcIoInput(PCDBGCIO pIo, uint32_t cMillies)
|
---|
82 | {
|
---|
83 | PDBGCIPCCON pIpcCon = RT_FROM_MEMBER(pIo, DBGCIPCCON, Io);
|
---|
84 | if (!pIpcCon->fAlive)
|
---|
85 | return false;
|
---|
86 | int rc = RTLocalIpcSessionWaitForData(pIpcCon->hSession, cMillies);
|
---|
87 | if (RT_FAILURE(rc) && rc != VERR_TIMEOUT)
|
---|
88 | pIpcCon->fAlive = false;
|
---|
89 | return rc != VERR_TIMEOUT;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * @interface_method_impl{DBGCIO,pfnRead}
|
---|
95 | */
|
---|
96 | static DECLCALLBACK(int) dbgcIoProvIpcIoRead(PCDBGCIO pIo, void *pvBuf, size_t cbBuf, size_t *pcbRead)
|
---|
97 | {
|
---|
98 | PDBGCIPCCON pIpcCon = RT_FROM_MEMBER(pIo, DBGCIPCCON, Io);
|
---|
99 | if (!pIpcCon->fAlive)
|
---|
100 | return VERR_INVALID_HANDLE;
|
---|
101 | int rc = RTLocalIpcSessionRead(pIpcCon->hSession, pvBuf, cbBuf, pcbRead);
|
---|
102 | if (RT_SUCCESS(rc) && pcbRead != NULL && *pcbRead == 0)
|
---|
103 | rc = VERR_NET_SHUTDOWN;
|
---|
104 | if (RT_FAILURE(rc))
|
---|
105 | pIpcCon->fAlive = false;
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * @interface_method_impl{DBGCIO,pfnWrite}
|
---|
112 | */
|
---|
113 | static DECLCALLBACK(int) dbgcIoProvIpcIoWrite(PCDBGCIO pIo, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
|
---|
114 | {
|
---|
115 | PDBGCIPCCON pIpcCon = RT_FROM_MEMBER(pIo, DBGCIPCCON, Io);
|
---|
116 | if (!pIpcCon->fAlive)
|
---|
117 | return VERR_INVALID_HANDLE;
|
---|
118 |
|
---|
119 | int rc = RTLocalIpcSessionWrite(pIpcCon->hSession, pvBuf, cbBuf);
|
---|
120 | if (RT_FAILURE(rc))
|
---|
121 | pIpcCon->fAlive = false;
|
---|
122 |
|
---|
123 | if (pcbWritten)
|
---|
124 | *pcbWritten = cbBuf;
|
---|
125 |
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * @interface_method_impl{DBGCIO,pfnSetReady}
|
---|
132 | */
|
---|
133 | static DECLCALLBACK(void) dbgcIoProvIpcIoSetReady(PCDBGCIO pIo, bool fReady)
|
---|
134 | {
|
---|
135 | /* stub */
|
---|
136 | NOREF(pIo);
|
---|
137 | NOREF(fReady);
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * @interface_method_impl{DBGCIOPROVREG,pfnCreate}
|
---|
143 | */
|
---|
144 | static DECLCALLBACK(int) dbgcIoProvIpcCreate(PDBGCIOPROV phDbgcIoProv, PCFGMNODE pCfg)
|
---|
145 | {
|
---|
146 | /*
|
---|
147 | * Get the address configuration.
|
---|
148 | */
|
---|
149 | char szAddress[512];
|
---|
150 | int rc = CFGMR3QueryStringDef(pCfg, "Address", szAddress, sizeof(szAddress), "");
|
---|
151 | if (RT_FAILURE(rc))
|
---|
152 | {
|
---|
153 | LogRel(("Configuration error: Failed querying \"Address\" -> rc=%Rc\n", rc));
|
---|
154 | return rc;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Create the server.
|
---|
159 | */
|
---|
160 | RTLOCALIPCSERVER hIpcSrv;
|
---|
161 | rc = RTLocalIpcServerCreate(&hIpcSrv, szAddress, RTLOCALIPC_FLAGS_NATIVE_NAME);
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | LogFlow(("dbgcIoProvIpcCreate: Created server on \"%s\"\n", szAddress));
|
---|
165 | *phDbgcIoProv = (DBGCIOPROV)hIpcSrv;
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * @interface_method_impl{DBGCIOPROVREG,pfnDestroy}
|
---|
175 | */
|
---|
176 | static DECLCALLBACK(void) dbgcIoProvIpcDestroy(DBGCIOPROV hDbgcIoProv)
|
---|
177 | {
|
---|
178 | int rc = RTLocalIpcServerDestroy((RTLOCALIPCSERVER)hDbgcIoProv);
|
---|
179 | AssertRC(rc);
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * @interface_method_impl{DBGCIOPROVREG,pfnWaitForConnect}
|
---|
185 | */
|
---|
186 | static DECLCALLBACK(int) dbgcIoProvIpcWaitForConnect(DBGCIOPROV hDbgcIoProv, RTMSINTERVAL cMsTimeout, PCDBGCIO *ppDbgcIo)
|
---|
187 | {
|
---|
188 | RTLOCALIPCSERVER hIpcSrv = (RTLOCALIPCSERVER)hDbgcIoProv;
|
---|
189 | RT_NOREF(cMsTimeout);
|
---|
190 |
|
---|
191 | RTLOCALIPCSESSION hSession = NIL_RTLOCALIPCSESSION;
|
---|
192 | int rc = RTLocalIpcServerListen(hIpcSrv, &hSession);
|
---|
193 | if (RT_SUCCESS(rc))
|
---|
194 | {
|
---|
195 | PDBGCIPCCON pIpcCon = (PDBGCIPCCON)RTMemAllocZ(sizeof(*pIpcCon));
|
---|
196 | if (RT_LIKELY(pIpcCon))
|
---|
197 | {
|
---|
198 | pIpcCon->Io.pfnDestroy = dbgcIoProvIpcIoDestroy;
|
---|
199 | pIpcCon->Io.pfnInput = dbgcIoProvIpcIoInput;
|
---|
200 | pIpcCon->Io.pfnRead = dbgcIoProvIpcIoRead;
|
---|
201 | pIpcCon->Io.pfnWrite = dbgcIoProvIpcIoWrite;
|
---|
202 | pIpcCon->Io.pfnPktBegin = NULL;
|
---|
203 | pIpcCon->Io.pfnPktEnd = NULL;
|
---|
204 | pIpcCon->Io.pfnSetReady = dbgcIoProvIpcIoSetReady;
|
---|
205 | pIpcCon->hSession = hSession;
|
---|
206 | pIpcCon->fAlive = true;
|
---|
207 | *ppDbgcIo = &pIpcCon->Io;
|
---|
208 | }
|
---|
209 | else
|
---|
210 | rc = VERR_NO_MEMORY;
|
---|
211 | }
|
---|
212 |
|
---|
213 | return rc;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * @interface_method_impl{DBGCIOPROVREG,pfnWaitInterrupt}
|
---|
219 | */
|
---|
220 | static DECLCALLBACK(int) dbgcIoProvIpcWaitInterrupt(DBGCIOPROV hDbgcIoProv)
|
---|
221 | {
|
---|
222 | return RTLocalIpcServerCancel((RTLOCALIPCSERVER)hDbgcIoProv);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * TCP I/O provider registration record.
|
---|
228 | */
|
---|
229 | const DBGCIOPROVREG g_DbgcIoProvIpc =
|
---|
230 | {
|
---|
231 | /** pszName */
|
---|
232 | "ipc",
|
---|
233 | /** pszDesc */
|
---|
234 | "IPC I/O provider.",
|
---|
235 | /** pfnCreate */
|
---|
236 | dbgcIoProvIpcCreate,
|
---|
237 | /** pfnDestroy */
|
---|
238 | dbgcIoProvIpcDestroy,
|
---|
239 | /** pfnWaitForConnect */
|
---|
240 | dbgcIoProvIpcWaitForConnect,
|
---|
241 | /** pfnWaitInterrupt */
|
---|
242 | dbgcIoProvIpcWaitInterrupt
|
---|
243 | };
|
---|
244 |
|
---|