callout_manager.cc 13.7 KB
Newer Older
1
// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
Stephen Morris's avatar
Stephen Morris committed
2
//
3 4 5
// 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/.
Stephen Morris's avatar
Stephen Morris committed
6

7 8
#include <config.h>

9 10
#include <hooks/callout_handle.h>
#include <hooks/callout_manager.h>
11
#include <hooks/hooks_log.h>
12
#include <hooks/pointer_converter.h>
13
#include <util/stopwatch.h>
Stephen Morris's avatar
Stephen Morris committed
14

15 16
#include <boost/static_assert.hpp>

Stephen Morris's avatar
Stephen Morris committed
17
#include <algorithm>
18
#include <climits>
Stephen Morris's avatar
Stephen Morris committed
19 20 21 22 23 24
#include <functional>
#include <utility>

using namespace std;

namespace isc {
25
namespace hooks {
Stephen Morris's avatar
Stephen Morris committed
26

27 28
// Constructor
CalloutManager::CalloutManager(int num_libraries)
29 30
    : server_hooks_(ServerHooks::getServerHooks()),
      current_hook_(-1), current_library_(-1),
31 32 33 34 35 36 37 38 39 40
      hook_vector_(ServerHooks::getServerHooks().getCount()),
      library_handle_(this), pre_library_handle_(this, 0),
      post_library_handle_(this, INT_MAX), num_libraries_(num_libraries)
{
    if (num_libraries < 0) {
        isc_throw(isc::BadValue, "number of libraries passed to the "
                  "CalloutManager must be >= 0");
    }
}

41
// Check that the index of a library is valid.  It can range from 1 - n
Stephen Morris's avatar
Stephen Morris committed
42 43 44
// (n is the number of libraries), 0 (pre-user library callouts), or INT_MAX
// (post-user library callouts).  It can also be -1 to indicate an invalid
// value.
45 46 47 48 49 50 51

void
CalloutManager::checkLibraryIndex(int library_index) const {
    if (((library_index >= -1) && (library_index <= num_libraries_)) ||
        (library_index == INT_MAX)) {
        return;
    }
52 53 54 55

    isc_throw(NoSuchLibrary, "library index " << library_index <<
              " is not valid for the number of loaded libraries (" <<
              num_libraries_ << ")");
56 57
}

58
// Register a callout for the current library.
Stephen Morris's avatar
Stephen Morris committed
59 60

void
61
CalloutManager::registerCallout(const std::string& name, CalloutPtr callout) {
Stephen Morris's avatar
Stephen Morris committed
62
    // Note the registration.
63
    LOG_DEBUG(callouts_logger, HOOKS_DBG_CALLS, HOOKS_CALLOUT_REGISTRATION)
Stephen Morris's avatar
Stephen Morris committed
64 65
        .arg(current_library_).arg(name);

66 67 68
    // Sanity check that the current library index is set to a valid value.
    checkLibraryIndex(current_library_);

Stephen Morris's avatar
Stephen Morris committed
69 70
    // Get the index associated with this hook (validating the name in the
    // process).
71
    int hook_index = server_hooks_.getIndex(name);
Stephen Morris's avatar
Stephen Morris committed
72

Francis Dupont's avatar
Francis Dupont committed
73 74 75 76 77
    // New hooks can have been registered since the manager was constructed.
    if (hook_index >= hook_vector_.size()) {
        hook_vector_.resize(server_hooks_.getCount());
    }

Stephen Morris's avatar
Stephen Morris committed
78 79 80 81 82
    // Iterate through the callout vector for the hook from start to end,
    // looking for the first entry where the library index is greater than
    // the present index.
    for (CalloutVector::iterator i = hook_vector_[hook_index].begin();
         i != hook_vector_[hook_index].end(); ++i) {
83
        if (i->first > current_library_) {
84 85
            // Found an element whose library index number is greater than the
            // current index, so insert the new element ahead of this one.
86 87
            hook_vector_[hook_index].insert(i, make_pair(current_library_,
                                                         callout));
Stephen Morris's avatar
Stephen Morris committed
88 89 90 91
            return;
        }
    }

92 93 94
    // Reached the end of the vector, so there is no element in the (possibly
    // empty) set of callouts with a library index greater than the current
    // library index.  Inset the callout at the end of the list.
95
    hook_vector_[hook_index].push_back(make_pair(current_library_, callout));
Stephen Morris's avatar
Stephen Morris committed
96 97 98 99 100 101 102
}

// Check if callouts are present for a given hook index.

bool
CalloutManager::calloutsPresent(int hook_index) const {
    // Validate the hook index.
103 104 105 106
    if ((hook_index < 0) || (hook_index >= hook_vector_.size())) {
        isc_throw(NoSuchHook, "hook index " << hook_index <<
                  " is not valid for the list of registered hooks");
    }
Stephen Morris's avatar
Stephen Morris committed
107 108 109 110 111

    // Valid, so are there any callouts associated with that hook?
    return (!hook_vector_[hook_index].empty());
}

112 113
bool
CalloutManager::commandHandlersPresent(const std::string& command_name) const {
114 115 116 117 118 119 120 121 122 123
    // Check if the hook point for the specified command exists.
    int index = ServerHooks::getServerHooks().findIndex(
                    ServerHooks::commandToHookName(command_name));
    if (index >= 0) {
        // The hook point exits but it is possible that there are no
        // callouts/command handlers. This is possible if there was a
        // hook library supporting this command attached, but it was
        // later unloaded. The hook points are not deregistered in
        // this case. Only callouts are deregistered.
        // Let's check if callouts are present for this hook point.
124 125
        return (calloutsPresent(index));
    }
126 127 128 129

    // Hook point not created, so we don't support this command in
    // any of the hooks libraries.
    return (false);
130 131 132
}


Stephen Morris's avatar
Stephen Morris committed
133 134
// Call all the callouts for a given hook.

135
void
Stephen Morris's avatar
Stephen Morris committed
136
CalloutManager::callCallouts(int hook_index, CalloutHandle& callout_handle) {
137

138 139 140
    // Clear the "skip" flag so we don't carry state from a previous call.
    // This is done regardless of whether callouts are present to avoid passing
    // any state from the previous call of callCallouts().
141
    callout_handle.setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
142

143 144 145 146
    // Only initialize and iterate if there are callouts present.  This check
    // also catches the case of an invalid index.
    if (calloutsPresent(hook_index)) {

Stephen Morris's avatar
Stephen Morris committed
147 148 149 150
        // Set the current hook index.  This is used should a callout wish to
        // determine to what hook it is attached.
        current_hook_ = hook_index;

151 152 153 154 155 156 157
        // Duplicate the callout vector for this hook and work through that.
        // This step is needed because we allow dynamic registration and
        // deregistration of callouts.  If a callout attached to a hook modified
        // the list of callouts on that hook, the underlying CalloutVector would
        // change and potentially affect the iteration through that vector.
        CalloutVector callouts(hook_vector_[hook_index]);

158 159
        // This object will be used to measure execution time of each callout
        // and the total time spent in callouts for this hook point.
160 161
        util::Stopwatch stopwatch;

162
        // Mark that the callouts begin for the hook.
163
        LOG_DEBUG(callouts_logger, HOOKS_DBG_CALLS, HOOKS_CALLOUTS_BEGIN)
164 165
            .arg(server_hooks_.getName(current_hook_));

166 167 168 169 170 171 172 173 174
        // Call all the callouts.
        for (CalloutVector::const_iterator i = callouts.begin();
             i != callouts.end(); ++i) {
            // In case the callout tries to register or deregister a callout,
            // set the current library index to the index associated with the
            // library that registered the callout being called.
            current_library_ = i->first;

            // Call the callout
Stephen Morris's avatar
Stephen Morris committed
175
            try {
176
                stopwatch.start();
Stephen Morris's avatar
Stephen Morris committed
177
                int status = (*i->second)(callout_handle);
178
                stopwatch.stop();
Stephen Morris's avatar
Stephen Morris committed
179
                if (status == 0) {
180
                    LOG_DEBUG(callouts_logger, HOOKS_DBG_EXTENDED_CALLS,
181
                              HOOKS_CALLOUT_CALLED).arg(current_library_)
182
                        .arg(server_hooks_.getName(current_hook_))
183 184
                        .arg(PointerConverter(i->second).dlsymPtr())
                        .arg(stopwatch.logFormatLastDuration());
Stephen Morris's avatar
Stephen Morris committed
185
                } else {
186
                    LOG_ERROR(callouts_logger, HOOKS_CALLOUT_ERROR)
Stephen Morris's avatar
Stephen Morris committed
187
                        .arg(current_library_)
188
                        .arg(server_hooks_.getName(current_hook_))
189 190
                        .arg(PointerConverter(i->second).dlsymPtr())
                        .arg(stopwatch.logFormatLastDuration());
Stephen Morris's avatar
Stephen Morris committed
191
                }
192
            } catch (const std::exception& e) {
193 194 195
                // If an exception occurred, the stopwatch.stop() hasn't been
                // called, so we have to call it here.
                stopwatch.stop();
Stephen Morris's avatar
Stephen Morris committed
196
                // Any exception, not just ones based on isc::Exception
197
                LOG_ERROR(callouts_logger, HOOKS_CALLOUT_EXCEPTION)
198
                    .arg(current_library_)
199
                    .arg(server_hooks_.getName(current_hook_))
200
                    .arg(PointerConverter(i->second).dlsymPtr())
201 202
                    .arg(e.what())
                    .arg(stopwatch.logFormatLastDuration());
203 204
            }

205
        }
206

207 208
        // Mark end of callout execution. Include the total execution
        // time for callouts.
209
        LOG_DEBUG(callouts_logger, HOOKS_DBG_CALLS, HOOKS_CALLOUTS_COMPLETE)
210 211 212
            .arg(server_hooks_.getName(current_hook_))
            .arg(stopwatch.logFormatTotalDuration());

Francis Dupont's avatar
Francis Dupont committed
213
        // Reset the current hook and library indexes to an invalid value to
Stephen Morris's avatar
Stephen Morris committed
214 215
        // catch any programming errors.
        current_hook_ = -1;
216 217
        current_library_ = -1;
    }
Stephen Morris's avatar
Stephen Morris committed
218 219
}

220 221 222 223
void
CalloutManager::callCommandHandlers(const std::string& command_name,
                                    CalloutHandle& callout_handle) {
    // Get the index of the hook point for the specified command.
224
    // This will throw an exception if the hook point doesn't exist.
225 226 227 228 229 230 231 232 233
    // The caller should check if the hook point exists by calling
    // commandHandlersPresent.
    int index = ServerHooks::getServerHooks().getIndex(
                    ServerHooks::commandToHookName(command_name));
    // Call the handlers for this command.
    callCallouts(index, callout_handle);
}


234
// Deregister a callout registered by the current library on a particular hook.
Stephen Morris's avatar
Stephen Morris committed
235 236

bool
237 238 239
CalloutManager::deregisterCallout(const std::string& name, CalloutPtr callout) {
    // Sanity check that the current library index is set to a valid value.
    checkLibraryIndex(current_library_);
Stephen Morris's avatar
Stephen Morris committed
240 241 242

    // Get the index associated with this hook (validating the name in the
    // process).
243
    int hook_index = server_hooks_.getIndex(name);
Stephen Morris's avatar
Stephen Morris committed
244

245 246 247 248 249
    // New hooks can have been registered since the manager was constructed.
    if (hook_index >= hook_vector_.size()) {
        return (false);
    }

250
    /// Construct a CalloutEntry matching the current library and the callout
Stephen Morris's avatar
Stephen Morris committed
251
    /// we want to remove.
252
    CalloutEntry target(current_library_, callout);
Stephen Morris's avatar
Stephen Morris committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

    /// To decide if any entries were removed, we'll record the initial size
    /// of the callout vector for the hook, and compare it with the size after
    /// the removal.
    size_t initial_size = hook_vector_[hook_index].size();

    // The next bit is standard STL (see "Item 33" in "Effective STL" by
    // Scott Meyers).
    //
    // remove_if reorders the hook vector so that all items not matching
    // the predicate are at the start of the vector and returns a pointer
    // to the next element. (In this case, the predicate is that the item
    // is equal to the value of the passed callout.)  The erase() call
    // removes everything from that element to the end of the vector, i.e.
    // all the matching elements.
    hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
                                             hook_vector_[hook_index].end(),
                                             bind1st(equal_to<CalloutEntry>(),
                                                     target)),
                                   hook_vector_[hook_index].end());

    // Return an indication of whether anything was removed.
275 276
    bool removed = initial_size != hook_vector_[hook_index].size();
    if (removed) {
277
        LOG_DEBUG(callouts_logger, HOOKS_DBG_EXTENDED_CALLS,
278
                  HOOKS_CALLOUT_DEREGISTERED).arg(current_library_).arg(name);
279 280 281
    }

    return (removed);
Stephen Morris's avatar
Stephen Morris committed
282 283 284 285 286
}

// Deregister all callouts on a given hook.

bool
287
CalloutManager::deregisterAllCallouts(const std::string& name) {
Stephen Morris's avatar
Stephen Morris committed
288 289 290

    // Get the index associated with this hook (validating the name in the
    // process).
291
    int hook_index = server_hooks_.getIndex(name);
Stephen Morris's avatar
Stephen Morris committed
292

293 294 295 296 297
    // New hooks can have been registered since the manager was constructed.
    if (hook_index >= hook_vector_.size()) {
        return (false);
    }

298 299
    /// Construct a CalloutEntry matching the current library (the callout
    /// pointer is NULL as we are not checking that).
300
    CalloutEntry target(current_library_, static_cast<CalloutPtr>(0));
Stephen Morris's avatar
Stephen Morris committed
301 302 303 304 305

    /// To decide if any entries were removed, we'll record the initial size
    /// of the callout vector for the hook, and compare it with the size after
    /// the removal.
    size_t initial_size = hook_vector_[hook_index].size();
306

Stephen Morris's avatar
Stephen Morris committed
307 308 309 310 311 312 313
    // Remove all callouts matching this library.
    hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
                                             hook_vector_[hook_index].end(),
                                             bind1st(CalloutLibraryEqual(),
                                                     target)),
                                   hook_vector_[hook_index].end());

314
    // Return an indication of whether anything was removed.
315 316
    bool removed = initial_size != hook_vector_[hook_index].size();
    if (removed) {
317
        LOG_DEBUG(callouts_logger, HOOKS_DBG_EXTENDED_CALLS,
318
                  HOOKS_ALL_CALLOUTS_DEREGISTERED).arg(current_library_)
319 320 321 322
                                                .arg(name);
    }

    return (removed);
Stephen Morris's avatar
Stephen Morris committed
323 324
}

325 326 327
void
CalloutManager::registerCommandHook(const std::string& command_name) {
    ServerHooks& hooks = ServerHooks::getServerHooks();
328
    int hook_index = hooks.findIndex(ServerHooks::commandToHookName(command_name));
329 330 331 332 333 334 335 336 337 338 339 340
    if (hook_index < 0) {
        // Hook for this command doesn't exist. Let's create one.
        hooks.registerHook(ServerHooks::commandToHookName(command_name));
        // Callout Manager's vector of hooks have to be resized to hold the
        // information about callouts for this new hook point. This should
        // add new element at the end of the hook_vector_. The index of this
        // element will match the index of the hook point in the ServerHooks
        // because ServerHooks allocates indexes incrementally.
        hook_vector_.resize(server_hooks_.getCount());
    }
}

Stephen Morris's avatar
Stephen Morris committed
341 342
} // namespace util
} // namespace isc