Tooling changes, some socket code.
authorTom Balzer <niebieskitrociny@gmail.com>
Tue, 19 Jun 2018 07:04:02 +0000 (02:04 -0500)
committerTom Balzer <niebieskitrociny@gmail.com>
Tue, 19 Jun 2018 07:04:02 +0000 (02:04 -0500)
Makefile
kill-tomd.sh [new file with mode: 0755]
run.sh [new file with mode: 0755]
src/dumb-client/main.c [moved from src/main.c with 51% similarity]
src/tomd/main.c [new file with mode: 0644]

index bf84ead..325d894 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
 # TODO > Use GNU automake tools
 
 tomd:
-       gcc -o tomd ./src/main.c
+       gcc -o tomd ./src/tomd/main.c
diff --git a/kill-tomd.sh b/kill-tomd.sh
new file mode 100755 (executable)
index 0000000..be6a602
--- /dev/null
@@ -0,0 +1 @@
+kill -n 15 $(pgrep tomd)
diff --git a/run.sh b/run.sh
new file mode 100755 (executable)
index 0000000..13cfbe1
--- /dev/null
+++ b/run.sh
@@ -0,0 +1 @@
+sudo ./kill-tomd.sh; rm ./tomd; make && sudo ./tomd;
similarity index 51%
rename from src/main.c
rename to src/dumb-client/main.c
index 54e375e..6fca06f 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 <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
-
-static void header(void)
-{
-  printf("Tom's Daemon, Copyright (C) 2018 Thomas Balzer\n");
-  printf("GPL v3 or later license.\n");
-}
-
-static void init(void)
-{
-  /* TODO */
-}
-
-/* These defines are here to improve the local readability */
-#define CHDIR            0
-#define DONT_CHDIR       1
-#define CLOSE_PIPES      0
-#define DONT_CLOSE_PIPES 1
-
-static void daemonize(void)
-{
-  /* daemon is in unistd. */
-  /* arg1 - 0 changes dir to / */
-  /* arg2 - 0 closes all pipes (/dev/null) */
-  /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE
-     PROCESS  */
-  daemon(DONT_CHDIR, CLOSE_PIPES);
-}
+#include <stdio.h>
 
-static void run(void)
+void init(void)
 {
-  /* listen on socket, respond to requests. */
-  /* perform all registered 'tick' operations */
+  /* init socket connection */
+  
 }
 
 int main(int argc, char **argv)
 {
-  header();
-  init();
-  daemonize();
-  run();
+  printf("dumb client startup.\n");
+
+  
   
   return EXIT_SUCCESS;
 }
diff --git a/src/tomd/main.c b/src/tomd/main.c
new file mode 100644 (file)
index 0000000..14c7397
--- /dev/null
@@ -0,0 +1,169 @@
+/* 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 <stddef.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+
+static void header(void)
+{
+  printf("Tom's Daemon, Copyright (C) 2018 Thomas Balzer\n");
+  printf("GPL v3 or later license.\n");
+}
+
+static int sfd;
+static char socket_dirname[100];
+static char socket_filename[100];
+
+static void gen_socket_filename(void)
+{
+  uid_t this_user_id =
+    getuid();
+  sprintf(socket_dirname,
+          "/run/user/%d/tomd",
+          this_user_id);
+  sprintf(socket_filename,
+          "/run/user/%d/tomd/socket",
+          this_user_id);
+
+  printf("making dir '%s'\n", socket_dirname);
+  /* lazy way */
+  char buf[100];
+  sprintf(buf, "mkdir -p %s", socket_dirname);
+  system(buf);
+  /* if(mkdir(socket_dirname, S_IRWXU) != 0){ */
+  /*   perror("socket mkdir"); */
+  /*   exit(EXIT_FAILURE); */
+  /* } */
+
+  printf("removing file '%s'\n", socket_filename);
+  if(unlink(socket_filename) != 0){
+    if(errno == ENOENT){
+      printf("socket file doesn't exist\n");
+    }else{
+      perror("socket unlink");
+      exit(EXIT_FAILURE);
+    }
+  }
+}
+
+static void init(void)
+{
+  gen_socket_filename();
+  
+  /* listen on socket, respond to requests. */
+  /* perform all registered 'tick' operations */
+  sfd =
+    socket(PF_LOCAL,         /* namespace - local unix socket */
+           SOCK_STREAM,      /* style */
+           0);               /* protocol */
+
+  if(sfd < 0){
+    perror("socket");
+    exit(EXIT_FAILURE);
+  }
+
+  struct sockaddr_un name;
+  size_t size;
+
+  name.sun_family = AF_LOCAL;
+  strncpy(name.sun_path,
+          socket_filename,
+          sizeof(name.sun_path));
+
+  size = SUN_LEN(&name);
+  printf("attempting to bind to '%s'\n",
+         socket_filename);
+  if(bind(sfd,
+          (struct sockaddr *) &name,
+          size) < 0){
+    perror("bind");
+    exit(EXIT_FAILURE);
+  }
+
+  printf("initialized tomd socket connections\n");
+}
+
+/* These defines are here to improve the local readability */
+#define CHDIR            0
+#define DONT_CHDIR       1
+#define CLOSE_PIPES      0
+#define DONT_CLOSE_PIPES 1
+
+static void daemonize(void)
+{
+  /* daemon is in unistd. */
+  /* arg1 - 0 changes dir to / */
+  /* arg2 - 0 closes all pipes (/dev/null) */
+  /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE
+     PROCESS  */
+  daemon(DONT_CHDIR, DONT_CLOSE_PIPES);
+}
+
+static void run(void)
+{
+  /* loop: */
+  /*  1 listen */
+  /*  2 accept/select */
+  /*  3 handle dgram */
+  int listen_bool =
+    listen(sfd,
+           10); /* max connection limit is 10 for arbitrary reasons */
+  if(listen_bool == -1){
+    perror("listen");
+    exit(EXIT_FAILURE);
+  }
+
+  for(;;){
+    int accept_sfd =
+      accept(sfd,
+             NULL,              /* requester info */
+             NULL);             /* len of requester info */
+    if(accept_sfd < 0){
+      perror("accept socket");
+      exit(EXIT_FAILURE);
+    }
+
+    printf("accepted socket connection\n");
+    close(accept_sfd);
+    break;
+  }
+}
+
+static void cleanup(void)
+{
+  if(sfd >= 0){
+    close(sfd);
+  }
+}
+
+int main(int argc, char **argv)
+{
+  header();
+  init();
+  daemonize();
+  run();
+  cleanup();
+  
+  return EXIT_SUCCESS;
+}