Added better generic support for connector options.
This commit is contained in:
parent
0daa757e92
commit
0c059f0ca3
2 changed files with 86 additions and 20 deletions
|
@ -21,6 +21,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <fstream>
|
||||
#include <dirent.h> //for getMyExec
|
||||
|
||||
bool Util::Config::is_active = false;
|
||||
std::string Util::Config::libver = PACKAGE_VERSION;
|
||||
|
@ -167,7 +168,7 @@ void Util::Config::printHelp(std::ostream & output){
|
|||
|
||||
/// Parses commandline arguments.
|
||||
/// Calls exit if an unknown option is encountered, printing a help message.
|
||||
void Util::Config::parseArgs(int & argc, char ** & argv){
|
||||
bool Util::Config::parseArgs(int & argc, char ** & argv){
|
||||
int opt = 0;
|
||||
std::string shortopts;
|
||||
struct option * longOpts = (struct option*)calloc(long_count + 1, sizeof(struct option));
|
||||
|
@ -250,10 +251,9 @@ void Util::Config::parseArgs(int & argc, char ** & argv){
|
|||
long_i++;
|
||||
}
|
||||
if (long_i <= arg_count){
|
||||
std::cerr << "Usage error: missing argument(s)." << std::endl;
|
||||
printHelp(std::cout);
|
||||
exit(1);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Returns a reference to the current value of an option or default if none was set.
|
||||
|
@ -334,22 +334,67 @@ void Util::Config::signal_handler(int signum){
|
|||
}
|
||||
} //signal_handler
|
||||
|
||||
/// Adds the default connector options to this Util::Config object.
|
||||
void Util::Config::addConnectorOptions(int port){
|
||||
JSON::Value stored_port = JSON::fromString("{\"long\":\"port\", \"short\":\"p\", \"arg\":\"integer\", \"help\":\"TCP port to listen on.\"}");
|
||||
stored_port["value"].append((long long int)port);
|
||||
addOption("listen_port", stored_port);
|
||||
addOption("listen_interface",
|
||||
JSON::fromString(
|
||||
"{\"long\":\"interface\", \"value\":[\"0.0.0.0\"], \"short\":\"i\", \"arg\":\"string\", \"help\":\"Interface address to listen on, or 0.0.0.0 for all available interfaces.\"}"));
|
||||
addOption("username",
|
||||
JSON::fromString(
|
||||
"{\"long\":\"username\", \"value\":[\"root\"], \"short\":\"u\", \"arg\":\"string\", \"help\":\"Username to drop privileges to, or root to not drop provileges.\"}"));
|
||||
addOption("daemonize",
|
||||
JSON::fromString(
|
||||
"{\"long\":\"daemon\", \"short\":\"d\", \"value\":[1], \"long_off\":\"nodaemon\", \"short_off\":\"n\", \"help\":\"Whether or not to daemonize the process after starting.\"}"));
|
||||
/// Adds the default connector options. Also updates the capabilities structure with the default options.
|
||||
/// Besides the options addBasicConnectorOptions adds, this function also adds port and interface options.
|
||||
void Util::Config::addConnectorOptions(int port, JSON::Value & capabilities){
|
||||
JSON::Value option;
|
||||
option.null();
|
||||
option["long"] = "port";
|
||||
option["short"] = "p";
|
||||
option["arg"] = "integer";
|
||||
option["help"] = "TCP port to listen on";
|
||||
option["value"].append((long long)port);
|
||||
addOption("listen_port", option);
|
||||
capabilities["optional"]["port"]["name"] = "TCP port";
|
||||
capabilities["optional"]["port"]["help"] = "TCP port to listen on - default if unprovided is "+option["value"][0u].asString();
|
||||
capabilities["optional"]["port"]["type"] = "uint";
|
||||
capabilities["optional"]["port"]["default"] = option["value"][0u];
|
||||
|
||||
option.null();
|
||||
option["long"] = "interface";
|
||||
option["short"] = "i";
|
||||
option["arg"] = "string";
|
||||
option["help"] = "Interface address to listen on, or 0.0.0.0 for all available interfaces.";
|
||||
option["value"].append("0.0.0.0");
|
||||
addOption("listen_interface", option);
|
||||
capabilities["optional"]["interface"]["name"] = "Interface";
|
||||
capabilities["optional"]["interface"]["help"] = "Address of the interface to listen on - default if unprovided is all interfaces";
|
||||
capabilities["optional"]["interface"]["type"] = "str";
|
||||
|
||||
addBasicConnectorOptions(capabilities);
|
||||
} //addConnectorOptions
|
||||
|
||||
/// Adds the default connector options. Also updates the capabilities structure with the default options.
|
||||
void Util::Config::addBasicConnectorOptions(JSON::Value & capabilities){
|
||||
JSON::Value option;
|
||||
option.null();
|
||||
option["long"] = "username";
|
||||
option["short"] = "u";
|
||||
option["arg"] = "string";
|
||||
option["help"] = "Username to drop privileges to, or root to not drop provileges.";
|
||||
option["value"].append("root");
|
||||
addOption("username", option);
|
||||
capabilities["optional"]["username"]["name"] = "Username";
|
||||
capabilities["optional"]["username"]["help"] = "Username to drop privileges to - default if unprovided means do not drop privileges";
|
||||
capabilities["optional"]["username"]["type"] = "str";
|
||||
|
||||
option.null();
|
||||
option["long"] = "daemon";
|
||||
option["short"] = "d";
|
||||
option["long_off"] = "nodaemon";
|
||||
option["short_off"] = "n";
|
||||
option["help"] = "Whether or not to daemonize the process after starting.";
|
||||
option["value"].append(1ll);
|
||||
addOption("daemonize", option);
|
||||
|
||||
option.null();
|
||||
option["long"] = "json";
|
||||
option["short"] = "j";
|
||||
option["help"] = "Output connector info in JSON format, then exit.";
|
||||
option["value"].append(0ll);
|
||||
addOption("json", option);
|
||||
}
|
||||
|
||||
/// Gets directory the current executable is stored in.
|
||||
std::string Util::getMyPath(){
|
||||
char mypath[500];
|
||||
|
@ -371,6 +416,23 @@ std::string Util::getMyPath(){
|
|||
return tPath;
|
||||
}
|
||||
|
||||
/// Gets all executables in getMyPath that start with "Mist".
|
||||
void Util::getMyExec(std::deque<std::string> & execs){
|
||||
std::string path = Util::getMyPath();
|
||||
DIR * d = opendir(path.c_str());
|
||||
if (!d){return;}
|
||||
struct dirent *dp;
|
||||
do {
|
||||
errno = 0;
|
||||
if (dp = readdir(d)){
|
||||
if (strncmp(dp->d_name, "Mist", 4) == 0){
|
||||
execs.push_back(dp->d_name);
|
||||
}
|
||||
}
|
||||
} while (dp != NULL);
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
/// Sets the current process' running user
|
||||
void Util::setUser(std::string username){
|
||||
if (username != "root"){
|
||||
|
|
|
@ -27,18 +27,22 @@ namespace Util {
|
|||
Config(std::string cmd, std::string version);
|
||||
void addOption(std::string optname, JSON::Value option);
|
||||
void printHelp(std::ostream & output);
|
||||
void parseArgs(int & argc, char ** & argv);
|
||||
bool parseArgs(int & argc, char ** & argv);
|
||||
JSON::Value & getOption(std::string optname, bool asArray = false);
|
||||
std::string getString(std::string optname);
|
||||
long long int getInteger(std::string optname);
|
||||
bool getBool(std::string optname);
|
||||
void activate();
|
||||
void addConnectorOptions(int port);
|
||||
void addBasicConnectorOptions(JSON::Value & capabilities);
|
||||
void addConnectorOptions(int port, JSON::Value & capabilities);
|
||||
};
|
||||
|
||||
/// Gets directory the current executable is stored in.
|
||||
std::string getMyPath();
|
||||
|
||||
/// Gets all executables in getMyPath that start with "Mist".
|
||||
void getMyExec(std::deque<std::string> & execs);
|
||||
|
||||
/// Will set the active user to the named username.
|
||||
void setUser(std::string user);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue