blob: 6443e2c3dd15be0c8f622951dc59c86d2079d392 [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <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>
Patrick Benavoliec0f84e2011-10-27 14:34:38 +020033#include <algorithm>
34#include <ctype.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020035#include "SystemClass.h"
36#include "SubsystemLibrary.h"
37#include "AutoLog.h"
38
39#define base CConfigurableElement
40
Patrick Benavoliec0f84e2011-10-27 14:34:38 +020041// A plugin file name is of the form:
42// lib<type>-subsystem.so
43// The plugin symbol is of the form:
44// get<TYPE>SusbystemBuilder
45
46// Plugin file naming
47const char* gpcPluginPattern = "-subsystem.so";
48const char* gpcLibraryPrefix = "lib";
49
50// Plugin symbol naming
51const char* gpcPluginSymbolPrefix = "get";
52const char* gpcPluginSymbolSuffix = "SusbystemBuilder";
53
Patrick Benavoli68a91282011-08-31 11:23:23 +020054// Used by subsystem plugins
55typedef void (*GetSusbystemBuilder)(CSubsystemLibrary*);
56
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
76bool CSystemClass::loadSubsystems(string& strError, const vector<string>& astrPluginFolderPaths)
77{
78 CAutoLog autoLlog(this, "Loading subsystem plugins");
79
80 // Plugin list
81 vector<string> astrPluginFiles;
82
83 uint32_t uiFolderLocation;
84
85 for (uiFolderLocation = 0; uiFolderLocation < astrPluginFolderPaths.size(); uiFolderLocation++) {
86
87 // Folder for current SystemClass
Patrick Benavoli95ac0342011-11-07 20:32:51 +010088 string strPluginPath = astrPluginFolderPaths[uiFolderLocation] + "/";
Patrick Benavoli68a91282011-08-31 11:23:23 +020089
90 // Get plugin list
91 getPluginFiles(strPluginPath, astrPluginFiles);
92 }
93 // Check at least one subsystem plugin available
94 if (!astrPluginFiles.size()) {
95
96 // No plugin found?
97 strError = "No subsystem plugin found";
Patrick Benavoli1387bda2011-08-31 11:23:24 +020098
99 return false;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200100 }
101
102 // Actually load plugins
103 uint32_t uiPlugin;
104 // Start clean
105 _pSubsystemLibrary->clean();
106
107 for (uiPlugin = 0; uiPlugin < astrPluginFiles.size(); uiPlugin++) {
108
109 string strPluginFileName = astrPluginFiles[uiPlugin];
110
111 log("Loading subsystem plugin path \"%s\"", strPluginFileName.c_str());
112
113 void* lib_handle = dlopen(strPluginFileName.c_str(), RTLD_LAZY);
114
115 if (!lib_handle) {
116
117 // Return error
118 const char* pcError = dlerror();
119
120 if (pcError) {
121
122 strError = pcError;
123 } else {
124
125 strError = "Unable to load subsystem plugin " + strPluginFileName;
126 }
127
128 _pSubsystemLibrary->clean();
129
130 return false;
131 }
132
Patrick Benavoliec0f84e2011-10-27 14:34:38 +0200133 // Extract plugin type out of file name
134 string strPluginPattern = gpcPluginPattern;
135 string strLibraryPrefix = gpcLibraryPrefix;
136 // Remove folder
137 int32_t iSlashPos = strPluginFileName.rfind('/') + 1 + strLibraryPrefix.length();
138 // Get type
139 string strPluginType = strPluginFileName.substr(iSlashPos, strPluginFileName.length() - iSlashPos - strPluginPattern.length());
140
141 // Make it upper case
142 std::transform(strPluginType.begin(), strPluginType.end(), strPluginType.begin(), ::toupper);
143
144 // Get plugin symbol
145 string strPluginSymbol = gpcPluginSymbolPrefix + strPluginType + gpcPluginSymbolSuffix;
146
147 // Load symbol from library
148 GetSusbystemBuilder pfnGetSusbystemBuilder = (GetSusbystemBuilder)dlsym(lib_handle, strPluginSymbol.c_str());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200149
150 if (!pfnGetSusbystemBuilder) {
151
Patrick Benavoliec0f84e2011-10-27 14:34:38 +0200152 strError = "Subsystem plugin " + strPluginFileName + " does not contain " + strPluginSymbol + " symbol.";
Patrick Benavoli68a91282011-08-31 11:23:23 +0200153
154 _pSubsystemLibrary->clean();
155
156 return false;
157 }
158
159 // Fill library
160 pfnGetSusbystemBuilder(_pSubsystemLibrary);
161 }
162
163 return true;
164}
165
166bool CSystemClass::getPluginFiles(const string& strPluginPath, vector<string>& astrPluginFiles) const
167{
168 log("Seeking subsystem plugins from folder \"%s\"", strPluginPath.c_str());
169
170 DIR *dirp;
171 struct dirent *dp;
172
173 if ((dirp = opendir(strPluginPath.c_str())) == NULL) {
174
175 return false;
176 }
177
Patrick Benavoliec0f84e2011-10-27 14:34:38 +0200178 const string strPluginPattern(gpcPluginPattern);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200179
180 // Parse it and load plugins
181 while ((dp = readdir(dirp)) != NULL) {
182
183 string strFileName(dp->d_name);
184
Patrick Benavoliec0f84e2011-10-27 14:34:38 +0200185 // Check file name ends with pattern
Patrick Benavoli68a91282011-08-31 11:23:23 +0200186 size_t uiPatternPos = strFileName.rfind(strPluginPattern, -1);
187
188 if (uiPatternPos != (size_t)-1 && uiPatternPos == strFileName.size() - strPluginPattern.size()) {
189 // Found plugin
190 astrPluginFiles.push_back(strPluginPath + strFileName);
191 }
192 }
193
194 // Close plugin folder
195 closedir(dirp);
196
197 return true;
198}
199
200const CSubsystemLibrary* CSystemClass::getSubsystemLibrary() const
201{
202 return _pSubsystemLibrary;
203}
204
205bool CSystemClass::init(string& strError)
206{
207 return base::init(strError);
208}
209