Updated tomd and tomc to do solid communication over sockets.
authorTom Balzer <niebieskitrociny@gmail.com>
Thu, 28 Jun 2018 08:11:55 +0000 (03:11 -0500)
committerTom Balzer <niebieskitrociny@gmail.com>
Thu, 28 Jun 2018 08:11:55 +0000 (03:11 -0500)
include/job.h
src/common/guile_helpers.c
src/tomc/main.c
src/tomd/main.c

index 137118e..b69678c 100644 (file)
 /* You should have received a copy of the GNU General Public License */
 /* along with tomd.  If not, see <http://www.gnu.org/licenses/>. */
 
+#include <unistd.h>
+
 struct job{
   char *name;
   char *cmd;
   char *args[10];
+  pid_t pid;
   /* todo */
   /* enum trigger start_trigger; */
   /* enum trigger end_trigger; */
index 84d72d1..1cb6b2e 100644 (file)
@@ -166,18 +166,6 @@ void run_job(int index)
 
   pid_t pid = fork();
   if(pid == 0){                 /* child */
-    /* int i = 0; */
-    /* while(1){ */
-    /*   tomd_p("hallo"); */
-    /*   if(job->args[i] == NULL){ */
-    /*     tomd_p("arg [%d] is NULL", i); */
-    /*     break; */
-    /*   }else{ */
-    /*     tomd_p("arg [%d] is '%s'", i, job->args[i]); */
-    /*   } */
-    /*   i++; */
-    /* } */
-
     /* redirect to a file */
     char buf[100];
     strcpy(buf, LOG_DIR);
@@ -202,9 +190,15 @@ void run_job(int index)
     exit(EXIT_FAILURE);
   }else{                        /* parent */
     tomd_p("forked [%d] to run '%s'", pid, job->cmd);
+    job->pid = pid;
   }
 }
 
+void lookup_job(char *my_job_name)
+{
+  
+}
+
 void run_jobs(void)
 {
   int i;
index 9e09809..f766562 100644 (file)
@@ -103,16 +103,6 @@ static void print_options(void)
   tomc_p("---------");
 }
 
-#define HALLO "hallo there from dumb-client"
-
-void write_hallo(void)
-{
-  printf("writing hallo\n");
-  ssize_t wrote =
-    write(sfd, HALLO, sizeof HALLO);
-  printf("wrote %d bytes.\n", wrote);
-}
-
 static char write_buf[100];
 static char socket_buf[100];
 
@@ -121,19 +111,28 @@ 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);                \
     if(wrote != write_len){                                         \
       perror("[tomc] write size mismatch");                         \
       exit(EXIT_FAILURE);                                           \
-    }                                                               \
+    }}                                                               
+    
+
+#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';                    \
   }
-
-#define SOCK_READ { read(sfd, socket_buf, 100); }
 #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) {                       \
@@ -151,26 +150,6 @@ static void write_client(void)
   SOCK_ACK;
 }
 
-static void write_start(void)
-{
-
-}
-
-static void write_stop(void)
-{
-  SOCK_REQUEST("stop");
-}
-
-static void write_kill(void)
-{
-  SOCK_REQUEST("kill");
-}
-
-static void write_status(void)
-{
-  SOCK_REQUEST("status");
-}
-
 static void write_requests(void)
 {
   write_client();
index 1f9b207..dea236d 100644 (file)
@@ -113,7 +113,13 @@ static void init(void)
   load_jobs();
 }
 
-/* These defines are here to improve the local readability */
+#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"); \
+    }                                                                   \
+  }
+
 #define CHDIR            0
 #define DONT_CHDIR       1
 #define CLOSE_PIPES      0
@@ -129,6 +135,85 @@ static void daemonize(void)
   daemon(DONT_CHDIR, DONT_CLOSE_PIPES);
 }
 
+#define READ_SOCKET {\
+    int size = read(asfd, buf, 100);            \
+    buf[size] = '\0';                           \
+  }
+
+static int validate_sender(int asfd, char *buf)
+{
+  READ_SOCKET;
+  if(strcmp(buf, "client:tomc") == 0){
+    SEND_ACK;
+    return 0;
+  }
+  tomd_p("received '%s' instead of client:tomc", buf);
+  return -1;
+}
+
+enum requests { KILL, STATUS, STOP, START, UNKNOWN};
+#define X(a, b) { a, b }
+static struct {
+  char *str;
+  enum requests request;
+} request_types[] =
+  {
+   X("kill", KILL),
+   X("status", STATUS),
+   X("start", START),
+   X("stop", STOP),
+   X(NULL, -1)
+  };
+#undef X
+
+static void handle_request(int asfd, char *buf)
+{
+  READ_SOCKET;
+  enum requests request = UNKNOWN;
+  int i = 0;
+  while(1){
+    if(request_types[i].str == NULL){
+      break;
+    }
+
+    tomd_p("loop [%d]: comparing '%s' to '%s'",
+           i, buf, request_types[i].str);
+    if(strcmp(buf, request_types[i].str) == 0){
+      request = request_types[i].request;
+      break;
+    }
+    
+    i++;
+  }
+
+  if(request == UNKNOWN){
+    tomd_p("unknown request type!");
+    return;
+  }
+
+  SEND_ACK;
+  READ_SOCKET;
+  // cross reference given name against known services
+  tomd_p("looking up '%s'", buf);
+  SEND_ACK;
+  /* lookup_job(buf); */
+}
+
+static void handle_connection(int asfd)
+{
+  char buf[100] = {};
+
+  if(validate_sender(asfd, buf) != 0){
+    tomd_p("invalid sender.");
+    return;
+  }
+
+  tomd_p("validated client");
+  handle_request(asfd, buf);
+
+  close(asfd);
+}
+
 static void run(void)
 {
   /* loop: */
@@ -143,30 +228,26 @@ static void run(void)
     exit(EXIT_FAILURE);
   }
 
+  int i = 0;
   for(;;){
     struct sockaddr addr;
-    socklen_t size;
+    /* sometimes gives EINVAL if not initialized. */
+    socklen_t size = sizeof(addr);
+    tomd_p("accept loop [%d]", i++);
+    errno = 0;
     int accept_sfd =
       accept(sfd,
              &addr,             /* requester info */
              &size);            /* len of requester info */
     if(accept_sfd < 0){
+      if(errno == EINVAL){
+        tomd_p("EINVAL");
+      }
       perror("[tomd] accept socket");
       exit(EXIT_FAILURE);
     }
-
-    /* int getsockname(accept_sfd, */
-    /*                 addr, */
-    /*                 size); */
-    
-    tomd_p("accepted socket connection from '%s'",
-           ((struct sockaddr_un *) &addr)->sun_path);
-
-    char accept_buf[100] = {};
     
-    read(accept_sfd, accept_buf, 100);
-    tomd_p("got message '%s', accept_buf");
-    close(accept_sfd);
+    handle_connection(accept_sfd);
   }
 }