blob: 98917d76cdc54a4c01cb351923beb8cbbe04cb07 [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * 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 Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
25#include <dlfcn.h>
26#include <dirent.h>
Patrick Benavoliec0f84e2011-10-27 14:34:38 +020027#include <algorithm>
28#include <ctype.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020029#include "SystemClass.h"
30#include "SubsystemLibrary.h"
31#include "AutoLog.h"
Patrick Benavoli6ccab9d2011-11-10 23:21:01 +010032#include "VirtualSubsystem.h"
33#include "NamedElementBuilderTemplate.h"
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +010034#include <assert.h>
Guillaume Denneulina4ec15d2012-02-17 14:38:14 +010035#include "PluginLocation.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020036
37#define base CConfigurableElement
38
Frédéric Boisnardd5679ac2013-07-31 17:40:57 +020039/**
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 Benavoliec0f84e2011-10-27 14:34:38 +020046// Plugin file naming
Frédéric Boisnardd5679ac2013-07-31 17:40:57 +020047const char* gpcPluginSuffix = "-subsystem";
48const char* gpcPluginPrefix = "lib";
Patrick Benavoliec0f84e2011-10-27 14:34:38 +020049
50// Plugin symbol naming
51const char* gpcPluginSymbolPrefix = "get";
Frédéric Boisnard487ce852013-07-19 17:17:52 +020052const char* gpcPluginSymbolSuffix = "SubsystemBuilder";
Patrick Benavoliec0f84e2011-10-27 14:34:38 +020053
Patrick Benavoli68a91282011-08-31 11:23:23 +020054// Used by subsystem plugins
Frédéric Boisnard487ce852013-07-19 17:17:52 +020055typedef void (*GetSubsystemBuilder)(CSubsystemLibrary*);
Patrick Benavoli68a91282011-08-31 11:23:23 +020056
Patrick Benavoli95ac0342011-11-07 20:32:51 +010057CSystemClass::CSystemClass() : _pSubsystemLibrary(new CSubsystemLibrary)
Patrick Benavoli68a91282011-08-31 11:23:23 +020058{
59}
60
61CSystemClass::~CSystemClass()
62{
63 delete _pSubsystemLibrary;
64}
65
66bool CSystemClass::childrenAreDynamic() const
67{
68 return true;
69}
70
71string CSystemClass::getKind() const
72{
73 return "SystemClass";
74}
75
Guillaume Denneulina4ec15d2012-02-17 14:38:14 +010076bool CSystemClass::loadSubsystems(string& strError, const CSubsystemPlugins* pSubsystemPlugins)
Patrick Benavoli68a91282011-08-31 11:23:23 +020077{
Kevin Rocardace81f82012-12-11 16:19:17 +010078 CAutoLog autoLlog_info(this, "Loading subsystem plugins");
Patrick Benavoli68a91282011-08-31 11:23:23 +020079
80 // Plugin list
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +010081 list<string> lstrPluginFiles;
Patrick Benavoli68a91282011-08-31 11:23:23 +020082
Guillaume Denneulina4ec15d2012-02-17 14:38:14 +010083 uint32_t uiPluginLocation;
Patrick Benavoli68a91282011-08-31 11:23:23 +020084
Guillaume Denneulina4ec15d2012-02-17 14:38:14 +010085 for (uiPluginLocation = 0; uiPluginLocation < pSubsystemPlugins->getNbChildren(); uiPluginLocation++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +020086
Guillaume Denneulina4ec15d2012-02-17 14:38:14 +010087 // Get Folder for current Plugin Location
88 const CPluginLocation* pPluginLocation = static_cast<const CPluginLocation*>(pSubsystemPlugins->getChild(uiPluginLocation));
Patrick Benavoli68a91282011-08-31 11:23:23 +020089
Guillaume Denneulina4ec15d2012-02-17 14:38:14 +010090 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 Benavoli68a91282011-08-31 11:23:23 +0200103 }
104 // Check at least one subsystem plugin available
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100105 if (!lstrPluginFiles.size()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200106
107 // No plugin found?
Kevin Rocardace81f82012-12-11 16:19:17 +0100108 log_warning("No subsystem plugin found");
Patrick Benavoli1387bda2011-08-31 11:23:24 +0200109
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100110 // Don't bail out now that we have virtual subsystems
Patrick Benavoli68a91282011-08-31 11:23:23 +0200111 }
112
Patrick Benavoli68a91282011-08-31 11:23:23 +0200113 // Start clean
114 _pSubsystemLibrary->clean();
115
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100116 // Actually load plugins
117 while (lstrPluginFiles.size()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200118
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100119 // 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 Benavoli68a91282011-08-31 11:23:23 +0200123
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100124 // Attempt to load the complete list
125 if (!loadPlugins(lstrPluginFiles, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200126
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100127 // Display the list of plugins we were unable to load
Patrick Benavoli68a91282011-08-31 11:23:23 +0200128
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100129 // Leave clean
Patrick Benavoli68a91282011-08-31 11:23:23 +0200130 _pSubsystemLibrary->clean();
131
132 return false;
133 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200134 }
Kevin Rocardace81f82012-12-11 16:19:17 +0100135 log_info("All subsystem plugins successfully loaded");
Patrick Benavoli68a91282011-08-31 11:23:23 +0200136
Patrick Benavoli6ccab9d2011-11-10 23:21:01 +0100137 // Add virtual subsystem builder
138 _pSubsystemLibrary->addElementBuilder(new TNamedElementBuilderTemplate<CVirtualSubsystem>("Virtual"));
139
Patrick Benavoli68a91282011-08-31 11:23:23 +0200140 return true;
141}
142
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100143// Plugin symbol computation
144string CSystemClass::getPluginSymbol(const string& strPluginPath)
145{
146 // Extract plugin type out of file name
Frédéric Boisnardd5679ac2013-07-31 17:40:57 +0200147 string strPluginSuffix = gpcPluginSuffix;
148 string strPluginPrefix = gpcPluginPrefix;
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100149
Frédéric Boisnardd5679ac2013-07-31 17:40:57 +0200150 // Remove folder and library prefix
151 size_t iPluginTypePos = strPluginPath.rfind('/') + 1 + strPluginPrefix.length();
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100152
Frédéric Boisnardd5679ac2013-07-31 17:40:57 +0200153 // 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 Benavoli9bed7ce2011-11-20 20:04:35 +0100158
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
167bool 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 Rocardace81f82012-12-11 16:19:17 +0100179 log_info("Attempting to load subsystem plugin path \"%s\"", strPluginFileName.c_str());
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100180
181 // Load attempt
182 void* lib_handle = dlopen(strPluginFileName.c_str(), RTLD_LAZY);
183
184 if (!lib_handle) {
185
186 // Failed
Kevin Rocardace81f82012-12-11 16:19:17 +0100187 log_warning("Plugin load failed: %s, proceeding on with remaining ones", dlerror());
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100188
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 Boisnard487ce852013-07-19 17:17:52 +0200199 GetSubsystemBuilder pfnGetSubsystemBuilder = (GetSubsystemBuilder)dlsym(lib_handle, strPluginSymbol.c_str());
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100200
Frédéric Boisnard487ce852013-07-19 17:17:52 +0200201 if (!pfnGetSubsystemBuilder) {
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100202
203 strError = "Subsystem plugin " + strPluginFileName + " does not contain " + strPluginSymbol + " symbol.";
204
205 return false;
206 }
207
208 // Fill library
Frédéric Boisnard487ce852013-07-19 17:17:52 +0200209 pfnGetSubsystemBuilder(_pSubsystemLibrary);
Patrick Benavoli9bed7ce2011-11-20 20:04:35 +0100210
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 Benavoli68a91282011-08-31 11:23:23 +0200235const CSubsystemLibrary* CSystemClass::getSubsystemLibrary() const
236{
237 return _pSubsystemLibrary;
238}
239
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +0100240void 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 Rocard2fbe6e82013-03-26 17:09:29 +0100259void 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 Benavoli68a91282011-08-31 11:23:23 +0200273bool CSystemClass::init(string& strError)
274{
275 return base::init(strError);
276}
277
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200278// From IXmlSource
279void 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}