diff --git a/lib/config.cpp b/lib/config.cpp index 17cb5d4f..dd37ebcb 100644 --- a/lib/config.cpp +++ b/lib/config.cpp @@ -12,6 +12,7 @@ #endif #include #include +#include #include #include #include @@ -21,56 +22,279 @@ #include #include +bool Util::Config::is_active = false; + /// Creates a new configuration manager. -Util::Config::Config(){ - listen_port = 4242; - daemon_mode = true; - interface = "0.0.0.0"; - username = "root"; +Util::Config::Config(std::string cmd, std::string version){ + vals.null(); + long_count = 0; + vals["cmd"]["current"] = cmd; + vals["version"]["long"] = "version"; + vals["version"]["short"] = "v"; + vals["version"]["help"] = "Display library and application version, then exit."; + vals["help"]["long"] = "help"; + vals["help"]["short"] = "h"; + vals["help"]["help"] = "Display usage and version information, then exit."; + vals["version"]["current"] = version; } +/// Adds an option to the configuration parser. +/// The option needs an unique name (doubles will overwrite the previous) and can contain the following in the option itself: +/// { +/// "short":"o", //The short option letter +/// "long":"onName", //The long option +/// "short_off":"n", //The short option-off letter +/// "long_off":"offName", //The long option-off +/// "arg":"integer", //The type of argument, if required. +/// "default":1234, //The default value for this option if it is not given on the commandline. +/// "arg_num":1, //The count this value has on the commandline, after all the options have been processed. +/// "help":"Blahblahblah" //The helptext for this option. +/// } +void Util::Config::addOption(std::string optname, JSON::Value option){ + vals[optname] = option; + long_count = 0; + for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){ + if (it->second.isMember("long")){long_count++;} + if (it->second.isMember("long_off")){long_count++;} + } +} + +/// Prints a usage message to the given output. +void Util::Config::printHelp(std::ostream & output){ + int longest = 0; + std::map args; + for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){ + int current = 0; + if (it->second.isMember("long")){current += it->second["long"].asString().size() + 4;} + if (it->second.isMember("short")){current += it->second["short"].asString().size() + 3;} + if (current > longest){longest = current;} + current = 0; + if (it->second.isMember("long_off")){current += it->second["long_off"].asString().size() + 4;} + if (it->second.isMember("short_off")){current += it->second["short_off"].asString().size() + 3;} + if (current > longest){longest = current;} + if (it->second.isMember("arg_num")){ + current = it->first.size() + 3; + if (current > longest){longest = current;} + args[it->second["arg_num"].asInt()] = it->first; + } + } + output << "Usage: " << getString("cmd") << " [options]"; + for (std::map::iterator i = args.begin(); i != args.end(); i++){ + if (vals[i->second].isMember("default")){ + output << " [" << i->second << "]"; + }else{ + output << " " << i->second; + } + } + output << std::endl << std::endl; + for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){ + std::string f; + if (it->second.isMember("long") || it->second.isMember("short")){ + if (it->second.isMember("long") && it->second.isMember("short")){ + f = "--" + it->second["long"].asString() + ", -" + it->second["short"].asString(); + }else{ + if (it->second.isMember("long")){ + f = "--" + it->second["long"].asString(); + } + if (it->second.isMember("short")){ + f = "-" + it->second["short"].asString(); + } + } + while (f.size() < longest){f.append(" ");} + if (it->second.isMember("arg")){ + output << f << "(" << it->second["arg"].asString() << ") " << it->second["help"].asString() << std::endl; + }else{ + output << f << it->second["help"].asString() << std::endl; + } + } + if (it->second.isMember("long_off") || it->second.isMember("short_off")){ + if (it->second.isMember("long_off") && it->second.isMember("short_off")){ + f = "--" + it->second["long_off"].asString() + ", -" + it->second["short_off"].asString(); + }else{ + if (it->second.isMember("long_off")){ + f = "--" + it->second["long_off"].asString(); + } + if (it->second.isMember("short_off")){ + f = "-" + it->second["short_off"].asString(); + } + } + while (f.size() < longest){f.append(" ");} + if (it->second.isMember("arg")){ + output << f << "(" << it->second["arg"].asString() << ") " << it->second["help"].asString() << std::endl; + }else{ + output << f << it->second["help"].asString() << std::endl; + } + } + if (it->second.isMember("arg_num")){ + f = it->first; + while (f.size() < longest){f.append(" ");} + output << f << "(" << it->second["arg"].asString() << ") " << it->second["help"].asString() << std::endl; + } + } +} + + /// Parses commandline arguments. /// Calls exit if an unknown option is encountered, printing a help message. -/// confsection must be either already set or never be set at all when this function is called. -/// In other words: do not change confsection after calling this function. void Util::Config::parseArgs(int argc, char ** argv){ int opt = 0; - static const char *optString = "ndvp:i:u:c:h?"; - static const struct option longOpts[] = { - {"help",0,0,'h'}, - {"port",1,0,'p'}, - {"interface",1,0,'i'}, - {"username",1,0,'u'}, - {"nodaemon",0,0,'n'}, - {"daemon",0,0,'d'}, - {"version",0,0,'v'}, - 0 - }; - while ((opt = getopt_long(argc, argv, optString, longOpts, 0)) != -1){ + std::string shortopts; + struct option * longOpts = (struct option*)calloc(long_count+1, sizeof(struct option)); + int long_i = 0; + int arg_count = 0; + for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){ + if (it->second.isMember("short")){ + shortopts += it->second["short"].asString(); + if (it->second.isMember("arg")){shortopts += ":";} + } + if (it->second.isMember("short_off")){ + shortopts += it->second["short_off"].asString(); + if (it->second.isMember("arg")){shortopts += ":";} + } + if (it->second.isMember("long")){ + longOpts[long_i].name = it->second["long"].asString().c_str(); + longOpts[long_i].val = it->second["short"].asString()[0]; + if (it->second.isMember("arg")){longOpts[long_i].has_arg = 1;} + long_i++; + } + if (it->second.isMember("long_off")){ + longOpts[long_i].name = it->second["long_off"].asString().c_str(); + longOpts[long_i].val = it->second["short_off"].asString()[0]; + if (it->second.isMember("arg")){longOpts[long_i].has_arg = 1;} + long_i++; + } + if (it->second.isMember("arg_num") && !it->second.isMember("default")){ + if (it->second["arg_num"].asInt() > arg_count){ + arg_count = it->second["arg_num"].asInt(); + } + } + } + while ((opt = getopt_long(argc, argv, shortopts.c_str(), longOpts, 0)) != -1){ switch (opt){ - case 'p': listen_port = atoi(optarg); break; - case 'i': interface = optarg; break; - case 'n': daemon_mode = false; break; - case 'd': daemon_mode = true; break; - case 'u': username = optarg; break; - case 'v': - printf("%s\n", PACKAGE_VERSION); - exit(1); - break; case 'h': case '?': - std::string doingdaemon = "true"; - if (!daemon_mode){doingdaemon = "false";} - printf("Options: -h[elp], -?, -v[ersion], -n[odaemon], -d[aemon], -p[ort] VAL, -i[nterface] VAL, -u[sername] VAL\n"); - printf("Defaults:\n interface: %s\n port: %i\n daemon mode: %s\n username: %s\n", interface.c_str(), listen_port, doingdaemon.c_str(), username.c_str()); - printf("Username root means no change to UID, no matter what the UID is.\n"); - printf("This is %s version %s\n", argv[0], PACKAGE_VERSION); + printHelp(std::cout); + case 'v': + std::cout << "Library version: " PACKAGE_VERSION << std::endl; + std::cout << "Application version: " << getString("version") << std::endl; exit(1); break; + default: + for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){ + if (it->second.isMember("short") && it->second["short"].asString()[0] == opt){ + if (it->second.isMember("arg")){ + it->second["current"] = (std::string)optarg; + }else{ + it->second["current"] = 1; + } + break; + } + if (it->second.isMember("short_off") && it->second["short_off"].asString()[0] == opt){ + it->second["current"] = 0; + } + } + break; } }//commandline options parser + free(longOpts);//free the long options array + long_i = 1;//re-use long_i as an argument counter + while (optind < argc){//parse all remaining options, ignoring anything unexpected. + for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){ + if (it->second.isMember("arg_num") && it->second["arg_num"].asInt() == long_i){ + it->second["current"] = (std::string)argv[optind]; + optind++; + long_i++; + break; + } + } + } + if (long_i <= arg_count){ + std::cerr << "Usage error: missing argument(s)." << std::endl; + printHelp(std::cout); + exit(1); + } } +/// Returns a reference to the current value of an option or default if none was set. +/// If the option does not exist, this exits the application with a return code of 37. +JSON::Value & Util::Config::getOption(std::string optname){ + if (!vals.isMember(optname)){ + std::cout << "Fatal error: a non-existent option was accessed." << std::endl; + exit(37); + } + if (vals[optname].isMember("current")){ + return vals[optname]["current"]; + }else{ + return vals[optname]["default"]; + } +} + +/// Returns the current value of an option or default if none was set as a string. +/// Calls getOption internally. +std::string Util::Config::getString(std::string optname){ + return getOption(optname).asString(); +} + +/// Returns the current value of an option or default if none was set as a long long int. +/// Calls getOption internally. +long long int Util::Config::getInteger(std::string optname){ + return getOption(optname).asInt(); +} + +/// Returns the current value of an option or default if none was set as a bool. +/// Calls getOption internally. +bool Util::Config::getBool(std::string optname){ + return getOption(optname).asBool(); +} + +/// Activated the stored config. This will: +/// - Drop permissions to the stored "username". +/// - Daemonize the process if "daemonize" is true. +/// - Set is_active to true. +/// - Set up a signal handler to set is_active to false for the SIGINT, SIGHUP and SIGTERM signals. +void Util::Config::activate(){ + setUser(getString("username")); + if (getBool("daemonize")){Daemonize();} + struct sigaction new_action; + new_action.sa_handler = signal_handler; + sigemptyset (&new_action.sa_mask); + new_action.sa_flags = 0; + sigaction(SIGINT, &new_action, NULL); + sigaction(SIGHUP, &new_action, NULL); + sigaction(SIGTERM, &new_action, NULL); + sigaction(SIGPIPE, &new_action, NULL); + sigaction(SIGCHLD, &new_action, NULL); + is_active = true; +} + +/// Basic signal handler. Sets is_active to false if it receives +/// a SIGINT, SIGHUP or SIGTERM signal, reaps children for the SIGCHLD +/// signal, and ignores all other signals. +void Util::Config::signal_handler(int signum){ + switch (signum){ + case SIGINT://these three signals will set is_active to false. + case SIGHUP: + case SIGTERM: + is_active = false; + break; + case SIGCHLD://when a child dies, reap it. + wait(0); + break; + default: //other signals are ignored + break; + } +}//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["default"] = port; + addOption("listen_port", stored_port); + addOption("listen_interface", JSON::fromString("{\"long\":\"interface\", \"default\":\"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\", \"default\":\"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\", \"default\":1, \"long_off\":\"nodaemon\", \"short_off\":\"n\", \"help\":\"Whether or not to daemonize the process after starting.\"}")); +}//addConnectorOptions + /// Sets the current process' running user void Util::setUser(std::string username){ if (username != "root"){ diff --git a/lib/config.h b/lib/config.h index 04b9b732..5db09351 100644 --- a/lib/config.h +++ b/lib/config.h @@ -3,22 +3,31 @@ #pragma once #include - -#define STRINGIFY(x) #x -#define TOSTRING(x) STRINGIFY(x) +#include "json.h" /// Contains utility code, not directly related to streaming media namespace Util{ /// Deals with parsing configuration from commandline options. class Config{ + private: + JSON::Value vals; ///< Holds all current config values + int long_count; + static void signal_handler(int signum); public: - bool daemon_mode; - std::string interface; - int listen_port; - std::string username; - Config(); + //variables + static bool is_active; ///< Set to true by activate(), set to false by the signal handler. + //functions + 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); + JSON::Value & getOption(std::string optname); + std::string getString(std::string optname); + long long int getInteger(std::string optname); + bool getBool(std::string optname); + void activate(); + void addConnectorOptions(int port); }; /// Will set the active user to the named username.