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