main.cc 3.71 KB
Newer Older
1 2 3 4 5 6 7
// Copyright (C) 2018 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>
8
#include <kea_version.h>
9

10 11
#include <netconf/netconf_log.h>
#include <exceptions/exceptions.h>
12
#include <process/daemon.h>
13
#include <iostream>
14 15 16 17
#include <fstream>
#include <unistd.h>
#include <cstdio>
#include <signal.h>
18

19
#include <sysrepo-cpp/Session.h>
20

21 22 23 24 25
using namespace std;
using namespace isc;
using namespace isc::netconf;

/// @brief Prints Kea Usage and exits
26 27 28 29
///
/// Note: This function never returns. It terminates the process.
void
usage() {
30 31 32 33 34 35 36
    cerr << "Kea netconf daemon, version " << VERSION << endl
         << endl
         << "Usage: " << endl
         << "  -c: config-file" << endl
         << "  -d: debug mode (maximum verbosity)" << endl
         << "  -v: print version number and exit" << endl
         << "  -V: print extended version and exit" << endl;
37 38 39
    exit(EXIT_FAILURE);
}

40
/// @name Temporary code until isc::process::Daemon is used.
41 42 43 44 45 46 47 48 49
///
/// @{
const char* PID_FILENAME = "kea-netconf.test_config.pid";

volatile bool SHUTDOWN_FLAG = false;

void
createPIDFile(int pid) {
    // This is not a real implemented. We will soon use the one coming
50
    // from isc::process::Daemon AFTER it's moved to libprocess.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    ofstream file(PID_FILENAME, ios::trunc);
    file << pid;
}

void
deletePIDFile() {
    remove(PID_FILENAME);
}

static void signal_handler(int ) {
    SHUTDOWN_FLAG = true;
}
/// @}

66 67 68 69
int
main(int argc, char* argv[]) {
    // The standard config file
    std::string config_file("");
70
    int ch;
71 72 73 74

    while ((ch = getopt(argc, argv, "vVc:")) != -1) {
        switch (ch) {
        case 'v':
75
            cout << string(PACKAGE_VERSION) << endl;
76 77 78
            return (EXIT_SUCCESS);

        case 'V':
79 80
            cout << string(PACKAGE_VERSION) << endl;
            cout << "git " << EXTENDED_VERSION << endl;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            return (EXIT_SUCCESS);

        case 'c': // config file
            config_file = optarg;
            break;
        default:
            usage();
        }
    }

    // Check for extraneous parameters.
    if (argc > optind) {
        usage();
    }

    // Configuration file is required.
    if (config_file.empty()) {
        cerr << "Configuration file not specified." << endl;
        usage();
    }

    int ret = EXIT_SUCCESS;
    try {
104

105
        // Temporary code. This will be replaced with isc::process::Daemon
106 107 108 109 110 111
        // once it is migrated to libprocess. We DO NOT want to bring
        // the whole libdhcpsrv into netconf.
        createPIDFile(getpid());
        signal(SIGHUP, signal_handler);
        signal(SIGINT, signal_handler);
        signal(SIGTERM, signal_handler);
112 113 114

        // Initialize logging.  If verbose, we'll use maximum verbosity.
        bool verbose_mode = true;
115
        isc::process::Daemon::loggerInit(NETCONF_LOGGER_NAME, verbose_mode);
116
        LOG_INFO(netconf_logger, NETCONF_STARTING).arg(VERSION).arg(getpid());
117

118 119
        Connection conn("kea-netconf");
        
120
        // Tell the admin we are ready to process packets
121
        LOG_INFO(netconf_logger, NETCONF_STATED).arg(VERSION);
122 123

        // And run the main loop of the server.
124
        while (!SHUTDOWN_FLAG) {
125 126 127 128
            cout << "Dummy kea-netconf running. Press ctrl-c to terminate."
                 << endl;
            sleep(1);
        }
129 130 131

        LOG_INFO(netconf_logger, NETCONF_SHUTDOWN);

132 133
        deletePIDFile();

134
    } catch (const isc::Exception& ex) {
135
        // First, we parint the error on stderr (that should always work)
136 137
        cerr << "ERROR:" << ex.what() << endl;
        ret = EXIT_FAILURE;
138 139 140 141
    }

    return (ret);
}