summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/net.h7
-rw-r--r--src/daemon/main.c48
-rw-r--r--src/daemon/net.c6
-rw-r--r--src/daemon/proto_stdio.c43
-rw-r--r--src/daemon/purple.c7
5 files changed, 76 insertions, 35 deletions
diff --git a/include/net.h b/include/net.h
index 02767dc..8385943 100644
--- a/include/net.h
+++ b/include/net.h
@@ -22,7 +22,11 @@
#ifndef USURPATION_NET_H_INCLUDED
#define USURPATION_NET_H_INCLUDED
+#ifdef unix
+#include <sys/socket.h>
#include <netinet/in.h>
+#endif
+
#include <stddef.h>
#define MTU 1500
@@ -51,6 +55,9 @@ int net_close(int nd);
*/
int net_getlastdata(int nd, char ** const data);
int net_send(int nd, const char * const buf, size_t buf_size);
+
+#ifdef unix
int net_send_addr(int nd, const char * const buf, size_t buf_size, const struct sockaddr_in * const addr);
+#endif
#endif /* USURPATION_NET_H_INCLUDED */
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);
}
}