VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGuestCtrl.cpp@ 28800

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/* $Id: VBoxGuestR3LibGuestCtrl.cpp 28800 2010-04-27 08:22:32Z 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
46using 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 */
55VBGLR3DECL(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 */
81VBGLR3DECL(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 */
105VBGLR3DECL(int) VbglR3GuestCtrlGetHostMsg(uint32_t u32ClientId, uint32_t *puMsg, uint32_t *puNumParms, uint32_t u32Timeout)
106{
107 AssertPtr(puMsg);
108 AssertPtr(puNumParms);
109
110 VBoxGuestCtrlHGCMMsgType Msg;
111
112 Msg.hdr.u32Timeout = u32Timeout;
113 Msg.hdr.fInterruptible = true;
114
115 Msg.hdr.info.result = VERR_WRONG_ORDER;
116 Msg.hdr.info.u32ClientID = u32ClientId;
117 Msg.hdr.info.u32Function = GUEST_GET_HOST_MSG; /* Tell the host we want our next command. */
118 Msg.hdr.info.cParms = 2; /* Just peek for the next message! */
119
120 VbglHGCMParmUInt32Set(&Msg.msg, 0);
121 VbglHGCMParmUInt32Set(&Msg.num_parms, 0);
122
123 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL_TIMED(sizeof(Msg)), &Msg, sizeof(Msg));
124 if (RT_SUCCESS(rc))
125 {
126 rc = VbglHGCMParmUInt32Get(&Msg.msg, puMsg);
127 if (RT_SUCCESS(rc))
128 rc = VbglHGCMParmUInt32Get(&Msg.num_parms, puNumParms);
129 if (RT_SUCCESS(rc))
130 rc = Msg.hdr.info.result;
131 /* Ok, so now we know what message type and how much parameters there are. */
132 }
133 return rc;
134}
135
136
137/**
138 * Allocates and gets host data, based on the message id.
139 *
140 * This will block until data becomes available.
141 *
142 * @returns VBox status code.
143 * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
144 * @param uNumParms
145 ** @todo Docs!
146 */
147VBGLR3DECL(int) VbglR3GuestCtrlExecGetHostCmd(uint32_t u32ClientId, uint32_t uNumParms,
148 uint32_t *puContext,
149 char *pszCmd, uint32_t cbCmd,
150 uint32_t *puFlags,
151 char *pszArgs, uint32_t cbArgs, uint32_t *puNumArgs,
152 char *pszEnv, uint32_t *pcbEnv, uint32_t *puNumEnvVars,
153 char *pszStdIn, uint32_t cbStdIn,
154 char *pszStdOut, uint32_t cbStdOut,
155 char *pszStdErr, uint32_t cbStdErr,
156 char *pszUser, uint32_t cbUser,
157 char *pszPassword, uint32_t cbPassword,
158 uint32_t *puTimeLimit)
159{
160 AssertPtr(puContext);
161 AssertPtr(pszCmd);
162 AssertPtr(puFlags);
163 AssertPtr(pszArgs);
164 AssertPtr(puNumArgs);
165 AssertPtr(pszEnv);
166 AssertPtr(pcbEnv);
167 AssertPtr(puNumEnvVars);
168 AssertPtr(pszStdIn);
169 AssertPtr(pszStdOut);
170 AssertPtr(pszStdOut);
171 AssertPtr(pszStdErr);
172 AssertPtr(pszUser);
173 AssertPtr(pszPassword);
174 AssertPtr(puTimeLimit);
175
176 VBoxGuestCtrlHGCMMsgExecCmd Msg;
177
178 Msg.hdr.result = VERR_WRONG_ORDER;
179 Msg.hdr.u32ClientID = u32ClientId;
180 Msg.hdr.u32Function = GUEST_GET_HOST_MSG;
181 Msg.hdr.cParms = uNumParms;
182
183 VbglHGCMParmUInt32Set(&Msg.context, 0); /** @todo Put this some header struct! */
184 VbglHGCMParmPtrSet(&Msg.cmd, pszCmd, cbCmd);
185 VbglHGCMParmUInt32Set(&Msg.flags, 0);
186 VbglHGCMParmUInt32Set(&Msg.num_args, 0);
187 VbglHGCMParmPtrSet(&Msg.args, pszArgs, cbArgs);
188 VbglHGCMParmUInt32Set(&Msg.num_env, 0);
189 VbglHGCMParmUInt32Set(&Msg.cb_env, 0);
190 VbglHGCMParmPtrSet(&Msg.env, pszEnv, *pcbEnv);
191 VbglHGCMParmPtrSet(&Msg.std_in, pszStdIn, cbStdIn);
192 VbglHGCMParmPtrSet(&Msg.std_out, pszStdOut, cbStdOut);
193 VbglHGCMParmPtrSet(&Msg.std_err, pszStdErr, cbStdErr);
194 VbglHGCMParmPtrSet(&Msg.username, pszUser, cbUser);
195 VbglHGCMParmPtrSet(&Msg.password, pszPassword, cbPassword);
196 VbglHGCMParmUInt32Set(&Msg.timeout, 0);
197
198 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
199 if (RT_SUCCESS(rc))
200 {
201 int rc2 = Msg.hdr.result;
202 if (RT_FAILURE(rc2))
203 {
204 rc = rc2;
205 }
206 else
207 {
208 Msg.context.GetUInt32(puContext);
209 Msg.flags.GetUInt32(puFlags);
210 Msg.num_args.GetUInt32(puNumArgs);
211 Msg.num_env.GetUInt32(puNumEnvVars);
212 Msg.cb_env.GetUInt32(pcbEnv);
213 Msg.timeout.GetUInt32(puTimeLimit);
214 }
215 }
216 return rc;
217}
218
219
220/**
221 * Allocates and gets host data, based on the message id.
222 *
223 * This will block until data becomes available.
224 *
225 * @returns VBox status code.
226 * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
227 * @param uNumParms
228 ** @todo Docs!
229 */
230VBGLR3DECL(int) VbglR3GuestCtrlExecGetHostCmdOutput(uint32_t u32ClientId, uint32_t uNumParms,
231 uint32_t *puContext, uint32_t *puPID,
232 uint32_t *puHandle, uint32_t *puFlags)
233{
234 AssertPtr(puContext);
235 AssertPtr(puPID);
236
237 VBoxGuestCtrlHGCMMsgExecOut Msg;
238
239 Msg.hdr.result = VERR_WRONG_ORDER;
240 Msg.hdr.u32ClientID = u32ClientId;
241 Msg.hdr.u32Function = GUEST_GET_HOST_MSG;
242 Msg.hdr.cParms = uNumParms;
243
244 VbglHGCMParmUInt32Set(&Msg.context, 0); /** @todo Put this some header struct! */
245 VbglHGCMParmUInt32Set(&Msg.pid, 0);
246 VbglHGCMParmUInt32Set(&Msg.handle, 0);
247 VbglHGCMParmUInt32Set(&Msg.flags, 0);
248
249 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
250 if (RT_SUCCESS(rc))
251 {
252 int rc2 = Msg.hdr.result;
253 if (RT_FAILURE(rc2))
254 {
255 rc = rc2;
256 }
257 else
258 {
259 Msg.context.GetUInt32(puContext);
260 Msg.pid.GetUInt32(puPID);
261 Msg.handle.GetUInt32(puHandle);
262 Msg.flags.GetUInt32(puFlags);
263 }
264 }
265 return rc;
266}
267
268
269/**
270 * Reports the process status (along with some other stuff) to the host.
271 *
272 * @returns VBox status code.
273 ** @todo Docs!
274 */
275VBGLR3DECL(int) VbglR3GuestCtrlExecReportStatus(uint32_t u32ClientId,
276 uint32_t u32Context,
277 uint32_t u32PID,
278 uint32_t u32Status,
279 uint32_t u32Flags,
280 void *pvData,
281 uint32_t cbData)
282{
283 VBoxGuestCtrlHGCMMsgExecStatus Msg;
284
285 Msg.hdr.result = VERR_WRONG_ORDER;
286 Msg.hdr.u32ClientID = u32ClientId;
287 Msg.hdr.u32Function = GUEST_EXEC_SEND_STATUS;
288 Msg.hdr.cParms = 5;
289
290 VbglHGCMParmUInt32Set(&Msg.context, u32Context);
291 VbglHGCMParmUInt32Set(&Msg.pid, u32PID);
292 VbglHGCMParmUInt32Set(&Msg.status, u32Status);
293 VbglHGCMParmUInt32Set(&Msg.flags, u32Flags);
294 VbglHGCMParmPtrSet(&Msg.data, pvData, cbData);
295
296 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
297 if (RT_SUCCESS(rc))
298 {
299 int rc2 = Msg.hdr.result;
300 if (RT_FAILURE(rc2))
301 rc = rc2;
302 }
303 return rc;
304}
305
306
307/**
308 * Sends output (from stdout/stderr) from a running process.
309 *
310 * @returns VBox status code.
311 ** @todo Docs!
312 */
313VBGLR3DECL(int) VbglR3GuestCtrlExecSendOut(uint32_t u32ClientId,
314 uint32_t u32Context,
315 uint32_t u32PID,
316 uint32_t u32Handle,
317 uint32_t u32Flags,
318 void *pvData,
319 uint32_t cbData)
320{
321 VBoxGuestCtrlHGCMMsgExecOut Msg;
322
323 Msg.hdr.result = VERR_WRONG_ORDER;
324 Msg.hdr.u32ClientID = u32ClientId;
325 Msg.hdr.u32Function = GUEST_EXEC_SEND_OUTPUT;
326 Msg.hdr.cParms = 5;
327
328 VbglHGCMParmUInt32Set(&Msg.context, u32Context);
329 VbglHGCMParmUInt32Set(&Msg.pid, u32PID);
330 VbglHGCMParmUInt32Set(&Msg.handle, u32Handle);
331 VbglHGCMParmUInt32Set(&Msg.flags, u32Flags);
332 VbglHGCMParmPtrSet(&Msg.data, pvData, cbData);
333
334 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
335 if (RT_SUCCESS(rc))
336 {
337 int rc2 = Msg.hdr.result;
338 if (RT_FAILURE(rc2))
339 rc = rc2;
340 }
341 return rc;
342}
343
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette