diff options
author | 2019-05-29 13:03:49 +0300 | |
---|---|---|
committer | 2019-05-29 13:03:49 +0300 | |
commit | 187fe61700fc97fff565ec53aac65c664042feae (patch) | |
tree | 498d0c00af96c4f666b4ef4dbb6db0966a485852 /src/daemon | |
parent | aef4eba5572d6b42f8ef2913ec41c0e778731960 (diff) | |
download | usurpation-187fe61700fc97fff565ec53aac65c664042feae.tar.gz usurpation-187fe61700fc97fff565ec53aac65c664042feae.tar.bz2 usurpation-187fe61700fc97fff565ec53aac65c664042feae.zip |
daemon: get ready to use messaging interfaces.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/daemon')
-rw-r--r-- | src/daemon/main.c | 48 | ||||
-rw-r--r-- | src/daemon/net.c | 6 | ||||
-rw-r--r-- | src/daemon/proto_stdio.c | 43 | ||||
-rw-r--r-- | src/daemon/purple.c | 7 |
4 files changed, 69 insertions, 35 deletions
diff --git a/src/daemon/main.c b/src/daemon/main.c index 94e59fd..37b1a51 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -23,31 +23,46 @@ #include <time.h> #include <stdlib.h> #include <stdio.h> +#include <string.h> #include "settings.h" #include "net.h" #include "purple.h" +#include "proto_stdio.h" static struct _state { int nd; -} _progstate; +} __progstate; -void cleanup(void); +static void cleanup(void); +static int __main_loop(const struct timespec * const iter_len); /* the logic is a placeholder right now */ int main(int argc, char **argv) { extern const char * const version; - struct timespec t = {3600, 0}; /* one hour */ + static const struct timespec t = {0, 250 * 1000 * 1000}; /* 250ms */ + char *proto; printf("Usurpation daemon version %s starting\n", version); atexit(cleanup); settings_init(); - _progstate.nd = net_init(setting_port()); - if (purple_init() && setting_verbose()) { - fprintf(stderr, "libpurple initialization failed\n"); + __progstate.nd = net_init(setting_port()); + proto = setting_im_proto(); + + if (strcmp(proto, "null")) { + if (purple_init() && setting_verbose()) { + fprintf(stderr, "libpurple initialization failed\n"); + } + } else { + if (proto_stdio_init() && setting_verbose()) { + fprintf(stderr, "libpurple initialization failed\n"); + } } + free(proto); + proto = NULL; + /* 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. @@ -56,17 +71,28 @@ int main(int argc, char **argv) daemon(0, 0); } - while(!(nanosleep(&t, NULL))) { - /* noop */ + return __main_loop(&t); +} + +/* TODO: semi-stub */ +static int __main_loop(const struct timespec * const iter_len) +{ + struct timespec t; + int ret = A_OK; + + t = *iter_len; + while (!ret) { + nanosleep(&t, NULL); } - return 0; + return ret; } -void cleanup(void) +static void cleanup(void) { purple_close(); - net_close(_progstate.nd); + proto_stdio_close(); + net_close(__progstate.nd); settings_cleanup(); } diff --git a/src/daemon/net.c b/src/daemon/net.c index 29f1fde..62172fe 100644 --- a/src/daemon/net.c +++ b/src/daemon/net.c @@ -165,7 +165,7 @@ int net_send(int nd, const char * const buf, size_t buf_size) ssize_t sent; struct timespec now; - if (nd > count || nd < 1 || state[--nd].available) { + if (nd > count || nd < 1 || state[--nd].available || state[nd].status != A_OK) { ret = ERROR; } else { if (setting_verbose() >= INFO) { @@ -250,10 +250,10 @@ static void *dolisten(void * state) if (oldstatus != st->status) { oldstatus = st->status; - if(st->status == DEAD) { + if (st->status == DEAD) { /* this timestamp is arbitraty */ setting_verbose() >= INFO ? fprintf(stderr, "Connection with the client has been lost. Last reply since: %li \n", st->lastreply.tv_sec) : 0; - } else { + } else if (st->status == A_OK) { ipstring = inet_ntoa(st->clientaddr.sin_addr); setting_verbose() >= INFO ? fprintf(stderr, "Successful incoming connection from %s\n", ipstring) : 0; } diff --git a/src/daemon/proto_stdio.c b/src/daemon/proto_stdio.c index f0a22a6..9aecdf0 100644 --- a/src/daemon/proto_stdio.c +++ b/src/daemon/proto_stdio.c @@ -27,32 +27,31 @@ void message_receive(char *arg) { int cancelstate; - int done = 0; - while (!done) { - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate); - pthread_mutex_lock(&state.out_m); - if (!state.writebuf) { - state.writebuf = strdup(arg); - done = 1; - } - pthread_mutex_unlock(&state.out_m); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate); - pthread_testcancel(); - } + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate); + pthread_mutex_lock(&state.out_m); + state.writebuf = strdup(arg); + pthread_mutex_unlock(&state.out_m); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate); + pthread_testcancel(); + nanosleep(&respite, NULL); } char *message_send(void) { - char *ret; + char *ret = NULL; int cancelstate; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate); pthread_mutex_lock(&state.out_m); - ret = strdup(state.readbuf); - free(state.readbuf); - state.readbuf = NULL; - state.readbufsize = 0; + + if (state.readbuf) { + ret = strdup(state.readbuf); + free(state.readbuf); + state.readbuf = NULL; + state.readbufsize = 0; + } + pthread_mutex_unlock(&state.out_m); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate); pthread_testcancel(); @@ -140,12 +139,16 @@ int proto_stdio_init(void) void proto_stdio_close(void) { - if (pthread_mutex_trylock(&state.mutex) == EBUSY) { + int status; + + status = pthread_mutex_trylock(&state.mutex); + + if (status == EBUSY) { pthread_cancel(state.stdio_in); pthread_cancel(state.stdio_out); pthread_join(state.stdio_in, NULL); pthread_join(state.stdio_out, NULL); + } else if (!status) { + pthread_mutex_unlock(&state.mutex); } - - pthread_mutex_unlock(&state.mutex); } diff --git a/src/daemon/purple.c b/src/daemon/purple.c index 882ea61..e004448 100644 --- a/src/daemon/purple.c +++ b/src/daemon/purple.c @@ -246,9 +246,14 @@ int purple_init(void) void purple_close(void) { + int status; - if (pthread_mutex_trylock(&state.mutex) == EBUSY) { + status = pthread_mutex_trylock(&state.mutex); + + if (status == EBUSY) { pthread_cancel(state.purple); pthread_join(state.purple, NULL); + } else if (!status) { + pthread_mutex_unlock(&state.mutex); } } |