Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1 | /* <auto_header> |
| 2 | * <FILENAME> |
| 3 | * |
| 4 | * INTEL CONFIDENTIAL |
| 5 | * Copyright © 2011 Intel |
| 6 | * Corporation All Rights Reserved. |
| 7 | * |
| 8 | * The source code contained or described herein and all documents related to |
| 9 | * the source code ("Material") are owned by Intel Corporation or its suppliers |
| 10 | * or licensors. Title to the Material remains with Intel Corporation or its |
| 11 | * suppliers and licensors. The Material contains trade secrets and proprietary |
| 12 | * and confidential information of Intel or its suppliers and licensors. The |
| 13 | * Material is protected by worldwide copyright and trade secret laws and |
| 14 | * treaty provisions. No part of the Material may be used, copied, reproduced, |
| 15 | * modified, published, uploaded, posted, transmitted, distributed, or |
| 16 | * disclosed in any way without Intel’s prior express written permission. |
| 17 | * |
| 18 | * No license under any patent, copyright, trade secret or other intellectual |
| 19 | * property right is granted to or conferred upon you by disclosure or delivery |
| 20 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 21 | * otherwise. Any license under such intellectual property rights must be |
| 22 | * express and approved by Intel in writing. |
| 23 | * |
| 24 | * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com) |
| 25 | * CREATED: 2011-06-01 |
| 26 | * UPDATED: 2011-07-27 |
| 27 | * |
| 28 | * |
| 29 | * </auto_header> |
| 30 | */ |
| 31 | #include <dlfcn.h> |
| 32 | #include <dirent.h> |
| 33 | #include "SystemClass.h" |
| 34 | #include "SubsystemLibrary.h" |
| 35 | #include "AutoLog.h" |
| 36 | |
| 37 | #define base CConfigurableElement |
| 38 | |
| 39 | // Used by subsystem plugins |
| 40 | typedef void (*GetSusbystemBuilder)(CSubsystemLibrary*); |
| 41 | |
| 42 | CSystemClass::CSystemClass(const string& strName) : base(strName), _pSubsystemLibrary(new CSubsystemLibrary) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | CSystemClass::~CSystemClass() |
| 47 | { |
| 48 | delete _pSubsystemLibrary; |
| 49 | } |
| 50 | |
| 51 | bool CSystemClass::childrenAreDynamic() const |
| 52 | { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | string CSystemClass::getKind() const |
| 57 | { |
| 58 | return "SystemClass"; |
| 59 | } |
| 60 | |
| 61 | bool CSystemClass::loadSubsystems(string& strError, const vector<string>& astrPluginFolderPaths) |
| 62 | { |
| 63 | CAutoLog autoLlog(this, "Loading subsystem plugins"); |
| 64 | |
| 65 | // Plugin list |
| 66 | vector<string> astrPluginFiles; |
| 67 | |
| 68 | uint32_t uiFolderLocation; |
| 69 | |
| 70 | for (uiFolderLocation = 0; uiFolderLocation < astrPluginFolderPaths.size(); uiFolderLocation++) { |
| 71 | |
| 72 | // Folder for current SystemClass |
| 73 | string strPluginPath = astrPluginFolderPaths[uiFolderLocation] + "/" + getName() + "/"; |
| 74 | |
| 75 | // Get plugin list |
| 76 | getPluginFiles(strPluginPath, astrPluginFiles); |
| 77 | } |
| 78 | // Check at least one subsystem plugin available |
| 79 | if (!astrPluginFiles.size()) { |
| 80 | |
| 81 | // No plugin found? |
| 82 | strError = "No subsystem plugin found"; |
Patrick Benavoli | 1387bda | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 83 | |
| 84 | return false; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Actually load plugins |
| 88 | uint32_t uiPlugin; |
| 89 | // Start clean |
| 90 | _pSubsystemLibrary->clean(); |
| 91 | |
| 92 | for (uiPlugin = 0; uiPlugin < astrPluginFiles.size(); uiPlugin++) { |
| 93 | |
| 94 | string strPluginFileName = astrPluginFiles[uiPlugin]; |
| 95 | |
| 96 | log("Loading subsystem plugin path \"%s\"", strPluginFileName.c_str()); |
| 97 | |
| 98 | void* lib_handle = dlopen(strPluginFileName.c_str(), RTLD_LAZY); |
| 99 | |
| 100 | if (!lib_handle) { |
| 101 | |
| 102 | // Return error |
| 103 | const char* pcError = dlerror(); |
| 104 | |
| 105 | if (pcError) { |
| 106 | |
| 107 | strError = pcError; |
| 108 | } else { |
| 109 | |
| 110 | strError = "Unable to load subsystem plugin " + strPluginFileName; |
| 111 | } |
| 112 | |
| 113 | _pSubsystemLibrary->clean(); |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | GetSusbystemBuilder pfnGetSusbystemBuilder = (GetSusbystemBuilder)dlsym(lib_handle, "getSusbystemBuilder"); |
| 119 | |
| 120 | if (!pfnGetSusbystemBuilder) { |
| 121 | |
| 122 | strError = "Subsystem plugin " + strPluginFileName + " does not contain getSusbystemBuilder symbol."; |
| 123 | |
| 124 | _pSubsystemLibrary->clean(); |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | // Fill library |
| 130 | pfnGetSusbystemBuilder(_pSubsystemLibrary); |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | bool CSystemClass::getPluginFiles(const string& strPluginPath, vector<string>& astrPluginFiles) const |
| 137 | { |
| 138 | log("Seeking subsystem plugins from folder \"%s\"", strPluginPath.c_str()); |
| 139 | |
| 140 | DIR *dirp; |
| 141 | struct dirent *dp; |
| 142 | |
| 143 | if ((dirp = opendir(strPluginPath.c_str())) == NULL) { |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | const string strPluginPattern("-subsystem.so"); |
| 149 | |
| 150 | // Parse it and load plugins |
| 151 | while ((dp = readdir(dirp)) != NULL) { |
| 152 | |
| 153 | string strFileName(dp->d_name); |
| 154 | |
| 155 | // Check file name ends with "-susbsystem.so" |
| 156 | size_t uiPatternPos = strFileName.rfind(strPluginPattern, -1); |
| 157 | |
| 158 | if (uiPatternPos != (size_t)-1 && uiPatternPos == strFileName.size() - strPluginPattern.size()) { |
| 159 | // Found plugin |
| 160 | astrPluginFiles.push_back(strPluginPath + strFileName); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Close plugin folder |
| 165 | closedir(dirp); |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | const CSubsystemLibrary* CSystemClass::getSubsystemLibrary() const |
| 171 | { |
| 172 | return _pSubsystemLibrary; |
| 173 | } |
| 174 | |
| 175 | bool CSystemClass::init(string& strError) |
| 176 | { |
| 177 | return base::init(strError); |
| 178 | } |
| 179 | |