summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/daemon/proto_stdio.c116
-rw-r--r--src/daemon/proto_stdio_private.h2
2 files changed, 77 insertions, 41 deletions
diff --git a/src/daemon/proto_stdio.c b/src/daemon/proto_stdio.c
index 533dc76..f0a22a6 100644
--- a/src/daemon/proto_stdio.c
+++ b/src/daemon/proto_stdio.c
@@ -26,59 +26,93 @@
void message_receive(char *arg)
{
- int done = 0;
- while(!done)
- {
- pthread_mutex_lock(&state.out_m);
- if(!state.writebuf)
- {
- state.writebuf = strdup(arg);
- done = 1;
- }
- pthread_mutex_unlock(&state.out_m);
- }
+ 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();
+ }
}
-char * message_send(void)
+char *message_send(void)
{
- char * ret;
- pthread_mutex_lock(&state.out_m);
- ret = strdup(state.writebuf);
- free(state.writebuf);
- state.writebuf = NULL;
- pthread_mutex_unlock(&state.out_m);
- return ret;
+ char *ret;
+ 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;
+ pthread_mutex_unlock(&state.out_m);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
+ pthread_testcancel();
+
+ return ret;
}
static void *read_stdin(void *arg)
{
- while(1)
- {
- pthread_mutex_lock(&state.in_m);
- if(!state.readbuf)
- {
- state.readbufsize = getline(&state.readbuf,NULL, stdin);
- }
- pthread_mutex_unlock(&state.in_m);
- }
- return NULL;
+ int cancelstate;
+
+ (void) arg;
+
+ while(1) {
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
+ pthread_mutex_lock(&state.in_m);
+
+ if (!state.readbuf) {
+ state.readbufsize = getline(&state.readbuf, NULL, stdin);
+ }
+
+ pthread_mutex_unlock(&state.in_m);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
+ pthread_testcancel();
+ nanosleep(&respite, NULL);
+ }
+
+ return NULL;
}
static void *write_stdout(void *arg)
{
- while(1)
- {
- pthread_mutex_lock(&state.out_m);
- if(state.writebuf)
- {
- printf("%s", state.writebuf);
- free(state.writebuf);
- state.writebuf = NULL;
- }
- pthread_mutex_unlock(&state.out_m);
- }
- return NULL;
+ int cancelstate;
+
+ (void) arg;
+
+ while(1) {
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
+ pthread_mutex_lock(&state.out_m);
+
+ if (state.writebuf) {
+ if (state.writebuf[strlen(state.writebuf) - 1] == '\n') {
+ printf("%s", state.writebuf);
+ } else {
+ printf("%s\n", state.writebuf);
+ }
+
+ free(state.writebuf);
+ state.writebuf = NULL;
+ state.writebufsize = 0;
+ }
+
+ pthread_mutex_unlock(&state.out_m);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
+ pthread_testcancel();
+ nanosleep(&respite, NULL);
+ }
+
+ return NULL;
}
diff --git a/src/daemon/proto_stdio_private.h b/src/daemon/proto_stdio_private.h
index 4b0ab6f..209d191 100644
--- a/src/daemon/proto_stdio_private.h
+++ b/src/daemon/proto_stdio_private.h
@@ -42,4 +42,6 @@ static struct state {
static void *read_stdin(void *arg);
static void *write_stdout(void *arg);
+static const struct timespec respite = {0, 10 * 1000 * 1000}; /* 10ms */
+
#endif /* USURPATION_PROTO_STDIO_PRIVATE_H */