source: nscp/NSCPlugin.cpp @ a0e17ff

0.4.00.4.10.4.2stable
Last change on this file since a0e17ff was a34b229, checked in by Michael Medin <michael@…>, 5 years ago

2008-09-09 MickeM

  • Fixed issue with & and some commands via check_nt.
  • Fixed a crash on exit (which I added in Rc1).
  • Added 10 "bytes" the CPU buffer: (#174) + Added new option to [EventLog?] section buffer_size to change the size of the buffer used when scanning the evenlotg (defaults to 64k).
  • Fixed error handling in CHeckEventLog so errors are repoorted properly (#184)
  • Property mode set to 100644
File size: 13.0 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/**
25 * Default c-tor
26 * Initializes the plug in name but does not load the actual plug in.<br>
27 * To load the plug in use function load() that loads an initializes the plug in.
28 *
29 * @param file The file (DLL) to load as a NSC plug in.
30 */
31NSCPlugin::NSCPlugin(const std::wstring file)
32        : file_(file)
33        ,hModule_(NULL)
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{
49}
50
51NSCPlugin::NSCPlugin(NSCPlugin &other)
52        :hModule_(NULL)
53        ,fLoadModule(NULL)
54        ,fGetName(NULL)
55        ,fHasCommandHandler(NULL)
56        ,fUnLoadModule(NULL)
57        ,fHasMessageHandler(NULL)
58        ,fHandleMessage(NULL)
59        ,fGetDescription(NULL)
60        ,fGetConfigurationMeta(NULL)
61        ,fGetVersion(NULL)
62        ,fCommandLineExec(NULL)
63        ,fShowTray(NULL)
64        ,fHideTray(NULL)
65        ,bLoaded_(false)
66        ,lastIsMsgPlugin_(false)
67{
68        if (other.bLoaded_) {
69                file_ = other.file_;
70                hModule_ = LoadLibrary(file_.c_str());
71                if (!hModule_)
72                        throw NSPluginException(file_, _T("Could not load library: ") + error::lookup::last_error());
73                loadRemoteProcs_();
74                if (!fLoadModule)
75                        throw NSPluginException(file_, _T("Critical error (fLoadModule)"));
76                bLoaded_ = other.bLoaded_;
77        }
78}
79
80/**
81 * Default d-tor
82 */
83NSCPlugin::~NSCPlugin() {
84        if (isLoaded())
85                unload();
86}
87/**
88 * Returns the name of the plug in.
89 *
90 * @return Name of the plug in.
91 *
92 * @throws NSPluginException if the module is not loaded.
93 */
94std::wstring NSCPlugin::getName() {
95        TCHAR *buffer = new TCHAR[1024];
96        if (!getName_(buffer, 1023)) {
97                return _T("Could not get name");
98        }
99        std::wstring ret = buffer;
100        delete [] buffer;
101        return ret;
102}
103std::wstring NSCPlugin::getDescription() {
104        TCHAR *buffer = new TCHAR[4096];
105        if (!getDescription_(buffer, 4095)) {
106                throw NSPluginException(file_, _T("Could not get description"));
107        }
108        std::wstring ret = buffer;
109        delete [] buffer;
110        return ret;
111}
112
113/**
114 * Loads the plug in (DLL) and initializes the plug in by calling NSLoadModule
115 *
116 * @throws NSPluginException when exceptions occur.
117 * Exceptions include but are not limited to: DLL fails to load, DLL is not a correct plug in.
118 */
119void NSCPlugin::load_dll() {
120        if (isLoaded())
121                throw NSPluginException(file_, _T("Module already loaded"));
122        hModule_ = LoadLibrary(file_.c_str());
123        if (!hModule_)
124                throw NSPluginException(file_, _T("Could not load library: ") + error::lookup::last_error());
125        loadRemoteProcs_();
126        bLoaded_ = true;
127}
128
129void NSCPlugin::load_plugin() {
130        if (!fLoadModule)
131                throw NSPluginException(file_, _T("Critical error (fLoadModule)"));
132        if (!fLoadModule())
133                throw NSPluginException(file_, _T("Could not load plug in"));
134}
135
136
137/**
138 * Get the plug in version.
139 *
140 * @bug Not implemented as of yet
141 *
142 * @param *major
143 * @param *minor
144 * @param *revision
145 * @return False
146 */
147bool NSCPlugin::getVersion(int *major, int *minor, int *revision) {
148        if (!isLoaded())
149                throw NSPluginException(file_, _T("Library is not loaded"));
150        if (!fGetVersion)
151                throw NSPluginException(file_, _T("Critical error (fGetVersion)"));
152        try {
153                return fGetVersion(major, minor, revision)?true:false;
154        } catch (...) {
155                throw NSPluginException(file_, _T("Unhandled exception in getVersion."));
156        }
157}
158/**
159 * Returns true if the plug in has a command handler.
160 * @return true if the plug in has a command handler.
161 * @throws NSPluginException if the module is not loaded.
162 */
163bool NSCPlugin::hasCommandHandler() {
164        if (!isLoaded())
165                throw NSPluginException(file_, _T("Module not loaded"));
166        try {
167                if (fHasCommandHandler())
168                        return true;
169                return false;
170        } catch (...) {
171                throw NSPluginException(file_, _T("Unhandled exception in hasCommandHandler."));
172        }
173}
174/**
175* Returns true if the plug in has a message (log) handler.
176* @return true if the plug in has a message (log) handler.
177* @throws NSPluginException if the module is not loaded.
178*/
179bool NSCPlugin::hasMessageHandler() {
180        if (!isLoaded())
181                throw NSPluginException(file_, _T("Module not loaded"));
182        try {
183                if (fHasMessageHandler()) {
184                        lastIsMsgPlugin_ = true;
185                        return true;
186                }
187                return false;
188        } catch (...) {
189                throw NSPluginException(file_, _T("Unhandled exception in hasMessageHandler."));
190        }
191}
192/**
193 * Allow for the plug in to handle a command from the input core.
194 *
195 * Plug ins may refuse to handle the plug in (if not applicable) by returning an empty string.
196 *
197 * @param command The command name (is a string encoded number for legacy commands)
198 * @param argLen The length of the argument buffer.
199 * @param **arguments The arguments for this command
200 * @param returnMessageBuffer Return buffer for plug in to store the result of the executed command.
201 * @param returnMessageBufferLen Size of returnMessageBuffer
202 * @param returnPerfBuffer Return buffer for performance data
203 * @param returnPerfBufferLen Size of returnPerfBuffer
204 * @return Status of execution. Could be error codes, buffer length messages etc.
205 * @throws NSPluginException if the module is not loaded.
206 */
207NSCAPI::nagiosReturn NSCPlugin::handleCommand(const TCHAR* command, const unsigned int argLen, TCHAR **arguments, TCHAR* returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR* returnPerfBuffer, unsigned int returnPerfBufferLen) {
208        if (!isLoaded())
209                throw NSPluginException(file_, _T("Library is not loaded"));
210        try {
211                return fHandleCommand(command, argLen, arguments, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
212        } catch (...) {
213                throw NSPluginException(file_, _T("Unhandled exception in handleCommand."));
214        }
215}
216/**
217 * Handle a message from the core (or any other (or even potentially self) plug in).
218 * A message may be anything really errors, log messages etc.
219 *
220 * @param msgType Type of message (error, warning, debug, etc.)
221 * @param file The file that generated this message generally __FILE__.
222 * @param line The line in the file that generated the message generally __LINE__
223 * @throws NSPluginException if the module is not loaded.
224 */
225void NSCPlugin::handleMessage(int msgType, const TCHAR* file, const int line, const TCHAR *message) {
226        if (!fHandleMessage)
227                throw NSPluginException(file_, _T("Library is not loaded"));
228        try {
229                fHandleMessage(msgType, file, line, message);
230        } catch (...) {
231                throw NSPluginException(file_, _T("Unhandled exception in handleMessage."));
232        }
233}
234/**
235 * Unload the plug in
236 * @throws NSPluginException if the module is not loaded and/or cannot be unloaded (plug in remains loaded if so).
237 */
238void NSCPlugin::unload() {
239        if (!isLoaded())
240                throw NSPluginException(file_, _T("Library is not loaded"));
241        if (!fUnLoadModule)
242                throw NSPluginException(file_, _T("Critical error (fUnLoadModule)"));
243        try {
244                fUnLoadModule();
245        } catch (...) {
246                throw NSPluginException(file_, _T("Unhandled exception in handleMessage."));
247        }
248        FreeLibrary(hModule_);
249        hModule_ = NULL;
250        bLoaded_ = false;
251}
252bool NSCPlugin::getName_(TCHAR* buf, unsigned int buflen) {
253        if (fGetName == NULL)
254                return false;//throw NSPluginException(file_, _T("Critical error (fGetName)"));
255        try {
256                return fGetName(buf, buflen)?true:false;
257        } catch (...) {
258                return false; //throw NSPluginException(file_, _T("Unhandled exception in getName."));
259        }
260}
261bool NSCPlugin::getDescription_(TCHAR* buf, unsigned int buflen) {
262        if (fGetDescription == NULL)
263                throw NSPluginException(file_, _T("Critical error (fGetDescription)"));
264        try {
265                return fGetDescription(buf, buflen)?true:false;
266        } catch (...) {
267                throw NSPluginException(file_, _T("Unhandled exception in getDescription."));
268        }
269}
270
271void NSCPlugin::showTray() {
272        if (fShowTray == NULL)
273                throw NSPluginException(file_, _T("Critical error (ShowTray)"));
274        try {
275                fShowTray();
276        } catch (...) {
277                throw NSPluginException(file_, _T("Unhandled exception in ShowTray."));
278        }
279}
280void NSCPlugin::hideTray() {
281        if (fHideTray == NULL)
282                throw NSPluginException(file_, _T("Critical error (HideTray)"));
283        try {
284                fHideTray();
285        } catch (...) {
286                throw NSPluginException(file_, _T("Unhandled exception in HideTray."));
287        }
288}
289
290/**
291 * Load all remote function pointers from the loaded module.
292 * These pointers are cached for "speed" which might (?) be dangerous if something changes.
293 * @throws NSPluginException if any of the function pointers fail to load.
294 * If NSPluginException  is thrown the loaded might remain partially loaded and crashes might occur if plug in is used in this state.
295 */
296void NSCPlugin::loadRemoteProcs_(void) {
297
298        fLoadModule = (lpLoadModule)GetProcAddress(hModule_, "NSLoadModule");
299        if (!fLoadModule)
300                throw NSPluginException(file_, _T("Could not load NSLoadModule"));
301
302        fModuleHelperInit = (lpModuleHelperInit)GetProcAddress(hModule_, "NSModuleHelperInit");
303        if (!fModuleHelperInit)
304                throw NSPluginException(file_, _T("Could not load NSModuleHelperInit"));
305
306        try {
307                fModuleHelperInit(NSAPILoader);
308        } catch (...) {
309                throw NSPluginException(file_, _T("Unhandled exception in getDescription."));
310        }
311       
312        fGetName = (lpGetName)GetProcAddress(hModule_, "NSGetModuleName");
313        if (!fGetName)
314                throw NSPluginException(file_, _T("Could not load NSGetModuleName"));
315
316        fGetVersion = (lpGetVersion)GetProcAddress(hModule_, "NSGetModuleVersion");
317        if (!fGetVersion)
318                throw NSPluginException(file_, _T("Could not load NSGetModuleVersion"));
319
320        fGetDescription = (lpGetDescription)GetProcAddress(hModule_, "NSGetModuleDescription");
321        if (!fGetDescription)
322                throw NSPluginException(file_, _T("Could not load NSGetModuleDescription"));
323
324        fHasCommandHandler = (lpHasCommandHandler)GetProcAddress(hModule_, "NSHasCommandHandler");
325        if (!fHasCommandHandler)
326                throw NSPluginException(file_, _T("Could not load NSHasCommandHandler"));
327
328        fHasMessageHandler = (lpHasMessageHandler)GetProcAddress(hModule_, "NSHasMessageHandler");
329        if (!fHasMessageHandler)
330                throw NSPluginException(file_, _T("Could not load NSHasMessageHandler"));
331
332        fHandleCommand = (lpHandleCommand)GetProcAddress(hModule_, "NSHandleCommand");
333        if (!fHandleCommand)
334                throw NSPluginException(file_, _T("Could not load NSHandleCommand"));
335
336        fHandleMessage = (lpHandleMessage)GetProcAddress(hModule_, "NSHandleMessage");
337        if (!fHandleMessage)
338                throw NSPluginException(file_, _T("Could not load NSHandleMessage"));
339
340        fUnLoadModule = (lpUnLoadModule)GetProcAddress(hModule_, "NSUnloadModule");
341        if (!fUnLoadModule)
342                throw NSPluginException(file_, _T("Could not load NSUnloadModule"));
343
344        fGetConfigurationMeta = (lpGetConfigurationMeta)GetProcAddress(hModule_, "NSGetConfigurationMeta");
345        fCommandLineExec = (lpCommandLineExec)GetProcAddress(hModule_, "NSCommandLineExec");
346
347        fShowTray = (lpShowTray)GetProcAddress(hModule_, "ShowIcon");
348        fHideTray = (lpHideTray)GetProcAddress(hModule_, "HideIcon");
349
350}
351
352
353std::wstring NSCPlugin::getCongifurationMeta()
354{
355        TCHAR *buffer = new TCHAR[4097];
356        if (!getConfigurationMeta_(buffer, 4096)) {
357                throw NSPluginException(file_, _T("Could not get metadata"));
358        }
359        std::wstring ret = buffer;
360        delete [] buffer;
361        return ret;
362}
363bool NSCPlugin::getConfigurationMeta_(TCHAR* buf, unsigned int buflen) {
364        if (fGetConfigurationMeta == NULL)
365                throw NSPluginException(file_, _T("Critical error (getCongifurationMeta)"));
366        try {
367                return fGetConfigurationMeta(buflen, buf)?true:false;
368        } catch (...) {
369                throw NSPluginException(file_, _T("Unhandled exception in getConfigurationMeta."));
370        }
371}
372
373int NSCPlugin::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR **arguments) {
374        if (fCommandLineExec== NULL)
375                throw NSPluginException(file_, _T("Module does not support CommandLineExec"));
376        try {
377                return fCommandLineExec(command, argLen, arguments);
378        } catch (...) {
379                throw NSPluginException(file_, _T("Unhandled exception in commandLineExec."));
380        }
381}
Note: See TracBrowser for help on using the repository browser.