VirtualBox

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

Last change on this file since 33696 was 33695, checked in by vboxsync, 14 years ago

build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.2 KB
Line 
1/* $Id: ExtPackManagerImpl.cpp 33695 2010-11-02 15:09:41Z 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"), szErr);
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 /*
836 * Figure some stuff out before creating the instance data.
837 */
838 char szBaseDir[RTPATH_MAX];
839 int rc = RTPathAppPrivateArch(szBaseDir, sizeof(szBaseDir));
840 AssertLogRelRCReturn(rc, E_FAIL);
841 rc = RTPathAppend(szBaseDir, sizeof(szBaseDir), "ExtensionPacks");
842 AssertLogRelRCReturn(rc, E_FAIL);
843
844 char szCertificatDir[RTPATH_MAX];
845 rc = RTPathAppPrivateArch(szCertificatDir, sizeof(szCertificatDir));
846 AssertLogRelRCReturn(rc, E_FAIL);
847 rc = RTPathAppend(szBaseDir, sizeof(szCertificatDir), "Certificates");
848 AssertLogRelRCReturn(rc, E_FAIL);
849
850 /*
851 * Allocate and initialize the instance data.
852 */
853 m = new Data;
854 m->strBaseDir = szBaseDir;
855 m->strCertificatDirPath = szCertificatDir;
856 m->strDropZoneDir = a_pszDropZoneDir;
857
858 /*
859 * Go looking for extensions. The RTDirOpen may fail if nothing has been
860 * installed yet, or if root is paranoid and has revoked our access to them.
861 *
862 * We ASSUME that there are no files, directories or stuff in the directory
863 * that exceed the max name length in RTDIRENTRYEX.
864 */
865 PRTDIR pDir;
866 int vrc = RTDirOpen(&pDir, szBaseDir);
867 if (RT_FAILURE(vrc))
868 return S_OK;
869 HRESULT hrc = S_OK;
870 for (;;)
871 {
872 RTDIRENTRYEX Entry;
873 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
874 if (RT_FAILURE(vrc))
875 {
876 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
877 break;
878 }
879 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
880 && strcmp(Entry.szName, ".") != 0
881 && strcmp(Entry.szName, "..") != 0 )
882 {
883 /*
884 * All directories are extensions, the shall be nothing but
885 * extensions in this subdirectory.
886 */
887 ComObjPtr<ExtPack> NewExtPack;
888 HRESULT hrc2 = NewExtPack.createObject();
889 if (SUCCEEDED(hrc2))
890 hrc2 = NewExtPack->init(Entry.szName, szBaseDir);
891 if (SUCCEEDED(hrc2))
892 m->llInstalledExtPacks.push_back(NewExtPack);
893 else if (SUCCEEDED(rc))
894 hrc = hrc2;
895 }
896 }
897 RTDirClose(pDir);
898
899 /*
900 * Look for things in the drop zone.
901 */
902 if (SUCCEEDED(hrc) && a_fCheckDropZone)
903 processDropZone();
904
905 return hrc;
906}
907
908/**
909 * COM cruft.
910 */
911void ExtPackManager::FinalRelease()
912{
913 uninit();
914}
915
916/**
917 * Do the actual cleanup.
918 */
919void ExtPackManager::uninit()
920{
921 /* Enclose the state transition Ready->InUninit->NotReady */
922 AutoUninitSpan autoUninitSpan(this);
923 if (!autoUninitSpan.uninitDone() && m != NULL)
924 {
925 delete m;
926 m = NULL;
927 }
928}
929
930
931STDMETHODIMP ExtPackManager::COMGETTER(InstalledExtPacks)(ComSafeArrayOut(IExtPack *, a_paExtPacks))
932{
933 CheckComArgSafeArrayNotNull(a_paExtPacks);
934
935 AutoCaller autoCaller(this);
936 HRESULT hrc = autoCaller.rc();
937 if (SUCCEEDED(hrc))
938 {
939 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
940
941 SafeIfaceArray<IExtPack> SaExtPacks(m->llInstalledExtPacks);
942 SaExtPacks.detachTo(ComSafeArrayOutArg(a_paExtPacks));
943 }
944
945 return hrc;
946}
947
948STDMETHODIMP ExtPackManager::Find(IN_BSTR a_bstrName, IExtPack **a_pExtPack)
949{
950 CheckComArgNotNull(a_bstrName);
951 CheckComArgOutPointerValid(a_pExtPack);
952 Utf8Str strName(a_bstrName);
953
954 AutoCaller autoCaller(this);
955 HRESULT hrc = autoCaller.rc();
956 if (SUCCEEDED(hrc))
957 {
958 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
959
960 ComPtr<ExtPack> ptrExtPack = findExtPack(strName.c_str());
961 if (!ptrExtPack.isNull())
962 ptrExtPack.queryInterfaceTo(a_pExtPack);
963 else
964 hrc = VBOX_E_OBJECT_NOT_FOUND;
965 }
966
967 return hrc;
968}
969
970STDMETHODIMP ExtPackManager::Install(IN_BSTR a_bstrTarball, BSTR *a_pbstrName)
971{
972 CheckComArgNotNull(a_bstrTarball);
973 CheckComArgOutPointerValid(a_pbstrName);
974 Utf8Str strTarball(a_bstrTarball);
975
976 AutoCaller autoCaller(this);
977 HRESULT hrc = autoCaller.rc();
978 if (SUCCEEDED(hrc))
979 {
980 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
981
982 /*
983 * Check that the file exists and that we can access it.
984 */
985 if (RTFileExists(strTarball.c_str()))
986 {
987 RTFILE hFile;
988 int vrc = RTFileOpen(&hFile, strTarball.c_str(), RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
989 if (RT_SUCCESS(vrc))
990 {
991 RTFSOBJINFO ObjInfo;
992 vrc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_NOTHING);
993 if ( RT_SUCCESS(vrc)
994 && RTFS_IS_FILE(ObjInfo.Attr.fMode))
995 {
996 /*
997 * Derive the name of the extension pack from the file
998 * name.
999 *
1000 * RESTRICTION: The name can only contain english alphabet
1001 * charactes, decimal digits and space.
1002 * Impose a max length of 64 chars.
1003 */
1004 char *pszName = RTStrDup(RTPathFilename(strTarball.c_str()));
1005 if (pszName)
1006 {
1007 char *pszEnd = pszName;
1008 while (RT_C_IS_ALNUM(*pszEnd) || *pszEnd == ' ')
1009 pszEnd++;
1010 if ( pszEnd == pszName
1011 || pszEnd - pszName <= 64)
1012 {
1013 *pszEnd = '\0';
1014
1015 /*
1016 * Refresh the data we have on the extension pack
1017 * as it may be made stale by direct meddling or
1018 * some other user.
1019 */
1020 ExtPack *pExtPack;
1021 hrc = refreshExtPack(pszName, false /*a_fUnsuableIsError*/, &pExtPack);
1022 if (SUCCEEDED(hrc) && !pExtPack)
1023 {
1024 /*
1025 * Run the set-uid-to-root binary that performs the actual
1026 * installation. Then create an object for the packet (we
1027 * do this even on failure, to be on the safe side).
1028 */
1029 char szTarballFd[64];
1030 RTStrPrintf(szTarballFd, sizeof(szTarballFd), "0x%RX64",
1031 (uint64_t)RTFileToNative(hFile));
1032
1033 hrc = runSetUidToRootHelper("install",
1034 "--base-dir", m->strBaseDir.c_str(),
1035 "--name", pszName,
1036 "--certificate-dir", m->strCertificatDirPath.c_str(),
1037 "--tarball", strTarball.c_str(),
1038 "--tarball-fd", &szTarballFd[0],
1039 NULL);
1040 if (SUCCEEDED(hrc))
1041 {
1042 hrc = refreshExtPack(pszName, true /*a_fUnsuableIsError*/, &pExtPack);
1043 if (SUCCEEDED(hrc))
1044 {
1045 LogRel(("ExtPackManager: Successfully installed extension pack '%s'.\n", pszName));
1046 pExtPack->callInstalledHook();
1047 }
1048 }
1049 else
1050 {
1051 ErrorInfoKeeper Eik;
1052 refreshExtPack(pszName, false /*a_fUnsuableIsError*/, NULL);
1053 }
1054 }
1055 else if (SUCCEEDED(hrc))
1056 hrc = setError(E_FAIL,
1057 tr("Extension pack '%s' is already installed."
1058 " In case of a reinstallation, please uninstall it first"),
1059 pszName);
1060 }
1061 else
1062 hrc = setError(E_FAIL, tr("Malformed '%s' file name"), strTarball.c_str());
1063 }
1064 else
1065 hrc = E_OUTOFMEMORY;
1066 }
1067 else if (RT_SUCCESS(vrc))
1068 hrc = setError(E_FAIL, tr("'%s' is not a regular file"), strTarball.c_str());
1069 else
1070 hrc = setError(E_FAIL, tr("Failed to query info on '%s' (%Rrc)"), strTarball.c_str(), vrc);
1071 RTFileClose(hFile);
1072 }
1073 else
1074 hrc = setError(E_FAIL, tr("Failed to open '%s' (%Rrc)"), strTarball.c_str(), vrc);
1075 }
1076 else if (RTPathExists(strTarball.c_str()))
1077 hrc = setError(E_FAIL, tr("'%s' is not a regular file"), strTarball.c_str());
1078 else
1079 hrc = setError(E_FAIL, tr("File '%s' was inaccessible or not found"), strTarball.c_str());
1080 }
1081
1082 return hrc;
1083}
1084
1085STDMETHODIMP ExtPackManager::Uninstall(IN_BSTR a_bstrName, BOOL a_fForcedRemoval)
1086{
1087 CheckComArgNotNull(a_bstrName);
1088 Utf8Str strName(a_bstrName);
1089
1090 AutoCaller autoCaller(this);
1091 HRESULT hrc = autoCaller.rc();
1092 if (SUCCEEDED(hrc))
1093 {
1094 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1095
1096 /*
1097 * Refresh the data we have on the extension pack as it may be made
1098 * stale by direct meddling or some other user.
1099 */
1100 ExtPack *pExtPack;
1101 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1102 if (SUCCEEDED(hrc))
1103 {
1104 if (!pExtPack)
1105 {
1106 LogRel(("ExtPackManager: Extension pack '%s' is not installed, so nothing to uninstall.\n", strName.c_str()));
1107 hrc = S_OK; /* nothing to uninstall */
1108 }
1109 else
1110 {
1111 /*
1112 * Call the uninstall hook and unload the main dll.
1113 */
1114 hrc = pExtPack->callUninstallHookAndClose(a_fForcedRemoval != FALSE);
1115 if (SUCCEEDED(hrc))
1116 {
1117 /*
1118 * Run the set-uid-to-root binary that performs the
1119 * uninstallation. Then refresh the object.
1120 *
1121 * This refresh is theorically subject to races, but it's of
1122 * the don't-do-that variety.
1123 */
1124 const char *pszForcedOpt = a_fForcedRemoval ? "--forced" : NULL;
1125 hrc = runSetUidToRootHelper("uninstall",
1126 "--base-dir", m->strBaseDir.c_str(),
1127 "--name", strName.c_str(),
1128 pszForcedOpt, /* Last as it may be NULL. */
1129 NULL);
1130 if (SUCCEEDED(hrc))
1131 {
1132 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1133 if (SUCCEEDED(hrc))
1134 {
1135 if (!pExtPack)
1136 LogRel(("ExtPackManager: Successfully uninstalled extension pack '%s'.\n", strName.c_str()));
1137 else
1138 hrc = setError(E_UNEXPECTED,
1139 tr("Uninstall extension pack '%s' failed under mysterious circumstances"),
1140 strName.c_str());
1141 }
1142 }
1143 else
1144 {
1145 ErrorInfoKeeper Eik;
1146 refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, NULL);
1147 }
1148 }
1149 }
1150 }
1151 }
1152
1153 return hrc;
1154}
1155
1156
1157/**
1158 * Runs the helper application that does the privileged operations.
1159 *
1160 * @returns S_OK or a failure status with error information set.
1161 * @param a_pszCommand The command to execute.
1162 * @param ... The argument strings that goes along with the
1163 * command. Maximum is about 16. Terminated by a
1164 * NULL.
1165 */
1166HRESULT ExtPackManager::runSetUidToRootHelper(const char *a_pszCommand, ...)
1167{
1168 /*
1169 * Calculate the path to the helper application.
1170 */
1171 char szExecName[RTPATH_MAX];
1172 int vrc = RTPathAppPrivateArch(szExecName, sizeof(szExecName));
1173 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1174
1175 vrc = RTPathAppend(szExecName, sizeof(szExecName), VBOX_EXTPACK_HELPER_NAME);
1176 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1177
1178 /*
1179 * Convert the variable argument list to a RTProcCreate argument vector.
1180 */
1181 const char *apszArgs[20];
1182 unsigned cArgs = 0;
1183 apszArgs[cArgs++] = &szExecName[0];
1184 apszArgs[cArgs++] = a_pszCommand;
1185
1186 va_list va;
1187 va_start(va, a_pszCommand);
1188 const char *pszLastArg;
1189 LogRel(("ExtPack: Executing '%s'", szExecName));
1190 do
1191 {
1192 LogRel((" '%s'", apszArgs[cArgs - 1]));
1193 AssertReturn(cArgs < RT_ELEMENTS(apszArgs) - 1, E_UNEXPECTED);
1194 pszLastArg = va_arg(va, const char *);
1195 apszArgs[cArgs++] = pszLastArg;
1196 } while (pszLastArg != NULL);
1197 LogRel(("\n"));
1198 va_end(va);
1199 apszArgs[cArgs] = NULL;
1200
1201 /*
1202 * Create a PIPE which we attach to stderr so that we can read the error
1203 * message on failure and report it back to the caller.
1204 */
1205 RTPIPE hPipeR;
1206 RTHANDLE hStdErrPipe;
1207 hStdErrPipe.enmType = RTHANDLETYPE_PIPE;
1208 vrc = RTPipeCreate(&hPipeR, &hStdErrPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
1209 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1210
1211 /*
1212 * Spawn the process.
1213 */
1214 HRESULT hrc;
1215 RTPROCESS hProcess;
1216 vrc = RTProcCreateEx(szExecName,
1217 apszArgs,
1218 RTENV_DEFAULT,
1219 0 /*fFlags*/,
1220 NULL /*phStdIn*/,
1221 NULL /*phStdOut*/,
1222 &hStdErrPipe,
1223 NULL /*pszAsUser*/,
1224 NULL /*pszPassword*/,
1225 &hProcess);
1226 if (RT_SUCCESS(vrc))
1227 {
1228 vrc = RTPipeClose(hStdErrPipe.u.hPipe);
1229 hStdErrPipe.u.hPipe = NIL_RTPIPE;
1230
1231 /*
1232 * Read the pipe output until the process completes.
1233 */
1234 RTPROCSTATUS ProcStatus = { -42, RTPROCEXITREASON_ABEND };
1235 size_t cbStdErrBuf = 0;
1236 size_t offStdErrBuf = 0;
1237 char *pszStdErrBuf = NULL;
1238 do
1239 {
1240 /*
1241 * Service the pipe. Block waiting for output or the pipe breaking
1242 * when the process terminates.
1243 */
1244 if (hPipeR != NIL_RTPIPE)
1245 {
1246 char achBuf[16]; ///@todo 1024
1247 size_t cbRead;
1248 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
1249 if (RT_SUCCESS(vrc))
1250 {
1251 /* grow the buffer? */
1252 size_t cbBufReq = offStdErrBuf + cbRead + 1;
1253 if ( cbBufReq > cbStdErrBuf
1254 && cbBufReq < _256K)
1255 {
1256 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
1257 void *pvNew = RTMemRealloc(pszStdErrBuf, cbNew);
1258 if (pvNew)
1259 {
1260 pszStdErrBuf = (char *)pvNew;
1261 cbStdErrBuf = cbNew;
1262 }
1263 }
1264
1265 /* append if we've got room. */
1266 if (cbBufReq <= cbStdErrBuf)
1267 {
1268 memcpy(&pszStdErrBuf[offStdErrBuf], achBuf, cbRead);
1269 offStdErrBuf = offStdErrBuf + cbRead;
1270 pszStdErrBuf[offStdErrBuf] = '\0';
1271 }
1272 }
1273 else
1274 {
1275 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
1276 RTPipeClose(hPipeR);
1277 hPipeR = NIL_RTPIPE;
1278 }
1279 }
1280
1281 /*
1282 * Service the process. Block if we have no pipe.
1283 */
1284 if (hProcess != NIL_RTPROCESS)
1285 {
1286 vrc = RTProcWait(hProcess,
1287 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
1288 &ProcStatus);
1289 if (RT_SUCCESS(vrc))
1290 hProcess = NIL_RTPROCESS;
1291 else
1292 AssertLogRelMsgStmt(vrc != VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProcess = NIL_RTPROCESS);
1293 }
1294 } while ( hPipeR != NIL_RTPIPE
1295 || hProcess != NIL_RTPROCESS);
1296
1297 /*
1298 * Compose the status code and, on failure, error message.
1299 */
1300 LogRel(("ExtPack: enmReason=%d iStatus=%d stderr='%s'\n",
1301 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : ""));
1302
1303 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
1304 && ProcStatus.iStatus == 0
1305 && offStdErrBuf == 0)
1306 hrc = S_OK;
1307 else if (ProcStatus.enmReason == RTPROCEXITREASON_NORMAL)
1308 {
1309 AssertMsg(ProcStatus.iStatus != 0, ("%s\n", pszStdErrBuf));
1310 hrc = setError(E_UNEXPECTED, tr("The installer failed with exit code %d: %s"),
1311 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1312 }
1313 else if (ProcStatus.enmReason == RTPROCEXITREASON_SIGNAL)
1314 hrc = setError(E_UNEXPECTED, tr("The installer was killed by signal #d (stderr: %s)"),
1315 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1316 else if (ProcStatus.enmReason == RTPROCEXITREASON_ABEND)
1317 hrc = setError(E_UNEXPECTED, tr("The installer aborted abnormally (stderr: %s)"),
1318 offStdErrBuf ? pszStdErrBuf : "");
1319 else
1320 hrc = setError(E_UNEXPECTED, tr("internal error: enmReason=%d iStatus=%d stderr='%s'"),
1321 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1322
1323 RTMemFree(pszStdErrBuf);
1324 }
1325 else
1326 hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to launch the helper application '%s' (%Rrc)"), szExecName, vrc);
1327
1328 RTPipeClose(hPipeR);
1329 RTPipeClose(hStdErrPipe.u.hPipe);
1330
1331 return hrc;
1332}
1333
1334/**
1335 * Finds an installed extension pack.
1336 *
1337 * @returns Pointer to the extension pack if found, NULL if not. (No reference
1338 * counting problem here since the caller must be holding the lock.)
1339 * @param a_pszName The name of the extension pack.
1340 */
1341ExtPack *ExtPackManager::findExtPack(const char *a_pszName)
1342{
1343 size_t cchName = strlen(a_pszName);
1344
1345 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1346 it != m->llInstalledExtPacks.end();
1347 it++)
1348 {
1349 ExtPack::Data *pExtPackData = (*it)->m;
1350 if ( pExtPackData
1351 && pExtPackData->Desc.strName.length() == cchName
1352 && pExtPackData->Desc.strName.compare(a_pszName, iprt::MiniString::CaseInsensitive) == 0)
1353 return (*it);
1354 }
1355 return NULL;
1356}
1357
1358/**
1359 * Removes an installed extension pack from the internal list.
1360 *
1361 * The package is expected to exist!
1362 *
1363 * @param a_pszName The name of the extension pack.
1364 */
1365void ExtPackManager::removeExtPack(const char *a_pszName)
1366{
1367 size_t cchName = strlen(a_pszName);
1368
1369 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1370 it != m->llInstalledExtPacks.end();
1371 it++)
1372 {
1373 ExtPack::Data *pExtPackData = (*it)->m;
1374 if ( pExtPackData
1375 && pExtPackData->Desc.strName.length() == cchName
1376 && pExtPackData->Desc.strName.compare(a_pszName, iprt::MiniString::CaseInsensitive) == 0)
1377 {
1378 m->llInstalledExtPacks.erase(it);
1379 return;
1380 }
1381 }
1382 AssertMsgFailed(("%s\n", a_pszName));
1383}
1384
1385/**
1386 * Refreshes the specified extension pack.
1387 *
1388 * This may remove the extension pack from the list, so any non-smart pointers
1389 * to the extension pack object may become invalid.
1390 *
1391 * @returns S_OK and *ppExtPack on success, COM status code and error message
1392 * on failure.
1393 * @param a_pszName The extension to update..
1394 * @param a_fUnsuableIsError If @c true, report an unusable extension pack
1395 * as an error.
1396 * @param a_ppExtPack Where to store the pointer to the extension
1397 * pack of it is still around after the refresh.
1398 * This is optional.
1399 * @remarks Caller holds the extension manager lock.
1400 */
1401HRESULT ExtPackManager::refreshExtPack(const char *a_pszName, bool a_fUnsuableIsError, ExtPack **a_ppExtPack)
1402{
1403 HRESULT hrc;
1404 ExtPack *pExtPack = findExtPack(a_pszName);
1405 if (pExtPack)
1406 {
1407 /*
1408 * Refresh existing object.
1409 */
1410 bool fCanDelete;
1411 hrc = pExtPack->refresh(&fCanDelete);
1412 if (SUCCEEDED(hrc))
1413 {
1414 if (fCanDelete)
1415 {
1416 removeExtPack(a_pszName);
1417 pExtPack = NULL;
1418 }
1419 }
1420 }
1421 else
1422 {
1423 /*
1424 * Does the dir exist? Make some special effort to deal with case
1425 * sensitivie file systems (a_pszName is case insensitive).
1426 */
1427 char szDir[RTPATH_MAX];
1428 int vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), a_pszName);
1429 AssertLogRelRCReturn(vrc, E_FAIL);
1430
1431 RTDIRENTRYEX Entry;
1432 RTFSOBJINFO ObjInfo;
1433 vrc = RTPathQueryInfoEx(szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1434 bool fExists = RT_SUCCESS(vrc) && RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode);
1435 if (!fExists)
1436 {
1437 PRTDIR pDir;
1438 vrc = RTDirOpen(&pDir, m->strBaseDir.c_str());
1439 if (RT_SUCCESS(vrc))
1440 {
1441 for (;;)
1442 {
1443 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1444 if (RT_FAILURE(vrc))
1445 {
1446 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1447 break;
1448 }
1449 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
1450 && !RTStrICmp(Entry.szName, a_pszName))
1451 {
1452 /*
1453 * The installed extension pack has a uses different case.
1454 * Update the name and directory variables.
1455 */
1456 vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), Entry.szName); /* not really necessary */
1457 AssertLogRelRCReturnStmt(vrc, E_UNEXPECTED, RTDirClose(pDir));
1458 a_pszName = Entry.szName;
1459 fExists = true;
1460 break;
1461 }
1462 }
1463 RTDirClose(pDir);
1464 }
1465 }
1466 if (fExists)
1467 {
1468 /*
1469 * We've got something, create a new extension pack object for it.
1470 */
1471 ComObjPtr<ExtPack> NewExtPack;
1472 hrc = NewExtPack.createObject();
1473 if (SUCCEEDED(hrc))
1474 hrc = NewExtPack->init(a_pszName, m->strBaseDir.c_str());
1475 if (SUCCEEDED(hrc))
1476 {
1477 m->llInstalledExtPacks.push_back(NewExtPack);
1478 if (NewExtPack->m->fUsable)
1479 LogRel(("ExtPackManager: Found extension pack '%s'.\n", a_pszName));
1480 else
1481 LogRel(("ExtPackManager: Found bad extension pack '%s': %s\n",
1482 a_pszName, NewExtPack->m->strWhyUnusable.c_str() ));
1483 pExtPack = NewExtPack;
1484 }
1485 }
1486 else
1487 hrc = S_OK;
1488 }
1489
1490 /*
1491 * Report error if not usable, if that is desired.
1492 */
1493 if ( SUCCEEDED(hrc)
1494 && pExtPack
1495 && a_fUnsuableIsError
1496 && !pExtPack->m->fUsable)
1497 hrc = setError(E_FAIL, "%s", pExtPack->m->strWhyUnusable.c_str());
1498
1499 if (a_ppExtPack)
1500 *a_ppExtPack = pExtPack;
1501 return hrc;
1502}
1503
1504
1505/**
1506 * Processes anything new in the drop zone.
1507 */
1508void ExtPackManager::processDropZone(void)
1509{
1510 AutoCaller autoCaller(this);
1511 HRESULT hrc = autoCaller.rc();
1512 if (FAILED(hrc))
1513 return;
1514 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1515
1516 if (m->strDropZoneDir.isEmpty())
1517 return;
1518
1519 PRTDIR pDir;
1520 int vrc = RTDirOpen(&pDir, m->strDropZoneDir.c_str());
1521 if (RT_FAILURE(vrc))
1522 return;
1523 for (;;)
1524 {
1525 RTDIRENTRYEX Entry;
1526 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1527 if (RT_FAILURE(vrc))
1528 {
1529 AssertMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1530 break;
1531 }
1532
1533 /*
1534 * We're looking for files with the right extension. Symbolic links
1535 * will be ignored.
1536 */
1537 if ( RTFS_IS_FILE(Entry.Info.Attr.fMode)
1538 && RTStrICmp(RTPathExt(Entry.szName), VBOX_EXTPACK_SUFFIX) == 0)
1539 {
1540 /* We create (and check for) a blocker file to prevent this
1541 extension pack from being installed more than once. */
1542 char szPath[RTPATH_MAX];
1543 vrc = RTPathJoin(szPath, sizeof(szPath), m->strDropZoneDir.c_str(), Entry.szName);
1544 if (RT_SUCCESS(vrc))
1545 vrc = RTPathAppend(szPath, sizeof(szPath), "-done");
1546 AssertRC(vrc);
1547 if (RT_SUCCESS(vrc))
1548 {
1549 RTFILE hFile;
1550 vrc = RTFileOpen(&hFile, szPath, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE);
1551 if (RT_SUCCESS(vrc))
1552 {
1553 /* Construct the full path to the extension pack and invoke
1554 the Install method to install it. Write errors to the
1555 done file. */
1556 vrc = RTPathJoin(szPath, sizeof(szPath), m->strDropZoneDir.c_str(), Entry.szName); AssertRC(vrc);
1557 Bstr strName;
1558 hrc = Install(Bstr(szPath).raw(), strName.asOutParam());
1559 if (SUCCEEDED(hrc))
1560 RTFileWrite(hFile, "succeeded\n", sizeof("succeeded\n"), NULL);
1561 else
1562 {
1563 Utf8Str strErr;
1564 com::ErrorInfo Info;
1565 if (Info.isFullAvailable())
1566 strErr.printf("failed\n"
1567 "%ls\n"
1568 "Details: code %Rhrc (%#RX32), component %ls, interface %ls, callee %ls\n"
1569 ,
1570 Info.getText().raw(),
1571 Info.getResultCode(),
1572 Info.getResultCode(),
1573 Info.getComponent().raw(),
1574 Info.getInterfaceName().raw(),
1575 Info.getCalleeName().raw());
1576 else
1577 strErr.printf("failed\n"
1578 "hrc=%Rhrc (%#RX32)\n", hrc, hrc);
1579 RTFileWrite(hFile, strErr.c_str(), strErr.length(), NULL);
1580 }
1581 RTFileClose(hFile);
1582 }
1583 }
1584 }
1585 } /* foreach dir entry */
1586 RTDirClose(pDir);
1587}
1588
1589
1590/**
1591 * Calls the pfnVMCreated hook for all working extension packs.
1592 *
1593 * @param a_pMachine The machine interface of the new VM.
1594 */
1595void ExtPackManager::callAllVmCreatedHooks(IMachine *a_pMachine)
1596{
1597 AutoCaller autoCaller(this);
1598 HRESULT hrc = autoCaller.rc();
1599 if (FAILED(hrc))
1600 return;
1601 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1602
1603 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1604 it != m->llInstalledExtPacks.end();
1605 it++)
1606 (*it)->callVmCreatedHook(a_pMachine);
1607}
1608
1609/**
1610 * Calls the pfnVMConfigureVMM hook for all working extension packs.
1611 *
1612 * @returns VBox status code. Stops on the first failure, expecting the caller
1613 * to signal this to the caller of the CFGM constructor.
1614 * @param a_pConsole The console interface for the VM.
1615 * @param a_pVM The VM handle.
1616 */
1617int ExtPackManager::callAllVmConfigureVmmHooks(IConsole *a_pConsole, PVM a_pVM)
1618{
1619 AutoCaller autoCaller(this);
1620 HRESULT hrc = autoCaller.rc();
1621 if (FAILED(hrc))
1622 return Global::vboxStatusCodeFromCOM(hrc);
1623 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1624
1625 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1626 it != m->llInstalledExtPacks.end();
1627 it++)
1628 {
1629 int vrc = (*it)->callVmConfigureVmmHook(a_pConsole, a_pVM);
1630 if (RT_FAILURE(vrc))
1631 return vrc;
1632 }
1633
1634 return VINF_SUCCESS;
1635}
1636
1637/**
1638 * Calls the pfnVMPowerOn hook for all working extension packs.
1639 *
1640 * @returns VBox status code. Stops on the first failure, expecting the caller
1641 * to not power on the VM.
1642 * @param a_pConsole The console interface for the VM.
1643 * @param a_pVM The VM handle.
1644 */
1645int ExtPackManager::callAllVmPowerOnHooks(IConsole *a_pConsole, PVM a_pVM)
1646{
1647 AutoCaller autoCaller(this);
1648 HRESULT hrc = autoCaller.rc();
1649 if (FAILED(hrc))
1650 return Global::vboxStatusCodeFromCOM(hrc);
1651 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1652
1653 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1654 it != m->llInstalledExtPacks.end();
1655 it++)
1656 {
1657 int vrc = (*it)->callVmPowerOnHook(a_pConsole, a_pVM);
1658 if (RT_FAILURE(vrc))
1659 return vrc;
1660 }
1661
1662 return VINF_SUCCESS;
1663}
1664
1665/**
1666 * Calls the pfnVMPowerOff hook for all working extension packs.
1667 *
1668 * @param a_pConsole The console interface for the VM.
1669 * @param a_pVM The VM handle. Can be NULL.
1670 */
1671void ExtPackManager::callAllVmPowerOffHooks(IConsole *a_pConsole, PVM a_pVM)
1672{
1673 AutoCaller autoCaller(this);
1674 HRESULT hrc = autoCaller.rc();
1675 if (FAILED(hrc))
1676 return;
1677 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1678
1679 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1680 it != m->llInstalledExtPacks.end();
1681 it++)
1682 (*it)->callVmPowerOnHook(a_pConsole, a_pVM);
1683}
1684
1685
1686/* 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