Update to redirect logs to a file in /var/log/tomd
authorTom Balzer <niebie@localhost.localdomain>
Wed, 27 Jun 2018 06:02:58 +0000 (01:02 -0500)
committerTom Balzer <niebie@localhost.localdomain>
Wed, 27 Jun 2018 06:02:58 +0000 (01:02 -0500)
guile/tomd/job.scm
include/job.h
include/scm_interface.h
src/common/guile_helpers.c
src/tomc/main.c [new file with mode: 0644]

index 94b0a9f..51492c0 100644 (file)
             job-args c-job-args
             job-start-trigger c-job-start-trigger
             job-end-trigger c-job-end-trigger
+            job-name c-job-name
             c-check-job))
 
 ;;; records
 (define-record-type <job>
-  (make-job command-line args start-trigger end-trigger)
+  (make-job name command-line args start-trigger end-trigger)
   job?
+  (name job-name)
   (command-line job-command-line)
   (args job-args)
   (start-trigger job-start-trigger)
@@ -49,6 +51,9 @@
 (define (c-job-end-trigger obj)
   (job-end-trigger obj))
 
+(define (c-job-name obj)
+  (job-name obj))
+
 ;;; functions
 (define (get-keyword-value args keyword default)
   (let ((keyword-value (memq keyword args)))
@@ -60,7 +65,8 @@
   (let ((command-line  (get-keyword-value rest #:command-line  #f))
         (args          (get-keyword-value rest #:args          (list)))
         (start-trigger (get-keyword-value rest #:start-trigger 'login))
-        (end-trigger   (get-keyword-value rest #:end-trigger   #f)))
+        (end-trigger   (get-keyword-value rest #:end-trigger   #f))
+        (name          (get-keyword-value rest #:name          #f)))
     ;; do thing with keyword-ed variables
     ;; (display "settings:") (newline)
     ;; (format (current-output-port)
@@ -77,7 +83,8 @@
     ;; (newline)
 
     ;; create a new object that represents the args given.
-    (make-job command-line
+    (make-job name
+              command-line
               args
               start-trigger
               end-trigger)
index dcb2eed..137118e 100644 (file)
@@ -16,6 +16,7 @@
 /* along with tomd.  If not, see <http://www.gnu.org/licenses/>. */
 
 struct job{
+  char *name;
   char *cmd;
   char *args[10];
   /* todo */
index 17e6b78..c6e9296 100644 (file)
@@ -20,6 +20,7 @@
 #include "macros.h"
 
 WRAP_SCM_FUNCTION_1("tomd job", "c-check-job", job_predicate)
+WRAP_SCM_FUNCTION_1("tomd job", "c-job-name", get_name)
 WRAP_SCM_FUNCTION_1("tomd job", "c-job-cmd", get_cmd)
 WRAP_SCM_FUNCTION_1("tomd job", "c-job-args", get_args)
 WRAP_SCM_FUNCTION_1("tomd job", "c-job-start-trigger", get_start_trigger)
index 1b57dfa..84d72d1 100644 (file)
@@ -17,6 +17,8 @@
 
 #include <libguile.h>
 #include <pwd.h>
+#include <fcntl.h>
+#include <string.h>
 
 #include "../../include/job.h"
 #include "../../include/macros.h"
@@ -38,6 +40,7 @@ static struct job load_job(SCM job_list, int index)
     return ret;
   }
 
+  SCM scm_name = get_name(scm_cur_job);
   SCM scm_cmd = get_cmd(scm_cur_job);
   SCM scm_args = get_args(scm_cur_job);
 
@@ -48,8 +51,8 @@ static struct job load_job(SCM job_list, int index)
   SCM scm_start_trigger = get_start_trigger(scm_cur_job);
   SCM scm_end_trigger = get_end_trigger(scm_cur_job);
 
+  char *job_name = scm_to_locale_string(scm_name);
   char *job_cmd = scm_to_locale_string(scm_cmd);
-
   char *real_args[10];
   int jlen = scm_to_int(scm_length(scm_args));
   for(int j = 0;
@@ -59,6 +62,7 @@ static struct job load_job(SCM job_list, int index)
       scm_to_locale_string(SCM_ARR(scm_args, j));
   }
 
+  ret.name = job_name;
   ret.cmd = job_cmd;
   
   for(int j = 1;
@@ -154,23 +158,44 @@ void load_jobs(void)
   }
 }
 
+#define LOG_DIR "/var/log/tomd/"
+
 void run_job(int index)
 {
   struct job *job = &jobs[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++;
+    /* 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);
+    strcpy(buf + strlen(LOG_DIR), job->name);
+    tomd_p("redirecting stdout to %s.", buf);
+    if(unlink(buf) != 0){
+      perror("file couldn't be removed.");
     }
+
+    int fd = open(buf, O_WRONLY | O_CREAT, 0644);
+    if(fd < 0){
+      perror("[tomd] couldn't open file.");
+      return;
+    }
+
+    dup2(fd, STDOUT_FILENO);
+    dup2(fd, STDERR_FILENO);
+    close(fd);
     execvp(job->cmd, job->args);
     tomd_p("execvp for '%s' failed", job->cmd);
     perror("");
diff --git a/src/tomc/main.c b/src/tomc/main.c
new file mode 100644 (file)
index 0000000..a8edb48
--- /dev/null
@@ -0,0 +1,71 @@
+/* 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 <stdlib.h>
+#include <stdio.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+static int sfd;
+
+static void init(void)
+{
+  /* init socket connection */
+  sfd =
+    socket(PF_LOCAL,
+           SOCK_STREAM,
+           0);
+  if(sfd < 0){
+    perror("socket");
+    exit(EXIT_FAILURE);
+  }
+
+  struct sockaddr_un addr;
+  addr.sun_family = AF_LOCAL;
+  sprintf(addr.sun_path, "/run/user/1000/tomd/socket");
+  
+  if(connect(sfd, (struct sockaddr *) &addr, SUN_LEN(&addr)) != 0){
+    perror("connect");
+    exit(EXIT_FAILURE);
+  }
+}
+
+#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 void header(void)
+{
+  tomd_p("Tom's Client, Copyright (C) 2018 Thomas Balzer");
+  tomd_p("GPL v3 or later license.");
+}
+
+int main(int argc, char **argv)
+{
+  header();
+  init();
+  write_hallo();
+  
+  return EXIT_SUCCESS;
+}