| 1 | /* Copyright (C) 2018 Thomas Balzer */ |
| 2 | |
| 3 | /* This file is part of tomd. */ |
| 4 | |
| 5 | /* tomd is free software: you can redistribute it and/or modify */ |
| 6 | /* it under the terms of the GNU General Public License as published by */ |
| 7 | /* the Free Software Foundation, either version 3 of the License, or */ |
| 8 | /* (at your option) any later version. */ |
| 9 | |
| 10 | /* tomd is distributed in the hope that it will be useful, */ |
| 11 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 12 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 13 | /* GNU General Public License for more details. */ |
| 14 | |
| 15 | /* You should have received a copy of the GNU General Public License */ |
| 16 | /* along with tomd. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <libguile.h> |
| 19 | #include <pwd.h> |
| 20 | |
| 21 | #include "../../include/macros.h" |
| 22 | #include "../../include/scm_interface.h" |
| 23 | |
| 24 | #define MANIFEST_LOC "/.config/tomd/init/manifest.scm" |
| 25 | |
| 26 | static void load_job(SCM job_list, int index) |
| 27 | { |
| 28 | SCM scm_cur_job = SCM_ARR(job_list, index); |
| 29 | |
| 30 | if(scm_is_false(job_predicate(scm_cur_job))){ |
| 31 | tomd_p("job %d wasn't a real job type.", index); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | SCM scm_cmd = get_cmd(scm_cur_job); |
| 36 | SCM scm_args = get_args(scm_cur_job); |
| 37 | |
| 38 | /* TODO > Handle these. */ |
| 39 | /* planned is to be able to run at different points, and at */ |
| 40 | /* the request of a user. for testing we do things all at */ |
| 41 | /* boot up. */ |
| 42 | SCM scm_start_trigger = get_start_trigger(scm_cur_job); |
| 43 | SCM scm_end_trigger = get_end_trigger(scm_cur_job); |
| 44 | |
| 45 | char *job_cmd = scm_to_locale_string(scm_cmd); |
| 46 | |
| 47 | char *real_args[10]; |
| 48 | int jlen = scm_to_int(scm_length(scm_args)); |
| 49 | for(int j = 0; |
| 50 | j < jlen; |
| 51 | j++){ |
| 52 | real_args[j] = |
| 53 | scm_to_locale_string(SCM_ARR(scm_args, j)); |
| 54 | } |
| 55 | |
| 56 | tomd_p("JOB <%d>:", index); |
| 57 | tomd_p(" cmd:'%s'", job_cmd); |
| 58 | for(int j = 0; |
| 59 | j < jlen; |
| 60 | j++){ |
| 61 | tomd_p(" arg [%d]:'%s'", j, real_args[j]); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static void *load_manifest(void *args) |
| 66 | { |
| 67 | char *manifest_loc; |
| 68 | if(!args){ |
| 69 | tomd_p("arg to load_manifest is NULL"); |
| 70 | exit(EXIT_FAILURE); |
| 71 | } |
| 72 | manifest_loc = (char *)args; |
| 73 | |
| 74 | scm_c_primitive_load(args); |
| 75 | |
| 76 | SCM scm_job_list = |
| 77 | scm_c_public_ref("tomd manifest", "job-list"); |
| 78 | |
| 79 | if(scm_is_false(scm_job_list)){ |
| 80 | tomd_p("no job-list found in manifest.scm"); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | if(scm_is_false(scm_list_p(scm_job_list))){ |
| 85 | tomd_p("job-list found, but isn't a list."); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | int i; |
| 90 | int len = SCM_LIST_LEN(scm_job_list); |
| 91 | for(i = 0; |
| 92 | i < len; |
| 93 | i++){ |
| 94 | load_job(scm_job_list, i); |
| 95 | } |
| 96 | tomd_p("looked at %d jobs.", i); |
| 97 | } |
| 98 | |
| 99 | static char *lookup_user_folder(void) |
| 100 | { |
| 101 | uid_t user = getuid(); |
| 102 | struct passwd *userpasswd = |
| 103 | getpwuid(user); |
| 104 | |
| 105 | /* take the manifest location and sub ~ for pw_dir */ |
| 106 | char *buffer = (char *)malloc(sizeof(char) * 300); |
| 107 | strcpy(buffer, userpasswd->pw_dir); |
| 108 | int len = strlen(userpasswd->pw_dir); |
| 109 | strcpy(buffer + len, MANIFEST_LOC); |
| 110 | return buffer; |
| 111 | } |
| 112 | |
| 113 | void load_jobs(void) |
| 114 | { |
| 115 | char *manifest = lookup_user_folder(); |
| 116 | tomd_p("Loading jobs from '%s'", manifest); |
| 117 | |
| 118 | /* get into the manifest.scm file */ |
| 119 | void *res = |
| 120 | scm_with_guile(load_manifest, |
| 121 | (void *) manifest); |
| 122 | |
| 123 | tomd_p("Finished loading."); |
| 124 | } |