blob: 62b6538a0e41af43cbed078c374a24b4cea79f6b [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>
33#include "SystemClass.h"
34#include "SubsystemLibrary.h"
35#include "AutoLog.h"
36
37#define base CConfigurableElement
38
39// Used by subsystem plugins
40typedef void (*GetSusbystemBuilder)(CSubsystemLibrary*);
41
42CSystemClass::CSystemClass(const string& strName) : base(strName), _pSubsystemLibrary(new CSubsystemLibrary)
43{
44}
45
46CSystemClass::~CSystemClass()
47{
48 delete _pSubsystemLibrary;
49}
50
51bool CSystemClass::childrenAreDynamic() const
52{
53 return true;
54}
55
56string CSystemClass::getKind() const
57{
58 return "SystemClass";
59}
60
61bool 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";
83 }
84
85 // Actually load plugins
86 uint32_t uiPlugin;
87 // Start clean
88 _pSubsystemLibrary->clean();
89
90 for (uiPlugin = 0; uiPlugin < astrPluginFiles.size(); uiPlugin++) {
91
92 string strPluginFileName = astrPluginFiles[uiPlugin];
93
94 log("Loading subsystem plugin path \"%s\"", strPluginFileName.c_str());
95
96 void* lib_handle = dlopen(strPluginFileName.c_str(), RTLD_LAZY);
97
98 if (!lib_handle) {
99
100 // Return error
101 const char* pcError = dlerror();
102
103 if (pcError) {
104
105 strError = pcError;
106 } else {
107
108 strError = "Unable to load subsystem plugin " + strPluginFileName;
109 }
110
111 _pSubsystemLibrary->clean();
112
113 return false;
114 }
115
116 GetSusbystemBuilder pfnGetSusbystemBuilder = (GetSusbystemBuilder)dlsym(lib_handle, "getSusbystemBuilder");
117
118 if (!pfnGetSusbystemBuilder) {
119
120 strError = "Subsystem plugin " + strPluginFileName + " does not contain getSusbystemBuilder symbol.";
121
122 _pSubsystemLibrary->clean();
123
124 return false;
125 }
126
127 // Fill library
128 pfnGetSusbystemBuilder(_pSubsystemLibrary);
129 }
130
131 return true;
132}
133
134bool CSystemClass::getPluginFiles(const string& strPluginPath, vector<string>& astrPluginFiles) const
135{
136 log("Seeking subsystem plugins from folder \"%s\"", strPluginPath.c_str());
137
138 DIR *dirp;
139 struct dirent *dp;
140
141 if ((dirp = opendir(strPluginPath.c_str())) == NULL) {
142
143 return false;
144 }
145
146 const string strPluginPattern("-subsystem.so");
147
148 // Parse it and load plugins
149 while ((dp = readdir(dirp)) != NULL) {
150
151 string strFileName(dp->d_name);
152
153 // Check file name ends with "-susbsystem.so"
154 size_t uiPatternPos = strFileName.rfind(strPluginPattern, -1);
155
156 if (uiPatternPos != (size_t)-1 && uiPatternPos == strFileName.size() - strPluginPattern.size()) {
157 // Found plugin
158 astrPluginFiles.push_back(strPluginPath + strFileName);
159 }
160 }
161
162 // Close plugin folder
163 closedir(dirp);
164
165 return true;
166}
167
168const CSubsystemLibrary* CSystemClass::getSubsystemLibrary() const
169{
170 return _pSubsystemLibrary;
171}
172
173bool CSystemClass::init(string& strError)
174{
175 return base::init(strError);
176}
177