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