summaryrefslogtreecommitdiffstats
path: root/src/lib/hooks/library_manager.cc
blob: 447c9ebb883975df7cce5d10d27220547a8730b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (C) 2013-2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <exceptions/exceptions.h>
#include <hooks/hooks.h>
#include <hooks/hooks_log.h>
#include <hooks/callout_manager.h>
#include <hooks/library_handle.h>
#include <hooks/library_manager.h>
#include <hooks/pointer_converter.h>
#include <hooks/server_hooks.h>
#include <log/logger_manager.h>
#include <log/logger_support.h>
#include <log/message_initializer.h>
#include <util/multi_threading_mgr.h>

#include <string>
#include <vector>

#include <dlfcn.h>

using namespace std;

namespace isc {
namespace hooks {

// Constructor (used by external agency)
LibraryManager::LibraryManager(const std::string& name, int index,
                               const boost::shared_ptr<CalloutManager>& manager)
        : dl_handle_(NULL), index_(index), manager_(manager),
          library_name_(name),
          server_hooks_(ServerHooks::getServerHooksPtr())
{
    if (!manager) {
        isc_throw(NoCalloutManager, "must specify a CalloutManager when "
                  "instantiating a LibraryManager object");
    }
}

// Constructor (used by "validate" for library validation).  Note that this
// sets "manager_" to not point to anything, which means that methods such as
// registerStandardCallout() will fail, probably with a segmentation fault.
// There are no checks for this condition in those methods: this constructor
// is declared "private", so can only be executed by a method in this class.
// The only method to do so is "validateLibrary", which takes care not to call
// methods requiring a non-NULL manager.
LibraryManager::LibraryManager(const std::string& name)
        : dl_handle_(NULL), index_(-1), manager_(), library_name_(name)
{}

// Destructor.
LibraryManager::~LibraryManager() {
    if (index_ >= 0) {
        // LibraryManager instantiated to load a library, so ensure that
        // it is unloaded before exiting.
        static_cast<void>(prepareUnloadLibrary());
    }

    // LibraryManager instantiated to validate a library, so just ensure
    // that it is closed before exiting.
    static_cast<void>(closeLibrary());
}

// Open the library

bool
LibraryManager::openLibrary() {

    // Open the library.  We'll resolve names now, so that if there are any
    // issues we don't bugcheck in the middle of apparently unrelated code.
    dl_handle_ = dlopen(library_name_.c_str(), RTLD_NOW | RTLD_LOCAL);
    if (dl_handle_ == NULL) {
        LOG_ERROR(hooks_logger, HOOKS_OPEN_ERROR).arg(library_name_)
                  .arg(dlerror());
    }

    return (dl_handle_ != NULL);
}

// Close the library if not already open

bool
LibraryManager::closeLibrary() {

    // Close the library if it is open. (If not, this is a no-op.)
    int status = 0;
    if (dl_handle_ != NULL) {
        status = dlclose(dl_handle_);
        dl_handle_ = NULL;
        if (status != 0) {
            LOG_ERROR(hooks_logger, HOOKS_CLOSE_ERROR).arg(library_name_)
                      .arg(dlerror());
        } else {
            LOG_INFO(hooks_logger, HOOKS_LIBRARY_CLOSED).arg(library_name_);
        }
    }

    return (status == 0);
}

// Check the version of the library

bool
LibraryManager::checkVersion() const {

    // Get the pointer to the "version" function.
    PointerConverter pc(dlsym(dl_handle_, VERSION_FUNCTION_NAME));
    if (pc.versionPtr() != NULL) {
        int version = KEA_HOOKS_VERSION - 1; // This is an invalid value
        try {
            version = (*pc.versionPtr())();
        } catch (...) {
            LOG_ERROR(hooks_logger, HOOKS_VERSION_EXCEPTION).arg(library_name_);
            return (false);
        }

        if (version == KEA_HOOKS_VERSION) {
            // All OK, version checks out
            LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS, HOOKS_LIBRARY_VERSION)
                      .arg(library_name_).arg(version);
            return (true);

        } else {
            LOG_ERROR(hooks_logger, HOOKS_INCORRECT_VERSION).arg(library_name_)
                      .arg(version).arg(KEA_HOOKS_VERSION);
        }
    } else {
        LOG_ERROR(hooks_logger, HOOKS_NO_VERSION).arg(library_name_);
    }

    return (false);
}

// Check the multi-threading compatibility of the library

bool
LibraryManager::checkMultiThreadingCompatible() const {

    // Compatible with single-threaded.
    if (!util::MultiThreadingMgr::instance().getMode()) {
        return (true);
    }

    // Get the pointer to the "multi_threading_compatible" function.
    PointerConverter pc(dlsym(dl_handle_, MULTI_THREADING_COMPATIBLE_FUNCTION_NAME));
    int compatible = 0;
    if (pc.multiThreadingCompatiblePtr()) {
        try {
            compatible = (*pc.multiThreadingCompatiblePtr())();
        } catch (...) {
            LOG_ERROR(hooks_logger, HOOKS_MULTI_THREADING_COMPATIBLE_EXCEPTION)
                .arg(library_name_);
            return (false);
        }

        LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS,
                  HOOKS_LIBRARY_MULTI_THREADING_COMPATIBLE)
            .arg(library_name_)
            .arg(compatible);
    }
    if (compatible == 0) {
        LOG_ERROR(hooks_logger, HOOKS_LIBRARY_MULTI_THREADING_NOT_COMPATIBLE)
            .arg(library_name_);
    }
    return (compatible != 0);
}

// Register the standard callouts

void
LibraryManager::registerStandardCallouts() {
    // Set the library index for doing the registration.  This is picked up
    // when the library handle is created.
    manager_->setLibraryIndex(index_);

    // Iterate through the list of known hooks
    vector<string> hook_names = ServerHooks::getServerHooks().getHookNames();
    for (size_t i = 0; i < hook_names.size(); ++i) {

        // Look up the symbol
        void* dlsym_ptr = dlsym(dl_handle_, hook_names[i].c_str());
        PointerConverter pc(dlsym_ptr);
        if (pc.calloutPtr() != NULL) {
            // Found a symbol, so register it.
            manager_->getLibraryHandle().registerCallout(hook_names[i],
                                                         pc.calloutPtr());
            LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS,
                      HOOKS_STD_CALLOUT_REGISTERED).arg(library_name_)
                      .arg(hook_names[i]).arg(dlsym_ptr);

        }
    }
}

// Run the "load" function if present.

bool
LibraryManager::runLoad() {

    // Get the pointer to the "load" function.
    PointerConverter pc(dlsym(dl_handle_, LOAD_FUNCTION_NAME));
    if (pc.loadPtr() != NULL) {

        // Call the load() function with the library handle.  We need to set
        // the CalloutManager's index appropriately.  We'll invalidate it
        // afterwards.

        int status = -1;
        try {
            manager_->setLibraryIndex(index_);
            status = (*pc.loadPtr())(manager_->getLibraryHandle());
        } catch (const isc::Exception& ex) {
            LOG_ERROR(hooks_logger, HOOKS_LOAD_FRAMEWORK_EXCEPTION)
                .arg(library_name_).arg(ex.what());
            return (false);
        } catch (...) {
            LOG_ERROR(hooks_logger, HOOKS_LOAD_EXCEPTION).arg(library_name_);
            return (false);
        }

        if (status != 0) {
            LOG_ERROR(hooks_logger, HOOKS_LOAD_ERROR).arg(library_name_)
                      .arg(status);
            return (false);
        } else {
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_LOAD_SUCCESS)
            .arg(library_name_);
        }

    } else {
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_NO_LOAD)
            .arg(library_name_);
    }

    return (true);
}


// Run the "unload" function if present.

bool
LibraryManager::prepareUnloadLibrary() {

    // Nothing to do.
    if (dl_handle_ == NULL) {
        return (true);
    }

    // Call once.
    if (index_ < 0) {
        return (true);
    }

    // Get the pointer to the "load" function.
    bool result = false;
    PointerConverter pc(dlsym(dl_handle_, UNLOAD_FUNCTION_NAME));
    if (pc.unloadPtr() != NULL) {

        // Call the load() function with the library handle.  We need to set
        // the CalloutManager's index appropriately.  We'll invalidate it
        // afterwards.
        int status = -1;
        try {
            status = (*pc.unloadPtr())();
            result = true;
        } catch (const isc::Exception& ex) {
            LOG_ERROR(hooks_logger, HOOKS_UNLOAD_FRAMEWORK_EXCEPTION)
                .arg(library_name_).arg(ex.what());
        } catch (...) {
            // Exception generated.  Note a warning as the unload will occur
            // anyway.
            LOG_WARN(hooks_logger, HOOKS_UNLOAD_EXCEPTION).arg(library_name_);
        }

        if (result) {
            if (status != 0) {
                LOG_ERROR(hooks_logger, HOOKS_UNLOAD_ERROR).arg(library_name_)
                    .arg(status);
                result = false;
            } else {
                LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_UNLOAD_SUCCESS)
                    .arg(library_name_);
            }
        }
    } else {
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_NO_UNLOAD)
            .arg(library_name_);
        result = true;
    }

    // Regardless of status, remove all callouts associated with this
    // library on all hooks.
    vector<string> hooks = ServerHooks::getServerHooks().getHookNames();
    manager_->setLibraryIndex(index_);
    for (size_t i = 0; i < hooks.size(); ++i) {
        bool removed = manager_->deregisterAllCallouts(hooks[i], index_);
        if (removed) {
            LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS, HOOKS_CALLOUTS_REMOVED)
                .arg(hooks[i]).arg(library_name_);
        }
    }

    // Mark as unload() ran.
    index_ = -1;

    return (result);
}

// The main library loading function.

bool
LibraryManager::loadLibrary() {
    LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_LIBRARY_LOADING)
        .arg(library_name_);

    // In the following, if a method such as openLibrary() fails, it will
    // have issued an error message so there is no need to issue another one
    // here.

    // Open the library (which is a check that it exists and is accessible).
    if (openLibrary()) {

        // The hook libraries provide their own log messages and logger
        // instances. This step is required to register log messages for
        // the library being loaded in the global dictionary. Ideally, this
        // should be called after all libraries have been loaded but we're
        // going to call the version() and load() functions here and these
        // functions may already contain logging statements.
        isc::log::MessageInitializer::loadDictionary();

        // The log messages registered by the new hook library may duplicate
        // some of the existing messages. Log warning for each duplicated
        // message now.
        isc::log::LoggerManager::logDuplicatedMessages();

        // Library opened OK, see if a version function is present and if so,
        // check what value it returns. Check multi-threading compatibility.
        if (checkVersion() && checkMultiThreadingCompatible()) {
            // Version OK, so now register the standard callouts and call the
            // library's load() function if present.
            registerStandardCallouts();
            if (runLoad()) {

                // Success - the library has been successfully loaded.
                LOG_INFO(hooks_logger, HOOKS_LIBRARY_LOADED).arg(library_name_);
                return (true);

            } else {

                // The load function failed, so back out.  We can't just close
                // the library as (a) we need to call the library's "unload"
                // function (if present) in case "load" allocated resources that
                // need to be freed and (b) we need to remove any callouts that
                // have been installed.
                static_cast<void>(prepareUnloadLibrary());
            }
        }

        // Either the version check or call to load() failed, so close the
        // library and free up resources.  Ignore the status return here - we
        // already know there's an error and will have output a message.
        static_cast<void>(closeLibrary());
    }

    return (false);
}

// The library unloading function.  Call the unload() function (if present),
// remove callouts from the callout manager, then close the library.  This is
// only run if the library is still loaded and is a no-op if the library is
// not open.

bool
LibraryManager::unloadLibrary() {
    bool result = true;
    if (dl_handle_ != NULL) {
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_LIBRARY_UNLOADING)
            .arg(library_name_);

        // Call the unload() function if present.  Note that this is done first
        // - operations take place in the reverse order to which they were done
        // when the library was loaded.
        if (index_ >= 0) {
            result = prepareUnloadLibrary();
        }

        // ... and close the library.
        result = closeLibrary() && result;
        if (result) {

            // Issue the informational message only if the library was unloaded
            // with no problems.  If there was an issue, an error message would
            // have been issued.
            LOG_INFO(hooks_logger, HOOKS_LIBRARY_UNLOADED).arg(library_name_);
        }
    }
    return (result);
}

// Validate the library.  We must be able to open it, and the version function
// must both exist and return the right number.  Note that this is a static
// method.

bool
LibraryManager::validateLibrary(const std::string& name) {
    // Instantiate a library manager for the validation.  We use the private
    // constructor as we don't supply a CalloutManager.
    LibraryManager manager(name);

    // Try to open it and, if we succeed, check the version.
    bool validated = manager.openLibrary() && manager.checkVersion() &&
        manager.checkMultiThreadingCompatible();

    // Regardless of whether the version checked out, close the library. (This
    // is a no-op if the library failed to open.)
    static_cast<void>(manager.closeLibrary());

    return (validated);
}

// @note Moved from its own hooks.cc file to avoid undefined reference
// with static link.
void hooksStaticLinkInit() {
    if (!isc::log::isLoggingInitialized()) {
        isc::log::initLogger(std::string("userlib"));
    }
}

} // namespace hooks
} // namespace isc