From 1110e851faa51e1cbd22be8a921f283ddb97ff77 Mon Sep 17 00:00:00 2001 From: Tom Balzer Date: Sun, 1 Jul 2018 20:59:13 -0500 Subject: [PATCH] Fixed inconsistent socket io. --- Makefile | 4 +- include/macros.h | 4 +- src/{tomc/socket_read.c => common/socketio.c} | 103 ++++++++++++------ src/tomc/main.c | 23 ++-- src/tomd/main.c | 31 ++++-- 5 files changed, 105 insertions(+), 60 deletions(-) rename src/{tomc/socket_read.c => common/socketio.c} (51%) diff --git a/Makefile b/Makefile index a785462..88957fe 100644 --- a/Makefile +++ b/Makefile @@ -8,5 +8,5 @@ tomd:./src/tomd/main.c ./src/common/guile_helpers.c -I/usr/include/guile/2.0 \ -lguile-2.0 -tomc:./src/tomc/main.c ./src/tomc/socket_read.c - gcc -o tomc ./src/tomc/main.c ./src/tomc/socket_read.c +tomc:./src/tomc/main.c ./src/common/socketio.c + gcc -o tomc ./src/tomc/main.c ./src/common/socketio.c diff --git a/include/macros.h b/include/macros.h index 75a38b8..ef193e8 100644 --- a/include/macros.h +++ b/include/macros.h @@ -1,8 +1,8 @@ #ifndef _MACROS_H #define _MACROS_H -#define tomc_p(...) {printf("[tomc] "); printf(__VA_ARGS__); printf("\n");} -#define tomd_p(...) {printf("[tomd] "); printf(__VA_ARGS__); printf("\n");} +#define tomc_p(...) {printf("[tomc] "); printf(__VA_ARGS__); printf("\n");} +#define tomd_p(...) {printf("[tomd] "); printf(__VA_ARGS__); printf("\n");} #define SCM_ARR(arr, index) (scm_list_ref(arr, scm_from_int(index))) #define SCM_LIST_LEN(list) (scm_to_int(scm_length(list))) #define WRAP_SCM_FUNCTION_1(module, scm_name, name) \ diff --git a/src/tomc/socket_read.c b/src/common/socketio.c similarity index 51% rename from src/tomc/socket_read.c rename to src/common/socketio.c index cc165de..3a362bd 100644 --- a/src/tomc/socket_read.c +++ b/src/common/socketio.c @@ -41,7 +41,7 @@ void setup_socket(int sfd) options.buf = malloc(sizeof(char) * MAX_LEN); } -static struct line *find_lines(void) +static struct line *find_lines(int size) { /* printf("entering find_lines\n"); */ struct line *lines = malloc(sizeof(struct line)); @@ -50,83 +50,114 @@ static struct line *find_lines(void) char *ptr = options.buf; lines->buf = options.buf; lines->next = NULL; - + int c = 0; /* printf("starting loop.\n"); */ while(1){ + if(c >= size){ + /* printf("killing read at end of reported read.\n"); */ + /* printf("(3) used [%d] of [%d]\n", c, size); */ + /* maybe put a null at end of cur_line->buf */ + return lines; + } if(*ptr == '\n' || *ptr == '\0'){ /* printf("----\n"); */ /* printf("found a new line.\n"); */ /* printf("'%c' -> \\0\n", *ptr); */ /* printf("'next \%c': %c\n", *(ptr+1)); */ - if(*ptr == '\0'){ - /* end of buf */ - /* printf("found the end. (1)\n"); */ - return lines; - } + /* if(*ptr == '\0'){ */ + /* /\* end of buf *\/ */ + /* /\* printf("found the end. (1)\n"); *\/ */ + /* printf("(1) used [%d] of [%d]\n", c, size); */ + /* return lines; */ + /* } */ *ptr = '\0'; - ptr++; - - while(*ptr == '\n'){ + ptr++; c++; + + while(*ptr == '\n' || *ptr == '\0'){ + if(c >= size){ + /* printf("killing read at end of reported read.\n"); */ + /* printf("(3) used [%d] of [%d]\n", c, size); */ + return lines; + } /* empty, discard */ /* printf("empty\n"); */ *ptr = '\0'; - ptr++; + ptr++; c++; } - if(*ptr == '\0'){ - /* end of buf */ - /* printf("found the end. (2)\n"); */ - return lines; - } + /* if(*ptr == '\0'){ */ + /* /\* end of buf *\/ */ + /* /\* printf("found the end. (2)\n"); *\/ */ + /* printf("(2) used [%d] of [%d]\n", c, size); */ + /* return lines; */ + /* } */ /* printf("hallo there.\n"); */ cur_line = malloc(sizeof(struct line)); old_line->next = cur_line; old_line = cur_line; cur_line->buf = ptr; + cur_line->next = NULL; } - printf("________|%c|\n", *ptr); - ptr++; - } + /* printf("________|%c|\n", *ptr); */ + ptr++; c++; + } } void socket_read(void (*func)(char *line)) { if(options.lines != NULL){ - printf("|||| already got some input. |||\n"); + /* printf("|||| already got some input. |||\n"); */ goto call_func; } int size; do_read: - printf("||||| doing read. ||||\n"); + /* printf("||||| doing read. ||||\n"); */ + memset(options.buf, 0, MAX_LEN); \ size = read(options.sfd, options.buf, MAX_LEN); - printf("||||| got read. ||||\n"); + /* printf("read size: %d\n", size); */ + /* printf("||||| got read. ||||\n"); */ if(size == 0){ - printf("||||| reading again. ||||\n"); + /* printf("||||| reading again. ||||\n"); */ goto do_read; }else{ - printf("||||| done read. ||||\n"); - options.lines = find_lines(); + /* printf("||||| done read. ||||\n"); */ + options.lines = find_lines(size); + + /* struct line *current_line = options.lines; */ + + /* int count = 0; */ + /* while(1){ */ + /* printf("[%d] '%s'\n", count++, current_line->buf); */ + /* if(current_line->next == NULL){ */ + /* printf("found the end.\n"); */ + /* break; */ + /* } */ + /* current_line = current_line->next; */ + /* } */ + } + call_func:{ struct line *cur_line = options.lines; - - int count = 0; - while(1){ - printf("[%d] '%s'\n", count++, cur_line->buf); - if(cur_line->next == NULL){ - printf("found the end.\n"); - break; + if(cur_line == NULL){ + /* printf("bad options line.\n"); */ + }else{ + while(1){ + /* printf("[%s]->", cur_line->buf); */ + if(cur_line->next == NULL){ + /* printf("(nil)\n"); */ + break; + }else{ + cur_line = cur_line->next; + } } - cur_line = cur_line->next; } } - call_func: - printf("calling function with %s\n", options.lines->buf); + /* printf("calling function with %s\n", options.lines->buf); */ func(options.lines->buf); options.lines = options.lines->next; return; } - diff --git a/src/tomc/main.c b/src/tomc/main.c index 3dd16c1..46f44a7 100644 --- a/src/tomc/main.c +++ b/src/tomc/main.c @@ -47,7 +47,7 @@ static void init(void) struct sockaddr_un addr; addr.sun_family = AF_LOCAL; sprintf(addr.sun_path, "/run/user/1000/tomd/socket"); - + if(connect(sfd, (struct sockaddr *) &addr, SUN_LEN(&addr)) != 0){ perror("connect"); exit(EXIT_FAILURE); @@ -74,7 +74,7 @@ static void extract_options(int argc, char **argv) } options.name = NULL; - + for(int i = 1; i < argc; i++){ @@ -117,13 +117,13 @@ static char socket_buf[100]; if(wrote != write_len){ \ perror("[tomc] write size mismatch"); \ exit(EXIT_FAILURE); \ - }} - + }} + int last_read_size = 0; static void do_read(char *line) { - printf("got %s as input.\n", line); + /* printf("got %s as input.\n", line); */ strcpy(socket_buf, line); /* printf("socket_buf: %s\n", socket_buf); */ } @@ -156,7 +156,7 @@ static void do_read(char *line) SOCK_WRITE(options.name); \ SOCK_ACK; \ } - + static void write_client(void) { SOCK_WRITE("client:tomc"); @@ -192,13 +192,14 @@ static void write_requests(void) } while(1){ - printf("looking for that last ack.\n"); + /* printf("looking for that last ack.\n"); */ SOCK_READ; if(strcmp(socket_buf, "ACK") == 0){ - tomc_p("got the ending ack."); + /* tomc_p("got the ending ack."); */ + write(sfd, "ACK", sizeof "ACK"); break; }else{ - printf("in else.\n"); + /* printf("in else.\n"); */ /* int len = strlen(socket_buf); */ /* int total = len; */ /* if(last_read_size == 0){ */ @@ -223,7 +224,7 @@ int main(int argc, char **argv) init(); write_requests(); - tomc_p("dying of our own accord."); - + /* tomc_p("dying of our own accord."); */ + return EXIT_SUCCESS; } diff --git a/src/tomd/main.c b/src/tomd/main.c index 3c2c84c..cfbb8b9 100644 --- a/src/tomd/main.c +++ b/src/tomd/main.c @@ -75,7 +75,7 @@ static void gen_socket_filename(void) static void socket_init(void) { gen_socket_filename(); - + /* listen on socket, respond to requests. */ /* perform all registered 'tick' operations */ sfd = @@ -117,8 +117,14 @@ static void init(void) #define SEND_ACK { \ int size; \ - if(size = write(asfd, "ACK\n", sizeof "ACK\n") != sizeof "ACK\n") { \ - tomd_p("didn't send as much as we though (%d != %d)", size, sizeof "ACK\n"); \ + char ACK[4];\ + ACK[0] = 'A';\ + ACK[1] = 'C';\ + ACK[2] = 'K';\ + ACK[3] = '\0';\ +if(size = write(asfd, ACK, sizeof(ACK)) != sizeof(ACK)) { \ + tomd_p("didn't send as much as we though (%d != %d)",\ + size, sizeof(ACK)); \ } \ } @@ -138,6 +144,7 @@ static void daemonize(void) } #define READ_SOCKET {\ + memset(buf, 0, sizeof(buf)); \ int size = read(asfd, buf, 100); \ buf[size] = '\0'; \ } @@ -184,7 +191,7 @@ static void handle_request(int asfd, char *buf) request = request_types[i].request; break; } - + i++; } @@ -222,11 +229,11 @@ static void handle_request(int asfd, char *buf) perror("[tomd] waitpid"); }else if(n_pid == 0){ DOUBLE_DUTY("waitpid == 0, check manual\n"); - }else{ + }else{ DOUBLE_DUTY("running: %s\n", !WIFEXITED(status) ? "yes" : "no"); - + if(WIFEXITED(status)){ DOUBLE_DUTY(" status: %d\n", WEXITSTATUS(status)); jp->pid = -1; @@ -237,7 +244,7 @@ static void handle_request(int asfd, char *buf) DOUBLE_DUTY("running: no\n"); DOUBLE_DUTY(" status: %d\n", jp->last_status); } - + if(jp->pid != -1){ DOUBLE_DUTY(" pid: %d\n", jp->pid); } @@ -249,6 +256,11 @@ static void handle_request(int asfd, char *buf) tomd_p("sending ack."); SEND_ACK; tomd_p("ack is sent."); + get_ack: + READ_SOCKET; + if(strcmp(buf, "ACK") != 0) + goto get_ack; + tomd_p("ack was ack'd."); } static void handle_connection(int asfd) @@ -264,6 +276,7 @@ static void handle_connection(int asfd) handle_request(asfd, buf); shutdown(asfd, SHUT_WR); + close(asfd); } static void run(void) @@ -298,7 +311,7 @@ static void run(void) perror("[tomd] accept socket"); exit(EXIT_FAILURE); } - + handle_connection(accept_sfd); } } @@ -318,6 +331,6 @@ int main(int argc, char **argv) /* daemonize(); */ run(); cleanup(); - + return EXIT_SUCCESS; } -- 2.20.1