summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-05-23 15:16:13 +0530
committerVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-06-02 12:43:26 +0530
commit15b1dc1788fdbe6b473a0fac6b14922304716555 (patch)
treeefbf37910cb32c98dd997e5af65cd11c251d6580
parent78154258b186ff3a35b81ee6e3bfa68157e617d5 (diff)
downloadphosphor-watchdog-15b1dc1788fdbe6b473a0fac6b14922304716555.tar.gz
phosphor-watchdog-15b1dc1788fdbe6b473a0fac6b14922304716555.zip
Add code to accept command line arguments
Accepts command line arguments for dbus object path, dbus service name and an optional systemd unit file to be called into when the timer expires. Change-Id: I3fbe59b36d73ed9f28de2596d77e6d2e99371622 Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
-rw-r--r--Makefile.am5
-rw-r--r--argument.cpp93
-rw-r--r--argument.hpp64
-rwxr-xr-xbootstrap.sh18
-rw-r--r--configure.ac23
-rw-r--r--mainapp.cpp52
6 files changed, 255 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..370d024
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,5 @@
+sbin_PROGRAMS = phosphor-watchdog
+
+phosphor_watchdog_SOURCES = \
+ argument.cpp \
+ mainapp.cpp
diff --git a/argument.cpp b/argument.cpp
new file mode 100644
index 0000000..61b1f65
--- /dev/null
+++ b/argument.cpp
@@ -0,0 +1,93 @@
+/**
+ * Copyright © 2016 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include <cassert>
+#include "argument.hpp"
+
+namespace phosphor
+{
+namespace watchdog
+{
+
+using namespace std::string_literals;
+
+const std::string ArgumentParser::trueString = "true"s;
+const std::string ArgumentParser::emptyString = ""s;
+
+const char* ArgumentParser::optionStr = "p:s:t:?h";
+const option ArgumentParser::options[] =
+{
+ { "path", required_argument, nullptr, 'o' },
+ { "service", required_argument, nullptr, 's' },
+ { "target", required_argument, nullptr, 't' },
+ { "help", no_argument, nullptr, 'h' },
+ { 0, 0, 0, 0},
+};
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+ int option = 0;
+ while (-1 != (option = getopt_long(argc, argv, optionStr, options, nullptr)))
+ {
+ if ((option == '?') || (option == 'h'))
+ {
+ usage(argv);
+ exit(-1);
+ }
+
+ auto i = &options[0];
+ while ((i->val != option) && (i->val != 0))
+ {
+ ++i;
+ }
+
+ if (i->val)
+ {
+ arguments[i->name] = (i->has_arg ? optarg : trueString);
+ }
+ }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+ auto i = arguments.find(opt);
+ if (i == arguments.end())
+ {
+ return emptyString;
+ }
+ else
+ {
+ return i->second;
+ }
+}
+
+void ArgumentParser::usage(char** argv)
+{
+ std::cerr << "Usage: " << argv[0] << " options\n";
+ std::cerr << "Options:\n";
+ std::cerr << " --help Print this menu\n";
+ std::cerr << " --path=<Dbus Object path> Dbus Object path."
+ " Ex: /xyz/openbmc_project/"
+ "state/watchdog/host0\n";
+ std::cerr << " --service=<Dbus Service name> Dbus Service name."
+ " Ex: xyz.openbmc_project."
+ "State.Watchdog.Host\n";
+ std::cerr << " [--target=<systemd unit>] Systemd unit to be called on"
+ " timeout\n";
+}
+} // namespace watchdog
+} // namespace phosphor
diff --git a/argument.hpp b/argument.hpp
new file mode 100644
index 0000000..29993d6
--- /dev/null
+++ b/argument.hpp
@@ -0,0 +1,64 @@
+#pragma once
+
+#include <getopt.h>
+#include <map>
+#include <string>
+
+namespace phosphor
+{
+namespace watchdog
+{
+/** @brief Class ArgumentParser - Encapsulates parsing command line options
+ * and populating arguments
+ */
+class ArgumentParser
+{
+ public:
+ ArgumentParser() = delete;
+ ~ArgumentParser() = default;
+ ArgumentParser(const ArgumentParser&) = delete;
+ ArgumentParser& operator=(const ArgumentParser&) = delete;
+ ArgumentParser(ArgumentParser&&) = default;
+ ArgumentParser& operator=(ArgumentParser&&) = default;
+
+ /** @brief Contructs ArgumentParser object
+ *
+ * @param argc - the main function's argc passed as is
+ * @param argv - the main function's argv passed as is
+ * @return Object constructed
+ */
+ ArgumentParser(int argc, char** argv);
+
+ /** @brief Given an option, returns its argument(optarg)
+ *
+ * @param opt - command line option string
+ *
+ * @return argument which is a standard optarg
+ */
+ const std::string& operator[](const std::string& opt);
+
+ /** @brief Displays usage
+ *
+ * @param argv - the main function's argv passed as is
+ */
+ static void usage(char** argv);
+
+ /** @brief Set to 'true' when an option is passed */
+ static const std::string trueString;
+
+ /** @brief Set to '' when an option is not passed */
+ static const std::string emptyString;
+
+ private:
+ /** @brief Option to argument mapping */
+ std::map<const std::string, std::string> arguments;
+
+ /** @brief Array of struct options as needed by getopt_long */
+ static const option options[];
+
+ /** @brief optstring as needed by getopt_long */
+ static const char* optionStr;
+};
+
+} // namespace watchdog
+} // namespace phosphor
diff --git a/bootstrap.sh b/bootstrap.sh
new file mode 100755
index 0000000..50b75b7
--- /dev/null
+++ b/bootstrap.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+AUTOCONF_FILES="Makefile.in aclocal.m4 ar-lib autom4te.cache compile \
+ config.guess config.h.in config.sub configure depcomp install-sh \
+ ltmain.sh missing *libtool test-driver"
+
+case $1 in
+ clean)
+ test -f Makefile && make maintainer-clean
+ for file in ${AUTOCONF_FILES}; do
+ find -name "$file" | xargs -r rm -rf
+ done
+ exit 0
+ ;;
+esac
+
+autoreconf -i
+echo 'Run "./configure ${CONFIGURE_FLAGS} && make"'
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..2f48a68
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,23 @@
+# Initialization
+AC_PREREQ([2.69])
+AC_INIT([phosphor-watchdog], [1.0],
+ [https://github.com/openbmc/phosphor-watchdog/issues])
+AC_LANG([C++])
+AC_CONFIG_HEADERS([config.h])
+AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
+AM_SILENT_RULES([yes])
+
+# Checks for programs.
+AC_PROG_CXX
+AC_PROG_INSTALL
+
+# Checks for typedefs, structures, and compiler characteristics.
+AX_CXX_COMPILE_STDCXX_14([noext])
+AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
+
+# For linking
+LT_INIT
+
+# Create configured output
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/mainapp.cpp b/mainapp.cpp
new file mode 100644
index 0000000..19aeb6a
--- /dev/null
+++ b/mainapp.cpp
@@ -0,0 +1,52 @@
+/**
+ * Copyright © 2017 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include "argument.hpp"
+
+static void exitWithError(const char* err, char** argv)
+{
+ phosphor::watchdog::ArgumentParser::usage(argv);
+ std::cerr << "ERROR: " << err << "\n";
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char** argv)
+{
+ // Read arguments.
+ auto options = phosphor::watchdog::ArgumentParser(argc, argv);
+
+ // Parse out path argument.
+ auto path = (options)["path"];
+ if (path == phosphor::watchdog::ArgumentParser::emptyString)
+ {
+ exitWithError("Path not specified.", argv);
+ }
+
+ // Parse out service name argument
+ auto service = (options)["service"];
+ if (service == phosphor::watchdog::ArgumentParser::emptyString)
+ {
+ exitWithError("Service not specified.", argv);
+ }
+
+ // Parse out target argument. It is fine if the caller does not
+ // pass this if they are not interested in calling into any target
+ // on meeting a condition.
+ auto target = (options)["target"];
+
+ return 0;
+}
OpenPOWER on IntegriCloud