VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlExecThread.cpp@ 37423

Last change on this file since 37423 was 36887, checked in by vboxsync, 14 years ago

GuestCtrl: Added support for getting separated stdout/stderr output.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1/* $Id: VBoxServiceControlExecThread.cpp 36887 2011-04-29 10:04:52Z vboxsync $ */
2/** @file
3 * VBoxServiceControlExecThread - Thread for an executed guest process.
4 */
5
6/*
7 * Copyright (C) 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/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/asm.h>
23#include <iprt/assert.h>
24#include <iprt/getopt.h>
25#include <iprt/mem.h>
26#include <iprt/pipe.h>
27#include <iprt/semaphore.h>
28#include <iprt/string.h>
29
30#include <VBox/HostServices/GuestControlSvc.h>
31
32#include "VBoxServicePipeBuf.h"
33#include "VBoxServiceControlExecThread.h"
34
35extern RTLISTNODE g_GuestControlExecThreads;
36extern RTCRITSECT g_GuestControlExecThreadsCritSect;
37
38
39/**
40 * Allocates and gives back a thread data struct which then can be used by the worker thread.
41 * Needs to be freed with VBoxServiceControlExecDestroyThreadData().
42 *
43 * @return IPRT status code.
44 * @param pThread The thread's handle to allocate the data for.
45 * @param u32ContextID The context ID bound to this request / command.
46 * @param pszCmd Full qualified path of process to start (without arguments).
47 * @param uFlags Process execution flags.
48 * @param pszArgs String of arguments to pass to the process to start.
49 * @param uNumArgs Number of arguments specified in pszArgs.
50 * @param pszEnv String of environment variables ("FOO=BAR") to pass to the process
51 * to start.
52 * @param cbEnv Size (in bytes) of environment variables.
53 * @param uNumEnvVars Number of environment variables specified in pszEnv.
54 * @param pszUser User name (account) to start the process under.
55 * @param pszPassword Password of specified user name (account).
56 * @param uTimeLimitMS Time limit (in ms) of the process' life time.
57 */
58int VBoxServiceControlExecThreadAlloc(PVBOXSERVICECTRLTHREAD pThread,
59 uint32_t u32ContextID,
60 const char *pszCmd, uint32_t uFlags,
61 const char *pszArgs, uint32_t uNumArgs,
62 const char *pszEnv, uint32_t cbEnv, uint32_t uNumEnvVars,
63 const char *pszUser, const char *pszPassword, uint32_t uTimeLimitMS)
64{
65 AssertPtr(pThread);
66
67 /* General stuff. */
68 pThread->Node.pPrev = NULL;
69 pThread->Node.pNext = NULL;
70
71 pThread->fShutdown = false;
72 pThread->fStarted = false;
73 pThread->fStopped = false;
74
75 pThread->uContextID = u32ContextID;
76 /* ClientID will be assigned when thread is started! */
77
78 /* Specific stuff. */
79 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)RTMemAlloc(sizeof(VBOXSERVICECTRLTHREADDATAEXEC));
80 if (pData == NULL)
81 return VERR_NO_MEMORY;
82
83 pData->uPID = 0; /* Don't have a PID yet. */
84 pData->pszCmd = RTStrDup(pszCmd);
85 pData->uFlags = uFlags;
86 pData->uNumEnvVars = 0;
87 pData->uNumArgs = 0; /* Initialize in case of RTGetOptArgvFromString() is failing ... */
88
89 /* Prepare argument list. */
90 int rc = RTGetOptArgvFromString(&pData->papszArgs, (int*)&pData->uNumArgs,
91 (uNumArgs > 0) ? pszArgs : "", NULL);
92 /* Did we get the same result? */
93 Assert(uNumArgs == pData->uNumArgs);
94
95 if (RT_SUCCESS(rc))
96 {
97 /* Prepare environment list. */
98 if (uNumEnvVars)
99 {
100 pData->papszEnv = (char **)RTMemAlloc(uNumEnvVars * sizeof(char*));
101 AssertPtr(pData->papszEnv);
102 pData->uNumEnvVars = uNumEnvVars;
103
104 const char *pszCur = pszEnv;
105 uint32_t i = 0;
106 uint32_t cbLen = 0;
107 while (cbLen < cbEnv)
108 {
109 /* sanity check */
110 if (i >= uNumEnvVars)
111 {
112 rc = VERR_INVALID_PARAMETER;
113 break;
114 }
115 int cbStr = RTStrAPrintf(&pData->papszEnv[i++], "%s", pszCur);
116 if (cbStr < 0)
117 {
118 rc = VERR_NO_STR_MEMORY;
119 break;
120 }
121 pszCur += cbStr + 1; /* Skip terminating '\0' */
122 cbLen += cbStr + 1; /* Skip terminating '\0' */
123 }
124 }
125
126 pData->pszUser = RTStrDup(pszUser);
127 pData->pszPassword = RTStrDup(pszPassword);
128 pData->uTimeLimitMS = uTimeLimitMS;
129
130 /* Adjust time limit value. */
131 pData->uTimeLimitMS = ( uTimeLimitMS == UINT32_MAX
132 || uTimeLimitMS == 0)
133 ? RT_INDEFINITE_WAIT : uTimeLimitMS;
134
135 /* Init buffers. */
136 rc = VBoxServicePipeBufInit(&pData->stdOut, false /*fNeedNotificationPipe*/);
137 if (RT_SUCCESS(rc))
138 {
139 rc = VBoxServicePipeBufInit(&pData->stdErr, false /*fNeedNotificationPipe*/);
140 if (RT_SUCCESS(rc))
141 rc = VBoxServicePipeBufInit(&pData->stdIn, true /*fNeedNotificationPipe*/);
142 }
143 }
144
145 if (RT_FAILURE(rc))
146 {
147 VBoxServiceControlExecThreadDestroy(pData);
148 }
149 else
150 {
151 pThread->enmType = kVBoxServiceCtrlThreadDataExec;
152 pThread->pvData = pData;
153 }
154 return rc;
155}
156
157
158/**
159 * Frees an allocated thread data structure along with all its allocated parameters.
160 *
161 * @param pData Pointer to thread data to free.
162 */
163void VBoxServiceControlExecThreadDestroy(PVBOXSERVICECTRLTHREADDATAEXEC pData)
164{
165 if (pData)
166 {
167 RTStrFree(pData->pszCmd);
168 if (pData->uNumEnvVars)
169 {
170 for (uint32_t i = 0; i < pData->uNumEnvVars; i++)
171 RTStrFree(pData->papszEnv[i]);
172 RTMemFree(pData->papszEnv);
173 }
174 RTGetOptArgvFree(pData->papszArgs);
175 RTStrFree(pData->pszUser);
176 RTStrFree(pData->pszPassword);
177
178 VBoxServicePipeBufDestroy(&pData->stdOut);
179 VBoxServicePipeBufDestroy(&pData->stdErr);
180 VBoxServicePipeBufDestroy(&pData->stdIn);
181
182 RTMemFree(pData);
183 pData = NULL;
184 }
185}
186
187
188/**
189 * Finds a (formerly) started process given by its PID.
190 * Internal function, does not do locking -- this must be done from the caller function!
191 *
192 * @return PVBOXSERVICECTRLTHREAD Process structure if found, otherwise NULL.
193 * @param uPID PID to search for.
194 */
195PVBOXSERVICECTRLTHREAD vboxServiceControlExecThreadGetByPID(uint32_t uPID)
196{
197 PVBOXSERVICECTRLTHREAD pNode = NULL;
198 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
199 {
200 if ( pNode->fStarted
201 && pNode->enmType == kVBoxServiceCtrlThreadDataExec)
202 {
203 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData;
204 if (pData && pData->uPID == uPID)
205 return pNode;
206 }
207 }
208 return NULL;
209}
210
211
212/**
213 * Injects input to a specified running process.
214 *
215 * @return IPRT status code.
216 * @param uPID PID of process to set the input for.
217 * @param fPendingClose Flag indicating whether this is the last input block sent to the process.
218 * @param pBuf Pointer to a buffer containing the actual input data.
219 * @param cbSize Size (in bytes) of the input buffer data.
220 * @param pcbWritten Pointer to number of bytes written to the process. Optional.
221 */
222int VBoxServiceControlExecThreadSetInput(uint32_t uPID, bool fPendingClose, uint8_t *pBuf,
223 uint32_t cbSize, uint32_t *pcbWritten)
224{
225 AssertPtrReturn(pBuf, VERR_INVALID_PARAMETER);
226
227 int rc = RTCritSectEnter(&g_GuestControlExecThreadsCritSect);
228 if (RT_SUCCESS(rc))
229 {
230 PVBOXSERVICECTRLTHREAD pNode = vboxServiceControlExecThreadGetByPID(uPID);
231 if (pNode)
232 {
233 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData;
234 AssertPtr(pData);
235
236 if (VBoxServicePipeBufIsEnabled(&pData->stdIn))
237 {
238 uint32_t cbWritten;
239 /*
240 * Feed the data to the pipe.
241 */
242 rc = VBoxServicePipeBufWriteToBuf(&pData->stdIn, pBuf,
243 cbSize, fPendingClose, &cbWritten);
244 if (pcbWritten)
245 *pcbWritten = cbWritten;
246 }
247 else
248 {
249 /* If input buffer is not enabled anymore we cannot handle that data ... */
250 rc = VERR_BAD_PIPE;
251 }
252 }
253 else
254 rc = VERR_NOT_FOUND; /* PID not found! */
255 RTCritSectLeave(&g_GuestControlExecThreadsCritSect);
256 }
257 return rc;
258}
259
260
261/**
262 * Gets output from stdout/stderr of a specified process.
263 *
264 * @return IPRT status code.
265 * @param uPID PID of process to retrieve the output from.
266 * @param uHandleId Stream ID (stdout = 0, stderr = 2) to get the output from.
267 * @param uTimeout Timeout (in ms) to wait for output becoming available.
268 * @param pBuf Pointer to a pre-allocated buffer to store the output.
269 * @param cbSize Size (in bytes) of the pre-allocated buffer.
270 * @param pcbRead Pointer to number of bytes read. Optional.
271 */
272int VBoxServiceControlExecThreadGetOutput(uint32_t uPID, uint32_t uHandleId, uint32_t uTimeout,
273 uint8_t *pBuf, uint32_t cbSize, uint32_t *pcbRead)
274{
275 AssertPtrReturn(pBuf, VERR_INVALID_PARAMETER);
276
277 int rc = RTCritSectEnter(&g_GuestControlExecThreadsCritSect);
278 if (RT_SUCCESS(rc))
279 {
280 PVBOXSERVICECTRLTHREAD pNode = vboxServiceControlExecThreadGetByPID(uPID);
281 if (pNode)
282 {
283 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData;
284 AssertPtr(pData);
285
286 PVBOXSERVICECTRLEXECPIPEBUF pPipeBuf;
287 switch (uHandleId)
288 {
289 case OUTPUT_HANDLE_ID_STDERR: /* StdErr */
290 pPipeBuf = &pData->stdErr;
291 break;
292
293 case OUTPUT_HANDLE_ID_STDOUT: /* StdOut */
294 default:
295 pPipeBuf = &pData->stdOut;
296 break;
297 }
298 AssertPtr(pPipeBuf);
299
300 /* If the stdout pipe buffer is enabled (that is, still could be filled by a running
301 * process) wait for the signal to arrive so that we don't return without any actual
302 * data read. */
303 if (VBoxServicePipeBufIsEnabled(pPipeBuf))
304 rc = VBoxServicePipeBufWaitForEvent(pPipeBuf, uTimeout);
305
306 if (RT_SUCCESS(rc))
307 {
308 uint32_t cbRead = cbSize;
309 rc = VBoxServicePipeBufRead(pPipeBuf, pBuf, cbSize, &cbRead);
310 if (RT_SUCCESS(rc))
311 {
312 if (pcbRead)
313 *pcbRead = cbRead;
314 }
315 }
316 }
317 else
318 rc = VERR_NOT_FOUND; /* PID not found! */
319
320 int rc2 = RTCritSectLeave(&g_GuestControlExecThreadsCritSect);
321 if (RT_SUCCESS(rc))
322 rc = rc2;
323 }
324 return rc;
325}
326
327
328/**
329 * Gracefully shuts down all process execution threads.
330 *
331 */
332void VBoxServiceControlExecThreadsShutdown(void)
333{
334 int rc = RTCritSectEnter(&g_GuestControlExecThreadsCritSect);
335 if (RT_SUCCESS(rc))
336 {
337 /* Signal all threads that we want to shutdown. */
338 PVBOXSERVICECTRLTHREAD pNode;
339 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
340 ASMAtomicXchgBool(&pNode->fShutdown, true);
341
342 /* Wait for threads to shutdown. */
343 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
344 {
345 if (pNode->Thread != NIL_RTTHREAD)
346 {
347 /* Wait a bit ... */
348 int rc2 = RTThreadWait(pNode->Thread, 30 * 1000 /* Wait 30 seconds max. */, NULL);
349 if (RT_FAILURE(rc2))
350 VBoxServiceError("Control: Thread failed to stop; rc2=%Rrc\n", rc2);
351 }
352
353 /* Destroy thread specific data. */
354 switch (pNode->enmType)
355 {
356 case kVBoxServiceCtrlThreadDataExec:
357 VBoxServiceControlExecThreadDestroy((PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData);
358 break;
359
360 default:
361 break;
362 }
363 }
364
365 /* Finally destroy thread list. */
366 pNode = RTListGetFirst(&g_GuestControlExecThreads, VBOXSERVICECTRLTHREAD, Node);
367 while (pNode)
368 {
369 PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICECTRLTHREAD, Node);
370 bool fLast = RTListNodeIsLast(&g_GuestControlExecThreads, &pNode->Node);
371
372 RTListNodeRemove(&pNode->Node);
373 RTMemFree(pNode);
374
375 if (fLast)
376 break;
377
378 pNode = pNext;
379 }
380
381 int rc2 = RTCritSectLeave(&g_GuestControlExecThreadsCritSect);
382 if (RT_SUCCESS(rc))
383 rc = rc2;
384 }
385 RTCritSectDelete(&g_GuestControlExecThreadsCritSect);
386}
387
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