]>
Commit | Line | Data |
---|---|---|
60918275 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | #include <assert.h> | |
1ffba6fe | 4 | #include <errno.h> |
60918275 LP |
5 | |
6 | #include "macro.h" | |
7 | #include "job.h" | |
8 | ||
9 | Job* job_new(Manager *m, JobType type, Name *name) { | |
10 | Job *j; | |
11 | ||
12 | assert(m); | |
13 | assert(type < _JOB_TYPE_MAX); | |
14 | assert(name); | |
15 | ||
16 | if (!(j = new0(Job, 1))) | |
17 | return NULL; | |
18 | ||
19 | j->manager = m; | |
20 | j->id = m->current_job_id++; | |
21 | j->type = type; | |
22 | j->name = name; | |
23 | ||
e5b5ae50 | 24 | /* We don't link it here, that's what job_dependency() is for */ |
60918275 LP |
25 | |
26 | return j; | |
27 | } | |
28 | ||
60918275 LP |
29 | void job_free(Job *j) { |
30 | assert(j); | |
31 | ||
32 | /* Detach from next 'bigger' objects */ | |
60918275 | 33 | if (j->linked) { |
11dd41ce LP |
34 | if (j->name->meta.job == j) |
35 | j->name->meta.job = NULL; | |
60918275 LP |
36 | |
37 | hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id)); | |
5cb5a6ff | 38 | j->linked = false; |
60918275 LP |
39 | } |
40 | ||
5cb5a6ff | 41 | /* Detach from next 'smaller' objects */ |
302d0040 | 42 | manager_transaction_unlink_job(j->manager, j); |
60918275 LP |
43 | |
44 | free(j); | |
45 | } | |
a66d02c3 | 46 | |
e5b5ae50 LP |
47 | JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) { |
48 | JobDependency *l; | |
49 | ||
50 | assert(object); | |
51 | ||
52 | /* Adds a new job link, which encodes that the 'subject' job | |
53 | * needs the 'object' job in some way. If 'subject' is NULL | |
54 | * this means the 'anchor' job (i.e. the one the user | |
55 | * explcitily asked for) is the requester. */ | |
56 | ||
ceed3570 | 57 | if (!(l = new0(JobDependency, 1))) |
e5b5ae50 LP |
58 | return NULL; |
59 | ||
60 | l->subject = subject; | |
61 | l->object = object; | |
62 | l->matters = matters; | |
63 | ||
64 | if (subject) { | |
65 | l->subject_next = subject->subject_list; | |
66 | subject->subject_list = l; | |
67 | } else { | |
68 | l->subject_next = object->manager->transaction_anchor; | |
69 | object->manager->transaction_anchor = l; | |
70 | } | |
71 | ||
72 | if (l->subject_next) | |
73 | l->subject_next->subject_prev = l; | |
74 | l->subject_prev = NULL; | |
75 | ||
76 | if ((l->object_next = object->object_list)) | |
77 | l->object_next->object_prev = l; | |
78 | l->object_prev = NULL; | |
79 | object->object_list = l; | |
80 | ||
81 | return l; | |
82 | } | |
83 | ||
84 | void job_dependency_free(JobDependency *l) { | |
85 | assert(l); | |
86 | ||
87 | if (l->subject_prev) | |
88 | l->subject_prev->subject_next = l->subject_next; | |
89 | else if (l->subject) | |
90 | l->subject->subject_list = l->subject_next; | |
91 | else | |
92 | l->object->manager->transaction_anchor = l->subject_next; | |
93 | ||
94 | if (l->subject_next) | |
95 | l->subject_next->subject_prev = l->subject_prev; | |
96 | ||
97 | if (l->object_prev) | |
98 | l->object_prev->object_next = l->object_next; | |
99 | else | |
100 | l->object->object_list = l->object_next; | |
101 | ||
102 | if (l->object_next) | |
103 | l->object_next->object_prev = l->object_prev; | |
104 | ||
105 | free(l); | |
106 | } | |
107 | ||
108 | void job_dependency_delete(Job *subject, Job *object, bool *matters) { | |
109 | JobDependency *l; | |
110 | ||
111 | assert(object); | |
112 | ||
113 | for (l = object->object_list; l; l = l->object_next) { | |
114 | assert(l->object == object); | |
115 | ||
116 | if (l->subject == subject) | |
117 | break; | |
118 | } | |
119 | ||
120 | if (!l) { | |
121 | if (matters) | |
122 | *matters = false; | |
123 | return; | |
124 | } | |
125 | ||
126 | if (matters) | |
127 | *matters = l->matters; | |
128 | ||
129 | job_dependency_free(l); | |
130 | } | |
131 | ||
1ffba6fe | 132 | const char* job_type_to_string(JobType t) { |
a66d02c3 LP |
133 | |
134 | static const char* const job_type_table[_JOB_TYPE_MAX] = { | |
42f4e3c4 | 135 | [JOB_START] = "start", |
5cb5a6ff | 136 | [JOB_VERIFY_ACTIVE] = "verify-active", |
42f4e3c4 | 137 | [JOB_STOP] = "stop", |
42f4e3c4 | 138 | [JOB_RELOAD] = "reload", |
e5b5ae50 | 139 | [JOB_RELOAD_OR_START] = "reload-or-start", |
42f4e3c4 LP |
140 | [JOB_RESTART] = "restart", |
141 | [JOB_TRY_RESTART] = "try-restart", | |
a66d02c3 LP |
142 | }; |
143 | ||
1ffba6fe LP |
144 | if (t < 0 || t >= _JOB_TYPE_MAX) |
145 | return "n/a"; | |
146 | ||
147 | return job_type_table[t]; | |
148 | } | |
149 | ||
150 | void job_dump(Job *j, FILE*f, const char *prefix) { | |
151 | ||
a66d02c3 | 152 | static const char* const job_state_table[_JOB_STATE_MAX] = { |
42f4e3c4 | 153 | [JOB_WAITING] = "waiting", |
5cb5a6ff | 154 | [JOB_RUNNING] = "running" |
a66d02c3 LP |
155 | }; |
156 | ||
157 | assert(j); | |
158 | assert(f); | |
159 | ||
ceed3570 LP |
160 | fprintf(f, |
161 | "%sJob %u:\n" | |
162 | "%s\tAction: %s → %s\n" | |
5cb5a6ff LP |
163 | "%s\tState: %s\n" |
164 | "%s\tForced: %s\n", | |
ceed3570 | 165 | prefix, j->id, |
1ffba6fe | 166 | prefix, name_id(j->name), job_type_to_string(j->type), |
5cb5a6ff LP |
167 | prefix, job_state_table[j->state], |
168 | prefix, yes_no(j->forced)); | |
a66d02c3 | 169 | } |
e5b5ae50 LP |
170 | |
171 | bool job_is_anchor(Job *j) { | |
172 | JobDependency *l; | |
173 | ||
174 | assert(j); | |
175 | ||
176 | for (l = j->object_list; l; l = l->object_next) | |
177 | if (!l->subject) | |
178 | return true; | |
179 | ||
180 | return false; | |
181 | } | |
1ffba6fe LP |
182 | |
183 | static bool types_match(JobType a, JobType b, JobType c, JobType d) { | |
184 | return | |
185 | (a == c && b == d) || | |
186 | (a == d && b == c); | |
187 | } | |
188 | ||
189 | int job_type_merge(JobType *a, JobType b) { | |
190 | if (*a == b) | |
191 | return 0; | |
192 | ||
193 | /* Merging is associative! a merged with b merged with c is | |
194 | * the same as a merged with c merged with b. */ | |
195 | ||
196 | /* Mergeability is transitive! if a can be merged with b and b | |
197 | * with c then a also with c */ | |
198 | ||
199 | /* Also, if a merged with b cannot be merged with c, then | |
200 | * either a or b cannot be merged with c either */ | |
201 | ||
5cb5a6ff | 202 | if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE)) |
1ffba6fe LP |
203 | *a = JOB_START; |
204 | else if (types_match(*a, b, JOB_START, JOB_RELOAD) || | |
205 | types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) || | |
5cb5a6ff | 206 | types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) || |
1ffba6fe LP |
207 | types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START)) |
208 | *a = JOB_RELOAD_OR_START; | |
209 | else if (types_match(*a, b, JOB_START, JOB_RESTART) || | |
210 | types_match(*a, b, JOB_START, JOB_TRY_RESTART) || | |
5cb5a6ff | 211 | types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) || |
1ffba6fe LP |
212 | types_match(*a, b, JOB_RELOAD, JOB_RESTART) || |
213 | types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) || | |
214 | types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) || | |
215 | types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART)) | |
216 | *a = JOB_RESTART; | |
5cb5a6ff | 217 | else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD)) |
1ffba6fe | 218 | *a = JOB_RELOAD; |
5cb5a6ff | 219 | else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) || |
1ffba6fe LP |
220 | types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART)) |
221 | *a = JOB_TRY_RESTART; | |
222 | else | |
223 | return -EEXIST; | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
5cb5a6ff | 228 | bool job_type_is_mergeable(JobType a, JobType b) { |
1ffba6fe LP |
229 | return job_type_merge(&a, b) >= 0; |
230 | } | |
231 | ||
232 | bool job_type_is_superset(JobType a, JobType b) { | |
233 | ||
5cb5a6ff LP |
234 | /* Checks whether operation a is a "superset" of b in its |
235 | * actions */ | |
1ffba6fe LP |
236 | |
237 | if (a == b) | |
238 | return true; | |
239 | ||
240 | switch (a) { | |
241 | case JOB_START: | |
5cb5a6ff | 242 | return b == JOB_VERIFY_ACTIVE; |
1ffba6fe LP |
243 | |
244 | case JOB_RELOAD: | |
5cb5a6ff LP |
245 | return |
246 | b == JOB_VERIFY_ACTIVE; | |
1ffba6fe LP |
247 | |
248 | case JOB_RELOAD_OR_START: | |
249 | return | |
250 | b == JOB_RELOAD || | |
5cb5a6ff LP |
251 | b == JOB_START || |
252 | b == JOB_VERIFY_ACTIVE; | |
1ffba6fe LP |
253 | |
254 | case JOB_RESTART: | |
255 | return | |
256 | b == JOB_START || | |
5cb5a6ff | 257 | b == JOB_VERIFY_ACTIVE || |
1ffba6fe LP |
258 | b == JOB_RELOAD || |
259 | b == JOB_RELOAD_OR_START || | |
260 | b == JOB_TRY_RESTART; | |
261 | ||
262 | case JOB_TRY_RESTART: | |
263 | return | |
5cb5a6ff | 264 | b == JOB_VERIFY_ACTIVE || |
1ffba6fe LP |
265 | b == JOB_RELOAD; |
266 | default: | |
267 | return false; | |
268 | ||
269 | } | |
270 | } | |
e094e853 LP |
271 | |
272 | bool job_type_is_conflicting(JobType a, JobType b) { | |
cd2dbd7d LP |
273 | assert(a >= 0 && a < _JOB_TYPE_MAX); |
274 | assert(b >= 0 && b < _JOB_TYPE_MAX); | |
e094e853 | 275 | |
5cb5a6ff | 276 | return (a == JOB_STOP) != (b == JOB_STOP); |
e094e853 | 277 | } |
cd2dbd7d | 278 | |
5cb5a6ff | 279 | bool job_is_runnable(Job *j) { |
034c6ed7 | 280 | Iterator i; |
5cb5a6ff LP |
281 | Name *other; |
282 | ||
283 | assert(j); | |
284 | assert(j->linked); | |
285 | ||
286 | /* Checks whether there is any job running for the names this | |
287 | * job needs to be running after (in the case of a 'positive' | |
288 | * job type) or before (in the case of a 'negative' job type | |
289 | * . */ | |
290 | ||
291 | if (j->type == JOB_START || | |
292 | j->type == JOB_VERIFY_ACTIVE || | |
293 | j->type == JOB_RELOAD || | |
294 | j->type == JOB_RELOAD_OR_START) { | |
295 | ||
296 | /* Immediate result is that the job is or might be | |
297 | * started. In this case lets wait for the | |
298 | * dependencies, regardless whether they are | |
299 | * starting or stopping something. */ | |
300 | ||
034c6ed7 | 301 | SET_FOREACH(other, j->name->meta.dependencies[NAME_AFTER], i) |
5cb5a6ff LP |
302 | if (other->meta.job) |
303 | return false; | |
304 | } | |
305 | ||
306 | /* Also, if something else is being stopped and we should | |
307 | * change state after it, then lets wait. */ | |
308 | ||
034c6ed7 | 309 | SET_FOREACH(other, j->name->meta.dependencies[NAME_BEFORE], i) |
5cb5a6ff LP |
310 | if (other->meta.job && |
311 | (other->meta.job->type == JOB_STOP || | |
312 | other->meta.job->type == JOB_RESTART || | |
313 | other->meta.job->type == JOB_TRY_RESTART)) | |
314 | return false; | |
315 | ||
316 | /* This means that for a service a and a service b where b | |
317 | * shall be started after a: | |
318 | * | |
319 | * start a + start b → 1st step start a, 2nd step start b | |
320 | * start a + stop b → 1st step stop b, 2nd step start a | |
321 | * stop a + start b → 1st step stop a, 2nd step start b | |
322 | * stop a + stop b → 1st step stop b, 2nd step stop a | |
323 | * | |
324 | * This has the side effect that restarts are properly | |
325 | * synchronized too. */ | |
326 | ||
327 | return true; | |
328 | } | |
329 | ||
330 | int job_run_and_invalidate(Job *j) { | |
331 | int r; | |
332 | assert(j); | |
333 | ||
034c6ed7 LP |
334 | if (j->in_run_queue) { |
335 | LIST_REMOVE(Job, run_queue, j->manager->run_queue, j); | |
336 | j->in_run_queue = false; | |
337 | } | |
5cb5a6ff LP |
338 | |
339 | if (j->state != JOB_WAITING) | |
340 | return 0; | |
341 | ||
034c6ed7 LP |
342 | if (!job_is_runnable(j)) |
343 | return -EAGAIN; | |
344 | ||
83c60c9f LP |
345 | j->state = JOB_RUNNING; |
346 | ||
5cb5a6ff LP |
347 | switch (j->type) { |
348 | ||
349 | case JOB_START: | |
350 | r = name_start(j->name); | |
351 | if (r == -EBADR) | |
352 | r = 0; | |
353 | break; | |
354 | ||
355 | case JOB_VERIFY_ACTIVE: { | |
356 | NameActiveState t = name_active_state(j->name); | |
357 | if (NAME_IS_ACTIVE_OR_RELOADING(t)) | |
358 | r = -EALREADY; | |
359 | else if (t == NAME_ACTIVATING) | |
360 | r = -EAGAIN; | |
361 | else | |
362 | r = -ENOEXEC; | |
363 | break; | |
364 | } | |
365 | ||
366 | case JOB_STOP: | |
367 | r = name_stop(j->name); | |
368 | break; | |
369 | ||
370 | case JOB_RELOAD: | |
371 | r = name_reload(j->name); | |
372 | break; | |
373 | ||
374 | case JOB_RELOAD_OR_START: | |
375 | if (name_active_state(j->name) == NAME_ACTIVE) | |
376 | r = name_reload(j->name); | |
377 | else | |
378 | r = name_start(j->name); | |
379 | break; | |
380 | ||
381 | case JOB_RESTART: { | |
382 | NameActiveState t = name_active_state(j->name); | |
383 | if (t == NAME_INACTIVE || t == NAME_ACTIVATING) { | |
384 | j->type = JOB_START; | |
385 | r = name_start(j->name); | |
386 | } else | |
387 | r = name_stop(j->name); | |
388 | break; | |
389 | } | |
390 | ||
391 | case JOB_TRY_RESTART: { | |
392 | NameActiveState t = name_active_state(j->name); | |
393 | if (t == NAME_INACTIVE || t == NAME_DEACTIVATING) | |
394 | r = -ENOEXEC; | |
395 | else if (t == NAME_ACTIVATING) { | |
396 | j->type = JOB_START; | |
397 | r = name_start(j->name); | |
398 | } else | |
399 | r = name_stop(j->name); | |
400 | break; | |
401 | } | |
402 | ||
403 | default: | |
404 | ; | |
405 | } | |
406 | ||
83c60c9f | 407 | if (r == -EALREADY) |
5cb5a6ff | 408 | r = job_finish_and_invalidate(j, true); |
83c60c9f LP |
409 | else if (r == -EAGAIN) { |
410 | j->state = JOB_WAITING; | |
411 | return -EAGAIN; | |
412 | } else if (r < 0) | |
5cb5a6ff LP |
413 | r = job_finish_and_invalidate(j, false); |
414 | ||
415 | return r; | |
416 | } | |
417 | ||
418 | int job_finish_and_invalidate(Job *j, bool success) { | |
419 | Name *n; | |
5cb5a6ff LP |
420 | Name *other; |
421 | NameType t; | |
034c6ed7 | 422 | Iterator i; |
5cb5a6ff LP |
423 | |
424 | assert(j); | |
425 | ||
034c6ed7 | 426 | /* Patch restart jobs so that they become normal start jobs */ |
5cb5a6ff LP |
427 | if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) { |
428 | j->state = JOB_RUNNING; | |
429 | j->type = JOB_START; | |
034c6ed7 LP |
430 | job_schedule_run(j); |
431 | return 0; | |
5cb5a6ff LP |
432 | } |
433 | ||
434 | n = j->name; | |
435 | t = j->type; | |
436 | job_free(j); | |
437 | ||
438 | /* Fail depending jobs on failure */ | |
439 | if (!success) { | |
440 | ||
441 | if (t == JOB_START || | |
442 | t == JOB_VERIFY_ACTIVE || | |
443 | t == JOB_RELOAD_OR_START) { | |
444 | ||
034c6ed7 | 445 | SET_FOREACH(other, n->meta.dependencies[NAME_REQUIRED_BY], i) |
5cb5a6ff LP |
446 | if (other->meta.job && |
447 | (other->meta.type == JOB_START || | |
448 | other->meta.type == JOB_VERIFY_ACTIVE || | |
449 | other->meta.type == JOB_RELOAD_OR_START)) | |
450 | job_finish_and_invalidate(other->meta.job, false); | |
451 | ||
034c6ed7 | 452 | SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUIRED_BY], i) |
5cb5a6ff LP |
453 | if (other->meta.job && |
454 | !other->meta.job->forced && | |
455 | (other->meta.type == JOB_START || | |
456 | other->meta.type == JOB_VERIFY_ACTIVE || | |
457 | other->meta.type == JOB_RELOAD_OR_START)) | |
458 | job_finish_and_invalidate(other->meta.job, false); | |
459 | ||
460 | } else if (t == JOB_STOP) { | |
461 | ||
034c6ed7 | 462 | SET_FOREACH(other, n->meta.dependencies[NAME_CONFLICTS], i) |
5cb5a6ff LP |
463 | if (other->meta.job && |
464 | (t == JOB_START || | |
465 | t == JOB_VERIFY_ACTIVE || | |
466 | t == JOB_RELOAD_OR_START)) | |
467 | job_finish_and_invalidate(other->meta.job, false); | |
468 | } | |
469 | } | |
470 | ||
471 | /* Try to start the next jobs that can be started */ | |
034c6ed7 | 472 | SET_FOREACH(other, n->meta.dependencies[NAME_AFTER], i) |
5cb5a6ff | 473 | if (other->meta.job) |
034c6ed7 LP |
474 | job_schedule_run(other->meta.job); |
475 | SET_FOREACH(other, n->meta.dependencies[NAME_BEFORE], i) | |
5cb5a6ff | 476 | if (other->meta.job) |
034c6ed7 | 477 | job_schedule_run(other->meta.job); |
5cb5a6ff LP |
478 | |
479 | return 0; | |
480 | } | |
034c6ed7 LP |
481 | |
482 | void job_schedule_run(Job *j) { | |
483 | assert(j); | |
484 | assert(j->linked); | |
485 | ||
486 | if (j->in_run_queue) | |
487 | return; | |
488 | ||
489 | LIST_PREPEND(Job, run_queue, j->manager->run_queue, j); | |
490 | j->in_run_queue = true; | |
491 | } |