Commit | Line | Data |
---|---|---|
4f839c09 TB |
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 | ||
82ff7d81 TB |
18 | #include <libguile.h> |
19 | #include <pwd.h> | |
20 | ||
2599cbf7 | 21 | #include "../../include/job.h" |
82ff7d81 | 22 | #include "../../include/macros.h" |
3024ed65 | 23 | #include "../../include/scm_interface.h" |
81643445 | 24 | |
82ff7d81 TB |
25 | #define MANIFEST_LOC "/.config/tomd/init/manifest.scm" |
26 | ||
2599cbf7 TB |
27 | #define MAX_JOBS 10 |
28 | ||
29 | static struct job jobs[MAX_JOBS]; | |
30 | ||
31 | static struct job load_job(SCM job_list, int index) | |
32 | { | |
33 | struct job ret = { NULL }; | |
64694f43 | 34 | SCM scm_cur_job = SCM_ARR(job_list, index); |
3024ed65 | 35 | |
3024ed65 TB |
36 | if(scm_is_false(job_predicate(scm_cur_job))){ |
37 | tomd_p("job %d wasn't a real job type.", index); | |
2599cbf7 | 38 | return ret; |
3024ed65 TB |
39 | } |
40 | ||
64694f43 TB |
41 | SCM scm_cmd = get_cmd(scm_cur_job); |
42 | SCM scm_args = get_args(scm_cur_job); | |
3024ed65 | 43 | |
64694f43 TB |
44 | /* TODO > Handle these. */ |
45 | /* planned is to be able to run at different points, and at */ | |
46 | /* the request of a user. for testing we do things all at */ | |
47 | /* boot up. */ | |
48 | SCM scm_start_trigger = get_start_trigger(scm_cur_job); | |
49 | SCM scm_end_trigger = get_end_trigger(scm_cur_job); | |
50 | ||
51 | char *job_cmd = scm_to_locale_string(scm_cmd); | |
3024ed65 TB |
52 | |
53 | char *real_args[10]; | |
54 | int jlen = scm_to_int(scm_length(scm_args)); | |
55 | for(int j = 0; | |
56 | j < jlen; | |
57 | j++){ | |
58 | real_args[j] = | |
59 | scm_to_locale_string(SCM_ARR(scm_args, j)); | |
60 | } | |
61 | ||
2599cbf7 TB |
62 | ret.cmd = job_cmd; |
63 | ||
7813ae49 TB |
64 | for(int j = 1; |
65 | j <= jlen; | |
3024ed65 | 66 | j++){ |
7813ae49 | 67 | ret.args[j] = real_args[j - 1]; |
3024ed65 | 68 | } |
7813ae49 TB |
69 | ret.args[jlen + 1] = NULL; |
70 | ret.args[0] = ret.cmd; | |
2599cbf7 TB |
71 | |
72 | return ret; | |
81643445 TB |
73 | } |
74 | ||
82ff7d81 | 75 | static void *load_manifest(void *args) |
81643445 | 76 | { |
82ff7d81 | 77 | char *manifest_loc; |
81643445 | 78 | if(!args){ |
82ff7d81 TB |
79 | tomd_p("arg to load_manifest is NULL"); |
80 | exit(EXIT_FAILURE); | |
81 | } | |
82 | manifest_loc = (char *)args; | |
83 | ||
82ff7d81 TB |
84 | scm_c_primitive_load(args); |
85 | ||
82ff7d81 TB |
86 | SCM scm_job_list = |
87 | scm_c_public_ref("tomd manifest", "job-list"); | |
88 | ||
89 | if(scm_is_false(scm_job_list)){ | |
90 | tomd_p("no job-list found in manifest.scm"); | |
91 | return NULL; | |
92 | } | |
93 | ||
94 | if(scm_is_false(scm_list_p(scm_job_list))){ | |
95 | tomd_p("job-list found, but isn't a list."); | |
96 | return NULL; | |
97 | } | |
98 | ||
82ff7d81 | 99 | int i; |
3024ed65 | 100 | int len = SCM_LIST_LEN(scm_job_list); |
2599cbf7 | 101 | tomd_p("len=%d, max=%d", len, MAX_JOBS); |
82ff7d81 | 102 | for(i = 0; |
2599cbf7 | 103 | i < len && i < MAX_JOBS; |
82ff7d81 | 104 | i++){ |
2599cbf7 | 105 | jobs[i] = load_job(scm_job_list, i); |
82ff7d81 TB |
106 | } |
107 | tomd_p("looked at %d jobs.", i); | |
2599cbf7 | 108 | jobs[i].cmd = NULL; |
82ff7d81 TB |
109 | } |
110 | ||
111 | static char *lookup_user_folder(void) | |
112 | { | |
113 | uid_t user = getuid(); | |
114 | struct passwd *userpasswd = | |
115 | getpwuid(user); | |
82ff7d81 TB |
116 | |
117 | /* take the manifest location and sub ~ for pw_dir */ | |
118 | char *buffer = (char *)malloc(sizeof(char) * 300); | |
119 | strcpy(buffer, userpasswd->pw_dir); | |
120 | int len = strlen(userpasswd->pw_dir); | |
121 | strcpy(buffer + len, MANIFEST_LOC); | |
82ff7d81 | 122 | return buffer; |
81643445 TB |
123 | } |
124 | ||
125 | void load_jobs(void) | |
126 | { | |
82ff7d81 TB |
127 | char *manifest = lookup_user_folder(); |
128 | tomd_p("Loading jobs from '%s'", manifest); | |
129 | ||
81643445 TB |
130 | void *res = |
131 | scm_with_guile(load_manifest, | |
82ff7d81 | 132 | (void *) manifest); |
82ff7d81 TB |
133 | |
134 | tomd_p("Finished loading."); | |
2599cbf7 TB |
135 | |
136 | int i = 0; | |
137 | while(1){ | |
138 | if(i >= MAX_JOBS || | |
139 | jobs[i].cmd == NULL) { | |
140 | break; | |
141 | } | |
142 | tomd_p("JOB <%d>:", i); | |
143 | tomd_p(" cmd:'%s'", jobs[i].cmd); | |
7813ae49 | 144 | int j = 1; |
2599cbf7 TB |
145 | while(1){ |
146 | if(i >= 10 || | |
147 | jobs[i].args[j] == NULL){ | |
148 | break; | |
149 | } | |
150 | tomd_p(" arg [%d]:'%s'", j, jobs[i].args[j]); | |
151 | j++; | |
152 | } | |
153 | i++; | |
154 | } | |
81643445 | 155 | } |
7813ae49 TB |
156 | |
157 | void run_job(int index) | |
158 | { | |
159 | struct job *job = &jobs[index]; | |
160 | ||
161 | pid_t pid = fork(); | |
162 | if(pid == 0){ /* child */ | |
163 | int i = 0; | |
164 | while(1){ | |
165 | tomd_p("hallo"); | |
166 | if(job->args[i] == NULL){ | |
167 | tomd_p("arg [%d] is NULL", i); | |
168 | break; | |
169 | }else{ | |
170 | tomd_p("arg [%d] is '%s'", i, job->args[i]); | |
171 | } | |
172 | i++; | |
173 | } | |
174 | execvp(job->cmd, job->args); | |
175 | tomd_p("execvp for '%s' failed", job->cmd); | |
176 | perror(""); | |
177 | exit(EXIT_FAILURE); | |
178 | }else{ /* parent */ | |
179 | tomd_p("forked [%d] to run '%s'", pid, job->cmd); | |
180 | } | |
181 | } | |
182 | ||
183 | void run_jobs(void) | |
184 | { | |
185 | int i; | |
186 | for(i = 0; | |
187 | i < MAX_JOBS; | |
188 | i++){ | |
189 | if(jobs[i].cmd == NULL){ | |
190 | tomd_p("out of jobs"); | |
191 | return; | |
192 | }else{ | |
193 | tomd_p("running job [%d] '%s'", i, jobs[i].cmd); | |
194 | run_job(i); | |
195 | } | |
196 | } | |
197 | } |