Broken comm stuff.
authorTom Balzer <niebieskitrociny@gmail.com>
Fri, 29 Jun 2018 21:08:03 +0000 (16:08 -0500)
committerTom Balzer <niebieskitrociny@gmail.com>
Fri, 29 Jun 2018 21:08:03 +0000 (16:08 -0500)
Makefile
include/job.h
include/manifest.h
src/common/guile_helpers.c
src/tomc/main.c
src/tomc/socket_read.c [new file with mode: 0644]
src/tomd/main.c

index 448a7d9..a785462 100644 (file)
--- 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
-       gcc -o tomc ./src/tomc/main.c
+tomc:./src/tomc/main.c ./src/tomc/socket_read.c
+       gcc -o tomc ./src/tomc/main.c ./src/tomc/socket_read.c
index b69678c..fb0c8c5 100644 (file)
@@ -22,6 +22,7 @@ struct job{
   char *cmd;
   char *args[10];
   pid_t pid;
+  int last_status;
   /* todo */
   /* enum trigger start_trigger; */
   /* enum trigger end_trigger; */
index 4ed5ad7..a038ab2 100644 (file)
@@ -22,3 +22,11 @@ void load_jobs(void);
 /* SUMMARY: */
 /* Run each job in sequence via fork + execv */
 void run_jobs(void);
+
+/* SUMMARY: */
+/* Lookup in the job table the job with a given name */
+struct job *lookup_job(char *buf);
+
+/* for tomc */
+void setup_socket(int sfd);
+void socket_read(void (*func)(char *line));
index 1cb6b2e..94828d7 100644 (file)
@@ -194,9 +194,19 @@ void run_job(int index)
   }
 }
 
-void lookup_job(char *my_job_name)
+struct job *lookup_job(char *my_job_name)
 {
-  
+  for(int i = 0;
+      i < MAX_JOBS;
+      i++){
+    if(jobs[i].name == NULL){
+      continue;
+    }
+    if(strcmp(jobs[i].name, my_job_name) == 0){
+      return &jobs[i];
+    }
+  }
+  return NULL;
 }
 
 void run_jobs(void)
index f766562..3dd16c1 100644 (file)
@@ -22,6 +22,7 @@
 #include <unistd.h>
 
 #include "../../include/macros.h"
+#include "../../include/manifest.h"
 
 static int sfd;
 
@@ -51,6 +52,7 @@ static void init(void)
     perror("connect");
     exit(EXIT_FAILURE);
   }
+  setup_socket(sfd);
 }
 
 static char *default_name = "no_name";
@@ -111,28 +113,41 @@ static char socket_buf[100];
     int write_len = strlen(write_buf);                              \
     ssize_t wrote =                                                 \
       write(sfd, write_buf, write_len);                             \
-    tomc_p("wrote '%s' (%d)", write_buf, write_len);                \
+    /* tomc_p("wrote '%s' (%d)", write_buf, write_len);                 */ \
     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);
+  strcpy(socket_buf, line);
+  /* printf("socket_buf: %s\n", socket_buf); */
+}
 
 #define SOCK_READ {                             \
-    int size = read(sfd, socket_buf, 100);      \
-    if(read == 0) {                             \
-      tomc_p("didn't actually get anything.");  \
-      socket_buf[0] = '\0';                     \
-    }                                           \
-    socket_buf[size] = '\0';                    \
+    socket_read(do_read);                       \
   }
+  /*   int size = read(sfd, socket_buf, 100);      \ */
+  /*   if(size == 0) {                             \ */
+  /*     tomc_p("didn't actually get anything.");  \ */
+  /*     socket_buf[0] = '\0';                     \ */
+  /*   }else{                                      \ */
+  /*     tomc_p("__debug__:size=%d", size);        \ */
+  /*   }                                           \ */
+  /*   socket_buf[size] = '\0';                    \ */
+  /*   tomc_p("__debug__: '%s'", socket_buf);\ */
+  /*   last_read_size = size;\ */
+  /* } */
 #define SOCK_READ_X(X){                                                 \
     SOCK_READ;                                                          \
     if(strcmp(socket_buf, X) != 0){                                     \
       tomc_p("protocol error. instead of ACK we got '%s'", socket_buf); \
       exit(EXIT_FAILURE);                                               \
     }                                                                   \
-    tomc_p("got an ACK");                                               \
   }
 #define SOCK_ACK {SOCK_READ_X("ACK")};
 #define SOCK_REQUEST(X) {                       \
@@ -140,8 +155,6 @@ static char socket_buf[100];
     SOCK_ACK;                                   \
     SOCK_WRITE(options.name);                   \
     SOCK_ACK;                                   \
-    SOCK_READ;                                  \
-    tomc_p("tomd reports '%s'", socket_buf);    \
   }
   
 static void write_client(void)
@@ -177,6 +190,29 @@ static void write_requests(void)
   if(options.status){
     SOCK_REQUEST("status");
   }
+
+  while(1){
+    printf("looking for that last ack.\n");
+    SOCK_READ;
+    if(strcmp(socket_buf, "ACK") == 0){
+      tomc_p("got the ending ack.");
+      break;
+    }else{
+      printf("in else.\n");
+      /* int len = strlen(socket_buf); */
+      /* int total = len; */
+      /* if(last_read_size == 0){ */
+      /*   continue; */
+      /* } */
+      tomc_p(socket_buf);
+      /* while(total != last_read_size -1){ */
+      /*   tomc_p("__debug__:total=%d, size=%d", total, last_read_size); */
+      /*   char *ptr = socket_buf + total; */
+      /*   tomc_p("'%s'", ptr); */
+      /*   total += strlen(ptr); */
+      /* } */
+    }
+  }
 }
 
 int main(int argc, char **argv)
@@ -186,6 +222,8 @@ int main(int argc, char **argv)
   print_options();
   init();
   write_requests();
+
+  tomc_p("dying of our own accord.");
   
   return EXIT_SUCCESS;
 }
diff --git a/src/tomc/socket_read.c b/src/tomc/socket_read.c
new file mode 100644 (file)
index 0000000..cc165de
--- /dev/null
@@ -0,0 +1,132 @@
+/* Copyright (C) 2018 Thomas Balzer */
+
+/* This file is part of tomd. */
+
+/* tomd is free software: you can redistribute it and/or modify */
+/* it under the terms of the GNU General Public License as published by */
+/* the Free Software Foundation, either version 3 of the License, or */
+/* (at your option) any later version. */
+
+/* tomd is distributed in the hope that it will be useful, */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
+/* GNU General Public License for more details. */
+
+/* You should have received a copy of the GNU General Public License */
+/* along with tomd.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+
+#define MAX_LEN 1024
+
+struct line{
+  char *buf;
+  struct line *next;
+};
+
+static struct{
+  char *buf;
+  int sfd;
+  struct line *lines;
+}options;
+
+void setup_socket(int sfd)
+{
+  options.sfd = sfd;
+  options.buf = malloc(sizeof(char) * MAX_LEN);
+}
+
+static struct line *find_lines(void)
+{
+  /* printf("entering find_lines\n"); */
+  struct line *lines = malloc(sizeof(struct line));
+  struct line *cur_line = lines;
+  struct line *old_line = lines;
+  char *ptr = options.buf;
+  lines->buf = options.buf;
+  lines->next = NULL;
+
+  /* printf("starting loop.\n"); */
+  while(1){
+    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;
+      }
+
+      *ptr = '\0';
+      ptr++;
+
+      while(*ptr == '\n'){
+        /* empty, discard */
+        /* printf("empty\n"); */
+        *ptr = '\0';
+        ptr++;
+      }
+
+      if(*ptr == '\0'){
+        /* end of buf */
+        /* printf("found the end. (2)\n"); */
+        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;
+    }
+    printf("________|%c|\n", *ptr);
+    ptr++;
+  }      
+}
+
+void socket_read(void (*func)(char *line))
+{
+  if(options.lines != NULL){
+    printf("|||| already got some input. |||\n");
+    goto call_func;
+  }
+
+  int size;
+ do_read:
+  printf("||||| doing read. ||||\n");
+  size = read(options.sfd, options.buf, MAX_LEN);
+  printf("||||| got read. ||||\n");
+  if(size == 0){
+    printf("||||| reading again. ||||\n");
+    goto do_read;
+  }else{
+    printf("||||| done read. ||||\n");
+    options.lines = find_lines();
+
+    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;
+      }
+      cur_line = cur_line->next;
+    }
+  }
+
+ call_func:
+  printf("calling function with %s\n", options.lines->buf);
+  func(options.lines->buf);
+  options.lines = options.lines->next;
+  return;
+}
+
index dea236d..3c2c84c 100644 (file)
 #include <sys/un.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
+#include <sys/wait.h>
 
 #include "../../include/macros.h"
 #include "../../include/manifest.h"
+#include "../../include/job.h"
 
 static void header(void)
 {
@@ -115,8 +117,8 @@ static void init(void)
 
 #define SEND_ACK {                                                      \
     int size;                                                           \
-    if(size = write(asfd, "ACK", sizeof "ACK") != sizeof "ACK") {       \
-      tomd_p("didn't send as much as we though (%d != %d)", size, sizeof "ACK"); \
+    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"); \
     }                                                                   \
   }
 
@@ -196,7 +198,57 @@ static void handle_request(int asfd, char *buf)
   // cross reference given name against known services
   tomd_p("looking up '%s'", buf);
   SEND_ACK;
-  /* lookup_job(buf); */
+  struct job *jp = lookup_job(buf);
+  #define DOUBLE_DUTY(...) {                      \
+      char tmp[100];                            \
+      tomd_p(__VA_ARGS__);                      \
+      sprintf(tmp, __VA_ARGS__);                \
+      int tmplen = strlen(tmp);                 \
+      tomd_p("__debug__:'%s'", tmp);\
+      write(asfd, tmp, tmplen);               \
+    }
+  if(jp == NULL){
+    DOUBLE_DUTY("lookup of '%s' failed.\n", buf);
+  } else {
+
+    tomd_p("-----");
+    DOUBLE_DUTY("found job record for '%s'\n", jp->name);
+
+    if(request == STATUS){
+      if(jp->pid != -1){
+        int status = 0;
+        int n_pid = waitpid(jp->pid, &status, WNOHANG);
+        if(n_pid < 0){
+          perror("[tomd] waitpid");
+        }else if(n_pid == 0){
+          DOUBLE_DUTY("waitpid == 0, check manual\n");
+        }else{ 
+          DOUBLE_DUTY("running: %s\n",
+                      !WIFEXITED(status) ? "yes"
+                      : "no");
+        
+          if(WIFEXITED(status)){
+            DOUBLE_DUTY(" status: %d\n", WEXITSTATUS(status));
+            jp->pid = -1;
+            jp->last_status = status;
+          }
+        }
+      }else{
+        DOUBLE_DUTY("running: no\n");
+        DOUBLE_DUTY(" status: %d\n", jp->last_status);
+      }
+    
+      if(jp->pid != -1){
+        DOUBLE_DUTY("    pid: %d\n", jp->pid);
+      }
+    }else{
+      DOUBLE_DUTY("don't know how to handle that request.\n");
+    }
+  }
+  tomd_p("-----");
+  tomd_p("sending ack.");
+  SEND_ACK;
+  tomd_p("ack is sent.");
 }
 
 static void handle_connection(int asfd)
@@ -211,7 +263,7 @@ static void handle_connection(int asfd)
   tomd_p("validated client");
   handle_request(asfd, buf);
 
-  close(asfd);
+  shutdown(asfd, SHUT_WR);
 }
 
 static void run(void)
@@ -263,7 +315,7 @@ int main(int argc, char **argv)
   header();
   init();
   run_jobs();
-  daemonize();
+  /* daemonize(); */
   run();
   cleanup();