From de18684a25cdc1e310636c5f74d4e901881b5710 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Fri, 29 Mar 2019 14:01:49 +0200 Subject: daemon: style adjustment Fix style inconsistencies and move port settings to settings.c. Part of Ticket 3. Signed-off-by: Gediminas Jakutis --- include/settings.h | 2 ++ src/daemon/main.c | 4 +++- src/daemon/net.c | 24 ++++++++++++------------ src/daemon/settings.c | 21 ++++++++++++++++----- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/include/settings.h b/include/settings.h index cdb30e8..f4bb914 100644 --- a/include/settings.h +++ b/include/settings.h @@ -21,6 +21,8 @@ #ifndef USURPATION_SETTINGS_H #define USURPATION_SETTINGS_H +void settings_init(void); int setting_detach(void); +unsigned short int setting_port(void); #endif /* USURPATION_SETTINGS_H */ diff --git a/src/daemon/main.c b/src/daemon/main.c index a729f84..302a9ce 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -33,7 +33,9 @@ int main(int argc, char **argv) printf("Usurpation daemon version %s starting\n", version); - net_init(6996); // To do: get port from settings. + settings_init(); + net_init(setting_port()); /* TODO: get port from settings. */ + /* by default and if running by as a system service, the init system * needs to keep control of the process and thus only detach if * requested when ran manually by a user. diff --git a/src/daemon/net.c b/src/daemon/net.c index 2969d9b..27375e3 100644 --- a/src/daemon/net.c +++ b/src/daemon/net.c @@ -185,18 +185,18 @@ static void *dolisten(void * state) } else { st->status = A_OK; } - - if (oldstatus != st->status) - { - oldstatus = st->status; - if(st->status == DEAD) - /* this timestamp is arbitraty */ - fprintf(stderr, "Connection with the client has been lost. Last reply since: %li \n", st->lastreply.tv_sec); - else{ - ipstring = inet_ntoa(clientaddr.sin_addr); - fprintf(stderr, "Successful incoming connection from %s\n", ipstring); - } - } + + if (oldstatus != st->status) + { + oldstatus = st->status; + if(st->status == DEAD) { + /* this timestamp is arbitraty */ + fprintf(stderr, "Connection with the client has been lost. Last reply since: %li \n", st->lastreply.tv_sec); + } else { + ipstring = inet_ntoa(clientaddr.sin_addr); + fprintf(stderr, "Successful incoming connection from %s\n", ipstring); + } + } pthread_mutex_unlock(&st->datamutex); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate); pthread_testcancel(); diff --git a/src/daemon/settings.c b/src/daemon/settings.c index 6796ac1..3777f23 100644 --- a/src/daemon/settings.c +++ b/src/daemon/settings.c @@ -22,14 +22,25 @@ static struct settings { int daemonize; + unsigned short int port; } settings; -int setting_detach(void) -{ - /* should be, in this order: looking at options provided via command +/* TODO: should be, in this order: looking at options provided via command * line arguments, looking at at /etc/conf.d/usurpation.conf or - * using default. - * Right now just uses default. + * using default. Right now just uses defaults. */ +void settings_init(void) +{ + settings.daemonize =0; + settings.port = 6996 +} + +int setting_detach(void) +{ return settings.daemonize; } + +unsigned short int setting_port(void) +{ + return settings.port; +} -- cgit v1.2.3