VirtualBox

source: vbox/trunk/src/VBox/Main/ExtPackManagerImpl.cpp@ 33944

Last change on this file since 33944 was 33806, checked in by vboxsync, 14 years ago

ExtPack changes, related IPRT changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 56.9 KB
Line 
1/* $Id: ExtPackManagerImpl.cpp 33806 2010-11-05 17:20:15Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for Extension Packs, VBoxSVC & VBoxC.
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
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "ExtPackManagerImpl.h"
23#include "ExtPackUtil.h"
24
25#include <iprt/buildconfig.h>
26#include <iprt/ctype.h>
27#include <iprt/dir.h>
28#include <iprt/env.h>
29#include <iprt/file.h>
30#include <iprt/ldr.h>
31#include <iprt/param.h>
32#include <iprt/path.h>
33#include <iprt/pipe.h>
34#include <iprt/process.h>
35#include <iprt/string.h>
36
37#include <VBox/com/array.h>
38#include <VBox/com/ErrorInfo.h>
39#include <VBox/log.h>
40#include <VBox/sup.h>
41#include <VBox/version.h>
42#include "AutoCaller.h"
43#include "Global.h"
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
49/** @name VBOX_EXTPACK_HELPER_NAME
50 * The name of the utility application we employ to install and uninstall the
51 * extension packs. This is a set-uid-to-root binary on unixy platforms, which
52 * is why it has to be a separate application.
53 */
54#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
55# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelper.exe"
56#else
57# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelper"
58#endif
59
60
61/*******************************************************************************
62* Structures and Typedefs *
63*******************************************************************************/
64/**
65 * Private extension pack data.
66 */
67struct ExtPack::Data
68{
69public:
70 /** The extension pack description (loaded from the XML, mostly). */
71 VBOXEXTPACKDESC Desc;
72 /** The file system object info of the XML file.
73 * This is for detecting changes and save time in refresh(). */
74 RTFSOBJINFO ObjInfoDesc;
75 /** Whether it's usable or not. */
76 bool fUsable;
77 /** Why it is unusable. */
78 Utf8Str strWhyUnusable;
79
80 /** Where the extension pack is located. */
81 Utf8Str strExtPackPath;
82 /** The file system object info of the extension pack directory.
83 * This is for detecting changes and save time in refresh(). */
84 RTFSOBJINFO ObjInfoExtPack;
85 /** The full path to the main module. */
86 Utf8Str strMainModPath;
87 /** The file system object info of the main module.
88 * This is used to determin whether to bother try reload it. */
89 RTFSOBJINFO ObjInfoMainMod;
90 /** The module handle of the main extension pack module. */
91 RTLDRMOD hMainMod;
92
93 /** The helper callbacks for the extension pack. */
94 VBOXEXTPACKHLP Hlp;
95 /** Pointer back to the extension pack object (for Hlp methods). */
96 ExtPack *pThis;
97 /** The extension pack registration structure. */
98 PCVBOXEXTPACKREG pReg;
99};
100
101/** List of extension packs. */
102typedef std::list< ComObjPtr<ExtPack> > ExtPackList;
103
104/**
105 * Private extension pack manager data.
106 */
107struct ExtPackManager::Data
108{
109 /** The directory where the extension packs are installed. */
110 Utf8Str strBaseDir;
111 /** The directory where the extension packs can be dropped for automatic
112 * installation. */
113 Utf8Str strDropZoneDir;
114 /** The directory where the certificates this installation recognizes are
115 * stored. */
116 Utf8Str strCertificatDirPath;
117 /** The list of installed extension packs. */
118 ExtPackList llInstalledExtPacks;
119};
120
121
122DEFINE_EMPTY_CTOR_DTOR(ExtPack)
123
124/**
125 * Called by ComObjPtr::createObject when creating the object.
126 *
127 * Just initialize the basic object state, do the rest in init().
128 *
129 * @returns S_OK.
130 */
131HRESULT ExtPack::FinalConstruct()
132{
133 m = NULL;
134 return S_OK;
135}
136
137/**
138 * Initializes the extension pack by reading its file.
139 *
140 * @returns COM status code.
141 * @param a_pszName The name of the extension pack. This is also the
142 * name of the subdirector under @a a_pszParentDir
143 * where the extension pack is installed.
144 * @param a_pszParentDir The parent directory.
145 */
146HRESULT ExtPack::init(const char *a_pszName, const char *a_pszParentDir)
147{
148 static const VBOXEXTPACKHLP s_HlpTmpl =
149 {
150 /* u32Version = */ VBOXEXTPACKHLP_VERSION,
151 /* uVBoxFullVersion = */ VBOX_FULL_VERSION,
152 /* uVBoxVersionRevision = */ 0,
153 /* u32Padding = */ 0,
154 /* pszVBoxVersion = */ "",
155 /* pfnFindModule = */ ExtPack::hlpFindModule,
156 /* u32EndMarker = */ VBOXEXTPACKHLP_VERSION
157 };
158
159 /*
160 * Figure out where we live and allocate + initialize our private data.
161 */
162 char szDir[RTPATH_MAX];
163 int vrc = RTPathJoin(szDir, sizeof(szDir), a_pszParentDir, a_pszName);
164 AssertLogRelRCReturn(vrc, E_FAIL);
165
166 m = new Data;
167 m->Desc.strName = a_pszName;
168 RT_ZERO(m->ObjInfoDesc);
169 m->fUsable = false;
170 m->strWhyUnusable = tr("ExtPack::init failed");
171 m->strExtPackPath = szDir;
172 RT_ZERO(m->ObjInfoExtPack);
173 m->strMainModPath.setNull();
174 RT_ZERO(m->ObjInfoMainMod);
175 m->hMainMod = NIL_RTLDRMOD;
176 m->Hlp = s_HlpTmpl;
177 m->Hlp.pszVBoxVersion = RTBldCfgVersion();
178 m->Hlp.uVBoxInternalRevision = RTBldCfgRevision();
179 m->pThis = this;
180 m->pReg = NULL;
181
182 /*
183 * Probe the extension pack (this code is shared with refresh()).
184 */
185 probeAndLoad();
186
187 return S_OK;
188}
189
190/**
191 * COM cruft.
192 */
193void ExtPack::FinalRelease()
194{
195 uninit();
196}
197
198/**
199 * Do the actual cleanup.
200 */
201void ExtPack::uninit()
202{
203 /* Enclose the state transition Ready->InUninit->NotReady */
204 AutoUninitSpan autoUninitSpan(this);
205 if (!autoUninitSpan.uninitDone() && m != NULL)
206 {
207 if (m->hMainMod != NIL_RTLDRMOD)
208 {
209 AssertPtr(m->pReg);
210 if (m->pReg->pfnUnload != NULL)
211 m->pReg->pfnUnload(m->pReg);
212
213 RTLdrClose(m->hMainMod);
214 m->hMainMod = NIL_RTLDRMOD;
215 m->pReg = NULL;
216 }
217
218 delete m;
219 m = NULL;
220 }
221}
222
223
224/**
225 * Calls the installed hook.
226 * @remarks Caller holds the extension manager lock.
227 */
228void ExtPack::callInstalledHook(void)
229{
230 if ( m->hMainMod != NIL_RTLDRMOD
231 && m->pReg->pfnInstalled)
232 m->pReg->pfnInstalled(m->pReg);
233}
234
235/**
236 * Calls the uninstall hook and closes the module.
237 *
238 * @returns S_OK or COM error status with error information.
239 * @param a_fForcedRemoval When set, we'll ignore complaints from the
240 * uninstall hook.
241 * @remarks The caller holds the manager's write lock.
242 */
243HRESULT ExtPack::callUninstallHookAndClose(bool a_fForcedRemoval)
244{
245 HRESULT hrc = S_OK;
246
247 if (m->hMainMod != NIL_RTLDRMOD)
248 {
249 if (m->pReg->pfnUninstall)
250 {
251 int vrc = m->pReg->pfnUninstall(m->pReg);
252 if (RT_FAILURE(vrc))
253 {
254 LogRel(("ExtPack pfnUninstall returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
255 if (!a_fForcedRemoval)
256 hrc = setError(E_FAIL, tr("pfnUninstall returned %Rrc"), vrc);
257 }
258 }
259 if (SUCCEEDED(hrc))
260 {
261 RTLdrClose(m->hMainMod);
262 m->hMainMod = NIL_RTLDRMOD;
263 m->pReg = NULL;
264 }
265 }
266
267 return hrc;
268}
269
270/**
271 * Calls the pfnVMCreate hook.
272 *
273 * @param a_pMachine The machine interface of the new VM.
274 * @remarks Caller holds the extension manager lock.
275 */
276void ExtPack::callVmCreatedHook(IMachine *a_pMachine)
277{
278 if ( m->hMainMod != NIL_RTLDRMOD
279 && m->pReg->pfnVMCreated)
280 m->pReg->pfnVMCreated(m->pReg, a_pMachine);
281}
282
283/**
284 * Calls the pfnVMConfigureVMM hook.
285 *
286 * @returns VBox status code, LogRel called on failure.
287 * @param a_pConsole The console interface.
288 * @param a_pVM The VM handle.
289 * @remarks Caller holds the extension manager lock.
290 */
291int ExtPack::callVmConfigureVmmHook(IConsole *a_pConsole, PVM a_pVM)
292{
293 if ( m->hMainMod != NIL_RTLDRMOD
294 && m->pReg->pfnVMConfigureVMM)
295 {
296 int vrc = m->pReg->pfnVMConfigureVMM(m->pReg, a_pConsole, a_pVM);
297 if (RT_FAILURE(vrc))
298 {
299 LogRel(("ExtPack pfnVMConfigureVMM returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
300 return vrc;
301 }
302 }
303 return VINF_SUCCESS;
304}
305
306/**
307 * Calls the pfnVMPowerOn hook.
308 *
309 * @returns VBox status code, LogRel called on failure.
310 * @param a_pConsole The console interface.
311 * @param a_pVM The VM handle.
312 * @remarks Caller holds the extension manager lock.
313 */
314int ExtPack::callVmPowerOnHook(IConsole *a_pConsole, PVM a_pVM)
315{
316 if ( m->hMainMod != NIL_RTLDRMOD
317 && m->pReg->pfnVMPowerOn)
318 {
319 int vrc = m->pReg->pfnVMPowerOn(m->pReg, a_pConsole, a_pVM);
320 if (RT_FAILURE(vrc))
321 {
322 LogRel(("ExtPack pfnVMPowerOn returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
323 return vrc;
324 }
325 }
326 return VINF_SUCCESS;
327}
328
329/**
330 * Calls the pfnVMPowerOff hook.
331 *
332 * @param a_pConsole The console interface.
333 * @param a_pVM The VM handle.
334 * @remarks Caller holds the extension manager lock.
335 */
336void ExtPack::callVmPowerOffHook(IConsole *a_pConsole, PVM a_pVM)
337{
338 if ( m->hMainMod != NIL_RTLDRMOD
339 && m->pReg->pfnVMPowerOff)
340 m->pReg->pfnVMPowerOff(m->pReg, a_pConsole, a_pVM);
341}
342
343/**
344 * Refreshes the extension pack state.
345 *
346 * This is called by the manager so that the on disk changes are picked up.
347 *
348 * @returns S_OK or COM error status with error information.
349 * @param pfCanDelete Optional can-delete-this-object output indicator.
350 * @remarks Caller holds the extension manager lock for writing.
351 */
352HRESULT ExtPack::refresh(bool *pfCanDelete)
353{
354 if (pfCanDelete)
355 *pfCanDelete = false;
356
357 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS); /* for the COMGETTERs */
358
359 /*
360 * Has the module been deleted?
361 */
362 RTFSOBJINFO ObjInfoExtPack;
363 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
364 if ( RT_FAILURE(vrc)
365 || !RTFS_IS_DIRECTORY(ObjInfoExtPack.Attr.fMode))
366 {
367 if (pfCanDelete)
368 *pfCanDelete = true;
369 return S_OK;
370 }
371
372 /*
373 * We've got a directory, so try query file system object info for the
374 * files we are interested in as well.
375 */
376 RTFSOBJINFO ObjInfoDesc;
377 char szDescFilePath[RTPATH_MAX];
378 vrc = RTPathJoin(szDescFilePath, sizeof(szDescFilePath), m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME);
379 if (RT_SUCCESS(vrc))
380 vrc = RTPathQueryInfoEx(szDescFilePath, &ObjInfoDesc, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
381 if (RT_FAILURE(vrc))
382 RT_ZERO(ObjInfoDesc);
383
384 RTFSOBJINFO ObjInfoMainMod;
385 if (m->strMainModPath.isNotEmpty())
386 vrc = RTPathQueryInfoEx(m->strMainModPath.c_str(), &ObjInfoMainMod, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
387 if (m->strMainModPath.isEmpty() || RT_FAILURE(vrc))
388 RT_ZERO(ObjInfoMainMod);
389
390 /*
391 * If we have a usable module already, just verify that things haven't
392 * changed since we loaded it.
393 */
394 if (m->fUsable)
395 {
396 /** @todo not important, so it can wait. */
397 }
398 /*
399 * Ok, it is currently not usable. If anything has changed since last time
400 * reprobe the extension pack.
401 */
402 else if ( !objinfoIsEqual(&ObjInfoDesc, &m->ObjInfoDesc)
403 || !objinfoIsEqual(&ObjInfoMainMod, &m->ObjInfoMainMod)
404 || !objinfoIsEqual(&ObjInfoExtPack, &m->ObjInfoExtPack) )
405 probeAndLoad();
406
407 return S_OK;
408}
409
410/**
411 * Probes the extension pack, loading the main dll and calling its registration
412 * entry point.
413 *
414 * This updates the state accordingly, the strWhyUnusable and fUnusable members
415 * being the most important ones.
416 */
417void ExtPack::probeAndLoad(void)
418{
419 m->fUsable = true;
420
421 /*
422 * Query the file system info for the extension pack directory. This and
423 * all other file system info we save is for the benefit of refresh().
424 */
425 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &m->ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
426 if (RT_FAILURE(vrc))
427 {
428 m->strWhyUnusable.printf(tr("RTPathQueryInfoEx on '%s' failed: %Rrc"), m->strExtPackPath.c_str(), vrc);
429 return;
430 }
431 if (RTFS_IS_DIRECTORY(m->ObjInfoExtPack.Attr.fMode))
432 {
433 if (RTFS_IS_SYMLINK(m->ObjInfoExtPack.Attr.fMode))
434 m->strWhyUnusable.printf(tr("'%s' is a symbolic link, this is not allowed"), m->strExtPackPath.c_str(), vrc);
435 else if (RTFS_IS_FILE(m->ObjInfoExtPack.Attr.fMode))
436 m->strWhyUnusable.printf(tr("'%s' is a symbolic file, not a directory"), m->strExtPackPath.c_str(), vrc);
437 else
438 m->strWhyUnusable.printf(tr("'%s' is not a directory (fMode=%#x)"), m->strExtPackPath.c_str(), m->ObjInfoExtPack.Attr.fMode);
439 return;
440 }
441
442 char szErr[2048];
443 RT_ZERO(szErr);
444 vrc = SUPR3HardenedVerifyDir(m->strExtPackPath.c_str(), true /*fRecursive*/, true /*fCheckFiles*/, szErr, sizeof(szErr));
445 if (RT_FAILURE(vrc))
446 {
447 m->strWhyUnusable.printf(tr("%s (rc=%Rrc)"), szErr, vrc);
448 return;
449 }
450
451 /*
452 * Read the description file.
453 */
454 iprt::MiniString strSavedName(m->Desc.strName);
455 iprt::MiniString *pStrLoadErr = VBoxExtPackLoadDesc(m->strExtPackPath.c_str(), &m->Desc, &m->ObjInfoDesc);
456 if (pStrLoadErr != NULL)
457 {
458 m->strWhyUnusable.printf(tr("Failed to load '%s/%s': %s"),
459 m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME, pStrLoadErr->c_str());
460 m->Desc.strName = strSavedName;
461 delete pStrLoadErr;
462 return;
463 }
464
465 /*
466 * Make sure the XML name and directory matches.
467 */
468 if (!m->Desc.strName.equalsIgnoreCase(strSavedName))
469 {
470 m->strWhyUnusable.printf(tr("The description name ('%s') and directory name ('%s) does not match"),
471 m->Desc.strName.c_str(), strSavedName.c_str());
472 m->Desc.strName = strSavedName;
473 return;
474 }
475
476 /*
477 * Load the main DLL and call the predefined entry point.
478 */
479 bool fIsNative;
480 if (!findModule(m->Desc.strMainModule.c_str(), NULL /* default extension */,
481 &m->strMainModPath, &fIsNative, &m->ObjInfoMainMod))
482 {
483 m->strWhyUnusable.printf(tr("Failed to locate the main module ('%'s)"), m->Desc.strMainModule.c_str());
484 return;
485 }
486
487 vrc = SUPR3HardenedVerifyPlugIn(m->strMainModPath.c_str(), szErr, sizeof(szErr));
488 if (RT_FAILURE(vrc))
489 {
490 m->strWhyUnusable.printf(tr("%s"), szErr);
491 return;
492 }
493
494 if (fIsNative)
495 {
496 vrc = RTLdrLoad(m->strMainModPath.c_str(), &m->hMainMod);
497 if (RT_FAILURE(vrc))
498 {
499 m->hMainMod = NIL_RTLDRMOD;
500 m->strWhyUnusable.printf(tr("Failed to locate load the main module ('%'s): %Rrc"),
501 m->strMainModPath.c_str(), vrc);
502 return;
503 }
504 }
505 else
506 {
507 m->strWhyUnusable.printf(tr("Only native main modules are currently supported"));
508 return;
509 }
510
511 /*
512 * Resolve the predefined entry point.
513 */
514 PFNVBOXEXTPACKREGISTER pfnRegistration;
515 vrc = RTLdrGetSymbol(m->hMainMod, VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, (void **)&pfnRegistration);
516 if (RT_SUCCESS(vrc))
517 {
518 RT_ZERO(szErr);
519 vrc = pfnRegistration(&m->Hlp, &m->pReg, szErr, sizeof(szErr) - 16);
520 if ( RT_SUCCESS(vrc)
521 && szErr[0] == '\0'
522 && VALID_PTR(m->pReg))
523 {
524 if ( m->pReg->u32Version == VBOXEXTPACKREG_VERSION
525 && m->pReg->u32EndMarker == VBOXEXTPACKREG_VERSION)
526 {
527 if ( (!m->pReg->pfnInstalled || RT_VALID_PTR(m->pReg->pfnInstalled))
528 && (!m->pReg->pfnUninstall || RT_VALID_PTR(m->pReg->pfnUninstall))
529 && (!m->pReg->pfnUnload || RT_VALID_PTR(m->pReg->pfnUnload))
530 && (!m->pReg->pfnVMCreated || RT_VALID_PTR(m->pReg->pfnVMCreated))
531 && (!m->pReg->pfnVMConfigureVMM || RT_VALID_PTR(m->pReg->pfnVMConfigureVMM))
532 && (!m->pReg->pfnVMPowerOn || RT_VALID_PTR(m->pReg->pfnVMPowerOn))
533 && (!m->pReg->pfnVMPowerOff || RT_VALID_PTR(m->pReg->pfnVMPowerOff))
534 )
535 {
536 /*
537 * We're good!
538 */
539 m->fUsable = true;
540 m->strWhyUnusable.setNull();
541 return;
542 }
543
544 m->strWhyUnusable = tr("The registration structure contains on or more invalid function pointers");
545 }
546 else
547 m->strWhyUnusable.printf(tr("Unsupported registration structure version %u.%u"),
548 RT_HIWORD(m->pReg->u32Version), RT_LOWORD(m->pReg->u32Version));
549 }
550 else
551 {
552 szErr[sizeof(szErr) - 1] = '\0';
553 m->strWhyUnusable.printf(tr("%s returned %Rrc, pReg=%p szErr='%s'"),
554 VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, vrc, m->pReg, szErr);
555 }
556 m->pReg = NULL;
557 }
558
559 RTLdrClose(m->hMainMod);
560 m->hMainMod = NIL_RTLDRMOD;
561}
562
563/**
564 * Finds a module.
565 *
566 * @returns true if found, false if not.
567 * @param a_pszName The module base name (no extension).
568 * @param a_pszExt The extension. If NULL we use default
569 * extensions.
570 * @param a_pStrFound Where to return the path to the module we've
571 * found.
572 * @param a_pfNative Where to return whether this is a native module
573 * or an agnostic one. Optional.
574 * @param a_pObjInfo Where to return the file system object info for
575 * the module. Optional.
576 */
577bool ExtPack::findModule(const char *a_pszName, const char *a_pszExt,
578 Utf8Str *a_pStrFound, bool *a_pfNative, PRTFSOBJINFO a_pObjInfo) const
579{
580 /*
581 * Try the native path first.
582 */
583 char szPath[RTPATH_MAX];
584 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetDotArch());
585 AssertLogRelRCReturn(vrc, false);
586 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
587 AssertLogRelRCReturn(vrc, false);
588 if (!a_pszExt)
589 {
590 vrc = RTStrCat(szPath, sizeof(szPath), RTLdrGetSuff());
591 AssertLogRelRCReturn(vrc, false);
592 }
593
594 RTFSOBJINFO ObjInfo;
595 if (!a_pObjInfo)
596 a_pObjInfo = &ObjInfo;
597 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
598 if (RT_SUCCESS(vrc) && RTFS_IS_FIFO(a_pObjInfo->Attr.fMode))
599 {
600 if (a_pfNative)
601 *a_pfNative = true;
602 a_pStrFound = new Utf8Str(szPath);
603 return true;
604 }
605
606 /*
607 * Try the platform agnostic modules.
608 */
609 /* gcc.x86/module.rel */
610 char szSubDir[32];
611 RTStrPrintf(szSubDir, sizeof(szSubDir), "%s.%s", RTBldCfgCompiler(), RTBldCfgTargetArch());
612 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szSubDir);
613 AssertLogRelRCReturn(vrc, false);
614 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
615 AssertLogRelRCReturn(vrc, false);
616 if (!a_pszExt)
617 {
618 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
619 AssertLogRelRCReturn(vrc, false);
620 }
621 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
622 if (RT_SUCCESS(vrc) && RTFS_IS_FIFO(a_pObjInfo->Attr.fMode))
623 {
624 if (a_pfNative)
625 *a_pfNative = false;
626 a_pStrFound = new Utf8Str(szPath);
627 return true;
628 }
629
630 /* x86/module.rel */
631 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetArch());
632 AssertLogRelRCReturn(vrc, false);
633 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
634 AssertLogRelRCReturn(vrc, false);
635 if (!a_pszExt)
636 {
637 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
638 AssertLogRelRCReturn(vrc, false);
639 }
640 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
641 if (RT_SUCCESS(vrc) && RTFS_IS_FIFO(a_pObjInfo->Attr.fMode))
642 {
643 if (a_pfNative)
644 *a_pfNative = false;
645 a_pStrFound = new Utf8Str(szPath);
646 return true;
647 }
648
649 return false;
650}
651
652/**
653 * Compares two file system object info structures.
654 *
655 * @returns true if equal, false if not.
656 * @param pObjInfo1 The first.
657 * @param pObjInfo2 The second.
658 * @todo IPRT should do this, really.
659 */
660/* static */ bool ExtPack::objinfoIsEqual(PCRTFSOBJINFO pObjInfo1, PCRTFSOBJINFO pObjInfo2)
661{
662 if (!RTTimeSpecIsEqual(&pObjInfo1->ModificationTime, &pObjInfo2->ModificationTime))
663 return false;
664 if (!RTTimeSpecIsEqual(&pObjInfo1->ChangeTime, &pObjInfo2->ChangeTime))
665 return false;
666 if (!RTTimeSpecIsEqual(&pObjInfo1->BirthTime, &pObjInfo2->BirthTime))
667 return false;
668 if (pObjInfo1->cbObject != pObjInfo2->cbObject)
669 return false;
670 if (pObjInfo1->Attr.fMode != pObjInfo2->Attr.fMode)
671 return false;
672 if (pObjInfo1->Attr.enmAdditional == pObjInfo2->Attr.enmAdditional)
673 {
674 switch (pObjInfo1->Attr.enmAdditional)
675 {
676 case RTFSOBJATTRADD_UNIX:
677 if (pObjInfo1->Attr.u.Unix.uid != pObjInfo2->Attr.u.Unix.uid)
678 return false;
679 if (pObjInfo1->Attr.u.Unix.gid != pObjInfo2->Attr.u.Unix.gid)
680 return false;
681 if (pObjInfo1->Attr.u.Unix.INodeIdDevice != pObjInfo2->Attr.u.Unix.INodeIdDevice)
682 return false;
683 if (pObjInfo1->Attr.u.Unix.INodeId != pObjInfo2->Attr.u.Unix.INodeId)
684 return false;
685 if (pObjInfo1->Attr.u.Unix.GenerationId != pObjInfo2->Attr.u.Unix.GenerationId)
686 return false;
687 break;
688 default:
689 break;
690 }
691 }
692 return true;
693}
694
695
696/**
697 * @interface_method_impl{VBOXEXTPACKHLP,pfnFindModule}
698 */
699/*static*/ DECLCALLBACK(int)
700ExtPack::hlpFindModule(PCVBOXEXTPACKHLP pHlp, const char *pszName, const char *pszExt,
701 char *pszFound, size_t cbFound, bool *pfNative)
702{
703 /*
704 * Validate the input and get our bearings.
705 */
706 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
707 AssertPtrNullReturn(pszExt, VERR_INVALID_POINTER);
708 AssertPtrReturn(pszFound, VERR_INVALID_POINTER);
709 AssertPtrNullReturn(pfNative, VERR_INVALID_POINTER);
710
711 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
712 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
713 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
714 AssertPtrReturn(m, VERR_INVALID_POINTER);
715 ExtPack *pThis = m->pThis;
716 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
717
718 /*
719 * This is just a wrapper around findModule.
720 */
721 Utf8Str strFound;
722 if (pThis->findModule(pszName, pszExt, &strFound, pfNative, NULL))
723 return RTStrCopy(pszFound, cbFound, strFound.c_str());
724 return VERR_FILE_NOT_FOUND;
725}
726
727
728
729
730
731STDMETHODIMP ExtPack::COMGETTER(Name)(BSTR *a_pbstrName)
732{
733 CheckComArgOutPointerValid(a_pbstrName);
734
735 AutoCaller autoCaller(this);
736 HRESULT hrc = autoCaller.rc();
737 if (SUCCEEDED(hrc))
738 {
739 Bstr str(m->Desc.strName);
740 str.cloneTo(a_pbstrName);
741 }
742 return hrc;
743}
744
745STDMETHODIMP ExtPack::COMGETTER(Description)(BSTR *a_pbstrDescription)
746{
747 CheckComArgOutPointerValid(a_pbstrDescription);
748
749 AutoCaller autoCaller(this);
750 HRESULT hrc = autoCaller.rc();
751 if (SUCCEEDED(hrc))
752 {
753 Bstr str(m->Desc.strDescription);
754 str.cloneTo(a_pbstrDescription);
755 }
756 return hrc;
757}
758
759STDMETHODIMP ExtPack::COMGETTER(Version)(BSTR *a_pbstrVersion)
760{
761 CheckComArgOutPointerValid(a_pbstrVersion);
762
763 AutoCaller autoCaller(this);
764 HRESULT hrc = autoCaller.rc();
765 if (SUCCEEDED(hrc))
766 {
767 Bstr str(m->Desc.strVersion);
768 str.cloneTo(a_pbstrVersion);
769 }
770 return hrc;
771}
772
773STDMETHODIMP ExtPack::COMGETTER(Revision)(ULONG *a_puRevision)
774{
775 CheckComArgOutPointerValid(a_puRevision);
776
777 AutoCaller autoCaller(this);
778 HRESULT hrc = autoCaller.rc();
779 if (SUCCEEDED(hrc))
780 *a_puRevision = m->Desc.uRevision;
781 return hrc;
782}
783
784STDMETHODIMP ExtPack::COMGETTER(Usable)(BOOL *a_pfUsable)
785{
786 CheckComArgOutPointerValid(a_pfUsable);
787
788 AutoCaller autoCaller(this);
789 HRESULT hrc = autoCaller.rc();
790 if (SUCCEEDED(hrc))
791 *a_pfUsable = m->fUsable;
792 return hrc;
793}
794
795STDMETHODIMP ExtPack::COMGETTER(WhyUnusable)(BSTR *a_pbstrWhy)
796{
797 CheckComArgOutPointerValid(a_pbstrWhy);
798
799 AutoCaller autoCaller(this);
800 HRESULT hrc = autoCaller.rc();
801 if (SUCCEEDED(hrc))
802 m->strWhyUnusable.cloneTo(a_pbstrWhy);
803 return hrc;
804}
805
806
807
808
809DEFINE_EMPTY_CTOR_DTOR(ExtPackManager)
810
811/**
812 * Called by ComObjPtr::createObject when creating the object.
813 *
814 * Just initialize the basic object state, do the rest in init().
815 *
816 * @returns S_OK.
817 */
818HRESULT ExtPackManager::FinalConstruct()
819{
820 m = NULL;
821 return S_OK;
822}
823
824/**
825 * Initializes the extension pack manager.
826 *
827 * @returns COM status code.
828 * @param a_pszDropZoneDir The path to the drop zone directory.
829 * @param a_fCheckDropZone Whether to check the drop zone for new
830 * extensions or not. Only VBoxSVC does this
831 * and then only when wanted.
832 */
833HRESULT ExtPackManager::init(const char *a_pszDropZoneDir, bool a_fCheckDropZone)
834{
835 AutoInitSpan autoInitSpan(this);
836 AssertReturn(autoInitSpan.isOk(), E_FAIL);
837
838 /*
839 * Figure some stuff out before creating the instance data.
840 */
841 char szBaseDir[RTPATH_MAX];
842 int rc = RTPathAppPrivateArch(szBaseDir, sizeof(szBaseDir));
843 AssertLogRelRCReturn(rc, E_FAIL);
844 rc = RTPathAppend(szBaseDir, sizeof(szBaseDir), VBOX_EXTPACK_INSTALL_DIR);
845 AssertLogRelRCReturn(rc, E_FAIL);
846
847 char szCertificatDir[RTPATH_MAX];
848 rc = RTPathAppPrivateNoArch(szCertificatDir, sizeof(szCertificatDir));
849 AssertLogRelRCReturn(rc, E_FAIL);
850 rc = RTPathAppend(szBaseDir, sizeof(szCertificatDir), VBOX_EXTPACK_CERT_DIR);
851 AssertLogRelRCReturn(rc, E_FAIL);
852
853 /*
854 * Allocate and initialize the instance data.
855 */
856 m = new Data;
857 m->strBaseDir = szBaseDir;
858 m->strCertificatDirPath = szCertificatDir;
859 m->strDropZoneDir = a_pszDropZoneDir;
860
861 /*
862 * Go looking for extensions. The RTDirOpen may fail if nothing has been
863 * installed yet, or if root is paranoid and has revoked our access to them.
864 *
865 * We ASSUME that there are no files, directories or stuff in the directory
866 * that exceed the max name length in RTDIRENTRYEX.
867 */
868 HRESULT hrc = S_OK;
869 PRTDIR pDir;
870 int vrc = RTDirOpen(&pDir, szBaseDir);
871 if (RT_SUCCESS(vrc))
872 {
873 for (;;)
874 {
875 RTDIRENTRYEX Entry;
876 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
877 if (RT_FAILURE(vrc))
878 {
879 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
880 break;
881 }
882 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
883 && strcmp(Entry.szName, ".") != 0
884 && strcmp(Entry.szName, "..") != 0
885 && VBoxExtPackIsValidName(Entry.szName) )
886 {
887 /*
888 * All directories are extensions, the shall be nothing but
889 * extensions in this subdirectory.
890 */
891 ComObjPtr<ExtPack> NewExtPack;
892 HRESULT hrc2 = NewExtPack.createObject();
893 if (SUCCEEDED(hrc2))
894 hrc2 = NewExtPack->init(Entry.szName, szBaseDir);
895 if (SUCCEEDED(hrc2))
896 m->llInstalledExtPacks.push_back(NewExtPack);
897 else if (SUCCEEDED(rc))
898 hrc = hrc2;
899 }
900 }
901 RTDirClose(pDir);
902 }
903 /* else: ignore, the directory probably does not exist or something. */
904
905 /*
906 * Look for things in the drop zone.
907 */
908 if (SUCCEEDED(hrc) && a_fCheckDropZone)
909 processDropZone();
910
911 if (SUCCEEDED(hrc))
912 autoInitSpan.setSucceeded();
913 return hrc;
914}
915
916/**
917 * COM cruft.
918 */
919void ExtPackManager::FinalRelease()
920{
921 uninit();
922}
923
924/**
925 * Do the actual cleanup.
926 */
927void ExtPackManager::uninit()
928{
929 /* Enclose the state transition Ready->InUninit->NotReady */
930 AutoUninitSpan autoUninitSpan(this);
931 if (!autoUninitSpan.uninitDone() && m != NULL)
932 {
933 delete m;
934 m = NULL;
935 }
936}
937
938
939STDMETHODIMP ExtPackManager::COMGETTER(InstalledExtPacks)(ComSafeArrayOut(IExtPack *, a_paExtPacks))
940{
941 CheckComArgSafeArrayNotNull(a_paExtPacks);
942
943 AutoCaller autoCaller(this);
944 HRESULT hrc = autoCaller.rc();
945 if (SUCCEEDED(hrc))
946 {
947 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
948
949 SafeIfaceArray<IExtPack> SaExtPacks(m->llInstalledExtPacks);
950 SaExtPacks.detachTo(ComSafeArrayOutArg(a_paExtPacks));
951 }
952
953 return hrc;
954}
955
956STDMETHODIMP ExtPackManager::Find(IN_BSTR a_bstrName, IExtPack **a_pExtPack)
957{
958 CheckComArgNotNull(a_bstrName);
959 CheckComArgOutPointerValid(a_pExtPack);
960 Utf8Str strName(a_bstrName);
961
962 AutoCaller autoCaller(this);
963 HRESULT hrc = autoCaller.rc();
964 if (SUCCEEDED(hrc))
965 {
966 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
967
968 ComPtr<ExtPack> ptrExtPack = findExtPack(strName.c_str());
969 if (!ptrExtPack.isNull())
970 ptrExtPack.queryInterfaceTo(a_pExtPack);
971 else
972 hrc = VBOX_E_OBJECT_NOT_FOUND;
973 }
974
975 return hrc;
976}
977
978STDMETHODIMP ExtPackManager::Install(IN_BSTR a_bstrTarball, BSTR *a_pbstrName)
979{
980 CheckComArgNotNull(a_bstrTarball);
981 CheckComArgOutPointerValid(a_pbstrName);
982 Utf8Str strTarball(a_bstrTarball);
983
984 AutoCaller autoCaller(this);
985 HRESULT hrc = autoCaller.rc();
986 if (SUCCEEDED(hrc))
987 {
988 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
989
990 /*
991 * Check that the file exists and that we can access it.
992 */
993 if (RTFileExists(strTarball.c_str()))
994 {
995 RTFILE hFile;
996 int vrc = RTFileOpen(&hFile, strTarball.c_str(), RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
997 if (RT_SUCCESS(vrc))
998 {
999 RTFSOBJINFO ObjInfo;
1000 vrc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_NOTHING);
1001 if ( RT_SUCCESS(vrc)
1002 && RTFS_IS_FILE(ObjInfo.Attr.fMode))
1003 {
1004 /*
1005 * Derive the name of the extension pack from the file
1006 * name. Certain restrictions are here placed on the
1007 * tarball name.
1008 */
1009 iprt::MiniString *pStrName = VBoxExtPackExtractNameFromTarballPath(strTarball.c_str());
1010 if (pStrName)
1011 {
1012 /*
1013 * Refresh the data we have on the extension pack as it
1014 * may be made stale by direct meddling or some other user.
1015 */
1016 ExtPack *pExtPack;
1017 hrc = refreshExtPack(pStrName->c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1018 if (SUCCEEDED(hrc) && !pExtPack)
1019 {
1020 /*
1021 * Run the set-uid-to-root binary that performs the actual
1022 * installation. Then create an object for the packet (we
1023 * do this even on failure, to be on the safe side).
1024 */
1025 char szTarballFd[64];
1026 RTStrPrintf(szTarballFd, sizeof(szTarballFd), "0x%RX64",
1027 (uint64_t)RTFileToNative(hFile));
1028
1029 hrc = runSetUidToRootHelper("install",
1030 "--base-dir", m->strBaseDir.c_str(),
1031 "--certificate-dir", m->strCertificatDirPath.c_str(),
1032 "--name", pStrName->c_str(),
1033 "--tarball", strTarball.c_str(),
1034 "--tarball-fd", &szTarballFd[0],
1035 NULL);
1036 if (SUCCEEDED(hrc))
1037 {
1038 hrc = refreshExtPack(pStrName->c_str(), true /*a_fUnsuableIsError*/, &pExtPack);
1039 if (SUCCEEDED(hrc))
1040 {
1041 LogRel(("ExtPackManager: Successfully installed extension pack '%s'.\n", pStrName->c_str()));
1042 pExtPack->callInstalledHook();
1043 }
1044 }
1045 else
1046 {
1047 ErrorInfoKeeper Eik;
1048 refreshExtPack(pStrName->c_str(), false /*a_fUnsuableIsError*/, NULL);
1049 }
1050 }
1051 else if (SUCCEEDED(hrc))
1052 hrc = setError(E_FAIL,
1053 tr("Extension pack '%s' is already installed."
1054 " In case of a reinstallation, please uninstall it first"),
1055 pStrName->c_str());
1056 delete pStrName;
1057 }
1058 else
1059 hrc = setError(E_FAIL, tr("Malformed '%s' file name"), strTarball.c_str());
1060 }
1061 else if (RT_SUCCESS(vrc))
1062 hrc = setError(E_FAIL, tr("'%s' is not a regular file"), strTarball.c_str());
1063 else
1064 hrc = setError(E_FAIL, tr("Failed to query info on '%s' (%Rrc)"), strTarball.c_str(), vrc);
1065 RTFileClose(hFile);
1066 }
1067 else
1068 hrc = setError(E_FAIL, tr("Failed to open '%s' (%Rrc)"), strTarball.c_str(), vrc);
1069 }
1070 else if (RTPathExists(strTarball.c_str()))
1071 hrc = setError(E_FAIL, tr("'%s' is not a regular file"), strTarball.c_str());
1072 else
1073 hrc = setError(E_FAIL, tr("File '%s' was inaccessible or not found"), strTarball.c_str());
1074 }
1075
1076 return hrc;
1077}
1078
1079STDMETHODIMP ExtPackManager::Uninstall(IN_BSTR a_bstrName, BOOL a_fForcedRemoval)
1080{
1081 CheckComArgNotNull(a_bstrName);
1082 Utf8Str strName(a_bstrName);
1083
1084 AutoCaller autoCaller(this);
1085 HRESULT hrc = autoCaller.rc();
1086 if (SUCCEEDED(hrc))
1087 {
1088 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1089
1090 /*
1091 * Refresh the data we have on the extension pack as it may be made
1092 * stale by direct meddling or some other user.
1093 */
1094 ExtPack *pExtPack;
1095 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1096 if (SUCCEEDED(hrc))
1097 {
1098 if (!pExtPack)
1099 {
1100 LogRel(("ExtPackManager: Extension pack '%s' is not installed, so nothing to uninstall.\n", strName.c_str()));
1101 hrc = S_OK; /* nothing to uninstall */
1102 }
1103 else
1104 {
1105 /*
1106 * Call the uninstall hook and unload the main dll.
1107 */
1108 hrc = pExtPack->callUninstallHookAndClose(a_fForcedRemoval != FALSE);
1109 if (SUCCEEDED(hrc))
1110 {
1111 /*
1112 * Run the set-uid-to-root binary that performs the
1113 * uninstallation. Then refresh the object.
1114 *
1115 * This refresh is theorically subject to races, but it's of
1116 * the don't-do-that variety.
1117 */
1118 const char *pszForcedOpt = a_fForcedRemoval ? "--forced" : NULL;
1119 hrc = runSetUidToRootHelper("uninstall",
1120 "--base-dir", m->strBaseDir.c_str(),
1121 "--name", strName.c_str(),
1122 pszForcedOpt, /* Last as it may be NULL. */
1123 NULL);
1124 if (SUCCEEDED(hrc))
1125 {
1126 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1127 if (SUCCEEDED(hrc))
1128 {
1129 if (!pExtPack)
1130 LogRel(("ExtPackManager: Successfully uninstalled extension pack '%s'.\n", strName.c_str()));
1131 else
1132 hrc = setError(E_UNEXPECTED,
1133 tr("Uninstall extension pack '%s' failed under mysterious circumstances"),
1134 strName.c_str());
1135 }
1136 }
1137 else
1138 {
1139 ErrorInfoKeeper Eik;
1140 refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, NULL);
1141 }
1142 }
1143 }
1144 }
1145 }
1146
1147 return hrc;
1148}
1149
1150
1151/**
1152 * Runs the helper application that does the privileged operations.
1153 *
1154 * @returns S_OK or a failure status with error information set.
1155 * @param a_pszCommand The command to execute.
1156 * @param ... The argument strings that goes along with the
1157 * command. Maximum is about 16. Terminated by a
1158 * NULL.
1159 */
1160HRESULT ExtPackManager::runSetUidToRootHelper(const char *a_pszCommand, ...)
1161{
1162 /*
1163 * Calculate the path to the helper application.
1164 */
1165 char szExecName[RTPATH_MAX];
1166 int vrc = RTPathAppPrivateArch(szExecName, sizeof(szExecName));
1167 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1168
1169 vrc = RTPathAppend(szExecName, sizeof(szExecName), VBOX_EXTPACK_HELPER_NAME);
1170 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1171
1172 /*
1173 * Convert the variable argument list to a RTProcCreate argument vector.
1174 */
1175 const char *apszArgs[20];
1176 unsigned cArgs = 0;
1177 apszArgs[cArgs++] = &szExecName[0];
1178 apszArgs[cArgs++] = a_pszCommand;
1179
1180 va_list va;
1181 va_start(va, a_pszCommand);
1182 const char *pszLastArg;
1183 LogRel(("ExtPack: Executing '%s'", szExecName));
1184 do
1185 {
1186 LogRel((" '%s'", apszArgs[cArgs - 1]));
1187 AssertReturn(cArgs < RT_ELEMENTS(apszArgs) - 1, E_UNEXPECTED);
1188 pszLastArg = va_arg(va, const char *);
1189 apszArgs[cArgs++] = pszLastArg;
1190 } while (pszLastArg != NULL);
1191 LogRel(("\n"));
1192 va_end(va);
1193 apszArgs[cArgs] = NULL;
1194
1195 /*
1196 * Create a PIPE which we attach to stderr so that we can read the error
1197 * message on failure and report it back to the caller.
1198 */
1199 RTPIPE hPipeR;
1200 RTHANDLE hStdErrPipe;
1201 hStdErrPipe.enmType = RTHANDLETYPE_PIPE;
1202 vrc = RTPipeCreate(&hPipeR, &hStdErrPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
1203 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1204
1205 /*
1206 * Spawn the process.
1207 */
1208 HRESULT hrc;
1209 RTPROCESS hProcess;
1210 vrc = RTProcCreateEx(szExecName,
1211 apszArgs,
1212 RTENV_DEFAULT,
1213 0 /*fFlags*/,
1214 NULL /*phStdIn*/,
1215 NULL /*phStdOut*/,
1216 &hStdErrPipe,
1217 NULL /*pszAsUser*/,
1218 NULL /*pszPassword*/,
1219 &hProcess);
1220 if (RT_SUCCESS(vrc))
1221 {
1222 vrc = RTPipeClose(hStdErrPipe.u.hPipe);
1223 hStdErrPipe.u.hPipe = NIL_RTPIPE;
1224
1225 /*
1226 * Read the pipe output until the process completes.
1227 */
1228 RTPROCSTATUS ProcStatus = { -42, RTPROCEXITREASON_ABEND };
1229 size_t cbStdErrBuf = 0;
1230 size_t offStdErrBuf = 0;
1231 char *pszStdErrBuf = NULL;
1232 do
1233 {
1234 /*
1235 * Service the pipe. Block waiting for output or the pipe breaking
1236 * when the process terminates.
1237 */
1238 if (hPipeR != NIL_RTPIPE)
1239 {
1240 char achBuf[16]; ///@todo 1024
1241 size_t cbRead;
1242 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
1243 if (RT_SUCCESS(vrc))
1244 {
1245 /* grow the buffer? */
1246 size_t cbBufReq = offStdErrBuf + cbRead + 1;
1247 if ( cbBufReq > cbStdErrBuf
1248 && cbBufReq < _256K)
1249 {
1250 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
1251 void *pvNew = RTMemRealloc(pszStdErrBuf, cbNew);
1252 if (pvNew)
1253 {
1254 pszStdErrBuf = (char *)pvNew;
1255 cbStdErrBuf = cbNew;
1256 }
1257 }
1258
1259 /* append if we've got room. */
1260 if (cbBufReq <= cbStdErrBuf)
1261 {
1262 memcpy(&pszStdErrBuf[offStdErrBuf], achBuf, cbRead);
1263 offStdErrBuf = offStdErrBuf + cbRead;
1264 pszStdErrBuf[offStdErrBuf] = '\0';
1265 }
1266 }
1267 else
1268 {
1269 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
1270 RTPipeClose(hPipeR);
1271 hPipeR = NIL_RTPIPE;
1272 }
1273 }
1274
1275 /*
1276 * Service the process. Block if we have no pipe.
1277 */
1278 if (hProcess != NIL_RTPROCESS)
1279 {
1280 vrc = RTProcWait(hProcess,
1281 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
1282 &ProcStatus);
1283 if (RT_SUCCESS(vrc))
1284 hProcess = NIL_RTPROCESS;
1285 else
1286 AssertLogRelMsgStmt(vrc != VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProcess = NIL_RTPROCESS);
1287 }
1288 } while ( hPipeR != NIL_RTPIPE
1289 || hProcess != NIL_RTPROCESS);
1290
1291 /*
1292 * Compose the status code and, on failure, error message.
1293 */
1294 LogRel(("ExtPack: enmReason=%d iStatus=%d stderr='%s'\n",
1295 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : ""));
1296
1297 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
1298 && ProcStatus.iStatus == 0
1299 && offStdErrBuf == 0)
1300 hrc = S_OK;
1301 else if (ProcStatus.enmReason == RTPROCEXITREASON_NORMAL)
1302 {
1303 AssertMsg(ProcStatus.iStatus != 0, ("%s\n", pszStdErrBuf));
1304 hrc = setError(E_UNEXPECTED, tr("The installer failed with exit code %d: %s"),
1305 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1306 }
1307 else if (ProcStatus.enmReason == RTPROCEXITREASON_SIGNAL)
1308 hrc = setError(E_UNEXPECTED, tr("The installer was killed by signal #d (stderr: %s)"),
1309 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1310 else if (ProcStatus.enmReason == RTPROCEXITREASON_ABEND)
1311 hrc = setError(E_UNEXPECTED, tr("The installer aborted abnormally (stderr: %s)"),
1312 offStdErrBuf ? pszStdErrBuf : "");
1313 else
1314 hrc = setError(E_UNEXPECTED, tr("internal error: enmReason=%d iStatus=%d stderr='%s'"),
1315 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1316
1317 RTMemFree(pszStdErrBuf);
1318 }
1319 else
1320 hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to launch the helper application '%s' (%Rrc)"), szExecName, vrc);
1321
1322 RTPipeClose(hPipeR);
1323 RTPipeClose(hStdErrPipe.u.hPipe);
1324
1325 return hrc;
1326}
1327
1328/**
1329 * Finds an installed extension pack.
1330 *
1331 * @returns Pointer to the extension pack if found, NULL if not. (No reference
1332 * counting problem here since the caller must be holding the lock.)
1333 * @param a_pszName The name of the extension pack.
1334 */
1335ExtPack *ExtPackManager::findExtPack(const char *a_pszName)
1336{
1337 size_t cchName = strlen(a_pszName);
1338
1339 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1340 it != m->llInstalledExtPacks.end();
1341 it++)
1342 {
1343 ExtPack::Data *pExtPackData = (*it)->m;
1344 if ( pExtPackData
1345 && pExtPackData->Desc.strName.length() == cchName
1346 && pExtPackData->Desc.strName.compare(a_pszName, iprt::MiniString::CaseInsensitive) == 0)
1347 return (*it);
1348 }
1349 return NULL;
1350}
1351
1352/**
1353 * Removes an installed extension pack from the internal list.
1354 *
1355 * The package is expected to exist!
1356 *
1357 * @param a_pszName The name of the extension pack.
1358 */
1359void ExtPackManager::removeExtPack(const char *a_pszName)
1360{
1361 size_t cchName = strlen(a_pszName);
1362
1363 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1364 it != m->llInstalledExtPacks.end();
1365 it++)
1366 {
1367 ExtPack::Data *pExtPackData = (*it)->m;
1368 if ( pExtPackData
1369 && pExtPackData->Desc.strName.length() == cchName
1370 && pExtPackData->Desc.strName.compare(a_pszName, iprt::MiniString::CaseInsensitive) == 0)
1371 {
1372 m->llInstalledExtPacks.erase(it);
1373 return;
1374 }
1375 }
1376 AssertMsgFailed(("%s\n", a_pszName));
1377}
1378
1379/**
1380 * Refreshes the specified extension pack.
1381 *
1382 * This may remove the extension pack from the list, so any non-smart pointers
1383 * to the extension pack object may become invalid.
1384 *
1385 * @returns S_OK and *ppExtPack on success, COM status code and error message
1386 * on failure.
1387 * @param a_pszName The extension to update..
1388 * @param a_fUnsuableIsError If @c true, report an unusable extension pack
1389 * as an error.
1390 * @param a_ppExtPack Where to store the pointer to the extension
1391 * pack of it is still around after the refresh.
1392 * This is optional.
1393 * @remarks Caller holds the extension manager lock.
1394 */
1395HRESULT ExtPackManager::refreshExtPack(const char *a_pszName, bool a_fUnsuableIsError, ExtPack **a_ppExtPack)
1396{
1397 HRESULT hrc;
1398 ExtPack *pExtPack = findExtPack(a_pszName);
1399 if (pExtPack)
1400 {
1401 /*
1402 * Refresh existing object.
1403 */
1404 bool fCanDelete;
1405 hrc = pExtPack->refresh(&fCanDelete);
1406 if (SUCCEEDED(hrc))
1407 {
1408 if (fCanDelete)
1409 {
1410 removeExtPack(a_pszName);
1411 pExtPack = NULL;
1412 }
1413 }
1414 }
1415 else
1416 {
1417 /*
1418 * Does the dir exist? Make some special effort to deal with case
1419 * sensitivie file systems (a_pszName is case insensitive).
1420 */
1421 char szDir[RTPATH_MAX];
1422 int vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), a_pszName);
1423 AssertLogRelRCReturn(vrc, E_FAIL);
1424
1425 RTDIRENTRYEX Entry;
1426 RTFSOBJINFO ObjInfo;
1427 vrc = RTPathQueryInfoEx(szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1428 bool fExists = RT_SUCCESS(vrc) && RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode);
1429 if (!fExists)
1430 {
1431 PRTDIR pDir;
1432 vrc = RTDirOpen(&pDir, m->strBaseDir.c_str());
1433 if (RT_SUCCESS(vrc))
1434 {
1435 for (;;)
1436 {
1437 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1438 if (RT_FAILURE(vrc))
1439 {
1440 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1441 break;
1442 }
1443 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
1444 && !RTStrICmp(Entry.szName, a_pszName))
1445 {
1446 /*
1447 * The installed extension pack has a uses different case.
1448 * Update the name and directory variables.
1449 */
1450 vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), Entry.szName); /* not really necessary */
1451 AssertLogRelRCReturnStmt(vrc, E_UNEXPECTED, RTDirClose(pDir));
1452 a_pszName = Entry.szName;
1453 fExists = true;
1454 break;
1455 }
1456 }
1457 RTDirClose(pDir);
1458 }
1459 }
1460 if (fExists)
1461 {
1462 /*
1463 * We've got something, create a new extension pack object for it.
1464 */
1465 ComObjPtr<ExtPack> NewExtPack;
1466 hrc = NewExtPack.createObject();
1467 if (SUCCEEDED(hrc))
1468 hrc = NewExtPack->init(a_pszName, m->strBaseDir.c_str());
1469 if (SUCCEEDED(hrc))
1470 {
1471 m->llInstalledExtPacks.push_back(NewExtPack);
1472 if (NewExtPack->m->fUsable)
1473 LogRel(("ExtPackManager: Found extension pack '%s'.\n", a_pszName));
1474 else
1475 LogRel(("ExtPackManager: Found bad extension pack '%s': %s\n",
1476 a_pszName, NewExtPack->m->strWhyUnusable.c_str() ));
1477 pExtPack = NewExtPack;
1478 }
1479 }
1480 else
1481 hrc = S_OK;
1482 }
1483
1484 /*
1485 * Report error if not usable, if that is desired.
1486 */
1487 if ( SUCCEEDED(hrc)
1488 && pExtPack
1489 && a_fUnsuableIsError
1490 && !pExtPack->m->fUsable)
1491 hrc = setError(E_FAIL, "%s", pExtPack->m->strWhyUnusable.c_str());
1492
1493 if (a_ppExtPack)
1494 *a_ppExtPack = pExtPack;
1495 return hrc;
1496}
1497
1498
1499/**
1500 * Processes anything new in the drop zone.
1501 */
1502void ExtPackManager::processDropZone(void)
1503{
1504 AutoCaller autoCaller(this);
1505 HRESULT hrc = autoCaller.rc();
1506 if (FAILED(hrc))
1507 return;
1508 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1509
1510 if (m->strDropZoneDir.isEmpty())
1511 return;
1512
1513 PRTDIR pDir;
1514 int vrc = RTDirOpen(&pDir, m->strDropZoneDir.c_str());
1515 if (RT_FAILURE(vrc))
1516 return;
1517 for (;;)
1518 {
1519 RTDIRENTRYEX Entry;
1520 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1521 if (RT_FAILURE(vrc))
1522 {
1523 AssertMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1524 break;
1525 }
1526
1527 /*
1528 * We're looking for files with the right extension. Symbolic links
1529 * will be ignored.
1530 */
1531 if ( RTFS_IS_FILE(Entry.Info.Attr.fMode)
1532 && RTStrICmp(RTPathExt(Entry.szName), VBOX_EXTPACK_SUFFIX) == 0)
1533 {
1534 /* We create (and check for) a blocker file to prevent this
1535 extension pack from being installed more than once. */
1536 char szPath[RTPATH_MAX];
1537 vrc = RTPathJoin(szPath, sizeof(szPath), m->strDropZoneDir.c_str(), Entry.szName);
1538 if (RT_SUCCESS(vrc))
1539 vrc = RTPathAppend(szPath, sizeof(szPath), "-done");
1540 AssertRC(vrc);
1541 if (RT_SUCCESS(vrc))
1542 {
1543 RTFILE hFile;
1544 vrc = RTFileOpen(&hFile, szPath, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE);
1545 if (RT_SUCCESS(vrc))
1546 {
1547 /* Construct the full path to the extension pack and invoke
1548 the Install method to install it. Write errors to the
1549 done file. */
1550 vrc = RTPathJoin(szPath, sizeof(szPath), m->strDropZoneDir.c_str(), Entry.szName); AssertRC(vrc);
1551 Bstr strName;
1552 hrc = Install(Bstr(szPath).raw(), strName.asOutParam());
1553 if (SUCCEEDED(hrc))
1554 RTFileWrite(hFile, "succeeded\n", sizeof("succeeded\n"), NULL);
1555 else
1556 {
1557 Utf8Str strErr;
1558 com::ErrorInfo Info;
1559 if (Info.isFullAvailable())
1560 strErr.printf("failed\n"
1561 "%ls\n"
1562 "Details: code %Rhrc (%#RX32), component %ls, interface %ls, callee %ls\n"
1563 ,
1564 Info.getText().raw(),
1565 Info.getResultCode(),
1566 Info.getResultCode(),
1567 Info.getComponent().raw(),
1568 Info.getInterfaceName().raw(),
1569 Info.getCalleeName().raw());
1570 else
1571 strErr.printf("failed\n"
1572 "hrc=%Rhrc (%#RX32)\n", hrc, hrc);
1573 RTFileWrite(hFile, strErr.c_str(), strErr.length(), NULL);
1574 }
1575 RTFileClose(hFile);
1576 }
1577 }
1578 }
1579 } /* foreach dir entry */
1580 RTDirClose(pDir);
1581}
1582
1583
1584/**
1585 * Calls the pfnVMCreated hook for all working extension packs.
1586 *
1587 * @param a_pMachine The machine interface of the new VM.
1588 */
1589void ExtPackManager::callAllVmCreatedHooks(IMachine *a_pMachine)
1590{
1591 AutoCaller autoCaller(this);
1592 HRESULT hrc = autoCaller.rc();
1593 if (FAILED(hrc))
1594 return;
1595 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1596
1597 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1598 it != m->llInstalledExtPacks.end();
1599 it++)
1600 (*it)->callVmCreatedHook(a_pMachine);
1601}
1602
1603/**
1604 * Calls the pfnVMConfigureVMM hook for all working extension packs.
1605 *
1606 * @returns VBox status code. Stops on the first failure, expecting the caller
1607 * to signal this to the caller of the CFGM constructor.
1608 * @param a_pConsole The console interface for the VM.
1609 * @param a_pVM The VM handle.
1610 */
1611int ExtPackManager::callAllVmConfigureVmmHooks(IConsole *a_pConsole, PVM a_pVM)
1612{
1613 AutoCaller autoCaller(this);
1614 HRESULT hrc = autoCaller.rc();
1615 if (FAILED(hrc))
1616 return Global::vboxStatusCodeFromCOM(hrc);
1617 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1618
1619 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1620 it != m->llInstalledExtPacks.end();
1621 it++)
1622 {
1623 int vrc = (*it)->callVmConfigureVmmHook(a_pConsole, a_pVM);
1624 if (RT_FAILURE(vrc))
1625 return vrc;
1626 }
1627
1628 return VINF_SUCCESS;
1629}
1630
1631/**
1632 * Calls the pfnVMPowerOn hook for all working extension packs.
1633 *
1634 * @returns VBox status code. Stops on the first failure, expecting the caller
1635 * to not power on the VM.
1636 * @param a_pConsole The console interface for the VM.
1637 * @param a_pVM The VM handle.
1638 */
1639int ExtPackManager::callAllVmPowerOnHooks(IConsole *a_pConsole, PVM a_pVM)
1640{
1641 AutoCaller autoCaller(this);
1642 HRESULT hrc = autoCaller.rc();
1643 if (FAILED(hrc))
1644 return Global::vboxStatusCodeFromCOM(hrc);
1645 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1646
1647 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1648 it != m->llInstalledExtPacks.end();
1649 it++)
1650 {
1651 int vrc = (*it)->callVmPowerOnHook(a_pConsole, a_pVM);
1652 if (RT_FAILURE(vrc))
1653 return vrc;
1654 }
1655
1656 return VINF_SUCCESS;
1657}
1658
1659/**
1660 * Calls the pfnVMPowerOff hook for all working extension packs.
1661 *
1662 * @param a_pConsole The console interface for the VM.
1663 * @param a_pVM The VM handle. Can be NULL.
1664 */
1665void ExtPackManager::callAllVmPowerOffHooks(IConsole *a_pConsole, PVM a_pVM)
1666{
1667 AutoCaller autoCaller(this);
1668 HRESULT hrc = autoCaller.rc();
1669 if (FAILED(hrc))
1670 return;
1671 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1672
1673 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1674 it != m->llInstalledExtPacks.end();
1675 it++)
1676 (*it)->callVmPowerOnHook(a_pConsole, a_pVM);
1677}
1678
1679
1680/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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