From: Tom Balzer Date: Wed, 27 Jun 2018 06:02:58 +0000 (-0500) Subject: Update to redirect logs to a file in /var/log/tomd X-Git-Url: http://git.hcoop.net/tlb/tomd.git/commitdiff_plain/06570394b15e0200d117cace03d73bc3801d7d1a Update to redirect logs to a file in /var/log/tomd --- diff --git a/guile/tomd/job.scm b/guile/tomd/job.scm index 94b0a9f..51492c0 100644 --- a/guile/tomd/job.scm +++ b/guile/tomd/job.scm @@ -22,12 +22,14 @@ 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 - (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) diff --git a/include/job.h b/include/job.h index dcb2eed..137118e 100644 --- a/include/job.h +++ b/include/job.h @@ -16,6 +16,7 @@ /* along with tomd. If not, see . */ struct job{ + char *name; char *cmd; char *args[10]; /* todo */ diff --git a/include/scm_interface.h b/include/scm_interface.h index 17e6b78..c6e9296 100644 --- a/include/scm_interface.h +++ b/include/scm_interface.h @@ -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) diff --git a/src/common/guile_helpers.c b/src/common/guile_helpers.c index 1b57dfa..84d72d1 100644 --- a/src/common/guile_helpers.c +++ b/src/common/guile_helpers.c @@ -17,6 +17,8 @@ #include #include +#include +#include #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 index 0000000..a8edb48 --- /dev/null +++ b/src/tomc/main.c @@ -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 . */ + +#include +#include +#include +#include +#include + +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; +}