library_manager.cc 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15
#include <exceptions/exceptions.h>
16 17 18 19 20
#include <hooks/hooks.h>
#include <hooks/hooks_log.h>
#include <hooks/callout_manager.h>
#include <hooks/library_handle.h>
#include <hooks/library_manager.h>
21
#include <hooks/pointer_converter.h>
22 23 24 25 26 27 28 29 30 31 32 33
#include <hooks/server_hooks.h>

#include <string>
#include <vector>

#include <dlfcn.h>

using namespace std;

namespace isc {
namespace hooks {

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
// 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)
{
    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 (manager_) {
        // LibraryManager instantiated to load a library, so ensure that
        // it is unloaded before exiting.
        static_cast<void>(unloadLibrary());
    } else {
        // LibraryManager instantiated to validate a library, so just ensure
        // that it is closed before exiting.
        static_cast<void>(closeLibrary());
    }
}

71 72 73 74 75 76 77
// 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.
78
    dl_handle_ = dlopen(library_name_.c_str(), RTLD_NOW | RTLD_LOCAL);
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
    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());
        }
    }

    return (status == 0);
}

// Check the version of the library

bool
LibraryManager::checkVersion() const {

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

122
        if (version == KEA_HOOKS_VERSION) {
123
            // All OK, version checks out
124 125
            LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS, HOOKS_LIBRARY_VERSION)
                      .arg(library_name_).arg(version);
126 127 128 129
            return (true);

        } else {
            LOG_ERROR(hooks_logger, HOOKS_INCORRECT_VERSION).arg(library_name_)
130
                      .arg(version).arg(KEA_HOOKS_VERSION);
131 132 133 134 135 136 137 138 139 140 141 142
        }
    } else {
        LOG_ERROR(hooks_logger, HOOKS_NO_VERSION).arg(library_name_);
    }

    return (false);
}

// Register the standard callouts

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

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

        // Look up the symbol
152 153 154
        void* dlsym_ptr = dlsym(dl_handle_, hook_names[i].c_str());
        PointerConverter pc(dlsym_ptr);
        if (pc.calloutPtr() != NULL) {
155
            // Found a symbol, so register it.
156 157
            manager_->getLibraryHandle().registerCallout(hook_names[i],
                                                         pc.calloutPtr());
158 159 160
            LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS,
                      HOOKS_STD_CALLOUT_REGISTERED).arg(library_name_)
                      .arg(hook_names[i]).arg(dlsym_ptr);
161 162 163 164 165 166 167 168 169 170 171

        }
    }
}

// Run the "load" function if present.

bool
LibraryManager::runLoad() {

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

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

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

192 193 194 195 196
        if (status != 0) {
            LOG_ERROR(hooks_logger, HOOKS_LOAD_ERROR).arg(library_name_)
                      .arg(status);
            return (false);
        } else {
197
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_LOAD_SUCCESS)
198 199
            .arg(library_name_);
        }
200

201 202 203 204 205 206 207 208 209
    } else {
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_NO_LOAD)
            .arg(library_name_);
    }

    return (true);
}


210 211 212 213 214 215
// Run the "unload" function if present.

bool
LibraryManager::runUnload() {

    // Get the pointer to the "load" function.
216 217
    PointerConverter pc(dlsym(dl_handle_, UNLOAD_FUNCTION_NAME));
    if (pc.unloadPtr() != NULL) {
218 219 220 221

        // Call the load() function with the library handle.  We need to set
        // the CalloutManager's index appropriately.  We'll invalidate it
        // afterwards.
222 223 224
        int status = -1;
        try {
            status = (*pc.unloadPtr())();
225 226 227 228
        } catch (const isc::Exception& ex) {
            LOG_ERROR(hooks_logger, HOOKS_UNLOAD_FRAMEWORK_EXCEPTION)
                .arg(library_name_).arg(ex.what());
            return (false);
229 230 231 232 233 234 235
        } catch (...) {
            // Exception generated.  Note a warning as the unload will occur
            // anyway.
            LOG_WARN(hooks_logger, HOOKS_UNLOAD_EXCEPTION).arg(library_name_);
            return (false);
        }

236 237 238 239 240
        if (status != 0) {
            LOG_ERROR(hooks_logger, HOOKS_UNLOAD_ERROR).arg(library_name_)
                      .arg(status);
            return (false);
        } else {
241
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_UNLOAD_SUCCESS)
242 243 244 245 246 247 248 249 250 251
            .arg(library_name_);
        }
    } else {
        LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_NO_UNLOAD)
            .arg(library_name_);
    }

    return (true);
}

252 253 254 255
// The main library loading function.

bool
LibraryManager::loadLibrary() {
256
    LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_LIBRARY_LOADING)
257 258 259 260 261 262
        .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.

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

        // Library opened OK, see if a version function is present and if so,
267
        // check what value it returns.
268 269 270
        if (checkVersion()) {

            // Version OK, so now register the standard callouts and call the
271
            // library's load() function if present.
272 273 274 275 276 277
            registerStandardCallouts();
            if (runLoad()) {

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

279 280 281 282 283
            } 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
284 285
                // need to be freed and (b) we need to remove any callouts that
                // have been installed.
286 287 288
                static_cast<void>(unloadLibrary());
            }
        }
289

290 291 292
        // 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.
293
        static_cast<void>(closeLibrary());
294 295 296 297 298 299
    }

    return (false);
}

// The library unloading function.  Call the unload() function (if present),
300 301 302
// 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.
303 304 305

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

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        // 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.
        result = runUnload();

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

328 329 330 331 332 333 334 335
        // ... 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_);
336 337
        }
    }
338 339
    return (result);
}
340

341 342 343
// 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.
344

345 346 347 348 349
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);
350

351 352 353 354 355 356 357 358
    // Try to open it and, if we succeed, check the version.
    bool validated = manager.openLibrary() && manager.checkVersion();

    // 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);
359 360
}

361 362
} // namespace hooks
} // namespace isc