VirtualBox

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

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

Guest Control: Bugfixes for VBoxService.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1/* $Id: VBoxGuestR3LibGuestCtrl.cpp 28014 2010-04-06 15:20:35Z 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 VBoxGuestCtrlHGCMMsgType Msg;
112
113 Msg.hdr.result = VERR_WRONG_ORDER;
114 Msg.hdr.u32ClientID = u32ClientId;
115 Msg.hdr.u32Function = GUEST_GET_HOST_MSG; /* Tell the host we want our next command. */
116 Msg.hdr.cParms = 2;
117
118 VbglHGCMParmUInt32Set(&Msg.msg, 0);
119 VbglHGCMParmUInt32Set(&Msg.num_parms, 0);
120
121 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
122 if (RT_SUCCESS(rc))
123 {
124 rc = Msg.hdr.result;
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 /* Ok, so now we know what message type and how much parameters there are. */
131 }
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 ppvData
145 * @param uNumParms
146 */
147VBGLR3DECL(int) VbglR3GuestCtrlGetHostCmdExec(uint32_t u32ClientId, uint32_t uNumParms,
148 char *pszCmd, uint32_t cbCmd,
149 uint32_t *puFlags,
150 char *pszArgs, uint32_t cbArgs, uint32_t *puNumArgs,
151 char *pszEnv, uint32_t *pcbEnv, uint32_t *puNumEnvVars,
152 char *pszStdIn, uint32_t cbStdIn,
153 char *pszStdOut, uint32_t cbStdOut,
154 char *pszStdErr, uint32_t cbStdErr,
155 char *pszUser, uint32_t cbUser,
156 char *pszPassword, uint32_t cbPassword,
157 uint32_t *puTimeLimit)
158{
159 VBoxGuestCtrlHGCMMsgExecCmd Msg;
160
161 Msg.hdr.result = VERR_WRONG_ORDER;
162 Msg.hdr.u32ClientID = u32ClientId;
163 Msg.hdr.u32Function = GUEST_GET_HOST_MSG_DATA; /* Tell the host we want the actual data of a command. */
164 Msg.hdr.cParms = uNumParms;
165
166 VbglHGCMParmPtrSet(&Msg.cmd, pszCmd, cbCmd);
167 VbglHGCMParmUInt32Set(&Msg.flags, 0);
168 VbglHGCMParmUInt32Set(&Msg.num_args, 0);
169 VbglHGCMParmPtrSet(&Msg.args, pszArgs, cbArgs);
170 VbglHGCMParmUInt32Set(&Msg.num_env, 0);
171 VbglHGCMParmPtrSet(&Msg.env, pszEnv, *pcbEnv);
172 VbglHGCMParmPtrSet(&Msg.std_in, pszStdIn, cbStdIn);
173 VbglHGCMParmPtrSet(&Msg.std_out, pszStdOut, cbStdOut);
174 VbglHGCMParmPtrSet(&Msg.std_err, pszStdErr, cbStdErr);
175 VbglHGCMParmPtrSet(&Msg.username, pszUser, cbUser);
176 VbglHGCMParmPtrSet(&Msg.password, pszPassword, cbPassword);
177 VbglHGCMParmUInt32Set(&Msg.timeout, 0);
178
179 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
180 if (RT_SUCCESS(rc))
181 {
182 rc = Msg.hdr.result;
183
184 Msg.flags.GetUInt32(puFlags);
185 Msg.num_args.GetUInt32(puNumArgs);
186 Msg.num_env.GetUInt32(puNumEnvVars);
187 Msg.timeout.GetUInt32(puTimeLimit);
188 }
189 return rc;
190}
191
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