1 | /* $Id: VBoxGuestR3LibGuestCtrl.cpp 30013 2010-06-03 14:40:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, guest control.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/cpp/autores.h>
|
---|
35 | #include <iprt/stdarg.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
38 |
|
---|
39 | #include "VBGLR3Internal.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *******************************************************************************/
|
---|
45 |
|
---|
46 | using namespace guestControl;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Connects to the guest control service.
|
---|
50 | *
|
---|
51 | * @returns VBox status code
|
---|
52 | * @param pu32ClientId Where to put the client id on success. The client id
|
---|
53 | * must be passed to all the other calls to the service.
|
---|
54 | */
|
---|
55 | VBGLR3DECL(int) VbglR3GuestCtrlConnect(uint32_t *pu32ClientId)
|
---|
56 | {
|
---|
57 | VBoxGuestHGCMConnectInfo Info;
|
---|
58 | Info.result = VERR_WRONG_ORDER;
|
---|
59 | Info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
|
---|
60 | RT_ZERO(Info.Loc.u);
|
---|
61 | strcpy(Info.Loc.u.host.achName, "VBoxGuestControlSvc");
|
---|
62 | Info.u32ClientID = UINT32_MAX; /* try make valgrid shut up. */
|
---|
63 |
|
---|
64 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CONNECT, &Info, sizeof(Info));
|
---|
65 | if (RT_SUCCESS(rc))
|
---|
66 | {
|
---|
67 | rc = Info.result;
|
---|
68 | if (RT_SUCCESS(rc))
|
---|
69 | *pu32ClientId = Info.u32ClientID;
|
---|
70 | }
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Disconnect from the guest control service.
|
---|
77 | *
|
---|
78 | * @returns VBox status code.
|
---|
79 | * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
|
---|
80 | */
|
---|
81 | VBGLR3DECL(int) VbglR3GuestCtrlDisconnect(uint32_t u32ClientId)
|
---|
82 | {
|
---|
83 | VBoxGuestHGCMDisconnectInfo Info;
|
---|
84 | Info.result = VERR_WRONG_ORDER;
|
---|
85 | Info.u32ClientID = u32ClientId;
|
---|
86 |
|
---|
87 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Info, sizeof(Info));
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | rc = Info.result;
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Gets a host message.
|
---|
96 | *
|
---|
97 | * This will block until a message becomes available.
|
---|
98 | *
|
---|
99 | * @returns VBox status code.
|
---|
100 | * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
|
---|
101 | * @param puMsg Where to store the message id.
|
---|
102 | * @param puNumParms Where to store the number of parameters which will be received
|
---|
103 | * in a second call to the host.
|
---|
104 | */
|
---|
105 | VBGLR3DECL(int) VbglR3GuestCtrlGetHostMsg(uint32_t u32ClientId, uint32_t *puMsg, uint32_t *puNumParms)
|
---|
106 | {
|
---|
107 | AssertPtr(puMsg);
|
---|
108 | AssertPtr(puNumParms);
|
---|
109 |
|
---|
110 | VBoxGuestCtrlHGCMMsgType Msg;
|
---|
111 |
|
---|
112 | Msg.hdr.result = VERR_WRONG_ORDER;
|
---|
113 | Msg.hdr.u32ClientID = u32ClientId;
|
---|
114 | Msg.hdr.u32Function = GUEST_GET_HOST_MSG; /* Tell the host we want our next command. */
|
---|
115 | Msg.hdr.cParms = 2; /* Just peek for the next message! */
|
---|
116 |
|
---|
117 | VbglHGCMParmUInt32Set(&Msg.msg, 0);
|
---|
118 | VbglHGCMParmUInt32Set(&Msg.num_parms, 0);
|
---|
119 |
|
---|
120 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
121 | if (RT_SUCCESS(rc))
|
---|
122 | {
|
---|
123 | rc = VbglHGCMParmUInt32Get(&Msg.msg, puMsg);
|
---|
124 | if (RT_SUCCESS(rc))
|
---|
125 | rc = VbglHGCMParmUInt32Get(&Msg.num_parms, puNumParms);
|
---|
126 | if (RT_SUCCESS(rc))
|
---|
127 | rc = Msg.hdr.result;
|
---|
128 | /* Ok, so now we know what message type and how much parameters there are. */
|
---|
129 | }
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Asks the host to cancel (release) all pending waits which were deferred.
|
---|
136 | *
|
---|
137 | * @returns VBox status code.
|
---|
138 | * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
|
---|
139 | */
|
---|
140 | VBGLR3DECL(int) VbglR3GuestCtrlCancelPendingWaits(uint32_t u32ClientId)
|
---|
141 | {
|
---|
142 | VBoxGuestCtrlHGCMMsgCancelPendingWaits Msg;
|
---|
143 |
|
---|
144 | Msg.hdr.result = VERR_WRONG_ORDER;
|
---|
145 | Msg.hdr.u32ClientID = u32ClientId;
|
---|
146 | Msg.hdr.u32Function = GUEST_CANCEL_PENDING_WAITS;
|
---|
147 | Msg.hdr.cParms = 0;
|
---|
148 |
|
---|
149 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
150 | if (RT_SUCCESS(rc))
|
---|
151 | {
|
---|
152 | int rc2 = Msg.hdr.result;
|
---|
153 | if (RT_FAILURE(rc2))
|
---|
154 | rc = rc2;
|
---|
155 | }
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Allocates and gets host data, based on the message id.
|
---|
162 | *
|
---|
163 | * This will block until data becomes available.
|
---|
164 | *
|
---|
165 | * @returns VBox status code.
|
---|
166 | * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
|
---|
167 | * @param uNumParms
|
---|
168 | ** @todo Docs!
|
---|
169 | */
|
---|
170 | VBGLR3DECL(int) VbglR3GuestCtrlExecGetHostCmd(uint32_t u32ClientId, uint32_t uNumParms,
|
---|
171 | uint32_t *puContext,
|
---|
172 | char *pszCmd, uint32_t cbCmd,
|
---|
173 | uint32_t *puFlags,
|
---|
174 | char *pszArgs, uint32_t cbArgs, uint32_t *puNumArgs,
|
---|
175 | char *pszEnv, uint32_t *pcbEnv, uint32_t *puNumEnvVars,
|
---|
176 | char *pszUser, uint32_t cbUser,
|
---|
177 | char *pszPassword, uint32_t cbPassword,
|
---|
178 | uint32_t *puTimeLimit)
|
---|
179 | {
|
---|
180 | AssertPtr(puContext);
|
---|
181 | AssertPtr(pszCmd);
|
---|
182 | AssertPtr(puFlags);
|
---|
183 | AssertPtr(pszArgs);
|
---|
184 | AssertPtr(puNumArgs);
|
---|
185 | AssertPtr(pszEnv);
|
---|
186 | AssertPtr(pcbEnv);
|
---|
187 | AssertPtr(puNumEnvVars);
|
---|
188 | AssertPtr(pszUser);
|
---|
189 | AssertPtr(pszPassword);
|
---|
190 | AssertPtr(puTimeLimit);
|
---|
191 |
|
---|
192 | VBoxGuestCtrlHGCMMsgExecCmd Msg;
|
---|
193 |
|
---|
194 | Msg.hdr.result = VERR_WRONG_ORDER;
|
---|
195 | Msg.hdr.u32ClientID = u32ClientId;
|
---|
196 | Msg.hdr.u32Function = GUEST_GET_HOST_MSG;
|
---|
197 | Msg.hdr.cParms = uNumParms;
|
---|
198 |
|
---|
199 | VbglHGCMParmUInt32Set(&Msg.context, 0); /** @todo Put this some header struct! */
|
---|
200 | VbglHGCMParmPtrSet(&Msg.cmd, pszCmd, cbCmd);
|
---|
201 | VbglHGCMParmUInt32Set(&Msg.flags, 0);
|
---|
202 | VbglHGCMParmUInt32Set(&Msg.num_args, 0);
|
---|
203 | VbglHGCMParmPtrSet(&Msg.args, pszArgs, cbArgs);
|
---|
204 | VbglHGCMParmUInt32Set(&Msg.num_env, 0);
|
---|
205 | VbglHGCMParmUInt32Set(&Msg.cb_env, 0);
|
---|
206 | VbglHGCMParmPtrSet(&Msg.env, pszEnv, *pcbEnv);
|
---|
207 | VbglHGCMParmPtrSet(&Msg.username, pszUser, cbUser);
|
---|
208 | VbglHGCMParmPtrSet(&Msg.password, pszPassword, cbPassword);
|
---|
209 | VbglHGCMParmUInt32Set(&Msg.timeout, 0);
|
---|
210 |
|
---|
211 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
212 | if (RT_SUCCESS(rc))
|
---|
213 | {
|
---|
214 | int rc2 = Msg.hdr.result;
|
---|
215 | if (RT_FAILURE(rc2))
|
---|
216 | {
|
---|
217 | rc = rc2;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | Msg.context.GetUInt32(puContext);
|
---|
222 | Msg.flags.GetUInt32(puFlags);
|
---|
223 | Msg.num_args.GetUInt32(puNumArgs);
|
---|
224 | Msg.num_env.GetUInt32(puNumEnvVars);
|
---|
225 | Msg.cb_env.GetUInt32(pcbEnv);
|
---|
226 | Msg.timeout.GetUInt32(puTimeLimit);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Allocates and gets host data, based on the message id.
|
---|
235 | *
|
---|
236 | * This will block until data becomes available.
|
---|
237 | *
|
---|
238 | * @returns VBox status code.
|
---|
239 | * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
|
---|
240 | * @param uNumParms
|
---|
241 | ** @todo Docs!
|
---|
242 | */
|
---|
243 | VBGLR3DECL(int) VbglR3GuestCtrlExecGetHostCmdOutput(uint32_t u32ClientId, uint32_t uNumParms,
|
---|
244 | uint32_t *puContext, uint32_t *puPID,
|
---|
245 | uint32_t *puHandle, uint32_t *puFlags)
|
---|
246 | {
|
---|
247 | AssertPtr(puContext);
|
---|
248 | AssertPtr(puPID);
|
---|
249 |
|
---|
250 | VBoxGuestCtrlHGCMMsgExecOut Msg;
|
---|
251 |
|
---|
252 | Msg.hdr.result = VERR_WRONG_ORDER;
|
---|
253 | Msg.hdr.u32ClientID = u32ClientId;
|
---|
254 | Msg.hdr.u32Function = GUEST_GET_HOST_MSG;
|
---|
255 | Msg.hdr.cParms = uNumParms;
|
---|
256 |
|
---|
257 | VbglHGCMParmUInt32Set(&Msg.context, 0); /** @todo Put this some header struct! */
|
---|
258 | VbglHGCMParmUInt32Set(&Msg.pid, 0);
|
---|
259 | VbglHGCMParmUInt32Set(&Msg.handle, 0);
|
---|
260 | VbglHGCMParmUInt32Set(&Msg.flags, 0);
|
---|
261 |
|
---|
262 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
263 | if (RT_SUCCESS(rc))
|
---|
264 | {
|
---|
265 | int rc2 = Msg.hdr.result;
|
---|
266 | if (RT_FAILURE(rc2))
|
---|
267 | {
|
---|
268 | rc = rc2;
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | Msg.context.GetUInt32(puContext);
|
---|
273 | Msg.pid.GetUInt32(puPID);
|
---|
274 | Msg.handle.GetUInt32(puHandle);
|
---|
275 | Msg.flags.GetUInt32(puFlags);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Reports the process status (along with some other stuff) to the host.
|
---|
284 | *
|
---|
285 | * @returns VBox status code.
|
---|
286 | ** @todo Docs!
|
---|
287 | */
|
---|
288 | VBGLR3DECL(int) VbglR3GuestCtrlExecReportStatus(uint32_t u32ClientId,
|
---|
289 | uint32_t u32Context,
|
---|
290 | uint32_t u32PID,
|
---|
291 | uint32_t u32Status,
|
---|
292 | uint32_t u32Flags,
|
---|
293 | void *pvData,
|
---|
294 | uint32_t cbData)
|
---|
295 | {
|
---|
296 | VBoxGuestCtrlHGCMMsgExecStatus Msg;
|
---|
297 |
|
---|
298 | Msg.hdr.result = VERR_WRONG_ORDER;
|
---|
299 | Msg.hdr.u32ClientID = u32ClientId;
|
---|
300 | Msg.hdr.u32Function = GUEST_EXEC_SEND_STATUS;
|
---|
301 | Msg.hdr.cParms = 5;
|
---|
302 |
|
---|
303 | VbglHGCMParmUInt32Set(&Msg.context, u32Context);
|
---|
304 | VbglHGCMParmUInt32Set(&Msg.pid, u32PID);
|
---|
305 | VbglHGCMParmUInt32Set(&Msg.status, u32Status);
|
---|
306 | VbglHGCMParmUInt32Set(&Msg.flags, u32Flags);
|
---|
307 | VbglHGCMParmPtrSet(&Msg.data, pvData, cbData);
|
---|
308 |
|
---|
309 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
310 | if (RT_SUCCESS(rc))
|
---|
311 | {
|
---|
312 | int rc2 = Msg.hdr.result;
|
---|
313 | if (RT_FAILURE(rc2))
|
---|
314 | rc = rc2;
|
---|
315 | }
|
---|
316 | return rc;
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Sends output (from stdout/stderr) from a running process.
|
---|
322 | *
|
---|
323 | * @returns VBox status code.
|
---|
324 | ** @todo Docs!
|
---|
325 | */
|
---|
326 | VBGLR3DECL(int) VbglR3GuestCtrlExecSendOut(uint32_t u32ClientId,
|
---|
327 | uint32_t u32Context,
|
---|
328 | uint32_t u32PID,
|
---|
329 | uint32_t u32Handle,
|
---|
330 | uint32_t u32Flags,
|
---|
331 | void *pvData,
|
---|
332 | uint32_t cbData)
|
---|
333 | {
|
---|
334 | VBoxGuestCtrlHGCMMsgExecOut Msg;
|
---|
335 |
|
---|
336 | Msg.hdr.result = VERR_WRONG_ORDER;
|
---|
337 | Msg.hdr.u32ClientID = u32ClientId;
|
---|
338 | Msg.hdr.u32Function = GUEST_EXEC_SEND_OUTPUT;
|
---|
339 | Msg.hdr.cParms = 5;
|
---|
340 |
|
---|
341 | VbglHGCMParmUInt32Set(&Msg.context, u32Context);
|
---|
342 | VbglHGCMParmUInt32Set(&Msg.pid, u32PID);
|
---|
343 | VbglHGCMParmUInt32Set(&Msg.handle, u32Handle);
|
---|
344 | VbglHGCMParmUInt32Set(&Msg.flags, u32Flags);
|
---|
345 | VbglHGCMParmPtrSet(&Msg.data, pvData, cbData);
|
---|
346 |
|
---|
347 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
348 | if (RT_SUCCESS(rc))
|
---|
349 | {
|
---|
350 | int rc2 = Msg.hdr.result;
|
---|
351 | if (RT_FAILURE(rc2))
|
---|
352 | rc = rc2;
|
---|
353 | }
|
---|
354 | return rc;
|
---|
355 | }
|
---|
356 |
|
---|