lfc_controller.cc 9.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2015  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 <lfc/lfc_controller.h>
16
#include <util/pid_file.h>
17
#include <exceptions/exceptions.h>
Shawn Routhier's avatar
Shawn Routhier committed
18
#include <config.h>
19
#include <iostream>
20 21
#include <sstream>
#include <unistd.h>
22
#include <stdlib.h>
23
#include <cerrno>
24 25

using namespace std;
26
using namespace isc::util;
27 28 29 30 31 32

namespace isc {
namespace lfc {

/// @brief Defines the application name, it may be used to locate
/// configuration data and appears in log statements.
33
const char* LFCController::lfc_app_name_ = "DhcpLFC";
34 35

/// @brief Defines the executable name.
36
const char* LFCController::lfc_bin_name_ = "kea-lfc";
37

38
LFCController::LFCController()
39
    : protocol_version_(0), verbose_(false), config_file_(""), previous_file_(""),
Shawn Routhier's avatar
Shawn Routhier committed
40
      copy_file_(""), output_file_(""), finish_file_(""), pid_file_("") {
41 42
}

43
LFCController::~LFCController() {
44 45 46
}

void
47
LFCController::launch(int argc, char* argv[]) {
48 49 50 51 52 53 54 55 56 57 58
    try {
        parseArgs(argc, argv);
    } catch (const InvalidUsage& ex) {
        usage(ex.what());
        throw;  // rethrow it
    }

    std::cerr << "Starting lease file cleanup" << std::endl;

    // verify we are the only instance
    PIDFile pid_file(pid_file_);
59 60 61 62 63 64 65 66 67

    try {
        if (pid_file.check() == true) {
            // Already running instance, bail out
            std::cerr << "LFC instance already running" <<  std::endl;
            return;
        }
    } catch (const PIDFileError& pid_ex) {
        std::cerr << pid_ex.what() << std::endl;
68 69 70 71 72 73 74 75 76 77 78 79 80
        return;
    }

    // create the pid file for this instance
    try {
        pid_file.write();
    } catch (const PIDFileError& pid_ex) {
        std::cerr << pid_ex.what() << std::endl;
        return;
    }

    // do other work (TBD)
    std::cerr << "Add code to perform lease cleanup" << std::endl;
81 82 83 84 85 86 87 88 89 90
    // If we don't have a finish file do the processing

    // We either already had a finish file or just created one, do the
    // file cleanup, we don't want to return after the catch as we
    // still need to cleanup the pid file
    try {
        fileCleanup();
    } catch (const RunTimeFail& run_ex) {
        std::cerr << run_ex.what() << std::endl;
    }
91 92

    // delete the pid file for this instance
93 94 95 96 97 98
    try {
        pid_file.deleteFile();
    } catch (const PIDFileError& pid_ex) {
        std::cerr << pid_ex.what() << std::endl;
        return;
    }
99 100

    std::cerr << "LFC complete" << std::endl;
101 102 103
}

void
104
LFCController::parseArgs(int argc, char* argv[]) {
105 106
    int ch;

107 108
    opterr = 0;
    optind = 1;
109
    while ((ch = getopt(argc, argv, ":46dvVp:x:i:o:c:f:")) != -1) {
110
        switch (ch) {
Shawn Routhier's avatar
Shawn Routhier committed
111
        case '4':
112
            // Process DHCPv4 lease files.
113
            protocol_version_ = 4;
114 115
            break;

Shawn Routhier's avatar
Shawn Routhier committed
116
        case '6':
117
            // Process DHCPv6 lease files.
118
            protocol_version_ = 6;
119 120
            break;

Shawn Routhier's avatar
Shawn Routhier committed
121
        case 'v':
122
            // Print just Kea vesion and exit.
Shawn Routhier's avatar
Shawn Routhier committed
123
            std::cout << getVersion(false) << std::endl;
124 125
            exit(EXIT_SUCCESS);

Shawn Routhier's avatar
Shawn Routhier committed
126
        case 'V':
127
            // Print extended  Kea vesion and exit.
Shawn Routhier's avatar
Shawn Routhier committed
128
            std::cout << getVersion(true) << std::endl;
129 130
            exit(EXIT_SUCCESS);

Shawn Routhier's avatar
Shawn Routhier committed
131 132 133 134 135 136
        case 'd':
            // Verbose output.
            verbose_ = true;
            break;

        case 'p':
137 138 139 140 141 142 143 144 145
            // PID file name.
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "PID file name missing");
            }
            pid_file_ = optarg;
            break;

        case 'x':
            // Previous (or ex) file name.
Shawn Routhier's avatar
Shawn Routhier committed
146
            if (optarg == NULL) {
147
                isc_throw(InvalidUsage, "Previous (ex) file name missing");
Shawn Routhier's avatar
Shawn Routhier committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
            }
            previous_file_ = optarg;
            break;

        case 'i':
            // Copy file name.
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Copy file name missing");
            }
            copy_file_ = optarg;
            break;

        case 'o':
            // Output file name.
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Output file name missing");
            }
            output_file_ = optarg;
            break;

        case 'f':
169
            // Finish file name.
Shawn Routhier's avatar
Shawn Routhier committed
170 171 172 173 174 175 176
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Finish file name missing");
            }
            finish_file_ = optarg;
            break;

        case 'c':
177
            // Configuration file name
Shawn Routhier's avatar
Shawn Routhier committed
178 179 180 181 182 183
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Configuration file name missing");
            }
            config_file_ = optarg;
            break;

184
        case 'h':
Shawn Routhier's avatar
Shawn Routhier committed
185
            usage("");
186 187 188 189 190 191 192 193 194
            exit(EXIT_SUCCESS);

        case '?':
            // Unknown argument
            isc_throw(InvalidUsage, "Unknown argument");

        case ':':
            // Missing option argument
            isc_throw(InvalidUsage, "Missing option argument");
195 196

        default:
197 198 199
            // I don't think we should get here as the unknown arguments
            // and missing options cases should cover everything else
            isc_throw(InvalidUsage, "Invalid command line");
Shawn Routhier's avatar
Shawn Routhier committed
200
        }
201 202 203 204 205 206 207
    }

    // Check for extraneous parameters.
    if (argc > optind) {
        isc_throw(InvalidUsage, "Extraneous parameters.");
    }

208
    if (protocol_version_ == 0) {
209 210 211
        isc_throw(InvalidUsage, "DHCP version required");
    }

212 213 214 215
    if (pid_file_.empty()) {
        isc_throw(InvalidUsage, "PID file not specified");
    }

216
    if (previous_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
217
        isc_throw(InvalidUsage, "Previous file not specified");
218 219 220
    }

    if (copy_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
221
        isc_throw(InvalidUsage, "Copy file not specified");
222 223 224
    }

    if (output_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
225
        isc_throw(InvalidUsage, "Output file not specified");
226 227
    }

228
    if (finish_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
229
        isc_throw(InvalidUsage, "Finish file not specified");
230 231
    }

232
    if (config_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
233
        isc_throw(InvalidUsage, "Config file not specified");
234 235 236 237
    }

    // If verbose is set echo the input information
    if (verbose_ == true) {
238
      std::cerr << "Protocol version:    DHCPv" << protocol_version_ << std::endl
239 240 241 242 243 244
                << "Previous or ex lease file: " << previous_file_ << std::endl
                << "Copy lease file:           " << copy_file_ << std::endl
                << "Output lease file:         " << output_file_ << std::endl
                << "Finishn file:              " << finish_file_ << std::endl
                << "Config file:               " << config_file_ << std::endl
                << "PID file:                  " << pid_file_ << std::endl;
245 246 247 248
    }
}

void
249
LFCController::usage(const std::string& text) {
250 251 252 253 254
    if (text != "") {
        std::cerr << "Usage error: " << text << std::endl;
    }

    std::cerr << "Usage: " << lfc_bin_name_ << std::endl
255
              << " [-4|-6] -p file -x file -i file -o file -f file -c file" << std::endl
256
              << "   -4 or -6 clean a set of v4 or v6 lease files" << std::endl
257 258
              << "   -p <file>: PID file" << std::endl
              << "   -x <file>: previous or ex lease file" << std::endl
259 260
              << "   -i <file>: copy of lease file" << std::endl
              << "   -o <file>: output lease file" << std::endl
261
              << "   -f <file>: finish file" << std::endl
262 263 264 265
              << "   -c <file>: configuration file" << std::endl
              << "   -v: print version number and exit" << std::endl
              << "   -V: print extended version inforamtion and exit" << std::endl
              << "   -d: optional, verbose output " << std::endl
266
              << "   -h: print this message " << std::endl
267 268 269
              << std::endl;
}

270
std::string
271 272
LFCController::getVersion(const bool extended) const{
    std::stringstream version_stream;
273

274
    version_stream << VERSION;
275
    if (extended) {
276
        version_stream << std::endl << EXTENDED_VERSION;
277 278
    }

279
    return (version_stream.str());
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
void
LFCController::processLeases() const {
}

void
LFCController::fileCleanup() const {
    // Remove the old previous file
    if ((remove(previous_file_.c_str()) != 0) &&
        (errno != ENOENT)) {
        isc_throw(RunTimeFail, "Unable to delete previous file '"
                  << previous_file_ << "' error: " << strerror(errno));
    }

    // Remove the copy file
    if ((remove(copy_file_.c_str()) != 0) &&
        (errno != ENOENT)) {
        isc_throw(RunTimeFail, "Unable to delete copy file '"
                  << copy_file_ << "' error: " << strerror(errno));
    }

    // Rename the finish file to be the previous file
    if (rename(finish_file_.c_str(), previous_file_.c_str()) != 0)
        isc_throw(RunTimeFail, "Unable to move finish (" << finish_file_
                  << ") to previous (" << previous_file_
                  << ") error: " << strerror(errno));
}
308 309
}; // namespace isc::lfc
}; // namespace isc