Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 17 | #include "argument.hpp" |
| 18 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 19 | #include <cassert> |
| 20 | #include <iostream> |
| 21 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace watchdog |
| 25 | { |
| 26 | |
| 27 | using namespace std::string_literals; |
| 28 | |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 29 | const std::vector<std::string> emptyArg; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 30 | const std::string ArgumentParser::trueString = "true"s; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 31 | |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 32 | const char* ArgumentParser::optionStr = "p:s:t:a:f:i:ech"; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 33 | // clang-format off |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 34 | const option ArgumentParser::options[] = |
| 35 | { |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 36 | { "path", required_argument, nullptr, 'p' }, |
| 37 | { "service", required_argument, nullptr, 's' }, |
| 38 | { "target", required_argument, nullptr, 't' }, |
| 39 | { "action_target", required_argument, nullptr, 'a' }, |
| 40 | { "fallback_action", required_argument, nullptr, 'f' }, |
| 41 | { "fallback_interval", required_argument, nullptr, 'i' }, |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 42 | { "fallback_always", no_argument, nullptr, 'e' }, |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 43 | { "continue", no_argument, nullptr, 'c' }, |
| 44 | { "help", no_argument, nullptr, 'h' }, |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 45 | { 0, 0, 0, 0}, |
| 46 | }; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 47 | // clang-format on |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 48 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 49 | ArgumentParser::ArgumentParser(int argc, char* const argv[]) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 50 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 51 | int option; |
| 52 | int opt_idx = 0; |
William A. Kennington III | 8728491 | 2018-01-23 22:07:25 -0800 | [diff] [blame] | 53 | |
| 54 | // We have to reset optind because getopt_long keeps global state |
| 55 | // and uses optind to track what argv index it needs to process next. |
| 56 | // Since this object may be instantiated more than once or test suites may |
| 57 | // already process instructions, optind may not be initialized to point to |
| 58 | // the beginning of our argv. |
| 59 | optind = 0; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 60 | while (-1 != |
| 61 | (option = getopt_long(argc, argv, optionStr, options, &opt_idx))) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 62 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 63 | if (option == '?' || option == 'h') |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 64 | { |
| 65 | usage(argv); |
| 66 | exit(-1); |
| 67 | } |
| 68 | |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 69 | auto i = &options[opt_idx]; |
| 70 | while (i->val && i->val != option) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 71 | { |
| 72 | ++i; |
| 73 | } |
| 74 | |
| 75 | if (i->val) |
| 76 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 77 | arguments[i->name].push_back(optarg ? optarg : trueString); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 78 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 79 | |
| 80 | opt_idx = 0; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 84 | const std::vector<std::string>& ArgumentParser:: |
| 85 | operator[](const std::string& opt) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 86 | { |
| 87 | auto i = arguments.find(opt); |
| 88 | if (i == arguments.end()) |
| 89 | { |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 90 | return emptyArg; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
| 94 | return i->second; |
| 95 | } |
| 96 | } |
| 97 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 98 | void ArgumentParser::usage(char* const argv[]) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 99 | { |
| 100 | std::cerr << "Usage: " << argv[0] << " options\n"; |
| 101 | std::cerr << "Options:\n"; |
William A. Kennington III | 1292643 | 2018-02-25 19:54:59 -0800 | [diff] [blame] | 102 | std::cerr << " --help Print this menu\n"; |
| 103 | std::cerr << " --path=<Dbus Object path> Dbus Object path. " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 104 | "Ex: /xyz/openbmc_project/state/watchdog/host0\n"; |
William A. Kennington III | 1292643 | 2018-02-25 19:54:59 -0800 | [diff] [blame] | 105 | std::cerr << " --service=<Dbus Service name> Dbus Service " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 106 | "name. Ex: xyz.openbmc_project.State.Watchdog.Host\n"; |
William A. Kennington III | 1292643 | 2018-02-25 19:54:59 -0800 | [diff] [blame] | 107 | std::cerr << " [--target=<systemd unit>] Systemd unit to " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 108 | "be called on timeout for all actions but NONE. " |
| 109 | "Deprecated, use --action_target instead.\n"; |
William A. Kennington III | 180b690 | 2018-02-22 14:29:57 -0800 | [diff] [blame] | 110 | std::cerr << " [--action_target=<action>=<systemd unit>] Map of action to " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 111 | "systemd unit to be called on timeout if that action is " |
| 112 | "set for ExpireAction when the timer expires.\n"; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 113 | std::cerr << " [--fallback_action=<action>] Enables the " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 114 | "watchdog even when disabled via the dbus interface. " |
| 115 | "Perform this action when the fallback expires.\n"; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 116 | std::cerr << " [--fallback_interval=<interval in ms>] Enables the " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 117 | "watchdog even when disabled via the dbus interface. " |
| 118 | "Waits for this interval before performing the fallback " |
| 119 | "action.\n"; |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 120 | std::cerr << " [--fallback_always] Enables the " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 121 | "watchdog even when disabled by the dbus interface. " |
| 122 | "This option is only valid with a fallback specified.\n"; |
William A. Kennington III | 1292643 | 2018-02-25 19:54:59 -0800 | [diff] [blame] | 123 | std::cerr << " [--continue] Continue daemon " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 124 | "after watchdog timeout.\n"; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 125 | } |
| 126 | } // namespace watchdog |
| 127 | } // namespace phosphor |