libyui  3.9.3
YUIPlugin.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YUIPlugin.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <dlfcn.h>
27 
28 #define YUILogComponent "ui"
29 #include "YUILog.h"
30 
31 #include "YUIPlugin.h"
32 #include "YPath.h"
33 
34 #include "Libyui_config.h"
35 
36 using std::string;
37 
38 
39 
40 YUIPlugin::YUIPlugin( const char * pluginLibBaseName )
41 {
42  _pluginLibBaseName = string( pluginLibBaseName );
43 
44  string pluginFilename = pluginLibFullPath();
45 
46  _pluginLibHandle = dlopen( pluginFilename.c_str(),
47  RTLD_NOW | RTLD_GLOBAL);
48 
49  if ( ! _pluginLibHandle )
50  {
51  _errorMsg = dlerror();
52 
53  yuiError() << "Could not load UI plugin \"" << pluginLibBaseName
54  << "\": " << _errorMsg
55  << endl;
56  }
57 }
58 
59 
61 {
62  // This intentionally does NOT call unload(): This would be
63  // counterproductive for almost all use cases of this class.
64 }
65 
66 
67 void
69 {
70  if ( _pluginLibHandle )
71  dlclose( _pluginLibHandle );
72 }
73 
74 
75 string
77 {
78 
79  string pluginName = PLUGIN_PREFIX;
80  pluginName.append( _pluginLibBaseName );
81  pluginName.append( PLUGIN_SUFFIX );
82 
83  YPath plugin( PLUGINDIR, pluginName );
84 
85  return plugin.path();
86 }
87 
88 
89 void * YUIPlugin::locateSymbol( const char * symbol )
90 {
91  if ( ! _pluginLibHandle )
92  return 0;
93 
94  void * addr = dlsym( _pluginLibHandle, symbol );
95 
96  if ( ! addr )
97  {
98  yuiError() << "Could not locate symbol \"" << symbol
99  << "\" in " << pluginLibFullPath()
100  << endl;
101  }
102 
103  return addr;
104 }
105 
106 
107 bool YUIPlugin::error() const
108 {
109  return _pluginLibHandle == 0;
110 }
111 
112 
113 bool YUIPlugin::success() const
114 {
115  return _pluginLibHandle != 0;
116 }
117 
118 
119 string YUIPlugin::errorMsg() const
120 {
121  return _errorMsg;
122 }
std::string path()
Returns the full path of the file if found; if not found just the filename given in constructor...
Definition: YPath.cc:187
virtual ~YUIPlugin()
Destructor.
Definition: YUIPlugin.cc:60
std::string pluginLibFullPath() const
Returns the full path of the plugin library.
Definition: YUIPlugin.cc:76
YUIPlugin(const char *pluginLibBaseName)
Constructor: Load the specified plugin library from the standard UI plugin directory (/usr/lib/yui/)...
Definition: YUIPlugin.cc:40
void * locateSymbol(const char *symbol)
Try to locate the specified symbol (function or global variable) in the plugin library.
Definition: YUIPlugin.cc:89
Finds files (e.g.
Definition: YPath.h:43
bool success() const
Returns &#39;true&#39; if there was no error loading the plugin.
Definition: YUIPlugin.cc:113
std::string pluginLibBaseName() const
Returns the base name of the plugin library.
Definition: YUIPlugin.h:96
bool error() const
Returns &#39;true&#39; if there was an error loading the plugin.
Definition: YUIPlugin.cc:107
void unload()
Unload this plugin.
Definition: YUIPlugin.cc:68
std::string errorMsg() const
Returns a human readable (but in most cases untranslated) error message if there was an error...
Definition: YUIPlugin.cc:119