Kevin Rocard | 93250d1 | 2012-07-19 17:48:30 +0200 | [diff] [blame] | 1 | /* |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 2 | * INTEL CONFIDENTIAL |
| 3 | * Copyright © 2011 Intel |
| 4 | * Corporation All Rights Reserved. |
| 5 | * |
| 6 | * The source code contained or described herein and all documents related to |
| 7 | * the source code ("Material") are owned by Intel Corporation or its suppliers |
| 8 | * or licensors. Title to the Material remains with Intel Corporation or its |
| 9 | * suppliers and licensors. The Material contains trade secrets and proprietary |
| 10 | * and confidential information of Intel or its suppliers and licensors. The |
| 11 | * Material is protected by worldwide copyright and trade secret laws and |
| 12 | * treaty provisions. No part of the Material may be used, copied, reproduced, |
| 13 | * modified, published, uploaded, posted, transmitted, distributed, or |
| 14 | * disclosed in any way without Intel’s prior express written permission. |
| 15 | * |
| 16 | * No license under any patent, copyright, trade secret or other intellectual |
| 17 | * property right is granted to or conferred upon you by disclosure or delivery |
| 18 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 19 | * otherwise. Any license under such intellectual property rights must be |
| 20 | * express and approved by Intel in writing. |
| 21 | * |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 22 | * CREATED: 2011-06-01 |
| 23 | * UPDATED: 2011-07-27 |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 24 | */ |
| 25 | #include <dlfcn.h> |
| 26 | #include <dirent.h> |
Patrick Benavoli | ec0f84e | 2011-10-27 14:34:38 +0200 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <ctype.h> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | #include "SystemClass.h" |
| 30 | #include "SubsystemLibrary.h" |
| 31 | #include "AutoLog.h" |
Patrick Benavoli | 6ccab9d | 2011-11-10 23:21:01 +0100 | [diff] [blame] | 32 | #include "VirtualSubsystem.h" |
| 33 | #include "NamedElementBuilderTemplate.h" |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 34 | #include <assert.h> |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 35 | #include "PluginLocation.h" |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 36 | |
| 37 | #define base CConfigurableElement |
| 38 | |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame^] | 39 | /** |
| 40 | * A plugin file name is of the form: |
| 41 | * lib<type>-subsystem.so or lib<type>-subsystem._host.so |
| 42 | * |
| 43 | * The plugin symbol is of the form: |
| 44 | * get<TYPE>SubsystemBuilder |
| 45 | */ |
Patrick Benavoli | ec0f84e | 2011-10-27 14:34:38 +0200 | [diff] [blame] | 46 | // Plugin file naming |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame^] | 47 | const char* gpcPluginSuffix = "-subsystem"; |
| 48 | const char* gpcPluginPrefix = "lib"; |
Patrick Benavoli | ec0f84e | 2011-10-27 14:34:38 +0200 | [diff] [blame] | 49 | |
| 50 | // Plugin symbol naming |
| 51 | const char* gpcPluginSymbolPrefix = "get"; |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 52 | const char* gpcPluginSymbolSuffix = "SubsystemBuilder"; |
Patrick Benavoli | ec0f84e | 2011-10-27 14:34:38 +0200 | [diff] [blame] | 53 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 54 | // Used by subsystem plugins |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 55 | typedef void (*GetSubsystemBuilder)(CSubsystemLibrary*); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 56 | |
Patrick Benavoli | 95ac034 | 2011-11-07 20:32:51 +0100 | [diff] [blame] | 57 | CSystemClass::CSystemClass() : _pSubsystemLibrary(new CSubsystemLibrary) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | |
| 61 | CSystemClass::~CSystemClass() |
| 62 | { |
| 63 | delete _pSubsystemLibrary; |
| 64 | } |
| 65 | |
| 66 | bool CSystemClass::childrenAreDynamic() const |
| 67 | { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | string CSystemClass::getKind() const |
| 72 | { |
| 73 | return "SystemClass"; |
| 74 | } |
| 75 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 76 | bool CSystemClass::loadSubsystems(string& strError, const CSubsystemPlugins* pSubsystemPlugins) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 77 | { |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 78 | CAutoLog autoLlog_info(this, "Loading subsystem plugins"); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 79 | |
| 80 | // Plugin list |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 81 | list<string> lstrPluginFiles; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 82 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 83 | uint32_t uiPluginLocation; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 84 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 85 | for (uiPluginLocation = 0; uiPluginLocation < pSubsystemPlugins->getNbChildren(); uiPluginLocation++) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 86 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 87 | // Get Folder for current Plugin Location |
| 88 | const CPluginLocation* pPluginLocation = static_cast<const CPluginLocation*>(pSubsystemPlugins->getChild(uiPluginLocation)); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 89 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 90 | const string& strFolder = pPluginLocation->getFolder(); |
| 91 | |
| 92 | // Iterator on Plugin List: |
| 93 | list<string>::const_iterator it; |
| 94 | |
| 95 | const list<string>& pluginList = pPluginLocation->getPluginList(); |
| 96 | |
| 97 | for (it = pluginList.begin(); it != pluginList.end(); ++it) { |
| 98 | |
| 99 | // Fill Plugin files list |
| 100 | lstrPluginFiles.push_back(strFolder + "/" + *it); |
| 101 | } |
| 102 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 103 | } |
| 104 | // Check at least one subsystem plugin available |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 105 | if (!lstrPluginFiles.size()) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 106 | |
| 107 | // No plugin found? |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 108 | log_warning("No subsystem plugin found"); |
Patrick Benavoli | 1387bda | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 109 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 110 | // Don't bail out now that we have virtual subsystems |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 113 | // Start clean |
| 114 | _pSubsystemLibrary->clean(); |
| 115 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 116 | // Actually load plugins |
| 117 | while (lstrPluginFiles.size()) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 118 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 119 | // Because plugins might depend on one another, loading will be done |
| 120 | // as an iteration process that finishes successfully when the remaining |
| 121 | // list of plugins to load gets empty or unsuccessfully if the loading |
| 122 | // process failed to load at least one of them |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 123 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 124 | // Attempt to load the complete list |
| 125 | if (!loadPlugins(lstrPluginFiles, strError)) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 126 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 127 | // Display the list of plugins we were unable to load |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 128 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 129 | // Leave clean |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 130 | _pSubsystemLibrary->clean(); |
| 131 | |
| 132 | return false; |
| 133 | } |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 134 | } |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 135 | log_info("All subsystem plugins successfully loaded"); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 136 | |
Patrick Benavoli | 6ccab9d | 2011-11-10 23:21:01 +0100 | [diff] [blame] | 137 | // Add virtual subsystem builder |
| 138 | _pSubsystemLibrary->addElementBuilder(new TNamedElementBuilderTemplate<CVirtualSubsystem>("Virtual")); |
| 139 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 143 | // Plugin symbol computation |
| 144 | string CSystemClass::getPluginSymbol(const string& strPluginPath) |
| 145 | { |
| 146 | // Extract plugin type out of file name |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame^] | 147 | string strPluginSuffix = gpcPluginSuffix; |
| 148 | string strPluginPrefix = gpcPluginPrefix; |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 149 | |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame^] | 150 | // Remove folder and library prefix |
| 151 | size_t iPluginTypePos = strPluginPath.rfind('/') + 1 + strPluginPrefix.length(); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 152 | |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame^] | 153 | // Get index of -subsystem.so or -subsystem_host.so suffix |
| 154 | size_t iSubsystemPos = strPluginPath.find(strPluginSuffix, iPluginTypePos); |
| 155 | |
| 156 | // Get type (between iPluginTypePos and iSubsystemPos) |
| 157 | string strPluginType = strPluginPath.substr(iPluginTypePos, iSubsystemPos - iPluginTypePos); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 158 | |
| 159 | // Make it upper case |
| 160 | std::transform(strPluginType.begin(), strPluginType.end(), strPluginType.begin(), ::toupper); |
| 161 | |
| 162 | // Get plugin symbol |
| 163 | return gpcPluginSymbolPrefix + strPluginType + gpcPluginSymbolSuffix; |
| 164 | } |
| 165 | |
| 166 | // Plugin loading |
| 167 | bool CSystemClass::loadPlugins(list<string>& lstrPluginFiles, string& strError) |
| 168 | { |
| 169 | assert(lstrPluginFiles.size()); |
| 170 | |
| 171 | bool bAtLeastOneSybsystemPluginSuccessfullyLoaded = false; |
| 172 | |
| 173 | list<string>::iterator it = lstrPluginFiles.begin(); |
| 174 | |
| 175 | while (it != lstrPluginFiles.end()) { |
| 176 | |
| 177 | string strPluginFileName = *it; |
| 178 | |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 179 | log_info("Attempting to load subsystem plugin path \"%s\"", strPluginFileName.c_str()); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 180 | |
| 181 | // Load attempt |
| 182 | void* lib_handle = dlopen(strPluginFileName.c_str(), RTLD_LAZY); |
| 183 | |
| 184 | if (!lib_handle) { |
| 185 | |
| 186 | // Failed |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 187 | log_warning("Plugin load failed: %s, proceeding on with remaining ones", dlerror()); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 188 | |
| 189 | // Next plugin |
| 190 | ++it; |
| 191 | |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | // Get plugin symbol |
| 196 | string strPluginSymbol = getPluginSymbol(strPluginFileName); |
| 197 | |
| 198 | // Load symbol from library |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 199 | GetSubsystemBuilder pfnGetSubsystemBuilder = (GetSubsystemBuilder)dlsym(lib_handle, strPluginSymbol.c_str()); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 200 | |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 201 | if (!pfnGetSubsystemBuilder) { |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 202 | |
| 203 | strError = "Subsystem plugin " + strPluginFileName + " does not contain " + strPluginSymbol + " symbol."; |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | // Fill library |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 209 | pfnGetSubsystemBuilder(_pSubsystemLibrary); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 210 | |
| 211 | // Account for this success |
| 212 | bAtLeastOneSybsystemPluginSuccessfullyLoaded = true; |
| 213 | |
| 214 | // Remove successfully loaded plugin from list and select next |
| 215 | lstrPluginFiles.erase(it++); |
| 216 | } |
| 217 | |
| 218 | // Check for success |
| 219 | if (!bAtLeastOneSybsystemPluginSuccessfullyLoaded) { |
| 220 | |
| 221 | // Return list of plugins we were unable to load |
| 222 | strError = "Unable to load the following plugins:\n"; |
| 223 | |
| 224 | for (it = lstrPluginFiles.begin(); it != lstrPluginFiles.end(); ++it) { |
| 225 | |
| 226 | strError += *it + "\n"; |
| 227 | } |
| 228 | |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 235 | const CSubsystemLibrary* CSystemClass::getSubsystemLibrary() const |
| 236 | { |
| 237 | return _pSubsystemLibrary; |
| 238 | } |
| 239 | |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 240 | void CSystemClass::checkForSubsystemsToResync(CSyncerSet& syncerSet) |
| 241 | { |
| 242 | uint32_t uiNbChildren = getNbChildren(); |
| 243 | uint32_t uiChild; |
| 244 | |
| 245 | for (uiChild = 0; uiChild < uiNbChildren; uiChild++) { |
| 246 | |
| 247 | CSubsystem* pSubsystem = static_cast<CSubsystem*>(getChild(uiChild)); |
| 248 | |
| 249 | // Collect and consume the need for a resync |
| 250 | if (pSubsystem->needResync(true)) { |
| 251 | |
| 252 | log_info("Resynchronizing subsystem: %s", pSubsystem->getName().c_str()); |
| 253 | // get all subsystem syncers |
| 254 | pSubsystem->fillSyncerSet(syncerSet); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
Kevin Rocard | 2fbe6e8 | 2013-03-26 17:09:29 +0100 | [diff] [blame] | 259 | void CSystemClass::cleanSubsystemsNeedToResync() |
| 260 | { |
| 261 | uint32_t uiNbChildren = getNbChildren(); |
| 262 | uint32_t uiChild; |
| 263 | |
| 264 | for (uiChild = 0; uiChild < uiNbChildren; uiChild++) { |
| 265 | |
| 266 | CSubsystem* pSubsystem = static_cast<CSubsystem*>(getChild(uiChild)); |
| 267 | |
| 268 | // Consume the need for a resync |
| 269 | pSubsystem->needResync(true); |
| 270 | } |
| 271 | } |
| 272 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 273 | bool CSystemClass::init(string& strError) |
| 274 | { |
| 275 | return base::init(strError); |
| 276 | } |
| 277 | |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 278 | // From IXmlSource |
| 279 | void CSystemClass::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| 280 | { |
| 281 | // Set the name of system class |
| 282 | xmlElement.setNameAttribute(getName()); |
| 283 | |
| 284 | base::toXml(xmlElement, serializingContext); |
| 285 | } |