From 99b6373cd732c352cb8060441bf3437a8a08444a Mon Sep 17 00:00:00 2001 From: vemax78 Date: Sun, 16 Jun 2013 12:09:57 +0200 Subject: [PATCH] Allow daemon mode --- conf/ac.conf | 6 ++++-- conf/wtp.conf | 6 ++++-- src/ac/ac.c | 18 +++++++++++++++++- src/ac/ac.h | 3 ++- src/common/capwap.c | 38 +++++++++++++++++++++++++++++++++++++ src/common/capwap.h | 6 ++++++ src/common/capwap_error.h | 1 + src/common/capwap_logging.c | 14 ++++++++++++++ src/common/capwap_logging.h | 1 + src/wtp/wtp.c | 20 +++++++++++++++++-- src/wtp/wtp.h | 1 + 11 files changed, 106 insertions(+), 8 deletions(-) diff --git a/conf/ac.conf b/conf/ac.conf index 62a9f92..6b32013 100755 --- a/conf/ac.conf +++ b/conf/ac.conf @@ -3,6 +3,8 @@ version = "1.0"; application: { + standalone = false; + name = "ac 1"; binding = [ @@ -51,8 +53,8 @@ application: { presharedkey: { hint = "esempio"; - identity = "prova"; - pskkey = "123456"; + identity = "prova"; + pskkey = "123456"; }; x509: { diff --git a/conf/wtp.conf b/conf/wtp.conf index 6eb341f..94b17f3 100755 --- a/conf/wtp.conf +++ b/conf/wtp.conf @@ -3,6 +3,8 @@ version = "1.0"; application: { + standalone = false; + name = "wtp 1"; location = "Ufficio"; @@ -79,7 +81,7 @@ application: { network: { #binding = "eth1"; mtu = 1500; - #port = 10000; + transport = "udp"; ipv4 = true; @@ -88,7 +90,7 @@ application: { }; acdiscovery: { - search = false; + search = true; host = [ "127.0.0.1" ]; diff --git a/src/ac/ac.c b/src/ac/ac.c index df22935..8ccb5bd 100644 --- a/src/ac/ac.c +++ b/src/ac/ac.c @@ -16,6 +16,9 @@ static char g_configurationfile[260] = AC_DEFAULT_CONFIGURATION_FILE; /* Alloc AC */ static int ac_init(void) { + /* */ + g_ac.standalone = 1; + /* Network */ capwap_network_init(&g_ac.net); g_ac.mtu = CAPWAP_MTU_DEFAULT; @@ -137,7 +140,7 @@ static int ac_parsing_configuration_1_0(config_t* config) { if (configSetting != NULL) { int count = config_setting_length(configSetting); - /* Disable output interface */ + /* Disable output interface */ capwap_logging_disable_allinterface(); /* Enable selected interface */ @@ -158,6 +161,11 @@ static int ac_parsing_configuration_1_0(config_t* config) { } } + /* Set running mode */ + if (config_lookup_bool(config, "application.standalone", &configInt) == CONFIG_TRUE) { + g_ac.standalone = ((configInt != 0) ? 1 : 0); + } + /* Set name of AC */ if (config_lookup_string(config, "application.name", &configString) == CONFIG_TRUE) { if (strlen(configString) > CAPWAP_ACNAME_MAXLENGTH) { @@ -690,6 +698,14 @@ int main(int argc, char** argv) { if (value < 0) { result = AC_ERROR_LOAD_CONFIGURATION; } else if (value > 0) { + if (!g_ac.standalone) { + capwap_daemon(); + + /* Console logging is disabled in daemon mode */ + capwap_logging_disable_console(); + capwap_logging_info("Running AC in daemon mode"); + } + /* Complete configuration AC */ result = ac_configure(); if (result == CAPWAP_SUCCESSFUL) { diff --git a/src/ac/ac.h b/src/ac/ac.h index c19a6f7..a29edb4 100644 --- a/src/ac/ac.h +++ b/src/ac/ac.h @@ -83,8 +83,9 @@ struct ac_data_session_handshake { /* AC */ struct ac_t { + int standalone; int running; - + /* */ struct ac_state dfa; struct capwap_network net; diff --git a/src/common/capwap.c b/src/common/capwap.c index c5b20c7..16db157 100644 --- a/src/common/capwap.c +++ b/src/common/capwap.c @@ -160,3 +160,41 @@ void* capwap_clone(void* buffer, int buffersize) { return memcpy(bufferclone, buffer, buffersize); } + +/* */ +void capwap_daemon(void) { + int fd; + pid_t pid; + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) { + capwap_exit(CAPWAP_DAEMON_ERROR); + } else if (pid > 0) { + capwap_exit(CAPWAP_SUCCESSFUL); + } + + /* Change the file mode mask */ + umask(0); + + /* Create a new SID for the child process */ + if (setsid() < 0) { + capwap_exit(CAPWAP_DAEMON_ERROR); + } + + /* Change the current working directory */ + if ((chdir("/")) < 0) { + capwap_exit(CAPWAP_DAEMON_ERROR); + } + + /* Redirect the standard file descriptors to /dev/null */ + fd = open("/dev/null", 0); + if (fd == -1) { + capwap_exit(CAPWAP_DAEMON_ERROR); + } + + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + close(fd); +} diff --git a/src/common/capwap.h b/src/common/capwap.h index ede3185..6352dc8 100644 --- a/src/common/capwap.h +++ b/src/common/capwap.h @@ -12,9 +12,12 @@ #include #include #include +#include #include #include #include +#include +#include #ifdef HAVE_CONFIG_H #include "config.h" @@ -92,6 +95,9 @@ void capwap_killall_timeout(struct timeout_control* timeout); int capwap_is_enable_timeout(struct timeout_control* timeout, unsigned long index); int capwap_is_timeout(struct timeout_control* timeout, unsigned long index); +/* */ +void capwap_daemon(void); + /* */ #define capwap_outofmemory() capwap_logging_fatal("Out of memory %s(%d)", __FILE__, __LINE__); \ capwap_exit(CAPWAP_OUT_OF_MEMORY); diff --git a/src/common/capwap_error.h b/src/common/capwap_error.h index 28ac124..2c4c2ba 100644 --- a/src/common/capwap_error.h +++ b/src/common/capwap_error.h @@ -6,5 +6,6 @@ #define CAPWAP_OUT_OF_MEMORY -2 #define CAPWAP_REQUEST_ROOT -3 #define CAPWAP_CRYPT_ERROR -4 +#define CAPWAP_DAEMON_ERROR -5 #endif /* __CAPWAP_ERROR_HEADER__*/ diff --git a/src/common/capwap_logging.c b/src/common/capwap_logging.c index 63af04e..8cf69f6 100644 --- a/src/common/capwap_logging.c +++ b/src/common/capwap_logging.c @@ -81,6 +81,20 @@ void capwap_logging_enable_console(int error) { #endif } +/* */ +void capwap_logging_disable_console(void) { +#ifdef CAPWAP_MULTITHREADING_ENABLE + capwap_lock_enter(&l_loglock); +#endif + + loggingoutputstdout = 0; + loggingoutputstderr = 0; + +#ifdef CAPWAP_MULTITHREADING_ENABLE + capwap_lock_exit(&l_loglock); +#endif +} + /* */ #ifdef ENABLE_LOGGING void capwap_logging_printf(int level, const char* format, ...) { diff --git a/src/common/capwap_logging.h b/src/common/capwap_logging.h index ddf402a..bfab662 100644 --- a/src/common/capwap_logging.h +++ b/src/common/capwap_logging.h @@ -19,6 +19,7 @@ void capwap_logging_verboselevel(unsigned int level); /* */ void capwap_logging_disable_allinterface(); void capwap_logging_enable_console(int error); +void capwap_logging_disable_console(void); /* */ #ifdef ENABLE_LOGGING diff --git a/src/wtp/wtp.c b/src/wtp/wtp.c index 5188a1a..eeb842c 100644 --- a/src/wtp/wtp.c +++ b/src/wtp/wtp.c @@ -24,6 +24,9 @@ static int wtp_init(void) { /* Init WTP with default value */ memset(&g_wtp, 0, sizeof(struct wtp_t)); + /* Standard running mode is standalone */ + g_wtp.standalone = 1; + /* Standard name */ g_wtp.name.name = (uint8_t*)capwap_duplicate_string(WTP_STANDARD_NAME); g_wtp.location.value = (uint8_t*)capwap_duplicate_string(WTP_STANDARD_LOCATION); @@ -216,7 +219,7 @@ static int wtp_parsing_configuration_1_0(config_t* config) { if (configSetting != NULL) { int count = config_setting_length(configSetting); - /* Disable output interface */ + /* Disable output interface */ capwap_logging_disable_allinterface(); /* Enable selected interface */ @@ -237,6 +240,11 @@ static int wtp_parsing_configuration_1_0(config_t* config) { } } + /* Set running mode */ + if (config_lookup_bool(config, "application.standalone", &configInt) == CONFIG_TRUE) { + g_wtp.standalone = ((configInt != 0) ? 1 : 0); + } + /* Set name of WTP */ if (config_lookup_string(config, "application.name", &configString) == CONFIG_TRUE) { if (strlen(configString) > CAPWAP_WTPNAME_MAXLENGTH) { @@ -1014,7 +1022,7 @@ int main(int argc, char** argv) { result = CAPWAP_CRYPT_ERROR; capwap_logging_fatal("Error to init crypt engine"); } else { - /* Alloc a WTP */ + /* Init WTP */ if (!wtp_init()) { result = WTP_ERROR_SYSTEM_FAILER; capwap_logging_fatal("Error to init WTP engine"); @@ -1025,6 +1033,14 @@ int main(int argc, char** argv) { result = WTP_ERROR_LOAD_CONFIGURATION; capwap_logging_fatal("Error to load configuration"); } else if (value > 0) { + if (!g_wtp.standalone) { + capwap_daemon(); + + /* Console logging is disabled in daemon mode */ + capwap_logging_disable_console(); + capwap_logging_info("Running WTP in daemon mode"); + } + capwap_logging_info("Startup WTP"); /* Start WTP */ diff --git a/src/wtp/wtp.h b/src/wtp/wtp.h index 53580c0..96556a8 100644 --- a/src/wtp/wtp.h +++ b/src/wtp/wtp.h @@ -73,6 +73,7 @@ struct wtp_state { /* WTP */ struct wtp_t { + int standalone; int running; struct wtp_state dfa;