Broken comm stuff.
[tlb/tomd.git] / src / tomd / main.c
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();