Some socket stuff in tomc, start of support in tomd
[tlb/tomd.git] / src / tomd / main.c
index 14c7397..1f9b207 100644 (file)
 #include <sys/stat.h>
 #include <sys/socket.h>
 
+#include "../../include/macros.h"
+#include "../../include/manifest.h"
+
 static void header(void)
 {
-  printf("Tom's Daemon, Copyright (C) 2018 Thomas Balzer\n");
-  printf("GPL v3 or later license.\n");
+  tomd_p("Tom's Daemon, Copyright (C) 2018 Thomas Balzer");
+  tomd_p("GPL v3 or later license.");
 }
 
 static int sfd;
@@ -46,7 +49,7 @@ static void gen_socket_filename(void)
           "/run/user/%d/tomd/socket",
           this_user_id);
 
-  printf("making dir '%s'\n", socket_dirname);
+  tomd_p("making dir '%s'", socket_dirname);
   /* lazy way */
   char buf[100];
   sprintf(buf, "mkdir -p %s", socket_dirname);
@@ -56,18 +59,18 @@ static void gen_socket_filename(void)
   /*   exit(EXIT_FAILURE); */
   /* } */
 
-  printf("removing file '%s'\n", socket_filename);
+  tomd_p("removing file '%s'", socket_filename);
   if(unlink(socket_filename) != 0){
     if(errno == ENOENT){
-      printf("socket file doesn't exist\n");
+      tomd_p("socket file doesn't exist");
     }else{
-      perror("socket unlink");
+      perror("[tomd] socket unlink");
       exit(EXIT_FAILURE);
     }
   }
 }
 
-static void init(void)
+static void socket_init(void)
 {
   gen_socket_filename();
   
@@ -79,7 +82,7 @@ static void init(void)
            0);               /* protocol */
 
   if(sfd < 0){
-    perror("socket");
+    perror("[tomd] socket");
     exit(EXIT_FAILURE);
   }
 
@@ -92,16 +95,22 @@ static void init(void)
           sizeof(name.sun_path));
 
   size = SUN_LEN(&name);
-  printf("attempting to bind to '%s'\n",
+  tomd_p("attempting to bind to '%s'",
          socket_filename);
   if(bind(sfd,
           (struct sockaddr *) &name,
           size) < 0){
-    perror("bind");
+    perror("[tomd] bind");
     exit(EXIT_FAILURE);
   }
 
-  printf("initialized tomd socket connections\n");
+  tomd_p("initialized tomd socket connections");
+}
+
+static void init(void)
+{
+  socket_init();
+  load_jobs();
 }
 
 /* These defines are here to improve the local readability */
@@ -130,23 +139,34 @@ static void run(void)
     listen(sfd,
            10); /* max connection limit is 10 for arbitrary reasons */
   if(listen_bool == -1){
-    perror("listen");
+    perror("[tomd] listen");
     exit(EXIT_FAILURE);
   }
 
   for(;;){
+    struct sockaddr addr;
+    socklen_t size;
     int accept_sfd =
       accept(sfd,
-             NULL,              /* requester info */
-             NULL);             /* len of requester info */
+             &addr,             /* requester info */
+             &size);            /* len of requester info */
     if(accept_sfd < 0){
-      perror("accept socket");
+      perror("[tomd] accept socket");
       exit(EXIT_FAILURE);
     }
 
-    printf("accepted socket connection\n");
+    /* 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);
-    break;
   }
 }
 
@@ -161,6 +181,7 @@ int main(int argc, char **argv)
 {
   header();
   init();
+  run_jobs();
   daemonize();
   run();
   cleanup();