Browse Source

[trac3664] Add the command line processing

Continue adding the boiler plate files and
add the function to parse the command line arguments.
Shawn Routhier 10 years ago
parent
commit
c9ce195564
7 changed files with 415 additions and 58 deletions
  1. 2 2
      src/bin/lfc/Makefile.am
  2. 108 0
      src/bin/lfc/kea-lfc.xml
  3. 208 0
      src/bin/lfc/lfc.cc
  4. 92 0
      src/bin/lfc/lfc.h
  5. 0 22
      src/bin/lfc/lfc_lfc.cc
  6. 0 29
      src/bin/lfc/lfc_lfc.h
  7. 5 5
      src/bin/lfc/main.cc

+ 2 - 2
src/bin/lfc/Makefile.am

@@ -48,8 +48,8 @@ BUILT_SOURCES = lfc_messages.h lfc_messages.cc
 noinst_LTLIBRARIES = liblfc.la
 noinst_LTLIBRARIES = liblfc.la
 
 
 liblfc_la_SOURCES  =
 liblfc_la_SOURCES  =
-liblfc_la_SOURCES += lfc_lfc.h
-liblfc_la_SOURCES += lfc_lfc.cc
+liblfc_la_SOURCES += lfc.h
+liblfc_la_SOURCES += lfc.cc
 
 
 nodist_liblfc_la_SOURCES = lfc_messages.h lfc_messages.cc
 nodist_liblfc_la_SOURCES = lfc_messages.h lfc_messages.cc
 EXTRA_DIST += lfc_messages.mes
 EXTRA_DIST += lfc_messages.mes

+ 108 - 0
src/bin/lfc/kea-lfc.xml

@@ -0,0 +1,108 @@
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+               "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
+	       [<!ENTITY mdash "&#8212;">]>
+<!--
+ - 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.
+-->
+
+<refentry>
+
+  <refentryinfo>
+    <date>Feb 1, 2015</date>
+  </refentryinfo>
+
+  <refmeta>
+    <refentrytitle>kea-lfc</refentrytitle>
+    <manvolnum>8</manvolnum>
+    <refmiscinfo>Kea</refmiscinfo>
+  </refmeta>
+
+  <refnamediv>
+    <refname>kea-lfc</refname>
+    <refpurpose>Lease File Cleanup  process in Kea</refpurpose>
+  </refnamediv>
+
+  <docinfo>
+    <copyright>
+      <year>2015</year>
+      <holder>Internet Systems Consortium, Inc. ("ISC")</holder>
+    </copyright>
+  </docinfo>
+
+  <refsynopsisdiv>
+    <cmdsynopsis>
+      <command>kea-lfc</command>
+      <arg><option>-v</option></arg>
+    </cmdsynopsis>
+  </refsynopsisdiv>
+
+  <refsynopsisdiv>
+    <cmdsynopsis>
+      <command>kea-lfc</command>
+      <arg><option>-s</option></arg>
+    </cmdsynopsis>
+  </refsynopsisdiv>
+
+
+  <refsect1>
+    <title>DESCRIPTION</title>
+    <para>
+      The <command>kea-lfc</command> service process removes redundant
+      information for the files used to provide persistent storage for
+      the memfile data base back end.  The service is written to run as
+      a stand alone process.  While it can be started externally it
+      should be started by the Kea DHCP servers as desired and required.
+    </para>
+
+  </refsect1>
+
+  <refsect1>
+    <title>ARGUMENTS</title>
+
+    <para>The arguments are as follows:</para>
+
+    <variablelist>
+
+      <varlistentry>
+        <term><option>-v</option></term>
+        <listitem><para>
+          Verbose mode sets the logging level to debug. This is primarily
+          for development purposes in stand-alone mode.
+        </para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>-c</option></term>
+        <listitem><para>
+          Configuration file including the configuration for LFC process.
+          It may also contain configuration entries for other Kea services.
+        </para></listitem>
+      </varlistentry>
+
+    </variablelist>
+  </refsect1>
+
+  <refsect1>
+    <title>HISTORY</title>
+    <para>
+      The <command>kea-lfc</command> process was first coded in January
+      2015 by the ISC Kea/DHCP team.
+    </para>
+  </refsect1>
+</refentry><!--
+ - Local variables:
+ - mode: sgml
+ - End:
+-->

+ 208 - 0
src/bin/lfc/lfc.cc

@@ -0,0 +1,208 @@
+// 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.
+
+#include <config.h>
+#include <lfc/lfc.h>
+#include <exceptions/exceptions.h>
+#include <log/logger_support.h>
+#include <log/logger_manager.h>
+//#include <dhcpsrv/cfgmgr.h>
+#include <iostream>
+
+using namespace std;
+
+namespace isc {
+namespace lfc {
+
+/// @brief Defines the application name, it may be used to locate
+/// configuration data and appears in log statements.
+const char* lfc::lfc_app_name_ = "DhcpLFC";
+
+/// @brief Defines the executable name.
+const char*lfc::lfc_bin_name_ = "kea-lfc";
+
+lfc::lfc()
+    : dhcp_version_(0), verbose_(false), config_file_(""),
+      previous_file_(""), copy_file_(""), output_file_("") {
+    std::cerr << "created lfc" << std::endl;
+}
+
+lfc::~lfc() {
+    std::cerr << "destroyed lfc" << std::endl;
+}
+
+void
+lfc::launch(int argc, char* argv[], const bool test_mode) {
+  try {
+    parseArgs(argc, argv);
+  } catch (const InvalidUsage& ex) {
+    usage(ex.what());
+    throw;  // rethrow it
+  }
+
+  std::cerr << "launched lfc" << std::endl;
+}
+
+void
+lfc::parseArgs(int argc, char* argv[])
+{
+    int ch;
+
+    while ((ch = getopt(argc, argv, "46dvVp:i:o:c:")) != -1) {
+        switch (ch) {
+	case '4':
+            // Process DHCPv4 lease files.
+            dhcp_version_ = 4;
+            break;
+
+	case '6':
+            // Process DHCPv6 lease files.
+            dhcp_version_ = 6;
+            break;
+
+	case 'v':
+            // Print just Kea vesion and exit.
+	  //            std::cout << getVersion(false) << std::endl;
+            exit(EXIT_SUCCESS);
+
+	case 'V':
+            // Print extended  Kea vesion and exit.
+	  //std::cout << getVersion(true) << std::endl;
+            exit(EXIT_SUCCESS);
+
+	case 'd':
+	    // Verbose output.
+	    verbose_ = true;
+	    break;
+
+	case 'p':
+	    // Previous file name.
+	    if (optarg == NULL) {
+	        isc_throw(InvalidUsage, "Previous file name missing");
+	    }
+	    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 'c':
+	    // Previous file name.
+	    if (optarg == NULL) {
+	        isc_throw(InvalidUsage, "Configuration file name missing");
+	    }
+	    config_file_ = optarg;
+	    break;
+
+	default:
+	    usage("");
+	}
+    }
+
+    // Check for extraneous parameters.
+    if (argc > optind) {
+        isc_throw(InvalidUsage, "Extraneous parameters.");
+    }
+
+    if (dhcp_version_ == 0) {
+        isc_throw(InvalidUsage, "DHCP version required");
+    }
+
+    if (previous_file_.empty()) {
+        isc_throw(InvalidUsage, "Previous file not specified");	
+    }
+
+    if (copy_file_.empty()) {
+        isc_throw(InvalidUsage, "Copy file not specified");	
+    }
+
+    if (output_file_.empty()) {
+        isc_throw(InvalidUsage, "Output file not specified");	
+    }
+
+    if (config_file_.empty()) {
+        isc_throw(InvalidUsage, "Config file not specified");	
+    }
+
+    // If verbose is set echo the input information
+    if (verbose_ == true) {
+      std::cerr << "Protocol version:    " << dhcp_version_ << std::endl
+	        << "Previous lease file: " << previous_file_ << std::endl
+	        << "Copy lease file:     " << copy_file_ << std::endl
+	        << "Output lease file:   " << output_file_ << std::endl
+	        << "Config file:         " << config_file_ << std::endl;
+    }
+}
+
+void
+lfc::pidCheck()
+{
+}
+
+void
+lfc::pidDelete()
+{
+}
+
+void
+lfc::usage(const std::string & text)
+{
+    if (text != "") {
+        std::cerr << "Usage error: " << text << std::endl;
+    }
+
+    std::cerr << "Usage: " << lfc_bin_name_ << std::endl
+              << " [-4|-6] -p file -i file -o file -c file" << std::endl
+              << "   -4 or -6 clean a set of v4 or v6 lease files" << std::endl
+              << "   -p <file>: previous lease file" << std::endl
+              << "   -i <file>: copy of lease file" << std::endl
+              << "   -o <file>: output lease file" << std::endl
+              << "   -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
+              << std::endl;
+}
+
+}; // namespace isc::lfc
+}; // namespace isc
+
+//std::string
+//isc::dhcp::Daemon::getVersion(bool extended) {
+//    std::stringstream tmp;
+//
+//    tmp << VERSION;
+//    if (extended) {
+//        tmp << std::endl << EXTENDED_VERSION;
+//
+        // @todo print more details (is it Botan or OpenSSL build,
+        // with or without MySQL/Postgres? What compilation options were
+        // used? etc)
+//    }
+//
+//    return (tmp.str());
+//}

+ 92 - 0
src/bin/lfc/lfc.h

@@ -0,0 +1,92 @@
+// 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.
+
+#ifndef LFC_H
+#define LFC_H
+
+#include <boost/shared_ptr.hpp>
+
+#include <exceptions/exceptions.h>
+
+namespace isc {
+namespace lfc {
+
+/// @brief Exception thrown when the command line is invalid.
+class InvalidUsage : public isc::Exception {
+public:
+    InvalidUsage(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) { };
+};
+
+//class lfcBase;
+//typedef boost::shared_ptr<lfcBase> lfcBasePtr;
+
+class lfc {
+public:
+    /// @brief Defines the application name, it may be used to locate
+    /// configuration data and appears in log statements.
+    static const char* lfc_app_name_;
+
+    /// @brief Defines the executable name, by convention this should match
+    /// the executable name.
+    static const char* lfc_bin_name_;
+
+    /// @brief Constructor
+    lfc();
+
+    /// @brief Destructor
+    ~lfc();
+
+    /// @brief Acts as the primary entry point to start execution
+    /// of the process.  Provides the control logic:
+    ///
+    /// 1. parse command line arguments
+    /// 2. verifies that it is the only instance
+    /// 3. creates pid file
+    /// .... TBD
+    /// 4. remove pid file
+    /// 5. exit to the caller
+    void launch(int argc, char* argv[], const bool test_mode);
+
+    /// @brief Process the command line arguments.  It is the first
+    /// step taken after the process has been launched.
+    void parseArgs(int argc, char* argv[]);
+
+    /// @brief Use the pid to determine if there is another instance
+    /// and create a pid file if we are alone.
+    void pidCheck();
+
+    /// @brief Get rid of the pid file we created earlier
+    void pidDelete();
+
+    /// #brief prints the program usage text to std error.
+    ///
+    /// @param text is a string message which will preceded the usage text.
+    /// This is intended to be used for specific usage violation messages.
+    void usage(const std::string& text);
+
+private:
+    int dhcp_version_;
+    bool verbose_;
+    std::string config_file_;
+    std::string previous_file_;
+    std::string copy_file_;
+    std::string output_file_;
+};
+
+}; // namespace isc:lfc
+}; // namespace isc
+
+#endif
+

+ 0 - 22
src/bin/lfc/lfc_lfc.cc

@@ -1,22 +0,0 @@
-// 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.
-
-#include <lfc/lfc_lfc.h>
-
-namespace isc {
-namespace lfc {
-
-
-}; // namespace isc::lfc
-}; // namespace isc

+ 0 - 29
src/bin/lfc/lfc_lfc.h

@@ -1,29 +0,0 @@
-// 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.
-
-#ifndef LFC_LFC_H
-#define LFC_LFC_H
-
-#include <boost/shared_ptr.hpp>
-
-#include <exceptions/exceptions.h>
-
-namespace isc {
-namespace lfc {
-
-}; // namespace isc:lfc
-}; // namespace isc
-
-#endif
-

+ 5 - 5
src/bin/lfc/main.cc

@@ -13,7 +13,7 @@
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
 #include <config.h>
 #include <config.h>
-#include <lfc/lfc_lfc.h>
+#include <lfc/lfc.h>
 #include <exceptions/exceptions.h>
 #include <exceptions/exceptions.h>
 #include <log/logger_support.h>
 #include <log/logger_support.h>
 #include <log/logger_manager.h>
 #include <log/logger_manager.h>
@@ -31,16 +31,16 @@ using namespace std;
 /// errors, EXIT_FAILURE otherwise.
 /// errors, EXIT_FAILURE otherwise.
 int main(int argc, char* argv[]) {
 int main(int argc, char* argv[]) {
     int ret = EXIT_SUCCESS;
     int ret = EXIT_SUCCESS;
+    lfc lfc;
 
 
-    // Instantiate/fetch the lfc application controller singleton.
-    //    DControllerBasePtr& controller = D2Controller::instance();
+    // Instantiate/fetch the lfc application controller.
+    //    lfc& lfc = lfc::lfc();
 
 
     // Launch the controller passing in command line arguments.
     // Launch the controller passing in command line arguments.
     // Exit program with the controller's return code.
     // Exit program with the controller's return code.
     try  {
     try  {
         // 'false' value disables test mode.
         // 'false' value disables test mode.
-      //controller->launch(argc, argv, false);
-        std::cerr << "Service started:" << std::endl;      
+        lfc.launch(argc, argv, false);
     } catch (const isc::Exception& ex) {
     } catch (const isc::Exception& ex) {
         std::cerr << "Service failed:" << ex.what() << std::endl;
         std::cerr << "Service failed:" << ex.what() << std::endl;
         ret = EXIT_FAILURE;
         ret = EXIT_FAILURE;