Allow daemon mode

This commit is contained in:
vemax78 2013-06-16 12:09:57 +02:00
parent 7b6bd52f48
commit 99b6373cd7
11 changed files with 106 additions and 8 deletions

View File

@ -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: {

View File

@ -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"
];

View File

@ -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) {

View File

@ -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;

View File

@ -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);
}

View File

@ -12,9 +12,12 @@
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <sys/time.h>
#include <net/if.h>
#include <sys/stat.h>
#include <fcntl.h>
#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);

View File

@ -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__*/

View File

@ -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, ...) {

View File

@ -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

View File

@ -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 */

View File

@ -73,6 +73,7 @@ struct wtp_state {
/* WTP */
struct wtp_t {
int standalone;
int running;
struct wtp_state dfa;