Such style. (Code style unification)
This commit is contained in:
parent
57bcd8f25c
commit
8c01ec8897
57 changed files with 6548 additions and 6437 deletions
312
lib/config.cpp
312
lib/config.cpp
|
@ -37,7 +37,7 @@ bool Util::Config::is_active = false;
|
|||
unsigned int Util::Config::printDebugLevel = DEBUG;//
|
||||
std::string Util::Config::libver = PACKAGE_VERSION;
|
||||
|
||||
Util::Config::Config(){
|
||||
Util::Config::Config() {
|
||||
//global options here
|
||||
vals["debug"]["long"] = "debug";
|
||||
vals["debug"]["short"] = "g";
|
||||
|
@ -51,7 +51,7 @@ Util::Config::Config(){
|
|||
}
|
||||
|
||||
/// Creates a new configuration manager.
|
||||
Util::Config::Config(std::string cmd, std::string version){
|
||||
Util::Config::Config(std::string cmd, std::string version) {
|
||||
vals.null();
|
||||
long_count = 2;
|
||||
vals["cmd"]["value"].append(cmd);
|
||||
|
@ -67,7 +67,7 @@ Util::Config::Config(std::string cmd, std::string version){
|
|||
vals["debug"]["short"] = "g";
|
||||
vals["debug"]["arg"] = "integer";
|
||||
vals["debug"]["help"] = "The debug level at which messages need to be printed.";
|
||||
vals["debug"]["value"].append((long long)DEBUG);
|
||||
vals["debug"]["value"].append((long long)DEBUG);
|
||||
}
|
||||
|
||||
/// Adds an option to the configuration parser.
|
||||
|
@ -84,110 +84,110 @@ Util::Config::Config(std::string cmd, std::string version){
|
|||
/// "help":"Blahblahblah" //The helptext for this option.
|
||||
/// }
|
||||
///\endcode
|
||||
void Util::Config::addOption(std::string optname, JSON::Value option){
|
||||
void Util::Config::addOption(std::string optname, JSON::Value option) {
|
||||
vals[optname] = option;
|
||||
if ( !vals[optname].isMember("value") && vals[optname].isMember("default")){
|
||||
if (!vals[optname].isMember("value") && vals[optname].isMember("default")) {
|
||||
vals[optname]["value"].append(vals[optname]["default"]);
|
||||
vals[optname].removeMember("default");
|
||||
}
|
||||
long_count = 0;
|
||||
for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){
|
||||
if (it->second.isMember("long")){
|
||||
for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++) {
|
||||
if (it->second.isMember("long")) {
|
||||
long_count++;
|
||||
}
|
||||
if (it->second.isMember("long_off")){
|
||||
if (it->second.isMember("long_off")) {
|
||||
long_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prints a usage message to the given output.
|
||||
void Util::Config::printHelp(std::ostream & output){
|
||||
void Util::Config::printHelp(std::ostream & output) {
|
||||
unsigned int longest = 0;
|
||||
std::map<long long int, std::string> args;
|
||||
for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){
|
||||
for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++) {
|
||||
unsigned int current = 0;
|
||||
if (it->second.isMember("long")){
|
||||
if (it->second.isMember("long")) {
|
||||
current += it->second["long"].asString().size() + 4;
|
||||
}
|
||||
if (it->second.isMember("short")){
|
||||
if (it->second.isMember("short")) {
|
||||
current += it->second["short"].asString().size() + 3;
|
||||
}
|
||||
if (current > longest){
|
||||
if (current > longest) {
|
||||
longest = current;
|
||||
}
|
||||
current = 0;
|
||||
if (it->second.isMember("long_off")){
|
||||
if (it->second.isMember("long_off")) {
|
||||
current += it->second["long_off"].asString().size() + 4;
|
||||
}
|
||||
if (it->second.isMember("short_off")){
|
||||
if (it->second.isMember("short_off")) {
|
||||
current += it->second["short_off"].asString().size() + 3;
|
||||
}
|
||||
if (current > longest){
|
||||
if (current > longest) {
|
||||
longest = current;
|
||||
}
|
||||
if (it->second.isMember("arg_num")){
|
||||
if (it->second.isMember("arg_num")) {
|
||||
current = it->first.size() + 3;
|
||||
if (current > longest){
|
||||
if (current > longest) {
|
||||
longest = current;
|
||||
}
|
||||
args[it->second["arg_num"].asInt()] = it->first;
|
||||
}
|
||||
}
|
||||
output << "Usage: " << getString("cmd") << " [options]";
|
||||
for (std::map<long long int, std::string>::iterator i = args.begin(); i != args.end(); i++){
|
||||
if (vals[i->second].isMember("value") && vals[i->second]["value"].size()){
|
||||
for (std::map<long long int, std::string>::iterator i = args.begin(); i != args.end(); i++) {
|
||||
if (vals[i->second].isMember("value") && vals[i->second]["value"].size()) {
|
||||
output << " [" << i->second << "]";
|
||||
}else{
|
||||
} else {
|
||||
output << " " << i->second;
|
||||
}
|
||||
}
|
||||
output << std::endl << std::endl;
|
||||
for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){
|
||||
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")){
|
||||
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")){
|
||||
} else {
|
||||
if (it->second.isMember("long")) {
|
||||
f = "--" + it->second["long"].asString();
|
||||
}
|
||||
if (it->second.isMember("short")){
|
||||
if (it->second.isMember("short")) {
|
||||
f = "-" + it->second["short"].asString();
|
||||
}
|
||||
}
|
||||
while (f.size() < longest){
|
||||
while (f.size() < longest) {
|
||||
f.append(" ");
|
||||
}
|
||||
if (it->second.isMember("arg")){
|
||||
if (it->second.isMember("arg")) {
|
||||
output << f << "(" << it->second["arg"].asString() << ") " << it->second["help"].asString() << std::endl;
|
||||
}else{
|
||||
} 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")){
|
||||
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")){
|
||||
} else {
|
||||
if (it->second.isMember("long_off")) {
|
||||
f = "--" + it->second["long_off"].asString();
|
||||
}
|
||||
if (it->second.isMember("short_off")){
|
||||
if (it->second.isMember("short_off")) {
|
||||
f = "-" + it->second["short_off"].asString();
|
||||
}
|
||||
}
|
||||
while (f.size() < longest){
|
||||
while (f.size() < longest) {
|
||||
f.append(" ");
|
||||
}
|
||||
if (it->second.isMember("arg")){
|
||||
if (it->second.isMember("arg")) {
|
||||
output << f << "(" << it->second["arg"].asString() << ") " << it->second["help"].asString() << std::endl;
|
||||
}else{
|
||||
} else {
|
||||
output << f << it->second["help"].asString() << std::endl;
|
||||
}
|
||||
}
|
||||
if (it->second.isMember("arg_num")){
|
||||
if (it->second.isMember("arg_num")) {
|
||||
f = it->first;
|
||||
while (f.size() < longest){
|
||||
while (f.size() < longest) {
|
||||
f.append(" ");
|
||||
}
|
||||
output << f << "(" << it->second["arg"].asString() << ") " << it->second["help"].asString() << std::endl;
|
||||
|
@ -197,51 +197,51 @@ void Util::Config::printHelp(std::ostream & output){
|
|||
|
||||
/// Parses commandline arguments.
|
||||
/// Calls exit if an unknown option is encountered, printing a help message.
|
||||
bool 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));
|
||||
struct option * longOpts = (struct option *)calloc(long_count + 1, sizeof(struct option));
|
||||
int long_i = 0;
|
||||
int arg_count = 0;
|
||||
if (vals.size()){
|
||||
for (JSON::ObjIter it = vals.ObjBegin(); it != vals.ObjEnd(); it++){
|
||||
if (it->second.isMember("short")){
|
||||
if (vals.size()) {
|
||||
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")){
|
||||
if (it->second.isMember("arg")) {
|
||||
shortopts += ":";
|
||||
}
|
||||
}
|
||||
if (it->second.isMember("short_off")){
|
||||
if (it->second.isMember("short_off")) {
|
||||
shortopts += it->second["short_off"].asString();
|
||||
if (it->second.isMember("arg")){
|
||||
if (it->second.isMember("arg")) {
|
||||
shortopts += ":";
|
||||
}
|
||||
}
|
||||
if (it->second.isMember("long")){
|
||||
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")){
|
||||
if (it->second.isMember("arg")) {
|
||||
longOpts[long_i].has_arg = 1;
|
||||
}
|
||||
long_i++;
|
||||
}
|
||||
if (it->second.isMember("long_off")){
|
||||
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")){
|
||||
if (it->second.isMember("arg")) {
|
||||
longOpts[long_i].has_arg = 1;
|
||||
}
|
||||
long_i++;
|
||||
}
|
||||
if (it->second.isMember("arg_num") && !(it->second.isMember("value") && it->second["value"].size())){
|
||||
if (it->second["arg_num"].asInt() > arg_count){
|
||||
if (it->second.isMember("arg_num") && !(it->second.isMember("value") && it->second["value"].size())) {
|
||||
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){
|
||||
while ((opt = getopt_long(argc, argv, shortopts.c_str(), longOpts, 0)) != -1) {
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
case '?':
|
||||
printHelp(std::cout);
|
||||
|
@ -251,16 +251,16 @@ bool Util::Config::parseArgs(int & argc, char ** & argv){
|
|||
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")){
|
||||
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["value"].append((std::string)optarg);
|
||||
}else{
|
||||
} else {
|
||||
it->second["value"].append((long long int)1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (it->second.isMember("short_off") && it->second["short_off"].asString()[0] == opt){
|
||||
if (it->second.isMember("short_off") && it->second["short_off"].asString()[0] == opt) {
|
||||
it->second["value"].append((long long int)0);
|
||||
}
|
||||
}
|
||||
|
@ -269,9 +269,9 @@ bool Util::Config::parseArgs(int & argc, char ** & argv){
|
|||
} //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){
|
||||
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["value"].append((std::string)argv[optind]);
|
||||
break;
|
||||
}
|
||||
|
@ -279,7 +279,7 @@ bool Util::Config::parseArgs(int & argc, char ** & argv){
|
|||
optind++;
|
||||
long_i++;
|
||||
}
|
||||
if (long_i <= arg_count){
|
||||
if (long_i <= arg_count) {
|
||||
return false;
|
||||
}
|
||||
printDebugLevel = getInteger("debug");
|
||||
|
@ -288,17 +288,17 @@ bool Util::Config::parseArgs(int & argc, char ** & argv){
|
|||
|
||||
/// 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, bool asArray){
|
||||
if ( !vals.isMember(optname)){
|
||||
JSON::Value & Util::Config::getOption(std::string optname, bool asArray) {
|
||||
if (!vals.isMember(optname)) {
|
||||
std::cout << "Fatal error: a non-existent option '" << optname << "' was accessed." << std::endl;
|
||||
exit(37);
|
||||
}
|
||||
if ( !vals[optname].isMember("value") || !vals[optname]["value"].isArray()){
|
||||
if (!vals[optname].isMember("value") || !vals[optname]["value"].isArray()) {
|
||||
vals[optname]["value"].append(JSON::Value());
|
||||
}
|
||||
if (asArray){
|
||||
if (asArray) {
|
||||
return vals[optname]["value"];
|
||||
}else{
|
||||
} else {
|
||||
int n = vals[optname]["value"].size();
|
||||
return vals[optname]["value"][n - 1];
|
||||
}
|
||||
|
@ -306,30 +306,30 @@ JSON::Value & Util::Config::getOption(std::string optname, bool asArray){
|
|||
|
||||
/// 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){
|
||||
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){
|
||||
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){
|
||||
bool Util::Config::getBool(std::string optname) {
|
||||
return getOption(optname).asBool();
|
||||
}
|
||||
|
||||
struct callbackData{
|
||||
struct callbackData {
|
||||
Socket::Connection * sock;
|
||||
int (*cb)(Socket::Connection &);
|
||||
};
|
||||
|
||||
static void callThreadCallback(void * cDataArg){
|
||||
static void callThreadCallback(void * cDataArg) {
|
||||
DEBUG_MSG(DLVL_INSANE, "Thread for %p started", cDataArg);
|
||||
callbackData * cData = (callbackData*)cDataArg;
|
||||
callbackData * cData = (callbackData *)cDataArg;
|
||||
cData->cb(*(cData->sock));
|
||||
cData->sock->close();
|
||||
delete cData->sock;
|
||||
|
@ -337,19 +337,19 @@ static void callThreadCallback(void * cDataArg){
|
|||
DEBUG_MSG(DLVL_INSANE, "Thread for %p ended", cDataArg);
|
||||
}
|
||||
|
||||
int Util::Config::threadServer(Socket::Server & server_socket, int (*callback)(Socket::Connection &)){
|
||||
while (is_active && server_socket.connected()){
|
||||
int Util::Config::threadServer(Socket::Server & server_socket, int (*callback)(Socket::Connection &)) {
|
||||
while (is_active && server_socket.connected()) {
|
||||
Socket::Connection S = server_socket.accept();
|
||||
if (S.connected()){ //check if the new connection is valid
|
||||
if (S.connected()) { //check if the new connection is valid
|
||||
callbackData * cData = new callbackData;
|
||||
cData->sock = new Socket::Connection(S);
|
||||
cData->cb = callback;
|
||||
//spawn a new thread for this connection
|
||||
tthread::thread T(callThreadCallback, (void*)cData);
|
||||
tthread::thread T(callThreadCallback, (void *)cData);
|
||||
//detach it, no need to keep track of it anymore
|
||||
T.detach();
|
||||
DEBUG_MSG(DLVL_HIGH, "Spawned new thread for socket %i", S.getSocket());
|
||||
}else{
|
||||
} else {
|
||||
Util::sleep(10); //sleep 10ms
|
||||
}
|
||||
}
|
||||
|
@ -357,19 +357,19 @@ int Util::Config::threadServer(Socket::Server & server_socket, int (*callback)(S
|
|||
return 0;
|
||||
}
|
||||
|
||||
int Util::Config::forkServer(Socket::Server & server_socket, int (*callback)(Socket::Connection &)){
|
||||
while (is_active && server_socket.connected()){
|
||||
int Util::Config::forkServer(Socket::Server & server_socket, int (*callback)(Socket::Connection &)) {
|
||||
while (is_active && server_socket.connected()) {
|
||||
Socket::Connection S = server_socket.accept();
|
||||
if (S.connected()){ //check if the new connection is valid
|
||||
if (S.connected()) { //check if the new connection is valid
|
||||
pid_t myid = fork();
|
||||
if (myid == 0){ //if new child, start MAINHANDLER
|
||||
if (myid == 0) { //if new child, start MAINHANDLER
|
||||
server_socket.drop();
|
||||
return callback(S);
|
||||
}else{ //otherwise, do nothing or output debugging text
|
||||
} else { //otherwise, do nothing or output debugging text
|
||||
DEBUG_MSG(DLVL_HIGH, "Forked new process %i for socket %i", (int)myid, S.getSocket());
|
||||
S.drop();
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
Util::sleep(10); //sleep 10ms
|
||||
}
|
||||
}
|
||||
|
@ -377,15 +377,15 @@ int Util::Config::forkServer(Socket::Server & server_socket, int (*callback)(Soc
|
|||
return 0;
|
||||
}
|
||||
|
||||
int Util::Config::serveThreadedSocket(int (*callback)(Socket::Connection &)){
|
||||
int Util::Config::serveThreadedSocket(int (*callback)(Socket::Connection &)) {
|
||||
Socket::Server server_socket;
|
||||
if (vals.isMember("socket")){
|
||||
if (vals.isMember("socket")) {
|
||||
server_socket = Socket::Server(Util::getTmpFolder() + getString("socket"));
|
||||
}
|
||||
if (vals.isMember("listen_port") && vals.isMember("listen_interface")){
|
||||
if (vals.isMember("listen_port") && vals.isMember("listen_interface")) {
|
||||
server_socket = Socket::Server(getInteger("listen_port"), getString("listen_interface"), false);
|
||||
}
|
||||
if (!server_socket.connected()){
|
||||
if (!server_socket.connected()) {
|
||||
DEBUG_MSG(DLVL_DEVEL, "Failure to open socket");
|
||||
return 1;
|
||||
}
|
||||
|
@ -394,15 +394,15 @@ int Util::Config::serveThreadedSocket(int (*callback)(Socket::Connection &)){
|
|||
return threadServer(server_socket, callback);
|
||||
}
|
||||
|
||||
int Util::Config::serveForkedSocket(int (*callback)(Socket::Connection & S)){
|
||||
int Util::Config::serveForkedSocket(int (*callback)(Socket::Connection & S)) {
|
||||
Socket::Server server_socket;
|
||||
if (vals.isMember("socket")){
|
||||
if (vals.isMember("socket")) {
|
||||
server_socket = Socket::Server(Util::getTmpFolder() + getString("socket"));
|
||||
}
|
||||
if (vals.isMember("listen_port") && vals.isMember("listen_interface")){
|
||||
if (vals.isMember("listen_port") && vals.isMember("listen_interface")) {
|
||||
server_socket = Socket::Server(getInteger("listen_port"), getString("listen_interface"), false);
|
||||
}
|
||||
if (!server_socket.connected()){
|
||||
if (!server_socket.connected()) {
|
||||
DEBUG_MSG(DLVL_DEVEL, "Failure to open socket");
|
||||
return 1;
|
||||
}
|
||||
|
@ -416,21 +416,21 @@ int Util::Config::serveForkedSocket(int (*callback)(Socket::Connection & S)){
|
|||
/// - Daemonize the process if "daemonize" exists and 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(){
|
||||
if (vals.isMember("username")){
|
||||
void Util::Config::activate() {
|
||||
if (vals.isMember("username")) {
|
||||
setUser(getString("username"));
|
||||
}
|
||||
if (vals.isMember("daemonize") && getBool("daemonize")){
|
||||
if(vals.isMember("logfile") && getString("logfile") != ""){
|
||||
if (vals.isMember("daemonize") && getBool("daemonize")) {
|
||||
if (vals.isMember("logfile") && getString("logfile") != "") {
|
||||
Daemonize(true);
|
||||
}else{
|
||||
} else {
|
||||
Daemonize(false);
|
||||
}
|
||||
}
|
||||
struct sigaction new_action;
|
||||
struct sigaction cur_action;
|
||||
new_action.sa_handler = signal_handler;
|
||||
sigemptyset( &new_action.sa_mask);
|
||||
sigemptyset(&new_action.sa_mask);
|
||||
new_action.sa_flags = 0;
|
||||
sigaction(SIGINT, &new_action, NULL);
|
||||
sigaction(SIGHUP, &new_action, NULL);
|
||||
|
@ -438,8 +438,8 @@ void Util::Config::activate(){
|
|||
sigaction(SIGPIPE, &new_action, NULL);
|
||||
//check if a child signal handler isn't set already, if so, set it.
|
||||
sigaction(SIGCHLD, 0, &cur_action);
|
||||
if (cur_action.sa_handler == SIG_DFL || cur_action.sa_handler == SIG_IGN){
|
||||
sigaction(SIGCHLD, &new_action, NULL);
|
||||
if (cur_action.sa_handler == SIG_DFL || cur_action.sa_handler == SIG_IGN) {
|
||||
sigaction(SIGCHLD, &new_action, NULL);
|
||||
}
|
||||
is_active = true;
|
||||
}
|
||||
|
@ -447,24 +447,24 @@ void Util::Config::activate(){
|
|||
/// 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){
|
||||
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.
|
||||
int status;
|
||||
pid_t ret = -1;
|
||||
while (ret != 0){
|
||||
ret = waitpid( -1, &status, WNOHANG);
|
||||
if (ret < 0 && errno != EINTR){
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SIGCHLD: { //when a child dies, reap it.
|
||||
int status;
|
||||
pid_t ret = -1;
|
||||
while (ret != 0) {
|
||||
ret = waitpid(-1, &status, WNOHANG);
|
||||
if (ret < 0 && errno != EINTR) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: //other signals are ignored
|
||||
break;
|
||||
}
|
||||
|
@ -472,7 +472,7 @@ void Util::Config::signal_handler(int signum){
|
|||
|
||||
/// 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){
|
||||
void Util::Config::addConnectorOptions(int port, JSON::Value & capabilities) {
|
||||
JSON::Value option;
|
||||
option.null();
|
||||
option["long"] = "port";
|
||||
|
@ -482,11 +482,11 @@ void Util::Config::addConnectorOptions(int port, JSON::Value & capabilities){
|
|||
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"]["help"] = "TCP port to listen on - default if unprovided is " + option["value"][0u].asString();
|
||||
capabilities["optional"]["port"]["type"] = "uint";
|
||||
capabilities["optional"]["port"]["option"] = "--port";
|
||||
capabilities["optional"]["port"]["default"] = option["value"][0u];
|
||||
|
||||
|
||||
option.null();
|
||||
option["long"] = "interface";
|
||||
option["short"] = "i";
|
||||
|
@ -498,12 +498,12 @@ void Util::Config::addConnectorOptions(int port, JSON::Value & capabilities){
|
|||
capabilities["optional"]["interface"]["help"] = "Address of the interface to listen on - default if unprovided is all interfaces";
|
||||
capabilities["optional"]["interface"]["option"] = "--interface";
|
||||
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){
|
||||
void Util::Config::addBasicConnectorOptions(JSON::Value & capabilities) {
|
||||
JSON::Value option;
|
||||
option.null();
|
||||
option["long"] = "username";
|
||||
|
@ -518,7 +518,7 @@ void Util::Config::addBasicConnectorOptions(JSON::Value & capabilities){
|
|||
capabilities["optional"]["username"]["type"] = "str";
|
||||
|
||||
|
||||
if (capabilities.isMember("socket")){
|
||||
if (capabilities.isMember("socket")) {
|
||||
option.null();
|
||||
option["arg"] = "string";
|
||||
option["help"] = "Socket name that can be connected to for this connector.";
|
||||
|
@ -534,7 +534,7 @@ void Util::Config::addBasicConnectorOptions(JSON::Value & capabilities){
|
|||
option["help"] = "Whether or not to daemonize the process after starting.";
|
||||
option["value"].append(0ll);
|
||||
addOption("daemonize", option);
|
||||
|
||||
|
||||
option.null();
|
||||
option["long"] = "json";
|
||||
option["short"] = "j";
|
||||
|
@ -546,29 +546,29 @@ void Util::Config::addBasicConnectorOptions(JSON::Value & capabilities){
|
|||
|
||||
|
||||
/// Gets directory the current executable is stored in.
|
||||
std::string Util::getMyPath(){
|
||||
std::string Util::getMyPath() {
|
||||
char mypath[500];
|
||||
#ifdef __CYGWIN__
|
||||
#ifdef __CYGWIN__
|
||||
GetModuleFileName(0, mypath, 500);
|
||||
#else
|
||||
#ifdef __APPLE__
|
||||
memset( mypath, 0, 500);
|
||||
#else
|
||||
#ifdef __APPLE__
|
||||
memset(mypath, 0, 500);
|
||||
unsigned int refSize = 500;
|
||||
int ret = _NSGetExecutablePath(mypath,&refSize);
|
||||
#else
|
||||
int ret = _NSGetExecutablePath(mypath, &refSize);
|
||||
#else
|
||||
int ret = readlink("/proc/self/exe", mypath, 500);
|
||||
if (ret != -1){
|
||||
if (ret != -1) {
|
||||
mypath[ret] = 0;
|
||||
}else{
|
||||
} else {
|
||||
mypath[0] = 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
std::string tPath = mypath;
|
||||
size_t slash = tPath.rfind('/');
|
||||
if (slash == std::string::npos){
|
||||
if (slash == std::string::npos) {
|
||||
slash = tPath.rfind('\\');
|
||||
if (slash == std::string::npos){
|
||||
if (slash == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
@ -577,46 +577,48 @@ std::string Util::getMyPath(){
|
|||
}
|
||||
|
||||
/// Gets all executables in getMyPath that start with "Mist".
|
||||
void Util::getMyExec(std::deque<std::string> & execs){
|
||||
void Util::getMyExec(std::deque<std::string> & execs) {
|
||||
std::string path = Util::getMyPath();
|
||||
#ifdef __CYGWIN__
|
||||
#ifdef __CYGWIN__
|
||||
path += "\\Mist*";
|
||||
WIN32_FIND_DATA FindFileData;
|
||||
HANDLE hdl = FindFirstFile(path.c_str(), &FindFileData);
|
||||
while (hdl != INVALID_HANDLE_VALUE){
|
||||
while (hdl != INVALID_HANDLE_VALUE) {
|
||||
execs.push_back(FindFileData.cFileName);
|
||||
if (!FindNextFile(hdl, &FindFileData)){
|
||||
if (!FindNextFile(hdl, &FindFileData)) {
|
||||
FindClose(hdl);
|
||||
hdl = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#else
|
||||
DIR * d = opendir(path.c_str());
|
||||
if (!d){return;}
|
||||
struct dirent *dp;
|
||||
if (!d) {
|
||||
return;
|
||||
}
|
||||
struct dirent * dp;
|
||||
do {
|
||||
errno = 0;
|
||||
if ((dp = readdir(d))){
|
||||
if (strncmp(dp->d_name, "Mist", 4) == 0){
|
||||
if ((dp = readdir(d))) {
|
||||
if (strncmp(dp->d_name, "Mist", 4) == 0) {
|
||||
execs.push_back(dp->d_name);
|
||||
}
|
||||
}
|
||||
} while (dp != NULL);
|
||||
closedir(d);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Sets the current process' running user
|
||||
void Util::setUser(std::string username){
|
||||
if (username != "root"){
|
||||
void Util::setUser(std::string username) {
|
||||
if (username != "root") {
|
||||
struct passwd * user_info = getpwnam(username.c_str());
|
||||
if ( !user_info){
|
||||
if (!user_info) {
|
||||
DEBUG_MSG(DLVL_ERROR, "Error: could not setuid %s: could not get PID", username.c_str());
|
||||
return;
|
||||
}else{
|
||||
if (setuid(user_info->pw_uid) != 0){
|
||||
} else {
|
||||
if (setuid(user_info->pw_uid) != 0) {
|
||||
DEBUG_MSG(DLVL_ERROR, "Error: could not setuid %s: not allowed", username.c_str());
|
||||
}else{
|
||||
} else {
|
||||
DEBUG_MSG(DLVL_DEVEL, "Change user to %s", username.c_str());
|
||||
}
|
||||
}
|
||||
|
@ -627,13 +629,13 @@ void Util::setUser(std::string username){
|
|||
/// Works by calling daemon(1,0):
|
||||
/// Does not change directory to root.
|
||||
/// Does redirect output to /dev/null
|
||||
void Util::Daemonize(bool notClose){
|
||||
void Util::Daemonize(bool notClose) {
|
||||
DEBUG_MSG(DLVL_DEVEL, "Going into background mode...");
|
||||
int noClose = 0;
|
||||
if(notClose){
|
||||
if (notClose) {
|
||||
noClose = 1;
|
||||
}
|
||||
if (daemon(1, noClose) < 0){
|
||||
if (daemon(1, noClose) < 0) {
|
||||
DEBUG_MSG(DLVL_ERROR, "Failed to daemonize: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue