Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 1 | /* |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 2 | * INTEL CONFIDENTIAL |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 3 | * Copyright © 2011 Intel |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 4 | * Corporation All Rights Reserved. |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 5 | * |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 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. |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 15 | * |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 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. |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 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" |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 36 | #include "Utility.h" |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 37 | |
| 38 | #define base CConfigurableElement |
| 39 | |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame] | 40 | /** |
| 41 | * A plugin file name is of the form: |
| 42 | * lib<type>-subsystem.so or lib<type>-subsystem._host.so |
| 43 | * |
| 44 | * The plugin symbol is of the form: |
| 45 | * get<TYPE>SubsystemBuilder |
| 46 | */ |
Patrick Benavoli | ec0f84e | 2011-10-27 14:34:38 +0200 | [diff] [blame] | 47 | // Plugin file naming |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame] | 48 | const char* gpcPluginSuffix = "-subsystem"; |
| 49 | const char* gpcPluginPrefix = "lib"; |
Patrick Benavoli | ec0f84e | 2011-10-27 14:34:38 +0200 | [diff] [blame] | 50 | |
| 51 | // Plugin symbol naming |
| 52 | const char* gpcPluginSymbolPrefix = "get"; |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 53 | const char* gpcPluginSymbolSuffix = "SubsystemBuilder"; |
Patrick Benavoli | ec0f84e | 2011-10-27 14:34:38 +0200 | [diff] [blame] | 54 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 55 | // Used by subsystem plugins |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 56 | typedef void (*GetSubsystemBuilder)(CSubsystemLibrary*); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 57 | |
Patrick Benavoli | 95ac034 | 2011-11-07 20:32:51 +0100 | [diff] [blame] | 58 | CSystemClass::CSystemClass() : _pSubsystemLibrary(new CSubsystemLibrary) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | CSystemClass::~CSystemClass() |
| 63 | { |
| 64 | delete _pSubsystemLibrary; |
| 65 | } |
| 66 | |
| 67 | bool CSystemClass::childrenAreDynamic() const |
| 68 | { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | string CSystemClass::getKind() const |
| 73 | { |
| 74 | return "SystemClass"; |
| 75 | } |
| 76 | |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 77 | bool CSystemClass::loadSubsystems(string& strError, |
Kevin Rocard | 6be8035 | 2013-07-11 12:42:23 +0200 | [diff] [blame] | 78 | const CSubsystemPlugins* pSubsystemPlugins, |
| 79 | bool bVirtualSubsystemFallback) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 80 | { |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 81 | CAutoLog autoLog_info(this, "Loading subsystem plugins"); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 82 | |
Kevin Rocard | 6be8035 | 2013-07-11 12:42:23 +0200 | [diff] [blame] | 83 | // Start clean |
| 84 | _pSubsystemLibrary->clean(); |
| 85 | |
| 86 | // Add virtual subsystem builder |
| 87 | _pSubsystemLibrary->addElementBuilder("Virtual", |
| 88 | new TNamedElementBuilderTemplate<CVirtualSubsystem>()); |
| 89 | // Set virtual subsytem as builder fallback if required |
| 90 | _pSubsystemLibrary->enableDefaultMechanism(bVirtualSubsystemFallback); |
| 91 | |
| 92 | // Add subsystem defined in shared libraries |
| 93 | list<string> lstrError; |
| 94 | bool bLoadPluginsSuccess = loadSubsystemsFromSharedLibraries(lstrError, pSubsystemPlugins); |
| 95 | |
| 96 | if (bLoadPluginsSuccess) { |
| 97 | log_info("All subsystem plugins successfully loaded"); |
| 98 | } else { |
| 99 | // Log plugin as warning if no fallback available |
| 100 | log_table(!bVirtualSubsystemFallback, lstrError); |
| 101 | } |
| 102 | |
| 103 | if (!bVirtualSubsystemFallback) { |
| 104 | // Any problem reported is an error as there is no fallback. |
| 105 | // Fill strError for caller. |
| 106 | CUtility::asString(lstrError, strError); |
| 107 | } |
| 108 | |
| 109 | return bLoadPluginsSuccess || bVirtualSubsystemFallback; |
| 110 | } |
| 111 | |
| 112 | bool CSystemClass::loadSubsystemsFromSharedLibraries(list<string>& lstrError, |
| 113 | const CSubsystemPlugins* pSubsystemPlugins) |
| 114 | { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 115 | // Plugin list |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 116 | list<string> lstrPluginFiles; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 117 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 118 | uint32_t uiPluginLocation; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 119 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 120 | for (uiPluginLocation = 0; uiPluginLocation < pSubsystemPlugins->getNbChildren(); uiPluginLocation++) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 121 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 122 | // Get Folder for current Plugin Location |
| 123 | const CPluginLocation* pPluginLocation = static_cast<const CPluginLocation*>(pSubsystemPlugins->getChild(uiPluginLocation)); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 124 | |
Guillaume Denneulin | a4ec15d | 2012-02-17 14:38:14 +0100 | [diff] [blame] | 125 | const string& strFolder = pPluginLocation->getFolder(); |
| 126 | |
| 127 | // Iterator on Plugin List: |
| 128 | list<string>::const_iterator it; |
| 129 | |
| 130 | const list<string>& pluginList = pPluginLocation->getPluginList(); |
| 131 | |
| 132 | for (it = pluginList.begin(); it != pluginList.end(); ++it) { |
| 133 | |
| 134 | // Fill Plugin files list |
| 135 | lstrPluginFiles.push_back(strFolder + "/" + *it); |
| 136 | } |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 139 | // Actually load plugins |
Kevin Rocard | 6be8035 | 2013-07-11 12:42:23 +0200 | [diff] [blame] | 140 | while (!lstrPluginFiles.empty()) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 141 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 142 | // Because plugins might depend on one another, loading will be done |
| 143 | // as an iteration process that finishes successfully when the remaining |
| 144 | // list of plugins to load gets empty or unsuccessfully if the loading |
| 145 | // process failed to load at least one of them |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 146 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 147 | // Attempt to load the complete list |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 148 | if (!loadPlugins(lstrPluginFiles, lstrError)) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 149 | |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 150 | // Unable to load at least one plugin |
| 151 | break; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 152 | } |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 153 | } |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 154 | |
Kevin Rocard | 6be8035 | 2013-07-11 12:42:23 +0200 | [diff] [blame] | 155 | if (!lstrPluginFiles.empty()) { |
| 156 | // Unable to load at least one plugin |
| 157 | string strPluginUnloaded; |
| 158 | CUtility::asString(lstrPluginFiles, strPluginUnloaded, ", "); |
| 159 | |
Frédéric Boisnard | ecddaa3 | 2013-11-04 18:26:01 +0100 | [diff] [blame^] | 160 | lstrError.push_back("Unable to load the following plugins: " + strPluginUnloaded + "."); |
Kevin Rocard | 6be8035 | 2013-07-11 12:42:23 +0200 | [diff] [blame] | 161 | return false; |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 162 | } |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 163 | |
Kevin Rocard | 6be8035 | 2013-07-11 12:42:23 +0200 | [diff] [blame] | 164 | return true; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 167 | // Plugin symbol computation |
| 168 | string CSystemClass::getPluginSymbol(const string& strPluginPath) |
| 169 | { |
| 170 | // Extract plugin type out of file name |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame] | 171 | string strPluginSuffix = gpcPluginSuffix; |
| 172 | string strPluginPrefix = gpcPluginPrefix; |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 173 | |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame] | 174 | // Remove folder and library prefix |
| 175 | size_t iPluginTypePos = strPluginPath.rfind('/') + 1 + strPluginPrefix.length(); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 176 | |
Frédéric Boisnard | d5679ac | 2013-07-31 17:40:57 +0200 | [diff] [blame] | 177 | // Get index of -subsystem.so or -subsystem_host.so suffix |
| 178 | size_t iSubsystemPos = strPluginPath.find(strPluginSuffix, iPluginTypePos); |
| 179 | |
| 180 | // Get type (between iPluginTypePos and iSubsystemPos) |
| 181 | string strPluginType = strPluginPath.substr(iPluginTypePos, iSubsystemPos - iPluginTypePos); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 182 | |
| 183 | // Make it upper case |
| 184 | std::transform(strPluginType.begin(), strPluginType.end(), strPluginType.begin(), ::toupper); |
| 185 | |
| 186 | // Get plugin symbol |
| 187 | return gpcPluginSymbolPrefix + strPluginType + gpcPluginSymbolSuffix; |
| 188 | } |
| 189 | |
| 190 | // Plugin loading |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 191 | bool CSystemClass::loadPlugins(list<string>& lstrPluginFiles, list<string>& lstrError) |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 192 | { |
| 193 | assert(lstrPluginFiles.size()); |
| 194 | |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 195 | bool bAtLeastOneSubsystemPluginSuccessfullyLoaded = false; |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 196 | |
| 197 | list<string>::iterator it = lstrPluginFiles.begin(); |
| 198 | |
| 199 | while (it != lstrPluginFiles.end()) { |
| 200 | |
| 201 | string strPluginFileName = *it; |
| 202 | |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 203 | log_info("Attempting to load subsystem plugin path \"%s\"", strPluginFileName.c_str()); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 204 | |
| 205 | // Load attempt |
| 206 | void* lib_handle = dlopen(strPluginFileName.c_str(), RTLD_LAZY); |
| 207 | |
| 208 | if (!lib_handle) { |
| 209 | |
| 210 | // Failed |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 211 | lstrError.push_back("Plugin load failed: " + string(dlerror())); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 212 | |
| 213 | // Next plugin |
| 214 | ++it; |
| 215 | |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | // Get plugin symbol |
| 220 | string strPluginSymbol = getPluginSymbol(strPluginFileName); |
| 221 | |
| 222 | // Load symbol from library |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 223 | GetSubsystemBuilder pfnGetSubsystemBuilder = (GetSubsystemBuilder)dlsym(lib_handle, strPluginSymbol.c_str()); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 224 | |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 225 | if (!pfnGetSubsystemBuilder) { |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 226 | |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 227 | lstrError.push_back("Subsystem plugin " + strPluginFileName + |
| 228 | " does not contain " + strPluginSymbol + " symbol."); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 229 | |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 230 | continue; |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 233 | // Account for this success |
| 234 | bAtLeastOneSubsystemPluginSuccessfullyLoaded = true; |
| 235 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 236 | // Fill library |
Frédéric Boisnard | 487ce85 | 2013-07-19 17:17:52 +0200 | [diff] [blame] | 237 | pfnGetSubsystemBuilder(_pSubsystemLibrary); |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 238 | |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 239 | // Remove successfully loaded plugin from list and select next |
| 240 | lstrPluginFiles.erase(it++); |
| 241 | } |
| 242 | |
Kevin Rocard | ee7ceed | 2013-07-05 10:56:27 +0200 | [diff] [blame] | 243 | return bAtLeastOneSubsystemPluginSuccessfullyLoaded; |
Patrick Benavoli | 9bed7ce | 2011-11-20 20:04:35 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 246 | const CSubsystemLibrary* CSystemClass::getSubsystemLibrary() const |
| 247 | { |
| 248 | return _pSubsystemLibrary; |
| 249 | } |
| 250 | |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 251 | void CSystemClass::checkForSubsystemsToResync(CSyncerSet& syncerSet) |
| 252 | { |
| 253 | uint32_t uiNbChildren = getNbChildren(); |
| 254 | uint32_t uiChild; |
| 255 | |
| 256 | for (uiChild = 0; uiChild < uiNbChildren; uiChild++) { |
| 257 | |
| 258 | CSubsystem* pSubsystem = static_cast<CSubsystem*>(getChild(uiChild)); |
| 259 | |
| 260 | // Collect and consume the need for a resync |
| 261 | if (pSubsystem->needResync(true)) { |
| 262 | |
| 263 | log_info("Resynchronizing subsystem: %s", pSubsystem->getName().c_str()); |
| 264 | // get all subsystem syncers |
| 265 | pSubsystem->fillSyncerSet(syncerSet); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
Kevin Rocard | 2fbe6e8 | 2013-03-26 17:09:29 +0100 | [diff] [blame] | 270 | void CSystemClass::cleanSubsystemsNeedToResync() |
| 271 | { |
| 272 | uint32_t uiNbChildren = getNbChildren(); |
| 273 | uint32_t uiChild; |
| 274 | |
| 275 | for (uiChild = 0; uiChild < uiNbChildren; uiChild++) { |
| 276 | |
| 277 | CSubsystem* pSubsystem = static_cast<CSubsystem*>(getChild(uiChild)); |
| 278 | |
| 279 | // Consume the need for a resync |
| 280 | pSubsystem->needResync(true); |
| 281 | } |
| 282 | } |
| 283 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 284 | bool CSystemClass::init(string& strError) |
| 285 | { |
| 286 | return base::init(strError); |
| 287 | } |
| 288 | |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 289 | // From IXmlSource |
| 290 | void CSystemClass::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| 291 | { |
| 292 | // Set the name of system class |
| 293 | xmlElement.setNameAttribute(getName()); |
| 294 | |
| 295 | base::toXml(xmlElement, serializingContext); |
| 296 | } |