VirtualBox

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

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

Guest Control: Update; now buffered and deferred commands work.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/* $Id: VBoxGuestR3LibGuestCtrl.cpp 28086 2010-04-08 12:32:07Z 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)
110{
111 AssertPtr(puMsg);
112 AssertPtr(puNumParms);
113
114 VBoxGuestCtrlHGCMMsgType Msg;
115
116 Msg.hdr.result = VERR_WRONG_ORDER;
117 Msg.hdr.u32ClientID = u32ClientId;
118 Msg.hdr.u32Function = GUEST_GET_HOST_MSG; /* Tell the host we want our next command. */
119 Msg.hdr.cParms = 2;
120
121 VbglHGCMParmUInt32Set(&Msg.msg, 0);
122 VbglHGCMParmUInt32Set(&Msg.num_parms, 0);
123
124 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
125 if (RT_SUCCESS(rc))
126 {
127 rc = VbglHGCMParmUInt32Get(&Msg.msg, puMsg);
128 if (RT_SUCCESS(rc))
129 rc = VbglHGCMParmUInt32Get(&Msg.num_parms, puNumParms);
130 if (RT_SUCCESS(rc))
131 rc = Msg.hdr.result;
132 /* Ok, so now we know what message type and how much parameters there are. */
133 }
134 return rc;
135}
136
137
138/**
139 * Allocates and gets host data, based on the message id.
140 *
141 * This will block until data becomes available.
142 *
143 * @returns VBox status code.
144 * @param u32ClientId The client id returned by VbglR3GuestCtrlConnect().
145 * @param ppvData
146 * @param uNumParms
147 */
148VBGLR3DECL(int) VbglR3GuestCtrlGetHostCmdExec(uint32_t u32ClientId, uint32_t uNumParms,
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(pszCmd);
161 AssertPtr(puFlags);
162 AssertPtr(pszArgs);
163 AssertPtr(puNumArgs);
164 AssertPtr(pszEnv);
165 AssertPtr(pcbEnv);
166 AssertPtr(puNumEnvVars);
167 AssertPtr(pszStdIn);
168 AssertPtr(pszStdOut);
169 AssertPtr(pszStdOut);
170 AssertPtr(pszStdErr);
171 AssertPtr(pszUser);
172 AssertPtr(pszPassword);
173 AssertPtr(puTimeLimit);
174
175 VBoxGuestCtrlHGCMMsgExecCmd Msg;
176
177 Msg.hdr.result = VERR_WRONG_ORDER;
178 Msg.hdr.u32ClientID = u32ClientId;
179 Msg.hdr.u32Function = GUEST_GET_HOST_MSG;
180 Msg.hdr.cParms = uNumParms;
181
182 VbglHGCMParmPtrSet(&Msg.cmd, pszCmd, cbCmd);
183 VbglHGCMParmUInt32Set(&Msg.flags, 0);
184 VbglHGCMParmUInt32Set(&Msg.num_args, 0);
185 VbglHGCMParmPtrSet(&Msg.args, pszArgs, cbArgs);
186 VbglHGCMParmUInt32Set(&Msg.num_env, 0);
187 VbglHGCMParmUInt32Set(&Msg.cb_env, 0);
188 VbglHGCMParmPtrSet(&Msg.env, pszEnv, *pcbEnv);
189 VbglHGCMParmPtrSet(&Msg.std_in, pszStdIn, cbStdIn);
190 VbglHGCMParmPtrSet(&Msg.std_out, pszStdOut, cbStdOut);
191 VbglHGCMParmPtrSet(&Msg.std_err, pszStdErr, cbStdErr);
192 VbglHGCMParmPtrSet(&Msg.username, pszUser, cbUser);
193 VbglHGCMParmPtrSet(&Msg.password, pszPassword, cbPassword);
194 VbglHGCMParmUInt32Set(&Msg.timeout, 0);
195
196 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
197 if (RT_SUCCESS(rc))
198 {
199 int rc2 = Msg.hdr.result;
200 if (RT_FAILURE(rc2))
201 {
202 rc = rc2;
203 }
204 else
205 {
206 Msg.flags.GetUInt32(puFlags);
207 Msg.num_args.GetUInt32(puNumArgs);
208 Msg.num_env.GetUInt32(puNumEnvVars);
209 Msg.cb_env.GetUInt32(pcbEnv);
210 Msg.timeout.GetUInt32(puTimeLimit);
211 }
212 }
213 return rc;
214}
215
Note: See TracBrowser for help on using the repository browser.

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