SystemClass.cpp revision 68a912857707864bbaaff9808717813105072a6e
168a912857707864bbaaff9808717813105072a6ePatrick Benavoli/* <auto_header>
268a912857707864bbaaff9808717813105072a6ePatrick Benavoli * <FILENAME>
368a912857707864bbaaff9808717813105072a6ePatrick Benavoli *
468a912857707864bbaaff9808717813105072a6ePatrick Benavoli * INTEL CONFIDENTIAL
568a912857707864bbaaff9808717813105072a6ePatrick Benavoli * Copyright © 2011 Intel
668a912857707864bbaaff9808717813105072a6ePatrick Benavoli * Corporation All Rights Reserved.
768a912857707864bbaaff9808717813105072a6ePatrick Benavoli *
868a912857707864bbaaff9808717813105072a6ePatrick Benavoli * The source code contained or described herein and all documents related to
968a912857707864bbaaff9808717813105072a6ePatrick Benavoli * the source code ("Material") are owned by Intel Corporation or its suppliers
1068a912857707864bbaaff9808717813105072a6ePatrick Benavoli * or licensors. Title to the Material remains with Intel Corporation or its
1168a912857707864bbaaff9808717813105072a6ePatrick Benavoli * suppliers and licensors. The Material contains trade secrets and proprietary
1268a912857707864bbaaff9808717813105072a6ePatrick Benavoli * and confidential information of Intel or its suppliers and licensors. The
1368a912857707864bbaaff9808717813105072a6ePatrick Benavoli * Material is protected by worldwide copyright and trade secret laws and
1468a912857707864bbaaff9808717813105072a6ePatrick Benavoli * treaty provisions. No part of the Material may be used, copied, reproduced,
1568a912857707864bbaaff9808717813105072a6ePatrick Benavoli * modified, published, uploaded, posted, transmitted, distributed, or
1668a912857707864bbaaff9808717813105072a6ePatrick Benavoli * disclosed in any way without Intel’s prior express written permission.
1768a912857707864bbaaff9808717813105072a6ePatrick Benavoli *
1868a912857707864bbaaff9808717813105072a6ePatrick Benavoli * No license under any patent, copyright, trade secret or other intellectual
1968a912857707864bbaaff9808717813105072a6ePatrick Benavoli * property right is granted to or conferred upon you by disclosure or delivery
2068a912857707864bbaaff9808717813105072a6ePatrick Benavoli * of the Materials, either expressly, by implication, inducement, estoppel or
2168a912857707864bbaaff9808717813105072a6ePatrick Benavoli * otherwise. Any license under such intellectual property rights must be
2268a912857707864bbaaff9808717813105072a6ePatrick Benavoli * express and approved by Intel in writing.
2368a912857707864bbaaff9808717813105072a6ePatrick Benavoli *
2468a912857707864bbaaff9808717813105072a6ePatrick Benavoli *  AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
2568a912857707864bbaaff9808717813105072a6ePatrick Benavoli * CREATED: 2011-06-01
2668a912857707864bbaaff9808717813105072a6ePatrick Benavoli * UPDATED: 2011-07-27
2768a912857707864bbaaff9808717813105072a6ePatrick Benavoli *
2868a912857707864bbaaff9808717813105072a6ePatrick Benavoli *
2968a912857707864bbaaff9808717813105072a6ePatrick Benavoli * </auto_header>
3068a912857707864bbaaff9808717813105072a6ePatrick Benavoli */
3168a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include <dlfcn.h>
3268a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include <dirent.h>
3368a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "SystemClass.h"
3468a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "SubsystemLibrary.h"
3568a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "AutoLog.h"
3668a912857707864bbaaff9808717813105072a6ePatrick Benavoli
3768a912857707864bbaaff9808717813105072a6ePatrick Benavoli#define base CConfigurableElement
3868a912857707864bbaaff9808717813105072a6ePatrick Benavoli
3968a912857707864bbaaff9808717813105072a6ePatrick Benavoli// Used by subsystem plugins
4068a912857707864bbaaff9808717813105072a6ePatrick Benavolitypedef void (*GetSusbystemBuilder)(CSubsystemLibrary*);
4168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
4268a912857707864bbaaff9808717813105072a6ePatrick BenavoliCSystemClass::CSystemClass(const string& strName) : base(strName), _pSubsystemLibrary(new CSubsystemLibrary)
4368a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
4468a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
4568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
4668a912857707864bbaaff9808717813105072a6ePatrick BenavoliCSystemClass::~CSystemClass()
4768a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
4868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    delete _pSubsystemLibrary;
4968a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
5068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
5168a912857707864bbaaff9808717813105072a6ePatrick Benavolibool CSystemClass::childrenAreDynamic() const
5268a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
5368a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return true;
5468a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
5568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
5668a912857707864bbaaff9808717813105072a6ePatrick Benavolistring CSystemClass::getKind() const
5768a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
5868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return "SystemClass";
5968a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
6068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
6168a912857707864bbaaff9808717813105072a6ePatrick Benavolibool CSystemClass::loadSubsystems(string& strError, const vector<string>& astrPluginFolderPaths)
6268a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
6368a912857707864bbaaff9808717813105072a6ePatrick Benavoli    CAutoLog autoLlog(this, "Loading subsystem plugins");
6468a912857707864bbaaff9808717813105072a6ePatrick Benavoli
6568a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Plugin list
6668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    vector<string> astrPluginFiles;
6768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
6868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    uint32_t uiFolderLocation;
6968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
7068a912857707864bbaaff9808717813105072a6ePatrick Benavoli    for (uiFolderLocation = 0; uiFolderLocation < astrPluginFolderPaths.size(); uiFolderLocation++) {
7168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
7268a912857707864bbaaff9808717813105072a6ePatrick Benavoli        // Folder for current SystemClass
7368a912857707864bbaaff9808717813105072a6ePatrick Benavoli        string strPluginPath = astrPluginFolderPaths[uiFolderLocation] + "/" + getName() + "/";
7468a912857707864bbaaff9808717813105072a6ePatrick Benavoli
7568a912857707864bbaaff9808717813105072a6ePatrick Benavoli        // Get plugin list
7668a912857707864bbaaff9808717813105072a6ePatrick Benavoli        getPluginFiles(strPluginPath, astrPluginFiles);
7768a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
7868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Check at least one subsystem plugin available
7968a912857707864bbaaff9808717813105072a6ePatrick Benavoli    if (!astrPluginFiles.size()) {
8068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
8168a912857707864bbaaff9808717813105072a6ePatrick Benavoli        // No plugin found?
8268a912857707864bbaaff9808717813105072a6ePatrick Benavoli        strError = "No subsystem plugin found";
8368a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
8468a912857707864bbaaff9808717813105072a6ePatrick Benavoli
8568a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Actually load plugins
8668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    uint32_t uiPlugin;
8768a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Start clean
8868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    _pSubsystemLibrary->clean();
8968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9068a912857707864bbaaff9808717813105072a6ePatrick Benavoli    for (uiPlugin = 0; uiPlugin < astrPluginFiles.size(); uiPlugin++) {
9168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9268a912857707864bbaaff9808717813105072a6ePatrick Benavoli        string strPluginFileName = astrPluginFiles[uiPlugin];
9368a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9468a912857707864bbaaff9808717813105072a6ePatrick Benavoli        log("Loading subsystem plugin path \"%s\"", strPluginFileName.c_str());
9568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9668a912857707864bbaaff9808717813105072a6ePatrick Benavoli        void* lib_handle = dlopen(strPluginFileName.c_str(), RTLD_LAZY);
9768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9868a912857707864bbaaff9808717813105072a6ePatrick Benavoli        if (!lib_handle) {
9968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
10068a912857707864bbaaff9808717813105072a6ePatrick Benavoli            // Return error
10168a912857707864bbaaff9808717813105072a6ePatrick Benavoli            const char* pcError = dlerror();
10268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
10368a912857707864bbaaff9808717813105072a6ePatrick Benavoli            if (pcError) {
10468a912857707864bbaaff9808717813105072a6ePatrick Benavoli
10568a912857707864bbaaff9808717813105072a6ePatrick Benavoli                strError = pcError;
10668a912857707864bbaaff9808717813105072a6ePatrick Benavoli            } else {
10768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
10868a912857707864bbaaff9808717813105072a6ePatrick Benavoli                strError = "Unable to load subsystem plugin " + strPluginFileName;
10968a912857707864bbaaff9808717813105072a6ePatrick Benavoli            }
11068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
11168a912857707864bbaaff9808717813105072a6ePatrick Benavoli            _pSubsystemLibrary->clean();
11268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
11368a912857707864bbaaff9808717813105072a6ePatrick Benavoli            return false;
11468a912857707864bbaaff9808717813105072a6ePatrick Benavoli        }
11568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
11668a912857707864bbaaff9808717813105072a6ePatrick Benavoli        GetSusbystemBuilder pfnGetSusbystemBuilder = (GetSusbystemBuilder)dlsym(lib_handle, "getSusbystemBuilder");
11768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
11868a912857707864bbaaff9808717813105072a6ePatrick Benavoli        if (!pfnGetSusbystemBuilder) {
11968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
12068a912857707864bbaaff9808717813105072a6ePatrick Benavoli            strError = "Subsystem plugin " + strPluginFileName + " does not contain getSusbystemBuilder symbol.";
12168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
12268a912857707864bbaaff9808717813105072a6ePatrick Benavoli            _pSubsystemLibrary->clean();
12368a912857707864bbaaff9808717813105072a6ePatrick Benavoli
12468a912857707864bbaaff9808717813105072a6ePatrick Benavoli            return false;
12568a912857707864bbaaff9808717813105072a6ePatrick Benavoli        }
12668a912857707864bbaaff9808717813105072a6ePatrick Benavoli
12768a912857707864bbaaff9808717813105072a6ePatrick Benavoli        // Fill library
12868a912857707864bbaaff9808717813105072a6ePatrick Benavoli        pfnGetSusbystemBuilder(_pSubsystemLibrary);
12968a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
13068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
13168a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return true;
13268a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
13368a912857707864bbaaff9808717813105072a6ePatrick Benavoli
13468a912857707864bbaaff9808717813105072a6ePatrick Benavolibool CSystemClass::getPluginFiles(const string& strPluginPath, vector<string>& astrPluginFiles) const
13568a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
13668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    log("Seeking subsystem plugins from folder \"%s\"", strPluginPath.c_str());
13768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
13868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    DIR *dirp;
13968a912857707864bbaaff9808717813105072a6ePatrick Benavoli    struct dirent *dp;
14068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
14168a912857707864bbaaff9808717813105072a6ePatrick Benavoli    if ((dirp = opendir(strPluginPath.c_str())) == NULL) {
14268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
14368a912857707864bbaaff9808717813105072a6ePatrick Benavoli        return false;
14468a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
14568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
14668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    const string strPluginPattern("-subsystem.so");
14768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
14868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Parse it and load plugins
14968a912857707864bbaaff9808717813105072a6ePatrick Benavoli    while ((dp = readdir(dirp)) != NULL) {
15068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
15168a912857707864bbaaff9808717813105072a6ePatrick Benavoli        string strFileName(dp->d_name);
15268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
15368a912857707864bbaaff9808717813105072a6ePatrick Benavoli        // Check file name ends with "-susbsystem.so"
15468a912857707864bbaaff9808717813105072a6ePatrick Benavoli        size_t uiPatternPos = strFileName.rfind(strPluginPattern, -1);
15568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
15668a912857707864bbaaff9808717813105072a6ePatrick Benavoli        if (uiPatternPos != (size_t)-1 && uiPatternPos == strFileName.size() - strPluginPattern.size()) {
15768a912857707864bbaaff9808717813105072a6ePatrick Benavoli            // Found plugin
15868a912857707864bbaaff9808717813105072a6ePatrick Benavoli            astrPluginFiles.push_back(strPluginPath + strFileName);
15968a912857707864bbaaff9808717813105072a6ePatrick Benavoli        }
16068a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
16168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
16268a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Close plugin folder
16368a912857707864bbaaff9808717813105072a6ePatrick Benavoli    closedir(dirp);
16468a912857707864bbaaff9808717813105072a6ePatrick Benavoli
16568a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return true;
16668a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
16768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
16868a912857707864bbaaff9808717813105072a6ePatrick Benavoliconst CSubsystemLibrary* CSystemClass::getSubsystemLibrary() const
16968a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
17068a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return _pSubsystemLibrary;
17168a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
17268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
17368a912857707864bbaaff9808717813105072a6ePatrick Benavolibool CSystemClass::init(string& strError)
17468a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
17568a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return base::init(strError);
17668a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
17768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
178