VirtualBox

source: vbox/trunk/src/VBox/HostServices/GuestControl/gctrl.cpp@ 28345

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

GuestControl: gcc warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1/* $Id: gctrl.cpp 28185 2010-04-12 09:05:04Z vboxsync $ */
2/** @file
3 * Guest Control Service: Internal function used by service, Main and testcase.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/** @page pg_svc_guest_control Guest Control HGCM Service
23 *
24 * @todo Write up some nice text here.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#define LOG_GROUP LOG_GROUP_HGCM
31#include <VBox/HostServices/GuestControlSvc.h>
32
33/** @todo Remove unused header files below! */
34#include <iprt/alloca.h>
35#include <iprt/initterm.h>
36#include <iprt/crc32.h>
37#include <iprt/ctype.h>
38#include <iprt/env.h>
39#include <iprt/file.h>
40#include <iprt/getopt.h>
41#include <iprt/handle.h>
42#include <iprt/mem.h>
43#include <iprt/message.h>
44#include <iprt/param.h>
45#include <iprt/path.h>
46#include <iprt/pipe.h>
47#include <iprt/poll.h>
48#include <iprt/process.h>
49#include <iprt/stream.h>
50#include <iprt/thread.h>
51
52#include "gctrl.h"
53
54namespace guestControl {
55
56/**
57 * Creates the argument list as an array used for executing a program.
58 *
59 * @returns VBox status code.
60 *
61 * @todo
62 *
63 * @todo Respect spaces when quoting for arguments, e.g. "c:\\program files\\".
64 * @todo Handle empty ("") argguments.
65 */
66int gctrlPrepareExecArgv(char *pszArgs, void **ppvList, uint32_t *pcbList, uint32_t *pcArgs)
67{
68 char **ppaArg;
69 int iArgs;
70 int rc = RTGetOptArgvFromString(&ppaArg, &iArgs, pszArgs, NULL);
71 if (RT_SUCCESS(rc))
72 {
73 char *pszTemp = NULL;
74 *pcbList = 0;
75 for (int i=0; i<iArgs; i++)
76 {
77 if (i > 0) /* Insert space as delimiter. */
78 rc = RTStrAAppendN(&pszTemp, " ", 1);
79
80 if (RT_FAILURE(rc))
81 break;
82 else
83 {
84 rc = RTStrAAppendN(&pszTemp, ppaArg[i], strlen(ppaArg[i]));
85 if (RT_FAILURE(rc))
86 break;
87 }
88 }
89 RTGetOptArgvFree(ppaArg);
90 if (RT_SUCCESS(rc))
91 {
92 *ppvList = pszTemp;
93 *pcArgs = iArgs;
94 *pcbList = strlen(pszTemp) + 1; /* Include zero termination. */
95 }
96 else
97 RTStrFree(pszTemp);
98 }
99 return rc;
100}
101
102
103/**
104 * Appends environment variables to the environment block. Each var=value pair is separated
105 * by NULL (\0) sequence. The whole block will be stored in one blob and disassembled on the
106 * guest side later to fit into the HGCM param structure.
107 *
108 * @returns VBox status code.
109 *
110 * @todo
111 *
112 */
113int gctrlAddToExecEnvv(const char *pszEnv, void **ppvList, uint32_t *pcbList, uint32_t *pcEnv)
114{
115 int rc = VINF_SUCCESS;
116 uint32_t cbLen = strlen(pszEnv);
117 if (*ppvList)
118 {
119 uint32_t cbNewLen = *pcbList + cbLen + 1; /* Include zero termination. */
120 char *pvTmp = (char*)RTMemRealloc(*ppvList, cbNewLen);
121 if (NULL == pvTmp)
122 {
123 rc = VERR_NO_MEMORY;
124 }
125 else
126 {
127 memcpy(pvTmp + *pcbList, pszEnv, cbLen);
128 pvTmp[cbNewLen - 1] = '\0'; /* Add zero termination. */
129 *ppvList = (void**)pvTmp;
130 }
131 }
132 else
133 {
134 char *pcTmp;
135 if (RTStrAPrintf(&pcTmp, "%s", pszEnv) > 0)
136 {
137 *ppvList = (void**)pcTmp;
138 /* Reset counters. */
139 *pcEnv = 0;
140 *pcbList = 0;
141 }
142 }
143 if (RT_SUCCESS(rc))
144 {
145 *pcbList += cbLen + 1; /* Include zero termination. */
146 *pcEnv += 1; /* Increase env pairs count. */
147 }
148 return rc;
149}
150
151/*
152int gctrlAllocateExecBlock(PVBOXGUESTCTRLEXECBLOCK *ppBlock,
153 const char *pszCmd, uint32_t fFlags,
154 uint32_t cArgs, const char * const *papszArgs,
155 uint32_t cEnvVars, const char * const *papszEnv,
156 const char *pszStdIn, const char *pszStdOut, const char *pszStdErr,
157 const char *pszUsername, const char *pszPassword, RTMSINTERVAL cMillies)
158{
159 PVBOXGUESTCTRLEXECBLOCK pNewBlock = (VBOXGUESTCTRLEXECBLOCK*)RTMemAlloc(sizeof(VBOXGUESTCTRLEXECBLOCK));
160 int rc;
161 if (pNewBlock)
162 {
163
164
165 *ppBlock = pNewBlock;
166 rc = VINF_SUCCESS;
167 }
168 else
169 rc = VERR_NO_MEMORY;
170 return rc;
171}
172
173
174int gctrlFreeExecBlock(PVBOXGUESTCTRLEXECBLOCK pBlock)
175{
176 AssertPtr(pBlock);
177
178 RTStrFree(pBlock->pszCmd);
179 RTMemFree(pBlock->pvArgs);
180 RTMemFree(pBlock->pvEnv);
181 RTStrFree(pBlock->pszStdIn);
182 RTStrFree(pBlock->pszStdOut);
183 RTStrFree(pBlock->pszStdErr);
184 RTStrFree(pBlock->pszUsername);
185 RTStrFree(pBlock->pszPassword);
186
187 RT_ZERO(*pBlock);
188 return VINF_SUCCESS;
189}
190
191
192int gctrlPrepareHostCmdExec(PVBOXHGCMSVCPARM *ppaParms, uint32_t *pcParms,
193 PVBOXGUESTCTRLEXECBLOCK pBlock)
194{
195 AssertPtr(ppaParms);
196 AssertPtr(pBlock);
197
198 PVBOXHGCMSVCPARM pNewParms =
199 (VBOXHGCMSVCPARM*)RTMemAlloc(sizeof(VBOXHGCMSVCPARM) * 13);
200
201 int rc;
202 if (pNewParms)
203 {
204 pNewParms[0].setUInt32(HOST_EXEC_CMD);
205 pNewParms[1].setUInt32(pBlock->u32Flags);
206 pNewParms[2].setPointer((void*)pBlock->pszCmd, (uint32_t)strlen(pBlock->pszCmd) + 1);
207 pNewParms[3].setUInt32(pBlock->u32Args);
208 pNewParms[4].setPointer((void*)pBlock->pvArgs, pBlock->cbArgs);
209 pNewParms[5].setUInt32(pBlock->u32EnvVars);
210 pNewParms[6].setPointer((void*)pBlock->pvEnv, pBlock->cbEnv);
211 pNewParms[7].setPointer((void*)pBlock->pszStdIn, (uint32_t)strlen(pBlock->pszStdIn) + 1);
212 pNewParms[8].setPointer((void*)pBlock->pszStdOut, (uint32_t)strlen(pBlock->pszStdOut) + 1);
213 pNewParms[9].setPointer((void*)pBlock->pszStdErr, (uint32_t)strlen(pBlock->pszStdErr) + 1);
214 pNewParms[10].setPointer((void*)pBlock->pszUsername, (uint32_t)strlen(pBlock->pszUsername) + 1);
215 pNewParms[11].setPointer((void*)pBlock->pszPassword, (uint32_t)strlen(pBlock->pszPassword) + 1);
216 pNewParms[12].setUInt32(pBlock->cMillies);
217
218 *ppaParms = pNewParms;
219 rc = VINF_SUCCESS;
220 }
221 else
222 rc = VERR_NO_MEMORY;
223
224 if (pcParms)
225 *pcParms = 13;
226
227 return rc;
228}
229
230
231void gctrlFreeHostCmd(PVBOXHGCMSVCPARM paParms)
232{
233 RTMemFree(paParms);
234}
235*/
236
237}
238
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