VirtualBox

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

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

Guest Control: Update (first stuff for piping output).

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