Update to redirect logs to a file in /var/log/tomd
[tlb/tomd.git] / src / common / guile_helpers.c
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("");