source: nscp/service/NSCPlugin.cpp @ 7f9c823

0.4.00.4.10.4.2
Last change on this file since 7f9c823 was 7f9c823, checked in by Michael Medin <michael@…>, 4 years ago

First attempt at serious boostification.
NOTICE! This is NOT a complete edition, it build and runs but many features are disabled and/or broken.
But we have working, sockets and mutexes and conversion functions from boost inside now and more to come...
Also started to build with CMake since it works better then boost.build

  • Property mode set to 100644
File size: 13.1 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#include "stdafx.h"
22#include "NSClient++.h"
23#include <error.hpp>
24#include "core_api.h"
25/**
26 * Default c-tor
27 * Initializes the plug in name but does not load the actual plug in.<br>
28 * To load the plug in use function load() that loads an initializes the plug in.
29 *
30 * @param file The file (DLL) to load as a NSC plug in.
31 */
32NSCPlugin::NSCPlugin(const std::wstring file)
33        : module_(file)
34        ,fLoadModule(NULL)
35        ,fGetName(NULL)
36        ,fHasCommandHandler(NULL)
37        ,fUnLoadModule(NULL)
38        ,fHasMessageHandler(NULL)
39        ,fHandleMessage(NULL)
40        ,fGetDescription(NULL)
41        ,fGetConfigurationMeta(NULL)
42        ,fGetVersion(NULL)
43        ,fCommandLineExec(NULL)
44        ,fShowTray(NULL)
45        ,fHideTray(NULL)
46        ,bLoaded_(false)
47        ,lastIsMsgPlugin_(false)
48        ,broken_(false)
49{
50}
51/*
52NSCPlugin::NSCPlugin(NSCPlugin &other)
53        :module_()
54        ,fLoadModule(NULL)
55        ,fGetName(NULL)
56        ,fHasCommandHandler(NULL)
57        ,fUnLoadModule(NULL)
58        ,fHasMessageHandler(NULL)
59        ,fHandleMessage(NULL)
60        ,fGetDescription(NULL)
61        ,fGetConfigurationMeta(NULL)
62        ,fGetVersion(NULL)
63        ,fCommandLineExec(NULL)
64        ,fShowTray(NULL)
65        ,fHideTray(NULL)
66        ,bLoaded_(false)
67        ,lastIsMsgPlugin_(false)
68        ,broken_(false)
69{
70        if (other.bLoaded_) {
71                file_ = other.file_;
72                hModule_ = LoadLibrary(file_.c_str());
73                if (!hModule_)
74                        throw NSPluginException(file_, _T("Could not load library: ") + error::lookup::last_error());
75                loadRemoteProcs_();
76                if (!fLoadModule)
77                        throw NSPluginException(file_, _T("Critical error (fLoadModule)"));
78                bLoaded_ = other.bLoaded_;
79        }
80}
81*/
82/**
83 * Default d-tor
84 */
85NSCPlugin::~NSCPlugin() {
86        if (isLoaded())
87                unload();
88}
89/**
90 * Returns the name of the plug in.
91 *
92 * @return Name of the plug in.
93 *
94 * @throws NSPluginException if the module is not loaded.
95 */
96std::wstring NSCPlugin::getName() {
97        wchar_t *buffer = new wchar_t[1024];
98        if (!getName_(buffer, 1023)) {
99                return _T("Could not get name");
100        }
101        std::wstring ret = buffer;
102        delete [] buffer;
103        return ret;
104}
105std::wstring NSCPlugin::getDescription() {
106        wchar_t *buffer = new wchar_t[4096];
107        if (!getDescription_(buffer, 4095)) {
108                throw NSPluginException(module_, _T("Could not get description"));
109        }
110        std::wstring ret = buffer;
111        delete [] buffer;
112        return ret;
113}
114
115/**
116 * Loads the plug in (DLL) and initializes the plug in by calling NSLoadModule
117 *
118 * @throws NSPluginException when exceptions occur.
119 * Exceptions include but are not limited to: DLL fails to load, DLL is not a correct plug in.
120 */
121void NSCPlugin::load_dll() {
122        if (module_.is_loaded())
123                throw NSPluginException(module_, _T("Module already loaded"));
124        try {
125                module_.load_library();
126        } catch (dll::dll_exception &e) {
127                throw NSPluginException(module_, e.what());
128        }
129        loadRemoteProcs_();
130        bLoaded_ = true;
131}
132
133bool NSCPlugin::load_plugin(NSCAPI::moduleLoadMode mode) {
134        if (!fLoadModule)
135                throw NSPluginException(module_, _T("Critical error (fLoadModule)"));
136        return fLoadModule(mode);
137}
138
139void NSCPlugin::setBroken(bool broken) {
140        broken_ = broken;
141}
142bool NSCPlugin::isBroken() {
143        return broken_;
144}
145
146
147/**
148 * Get the plug in version.
149 *
150 * @bug Not implemented as of yet
151 *
152 * @param *major
153 * @param *minor
154 * @param *revision
155 * @return False
156 */
157bool NSCPlugin::getVersion(int *major, int *minor, int *revision) {
158        if (!isLoaded())
159                throw NSPluginException(module_, _T("Library is not loaded"));
160        if (!fGetVersion)
161                throw NSPluginException(module_, _T("Critical error (fGetVersion)"));
162        try {
163                return fGetVersion(major, minor, revision)?true:false;
164        } catch (...) {
165                throw NSPluginException(module_, _T("Unhandled exception in getVersion."));
166        }
167}
168/**
169 * Returns true if the plug in has a command handler.
170 * @return true if the plug in has a command handler.
171 * @throws NSPluginException if the module is not loaded.
172 */
173bool NSCPlugin::hasCommandHandler() {
174        if (!isLoaded())
175                throw NSPluginException(module_, _T("Module not loaded"));
176        try {
177                if (fHasCommandHandler())
178                        return true;
179                return false;
180        } catch (...) {
181                throw NSPluginException(module_, _T("Unhandled exception in hasCommandHandler."));
182        }
183}
184/**
185* Returns true if the plug in has a message (log) handler.
186* @return true if the plug in has a message (log) handler.
187* @throws NSPluginException if the module is not loaded.
188*/
189bool NSCPlugin::hasMessageHandler() {
190        if (!isLoaded())
191                throw NSPluginException(module_, _T("Module not loaded"));
192        try {
193                if (fHasMessageHandler()) {
194                        lastIsMsgPlugin_ = true;
195                        return true;
196                }
197                return false;
198        } catch (...) {
199                throw NSPluginException(module_, _T("Unhandled exception in hasMessageHandler."));
200        }
201}
202/**
203 * Allow for the plug in to handle a command from the input core.
204 *
205 * Plug ins may refuse to handle the plug in (if not applicable) by returning an empty string.
206 *
207 * @param command The command name (is a string encoded number for legacy commands)
208 * @param argLen The length of the argument buffer.
209 * @param **arguments The arguments for this command
210 * @param returnMessageBuffer Return buffer for plug in to store the result of the executed command.
211 * @param returnMessageBufferLen Size of returnMessageBuffer
212 * @param returnPerfBuffer Return buffer for performance data
213 * @param returnPerfBufferLen Size of returnPerfBuffer
214 * @return Status of execution. Could be error codes, buffer length messages etc.
215 * @throws NSPluginException if the module is not loaded.
216 */
217NSCAPI::nagiosReturn NSCPlugin::handleCommand(const wchar_t* command, const unsigned int argLen, wchar_t **arguments, wchar_t* returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR* returnPerfBuffer, unsigned int returnPerfBufferLen) {
218        if (!isLoaded())
219                throw NSPluginException(module_, _T("Library is not loaded"));
220        try {
221                return fHandleCommand(command, argLen, arguments, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
222        } catch (...) {
223                throw NSPluginException(module_, _T("Unhandled exception in handleCommand."));
224        }
225}
226/**
227 * Handle a message from the core (or any other (or even potentially self) plug in).
228 * A message may be anything really errors, log messages etc.
229 *
230 * @param msgType Type of message (error, warning, debug, etc.)
231 * @param file The file that generated this message generally __FILE__.
232 * @param line The line in the file that generated the message generally __LINE__
233 * @throws NSPluginException if the module is not loaded.
234 */
235void NSCPlugin::handleMessage(int msgType, const wchar_t* file, const int line, const wchar_t *message) {
236        if (!fHandleMessage)
237                throw NSPluginException(module_, _T("Library is not loaded"));
238        try {
239                fHandleMessage(msgType, file, line, message);
240        } catch (...) {
241                throw NSPluginException(module_, _T("Unhandled exception in handleMessage."));
242        }
243}
244/**
245 * Unload the plug in
246 * @throws NSPluginException if the module is not loaded and/or cannot be unloaded (plug in remains loaded if so).
247 */
248void NSCPlugin::unload() {
249        if (!isLoaded())
250                throw NSPluginException(module_, _T("Library is not loaded"));
251        bLoaded_ = false;
252        if (!fUnLoadModule)
253                throw NSPluginException(module_, _T("Critical error (fUnLoadModule)"));
254        try {
255                fUnLoadModule();
256        } catch (...) {
257                throw NSPluginException(module_, _T("Unhandled exception in handleMessage."));
258        }
259        module_.unload_library();
260}
261bool NSCPlugin::getName_(wchar_t* buf, unsigned int buflen) {
262        if (fGetName == NULL)
263                return false;//throw NSPluginException(module_, _T("Critical error (fGetName)"));
264        try {
265                return fGetName(buf, buflen)?true:false;
266        } catch (...) {
267                return false; //throw NSPluginException(module_, _T("Unhandled exception in getName."));
268        }
269}
270bool NSCPlugin::getDescription_(wchar_t* buf, unsigned int buflen) {
271        if (fGetDescription == NULL)
272                throw NSPluginException(module_, _T("Critical error (fGetDescription)"));
273        try {
274                return fGetDescription(buf, buflen)?true:false;
275        } catch (...) {
276                throw NSPluginException(module_, _T("Unhandled exception in getDescription."));
277        }
278}
279
280void NSCPlugin::showTray() {
281        if (fShowTray == NULL)
282                throw NSPluginException(module_, _T("Critical error (ShowTray)"));
283        try {
284                fShowTray();
285        } catch (...) {
286                throw NSPluginException(module_, _T("Unhandled exception in ShowTray."));
287        }
288}
289void NSCPlugin::hideTray() {
290        if (fHideTray == NULL)
291                throw NSPluginException(module_, _T("Critical error (HideTray)"));
292        try {
293                fHideTray();
294        } catch (...) {
295                throw NSPluginException(module_, _T("Unhandled exception in HideTray."));
296        }
297}
298
299/**
300 * Load all remote function pointers from the loaded module.
301 * These pointers are cached for "speed" which might (?) be dangerous if something changes.
302 * @throws NSPluginException if any of the function pointers fail to load.
303 * If NSPluginException  is thrown the loaded might remain partially loaded and crashes might occur if plug in is used in this state.
304 */
305void NSCPlugin::loadRemoteProcs_(void) {
306
307        fLoadModule = (lpLoadModule)module_.load_proc("NSLoadModule");
308        if (!fLoadModule)
309                throw NSPluginException(module_, _T("Could not load NSLoadModule"));
310
311        fModuleHelperInit = (lpModuleHelperInit)module_.load_proc("NSModuleHelperInit");
312        if (!fModuleHelperInit)
313                throw NSPluginException(module_, _T("Could not load NSModuleHelperInit"));
314
315        try {
316                fModuleHelperInit(NSAPILoader);
317        } catch (...) {
318                throw NSPluginException(module_, _T("Unhandled exception in getDescription."));
319        }
320       
321        fGetName = (lpGetName)module_.load_proc("NSGetModuleName");
322        if (!fGetName)
323                throw NSPluginException(module_, _T("Could not load NSGetModuleName"));
324
325        fGetVersion = (lpGetVersion)module_.load_proc("NSGetModuleVersion");
326        if (!fGetVersion)
327                throw NSPluginException(module_, _T("Could not load NSGetModuleVersion"));
328
329        fGetDescription = (lpGetDescription)module_.load_proc("NSGetModuleDescription");
330        if (!fGetDescription)
331                throw NSPluginException(module_, _T("Could not load NSGetModuleDescription"));
332
333        fHasCommandHandler = (lpHasCommandHandler)module_.load_proc("NSHasCommandHandler");
334        if (!fHasCommandHandler)
335                throw NSPluginException(module_, _T("Could not load NSHasCommandHandler"));
336
337        fHasMessageHandler = (lpHasMessageHandler)module_.load_proc("NSHasMessageHandler");
338        if (!fHasMessageHandler)
339                throw NSPluginException(module_, _T("Could not load NSHasMessageHandler"));
340
341        fHandleCommand = (lpHandleCommand)module_.load_proc("NSHandleCommand");
342        if (!fHandleCommand)
343                throw NSPluginException(module_, _T("Could not load NSHandleCommand"));
344
345        fHandleMessage = (lpHandleMessage)module_.load_proc("NSHandleMessage");
346        if (!fHandleMessage)
347                throw NSPluginException(module_, _T("Could not load NSHandleMessage"));
348
349        fUnLoadModule = (lpUnLoadModule)module_.load_proc("NSUnloadModule");
350        if (!fUnLoadModule)
351                throw NSPluginException(module_, _T("Could not load NSUnloadModule"));
352
353        fGetConfigurationMeta = (lpGetConfigurationMeta)module_.load_proc("NSGetConfigurationMeta");
354        fCommandLineExec = (lpCommandLineExec)module_.load_proc("NSCommandLineExec");
355
356        fShowTray = (lpShowTray)module_.load_proc("ShowIcon");
357        fHideTray = (lpHideTray)module_.load_proc("HideIcon");
358
359}
360
361
362std::wstring NSCPlugin::getCongifurationMeta()
363{
364        wchar_t *buffer = new wchar_t[4097];
365        if (!getConfigurationMeta_(buffer, 4096)) {
366                throw NSPluginException(module_, _T("Could not get metadata"));
367        }
368        std::wstring ret = buffer;
369        delete [] buffer;
370        return ret;
371}
372bool NSCPlugin::getConfigurationMeta_(wchar_t* buf, unsigned int buflen) {
373        if (fGetConfigurationMeta == NULL)
374                throw NSPluginException(module_, _T("Critical error (getCongifurationMeta)"));
375        try {
376                return fGetConfigurationMeta(buflen, buf)?true:false;
377        } catch (...) {
378                throw NSPluginException(module_, _T("Unhandled exception in getConfigurationMeta."));
379        }
380}
381
382int NSCPlugin::commandLineExec(const wchar_t* command, const unsigned int argLen, wchar_t **arguments) {
383        if (fCommandLineExec== NULL)
384                throw NSPluginException(module_, _T("Module does not support CommandLineExec"));
385        try {
386                return fCommandLineExec(command, argLen, arguments);
387        } catch (...) {
388                throw NSPluginException(module_, _T("Unhandled exception in commandLineExec."));
389        }
390}
Note: See TracBrowser for help on using the repository browser.