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